diff -r 000000000000 -r 7fbec6da8123 plover-gtk/stockicons.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plover-gtk/stockicons.c Mon Nov 17 11:36:20 2014 +0000 @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2010 J. Ali Harlow + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include +#ifdef WIN32 +#include +#endif /* WIN32 */ +#include +#include +#include + +/* Checks whether a loader for SVG files has been registered + * with GdkPixbuf. + */ +static gboolean plover_pixbuf_supports_svg(void) +{ + GSList *formats; + GSList *tmp_list; + static gint found_svg=-1; + gchar **mime_types,**mime_type; + if (found_svg!=-1) + return found_svg; + formats=gdk_pixbuf_get_formats(); + found_svg=FALSE; + for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next) + { + mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data); + for (mime_type=mime_types;*mime_type && !found_svg;mime_type++) + if (!strcmp(*mime_type,"image/svg")) + found_svg=TRUE; + g_strfreev(mime_types); + } + g_slist_free(formats); + return found_svg; +} + +static void plover_install_icon_at_size(const char *icon_name, + GtkIconSet *icon_set,GtkIconSize size,const char *filename) +{ + int w,h; + GdkPixbuf *pixbuf; + GtkIconSource *source; + if (gtk_icon_size_lookup(size,&w,&h)) + { + pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL); + if (pixbuf) + { + source=gtk_icon_source_new(); + gtk_icon_source_set_size_wildcarded(source,FALSE); + gtk_icon_source_set_size(source,size); + gtk_icon_source_set_pixbuf(source,pixbuf); + gtk_icon_set_add_source(icon_set,source); + gtk_icon_source_free(source); + g_object_unref(pixbuf); + } + } +} + +/** + * plover_icons_add_to_stock: + * @type: The icon type, typically "apps" or "mimetype" + * @name: The icon name (the basename of files containing the icons) + * + * Find icons in /icons/hicolor and add them to the stock images + * so that, for example, gtk_image_new_from_stock() will be able find them. + * + * If there is an SVG loader registered with GdkPixbuf, then: + * /icons/hicolor/scalable/@type/@name.svg will be used. Otherwise, + * /icons/hicolor/24x24/@type/@name.png and + * /icons/hicolor/48x48/@type/@name.png will be used. + */ +void plover_icons_add_to_stock(const char *type,const char *name) +{ + gchar *prefix,*s,*filename; + GtkIconSource *source; + GtkIconSet *icon_set; + GtkIconFactory *factory; + factory=gtk_icon_factory_new(); + icon_set=gtk_icon_set_new(); +#ifdef WIN32 + prefix=g_win32_get_package_installation_directory_of_module(NULL); +#else + prefix=NULL; +#endif + if (plover_pixbuf_supports_svg()) + { + source=gtk_icon_source_new(); + s=g_strconcat(name,".svg"); + filename=g_build_filename(prefix?prefix:"/usr", + "share/icons/hicolor/scalable",type,s,NULL); + g_free(s); + gtk_icon_source_set_filename(source,filename); + g_free(filename); + gtk_icon_set_add_source(icon_set,source); + gtk_icon_source_free(source); + } + else + { + s=g_strconcat(name,".png"); + filename=g_build_filename(prefix?prefix:"/usr", + "share/icons/hicolor/24x24",type,s,NULL); + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_MENU, + filename); + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_BUTTON, + filename); + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR, + filename); + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR, + filename); + g_free(filename); + filename=g_build_filename(prefix?prefix:"/usr", + "share/icons/hicolor/48x48",type,s,NULL); + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DND, + filename); + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DIALOG, + filename); + g_free(filename); + g_free(s); + } + gtk_icon_factory_add(factory,name,icon_set); + gtk_icon_set_unref(icon_set); + //icon_set=gtk_icon_factory_lookup(factory,name); + gtk_icon_factory_add_default(factory); + g_object_unref(factory); +}