Statistics
| Revision:

root / src / stock_pixmap.c @ 3289

History | View | Annotate | Download (12.8 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2013 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 <glib.h>
21
#include <gtk/gtkwidget.h>
22
#include <gtk/gtkpixmap.h>
23
#include <gtk/gtkimage.h>
24
#include <gtk/gtkicontheme.h>
25

    
26
#include "stock_pixmap.h"
27
#include "gtkutils.h"
28
#include "utils.h"
29

    
30
#include "icons/address.xpm"
31
#include "icons/category.xpm"
32
#include "icons/clip.xpm"
33
#include "icons/complete.xpm"
34
#include "icons/continue.xpm"
35
#include "icons/deleted.xpm"
36
#include "icons/error.xpm"
37
#include "icons/forwarded.xpm"
38
#include "icons/interface.xpm"
39
#include "icons/jpilot.xpm"
40
#include "icons/ldap.xpm"
41
#include "icons/linewrap.xpm"
42
#include "icons/mark.xpm"
43
#include "icons/new.xpm"
44
#include "icons/replied.xpm"
45
#include "icons/unread.xpm"
46
#include "icons/vcard.xpm"
47
#include "icons/online.xpm"
48
#include "icons/offline.xpm"
49
#include "icons/mail.xpm"
50
#include "icons/stock_inbox.h"
51
#include "icons/stock_outbox.h"
52
#include "icons/stock_mail.h"
53
#include "icons/stock_attach.h"
54
#include "icons/stock_mail-compose.h"
55
#include "icons/stock_mail-compose_16.h"
56
#include "icons/stock_mail-forward.h"
57
#include "icons/stock_mail-receive.h"
58
#include "icons/stock_mail_receive_all.h"
59
#include "icons/stock_mail-reply.h"
60
#include "icons/stock_mail-reply-to-all.h"
61
#include "icons/stock_mail-send.h"
62
#include "icons/stock_mail_send_queue.h"
63
#include "icons/stock_insert-file.h"
64
#include "icons/stock_addressbook.h"
65
#include "icons/stock_delete.h"
66
#include "icons/stock_delete_16.h"
67
#include "icons/stock_spam.h"
68
#include "icons/stock_spam_16.h"
69
#include "icons/stock_notspam.h"
70
#include "icons/stock_hand-signed.h"
71
#include "icons/stock_sylpheed.h"
72
#include "icons/stock_sylpheed_16.h"
73
#include "icons/stock_sylpheed_32.h"
74
#include "icons/stock_sylpheed_newmail.h"
75
#include "icons/stock_sylpheed_newmail_16.h"
76
#include "icons/sylpheed-logo.h"
77
#include "icons/stock_person.h"
78
#include "icons/stock_book.h"
79
#include "icons/folder-close.h"
80
#include "icons/folder-open.h"
81
#include "icons/folder-noselect.h"
82
#include "icons/folder-search.h"
83
#include "icons/group.h"
84
#include "icons/html.h"
85

    
86
typedef struct _StockPixmapData        StockPixmapData;
87

    
88
struct _StockPixmapData
89
{
90
        gchar **data;
91
        GdkPixmap *pixmap;
92
        GdkBitmap *mask;
93

    
94
        GdkPixbuf *pixbuf;
95
        const guint8 *pixbuf_data;
96
        gint pixbuf_data_len;
97

    
98
        gchar *icon_name;
99
        gint size;
100

    
101
        gchar *icon_file;
102
        GdkPixbuf *icon_file_pixbuf;
103
};
104

    
105
static StockPixmapData pixmaps[] =
106
{
107
        {address_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "address.png"},
108
        {NULL, NULL, NULL, NULL, stock_book, sizeof(stock_book), "stock_book", 16, "stock_book.png"},
109
        {category_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "category.png"},
110
        {clip_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "clip.png"},
111
        {complete_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "complete.png"},
112
        {continue_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "continue.png"},
113
        {deleted_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "deleted.png"},
114
        {NULL, NULL, NULL, NULL, folder_close, sizeof(folder_close), "folder-close", 0, "folder-close.png"},
115
        {NULL, NULL, NULL, NULL, folder_open, sizeof(folder_open), "folder-open", 0, "folder-open.png"},
116
        {NULL, NULL, NULL, NULL, folder_noselect, sizeof(folder_noselect), "folder-noselect", 0, "folder-noselect.png"},
117
        {error_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "error.png"},
118
        {forwarded_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "forwarded.png"},
119
        {NULL, NULL, NULL, NULL, group, sizeof(group), "group", 0, "group.png"},
120
        {NULL, NULL, NULL, NULL, html, sizeof(html), "html", 0, "html.png"},
121
        {interface_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "interface.png"},
122
        {jpilot_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "jpilot.png"},
123
        {ldap_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "ldap.png"},
124
        {linewrap_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "linewrap.png"},
125
        {mark_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "mark.png"},
126
        {new_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "new.png"},
127
        {replied_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "replied.png"},
128
        {unread_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "unread.png"},
129
        {vcard_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "vcard.png"},
130
        {online_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "online.png"},
131
        {offline_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "offline.png"},
132
        {mail_xpm         , NULL, NULL, NULL, NULL, 0, NULL, 0, "mail.png"},
133

    
134
        {NULL, NULL, NULL, NULL, stock_inbox, sizeof(stock_inbox), "stock_inbox", 16, "stock_inbox.png"},
135
        {NULL, NULL, NULL, NULL, stock_outbox, sizeof(stock_outbox), "stock_outbox", 16, "stock_outbox.png"},
136
        {NULL, NULL, NULL, NULL, stock_mail_compose_16, sizeof(stock_mail_compose_16), "stock_mail-compose", 16, "stock_mail-compose_16.png"},
137
        {NULL, NULL, NULL, NULL, stock_delete_16, sizeof(stock_delete_16), GTK_STOCK_DELETE, 16, "stock_delete_16.png"},
138
        {NULL, NULL, NULL, NULL, stock_mail, sizeof(stock_mail), "stock_mail", 24, "stock_mail.png"},
139
        {NULL, NULL, NULL, NULL, stock_attach, sizeof(stock_attach), "stock_attach", 24, "stock_attach.png"},
140
        {NULL, NULL, NULL, NULL, stock_mail_compose, sizeof(stock_mail_compose), "stock_mail-compose", 24, "stock_mail-compose.png"},
141
        {NULL, NULL, NULL, NULL, stock_mail_forward, sizeof(stock_mail_forward), "stock_mail-forward", 24, "stock_mail-forward.png"},
142
        {NULL, NULL, NULL, NULL, stock_mail_receive, sizeof(stock_mail_receive), "stock_mail-receive", 24, "stock_mail-receive.png"},
143
        {NULL, NULL, NULL, NULL, stock_mail_receive_all, sizeof(stock_mail_receive_all), NULL, 0, "stock_mail_receive_all.png"},
144
        {NULL, NULL, NULL, NULL, stock_mail_reply, sizeof(stock_mail_reply), "stock_mail-reply", 24, "stock_mail-reply.png"},
145
        {NULL, NULL, NULL, NULL, stock_mail_reply_to_all, sizeof(stock_mail_reply_to_all), "stock_mail-reply-to-all", 24, "stock_mail-reply-to-all.png"},
146
        {NULL, NULL, NULL, NULL, stock_mail_send, sizeof(stock_mail_send), "stock_mail-send", 24, "stock_mail-send.png"},
147
        {NULL, NULL, NULL, NULL, stock_mail_send_queue, sizeof(stock_mail_send_queue), NULL, 0, "stock_mail_send_queue.png"},
148
        {NULL, NULL, NULL, NULL, stock_insert_file, sizeof(stock_insert_file), "stock_insert-file", 24, "stock_insert-file.png"},
149
        {NULL, NULL, NULL, NULL, stock_addressbook, sizeof(stock_addressbook), "stock_addressbook", 24, "stock_addressbook.png"},
150
        {NULL, NULL, NULL, NULL, stock_delete, sizeof(stock_delete), GTK_STOCK_DELETE, 24, "stock_delete.png"},
151
        {NULL, NULL, NULL, NULL, stock_spam, sizeof(stock_spam), "stock_spam", 24, "stock_spam.png"},
152
        {NULL, NULL, NULL, NULL, stock_notspam, sizeof(stock_notspam), "stock_notspam", 24, "stock_notspam.png"},
153
        {NULL, NULL, NULL, NULL, stock_hand_signed, sizeof(stock_hand_signed), "stock_hand-signed", 24, "stock_hand-signed.png"},
154
        {NULL, NULL, NULL, NULL, stock_sylpheed, sizeof(stock_sylpheed), NULL, 0, "stock_sylpheed.png"},
155
        {NULL, NULL, NULL, NULL, stock_sylpheed_16, sizeof(stock_sylpheed_16), NULL, 0, "stock_sylpheed_16.png"},
156
        {NULL, NULL, NULL, NULL, stock_sylpheed_32, sizeof(stock_sylpheed_32), NULL, 0, "stock_sylpheed_32.png"},
157
        {NULL, NULL, NULL, NULL, stock_sylpheed_newmail, sizeof(stock_sylpheed_newmail), NULL, 0, "stock_sylpheed_newmail.png"},
158
        {NULL, NULL, NULL, NULL, stock_sylpheed_newmail_16, sizeof(stock_sylpheed_newmail_16), NULL, 0, "stock_sylpheed_newmail_16.png"},
159
        {NULL, NULL, NULL, NULL, sylpheed_logo, sizeof(sylpheed_logo), NULL, 0, "sylpheed-logo.png"},
160
        {NULL, NULL, NULL, NULL, stock_person, sizeof(stock_person), "stock_person", 16, "stock_person.png"},
161
        {NULL, NULL, NULL, NULL, folder_search, sizeof(folder_search), "folder-search", 0, "folder-search.png"},
162
        {NULL, NULL, NULL, NULL, stock_spam_16, sizeof(stock_spam_16), "stock_spam", 16, "stock_spam_16.png"},
163

    
164
        /* for toolbar */
165
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_NETWORK, 24, "remote-mailbox.png"},
166
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_GO_DOWN, 24, "next.png"},
167
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_GO_UP, 24, "prev.png"},
168
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_FIND, 24, "search.png"},
169
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_PRINT, 24, "print.png"},
170
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_STOP, 24, "stop.png"},
171
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_EXECUTE, 24, "execute.png"},
172
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_PREFERENCES, 24, "common-prefs.png"},
173
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_PREFERENCES, 24, "account-prefs.png"},
174
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_SAVE, 24, "save.png"},
175
        {NULL, NULL, NULL, NULL, NULL, 0, GTK_STOCK_EDIT, 24, "editor.png"},
176
};
177

    
178
static gchar *theme_dir = NULL;
179

    
180

    
181
GtkWidget *stock_pixbuf_widget(GtkWidget *window, StockPixmap icon)
182
{
183
        GdkPixbuf *pixbuf;
184

    
185
        g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
186

    
187
        stock_pixbuf_gdk(window, icon, &pixbuf);
188
        return gtk_image_new_from_pixbuf(pixbuf);
189
}
190

    
191
GtkWidget *stock_pixbuf_widget_scale(GtkWidget *window, StockPixmap icon,
192
                                     gint width, gint height)
193
{
194
        GdkPixbuf *pixbuf;
195
        GdkPixbuf *scaled_pixbuf;
196
        GtkWidget *image;
197

    
198
        g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
199

    
200
        stock_pixbuf_gdk(window, icon, &pixbuf);
201
        scaled_pixbuf = gdk_pixbuf_scale_simple(pixbuf, width, height,
202
                                                GDK_INTERP_HYPER);
203
        image = gtk_image_new_from_pixbuf(scaled_pixbuf);
204
        g_object_unref(scaled_pixbuf);
205

    
206
        return image;
207
}
208

    
209
static gboolean stock_pixbuf_load_theme_icon_file(StockPixmap icon)
210
{
211
        StockPixmapData *pix_d;
212

    
213
        g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, FALSE);
214

    
215
        pix_d = &pixmaps[icon];
216

    
217
        if (!pix_d->icon_file_pixbuf && pix_d->icon_file && theme_dir) {
218
                gchar *path;
219

    
220
                path = g_strconcat(theme_dir, G_DIR_SEPARATOR_S,
221
                                   pix_d->icon_file, NULL);
222
                debug_print("stock_pixbuf_load_theme_icon_file: checking theme icon: %s\n", path);
223
                if (is_file_exist(path)) {
224
                        debug_print("stock_pixbuf_load_theme_icon_file: loading theme icon: %s\n", path);
225
                        pix_d->icon_file_pixbuf = gdk_pixbuf_new_from_file(path, NULL);
226
                        if (!pix_d->icon_file_pixbuf) {
227
                                g_warning("stock_pixbuf_load_theme_icon_file: loading of theme icon failed: %s", path);
228
                                pix_d->icon_file = NULL;
229
                        } else if (!pix_d->pixbuf)
230
                                pix_d->pixbuf = pix_d->icon_file_pixbuf;
231
                } else
232
                        pix_d->icon_file = NULL;
233
                g_free(path);
234
        }
235

    
236
        return pix_d->icon_file_pixbuf != NULL;
237
}
238

    
239
GtkWidget *stock_pixbuf_widget_for_toolbar(StockPixmap icon)
240
{
241
        GtkWidget *image = NULL;
242
        StockPixmapData *pix_d;
243

    
244
        g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
245

    
246
        stock_pixbuf_load_theme_icon_file(icon);
247

    
248
        pix_d = &pixmaps[icon];
249

    
250
        if (pix_d->icon_file_pixbuf) {
251
                image = gtk_image_new_from_pixbuf(pix_d->icon_file_pixbuf);
252
        }
253

    
254
        if (!image && pix_d->icon_name) {
255
                if (gtk_style_lookup_icon_set(gtk_widget_get_default_style(), pix_d->icon_name))
256
                        image = gtk_image_new_from_stock(pix_d->icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
257
                else
258
                        pix_d->icon_name = NULL;
259
        }
260

    
261
        if (!image)
262
                image = stock_pixbuf_widget(NULL, icon);
263

    
264
        return image;
265
}
266

    
267
/* create GdkPixmap if it has not created yet */
268
gint stock_pixmap_gdk(GtkWidget *window, StockPixmap icon,
269
                      GdkPixmap **pixmap, GdkBitmap **mask)
270
{
271
        StockPixmapData *pix_d;
272

    
273
        if (pixmap) *pixmap = NULL;
274
        if (mask)   *mask   = NULL;
275

    
276
        g_return_val_if_fail(window != NULL, -1);
277
        g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
278

    
279
        pix_d = &pixmaps[icon];
280

    
281
        g_return_val_if_fail(pix_d->data != NULL, -1);
282

    
283
        if (!pix_d->pixmap) {
284
                PIXMAP_CREATE(window, pix_d->pixmap, pix_d->mask,
285
                              pix_d->data);
286
        }
287

    
288
        if (pixmap) *pixmap = pix_d->pixmap;
289
        if (mask)   *mask   = pix_d->mask;
290

    
291
        return 0;
292
}
293

    
294
gint stock_pixbuf_gdk(GtkWidget *window, StockPixmap icon, GdkPixbuf **pixbuf)
295
{
296
        StockPixmapData *pix_d;
297

    
298
        if (pixbuf)
299
                *pixbuf = NULL;
300

    
301
        g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
302

    
303
        stock_pixbuf_load_theme_icon_file(icon);
304

    
305
        pix_d = &pixmaps[icon];
306

    
307
        if (!pix_d->pixbuf && pix_d->pixbuf_data)
308
                pix_d->pixbuf = gdk_pixbuf_new_from_inline
309
                        (pix_d->pixbuf_data_len, pix_d->pixbuf_data,
310
                         FALSE, NULL);
311
        if (!pix_d->pixbuf && pix_d->icon_name)
312
                pix_d->pixbuf = gtk_icon_theme_load_icon
313
                        (gtk_icon_theme_get_default(),
314
                         pix_d->icon_name, pix_d->size, 0, NULL);
315
        if (!pix_d->pixbuf && pix_d->data)
316
                pix_d->pixbuf = gdk_pixbuf_new_from_xpm_data
317
                        ((const gchar **)pix_d->data);
318
        if (!pix_d->pixbuf) {
319
                g_warning("can't read image %d\n", icon);
320
                return -1;
321
        }
322

    
323
        if (pixbuf)
324
                *pixbuf = pix_d->pixbuf;
325

    
326
        return 0;
327
}
328

    
329
gint stock_pixbuf_set_theme_dir(const gchar *dir)
330
{
331
        g_free(theme_dir);
332
        theme_dir = g_strdup(dir);
333

    
334
        return 0;
335
}