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