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