afb97568e1b388d082437ef6cf0f0301ed5cc1de
[claws.git] / src / stock_pixmap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21 #include <glib.h>
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkpixmap.h>
24 #include <string.h>
25 #include <dirent.h>
26
27 #include "stock_pixmap.h"
28 #include "gtkutils.h"
29 #include "utils.h"
30 #include "prefs_common.h"
31
32 #include "pixmaps/address.xpm"
33 #include "pixmaps/book.xpm"
34 #include "pixmaps/category.xpm"
35 #include "pixmaps/checkbox_off.xpm"
36 #include "pixmaps/checkbox_on.xpm"
37 #include "pixmaps/clip.xpm"
38 #include "pixmaps/clipkey.xpm"
39 #include "pixmaps/complete.xpm"
40 #include "pixmaps/continue.xpm"
41 #include "pixmaps/deleted.xpm"
42 #include "pixmaps/dir_close.xpm"
43 #include "pixmaps/dir_open.xpm"
44 #include "pixmaps/dir_open_hrm.xpm"
45 #include "pixmaps/error.xpm"
46 #include "pixmaps/forwarded.xpm"
47 #include "pixmaps/group.xpm"
48 #include "pixmaps/inbox.xpm"
49 #include "pixmaps/inbox_hrm.xpm"
50 #include "pixmaps/interface.xpm"
51 #include "pixmaps/jpilot.xpm"
52 #include "pixmaps/key.xpm"
53 #include "pixmaps/ldap.xpm"
54 #include "pixmaps/linewrap.xpm"
55 #include "pixmaps/mark.xpm"
56 #include "pixmaps/locked.xpm"
57 #include "pixmaps/new.xpm"
58 #include "pixmaps/outbox.xpm"
59 #include "pixmaps/outbox_hrm.xpm"
60 #include "pixmaps/replied.xpm"
61 #include "pixmaps/close.xpm"
62 #include "pixmaps/down_arrow.xpm"
63 #include "pixmaps/up_arrow.xpm"
64 #include "pixmaps/exec.xpm"
65 #include "pixmaps/mail.xpm"
66 #include "pixmaps/mail_attach.xpm"
67 #include "pixmaps/mail_compose.xpm"
68 #include "pixmaps/mail_forward.xpm"
69 #include "pixmaps/mail_receive.xpm"
70 #include "pixmaps/mail_receive_all.xpm"
71 #include "pixmaps/mail_reply.xpm"
72 #include "pixmaps/mail_reply_to_all.xpm"
73 #include "pixmaps/mail_reply_to_author.xpm"
74 #include "pixmaps/mail_send.xpm"
75 #include "pixmaps/mail_send_queue.xpm"
76 #include "pixmaps/news_compose.xpm"
77 #include "pixmaps/paste.xpm"
78 #include "pixmaps/preferences.xpm"
79 #include "pixmaps/properties.xpm"
80 #include "pixmaps/sylpheed_logo.xpm"
81 #include "pixmaps/address_book.xpm"
82 #include "pixmaps/trash.xpm"
83 #include "pixmaps/trash_hrm.xpm"
84 #include "pixmaps/unread.xpm"
85 #include "pixmaps/vcard.xpm"
86 #include "pixmaps/ignorethread.xpm"
87 #include "pixmaps/work_online.xpm"
88 #include "pixmaps/work_offline.xpm"
89 #include "pixmaps/notice_warn.xpm"
90 #include "pixmaps/notice_error.xpm"
91 #include "pixmaps/notice_note.xpm"
92 #include "pixmaps/quicksearch.xpm"
93
94 typedef struct _StockPixmapData StockPixmapData;
95
96 struct _StockPixmapData
97 {
98         gchar **data;
99         GdkPixmap *pixmap;
100         GdkBitmap *mask;
101         gchar *file;
102         gchar *icon_path;
103 };
104
105 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname);
106
107 static StockPixmapData pixmaps[] =
108 {
109         {address_xpm                    , NULL, NULL, "address", "  "},
110         {address_book_xpm               , NULL, NULL, "address_book", "  "},
111         {book_xpm                               , NULL, NULL, "book", "  "},
112         {category_xpm                   , NULL, NULL, "category", "  "},
113         {checkbox_off_xpm               , NULL, NULL, "checkbox_off", "  "},
114         {checkbox_on_xpm                , NULL, NULL, "checkbox_on", "  "},
115         {clip_xpm                               , NULL, NULL, "clip", "  "},
116         {clipkey_xpm                    , NULL, NULL, "clipkey", "  "},
117         {close_xpm                              , NULL, NULL, "close", "  "},
118         {complete_xpm                   , NULL, NULL, "complete", "  "},
119         {continue_xpm                   , NULL, NULL, "continue", "  "},
120         {deleted_xpm                    , NULL, NULL, "deleted", "  "},
121         {dir_close_xpm                  , NULL, NULL, "dir_close", "  "},
122         {dir_close_xpm                  , NULL, NULL, "dir_close_hrm", " "},
123         {dir_open_xpm                   , NULL, NULL, "dir_open", "  "},
124         {dir_open_hrm_xpm               , NULL, NULL, "dir_open_hrm", "  "},
125         {down_arrow_xpm                 , NULL, NULL, "down_arrow", "  "},
126         {up_arrow_xpm                   , NULL, NULL, "up_arrow", "  "},
127         {mail_compose_xpm               , NULL, NULL, "edit_extern", "  "},
128         {error_xpm                              , NULL, NULL, "error", "  "},
129         {exec_xpm                               , NULL, NULL, "exec", "  "},
130         {forwarded_xpm                  , NULL, NULL, "forwarded", "  "},
131         {group_xpm                              , NULL, NULL, "group", "  "},
132         {ignorethread_xpm               , NULL, NULL, "ignorethread", "  "},
133         {inbox_xpm                              , NULL, NULL, "inbox_close", "  "},
134         {inbox_hrm_xpm                  , NULL, NULL, "inbox_close_hrm", "  "},
135         {inbox_xpm                              , NULL, NULL, "inbox_open", "  "},
136         {inbox_hrm_xpm                  , NULL, NULL, "inbox_open_hrm", "  "},
137         {paste_xpm                              , NULL, NULL, "insert_file", "  "},
138         {interface_xpm                  , NULL, NULL, "interface", "  "},
139         {jpilot_xpm                             , NULL, NULL, "jpilot", "  "},
140         {key_xpm                                , NULL, NULL, "key", "  "},
141         {ldap_xpm                               , NULL, NULL, "ldap", "  "},
142         {linewrap_xpm                   , NULL, NULL, "linewrap", "  "},
143         {locked_xpm                             , NULL, NULL, "locked", "  "},
144         {mail_xpm                               , NULL, NULL, "mail", "  "},
145         {mail_attach_xpm                , NULL, NULL, "mail_attach", "  "},
146         {mail_compose_xpm               , NULL, NULL, "mail_compose", "  "},
147         {mail_forward_xpm               , NULL, NULL, "mail_forward", "  "},
148         {mail_receive_xpm               , NULL, NULL, "mail_receive", "  "},
149         {mail_receive_all_xpm   , NULL, NULL, "mail_receive_all", "  "},
150         {mail_reply_xpm                 , NULL, NULL, "mail_reply", "  "},
151         {mail_reply_to_all_xpm  , NULL, NULL, "mail_reply_to_all", "  "},
152         {mail_reply_to_author_xpm
153                                                         , NULL, NULL, "mail_reply_to_author", "  "},
154         {mail_send_xpm                  , NULL, NULL, "mail_send", "  "},
155         {mail_send_queue_xpm    , NULL, NULL, "mail_send_queue", "  "},
156         {mail_xpm                               , NULL, NULL, "mail_sign", "  "},
157         {mark_xpm                               , NULL, NULL, "mark", "  "},
158         {new_xpm                                , NULL, NULL, "new", "  "},
159         {news_compose_xpm               , NULL, NULL, "news_compose", "  "},
160         {outbox_xpm                             , NULL, NULL, "outbox_close", "  "},
161         {outbox_hrm_xpm                 , NULL, NULL, "outbox_close_hrm", "  "},
162         {outbox_xpm                             , NULL, NULL, "outbox_open", "  "},
163         {outbox_hrm_xpm                 , NULL, NULL, "outbox_open_hrm", "  "},
164         {replied_xpm                    , NULL, NULL, "replied", "  "},
165         {paste_xpm                              , NULL, NULL, "paste", "  "},
166         {preferences_xpm                , NULL, NULL, "preferences", "  "},
167         {properties_xpm                 , NULL, NULL, "properties", "  "},
168         {outbox_xpm                             , NULL, NULL, "queue_close", "  "},
169         {outbox_hrm_xpm                 , NULL, NULL, "queue_close_hrm", "  "},
170         {outbox_xpm                             , NULL, NULL, "queue_open", "  "},
171         {outbox_hrm_xpm                 , NULL, NULL, "queue_open_hrm", "  "},
172         {trash_xpm                              , NULL, NULL, "trash_open", "  "},
173         {trash_hrm_xpm                  , NULL, NULL, "trash_open_hrm", "  "},
174         {trash_xpm                              , NULL, NULL, "trash_close", "  "},
175         {trash_hrm_xpm                  , NULL, NULL, "trash_close_hrm", "  "},
176         {unread_xpm                             , NULL, NULL, "unread", "  "},
177         {vcard_xpm                              , NULL, NULL, "vcard", "  "},
178         {work_online_xpm                        , NULL, NULL, "work_online", "  "},
179         {work_offline_xpm                       , NULL, NULL, "work_offline", "  "},
180         {notice_warn_xpm                        , NULL, NULL, "notice_warn",  "  "},
181         {notice_error_xpm                       , NULL, NULL, "notice_error",  "  "},
182         {notice_note_xpm                        , NULL, NULL, "notice_note",  "  "},
183         {quicksearch_xpm                        , NULL, NULL, "quicksearch",  "  "},
184         {sylpheed_logo_xpm                      , NULL, NULL, "sylpheed_logo", "  "},
185 };
186
187 /* return newly constructed GtkPixmap from GdkPixmap */
188 GtkWidget *stock_pixmap_widget(GtkWidget *window, StockPixmap icon)
189 {
190         GdkPixmap *pixmap;
191         GdkBitmap *mask;
192
193         g_return_val_if_fail(window != NULL, NULL);
194         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
195
196         if (stock_pixmap_gdk(window, icon, &pixmap, &mask) != -1)
197                 return gtk_pixmap_new(pixmap, mask);
198         
199         return NULL;
200 }
201
202 /* create GdkPixmap if it has not created yet */
203 gint stock_pixmap_gdk(GtkWidget *window, StockPixmap icon,
204                       GdkPixmap **pixmap, GdkBitmap **mask)
205 {
206         StockPixmapData *pix_d;
207
208         if (pixmap) *pixmap = NULL;
209         if (mask)   *mask   = NULL;
210
211         g_return_val_if_fail(window != NULL, -1);
212         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
213
214         pix_d = &pixmaps[icon];
215
216         if (!pix_d->pixmap || (strcmp(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
217                 GdkPixmap *pix = NULL;
218         
219                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
220                         if ( is_dir_exist(prefs_common.pixmap_theme_path) ) {
221                                 char *icon_file_name; 
222                                 
223                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
224                                                              G_DIR_SEPARATOR_S,
225                                                              pix_d->file,
226                                                              ".xpm",
227                                                              NULL);
228                                 if (is_file_exist(icon_file_name))
229                                         PIXMAP_CREATE_FROM_FILE(window, pix, pix_d->mask, icon_file_name);
230                                 if (pix) 
231                                         pix_d->icon_path = prefs_common.pixmap_theme_path;
232                                 g_free(icon_file_name);
233                         } else {
234                                 /* even the path does not exist (deleted between two sessions), so
235                                 set the preferences to the internal theme */
236                                 prefs_common.pixmap_theme_path = DEFAULT_PIXMAP_THEME;
237                         }
238                 }
239                 pix_d->pixmap = pix;
240         }
241
242         if (!pix_d->pixmap) {
243                 PIXMAP_CREATE(window, pix_d->pixmap, pix_d->mask, pix_d->data);
244                 if (pix_d->pixmap) 
245                         pix_d->icon_path = DEFAULT_PIXMAP_THEME;        
246         }
247
248         g_return_val_if_fail(pix_d->pixmap != NULL, -1);
249         
250         if (pixmap) 
251                 *pixmap = pix_d->pixmap;
252         if (mask)   
253                 *mask   = pix_d->mask;
254
255         return 0;
256 }
257
258 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname)
259 {
260         struct dirent *d;
261         DIR *dp;
262         
263         if ((dp = opendir(dirname)) == NULL) {
264                 FILE_OP_ERROR(dirname, "opendir");
265                 return;
266         }
267         
268         while ((d = readdir(dp)) != NULL) {
269                 gchar *entry;
270                 gchar *fullentry;
271
272                 entry     = d->d_name;
273                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
274                 
275                 if (strcmp(entry, ".") != 0 && strcmp(entry, "..") != 0 && is_dir_exist(fullentry)) {
276                         gchar *filetoexist;
277                         int i;
278                         
279                         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
280                                 filetoexist = g_strconcat(fullentry, G_DIR_SEPARATOR_S, pixmaps[i].file, ".xpm", NULL);
281                                 if (is_file_exist(filetoexist)) {
282                                         *list = g_list_append(*list, fullentry);
283                                         break;
284                                 }
285                         }
286                         g_free(filetoexist);
287                         if (i == N_STOCK_PIXMAPS) 
288                                 g_free(fullentry);
289                 } else 
290                         g_free(fullentry);
291         }
292         closedir(dp);
293 }
294
295 GList *stock_pixmap_themes_list_new(void)
296 {
297         gchar *defaulttheme;
298         gchar *userthemes;
299         gchar *systemthemes;
300         GList *list = NULL;
301         
302         defaulttheme = g_strdup(DEFAULT_PIXMAP_THEME);
303         userthemes   = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
304                                    RC_DIR, G_DIR_SEPARATOR_S, 
305                                    PIXMAP_THEME_DIR, NULL);
306         systemthemes = g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
307                                    PIXMAP_THEME_DIR, NULL);
308
309         list = g_list_append(list, defaulttheme);
310         
311         stock_pixmap_find_themes_in_dir(&list, userthemes);
312         stock_pixmap_find_themes_in_dir(&list, systemthemes);
313
314         g_free(userthemes);
315         g_free(systemthemes);
316         return list;
317 }
318
319 void stock_pixmap_themes_list_free(GList *list)
320 {
321         GList *ptr;
322
323         for (ptr = g_list_first(list); ptr != NULL; ptr = g_list_next(ptr)) 
324                 if (ptr->data)
325                         g_free(ptr->data);
326         g_list_free(list);              
327 }
328
329 gchar *stock_pixmap_get_name (StockPixmap icon)
330 {
331         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
332         
333         return pixmaps[icon].file;
334
335 }
336
337 StockPixmap stock_pixmap_get_icon (gchar *file)
338 {
339         gint i;
340         
341         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
342                 if (strcmp (pixmaps[i].file, file) == 0)
343                         return i;
344         }
345         return -1;
346 }
347