plover-gtk/stockicons.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Apr 24 20:58:03 2018 +0100 (2018-04-24)
changeset 68 4348cb0e1ac3
parent 30 3ee18a3a0f58
child 106 cc42fad3fe31
permissions -rw-r--r--
Support expat version 2.2
     1 /*
     2  * Copyright (C) 2010  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 #include <stdlib.h>
    21 #ifdef WIN32
    22 #include <windows.h>
    23 #endif	/* WIN32 */
    24 #include <glib.h>
    25 #include <gtk/gtk.h>
    26 #include <plover-gtk/stockicons.h>
    27 
    28 /* Checks whether a loader for SVG files has been registered
    29  * with GdkPixbuf.
    30  */
    31 static gboolean plover_pixbuf_supports_svg(void)
    32 {
    33     GSList *formats;
    34     GSList *tmp_list;
    35     static gint found_svg=-1;
    36     gchar **mime_types,**mime_type;
    37     if (g_getenv("PLOVER_IGNORE_SVG_SUPPORT"))
    38 	return FALSE;
    39     if (found_svg!=-1)
    40 	return found_svg;
    41     formats=gdk_pixbuf_get_formats();
    42     found_svg=FALSE;
    43     for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
    44     {
    45 	mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
    46 	for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
    47 	    if (!strcmp(*mime_type,"image/svg"))
    48 		found_svg=TRUE;
    49 	g_strfreev(mime_types);
    50     }
    51     g_slist_free(formats);
    52     return found_svg;
    53 }
    54 
    55 static void plover_install_icon_at_size(const char *icon_name,
    56   GtkIconSet *icon_set,GtkIconSize size,const char *filename)
    57 {
    58     int w,h;
    59     GdkPixbuf *pixbuf;
    60     GtkSettings *settings;
    61     GtkIconSource *source;
    62     GError *err=NULL;
    63     settings=gtk_settings_get_default();
    64     if (!settings)
    65 	g_warning("plover: Can't add icons without a default screen");
    66     else if (gtk_icon_size_lookup_for_settings(settings,size,&w,&h))
    67     {
    68 	pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,&err);
    69 	if (pixbuf)
    70 	{
    71 	    source=gtk_icon_source_new();
    72 	    gtk_icon_source_set_size_wildcarded(source,FALSE);
    73 	    gtk_icon_source_set_size(source,size);
    74 	    gtk_icon_source_set_pixbuf(source,pixbuf);
    75 	    gtk_icon_set_add_source(icon_set,source);
    76 	    gtk_icon_source_free(source);
    77 	    g_object_unref(pixbuf);
    78 	}
    79 	else
    80 	{
    81 	    g_warning("%s: %s",filename,err->message);
    82 	    g_error_free(err);
    83 	}
    84     }
    85 }
    86 
    87 /**
    88  * plover_icons_add_to_stock:
    89  * @type: The icon type, typically "apps" or "mimetype"
    90  * @name: The icon name (the basename of files containing the icons)
    91  *
    92  * Find icons in <datadir>/icons/hicolor and add them to the stock images
    93  * so that, for example, gtk_image_new_from_stock() will be able find them.
    94  *
    95  * If there is an SVG loader registered with GdkPixbuf, then:
    96  * <datadir>/icons/hicolor/scalable/@type/@name.svg will be used. Otherwise,
    97  * <datadir>/icons/hicolor/24x24/@type/@name.png and
    98  * <datadir>/icons/hicolor/48x48/@type/@name.png will be used.
    99  */
   100 void plover_icons_add_to_stock(const char *type,const char *name)
   101 {
   102     gchar *datadir,*prefix,*s,*filename;
   103     GtkIconSource *source;
   104     GtkIconSet *icon_set;
   105     GtkIconFactory *factory;
   106     factory=gtk_icon_factory_new();
   107     icon_set=gtk_icon_set_new();
   108     datadir=g_strdup(g_getenv("PLOVER_ICONS_DATADIR"));
   109     if (!datadir)
   110     {
   111 #ifdef WIN32
   112 	prefix=g_win32_get_package_installation_directory_of_module(NULL);
   113 #else
   114 	prefix=NULL;
   115 #endif
   116 	if (!prefix)
   117 	    prefix=g_strdup("/usr");
   118 	datadir=g_strconcat(prefix,"share",NULL);
   119 	g_free(prefix);
   120     }
   121     if (plover_pixbuf_supports_svg())
   122     {
   123 	source=gtk_icon_source_new();
   124 	s=g_strconcat(name,".svg",NULL);
   125 	filename=g_build_filename(datadir,"icons/hicolor/scalable",type,s,NULL);
   126 	g_free(s);
   127 	gtk_icon_source_set_filename(source,filename);
   128 	g_free(filename);
   129 	gtk_icon_set_add_source(icon_set,source);
   130 	gtk_icon_source_free(source);
   131     }
   132     else
   133     {
   134 	s=g_strconcat(name,".png",NULL);
   135 	filename=g_build_filename(datadir,"icons/hicolor/24x24",type,s,NULL);
   136 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_MENU,
   137 	  filename);
   138 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_BUTTON,
   139 	  filename);
   140 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,
   141 	  filename);
   142 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,
   143 	  filename);
   144 	g_free(filename);
   145 	filename=g_build_filename(datadir,"icons/hicolor/48x48",type,s,NULL);
   146 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DND,
   147 	  filename);
   148 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DIALOG,
   149 	  filename);
   150 	g_free(filename);
   151 	g_free(s);
   152     }
   153     g_free(datadir);
   154     gtk_icon_factory_add(factory,name,icon_set);
   155     gtk_icon_set_unref(icon_set);
   156     //icon_set=gtk_icon_factory_lookup(factory,name);
   157     gtk_icon_factory_add_default(factory);
   158     g_object_unref(factory);
   159 }