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