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