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
ali@24
     1
/*
ali@24
     2
 * Copyright (C) 2010  J. Ali Harlow <ali@juiblex.co.uk>
ali@24
     3
 *
ali@24
     4
 * This program is free software; you can redistribute it and/or modify
ali@24
     5
 * it under the terms of the GNU General Public License as published by
ali@24
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@24
     7
 * (at your option) any later version.
ali@24
     8
 *
ali@24
     9
 * This program is distributed in the hope that it will be useful,
ali@24
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@24
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@24
    12
 * GNU General Public License for more details.
ali@24
    13
 *
ali@24
    14
 * You should have received a copy of the GNU General Public License along
ali@24
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@24
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@24
    17
 */
ali@24
    18
ali@24
    19
#include "config.h"
ali@24
    20
#include <stdlib.h>
ali@24
    21
#ifdef WIN32
ali@24
    22
#include <windows.h>
ali@24
    23
#endif	/* WIN32 */
ali@24
    24
#include <glib.h>
ali@24
    25
#include <gtk/gtk.h>
ali@24
    26
#include <plover-gtk/stockicons.h>
ali@24
    27
ali@24
    28
/* Checks whether a loader for SVG files has been registered
ali@24
    29
 * with GdkPixbuf.
ali@24
    30
 */
ali@24
    31
static gboolean plover_pixbuf_supports_svg(void)
ali@24
    32
{
ali@24
    33
    GSList *formats;
ali@24
    34
    GSList *tmp_list;
ali@24
    35
    static gint found_svg=-1;
ali@24
    36
    gchar **mime_types,**mime_type;
ali@24
    37
    if (found_svg!=-1)
ali@24
    38
	return found_svg;
ali@24
    39
    formats=gdk_pixbuf_get_formats();
ali@24
    40
    found_svg=FALSE;
ali@24
    41
    for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
ali@24
    42
    {
ali@24
    43
	mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
ali@24
    44
	for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
ali@24
    45
	    if (!strcmp(*mime_type,"image/svg"))
ali@24
    46
		found_svg=TRUE;
ali@24
    47
	g_strfreev(mime_types);
ali@24
    48
    }
ali@24
    49
    g_slist_free(formats);
ali@24
    50
    return found_svg;
ali@24
    51
}
ali@24
    52
ali@24
    53
static void plover_install_icon_at_size(const char *icon_name,
ali@24
    54
  GtkIconSet *icon_set,GtkIconSize size,const char *filename)
ali@24
    55
{
ali@24
    56
    int w,h;
ali@24
    57
    GdkPixbuf *pixbuf;
ali@24
    58
    GtkIconSource *source;
ali@24
    59
    if (gtk_icon_size_lookup(size,&w,&h))
ali@24
    60
    {
ali@24
    61
	pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
ali@24
    62
	if (pixbuf)
ali@24
    63
	{
ali@24
    64
	    source=gtk_icon_source_new();
ali@24
    65
	    gtk_icon_source_set_size_wildcarded(source,FALSE);
ali@24
    66
	    gtk_icon_source_set_size(source,size);
ali@24
    67
	    gtk_icon_source_set_pixbuf(source,pixbuf);
ali@24
    68
	    gtk_icon_set_add_source(icon_set,source);
ali@24
    69
	    gtk_icon_source_free(source);
ali@24
    70
	    g_object_unref(pixbuf);
ali@24
    71
	}
ali@24
    72
    }
ali@24
    73
}
ali@24
    74
ali@24
    75
/**
ali@24
    76
 * plover_icons_add_to_stock:
ali@24
    77
 * @type: The icon type, typically "apps" or "mimetype"
ali@24
    78
 * @name: The icon name (the basename of files containing the icons)
ali@24
    79
 *
ali@24
    80
 * Find icons in <datadir>/icons/hicolor and add them to the stock images
ali@24
    81
 * so that, for example, gtk_image_new_from_stock() will be able find them.
ali@24
    82
 *
ali@24
    83
 * If there is an SVG loader registered with GdkPixbuf, then:
ali@24
    84
 * <datadir>/icons/hicolor/scalable/@type/@name.svg will be used. Otherwise,
ali@24
    85
 * <datadir>/icons/hicolor/24x24/@type/@name.png and
ali@24
    86
 * <datadir>/icons/hicolor/48x48/@type/@name.png will be used.
ali@24
    87
 */
ali@24
    88
void plover_icons_add_to_stock(const char *type,const char *name)
ali@24
    89
{
ali@24
    90
    gchar *prefix,*s,*filename;
ali@24
    91
    GtkIconSource *source;
ali@24
    92
    GtkIconSet *icon_set;
ali@24
    93
    GtkIconFactory *factory;
ali@24
    94
    factory=gtk_icon_factory_new();
ali@24
    95
    icon_set=gtk_icon_set_new();
ali@24
    96
#ifdef WIN32
ali@24
    97
    prefix=g_win32_get_package_installation_directory_of_module(NULL);
ali@24
    98
#else
ali@24
    99
    prefix=NULL;
ali@24
   100
#endif
ali@24
   101
    if (plover_pixbuf_supports_svg())
ali@24
   102
    {
ali@24
   103
	source=gtk_icon_source_new();
ali@24
   104
	s=g_strconcat(name,".svg");
ali@24
   105
	filename=g_build_filename(prefix?prefix:"/usr",
ali@24
   106
	  "share/icons/hicolor/scalable",type,s,NULL);
ali@24
   107
	g_free(s);
ali@24
   108
	gtk_icon_source_set_filename(source,filename);
ali@24
   109
	g_free(filename);
ali@24
   110
	gtk_icon_set_add_source(icon_set,source);
ali@24
   111
	gtk_icon_source_free(source);
ali@24
   112
    }
ali@24
   113
    else
ali@24
   114
    {
ali@24
   115
	s=g_strconcat(name,".png");
ali@24
   116
	filename=g_build_filename(prefix?prefix:"/usr",
ali@24
   117
	  "share/icons/hicolor/24x24",type,s,NULL);
ali@24
   118
	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_MENU,
ali@24
   119
	  filename);
ali@24
   120
	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_BUTTON,
ali@24
   121
	  filename);
ali@24
   122
	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,
ali@24
   123
	  filename);
ali@24
   124
	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,
ali@24
   125
	  filename);
ali@24
   126
	g_free(filename);
ali@24
   127
	filename=g_build_filename(prefix?prefix:"/usr",
ali@24
   128
	  "share/icons/hicolor/48x48",type,s,NULL);
ali@24
   129
	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DND,
ali@24
   130
	  filename);
ali@24
   131
	plover_install_icon_at_size(name,icon_set,GTK_ICON_SIZE_DIALOG,
ali@24
   132
	  filename);
ali@24
   133
	g_free(filename);
ali@24
   134
	g_free(s);
ali@24
   135
    }
ali@24
   136
    gtk_icon_factory_add(factory,name,icon_set);
ali@24
   137
    gtk_icon_set_unref(icon_set);
ali@24
   138
    //icon_set=gtk_icon_factory_lookup(factory,name);
ali@24
   139
    gtk_icon_factory_add_default(factory);
ali@24
   140
    g_object_unref(factory);
ali@24
   141
}