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