2 * Copyright (C) 2010 J. Ali Harlow <ali@juiblex.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <plover-gtk/stockicons.h>
28 /* Checks whether a loader for SVG files has been registered
31 static gboolean plover_pixbuf_supports_svg(void)
35 static gint found_svg=-1;
36 gchar **mime_types,**mime_type;
39 formats=gdk_pixbuf_get_formats();
41 for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
43 mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
44 for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
45 if (!strcmp(*mime_type,"image/svg"))
47 g_strfreev(mime_types);
49 g_slist_free(formats);
53 static void plover_install_icon_at_size(const char *icon_name,
54 GtkIconSet *icon_set,GtkIconSize size,const char *filename)
58 GtkIconSource *source;
59 if (gtk_icon_size_lookup(size,&w,&h))
61 pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
64 source=gtk_icon_source_new();
65 gtk_icon_source_set_size_wildcarded(source,FALSE);
66 gtk_icon_source_set_size(source,size);
67 gtk_icon_source_set_pixbuf(source,pixbuf);
68 gtk_icon_set_add_source(icon_set,source);
69 gtk_icon_source_free(source);
70 g_object_unref(pixbuf);
76 * plover_icons_add_to_stock:
77 * @type: The icon type, typically "apps" or "mimetype"
78 * @name: The icon name (the basename of files containing the icons)
80 * Find icons in <datadir>/icons/hicolor and add them to the stock images
81 * so that, for example, gtk_image_new_from_stock() will be able find them.
83 * If there is an SVG loader registered with GdkPixbuf, then:
84 * <datadir>/icons/hicolor/scalable/@type/@name.svg will be used. Otherwise,
85 * <datadir>/icons/hicolor/24x24/@type/@name.png and
86 * <datadir>/icons/hicolor/48x48/@type/@name.png will be used.
88 void plover_icons_add_to_stock(const char *type,const char *name)
90 gchar *prefix,*s,*filename;
91 GtkIconSource *source;
93 GtkIconFactory *factory;
94 factory=gtk_icon_factory_new();
95 icon_set=gtk_icon_set_new();
97 prefix=g_win32_get_package_installation_directory_of_module(NULL);
101 if (plover_pixbuf_supports_svg())
103 source=gtk_icon_source_new();
104 s=g_strconcat(name,".svg",NULL);
105 filename=g_build_filename(prefix?prefix:"/usr",
106 "share/icons/hicolor/scalable",type,s,NULL);
108 gtk_icon_source_set_filename(source,filename);
110 gtk_icon_set_add_source(icon_set,source);
111 gtk_icon_source_free(source);
115 s=g_strconcat(name,".png",NULL);
116 filename=g_build_filename(prefix?prefix:"/usr",
117 "share/icons/hicolor/24x24",type,s,NULL);
118 plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_MENU,
120 plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_BUTTON,
122 plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,
124 plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,
127 filename=g_build_filename(prefix?prefix:"/usr",
128 "share/icons/hicolor/48x48",type,s,NULL);
129 plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DND,
131 plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DIALOG,
136 gtk_icon_factory_add(factory,name,icon_set);
137 gtk_icon_set_unref(icon_set);
138 //icon_set=gtk_icon_factory_lookup(factory,name);
139 gtk_icon_factory_add_default(factory);
140 g_object_unref(factory);