1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 * gdl-stock.c
3 *
4 * Copyright (C) 2003 Jeroen Zwartepoorte
5 *
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <gtk/gtk.h>
27 #include <gtk/gtkiconfactory.h>
28 #include "gdl-stock.h"
29 #include "gdl-stock-icons.h"
31 static GtkIconFactory *gdl_stock_factory = NULL;
33 static struct {
34 const gchar *stock_id;
35 const guint8 *icon_data;
36 const guint data_size;
37 }
38 gdl_icons[] =
39 {
40 { GDL_STOCK_CLOSE, stock_close_icon, sizeof (stock_close_icon) },
41 { GDL_STOCK_MENU_LEFT, stock_menu_left_icon, sizeof (stock_menu_left_icon) },
42 { GDL_STOCK_MENU_RIGHT, stock_menu_right_icon, sizeof (stock_menu_right_icon) }
43 };
45 static void
46 icon_set_from_data (GtkIconSet *set,
47 const guint8 *icon_data,
48 const guint data_size,
49 GtkIconSize size,
50 gboolean fallback)
51 {
52 GtkIconSource *source;
53 GdkPixbuf *pixbuf;
54 GError *err = NULL;
56 source = gtk_icon_source_new ();
58 gtk_icon_source_set_size (source, size);
59 gtk_icon_source_set_size_wildcarded (source, FALSE);
61 pixbuf = gdk_pixbuf_new_from_inline (data_size, icon_data, FALSE, &err);
62 if (err) {
63 g_warning ("%s",err->message);
64 g_error_free (err);
65 err = NULL;
66 g_object_unref (source);
67 return;
68 }
70 gtk_icon_source_set_pixbuf (source, pixbuf);
72 g_object_unref (pixbuf);
74 gtk_icon_set_add_source (set, source);
76 if (fallback) {
77 gtk_icon_source_set_size_wildcarded (source, TRUE);
78 gtk_icon_set_add_source (set, source);
79 }
81 gtk_icon_source_free (source);
82 }
84 static void
85 add_icon (GtkIconFactory *factory,
86 const gchar *stock_id,
87 const guint8 *icon_data,
88 const guint data_size)
89 {
90 GtkIconSet *set;
91 gboolean fallback = FALSE;
93 set = gtk_icon_factory_lookup (factory, stock_id);
95 if (!set) {
96 set = gtk_icon_set_new ();
97 gtk_icon_factory_add (factory, stock_id, set);
98 gtk_icon_set_unref (set);
100 fallback = TRUE;
101 }
103 icon_set_from_data (set, icon_data, data_size, GTK_ICON_SIZE_MENU, fallback);
104 }
106 void
107 gdl_stock_init (void)
108 {
109 static gboolean initialized = FALSE;
110 gint i;
112 if (initialized)
113 return;
115 gdl_stock_factory = gtk_icon_factory_new ();
117 for (i = 0; i < G_N_ELEMENTS (gdl_icons); i++) {
118 add_icon (gdl_stock_factory,
119 gdl_icons[i].stock_id,
120 gdl_icons[i].icon_data,
121 gdl_icons[i].data_size);
122 }
124 gtk_icon_factory_add_default (gdl_stock_factory);
126 initialized = TRUE;
127 }