1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/plover-gtk/stockicons.c Sat Nov 15 19:04:45 2014 +0000
1.3 @@ -0,0 +1,141 @@
1.4 +/*
1.5 + * Copyright (C) 2010 J. Ali Harlow <ali@juiblex.co.uk>
1.6 + *
1.7 + * This program is free software; you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation; either version 2 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License along
1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.20 + */
1.21 +
1.22 +#include "config.h"
1.23 +#include <stdlib.h>
1.24 +#ifdef WIN32
1.25 +#include <windows.h>
1.26 +#endif /* WIN32 */
1.27 +#include <glib.h>
1.28 +#include <gtk/gtk.h>
1.29 +#include <plover-gtk/stockicons.h>
1.30 +
1.31 +/* Checks whether a loader for SVG files has been registered
1.32 + * with GdkPixbuf.
1.33 + */
1.34 +static gboolean plover_pixbuf_supports_svg(void)
1.35 +{
1.36 + GSList *formats;
1.37 + GSList *tmp_list;
1.38 + static gint found_svg=-1;
1.39 + gchar **mime_types,**mime_type;
1.40 + if (found_svg!=-1)
1.41 + return found_svg;
1.42 + formats=gdk_pixbuf_get_formats();
1.43 + found_svg=FALSE;
1.44 + for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
1.45 + {
1.46 + mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
1.47 + for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
1.48 + if (!strcmp(*mime_type,"image/svg"))
1.49 + found_svg=TRUE;
1.50 + g_strfreev(mime_types);
1.51 + }
1.52 + g_slist_free(formats);
1.53 + return found_svg;
1.54 +}
1.55 +
1.56 +static void plover_install_icon_at_size(const char *icon_name,
1.57 + GtkIconSet *icon_set,GtkIconSize size,const char *filename)
1.58 +{
1.59 + int w,h;
1.60 + GdkPixbuf *pixbuf;
1.61 + GtkIconSource *source;
1.62 + if (gtk_icon_size_lookup(size,&w,&h))
1.63 + {
1.64 + pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
1.65 + if (pixbuf)
1.66 + {
1.67 + source=gtk_icon_source_new();
1.68 + gtk_icon_source_set_size_wildcarded(source,FALSE);
1.69 + gtk_icon_source_set_size(source,size);
1.70 + gtk_icon_source_set_pixbuf(source,pixbuf);
1.71 + gtk_icon_set_add_source(icon_set,source);
1.72 + gtk_icon_source_free(source);
1.73 + g_object_unref(pixbuf);
1.74 + }
1.75 + }
1.76 +}
1.77 +
1.78 +/**
1.79 + * plover_icons_add_to_stock:
1.80 + * @type: The icon type, typically "apps" or "mimetype"
1.81 + * @name: The icon name (the basename of files containing the icons)
1.82 + *
1.83 + * Find icons in <datadir>/icons/hicolor and add them to the stock images
1.84 + * so that, for example, gtk_image_new_from_stock() will be able find them.
1.85 + *
1.86 + * If there is an SVG loader registered with GdkPixbuf, then:
1.87 + * <datadir>/icons/hicolor/scalable/@type/@name.svg will be used. Otherwise,
1.88 + * <datadir>/icons/hicolor/24x24/@type/@name.png and
1.89 + * <datadir>/icons/hicolor/48x48/@type/@name.png will be used.
1.90 + */
1.91 +void plover_icons_add_to_stock(const char *type,const char *name)
1.92 +{
1.93 + gchar *prefix,*s,*filename;
1.94 + GtkIconSource *source;
1.95 + GtkIconSet *icon_set;
1.96 + GtkIconFactory *factory;
1.97 + factory=gtk_icon_factory_new();
1.98 + icon_set=gtk_icon_set_new();
1.99 +#ifdef WIN32
1.100 + prefix=g_win32_get_package_installation_directory_of_module(NULL);
1.101 +#else
1.102 + prefix=NULL;
1.103 +#endif
1.104 + if (plover_pixbuf_supports_svg())
1.105 + {
1.106 + source=gtk_icon_source_new();
1.107 + s=g_strconcat(name,".svg");
1.108 + filename=g_build_filename(prefix?prefix:"/usr",
1.109 + "share/icons/hicolor/scalable",type,s,NULL);
1.110 + g_free(s);
1.111 + gtk_icon_source_set_filename(source,filename);
1.112 + g_free(filename);
1.113 + gtk_icon_set_add_source(icon_set,source);
1.114 + gtk_icon_source_free(source);
1.115 + }
1.116 + else
1.117 + {
1.118 + s=g_strconcat(name,".png");
1.119 + filename=g_build_filename(prefix?prefix:"/usr",
1.120 + "share/icons/hicolor/24x24",type,s,NULL);
1.121 + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_MENU,
1.122 + filename);
1.123 + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_BUTTON,
1.124 + filename);
1.125 + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,
1.126 + filename);
1.127 + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,
1.128 + filename);
1.129 + g_free(filename);
1.130 + filename=g_build_filename(prefix?prefix:"/usr",
1.131 + "share/icons/hicolor/48x48",type,s,NULL);
1.132 + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DND,
1.133 + filename);
1.134 + plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DIALOG,
1.135 + filename);
1.136 + g_free(filename);
1.137 + g_free(s);
1.138 + }
1.139 + gtk_icon_factory_add(factory,name,icon_set);
1.140 + gtk_icon_set_unref(icon_set);
1.141 + //icon_set=gtk_icon_factory_lookup(factory,name);
1.142 + gtk_icon_factory_add_default(factory);
1.143 + g_object_unref(factory);
1.144 +}