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