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