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