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