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