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