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