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