Add new print button to pdf view.
[claws.git] / src / stock_pixmap.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtk.h>
26 #include <string.h>
27 #include <dirent.h>
28 #ifdef HAVE_SVG
29 #include <librsvg/rsvg.h>
30 #include <math.h>
31 #endif
32
33 #include "defs.h"
34 #include "stock_pixmap.h"
35 #include "gtkutils.h"
36 #include "utils.h"
37 #include "prefs_common.h"
38
39 #include "pixmaps/addr_one.xpm"
40 #include "pixmaps/addr_two.xpm"
41 #include "pixmaps/address.xpm"
42 #include "pixmaps/anonymous.xpm"
43 #include "pixmaps/book.xpm"
44 #include "pixmaps/category.xpm"
45 #include "pixmaps/checkbox_off.xpm"
46 #include "pixmaps/checkbox_on.xpm"
47 #include "pixmaps/clip.xpm"
48 #include "pixmaps/clipkey.xpm"
49 #include "pixmaps/complete.xpm"
50 #include "pixmaps/continue.xpm"
51 #include "pixmaps/deleted.xpm"
52 #include "pixmaps/error.xpm"
53 #include "pixmaps/edit_extern.xpm"
54 #include "pixmaps/forwarded.xpm"
55 #include "pixmaps/group.xpm"
56 #include "pixmaps/insert_file.xpm"
57 #include "pixmaps/interface.xpm"
58 #include "pixmaps/jpilot.xpm"
59 #include "pixmaps/key.xpm"
60 #include "pixmaps/key_gpg_signed.xpm"
61 #include "pixmaps/ldap.xpm"
62 #include "pixmaps/linewrap.xpm"
63 #include "pixmaps/linewrapcurrent.xpm"
64 #include "pixmaps/mark.xpm"
65 #include "pixmaps/locked.xpm"
66 #include "pixmaps/new.xpm"
67 #include "pixmaps/replied.xpm"
68 #include "pixmaps/replied_and_forwarded.xpm"
69 #include "pixmaps/close.xpm"
70 #include "pixmaps/down_arrow.xpm"
71 #include "pixmaps/up_arrow.xpm"
72 #include "pixmaps/exec.xpm"
73 #include "pixmaps/mail_draft.xpm"
74 #include "pixmaps/mail_attach.xpm"
75 #include "pixmaps/mail_compose.xpm"
76 #include "pixmaps/mail_forward.xpm"
77 #include "pixmaps/mail_receive.xpm"
78 #include "pixmaps/mail_receive_all.xpm"
79 #include "pixmaps/mail_reply.xpm"
80 #include "pixmaps/mail_reply_to_all.xpm"
81 #include "pixmaps/mail_reply_to_author.xpm"
82 #include "pixmaps/mail_reply_to_list.xpm"
83 #include "pixmaps/mail_send.xpm"
84 #include "pixmaps/mail_send_queue.xpm"
85 #include "pixmaps/mail_sign.xpm"
86 #include "pixmaps/open_mail.xpm"
87 #include "pixmaps/news_compose.xpm"
88 #include "pixmaps/paste.xpm"
89 #include "pixmaps/preferences.xpm"
90 #include "pixmaps/properties.xpm"
91 #include "pixmaps/queue_close.xpm"
92 #include "pixmaps/queue_close_hrm.xpm"
93 #include "pixmaps/queue_open.xpm"
94 #include "pixmaps/queue_open_hrm.xpm"
95 #include "pixmaps/claws_mail_icon.xpm"
96 #include "pixmaps/claws_mail_icon_64.xpm"
97 #include "pixmaps/claws_mail_compose_logo.xpm"
98 #ifndef GENERIC_UMPC
99 #include "pixmaps/claws_mail_logo.xpm"
100 #else
101 #include "pixmaps/claws_mail_logo_small.xpm"
102 #endif
103 #include "pixmaps/address_book.xpm"
104 #include "pixmaps/unread.xpm"
105 #include "pixmaps/read.xpm"
106 #include "pixmaps/vcard.xpm"
107 #include "pixmaps/ignorethread.xpm"
108 #include "pixmaps/online.xpm"
109 #include "pixmaps/offline.xpm"
110 #include "pixmaps/notice_warn.xpm"
111 #include "pixmaps/notice_error.xpm"
112 #include "pixmaps/notice_note.xpm"
113 #include "pixmaps/quicksearch.xpm"
114 #include "pixmaps/clip_gpg_signed.xpm"
115 #include "pixmaps/gpg_signed.xpm"
116 #include "pixmaps/go_folders.xpm"
117 #include "pixmaps/mime_text_plain.xpm"
118 #include "pixmaps/mime_text_html.xpm"
119 #include "pixmaps/mime_text_patch.xpm"
120 #include "pixmaps/mime_application.xpm"
121 #include "pixmaps/mime_image.xpm"
122 #include "pixmaps/mime_audio.xpm"
123 #include "pixmaps/mime_text_enriched.xpm"
124 #include "pixmaps/mime_unknown.xpm"
125 #include "pixmaps/mime_pdf.xpm"
126 #include "pixmaps/mime_ps.xpm"
127 #include "pixmaps/mime_calendar.xpm"
128 #include "pixmaps/mime_pgpsig.xpm"
129 #include "pixmaps/printer.xpm"
130 #include "pixmaps/privacy_signed.xpm"
131 #include "pixmaps/privacy_passed.xpm"
132 #include "pixmaps/privacy_failed.xpm"
133 #include "pixmaps/privacy_unknown.xpm"
134 #include "pixmaps/privacy_expired.xpm"
135 #include "pixmaps/privacy_warn.xpm"
136 #include "pixmaps/privacy_emblem_encrypted.xpm"
137 #include "pixmaps/privacy_emblem_signed.xpm"
138 #include "pixmaps/privacy_emblem_passed.xpm"
139 #include "pixmaps/privacy_emblem_failed.xpm"
140 #include "pixmaps/privacy_emblem_warn.xpm"
141 #include "pixmaps/mime_message.xpm"
142 #include "pixmaps/address_search.xpm"
143 #include "pixmaps/check_spelling.xpm"
144 #include "pixmaps/dir_close.xpm"
145 #include "pixmaps/dir_close_hrm.xpm"
146 #include "pixmaps/dir_open.xpm"
147 #include "pixmaps/dir_open_hrm.xpm"
148 #include "pixmaps/inbox_open.xpm"
149 #include "pixmaps/inbox_open_hrm.xpm"
150 #include "pixmaps/inbox_close.xpm"
151 #include "pixmaps/inbox_close_hrm.xpm"
152 #include "pixmaps/outbox_open.xpm"
153 #include "pixmaps/outbox_open_hrm.xpm"
154 #include "pixmaps/outbox_close.xpm"
155 #include "pixmaps/outbox_close_hrm.xpm"
156 #include "pixmaps/trash_open.xpm"
157 #include "pixmaps/trash_close.xpm"
158 #include "pixmaps/delete_btn.xpm"
159 #include "pixmaps/delete_dup_btn.xpm"
160 #include "pixmaps/cancel.xpm"
161 #include "pixmaps/trash_btn.xpm"
162 #include "pixmaps/trash_open_hrm.xpm"
163 #include "pixmaps/trash_close_hrm.xpm"
164 #include "pixmaps/drafts_close.xpm"
165 #include "pixmaps/drafts_open.xpm"
166 #include "pixmaps/dir_close_mark.xpm"
167 #include "pixmaps/dir_close_hrm_mark.xpm"
168 #include "pixmaps/dir_open_mark.xpm"
169 #include "pixmaps/dir_open_hrm_mark.xpm"
170 #include "pixmaps/inbox_open_mark.xpm"
171 #include "pixmaps/inbox_open_hrm_mark.xpm"
172 #include "pixmaps/inbox_close_mark.xpm"
173 #include "pixmaps/inbox_close_hrm_mark.xpm"
174 #include "pixmaps/outbox_open_mark.xpm"
175 #include "pixmaps/outbox_open_hrm_mark.xpm"
176 #include "pixmaps/outbox_close_mark.xpm"
177 #include "pixmaps/outbox_close_hrm_mark.xpm"
178 #include "pixmaps/trash_open_mark.xpm"
179 #include "pixmaps/trash_close_mark.xpm"
180 #include "pixmaps/queue_close_mark.xpm"
181 #include "pixmaps/queue_close_hrm_mark.xpm"
182 #include "pixmaps/queue_open_mark.xpm"
183 #include "pixmaps/queue_open_hrm_mark.xpm"
184 #include "pixmaps/trash_open_hrm_mark.xpm"
185 #include "pixmaps/trash_close_hrm_mark.xpm"
186 #include "pixmaps/drafts_close_mark.xpm"
187 #include "pixmaps/drafts_open_mark.xpm"
188 #include "pixmaps/dir_noselect_close.xpm"
189 #include "pixmaps/dir_noselect_close_mark.xpm"
190 #include "pixmaps/dir_noselect_open.xpm"
191 #include "pixmaps/dir_subs_close_mark.xpm"
192 #include "pixmaps/dir_subs_close.xpm"
193 #include "pixmaps/dir_subs_open.xpm"
194 #include "pixmaps/spam.xpm"
195 #include "pixmaps/spam_btn.xpm"
196 #include "pixmaps/ham_btn.xpm"
197 #include "pixmaps/moved.xpm"
198 #include "pixmaps/copied.xpm"
199 #include "pixmaps/selection.xpm"
200 #include "pixmaps/watchthread.xpm"
201 #include "pixmaps/empty.xpm"
202 #include "pixmaps/tray_newmail_offline.xpm"
203 #include "pixmaps/tray_newmail.xpm"
204 #include "pixmaps/tray_newmarkedmail_offline.xpm"
205 #include "pixmaps/tray_newmarkedmail.xpm"
206 #include "pixmaps/tray_nomail_offline.xpm"
207 #include "pixmaps/tray_nomail.xpm"
208 #include "pixmaps/tray_unreadmail_offline.xpm"
209 #include "pixmaps/tray_unreadmail.xpm"
210 #include "pixmaps/tray_unreadmarkedmail_offline.xpm"
211 #include "pixmaps/tray_unreadmarkedmail.xpm"
212 #include "pixmaps/doc_index.xpm"
213 #include "pixmaps/doc_index_close.xpm"
214 #include "pixmaps/doc_info.xpm"
215 #include "pixmaps/first_arrow.xpm"
216 #include "pixmaps/last_arrow.xpm"
217 #include "pixmaps/left_arrow.xpm"
218 #include "pixmaps/right_arrow.xpm"
219 #include "pixmaps/rotate_left.xpm"
220 #include "pixmaps/rotate_right.xpm"
221 #include "pixmaps/zoom_fit.xpm"
222 #include "pixmaps/zoom_in.xpm"
223 #include "pixmaps/zoom_out.xpm"
224 #include "pixmaps/zoom_width.xpm"
225 #include "pixmaps/mark_ignorethread.xpm"
226 #include "pixmaps/mark_watchthread.xpm"
227 #include "pixmaps/mark_mark.xpm"
228 #include "pixmaps/mark_unmark.xpm"
229 #include "pixmaps/mark_locked.xpm"
230 #include "pixmaps/mark_unlocked.xpm"
231 #include "pixmaps/mark_allread.xpm"
232 #include "pixmaps/mark_allunread.xpm"
233 #include "pixmaps/mark_read.xpm"
234 #include "pixmaps/mark_unread.xpm"
235
236 typedef struct _StockPixmapData StockPixmapData;
237
238 struct _StockPixmapData
239 {
240         gchar **data;
241         cairo_surface_t *pixmap;
242         cairo_pattern_t *mask;
243         gchar *file;
244         gchar *icon_path;
245         GdkPixbuf *pixbuf;
246 };
247
248 typedef struct _OverlayData OverlayData;
249
250 struct _OverlayData
251 {
252         gboolean is_pixmap;
253         cairo_surface_t *base_pixmap;
254         cairo_surface_t *overlay_pixmap;
255
256         GdkPixbuf *base_pixbuf;
257         GdkPixbuf *overlay_pixbuf;
258
259         guint base_height;
260         guint base_width;
261         guint overlay_height;
262         guint overlay_width;
263         OverlayPosition position;
264         gint border_x;
265         gint border_y;
266         gboolean highlight;
267 };
268
269 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname);
270
271 static StockPixmapData pixmaps[] =
272 {
273     {addr_one_xpm                     , NULL, NULL, "addr_one", NULL, NULL},
274     {addr_two_xpm                     , NULL, NULL, "addr_two", NULL, NULL},
275     {address_xpm                      , NULL, NULL, "address", NULL, NULL},
276     {address_book_xpm                 , NULL, NULL, "address_book", NULL, NULL},
277     {address_search_xpm               , NULL, NULL, "address_search", NULL, NULL},
278     {anonymous_xpm                    , NULL, NULL, "anonymous", NULL, NULL},
279     {book_xpm                         , NULL, NULL, "book", NULL, NULL},
280     {category_xpm                     , NULL, NULL, "category", NULL, NULL},
281     {checkbox_off_xpm                 , NULL, NULL, "checkbox_off", NULL, NULL},
282     {checkbox_on_xpm                  , NULL, NULL, "checkbox_on", NULL, NULL},
283     {check_spelling_xpm               , NULL, NULL, "check_spelling", NULL, NULL},
284     {clip_xpm                         , NULL, NULL, "clip", NULL, NULL},
285     {clipkey_xpm                      , NULL, NULL, "clipkey", NULL, NULL},
286     {clip_gpg_signed_xpm              , NULL, NULL, "clip_gpg_signed", NULL, NULL},
287     {close_xpm                        , NULL, NULL, "close", NULL, NULL},
288     {complete_xpm                     , NULL, NULL, "complete", NULL, NULL},
289     {continue_xpm                     , NULL, NULL, "continue", NULL, NULL},
290     {deleted_xpm                      , NULL, NULL, "deleted", NULL, NULL},
291     {dir_close_xpm                    , NULL, NULL, "dir_close", NULL, NULL},
292     {dir_close_hrm_xpm                , NULL, NULL, "dir_close_hrm", NULL, NULL},
293     {dir_open_xpm                     , NULL, NULL, "dir_open", NULL, NULL},
294     {dir_open_hrm_xpm                 , NULL, NULL, "dir_open_hrm", NULL, NULL},
295     {dir_close_mark_xpm               , NULL, NULL, "dir_close_mark", NULL, NULL},
296     {dir_close_hrm_mark_xpm           , NULL, NULL, "dir_close_hrm_mark", NULL, NULL},
297     {dir_open_mark_xpm                , NULL, NULL, "dir_open_mark", NULL, NULL},
298     {dir_open_hrm_mark_xpm            , NULL, NULL, "dir_open_hrm_mark", NULL, NULL},
299     {down_arrow_xpm                   , NULL, NULL, "down_arrow", NULL, NULL},
300     {up_arrow_xpm                     , NULL, NULL, "up_arrow", NULL, NULL},
301     {edit_extern_xpm                  , NULL, NULL, "edit_extern", NULL, NULL},
302     {error_xpm                        , NULL, NULL, "error", NULL, NULL},
303     {exec_xpm                         , NULL, NULL, "exec", NULL, NULL},
304     {forwarded_xpm                    , NULL, NULL, "forwarded", NULL, NULL},
305     {group_xpm                        , NULL, NULL, "group", NULL, NULL},
306     {ignorethread_xpm                 , NULL, NULL, "ignorethread", NULL, NULL},
307     {inbox_close_xpm                  , NULL, NULL, "inbox_close", NULL, NULL},
308     {inbox_close_hrm_xpm              , NULL, NULL, "inbox_close_hrm", NULL, NULL},
309     {inbox_open_xpm                   , NULL, NULL, "inbox_open", NULL, NULL},
310     {inbox_open_hrm_xpm               , NULL, NULL, "inbox_open_hrm", NULL, NULL},
311     {inbox_close_mark_xpm             , NULL, NULL, "inbox_close_mark", NULL, NULL},
312     {inbox_close_hrm_mark_xpm         , NULL, NULL, "inbox_close_hrm_mark", NULL, NULL},
313     {inbox_open_mark_xpm              , NULL, NULL, "inbox_open_mark", NULL, NULL},
314     {inbox_open_hrm_mark_xpm          , NULL, NULL, "inbox_open_hrm_mark", NULL, NULL},
315     {insert_file_xpm                  , NULL, NULL, "insert_file", NULL, NULL},
316     {interface_xpm                    , NULL, NULL, "interface", NULL, NULL},
317     {jpilot_xpm                       , NULL, NULL, "jpilot", NULL, NULL},
318     {key_xpm                          , NULL, NULL, "key", NULL, NULL},
319     {key_gpg_signed_xpm               , NULL, NULL, "key_gpg_signed", NULL, NULL},
320     {ldap_xpm                         , NULL, NULL, "ldap", NULL, NULL},
321     {linewrapcurrent_xpm              , NULL, NULL, "linewrapcurrent", NULL, NULL},
322     {linewrap_xpm                     , NULL, NULL, "linewrap", NULL, NULL},
323     {locked_xpm                       , NULL, NULL, "locked", NULL, NULL},
324     {mail_draft_xpm                   , NULL, NULL, "mail_draft", NULL, NULL},
325     {mail_attach_xpm                  , NULL, NULL, "mail_attach", NULL, NULL},
326     {mail_compose_xpm                 , NULL, NULL, "mail_compose", NULL, NULL},
327     {mail_forward_xpm                 , NULL, NULL, "mail_forward", NULL, NULL},
328     {mail_receive_xpm                 , NULL, NULL, "mail_receive", NULL, NULL},
329     {mail_receive_all_xpm             , NULL, NULL, "mail_receive_all", NULL, NULL},
330     {mail_reply_xpm                   , NULL, NULL, "mail_reply", NULL, NULL},
331     {mail_reply_to_all_xpm            , NULL, NULL, "mail_reply_to_all", NULL, NULL},
332     {mail_reply_to_author_xpm         , NULL, NULL, "mail_reply_to_author", NULL, NULL},
333     {mail_reply_to_list_xpm           , NULL, NULL, "mail_reply_to_list", NULL, NULL},
334     {mail_send_xpm                    , NULL, NULL, "mail_send", NULL, NULL},
335     {mail_send_queue_xpm              , NULL, NULL, "mail_send_queue", NULL, NULL},
336     {mail_sign_xpm                    , NULL, NULL, "mail_sign", NULL, NULL},
337     {open_mail_xpm                    , NULL, NULL, "open_mail", NULL, NULL},
338     {mark_xpm                         , NULL, NULL, "mark", NULL, NULL},
339     {new_xpm                          , NULL, NULL, "new", NULL, NULL},
340     {news_compose_xpm                 , NULL, NULL, "news_compose", NULL, NULL},
341     {outbox_close_xpm                 , NULL, NULL, "outbox_close", NULL, NULL},
342     {outbox_close_hrm_xpm             , NULL, NULL, "outbox_close_hrm", NULL, NULL},
343     {outbox_open_xpm                  , NULL, NULL, "outbox_open", NULL, NULL},
344     {outbox_open_hrm_xpm              , NULL, NULL, "outbox_open_hrm", NULL, NULL},
345     {outbox_close_mark_xpm            , NULL, NULL, "outbox_close_mark", NULL, NULL},
346     {outbox_close_hrm_mark_xpm        , NULL, NULL, "outbox_close_hrm_mark", NULL, NULL},
347     {outbox_open_mark_xpm             , NULL, NULL, "outbox_open_mark", NULL, NULL},
348     {outbox_open_hrm_mark_xpm         , NULL, NULL, "outbox_open_hrm_mark", NULL, NULL},
349     {replied_xpm                      , NULL, NULL, "replied", NULL, NULL},
350     {replied_and_forwarded_xpm        , NULL, NULL, "replied_and_forwarded", NULL, NULL},
351     {paste_xpm                        , NULL, NULL, "paste", NULL, NULL},
352     {preferences_xpm                  , NULL, NULL, "preferences", NULL, NULL},
353     {properties_xpm                   , NULL, NULL, "properties", NULL, NULL},
354     {queue_close_xpm                  , NULL, NULL, "queue_close", NULL, NULL},
355     {queue_close_hrm_xpm              , NULL, NULL, "queue_close_hrm", NULL, NULL},
356     {queue_open_xpm                   , NULL, NULL, "queue_open", NULL, NULL},
357     {queue_open_hrm_xpm               , NULL, NULL, "queue_open_hrm", NULL, NULL},
358     {trash_open_xpm                   , NULL, NULL, "trash_open", NULL, NULL},
359     {trash_open_hrm_xpm               , NULL, NULL, "trash_open_hrm", NULL, NULL},
360     {trash_close_xpm                  , NULL, NULL, "trash_close", NULL, NULL},
361     {trash_close_hrm_xpm              , NULL, NULL, "trash_close_hrm", NULL, NULL},
362     {queue_close_mark_xpm             , NULL, NULL, "queue_close_mark", NULL, NULL},
363     {queue_close_hrm_mark_xpm         , NULL, NULL, "queue_close_hrm_mark", NULL, NULL},
364     {queue_open_mark_xpm              , NULL, NULL, "queue_open_mark", NULL, NULL},
365     {queue_open_hrm_mark_xpm          , NULL, NULL, "queue_open_hrm_mark", NULL, NULL},
366     {trash_open_mark_xpm              , NULL, NULL, "trash_open_mark", NULL, NULL},
367     {trash_open_hrm_mark_xpm          , NULL, NULL, "trash_open_hrm_mark", NULL, NULL},
368     {trash_close_mark_xpm             , NULL, NULL, "trash_close_mark", NULL, NULL},
369     {trash_close_hrm_mark_xpm         , NULL, NULL, "trash_close_hrm_mark", NULL, NULL},
370     {unread_xpm                       , NULL, NULL, "unread", NULL, NULL},
371     {vcard_xpm                        , NULL, NULL, "vcard", NULL, NULL},
372     {online_xpm                       , NULL, NULL, "online", NULL, NULL},
373     {offline_xpm                      , NULL, NULL, "offline", NULL, NULL},
374     {notice_warn_xpm                  , NULL, NULL, "notice_warn",  NULL, NULL},
375     {notice_error_xpm                 , NULL, NULL, "notice_error",  NULL, NULL},
376     {notice_note_xpm                  , NULL, NULL, "notice_note",  NULL, NULL},
377     {quicksearch_xpm                  , NULL, NULL, "quicksearch",  NULL, NULL},
378     {gpg_signed_xpm                   , NULL, NULL, "gpg_signed", NULL, NULL},
379     {go_folders_xpm                   , NULL, NULL, "go_folders", NULL, NULL},
380     {drafts_close_xpm                 , NULL, NULL, "drafts_close", NULL, NULL},
381     {drafts_open_xpm                  , NULL, NULL, "drafts_open", NULL, NULL},
382     {drafts_close_mark_xpm            , NULL, NULL, "drafts_close_mark", NULL, NULL},
383     {drafts_open_mark_xpm             , NULL, NULL, "drafts_open_mark", NULL, NULL},
384     {mime_text_plain_xpm              , NULL, NULL, "mime_text_plain", NULL, NULL},
385     {mime_text_html_xpm               , NULL, NULL, "mime_text_html", NULL, NULL},
386     {mime_text_patch_xpm              , NULL, NULL, "mime_text_patch", NULL, NULL},
387     {mime_application_xpm             , NULL, NULL, "mime_application", NULL, NULL},
388     {mime_image_xpm                   , NULL, NULL, "mime_image", NULL, NULL},
389     {mime_audio_xpm                   , NULL, NULL, "mime_audio", NULL, NULL},
390     {mime_text_enriched_xpm           , NULL, NULL, "mime_text_enriched", NULL, NULL},
391     {mime_unknown_xpm                 , NULL, NULL, "mime_unknown", NULL, NULL},
392     {mime_pdf_xpm                     , NULL, NULL, "mime_pdf", NULL, NULL},
393     {mime_ps_xpm                      , NULL, NULL, "mime_ps", NULL, NULL},
394     {mime_calendar_xpm                , NULL, NULL, "mime_calendar", NULL, NULL},
395     {mime_pgpsig_xpm                  , NULL, NULL, "mime_pgpsig", NULL, NULL},
396     {printer_btn_xpm                  , NULL, NULL, "printer_btn", NULL, NULL},
397     {printer_xpm                      , NULL, NULL, "printer", NULL, NULL},
398     {privacy_signed_xpm               , NULL, NULL, "privacy_signed", NULL, NULL},
399     {privacy_passed_xpm               , NULL, NULL, "privacy_passed", NULL, NULL},
400     {privacy_failed_xpm               , NULL, NULL, "privacy_failed", NULL, NULL},
401     {privacy_unknown_xpm              , NULL, NULL, "privacy_unknown", NULL, NULL},
402     {privacy_expired_xpm              , NULL, NULL, "privacy_expired", NULL, NULL},
403     {privacy_warn_xpm                 , NULL, NULL, "privacy_warn", NULL, NULL},
404     {privacy_emblem_encrypted_xpm     , NULL, NULL, "privacy_emblem_encrypted", NULL, NULL},
405     {privacy_emblem_signed_xpm        , NULL, NULL, "privacy_emblem_signed", NULL, NULL},
406     {privacy_emblem_passed_xpm        , NULL, NULL, "privacy_emblem_passed", NULL, NULL},
407     {privacy_emblem_failed_xpm        , NULL, NULL, "privacy_emblem_failed", NULL, NULL},
408     {privacy_emblem_warn_xpm          , NULL, NULL, "privacy_emblem_warn", NULL, NULL},
409     {mime_message_xpm                 , NULL, NULL, "mime_message", NULL, NULL},
410     {claws_mail_icon_xpm              , NULL, NULL, "claws_mail_icon", NULL, NULL},
411     {claws_mail_icon_64_xpm           , NULL, NULL, "claws_mail_icon_64", NULL, NULL},
412     {read_xpm                         , NULL, NULL, "read", NULL, NULL},
413     {delete_btn_xpm                   , NULL, NULL, "delete_btn", NULL, NULL},
414     {delete_dup_btn_xpm               , NULL, NULL, "delete_dup_btn", NULL, NULL},
415     {cancel_xpm                       , NULL, NULL, "cancel", NULL, NULL},
416     {trash_btn_xpm                    , NULL, NULL, "trash_btn", NULL, NULL},
417     {claws_mail_compose_logo_xpm      , NULL, NULL, "claws_mail_compose_logo", NULL, NULL},
418 #ifndef GENERIC_UMPC
419     {claws_mail_logo_xpm              , NULL, NULL, "claws_mail_logo", NULL, NULL},
420 #else
421     {claws_mail_logo_small_xpm        , NULL, NULL, "claws_mail_logo_small", NULL, NULL},
422 #endif
423     {dir_noselect_close_xpm           , NULL, NULL, "dir_noselect_close", NULL, NULL},
424     {dir_noselect_close_mark_xpm      , NULL, NULL, "dir_noselect_close_mark", NULL, NULL},
425     {dir_noselect_open_xpm            , NULL, NULL, "dir_noselect_open", NULL, NULL},
426     {dir_subs_close_mark_xpm          , NULL, NULL, "dir_subs_close_mark", NULL, NULL},
427     {dir_subs_close_xpm               , NULL, NULL, "dir_subs_close", NULL, NULL},
428     {dir_subs_open_xpm                , NULL, NULL, "dir_subs_open", NULL, NULL},
429     {spam_xpm                         , NULL, NULL, "spam", NULL, NULL},
430     {spam_btn_xpm                     , NULL, NULL, "spam_btn", NULL, NULL},
431     {ham_btn_xpm                      , NULL, NULL, "ham_btn", NULL, NULL},
432     {moved_xpm                        , NULL, NULL, "moved", NULL, NULL},
433     {copied_xpm                       , NULL, NULL, "copied", NULL, NULL},
434     {selection_xpm                    , NULL, NULL, "selection", NULL, NULL},
435     {watchthread_xpm                  , NULL, NULL, "watchthread", NULL, NULL},
436     {tray_newmail_offline_xpm         , NULL, NULL, "tray_newmail_offline", NULL, NULL},
437     {tray_newmail_xpm                 , NULL, NULL, "tray_newmail", NULL, NULL},
438     {tray_newmarkedmail_offline_xpm   , NULL, NULL, "tray_newmarkedmail_offline", NULL, NULL},
439     {tray_newmarkedmail_xpm           , NULL, NULL, "tray_newmarkedmail", NULL, NULL},
440     {tray_nomail_offline_xpm          , NULL, NULL, "tray_nomail_offline", NULL, NULL},
441     {tray_nomail_xpm                  , NULL, NULL, "tray_nomail", NULL, NULL},
442     {tray_unreadmail_offline_xpm      , NULL, NULL, "tray_unreadmail_offline", NULL, NULL},
443     {tray_unreadmail_xpm              , NULL, NULL, "tray_unreadmail", NULL, NULL},
444     {tray_unreadmarkedmail_offline_xpm, NULL, NULL, "tray_unreadmarkedmail_offline", NULL, NULL},
445     {tray_unreadmarkedmail_xpm        , NULL, NULL, "tray_unreadmarkedmail", NULL, NULL},
446     {doc_index_xpm                    , NULL, NULL, "doc_index", NULL, NULL},
447     {doc_index_close_xpm              , NULL, NULL, "doc_index_close", NULL, NULL},
448     {doc_info_xpm                     , NULL, NULL, "doc_info", NULL, NULL},
449     {first_arrow_xpm                  , NULL, NULL, "first_arrow", NULL, NULL},
450     {last_arrow_xpm                   , NULL, NULL, "last_arrow", NULL, NULL},
451     {left_arrow_xpm                   , NULL, NULL, "left_arrow", NULL, NULL},
452     {right_arrow_xpm                  , NULL, NULL, "right_arrow", NULL, NULL},
453     {rotate_left_xpm                  , NULL, NULL, "rotate_left", NULL, NULL},
454     {rotate_right_xpm                 , NULL, NULL, "rotate_right", NULL, NULL},
455     {zoom_fit_xpm                     , NULL, NULL, "zoom_fit", NULL, NULL},
456     {zoom_in_xpm                      , NULL, NULL, "zoom_in", NULL, NULL},
457     {zoom_out_xpm                     , NULL, NULL, "zoom_out", NULL, NULL},
458     {zoom_width_xpm                   , NULL, NULL, "zoom_width", NULL, NULL},
459     {mark_ignorethread_xpm            , NULL, NULL, "mark_ignorethread", NULL, NULL},
460     {mark_watchthread_xpm             , NULL, NULL, "mark_watchthread", NULL, NULL},
461     {mark_mark_xpm                    , NULL, NULL, "mark_mark", NULL, NULL},
462     {mark_unmark_xpm                  , NULL, NULL, "mark_unmark", NULL, NULL},
463     {mark_locked_xpm                  , NULL, NULL, "mark_locked", NULL, NULL},
464     {mark_unlocked_xpm                , NULL, NULL, "mark_unlocked", NULL, NULL},
465     {mark_allread_xpm                 , NULL, NULL, "mark_allread", NULL, NULL},
466     {mark_allunread_xpm               , NULL, NULL, "mark_allunread", NULL, NULL},
467     {mark_read_xpm                    , NULL, NULL, "mark_read", NULL, NULL},
468     {mark_unread_xpm                  , NULL, NULL, "mark_unread", NULL, NULL},
469     {empty_xpm                        , NULL, NULL, "empty", NULL, NULL}
470 };
471
472 /* Supported theme extensions */
473 static const char *extension[] = {
474         ".png",
475         ".xpm",
476 #ifdef HAVE_SVG
477         ".svg",
478 #endif
479         NULL
480 };
481
482 /* return current supported extensions */
483 const char **stock_pixmap_theme_extensions(void)
484 {
485         return extension;
486 }
487
488 /* return newly constructed GtkPixmap from GdkPixmap */
489 GtkWidget *stock_pixmap_widget(StockPixmap icon)
490 {
491         GdkPixbuf *pixbuf;
492
493         cm_return_val_if_fail(icon < N_STOCK_PIXMAPS, NULL);
494
495         if (stock_pixbuf_gdk(icon, &pixbuf) != -1)
496                 return gtk_image_new_from_pixbuf(pixbuf);
497
498         return NULL;
499 }
500
501 #ifdef HAVE_SVG
502 /*
503  * Renders a SVG into a Cairo context at the given dimensions keeping
504  * the aspect ratio.
505  *
506  * Adapted from https://developer.gnome.org/rsvg/2.40/RsvgHandle.html
507  * #rsvg-handle-set-size-callback
508  */
509 void render_scaled_proportionally(RsvgHandle *handle, cairo_t *cr, int width, int height)
510 {
511         RsvgDimensionData dimensions;
512         double x_factor, y_factor;
513         double scale_factor;
514
515         rsvg_handle_get_dimensions(handle, &dimensions);
516
517         x_factor = (double) width / dimensions.width;
518         y_factor = (double) height / dimensions.height;
519
520         scale_factor = MIN(x_factor, y_factor);
521
522         cairo_scale(cr, scale_factor, scale_factor);
523
524         rsvg_handle_render_cairo(handle, cr);
525 }
526
527 /*
528  * Generates a new Pixbuf from a Cairo context of the given dimensions.
529  *
530  * Adapted from https://gist.github.com/bert/985903
531  */
532 GdkPixbuf *pixbuf_from_cairo(cairo_t *cr, gboolean alpha, int width, int height)
533 {
534         gint p_stride, /* Pixbuf stride value */
535              p_n_channels, /* RGB -> 3, RGBA -> 4 */
536              s_stride; /* Surface stride value */
537         guchar *p_pixels, /* Pixbuf's pixel data */
538                *s_pixels; /* Surface's pixel data */
539         cairo_surface_t *surface; /* Temporary image surface */
540         GdkPixbuf *pixbuf; /* Returned pixbuf */
541
542         /* Create pixbuf */
543         pixbuf = gdk_pixbuf_new
544                         (GDK_COLORSPACE_RGB, alpha, 8, width, height);
545         if (pixbuf == NULL) {
546                 g_warning("failed to create a new %d x %d pixbuf", width, height);
547                 return NULL;
548         }
549         /* Obtain surface from where pixel values will be copied */
550         surface = cairo_get_target(cr);
551         if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
552                 g_warning("invalid cairo surface for copying");
553                 return NULL;
554         }
555         /* Inspect pixbuf */
556         g_object_get(G_OBJECT(pixbuf),
557                 "rowstride", &p_stride,
558                 "n-channels", &p_n_channels,
559                 "pixels", &p_pixels,
560                 NULL);
561         /* and surface */
562         s_stride = cairo_image_surface_get_stride(surface);
563         s_pixels = cairo_image_surface_get_data(surface);
564
565         /* Copy pixel data from surface to pixbuf */
566         while (height--) {
567                 gint i;
568                 guchar *p_iter = p_pixels, *s_iter = s_pixels;
569                 for (i = 0; i < width; i++) {
570 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
571                         /* Pixbuf: RGB(A) - Surface: BGRA */
572                         gdouble alpha_factor = (gdouble)0xff / s_iter[3];
573                         p_iter[0] = (guchar)( s_iter[2] * alpha_factor + .5 );
574                         p_iter[1] = (guchar)( s_iter[1] * alpha_factor + .5 );
575                         p_iter[2] = (guchar)( s_iter[0] * alpha_factor + .5 );
576                         if (p_n_channels == 4)
577                                  p_iter[3] = s_iter[3];
578 #elif G_BYTE_ORDER == G_BIG_ENDIAN
579                         /* Pixbuf: RGB(A) - Surface: ARGB */
580                         gdouble alpha_factor = (gdouble)0xff / s_iter[0];
581                         p_iter[0] = (guchar)( s_iter[1] * alpha_factor + .5 );
582                         p_iter[1] = (guchar)( s_iter[2] * alpha_factor + .5 );
583                         p_iter[2] = (guchar)( s_iter[3] * alpha_factor + .5 );
584                         if (p_n_channels == 4)
585                                 p_iter[3] = s_iter[0];
586 #else /* PDP endianness */
587                         /* Pixbuf: RGB(A) - Surface: RABG */
588                         gdouble alpha_factor = (gdouble)0xff / s_iter[1];
589                         p_iter[0] = (guchar)( s_iter[0] * alpha_factor + .5 );
590                         p_iter[1] = (guchar)( s_iter[3] * alpha_factor + .5 );
591                         p_iter[2] = (guchar)( s_iter[2] * alpha_factor + .5 );
592                         if (p_n_channels == 4)
593                                 p_iter[3] = s_iter[1];
594 #endif
595                         s_iter += 4;
596                         p_iter += p_n_channels;
597                 }
598                 s_pixels += s_stride;
599                 p_pixels += p_stride;
600         }
601         /* Destroy context */
602         cairo_destroy(cr);
603
604         return pixbuf;
605 }
606
607 /*
608  * Renders a SVG file into a pixbuf with the dimensions of the
609  * given pixmap data (optionally with alpha channel).
610  */
611 GdkPixbuf *pixbuf_from_svg_like_icon(char *filename, GError **error, StockPixmapData *icondata, gboolean alpha)
612 {
613         int width, height;
614         cairo_surface_t *surface;
615         cairo_t *context;
616         RsvgHandle *handle;
617
618         cm_return_val_if_fail(filename != NULL, NULL);
619         cm_return_val_if_fail(icondata != NULL, NULL);
620
621         /* load SVG file */
622         handle = rsvg_handle_new_from_file(filename, error);
623         if (handle == NULL) {
624                 g_warning("failed loading SVG '%s': %s (%d)", filename,
625                                 (*error)->message, (*error)->code);
626                 return NULL;
627         }
628
629         /* scale dimensions */
630         if (prefs_common.enable_pixmap_scaling) {
631                 /* default is pixmap icon size */
632                 if (sscanf((icondata->data)[0], "%d %d ", &width, &height) != 2) {
633                         g_warning("failed reading icondata width and height");
634                         return NULL;
635                 }
636                 /* which can be modified by some factor */
637                 if (prefs_common.pixmap_scaling_ppi > 0) {
638                         gdouble factor = (gdouble) prefs_common.pixmap_scaling_ppi / MIN_PPI;
639                         width = (int) floor(factor * width);
640                         height = (int) floor(factor * height);
641                 }
642         } else { /* render using SVG size */
643                 RsvgDimensionData dimension;
644
645                 rsvg_handle_get_dimensions (handle, &dimension);
646                 width = dimension.width;
647                 height = dimension.height;
648         }
649
650         /* create drawing context */
651         surface = cairo_image_surface_create(
652                         alpha? CAIRO_FORMAT_ARGB32: CAIRO_FORMAT_RGB24,
653                         width, height);
654         if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
655                 g_warning("failed to create a cairo surface: %s",
656                                 cairo_status_to_string(cairo_surface_status(surface)));
657                 g_object_unref(handle);
658                 return NULL;
659         }
660         context = cairo_create(surface);
661         cairo_surface_destroy(surface);
662         if (cairo_status(context) != CAIRO_STATUS_SUCCESS) {
663                 g_warning("failed to create a cairo context: %s",
664                                 cairo_status_to_string(cairo_status(context)));
665                 cairo_destroy(context);
666                 return NULL;
667         }
668         /* render SVG */
669         render_scaled_proportionally(handle, context, width, height);
670         /* build result and destroy context */
671         return pixbuf_from_cairo(context, alpha, width, height);
672 }
673 #endif
674
675 /*!
676  *\brief
677  */
678 gint stock_pixbuf_gdk(StockPixmap icon, GdkPixbuf **pixbuf)
679 {
680         StockPixmapData *pix_d;
681         int i = 0;
682         gboolean theme_changed = FALSE;
683
684         if (pixbuf)
685                 *pixbuf = NULL;
686
687         cm_return_val_if_fail(icon < N_STOCK_PIXMAPS, -1);
688
689         pix_d = &pixmaps[icon];
690
691         theme_changed = (strcmp2(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0);
692         if (!pix_d->pixbuf || theme_changed) {
693                 GdkPixbuf *pix = NULL;
694
695                 if (theme_changed && pix_d->pixmap) {
696                         g_object_unref(pix_d->pixmap);
697                         pix_d->pixmap = NULL;
698                 }
699
700                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
701                         if (is_dir_exist(prefs_common.pixmap_theme_path)) {
702                                 char *icon_file_name;
703 try_next_extension:
704                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
705                                                              G_DIR_SEPARATOR_S,
706                                                              pix_d->file,
707                                                              extension[i],
708                                                              NULL);
709                                 if (is_file_exist(icon_file_name)) {
710                                         GError *err = NULL;
711 #ifdef HAVE_SVG
712                                         if (!strncmp(extension[i], ".svg", 4)) {
713                                                 pix = pixbuf_from_svg_like_icon(icon_file_name, &err, pix_d,
714                                                                 prefs_common.enable_alpha_svg);
715                                         } else {
716                                                 pix = gdk_pixbuf_new_from_file(icon_file_name, &err);
717                                         }
718 #else
719                                         pix = gdk_pixbuf_new_from_file(icon_file_name, &err);
720 #endif
721                                         if (err) g_error_free(err);
722                                 }
723                                 if (pix) {
724                                         g_free(pix_d->icon_path);
725                                         pix_d->icon_path = g_strdup(prefs_common.pixmap_theme_path);
726                                 }
727                                 g_free(icon_file_name);
728                                 if (!pix) {
729                                         i++;
730                                         if (extension[i])
731                                                 goto try_next_extension;
732                                 }
733                         } else {
734                                 /* even the path does not exist (deleted between two sessions), so
735                                 set the preferences to the internal theme */
736                                 prefs_common.pixmap_theme_path = g_strdup(DEFAULT_PIXMAP_THEME);
737                         }
738                 }
739                 pix_d->pixbuf = pix;
740         }
741
742         if (!pix_d->pixbuf) {
743                 pix_d->pixbuf = gdk_pixbuf_new_from_xpm_data((const gchar **) pix_d->data);
744                 if (pix_d->pixbuf) {
745                         g_free(pix_d->icon_path);
746                         pix_d->icon_path = g_strdup(DEFAULT_PIXMAP_THEME);
747                 }
748         }
749
750         cm_return_val_if_fail(pix_d->pixbuf != NULL, -1);
751
752         if (pixbuf)
753                 *pixbuf = pix_d->pixbuf;
754
755         /* pixbuf should have one ref outstanding */
756
757         return 0;
758 }
759
760 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname)
761 {
762         const gchar *entry;
763         gchar *fullentry;
764         GDir *dp;
765         GError *error = NULL;
766
767         if ((dp = g_dir_open(dirname, 0, &error)) == NULL) {
768                 debug_print("skipping theme scan, dir %s could not be opened: %s (%d)\n",
769                                 dirname ? dirname : "(null)", error->message, error->code);
770                 g_error_free(error);
771                 return;
772         }
773
774         while ((entry = g_dir_read_name(dp)) != NULL) {
775                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
776
777                 if (strcmp(entry, ".") != 0 && strcmp(entry, "..") != 0 && is_dir_exist(fullentry)) {
778                         gchar *filetoexist;
779                         gboolean found = FALSE;
780                         int i;
781                         int j;
782                         for (i = 0; i < N_STOCK_PIXMAPS && !found; i++) {
783                                 for (j = 0; extension[j] && !found; j++) {
784                                         filetoexist = g_strconcat(fullentry, G_DIR_SEPARATOR_S, pixmaps[i].file, extension[j], NULL);
785                                         if (is_file_exist(filetoexist)) {
786                                                 *list = g_list_append(*list, fullentry);
787                                                 found = TRUE;
788                                         }
789                                         g_free(filetoexist);
790                                 }
791                         }
792                         if (i == N_STOCK_PIXMAPS)
793                                 g_free(fullentry);
794                 } else
795                         g_free(fullentry);
796         }
797         g_dir_close(dp);
798 }
799
800 gchar *stock_pixmap_get_system_theme_dir_for_theme(const gchar *theme)
801 {
802         const gchar *sep = NULL;
803         if (theme && *theme)
804                 sep = G_DIR_SEPARATOR_S;
805 #ifndef G_OS_WIN32
806         return g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
807                            PIXMAP_THEME_DIR, sep, theme, NULL);
808 #else
809         return g_strconcat(w32_get_themes_dir(), sep, theme, NULL);
810 #endif
811 }
812
813 GList *stock_pixmap_themes_list_new(void)
814 {
815         gchar *defaulttheme;
816         gchar *userthemes;
817         gchar *systemthemes;
818         GList *list = NULL;
819
820         defaulttheme = g_strdup(DEFAULT_PIXMAP_THEME);
821         userthemes   = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
822                                    PIXMAP_THEME_DIR, NULL);
823         systemthemes = stock_pixmap_get_system_theme_dir_for_theme(NULL);
824
825         list = g_list_append(list, defaulttheme);
826         stock_pixmap_find_themes_in_dir(&list, userthemes);
827         stock_pixmap_find_themes_in_dir(&list, systemthemes);
828
829         g_free(userthemes);
830         g_free(systemthemes);
831         return list;
832 }
833
834 void stock_pixmap_themes_list_free(GList *list)
835 {
836         GList *ptr;
837
838         for (ptr = g_list_first(list); ptr != NULL; ptr = g_list_next(ptr))
839                 g_free(ptr->data);
840         g_list_free(list);
841 }
842
843 void stock_pixmap_invalidate_all_icons(void)
844 {
845         StockPixmapData *pix_d;
846         int i = 0;
847
848         while (i < N_STOCK_PIXMAPS) {
849                 pix_d = &pixmaps[i];
850                 if (pix_d->pixbuf) {
851                         g_object_unref(G_OBJECT(pix_d->pixbuf));
852                         pix_d->pixbuf = NULL;
853                 }
854                 if (pix_d->pixmap) {
855                         g_object_unref(G_OBJECT(pix_d->pixmap));
856                         pix_d->pixmap = NULL;
857                 }
858                 i++;
859         }
860 }
861
862 gchar *stock_pixmap_get_name (StockPixmap icon)
863 {
864         if (icon >= N_STOCK_PIXMAPS)
865                 return NULL;
866
867         return pixmaps[icon].file;
868
869 }
870
871 StockPixmap stock_pixmap_get_icon (gchar *file)
872 {
873         gint i;
874
875         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
876                 if (strcmp (pixmaps[i].file, file) == 0)
877                         return i;
878         }
879         return -1;
880 }
881
882 static gboolean do_pix_draw(GtkWidget *widget, cairo_t *cr,
883                             OverlayData *data)
884 {
885         GdkWindow *drawable = gtk_widget_get_window(widget);
886         gint left = 0;
887         gint top = 0;
888
889         if (data->is_pixmap) {
890                 cm_return_val_if_fail(data->base_pixmap != NULL, FALSE);
891         } else {
892                 cm_return_val_if_fail(data->base_pixbuf != NULL, FALSE);
893         }
894
895         if (data->highlight) {
896                 MainWindow *mw = NULL;
897
898                 mw = mainwindow_get_mainwindow();
899                 if (mw != NULL && mw->menubar != NULL) {
900                         cairo_t *cr;
901                         GdkColor color = gtk_widget_get_style(mw->menubar)->base[GTK_STATE_SELECTED];
902
903                         cr = gdk_cairo_create(drawable);
904                         gdk_cairo_set_source_color(cr, &color);
905                         cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
906                         cairo_set_line_width(cr, 1.);
907                         cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
908                         cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
909                         cairo_rectangle(cr, data->border_x-2, data->border_y-2,
910                             data->base_width+3, data->base_height+3);
911                         cairo_stroke(cr);
912                         cairo_destroy(cr);
913                 }
914         }
915
916         if (data->is_pixmap) {
917                 cairo_set_source_surface(cr, data->base_pixmap, data->border_x, data->border_y);
918                 cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT);
919                 cairo_rectangle(cr, data->border_x, data->border_y, data->base_width, data->base_height);
920                 cairo_fill(cr);
921         } else {
922                 gdk_cairo_set_source_pixbuf(cr, data->base_pixbuf, data->border_x, data->border_y);
923                 cairo_paint(cr);
924         }
925
926         if (data->position != OVERLAY_NONE) {
927
928                 switch (data->position) {
929                         case OVERLAY_TOP_LEFT:
930                         case OVERLAY_MID_LEFT:
931                         case OVERLAY_BOTTOM_LEFT:
932                                 left = 0;
933                                 break;
934
935                         case OVERLAY_TOP_CENTER:
936                         case OVERLAY_MID_CENTER:
937                         case OVERLAY_BOTTOM_CENTER:
938                                 left = (data->base_width + data->border_x * 2 - data->overlay_width)/2;
939                                 break;
940
941                         case OVERLAY_TOP_RIGHT:
942                         case OVERLAY_MID_RIGHT:
943                         case OVERLAY_BOTTOM_RIGHT:
944                                 left = data->base_width + data->border_x * 2 - data->overlay_width;
945                                 break;
946
947                         default:
948                                 break;
949                 }
950                 switch (data->position) {
951                         case OVERLAY_TOP_LEFT:
952                         case OVERLAY_TOP_CENTER:
953                         case OVERLAY_TOP_RIGHT:
954                                 top = 0;
955                                 break;
956
957                         case OVERLAY_MID_LEFT:
958                         case OVERLAY_MID_CENTER:
959                         case OVERLAY_MID_RIGHT:
960                                 top = (data->base_height + data->border_y * 2 - data->overlay_height)/2;
961                                 break;
962
963                         case OVERLAY_BOTTOM_LEFT:
964                         case OVERLAY_BOTTOM_CENTER:
965                         case OVERLAY_BOTTOM_RIGHT:
966                                 top = data->base_height + data->border_y * 2 - data->overlay_height;
967                                 break;
968
969                         default:
970                                 break;
971                 }
972         }
973
974         if (data->position != OVERLAY_NONE) {
975                 if (data->is_pixmap) {
976                         cm_return_val_if_fail(data->overlay_pixmap != NULL, FALSE);
977                         cairo_set_source_surface(cr, data->overlay_pixmap, left, top);
978                         cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
979                         cairo_rectangle (cr, left, top, data->overlay_width, data->overlay_height);
980                         cairo_fill(cr);
981                 } else {
982       cm_return_val_if_fail(data->overlay_pixbuf != NULL, FALSE);
983       gdk_cairo_set_source_pixbuf(cr, data->overlay_pixbuf, left, top);
984       cairo_paint(cr);
985                 }
986         }
987
988         return TRUE;
989 }
990
991 #if !GTK_CHECK_VERSION(3,0,0)
992 static gboolean pixmap_with_overlay_expose_event_cb(GtkWidget *widget, GdkEventExpose *expose,
993                                                    OverlayData *data)
994 #else
995 static gboolean pixmap_with_overlay_expose_event_cb(GtkWidget *widget, cairo_t *cr,
996                                                     OverlayData *data)
997 #endif
998 {
999 #if !GTK_CHECK_VERSION(3,0,0)
1000         cairo_t *cr;
1001         GdkWindow *drawable = gtk_widget_get_window(widget);
1002         gboolean result;
1003
1004         cr = gdk_cairo_create(drawable);
1005         gdk_window_clear_area (drawable, expose->area.x, expose->area.y,
1006                                expose->area.width, expose->area.height);
1007
1008         result = do_pix_draw(widget, cr, data);
1009         cairo_destroy(cr);
1010         return result;
1011 #else
1012         return do_pix_draw(widget, cr, data);
1013 #endif
1014 }
1015
1016 static void pixmap_with_overlay_destroy_cb(GtkWidget *object, OverlayData *data)
1017 {
1018         if (data->is_pixmap) {
1019                 cairo_surface_destroy(data->base_pixmap);
1020                 if (data->position != OVERLAY_NONE) {
1021                         cairo_surface_destroy(data->overlay_pixmap);
1022                 }
1023         } else {
1024                 g_object_unref(data->base_pixbuf);
1025                 if (data->position != OVERLAY_NONE) {
1026                         g_object_unref(data->overlay_pixbuf);
1027                 }
1028         }
1029         g_free(data);
1030 }
1031
1032 /**
1033  * \brief Get a widget showing one icon with another overlaid on top of it.
1034  *
1035  * The base icon is always centralised, the other icon can be positioned.
1036  * The overlay icon is ignored if pos=OVERLAY_NONE is used
1037  *
1038  * \param window   top-level window widget
1039  * \param icon     the base icon
1040  * \param overlay  the icon to overlay
1041  * \param pos      how to align the overlay widget, or OVERLAY_NONE for no overlay
1042  * \param border_x size of the border around the base icon (left and right)
1043  * \param border_y size of the border around the base icon (top and bottom)
1044  */
1045 GtkWidget *stock_pixmap_widget_with_overlay(StockPixmap icon,
1046                                             StockPixmap overlay, OverlayPosition pos,
1047                                             gint border_x, gint border_y)
1048 {
1049         cairo_surface_t *stock_pixmap = NULL;
1050         GdkPixbuf *stock_pixbuf = NULL;
1051         GtkWidget *widget = NULL;
1052         GtkWidget *stock_wid = NULL;
1053         GtkRequisition requisition;
1054         OverlayData *data = NULL;
1055
1056         data = g_new0(OverlayData, 1);
1057
1058         stock_wid = stock_pixmap_widget(icon);
1059         gtk_widget_get_requisition(stock_wid, &requisition);
1060
1061 #if !GTK_CHECK_VERSION(3, 0, 0)
1062         if (gtk_image_get_storage_type(GTK_IMAGE(stock_wid)) == GTK_IMAGE_PIXMAP)
1063                 data->is_pixmap = TRUE;
1064         else
1065 #endif
1066                 data->is_pixmap = FALSE;
1067
1068         if (data->is_pixmap) {
1069                 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(stock_wid));
1070                 stock_pixmap = cairo_get_target(cr);
1071                 cairo_surface_reference(stock_pixmap);
1072                 cairo_destroy(cr);
1073                 data->base_pixmap = stock_pixmap;
1074                 data->base_height = requisition.height;
1075                 data->base_width  = requisition.width;
1076                 gtk_widget_destroy(stock_wid);
1077
1078                 if (pos == OVERLAY_NONE) {
1079                         data->overlay_pixmap = NULL;
1080                 } else {
1081                         stock_wid = stock_pixmap_widget(overlay);
1082                         cr = gdk_cairo_create(gtk_widget_get_window(stock_wid));
1083                         stock_pixmap = cairo_get_target(cr);
1084                         cairo_surface_reference(stock_pixmap);
1085                         cairo_destroy(cr);
1086                         data->overlay_pixmap = stock_pixmap;
1087                         data->overlay_height = requisition.height;
1088                         data->overlay_width  = requisition.width;
1089
1090                         gtk_widget_destroy(stock_wid);
1091                 }
1092         } else {
1093                 data->is_pixmap = FALSE;
1094                 stock_pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(stock_wid));
1095                 g_object_ref(stock_pixbuf);
1096                 data->base_pixbuf = stock_pixbuf;
1097                 data->base_height = requisition.height;
1098                 data->base_width  = requisition.width;
1099                 gtk_widget_destroy(stock_wid);
1100                 if (pos == OVERLAY_NONE) {
1101                         data->overlay_pixmap = NULL;
1102                 } else {
1103                         stock_wid = stock_pixmap_widget(overlay);
1104                         stock_pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(stock_wid));
1105                         g_object_ref(stock_pixbuf);
1106                         data->overlay_pixbuf = stock_pixbuf;
1107                         data->overlay_height = requisition.height;
1108                         data->overlay_width  = requisition.width;
1109
1110                         gtk_widget_destroy(stock_wid);
1111                 }
1112         }
1113         data->position = pos;
1114         data->border_x = border_x;
1115         data->border_y = border_y;
1116         data->highlight = FALSE;
1117
1118         widget = gtk_drawing_area_new();
1119         gtk_widget_set_size_request(widget, data->base_width + border_x * 2,
1120                               data->base_height + border_y * 2);
1121 #if !GTK_CHECK_VERSION(3, 0, 0)
1122         g_signal_connect(G_OBJECT(widget), "expose_event",
1123                          G_CALLBACK(pixmap_with_overlay_expose_event_cb), data);
1124 #else
1125         g_signal_connect(G_OBJECT(widget), "draw",
1126                          G_CALLBACK(pixmap_with_overlay_expose_event_cb), data);
1127 #endif
1128         g_signal_connect(G_OBJECT(widget), "destroy",
1129                          G_CALLBACK(pixmap_with_overlay_destroy_cb), data);
1130         g_object_set_data(G_OBJECT(widget), "highlight", &(data->highlight));
1131         return widget;
1132
1133 }