plover-gtk/stockicons.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Nov 15 19:04:45 2014 +0000 (2014-11-15)
changeset 24 2b9f54d14cc2
child 30 3ee18a3a0f58
permissions -rw-r--r--
Add GUI front-end to setup and update
     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 (found_svg!=-1)
    38 	return found_svg;
    39     formats=gdk_pixbuf_get_formats();
    40     found_svg=FALSE;
    41     for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
    42     {
    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"))
    46 		found_svg=TRUE;
    47 	g_strfreev(mime_types);
    48     }
    49     g_slist_free(formats);
    50     return found_svg;
    51 }
    52 
    53 static void plover_install_icon_at_size(const char *icon_name,
    54   GtkIconSet *icon_set,GtkIconSize size,const char *filename)
    55 {
    56     int w,h;
    57     GdkPixbuf *pixbuf;
    58     GtkIconSource *source;
    59     if (gtk_icon_size_lookup(size,&w,&h))
    60     {
    61 	pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
    62 	if (pixbuf)
    63 	{
    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);
    71 	}
    72     }
    73 }
    74 
    75 /**
    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)
    79  *
    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.
    82  *
    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.
    87  */
    88 void plover_icons_add_to_stock(const char *type,const char *name)
    89 {
    90     gchar *prefix,*s,*filename;
    91     GtkIconSource *source;
    92     GtkIconSet *icon_set;
    93     GtkIconFactory *factory;
    94     factory=gtk_icon_factory_new();
    95     icon_set=gtk_icon_set_new();
    96 #ifdef WIN32
    97     prefix=g_win32_get_package_installation_directory_of_module(NULL);
    98 #else
    99     prefix=NULL;
   100 #endif
   101     if (plover_pixbuf_supports_svg())
   102     {
   103 	source=gtk_icon_source_new();
   104 	s=g_strconcat(name,".svg");
   105 	filename=g_build_filename(prefix?prefix:"/usr",
   106 	  "share/icons/hicolor/scalable",type,s,NULL);
   107 	g_free(s);
   108 	gtk_icon_source_set_filename(source,filename);
   109 	g_free(filename);
   110 	gtk_icon_set_add_source(icon_set,source);
   111 	gtk_icon_source_free(source);
   112     }
   113     else
   114     {
   115 	s=g_strconcat(name,".png");
   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,
   119 	  filename);
   120 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_BUTTON,
   121 	  filename);
   122 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,
   123 	  filename);
   124 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,
   125 	  filename);
   126 	g_free(filename);
   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,
   130 	  filename);
   131 	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DIALOG,
   132 	  filename);
   133 	g_free(filename);
   134 	g_free(s);
   135     }
   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);
   141 }