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