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