2006-02-24 [wwp] 2.0.0cvs81
[claws.git] / src / stock_pixmap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21 #include <glib.h>
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkimage.h>
24 #include <string.h>
25 #include <dirent.h>
26
27 #include "stock_pixmap.h"
28 #include "gtkutils.h"
29 #include "utils.h"
30 #include "prefs_common.h"
31
32 #include "pixmaps/address.xpm"
33 #include "pixmaps/book.xpm"
34 #include "pixmaps/category.xpm"
35 #include "pixmaps/checkbox_off.xpm"
36 #include "pixmaps/checkbox_on.xpm"
37 #include "pixmaps/clip.xpm"
38 #include "pixmaps/clipkey.xpm"
39 #include "pixmaps/complete.xpm"
40 #include "pixmaps/continue.xpm"
41 #include "pixmaps/deleted.xpm"
42 #include "pixmaps/error.xpm"
43 #include "pixmaps/forwarded.xpm"
44 #include "pixmaps/group.xpm"
45 #include "pixmaps/interface.xpm"
46 #include "pixmaps/jpilot.xpm"
47 #include "pixmaps/key.xpm"
48 #include "pixmaps/ldap.xpm"
49 #include "pixmaps/linewrap.xpm"
50 #include "pixmaps/linewrapcurrent.xpm"
51 #include "pixmaps/mark.xpm"
52 #include "pixmaps/locked.xpm"
53 #include "pixmaps/new.xpm"
54 #include "pixmaps/replied.xpm"
55 #include "pixmaps/close.xpm"
56 #include "pixmaps/down_arrow.xpm"
57 #include "pixmaps/up_arrow.xpm"
58 #include "pixmaps/exec.xpm"
59 #include "pixmaps/mail.xpm"
60 #include "pixmaps/mail_attach.xpm"
61 #include "pixmaps/mail_compose.xpm"
62 #include "pixmaps/mail_forward.xpm"
63 #include "pixmaps/mail_receive.xpm"
64 #include "pixmaps/mail_receive_all.xpm"
65 #include "pixmaps/mail_reply.xpm"
66 #include "pixmaps/mail_reply_to_all.xpm"
67 #include "pixmaps/mail_reply_to_author.xpm"
68 #include "pixmaps/mail_send.xpm"
69 #include "pixmaps/mail_send_queue.xpm"
70 #include "pixmaps/news_compose.xpm"
71 #include "pixmaps/paste.xpm"
72 #include "pixmaps/preferences.xpm"
73 #include "pixmaps/properties.xpm"
74 #include "pixmaps/sylpheed_icon.xpm"
75 #include "pixmaps/sylpheed_logo.xpm"
76 #include "pixmaps/address_book.xpm"
77 #include "pixmaps/unread.xpm"
78 #include "pixmaps/read.xpm"
79 #include "pixmaps/vcard.xpm"
80 #include "pixmaps/ignorethread.xpm"
81 #include "pixmaps/online.xpm"
82 #include "pixmaps/offline.xpm"
83 #include "pixmaps/notice_warn.xpm"
84 #include "pixmaps/notice_error.xpm"
85 #include "pixmaps/notice_note.xpm"
86 #include "pixmaps/quicksearch.xpm"
87 #include "pixmaps/clip_gpg_signed.xpm"
88 #include "pixmaps/gpg_signed.xpm"
89 #include "pixmaps/mime_text_plain.xpm"
90 #include "pixmaps/mime_text_html.xpm"
91 #include "pixmaps/mime_application.xpm"
92 #include "pixmaps/mime_image.xpm"
93 #include "pixmaps/mime_audio.xpm"
94 #include "pixmaps/mime_text_enriched.xpm"
95 #include "pixmaps/mime_unknown.xpm"
96 #include "pixmaps/privacy_signed.xpm"
97 #include "pixmaps/privacy_passed.xpm"
98 #include "pixmaps/privacy_failed.xpm"
99 #include "pixmaps/privacy_unknown.xpm"
100 #include "pixmaps/privacy_expired.xpm"
101 #include "pixmaps/privacy_warn.xpm"                 
102 #include "pixmaps/privacy_emblem_encrypted.xpm"
103 #include "pixmaps/privacy_emblem_signed.xpm"
104 #include "pixmaps/privacy_emblem_passed.xpm"
105 #include "pixmaps/privacy_emblem_failed.xpm"
106 #include "pixmaps/privacy_emblem_warn.xpm"
107 #include "pixmaps/mime_message.xpm"                  
108 #include "pixmaps/address_search.xpm"
109 #include "pixmaps/check_spelling.xpm"
110
111 #include "pixmaps/dir_close.xpm"
112 #include "pixmaps/dir_open.xpm"
113 #include "pixmaps/dir_open_hrm.xpm"
114 #include "pixmaps/inbox.xpm"
115 #include "pixmaps/inbox_hrm.xpm"
116 #include "pixmaps/outbox.xpm"
117 #include "pixmaps/outbox_hrm.xpm"
118 #include "pixmaps/trash.xpm"
119 #include "pixmaps/trash_btn.xpm"
120 #include "pixmaps/trash_hrm.xpm"
121 #include "pixmaps/drafts_close.xpm"
122 #include "pixmaps/drafts_open.xpm"
123 #include "pixmaps/dir_close_mark.xpm"
124 #include "pixmaps/dir_open_mark.xpm"
125 #include "pixmaps/dir_open_hrm_mark.xpm"
126 #include "pixmaps/inbox_mark.xpm"
127 #include "pixmaps/inbox_hrm_mark.xpm"
128 #include "pixmaps/outbox_mark.xpm"
129 #include "pixmaps/outbox_hrm_mark.xpm"
130 #include "pixmaps/trash_mark.xpm"
131 #include "pixmaps/trash_hrm_mark.xpm"
132 #include "pixmaps/drafts_close_mark.xpm"
133 #include "pixmaps/drafts_open_mark.xpm"
134 #include "pixmaps/dir_noselect.xpm"
135 #include "pixmaps/empty.xpm"
136
137 typedef struct _StockPixmapData StockPixmapData;
138
139 struct _StockPixmapData
140 {
141         gchar **data;
142         GdkPixmap *pixmap;
143         GdkBitmap *mask;
144         gchar *file;
145         gchar *icon_path;
146         GdkPixbuf *pixbuf;
147 };
148
149 typedef struct _OverlayData OverlayData;
150
151 struct _OverlayData
152 {
153         GdkPixmap *base_pixmap;
154         GdkBitmap *base_mask;
155         GdkPixmap *overlay_pixmap;
156         GdkBitmap *overlay_mask;
157         guint base_height;
158         guint base_width;
159         guint overlay_height;
160         guint overlay_width;
161         OverlayPosition position;
162         gint border_x;
163         gint border_y;
164 };
165
166 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname);
167
168 static StockPixmapData pixmaps[] =
169 {
170         {address_xpm                            , NULL, NULL, "address", NULL},
171         {address_book_xpm                       , NULL, NULL, "address_book", NULL},
172         {address_search_xpm                     , NULL, NULL, "address_search", NULL},
173         {book_xpm                               , NULL, NULL, "book", NULL},
174         {category_xpm                           , NULL, NULL, "category", NULL},
175         {checkbox_off_xpm                       , NULL, NULL, "checkbox_off", NULL},
176         {checkbox_on_xpm                        , NULL, NULL, "checkbox_on", NULL},
177         {check_spelling_xpm                     , NULL, NULL, "check_spelling", NULL},
178         {clip_xpm                               , NULL, NULL, "clip", NULL},
179         {clipkey_xpm                            , NULL, NULL, "clipkey", NULL},
180         {clip_gpg_signed_xpm                    , NULL, NULL, "clip_gpg_signed", NULL},
181         {close_xpm                              , NULL, NULL, "close", NULL},
182         {complete_xpm                           , NULL, NULL, "complete", NULL},
183         {continue_xpm                           , NULL, NULL, "continue", NULL},
184         {deleted_xpm                            , NULL, NULL, "deleted", NULL},
185         {dir_close_xpm                          , NULL, NULL, "dir_close", NULL},
186         {dir_close_xpm                          , NULL, NULL, "dir_close_hrm", NULL},
187         {dir_open_xpm                           , NULL, NULL, "dir_open", NULL},
188         {dir_open_hrm_xpm                       , NULL, NULL, "dir_open_hrm", NULL},
189         {dir_close_mark_xpm                     , NULL, NULL, "dir_close_mark", NULL},
190         {dir_close_mark_xpm                     , NULL, NULL, "dir_close_hrm_mark", NULL},
191         {dir_open_mark_xpm                      , NULL, NULL, "dir_open_mark", NULL},
192         {dir_open_hrm_mark_xpm                  , NULL, NULL, "dir_open_hrm_mark", NULL},
193         {down_arrow_xpm                         , NULL, NULL, "down_arrow", NULL},
194         {up_arrow_xpm                           , NULL, NULL, "up_arrow", NULL},
195         {mail_compose_xpm                       , NULL, NULL, "edit_extern", NULL},
196         {error_xpm                              , NULL, NULL, "error", NULL},
197         {exec_xpm                               , NULL, NULL, "exec", NULL},
198         {forwarded_xpm                          , NULL, NULL, "forwarded", NULL},
199         {group_xpm                              , NULL, NULL, "group", NULL},
200         {ignorethread_xpm                       , NULL, NULL, "ignorethread", NULL},
201         {inbox_xpm                              , NULL, NULL, "inbox_close", NULL},
202         {inbox_hrm_xpm                          , NULL, NULL, "inbox_close_hrm", NULL},
203         {inbox_xpm                              , NULL, NULL, "inbox_open", NULL},
204         {inbox_hrm_xpm                          , NULL, NULL, "inbox_open_hrm", NULL},
205         {inbox_mark_xpm                         , NULL, NULL, "inbox_close_mark", NULL},
206         {inbox_hrm_mark_xpm                     , NULL, NULL, "inbox_close_hrm_mark", NULL},
207         {inbox_mark_xpm                         , NULL, NULL, "inbox_open_mark", NULL},
208         {inbox_hrm_mark_xpm                     , NULL, NULL, "inbox_open_hrm_mark", NULL},
209         {paste_xpm                              , NULL, NULL, "insert_file", NULL},
210         {interface_xpm                          , NULL, NULL, "interface", NULL},
211         {jpilot_xpm                             , NULL, NULL, "jpilot", NULL},
212         {key_xpm                                , NULL, NULL, "key", NULL},
213         {ldap_xpm                               , NULL, NULL, "ldap", NULL},
214         {linewrapcurrent_xpm                    , NULL, NULL, "linewrapcurrent", NULL},
215         {linewrap_xpm                           , NULL, NULL, "linewrap", NULL},
216         {locked_xpm                             , NULL, NULL, "locked", NULL},
217         {mail_xpm                               , NULL, NULL, "mail", NULL},
218         {mail_attach_xpm                        , NULL, NULL, "mail_attach", NULL},
219         {mail_compose_xpm                       , NULL, NULL, "mail_compose", NULL},
220         {mail_forward_xpm                       , NULL, NULL, "mail_forward", NULL},
221         {mail_receive_xpm                       , NULL, NULL, "mail_receive", NULL},
222         {mail_receive_all_xpm                   , NULL, NULL, "mail_receive_all", NULL},
223         {mail_reply_xpm                         , NULL, NULL, "mail_reply", NULL},
224         {mail_reply_to_all_xpm                  , NULL, NULL, "mail_reply_to_all", NULL},
225         {mail_reply_to_author_xpm               , NULL, NULL, "mail_reply_to_author", NULL},
226         {mail_send_xpm                          , NULL, NULL, "mail_send", NULL},
227         {mail_send_queue_xpm                    , NULL, NULL, "mail_send_queue", NULL},
228         {mail_xpm                               , NULL, NULL, "mail_sign", NULL},
229         {mark_xpm                               , NULL, NULL, "mark", NULL},
230         {new_xpm                                , NULL, NULL, "new", NULL},
231         {news_compose_xpm                       , NULL, NULL, "news_compose", NULL},
232         {outbox_xpm                             , NULL, NULL, "outbox_close", NULL},
233         {outbox_hrm_xpm                         , NULL, NULL, "outbox_close_hrm", NULL},
234         {outbox_xpm                             , NULL, NULL, "outbox_open", NULL},
235         {outbox_hrm_xpm                         , NULL, NULL, "outbox_open_hrm", NULL},
236         {outbox_mark_xpm                        , NULL, NULL, "outbox_close_mark", NULL},
237         {outbox_hrm_mark_xpm                    , NULL, NULL, "outbox_close_hrm_mark", NULL},
238         {outbox_mark_xpm                        , NULL, NULL, "outbox_open_mark", NULL},
239         {outbox_hrm_mark_xpm                    , NULL, NULL, "outbox_open_hrm_mark", NULL},
240         {replied_xpm                            , NULL, NULL, "replied", NULL},
241         {paste_xpm                              , NULL, NULL, "paste", NULL},
242         {preferences_xpm                        , NULL, NULL, "preferences", NULL},
243         {properties_xpm                         , NULL, NULL, "properties", NULL},
244         {outbox_xpm                             , NULL, NULL, "queue_close", NULL},
245         {outbox_hrm_xpm                         , NULL, NULL, "queue_close_hrm", NULL},
246         {outbox_xpm                             , NULL, NULL, "queue_open", NULL},
247         {outbox_hrm_xpm                         , NULL, NULL, "queue_open_hrm", NULL},
248         {trash_xpm                              , NULL, NULL, "trash_open", NULL},
249         {trash_hrm_xpm                          , NULL, NULL, "trash_open_hrm", NULL},
250         {trash_xpm                              , NULL, NULL, "trash_close", NULL},
251         {trash_hrm_xpm                          , NULL, NULL, "trash_close_hrm", NULL},
252         {outbox_mark_xpm                        , NULL, NULL, "queue_close_mark", NULL},
253         {outbox_hrm_mark_xpm                    , NULL, NULL, "queue_close_hrm_mark", NULL},
254         {outbox_mark_xpm                        , NULL, NULL, "queue_open_mark", NULL},
255         {outbox_hrm_mark_xpm                    , NULL, NULL, "queue_open_hrm_mark", NULL},
256         {trash_mark_xpm                         , NULL, NULL, "trash_open_mark", NULL},
257         {trash_hrm_mark_xpm                     , NULL, NULL, "trash_open_hrm_mark", NULL},
258         {trash_mark_xpm                         , NULL, NULL, "trash_close_mark", NULL},
259         {trash_hrm_mark_xpm                     , NULL, NULL, "trash_close_hrm_mark", NULL},
260         {unread_xpm                             , NULL, NULL, "unread", NULL},
261         {vcard_xpm                              , NULL, NULL, "vcard", NULL},
262         {online_xpm                             , NULL, NULL, "online", NULL},
263         {offline_xpm                            , NULL, NULL, "offline", NULL},
264         {notice_warn_xpm                        , NULL, NULL, "notice_warn",  NULL},
265         {notice_error_xpm                       , NULL, NULL, "notice_error",  NULL},
266         {notice_note_xpm                        , NULL, NULL, "notice_note",  NULL},
267         {quicksearch_xpm                        , NULL, NULL, "quicksearch",  NULL},
268         {gpg_signed_xpm                         , NULL, NULL, "gpg_signed", NULL},
269         {drafts_close_xpm                       , NULL, NULL, "drafts_close", NULL},
270         {drafts_open_xpm                        , NULL, NULL, "drafts_open", NULL},
271         {drafts_close_mark_xpm                  , NULL, NULL, "drafts_close_mark", NULL},
272         {drafts_open_mark_xpm                   , NULL, NULL, "drafts_open_mark", NULL},
273         {mime_text_plain_xpm                    , NULL, NULL, "mime_text_plain", NULL},
274         {mime_text_html_xpm                     , NULL, NULL, "mime_text_html", NULL},
275         {mime_application_xpm                   , NULL, NULL, "mime_application", NULL},
276         {mime_image_xpm                         , NULL, NULL, "mime_image", NULL},
277         {mime_audio_xpm                         , NULL, NULL, "mime_audio", NULL},
278         {mime_text_enriched_xpm                 , NULL, NULL, "mime_text_enriched", NULL},
279         {mime_unknown_xpm                       , NULL, NULL, "mime_unknown", NULL},    
280         {privacy_signed_xpm                     , NULL, NULL, "privacy_signed", NULL},
281         {privacy_passed_xpm                     , NULL, NULL, "privacy_passed", NULL},
282         {privacy_failed_xpm                     , NULL, NULL, "privacy_failed", NULL},  
283         {privacy_unknown_xpm                    , NULL, NULL, "privacy_unknown", NULL},
284         {privacy_expired_xpm                    , NULL, NULL, "privacy_expired", NULL},
285         {privacy_warn_xpm                       , NULL, NULL, "privacy_warn", NULL},
286         {privacy_emblem_encrypted_xpm           , NULL, NULL, "privacy_emblem_encrypted", NULL},
287         {privacy_emblem_signed_xpm              , NULL, NULL, "privacy_emblem_signed", NULL},
288         {privacy_emblem_passed_xpm              , NULL, NULL, "privacy_emblem_passed", NULL},
289         {privacy_emblem_failed_xpm              , NULL, NULL, "privacy_emblem_failed", NULL},   
290         {privacy_emblem_warn_xpm                , NULL, NULL, "privacy_emblem_warn", NULL},
291         {mime_message_xpm                       , NULL, NULL, "mime_message", NULL},
292         {sylpheed_icon_xpm                      , NULL, NULL, "sylpheed_icon", NULL},
293         {read_xpm                               , NULL, NULL, "read", NULL},
294         {trash_btn_xpm                          , NULL, NULL, "trash_btn", NULL},
295         {sylpheed_logo_xpm                      , NULL, NULL, "sylpheed_logo", NULL},
296         {dir_noselect_xpm                       , NULL, NULL, "dir_noselect" , NULL},
297         {empty_xpm                              , NULL, NULL, "empty" , NULL}
298 };
299
300 /* return newly constructed GtkPixmap from GdkPixmap */
301 GtkWidget *stock_pixmap_widget(GtkWidget *window, StockPixmap icon)
302 {
303         GdkPixmap *pixmap;
304         GdkBitmap *mask;
305
306         g_return_val_if_fail(window != NULL, NULL);
307         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
308
309         if (stock_pixmap_gdk(window, icon, &pixmap, &mask) != -1)
310                 return gtk_image_new_from_pixmap(pixmap, mask);
311         
312         return NULL;
313 }
314
315 /*!
316  *\brief        
317  */
318 gint stock_pixbuf_gdk(GtkWidget *window, StockPixmap icon, GdkPixbuf **pixbuf)
319 {
320         StockPixmapData *pix_d;
321
322         if (pixbuf)
323                 *pixbuf = NULL;
324                 
325         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
326
327         pix_d = &pixmaps[icon];
328
329         if (!pix_d->pixbuf || (strcmp2(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
330                 GdkPixbuf *pix = NULL;
331         
332                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
333                         if (is_dir_exist(prefs_common.pixmap_theme_path)) {
334                                 char *icon_file_name; 
335                                 
336                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
337                                                              G_DIR_SEPARATOR_S,
338                                                              pix_d->file,
339                                                              ".xpm",
340                                                              NULL);
341                                 if (is_file_exist(icon_file_name)) {
342                                         GError *err = NULL;
343                                         pix = gdk_pixbuf_new_from_file(icon_file_name, &err);   
344                                         if (err) g_error_free(err);
345                                 }                                       
346                                 if (pix) {
347                                         if (pix_d->icon_path != NULL) g_free(pix_d->icon_path);
348                                         pix_d->icon_path = g_strdup(prefs_common.pixmap_theme_path);
349                                 }
350                                 g_free(icon_file_name);
351                         } else {
352                                 /* even the path does not exist (deleted between two sessions), so
353                                 set the preferences to the internal theme */
354                                 prefs_common.pixmap_theme_path = g_strdup(DEFAULT_PIXMAP_THEME);
355                         }
356                 }
357                 pix_d->pixbuf = pix;
358         }
359
360         if (!pix_d->pixbuf) {
361                 pix_d->pixbuf = gdk_pixbuf_new_from_xpm_data((const gchar **) pix_d->data);
362                 if (pix_d->pixbuf) {
363                         if (pix_d->icon_path != NULL) g_free(pix_d->icon_path);
364                         pix_d->icon_path = g_strdup(DEFAULT_PIXMAP_THEME);      
365                 }
366         }
367
368         g_return_val_if_fail(pix_d->pixbuf != NULL, -1);
369
370         if (pixbuf)
371                 *pixbuf = pix_d->pixbuf;
372
373         /* pixbuf should have one ref outstanding */            
374
375         return 0;
376 }
377
378 /* create GdkPixmap if it has not created yet */
379 gint stock_pixmap_gdk(GtkWidget *window, StockPixmap icon,
380                       GdkPixmap **pixmap, GdkBitmap **mask)
381 {
382         StockPixmapData *pix_d;
383
384         if (pixmap) *pixmap = NULL;
385         if (mask)   *mask   = NULL;
386
387         g_return_val_if_fail(window != NULL, -1);
388         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
389
390         pix_d = &pixmaps[icon];
391
392         if (!pix_d->pixmap || (strcmp2(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
393                 GdkPixmap *pix = NULL;
394         
395                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
396                         if ( is_dir_exist(prefs_common.pixmap_theme_path) ) {
397                                 char *icon_file_name; 
398                                 
399                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
400                                                              G_DIR_SEPARATOR_S,
401                                                              pix_d->file,
402                                                              ".xpm",
403                                                              NULL);
404                                 if (is_file_exist(icon_file_name))
405                                         PIXMAP_CREATE_FROM_FILE(window, pix, pix_d->mask, icon_file_name);
406                                 if (pix) {
407                                         if (pix_d->icon_path != NULL) g_free(pix_d->icon_path);
408                                         pix_d->icon_path = g_strdup(prefs_common.pixmap_theme_path);
409                                 }
410                                 g_free(icon_file_name);
411                         } else {
412                                 /* even the path does not exist (deleted between two sessions), so
413                                 set the preferences to the internal theme */
414                                 prefs_common.pixmap_theme_path = g_strdup(DEFAULT_PIXMAP_THEME);
415                         }
416                 }
417                 pix_d->pixmap = pix;
418         }
419
420         if (!pix_d->pixmap) {
421                 PIXMAP_CREATE(window, pix_d->pixmap, pix_d->mask, pix_d->data);
422                 if (pix_d->pixmap) {
423                         if (pix_d->icon_path != NULL) g_free(pix_d->icon_path);
424                         pix_d->icon_path = g_strdup(DEFAULT_PIXMAP_THEME);      
425                 }
426         }
427
428         g_return_val_if_fail(pix_d->pixmap != NULL, -1);
429         
430         if (pixmap) 
431                 *pixmap = pix_d->pixmap;
432         if (mask)   
433                 *mask   = pix_d->mask;
434
435         return 0;
436 }
437
438 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname)
439 {
440         struct dirent *d;
441         DIR *dp;
442         
443         if ((dp = opendir(dirname)) == NULL) {
444                 debug_print("dir %s not found, skipping theme scan", dirname);
445                 return;
446         }
447         
448         while ((d = readdir(dp)) != NULL) {
449                 gchar *entry;
450                 gchar *fullentry;
451
452                 entry     = d->d_name;
453                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
454                 
455                 if (strcmp(entry, ".") != 0 && strcmp(entry, "..") != 0 && is_dir_exist(fullentry)) {
456                         gchar *filetoexist;
457                         int i;
458                         
459                         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
460                                 filetoexist = g_strconcat(fullentry, G_DIR_SEPARATOR_S, pixmaps[i].file, ".xpm", NULL);
461                                 if (is_file_exist(filetoexist)) {
462                                         *list = g_list_append(*list, fullentry);
463                                         break;
464                                 }
465                         }
466                         g_free(filetoexist);
467                         if (i == N_STOCK_PIXMAPS) 
468                                 g_free(fullentry);
469                 } else 
470                         g_free(fullentry);
471         }
472         closedir(dp);
473 }
474
475 GList *stock_pixmap_themes_list_new(void)
476 {
477         gchar *defaulttheme;
478         gchar *userthemes;
479         gchar *systemthemes;
480         GList *list = NULL;
481         
482         defaulttheme = g_strdup(DEFAULT_PIXMAP_THEME);
483         userthemes   = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
484                                    RC_DIR, G_DIR_SEPARATOR_S, 
485                                    PIXMAP_THEME_DIR, NULL);
486         systemthemes = g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
487                                    PIXMAP_THEME_DIR, NULL);
488
489         list = g_list_append(list, defaulttheme);
490         
491         stock_pixmap_find_themes_in_dir(&list, userthemes);
492         stock_pixmap_find_themes_in_dir(&list, systemthemes);
493
494         g_free(userthemes);
495         g_free(systemthemes);
496         return list;
497 }
498
499 void stock_pixmap_themes_list_free(GList *list)
500 {
501         GList *ptr;
502
503         for (ptr = g_list_first(list); ptr != NULL; ptr = g_list_next(ptr)) 
504                 if (ptr->data)
505                         g_free(ptr->data);
506         g_list_free(list);              
507 }
508
509 gchar *stock_pixmap_get_name (StockPixmap icon)
510 {
511         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
512         
513         return pixmaps[icon].file;
514
515 }
516
517 StockPixmap stock_pixmap_get_icon (gchar *file)
518 {
519         gint i;
520         
521         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
522                 if (strcmp (pixmaps[i].file, file) == 0)
523                         return i;
524         }
525         return -1;
526 }
527
528 static gboolean pixmap_with_overlay_expose_event_cb(GtkWidget *widget, GdkEventExpose *expose,
529                                                     OverlayData *data) 
530 {
531         GdkDrawable *drawable = widget->window; 
532         GdkGC *gc_pix;
533         gint left = 0;
534         gint top = 0;
535
536         g_return_val_if_fail(data->base_pixmap != NULL, FALSE);
537         g_return_val_if_fail(data->base_mask != NULL, FALSE);
538
539         gc_pix = gdk_gc_new((GdkWindow *)drawable);
540                                                  
541         gdk_window_clear_area (drawable, expose->area.x, expose->area.y,
542                                expose->area.width, expose->area.height);
543
544         gdk_gc_set_tile(gc_pix, data->base_pixmap);
545         gdk_gc_set_ts_origin(gc_pix, data->border_x, data->border_y);
546         gdk_gc_set_clip_mask(gc_pix, data->base_mask);
547         gdk_gc_set_clip_origin(gc_pix, data->border_x, data->border_y);
548         gdk_gc_set_fill(gc_pix, GDK_TILED);
549
550         gdk_draw_rectangle(drawable, gc_pix, TRUE, data->border_x, data->border_y, 
551                            data->base_width, data->base_height);
552
553         if (data->position != OVERLAY_NONE) {
554                 g_return_val_if_fail(data->overlay_pixmap != NULL, FALSE);
555                 g_return_val_if_fail(data->overlay_mask != NULL, FALSE);
556
557                 gdk_gc_set_tile(gc_pix, data->overlay_pixmap);
558                 gdk_gc_set_clip_mask(gc_pix, data->overlay_mask);
559
560                 switch (data->position) {
561                         case OVERLAY_TOP_LEFT:
562                         case OVERLAY_MID_LEFT:
563                         case OVERLAY_BOTTOM_LEFT:
564                                 left = 0;
565                                 break;
566
567                         case OVERLAY_TOP_CENTER:
568                         case OVERLAY_MID_CENTER:
569                         case OVERLAY_BOTTOM_CENTER:
570                                 left = (data->base_width + data->border_x * 2  - data->overlay_width)/2;
571                                 break;
572
573                         case OVERLAY_TOP_RIGHT:
574                         case OVERLAY_MID_RIGHT:
575                         case OVERLAY_BOTTOM_RIGHT:
576                                 left = data->base_width + data->border_x * 2 - data->overlay_width;
577                                 break;
578
579                         default:
580                                 break;
581                 }
582                 switch (data->position) {
583                         case OVERLAY_TOP_LEFT:
584                         case OVERLAY_TOP_CENTER:
585                         case OVERLAY_TOP_RIGHT:
586                                 top = 0;
587                                 break;
588
589                         case OVERLAY_MID_LEFT:
590                         case OVERLAY_MID_CENTER:
591                         case OVERLAY_MID_RIGHT:
592                                 top = (data->base_height + data->border_y * 2  - data->overlay_height)/2;
593                                 break;
594                                         
595                         case OVERLAY_BOTTOM_LEFT:
596                         case OVERLAY_BOTTOM_CENTER:
597                         case OVERLAY_BOTTOM_RIGHT:
598                                 top = data->base_height + data->border_y * 2 - data->overlay_height;
599                                 break;
600
601                         default:
602                                 break;
603                 }
604
605                 gdk_gc_set_ts_origin(gc_pix, left, top);
606                 gdk_gc_set_clip_origin(gc_pix, left, top);
607                 gdk_gc_set_fill(gc_pix, GDK_TILED);
608                 gdk_draw_rectangle(drawable, gc_pix, TRUE, left, top, 
609                                    data->overlay_width, data->overlay_height);
610         }
611         g_object_unref(gc_pix);
612         
613         return TRUE;
614 }
615
616 static void pixmap_with_overlay_destroy_cb(GtkObject *object, OverlayData *data) 
617 {
618         g_object_unref(data->base_pixmap);
619         g_object_unref(data->base_mask);
620         if (data->position != OVERLAY_NONE) {
621                 g_object_unref(data->overlay_pixmap);
622                 g_object_unref(data->overlay_mask);
623         }
624         g_free(data);
625 }
626
627 /**
628  * \brief Get a widget showing one icon with another overlaid on top of it.
629  *
630  * The base icon is always centralised, the other icon can be positioned.
631  * The overlay icon is ignored if pos=OVERLAY_NONE is used
632  *
633  * \param window   top-level window widget
634  * \param icon     the base icon
635  * \param overlay  the icon to overlay
636  * \param pos      how to align the overlay widget, or OVERLAY_NONE for no overlay
637  * \param border_x size of the border around the base icon (left and right)
638  * \param border_y size of the border around the base icon (top and bottom)
639  */
640 GtkWidget *stock_pixmap_widget_with_overlay(GtkWidget *window, StockPixmap icon,
641                                             StockPixmap overlay, OverlayPosition pos,
642                                             gint border_x, gint border_y)
643 {
644         GdkPixmap *stock_pixmap;
645         GdkBitmap *stock_mask;
646         GtkWidget *widget;
647         GtkWidget *stock_wid;
648         OverlayData *data;
649         
650         data = g_new0(OverlayData, 1);
651
652         stock_wid = stock_pixmap_widget(window, icon);
653         gtk_image_get_pixmap(GTK_IMAGE(stock_wid), &stock_pixmap, &stock_mask);
654         g_object_ref(stock_pixmap);
655         g_object_ref(stock_mask);
656         data->base_pixmap = stock_pixmap;
657         data->base_mask   = stock_mask;
658         data->base_height = stock_wid->requisition.height;
659         data->base_width  = stock_wid->requisition.width;
660         gtk_widget_destroy(stock_wid);
661
662         if (pos == OVERLAY_NONE) {
663                 data->overlay_pixmap = NULL;
664                 data->overlay_mask   = NULL;
665         } else {
666                 stock_wid = stock_pixmap_widget(window, overlay);
667                 gtk_image_get_pixmap(GTK_IMAGE(stock_wid), &stock_pixmap, &stock_mask);
668                 g_object_ref(stock_pixmap);
669                 g_object_ref(stock_mask);
670                 data->overlay_pixmap = stock_pixmap;
671                 data->overlay_mask   = stock_mask;
672                 data->overlay_height = stock_wid->requisition.height;
673                 data->overlay_width  = stock_wid->requisition.width;
674
675                 gtk_widget_destroy(stock_wid);
676         }
677         
678         data->position = pos;
679         data->border_x = border_x;
680         data->border_y = border_y;
681
682         widget = gtk_drawing_area_new();
683         gtk_drawing_area_size(GTK_DRAWING_AREA(widget), data->base_width + border_x * 2, 
684                               data->base_height + border_y * 2);
685         g_signal_connect(G_OBJECT(widget), "expose_event", 
686                          G_CALLBACK(pixmap_with_overlay_expose_event_cb), data);
687         g_signal_connect(G_OBJECT(widget), "destroy",
688                          G_CALLBACK(pixmap_with_overlay_destroy_cb), data);
689         return widget;
690
691 }