2006-04-06 [mones] 2.1.0cvs7
[claws.git] / src / stock_pixmap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21 #include <glib.h>
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkimage.h>
24 #include <string.h>
25 #include <dirent.h>
26
27 #include "stock_pixmap.h"
28 #include "gtkutils.h"
29 #include "utils.h"
30 #include "prefs_common.h"
31
32 #include "pixmaps/address.xpm"
33 #include "pixmaps/book.xpm"
34 #include "pixmaps/category.xpm"
35 #include "pixmaps/checkbox_off.xpm"
36 #include "pixmaps/checkbox_on.xpm"
37 #include "pixmaps/clip.xpm"
38 #include "pixmaps/clipkey.xpm"
39 #include "pixmaps/complete.xpm"
40 #include "pixmaps/continue.xpm"
41 #include "pixmaps/deleted.xpm"
42 #include "pixmaps/error.xpm"
43 #include "pixmaps/forwarded.xpm"
44 #include "pixmaps/group.xpm"
45 #include "pixmaps/interface.xpm"
46 #include "pixmaps/jpilot.xpm"
47 #include "pixmaps/key.xpm"
48 #include "pixmaps/ldap.xpm"
49 #include "pixmaps/linewrap.xpm"
50 #include "pixmaps/linewrapcurrent.xpm"
51 #include "pixmaps/mark.xpm"
52 #include "pixmaps/locked.xpm"
53 #include "pixmaps/new.xpm"
54 #include "pixmaps/replied.xpm"
55 #include "pixmaps/close.xpm"
56 #include "pixmaps/down_arrow.xpm"
57 #include "pixmaps/up_arrow.xpm"
58 #include "pixmaps/exec.xpm"
59 #include "pixmaps/mail.xpm"
60 #include "pixmaps/mail_attach.xpm"
61 #include "pixmaps/mail_compose.xpm"
62 #include "pixmaps/mail_forward.xpm"
63 #include "pixmaps/mail_receive.xpm"
64 #include "pixmaps/mail_receive_all.xpm"
65 #include "pixmaps/mail_reply.xpm"
66 #include "pixmaps/mail_reply_to_all.xpm"
67 #include "pixmaps/mail_reply_to_author.xpm"
68 #include "pixmaps/mail_send.xpm"
69 #include "pixmaps/mail_send_queue.xpm"
70 #include "pixmaps/news_compose.xpm"
71 #include "pixmaps/paste.xpm"
72 #include "pixmaps/preferences.xpm"
73 #include "pixmaps/properties.xpm"
74 #include "pixmaps/sylpheed_icon.xpm"
75 #include "pixmaps/sylpheed_logo.xpm"
76 #include "pixmaps/address_book.xpm"
77 #include "pixmaps/unread.xpm"
78 #include "pixmaps/read.xpm"
79 #include "pixmaps/vcard.xpm"
80 #include "pixmaps/ignorethread.xpm"
81 #include "pixmaps/online.xpm"
82 #include "pixmaps/offline.xpm"
83 #include "pixmaps/notice_warn.xpm"
84 #include "pixmaps/notice_error.xpm"
85 #include "pixmaps/notice_note.xpm"
86 #include "pixmaps/quicksearch.xpm"
87 #include "pixmaps/clip_gpg_signed.xpm"
88 #include "pixmaps/gpg_signed.xpm"
89 #include "pixmaps/mime_text_plain.xpm"
90 #include "pixmaps/mime_text_html.xpm"
91 #include "pixmaps/mime_application.xpm"
92 #include "pixmaps/mime_image.xpm"
93 #include "pixmaps/mime_audio.xpm"
94 #include "pixmaps/mime_text_enriched.xpm"
95 #include "pixmaps/mime_unknown.xpm"
96 #include "pixmaps/privacy_signed.xpm"
97 #include "pixmaps/privacy_passed.xpm"
98 #include "pixmaps/privacy_failed.xpm"
99 #include "pixmaps/privacy_unknown.xpm"
100 #include "pixmaps/privacy_expired.xpm"
101 #include "pixmaps/privacy_warn.xpm"                 
102 #include "pixmaps/privacy_emblem_encrypted.xpm"
103 #include "pixmaps/privacy_emblem_signed.xpm"
104 #include "pixmaps/privacy_emblem_passed.xpm"
105 #include "pixmaps/privacy_emblem_failed.xpm"
106 #include "pixmaps/privacy_emblem_warn.xpm"
107 #include "pixmaps/mime_message.xpm"                  
108 #include "pixmaps/address_search.xpm"
109 #include "pixmaps/check_spelling.xpm"
110
111 #include "pixmaps/dir_close.xpm"
112 #include "pixmaps/dir_open.xpm"
113 #include "pixmaps/dir_open_hrm.xpm"
114 #include "pixmaps/inbox.xpm"
115 #include "pixmaps/inbox_hrm.xpm"
116 #include "pixmaps/outbox.xpm"
117 #include "pixmaps/outbox_hrm.xpm"
118 #include "pixmaps/trash.xpm"
119 #include "pixmaps/trash_btn.xpm"
120 #include "pixmaps/trash_hrm.xpm"
121 #include "pixmaps/drafts_close.xpm"
122 #include "pixmaps/drafts_open.xpm"
123 #include "pixmaps/dir_close_mark.xpm"
124 #include "pixmaps/dir_open_mark.xpm"
125 #include "pixmaps/dir_open_hrm_mark.xpm"
126 #include "pixmaps/inbox_mark.xpm"
127 #include "pixmaps/inbox_hrm_mark.xpm"
128 #include "pixmaps/outbox_mark.xpm"
129 #include "pixmaps/outbox_hrm_mark.xpm"
130 #include "pixmaps/trash_mark.xpm"
131 #include "pixmaps/trash_hrm_mark.xpm"
132 #include "pixmaps/drafts_close_mark.xpm"
133 #include "pixmaps/drafts_open_mark.xpm"
134 #include "pixmaps/dir_noselect.xpm"
135 #include "pixmaps/spam.xpm"
136 #include "pixmaps/spam_btn.xpm"
137 #include "pixmaps/ham_btn.xpm"
138 #include "pixmaps/empty.xpm"
139
140 typedef struct _StockPixmapData StockPixmapData;
141
142 struct _StockPixmapData
143 {
144         gchar **data;
145         GdkPixmap *pixmap;
146         GdkBitmap *mask;
147         gchar *file;
148         gchar *icon_path;
149         GdkPixbuf *pixbuf;
150 };
151
152 typedef struct _OverlayData OverlayData;
153
154 struct _OverlayData
155 {
156         GdkPixmap *base_pixmap;
157         GdkBitmap *base_mask;
158         GdkPixmap *overlay_pixmap;
159         GdkBitmap *overlay_mask;
160         guint base_height;
161         guint base_width;
162         guint overlay_height;
163         guint overlay_width;
164         OverlayPosition position;
165         gint border_x;
166         gint border_y;
167 };
168
169 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname);
170
171 static StockPixmapData pixmaps[] =
172 {
173         {address_xpm                            , NULL, NULL, "address", NULL},
174         {address_book_xpm                       , NULL, NULL, "address_book", NULL},
175         {address_search_xpm                     , NULL, NULL, "address_search", NULL},
176         {book_xpm                               , NULL, NULL, "book", NULL},
177         {category_xpm                           , NULL, NULL, "category", NULL},
178         {checkbox_off_xpm                       , NULL, NULL, "checkbox_off", NULL},
179         {checkbox_on_xpm                        , NULL, NULL, "checkbox_on", NULL},
180         {check_spelling_xpm                     , NULL, NULL, "check_spelling", NULL},
181         {clip_xpm                               , NULL, NULL, "clip", NULL},
182         {clipkey_xpm                            , NULL, NULL, "clipkey", NULL},
183         {clip_gpg_signed_xpm                    , NULL, NULL, "clip_gpg_signed", NULL},
184         {close_xpm                              , NULL, NULL, "close", NULL},
185         {complete_xpm                           , NULL, NULL, "complete", NULL},
186         {continue_xpm                           , NULL, NULL, "continue", NULL},
187         {deleted_xpm                            , NULL, NULL, "deleted", NULL},
188         {dir_close_xpm                          , NULL, NULL, "dir_close", NULL},
189         {dir_close_xpm                          , NULL, NULL, "dir_close_hrm", NULL},
190         {dir_open_xpm                           , NULL, NULL, "dir_open", NULL},
191         {dir_open_hrm_xpm                       , NULL, NULL, "dir_open_hrm", NULL},
192         {dir_close_mark_xpm                     , NULL, NULL, "dir_close_mark", NULL},
193         {dir_close_mark_xpm                     , NULL, NULL, "dir_close_hrm_mark", NULL},
194         {dir_open_mark_xpm                      , NULL, NULL, "dir_open_mark", NULL},
195         {dir_open_hrm_mark_xpm                  , NULL, NULL, "dir_open_hrm_mark", NULL},
196         {down_arrow_xpm                         , NULL, NULL, "down_arrow", NULL},
197         {up_arrow_xpm                           , NULL, NULL, "up_arrow", NULL},
198         {mail_compose_xpm                       , NULL, NULL, "edit_extern", NULL},
199         {error_xpm                              , NULL, NULL, "error", NULL},
200         {exec_xpm                               , NULL, NULL, "exec", NULL},
201         {forwarded_xpm                          , NULL, NULL, "forwarded", NULL},
202         {group_xpm                              , NULL, NULL, "group", NULL},
203         {ignorethread_xpm                       , NULL, NULL, "ignorethread", NULL},
204         {inbox_xpm                              , NULL, NULL, "inbox_close", NULL},
205         {inbox_hrm_xpm                          , NULL, NULL, "inbox_close_hrm", NULL},
206         {inbox_xpm                              , NULL, NULL, "inbox_open", NULL},
207         {inbox_hrm_xpm                          , NULL, NULL, "inbox_open_hrm", NULL},
208         {inbox_mark_xpm                         , NULL, NULL, "inbox_close_mark", NULL},
209         {inbox_hrm_mark_xpm                     , NULL, NULL, "inbox_close_hrm_mark", NULL},
210         {inbox_mark_xpm                         , NULL, NULL, "inbox_open_mark", NULL},
211         {inbox_hrm_mark_xpm                     , NULL, NULL, "inbox_open_hrm_mark", NULL},
212         {paste_xpm                              , NULL, NULL, "insert_file", NULL},
213         {interface_xpm                          , NULL, NULL, "interface", NULL},
214         {jpilot_xpm                             , NULL, NULL, "jpilot", NULL},
215         {key_xpm                                , NULL, NULL, "key", NULL},
216         {ldap_xpm                               , NULL, NULL, "ldap", NULL},
217         {linewrapcurrent_xpm                    , NULL, NULL, "linewrapcurrent", NULL},
218         {linewrap_xpm                           , NULL, NULL, "linewrap", NULL},
219         {locked_xpm                             , NULL, NULL, "locked", NULL},
220         {mail_xpm                               , NULL, NULL, "mail", NULL},
221         {mail_attach_xpm                        , NULL, NULL, "mail_attach", NULL},
222         {mail_compose_xpm                       , NULL, NULL, "mail_compose", NULL},
223         {mail_forward_xpm                       , NULL, NULL, "mail_forward", NULL},
224         {mail_receive_xpm                       , NULL, NULL, "mail_receive", NULL},
225         {mail_receive_all_xpm                   , NULL, NULL, "mail_receive_all", NULL},
226         {mail_reply_xpm                         , NULL, NULL, "mail_reply", NULL},
227         {mail_reply_to_all_xpm                  , NULL, NULL, "mail_reply_to_all", NULL},
228         {mail_reply_to_author_xpm               , NULL, NULL, "mail_reply_to_author", NULL},
229         {mail_send_xpm                          , NULL, NULL, "mail_send", NULL},
230         {mail_send_queue_xpm                    , NULL, NULL, "mail_send_queue", NULL},
231         {mail_xpm                               , NULL, NULL, "mail_sign", NULL},
232         {mark_xpm                               , NULL, NULL, "mark", NULL},
233         {new_xpm                                , NULL, NULL, "new", NULL},
234         {news_compose_xpm                       , NULL, NULL, "news_compose", NULL},
235         {outbox_xpm                             , NULL, NULL, "outbox_close", NULL},
236         {outbox_hrm_xpm                         , NULL, NULL, "outbox_close_hrm", NULL},
237         {outbox_xpm                             , NULL, NULL, "outbox_open", NULL},
238         {outbox_hrm_xpm                         , NULL, NULL, "outbox_open_hrm", NULL},
239         {outbox_mark_xpm                        , NULL, NULL, "outbox_close_mark", NULL},
240         {outbox_hrm_mark_xpm                    , NULL, NULL, "outbox_close_hrm_mark", NULL},
241         {outbox_mark_xpm                        , NULL, NULL, "outbox_open_mark", NULL},
242         {outbox_hrm_mark_xpm                    , NULL, NULL, "outbox_open_hrm_mark", NULL},
243         {replied_xpm                            , NULL, NULL, "replied", NULL},
244         {paste_xpm                              , NULL, NULL, "paste", NULL},
245         {preferences_xpm                        , NULL, NULL, "preferences", NULL},
246         {properties_xpm                         , NULL, NULL, "properties", NULL},
247         {outbox_xpm                             , NULL, NULL, "queue_close", NULL},
248         {outbox_hrm_xpm                         , NULL, NULL, "queue_close_hrm", NULL},
249         {outbox_xpm                             , NULL, NULL, "queue_open", NULL},
250         {outbox_hrm_xpm                         , NULL, NULL, "queue_open_hrm", NULL},
251         {trash_xpm                              , NULL, NULL, "trash_open", NULL},
252         {trash_hrm_xpm                          , NULL, NULL, "trash_open_hrm", NULL},
253         {trash_xpm                              , NULL, NULL, "trash_close", NULL},
254         {trash_hrm_xpm                          , NULL, NULL, "trash_close_hrm", NULL},
255         {outbox_mark_xpm                        , NULL, NULL, "queue_close_mark", NULL},
256         {outbox_hrm_mark_xpm                    , NULL, NULL, "queue_close_hrm_mark", NULL},
257         {outbox_mark_xpm                        , NULL, NULL, "queue_open_mark", NULL},
258         {outbox_hrm_mark_xpm                    , NULL, NULL, "queue_open_hrm_mark", NULL},
259         {trash_mark_xpm                         , NULL, NULL, "trash_open_mark", NULL},
260         {trash_hrm_mark_xpm                     , NULL, NULL, "trash_open_hrm_mark", NULL},
261         {trash_mark_xpm                         , NULL, NULL, "trash_close_mark", NULL},
262         {trash_hrm_mark_xpm                     , NULL, NULL, "trash_close_hrm_mark", NULL},
263         {unread_xpm                             , NULL, NULL, "unread", NULL},
264         {vcard_xpm                              , NULL, NULL, "vcard", NULL},
265         {online_xpm                             , NULL, NULL, "online", NULL},
266         {offline_xpm                            , NULL, NULL, "offline", NULL},
267         {notice_warn_xpm                        , NULL, NULL, "notice_warn",  NULL},
268         {notice_error_xpm                       , NULL, NULL, "notice_error",  NULL},
269         {notice_note_xpm                        , NULL, NULL, "notice_note",  NULL},
270         {quicksearch_xpm                        , NULL, NULL, "quicksearch",  NULL},
271         {gpg_signed_xpm                         , NULL, NULL, "gpg_signed", NULL},
272         {drafts_close_xpm                       , NULL, NULL, "drafts_close", NULL},
273         {drafts_open_xpm                        , NULL, NULL, "drafts_open", NULL},
274         {drafts_close_mark_xpm                  , NULL, NULL, "drafts_close_mark", NULL},
275         {drafts_open_mark_xpm                   , NULL, NULL, "drafts_open_mark", NULL},
276         {mime_text_plain_xpm                    , NULL, NULL, "mime_text_plain", NULL},
277         {mime_text_html_xpm                     , NULL, NULL, "mime_text_html", NULL},
278         {mime_application_xpm                   , NULL, NULL, "mime_application", NULL},
279         {mime_image_xpm                         , NULL, NULL, "mime_image", NULL},
280         {mime_audio_xpm                         , NULL, NULL, "mime_audio", NULL},
281         {mime_text_enriched_xpm                 , NULL, NULL, "mime_text_enriched", NULL},
282         {mime_unknown_xpm                       , NULL, NULL, "mime_unknown", NULL},    
283         {privacy_signed_xpm                     , NULL, NULL, "privacy_signed", NULL},
284         {privacy_passed_xpm                     , NULL, NULL, "privacy_passed", NULL},
285         {privacy_failed_xpm                     , NULL, NULL, "privacy_failed", NULL},  
286         {privacy_unknown_xpm                    , NULL, NULL, "privacy_unknown", NULL},
287         {privacy_expired_xpm                    , NULL, NULL, "privacy_expired", NULL},
288         {privacy_warn_xpm                       , NULL, NULL, "privacy_warn", NULL},
289         {privacy_emblem_encrypted_xpm           , NULL, NULL, "privacy_emblem_encrypted", NULL},
290         {privacy_emblem_signed_xpm              , NULL, NULL, "privacy_emblem_signed", NULL},
291         {privacy_emblem_passed_xpm              , NULL, NULL, "privacy_emblem_passed", NULL},
292         {privacy_emblem_failed_xpm              , NULL, NULL, "privacy_emblem_failed", NULL},   
293         {privacy_emblem_warn_xpm                , NULL, NULL, "privacy_emblem_warn", NULL},
294         {mime_message_xpm                       , NULL, NULL, "mime_message", NULL},
295         {sylpheed_icon_xpm                      , NULL, NULL, "sylpheed_icon", NULL},
296         {read_xpm                               , NULL, NULL, "read", NULL},
297         {trash_btn_xpm                          , NULL, NULL, "trash_btn", NULL},
298         {sylpheed_logo_xpm                      , NULL, NULL, "sylpheed_logo", NULL},
299         {dir_noselect_xpm                       , NULL, NULL, "dir_noselect" , NULL},
300         {spam_xpm                               , NULL, NULL, "spam" , NULL},
301         {spam_btn_xpm                           , NULL, NULL, "spam_btn" , NULL},
302         {ham_btn_xpm                            , NULL, NULL, "ham_btn" , NULL},
303         {empty_xpm                              , NULL, NULL, "empty" , NULL}
304 };
305
306 /* return newly constructed GtkPixmap from GdkPixmap */
307 GtkWidget *stock_pixmap_widget(GtkWidget *window, StockPixmap icon)
308 {
309         GdkPixmap *pixmap;
310         GdkBitmap *mask;
311
312         g_return_val_if_fail(window != NULL, NULL);
313         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
314
315         if (stock_pixmap_gdk(window, icon, &pixmap, &mask) != -1)
316                 return gtk_image_new_from_pixmap(pixmap, mask);
317         
318         return NULL;
319 }
320
321 /*!
322  *\brief        
323  */
324 gint stock_pixbuf_gdk(GtkWidget *window, StockPixmap icon, GdkPixbuf **pixbuf)
325 {
326         StockPixmapData *pix_d;
327
328         if (pixbuf)
329                 *pixbuf = NULL;
330                 
331         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
332
333         pix_d = &pixmaps[icon];
334
335         if (!pix_d->pixbuf || (strcmp2(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
336                 GdkPixbuf *pix = NULL;
337         
338                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
339                         if (is_dir_exist(prefs_common.pixmap_theme_path)) {
340                                 char *icon_file_name; 
341                                 
342                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
343                                                              G_DIR_SEPARATOR_S,
344                                                              pix_d->file,
345                                                              ".xpm",
346                                                              NULL);
347                                 if (is_file_exist(icon_file_name)) {
348                                         GError *err = NULL;
349                                         pix = gdk_pixbuf_new_from_file(icon_file_name, &err);   
350                                         if (err) g_error_free(err);
351                                 }                                       
352                                 if (pix) {
353                                         g_free(pix_d->icon_path);
354                                         pix_d->icon_path = g_strdup(prefs_common.pixmap_theme_path);
355                                 }
356                                 g_free(icon_file_name);
357                         } else {
358                                 /* even the path does not exist (deleted between two sessions), so
359                                 set the preferences to the internal theme */
360                                 prefs_common.pixmap_theme_path = g_strdup(DEFAULT_PIXMAP_THEME);
361                         }
362                 }
363                 pix_d->pixbuf = pix;
364         }
365
366         if (!pix_d->pixbuf) {
367                 pix_d->pixbuf = gdk_pixbuf_new_from_xpm_data((const gchar **) pix_d->data);
368                 if (pix_d->pixbuf) {
369                         g_free(pix_d->icon_path);
370                         pix_d->icon_path = g_strdup(DEFAULT_PIXMAP_THEME);      
371                 }
372         }
373
374         g_return_val_if_fail(pix_d->pixbuf != NULL, -1);
375
376         if (pixbuf)
377                 *pixbuf = pix_d->pixbuf;
378
379         /* pixbuf should have one ref outstanding */            
380
381         return 0;
382 }
383
384 /* create GdkPixmap if it has not created yet */
385 gint stock_pixmap_gdk(GtkWidget *window, StockPixmap icon,
386                       GdkPixmap **pixmap, GdkBitmap **mask)
387 {
388         StockPixmapData *pix_d;
389
390         if (pixmap) *pixmap = NULL;
391         if (mask)   *mask   = NULL;
392
393         g_return_val_if_fail(window != NULL, -1);
394         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
395
396         pix_d = &pixmaps[icon];
397
398         if (!pix_d->pixmap || (strcmp2(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
399                 GdkPixmap *pix = NULL;
400         
401                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
402                         if ( is_dir_exist(prefs_common.pixmap_theme_path) ) {
403                                 char *icon_file_name; 
404                                 
405                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
406                                                              G_DIR_SEPARATOR_S,
407                                                              pix_d->file,
408                                                              ".xpm",
409                                                              NULL);
410                                 if (is_file_exist(icon_file_name))
411                                         PIXMAP_CREATE_FROM_FILE(window, pix, pix_d->mask, icon_file_name);
412                                 if (pix) {
413                                         g_free(pix_d->icon_path);
414                                         pix_d->icon_path = g_strdup(prefs_common.pixmap_theme_path);
415                                 }
416                                 g_free(icon_file_name);
417                         } else {
418                                 /* even the path does not exist (deleted between two sessions), so
419                                 set the preferences to the internal theme */
420                                 prefs_common.pixmap_theme_path = g_strdup(DEFAULT_PIXMAP_THEME);
421                         }
422                 }
423                 pix_d->pixmap = pix;
424         }
425
426         if (!pix_d->pixmap) {
427                 PIXMAP_CREATE(window, pix_d->pixmap, pix_d->mask, pix_d->data);
428                 if (pix_d->pixmap) {
429                         g_free(pix_d->icon_path);
430                         pix_d->icon_path = g_strdup(DEFAULT_PIXMAP_THEME);      
431                 }
432         }
433
434         g_return_val_if_fail(pix_d->pixmap != NULL, -1);
435         
436         if (pixmap) 
437                 *pixmap = pix_d->pixmap;
438         if (mask)   
439                 *mask   = pix_d->mask;
440
441         return 0;
442 }
443
444 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname)
445 {
446         struct dirent *d;
447         DIR *dp;
448         
449         if ((dp = opendir(dirname)) == NULL) {
450                 debug_print("dir %s not found, skipping theme scan", dirname);
451                 return;
452         }
453         
454         while ((d = readdir(dp)) != NULL) {
455                 gchar *entry;
456                 gchar *fullentry;
457
458                 entry     = d->d_name;
459                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
460                 
461                 if (strcmp(entry, ".") != 0 && strcmp(entry, "..") != 0 && is_dir_exist(fullentry)) {
462                         gchar *filetoexist;
463                         int i;
464                         
465                         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
466                                 filetoexist = g_strconcat(fullentry, G_DIR_SEPARATOR_S, pixmaps[i].file, ".xpm", NULL);
467                                 if (is_file_exist(filetoexist)) {
468                                         *list = g_list_append(*list, fullentry);
469                                         break;
470                                 }
471                         }
472                         g_free(filetoexist);
473                         if (i == N_STOCK_PIXMAPS) 
474                                 g_free(fullentry);
475                 } else 
476                         g_free(fullentry);
477         }
478         closedir(dp);
479 }
480
481 GList *stock_pixmap_themes_list_new(void)
482 {
483         gchar *defaulttheme;
484         gchar *userthemes;
485         gchar *systemthemes;
486         GList *list = NULL;
487         
488         defaulttheme = g_strdup(DEFAULT_PIXMAP_THEME);
489         userthemes   = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
490                                    RC_DIR, G_DIR_SEPARATOR_S, 
491                                    PIXMAP_THEME_DIR, NULL);
492         systemthemes = g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
493                                    PIXMAP_THEME_DIR, NULL);
494
495         list = g_list_append(list, defaulttheme);
496         
497         stock_pixmap_find_themes_in_dir(&list, userthemes);
498         stock_pixmap_find_themes_in_dir(&list, systemthemes);
499
500         g_free(userthemes);
501         g_free(systemthemes);
502         return list;
503 }
504
505 void stock_pixmap_themes_list_free(GList *list)
506 {
507         GList *ptr;
508
509         for (ptr = g_list_first(list); ptr != NULL; ptr = g_list_next(ptr)) 
510                 g_free(ptr->data);
511         g_list_free(list);              
512 }
513
514 gchar *stock_pixmap_get_name (StockPixmap icon)
515 {
516         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
517         
518         return pixmaps[icon].file;
519
520 }
521
522 StockPixmap stock_pixmap_get_icon (gchar *file)
523 {
524         gint i;
525         
526         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
527                 if (strcmp (pixmaps[i].file, file) == 0)
528                         return i;
529         }
530         return -1;
531 }
532
533 static gboolean pixmap_with_overlay_expose_event_cb(GtkWidget *widget, GdkEventExpose *expose,
534                                                     OverlayData *data) 
535 {
536         GdkDrawable *drawable = widget->window; 
537         GdkGC *gc_pix;
538         gint left = 0;
539         gint top = 0;
540
541         g_return_val_if_fail(data->base_pixmap != NULL, FALSE);
542         g_return_val_if_fail(data->base_mask != NULL, FALSE);
543
544         gc_pix = gdk_gc_new((GdkWindow *)drawable);
545                                                  
546         gdk_window_clear_area (drawable, expose->area.x, expose->area.y,
547                                expose->area.width, expose->area.height);
548
549         gdk_gc_set_tile(gc_pix, data->base_pixmap);
550         gdk_gc_set_ts_origin(gc_pix, data->border_x, data->border_y);
551         gdk_gc_set_clip_mask(gc_pix, data->base_mask);
552         gdk_gc_set_clip_origin(gc_pix, data->border_x, data->border_y);
553         gdk_gc_set_fill(gc_pix, GDK_TILED);
554
555         gdk_draw_rectangle(drawable, gc_pix, TRUE, data->border_x, data->border_y, 
556                            data->base_width, data->base_height);
557
558         if (data->position != OVERLAY_NONE) {
559                 g_return_val_if_fail(data->overlay_pixmap != NULL, FALSE);
560                 g_return_val_if_fail(data->overlay_mask != NULL, FALSE);
561
562                 gdk_gc_set_tile(gc_pix, data->overlay_pixmap);
563                 gdk_gc_set_clip_mask(gc_pix, data->overlay_mask);
564
565                 switch (data->position) {
566                         case OVERLAY_TOP_LEFT:
567                         case OVERLAY_MID_LEFT:
568                         case OVERLAY_BOTTOM_LEFT:
569                                 left = 0;
570                                 break;
571
572                         case OVERLAY_TOP_CENTER:
573                         case OVERLAY_MID_CENTER:
574                         case OVERLAY_BOTTOM_CENTER:
575                                 left = (data->base_width + data->border_x * 2  - data->overlay_width)/2;
576                                 break;
577
578                         case OVERLAY_TOP_RIGHT:
579                         case OVERLAY_MID_RIGHT:
580                         case OVERLAY_BOTTOM_RIGHT:
581                                 left = data->base_width + data->border_x * 2 - data->overlay_width;
582                                 break;
583
584                         default:
585                                 break;
586                 }
587                 switch (data->position) {
588                         case OVERLAY_TOP_LEFT:
589                         case OVERLAY_TOP_CENTER:
590                         case OVERLAY_TOP_RIGHT:
591                                 top = 0;
592                                 break;
593
594                         case OVERLAY_MID_LEFT:
595                         case OVERLAY_MID_CENTER:
596                         case OVERLAY_MID_RIGHT:
597                                 top = (data->base_height + data->border_y * 2  - data->overlay_height)/2;
598                                 break;
599                                         
600                         case OVERLAY_BOTTOM_LEFT:
601                         case OVERLAY_BOTTOM_CENTER:
602                         case OVERLAY_BOTTOM_RIGHT:
603                                 top = data->base_height + data->border_y * 2 - data->overlay_height;
604                                 break;
605
606                         default:
607                                 break;
608                 }
609
610                 gdk_gc_set_ts_origin(gc_pix, left, top);
611                 gdk_gc_set_clip_origin(gc_pix, left, top);
612                 gdk_gc_set_fill(gc_pix, GDK_TILED);
613                 gdk_draw_rectangle(drawable, gc_pix, TRUE, left, top, 
614                                    data->overlay_width, data->overlay_height);
615         }
616         g_object_unref(gc_pix);
617         
618         return TRUE;
619 }
620
621 static void pixmap_with_overlay_destroy_cb(GtkObject *object, OverlayData *data) 
622 {
623         g_object_unref(data->base_pixmap);
624         g_object_unref(data->base_mask);
625         if (data->position != OVERLAY_NONE) {
626                 g_object_unref(data->overlay_pixmap);
627                 g_object_unref(data->overlay_mask);
628         }
629         g_free(data);
630 }
631
632 /**
633  * \brief Get a widget showing one icon with another overlaid on top of it.
634  *
635  * The base icon is always centralised, the other icon can be positioned.
636  * The overlay icon is ignored if pos=OVERLAY_NONE is used
637  *
638  * \param window   top-level window widget
639  * \param icon     the base icon
640  * \param overlay  the icon to overlay
641  * \param pos      how to align the overlay widget, or OVERLAY_NONE for no overlay
642  * \param border_x size of the border around the base icon (left and right)
643  * \param border_y size of the border around the base icon (top and bottom)
644  */
645 GtkWidget *stock_pixmap_widget_with_overlay(GtkWidget *window, StockPixmap icon,
646                                             StockPixmap overlay, OverlayPosition pos,
647                                             gint border_x, gint border_y)
648 {
649         GdkPixmap *stock_pixmap;
650         GdkBitmap *stock_mask;
651         GtkWidget *widget;
652         GtkWidget *stock_wid;
653         OverlayData *data;
654         
655         data = g_new0(OverlayData, 1);
656
657         stock_wid = stock_pixmap_widget(window, icon);
658         gtk_image_get_pixmap(GTK_IMAGE(stock_wid), &stock_pixmap, &stock_mask);
659         g_object_ref(stock_pixmap);
660         g_object_ref(stock_mask);
661         data->base_pixmap = stock_pixmap;
662         data->base_mask   = stock_mask;
663         data->base_height = stock_wid->requisition.height;
664         data->base_width  = stock_wid->requisition.width;
665         gtk_widget_destroy(stock_wid);
666
667         if (pos == OVERLAY_NONE) {
668                 data->overlay_pixmap = NULL;
669                 data->overlay_mask   = NULL;
670         } else {
671                 stock_wid = stock_pixmap_widget(window, overlay);
672                 gtk_image_get_pixmap(GTK_IMAGE(stock_wid), &stock_pixmap, &stock_mask);
673                 g_object_ref(stock_pixmap);
674                 g_object_ref(stock_mask);
675                 data->overlay_pixmap = stock_pixmap;
676                 data->overlay_mask   = stock_mask;
677                 data->overlay_height = stock_wid->requisition.height;
678                 data->overlay_width  = stock_wid->requisition.width;
679
680                 gtk_widget_destroy(stock_wid);
681         }
682         
683         data->position = pos;
684         data->border_x = border_x;
685         data->border_y = border_y;
686
687         widget = gtk_drawing_area_new();
688         gtk_drawing_area_size(GTK_DRAWING_AREA(widget), data->base_width + border_x * 2, 
689                               data->base_height + border_y * 2);
690         g_signal_connect(G_OBJECT(widget), "expose_event", 
691                          G_CALLBACK(pixmap_with_overlay_expose_event_cb), data);
692         g_signal_connect(G_OBJECT(widget), "destroy",
693                          G_CALLBACK(pixmap_with_overlay_destroy_cb), data);
694         return widget;
695
696 }