2009-03-02 23:31:01 +00:00
|
|
|
/*
|
|
|
|
* icons.[ch] written by Paolo Bacchilega, who writes:
|
2011-01-19 13:48:47 +00:00
|
|
|
* "There is no problem for me, you can license my code
|
|
|
|
* under whatever licence you wish :)"
|
2010-10-01 20:22:51 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2009-03-02 23:31:01 +00:00
|
|
|
*/
|
|
|
|
|
2009-03-03 17:58:25 +00:00
|
|
|
#include <string.h> /* strcmp */
|
2009-03-02 23:31:01 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "icons.h"
|
|
|
|
|
2009-03-03 00:55:03 +00:00
|
|
|
#ifdef HAVE_GIO
|
2009-09-27 17:47:45 +00:00
|
|
|
#if GTK_CHECK_VERSION( 2, 12, 0 )
|
|
|
|
#define USE_GIO_ICONS
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GIO_ICONS
|
2009-03-03 17:58:25 +00:00
|
|
|
#include <gio/gio.h>
|
|
|
|
#endif
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2009-03-03 00:55:03 +00:00
|
|
|
#define VOID_PIXBUF_KEY "void-pixbuf"
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2009-09-27 17:47:45 +00:00
|
|
|
#ifdef USE_GIO_ICONS
|
2009-03-02 23:31:01 +00:00
|
|
|
static const char *
|
|
|
|
get_static_string( const char *s )
|
|
|
|
{
|
2009-07-27 18:48:21 +00:00
|
|
|
static GStringChunk * static_strings = NULL;
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2009-07-27 18:48:21 +00:00
|
|
|
if( s == NULL )
|
|
|
|
return NULL;
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2009-07-27 18:48:21 +00:00
|
|
|
if( static_strings == NULL )
|
|
|
|
static_strings = g_string_chunk_new( 1024 );
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2009-07-27 18:48:21 +00:00
|
|
|
return g_string_chunk_insert_const( static_strings, s );
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
2009-03-05 16:08:16 +00:00
|
|
|
#endif
|
2009-03-02 23:31:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GtkIconTheme * icon_theme;
|
|
|
|
int icon_size;
|
|
|
|
GHashTable * cache;
|
|
|
|
} IconCache;
|
|
|
|
|
|
|
|
|
|
|
|
static IconCache *icon_cache[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
|
|
|
|
|
|
|
|
|
|
|
|
static GdkPixbuf*
|
|
|
|
create_void_pixbuf (int width,
|
|
|
|
int height)
|
|
|
|
{
|
|
|
|
GdkPixbuf *p;
|
|
|
|
|
|
|
|
p = gdk_pixbuf_new( GDK_COLORSPACE_RGB,
|
|
|
|
TRUE,
|
|
|
|
8,
|
|
|
|
width,
|
|
|
|
height);
|
|
|
|
gdk_pixbuf_fill( p, 0xFFFFFF00 );
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_size_in_pixels( GtkWidget * widget,
|
|
|
|
GtkIconSize icon_size )
|
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
gtk_icon_size_lookup_for_settings( gtk_widget_get_settings( widget ),
|
|
|
|
icon_size,
|
2010-01-05 23:47:50 +00:00
|
|
|
&width,
|
2009-03-02 23:31:01 +00:00
|
|
|
&height );
|
|
|
|
return MAX( width, height );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-05 23:47:50 +00:00
|
|
|
static IconCache *
|
2010-05-18 20:55:14 +00:00
|
|
|
icon_cache_new (GtkWidget * for_widget, int icon_size)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
IconCache * icon_cache;
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
g_return_val_if_fail( for_widget != NULL, NULL );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
icon_cache = g_new0( IconCache, 1 );
|
2010-01-05 23:47:50 +00:00
|
|
|
icon_cache->icon_theme = gtk_icon_theme_get_for_screen( gtk_widget_get_screen( for_widget ));
|
2009-03-02 23:31:01 +00:00
|
|
|
icon_cache->icon_size = get_size_in_pixels( for_widget, icon_size );
|
|
|
|
icon_cache->cache = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
g_hash_table_insert( icon_cache->cache, (void*)VOID_PIXBUF_KEY, create_void_pixbuf( icon_cache->icon_size, icon_cache->icon_size ));
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
return icon_cache;
|
|
|
|
}
|
|
|
|
|
2009-09-27 17:47:45 +00:00
|
|
|
#ifdef USE_GIO_ICONS
|
2009-03-02 23:31:01 +00:00
|
|
|
static const char *
|
|
|
|
_icon_cache_get_icon_key( GIcon * icon )
|
|
|
|
{
|
|
|
|
const char * key = NULL;
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
if ( G_IS_THEMED_ICON( icon )) {
|
|
|
|
char ** icon_names;
|
|
|
|
char * name;
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
g_object_get( icon, "names", &icon_names, NULL );
|
|
|
|
name = g_strjoinv( ",", icon_names );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
key = get_static_string( name );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
g_free( name );
|
2010-05-18 20:55:14 +00:00
|
|
|
g_strfreev( icon_names );
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
|
|
|
else if ( G_IS_FILE_ICON( icon )) {
|
|
|
|
GFile * file;
|
|
|
|
char * filename;
|
|
|
|
|
|
|
|
file = g_file_icon_get_file( G_FILE_ICON( icon ));
|
|
|
|
filename = g_file_get_path( file );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
key = get_static_string( filename );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
g_free( filename );
|
|
|
|
g_object_unref( file );
|
|
|
|
}
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GdkPixbuf *
|
2010-05-18 20:55:14 +00:00
|
|
|
get_themed_icon_pixbuf( GThemedIcon * icon,
|
|
|
|
int size,
|
|
|
|
GtkIconTheme * icon_theme )
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2009-08-11 19:36:11 +00:00
|
|
|
char ** icon_names = NULL;
|
2009-03-02 23:31:01 +00:00
|
|
|
GtkIconInfo * icon_info;
|
|
|
|
GdkPixbuf * pixbuf;
|
|
|
|
GError * error = NULL;
|
|
|
|
|
|
|
|
g_object_get( icon, "names", &icon_names, NULL );
|
|
|
|
|
|
|
|
icon_info = gtk_icon_theme_choose_icon( icon_theme, (const char **)icon_names, size, 0 );
|
|
|
|
if ( icon_info == NULL )
|
|
|
|
icon_info = gtk_icon_theme_lookup_icon( icon_theme, "text-x-generic", size, GTK_ICON_LOOKUP_USE_BUILTIN );
|
|
|
|
|
|
|
|
pixbuf = gtk_icon_info_load_icon( icon_info, &error );
|
|
|
|
if ( pixbuf == NULL ) {
|
2009-06-05 15:45:18 +00:00
|
|
|
if( error && error->message )
|
|
|
|
g_warning( "could not load icon pixbuf: %s\n", error->message );
|
2009-03-02 23:31:01 +00:00
|
|
|
g_clear_error( &error );
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_icon_info_free( icon_info );
|
|
|
|
g_strfreev( icon_names );
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GdkPixbuf *
|
2010-05-18 20:55:14 +00:00
|
|
|
get_file_icon_pixbuf( GFileIcon * icon, int size )
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
GFile * file;
|
|
|
|
char * filename;
|
|
|
|
GdkPixbuf * pixbuf;
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
file = g_file_icon_get_file( icon );
|
|
|
|
filename = g_file_get_path( file );
|
|
|
|
pixbuf = gdk_pixbuf_new_from_file_at_size( filename, size, -1, NULL );
|
|
|
|
g_free( filename );
|
|
|
|
g_object_unref( file );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GdkPixbuf *
|
2010-05-18 20:55:14 +00:00
|
|
|
_get_icon_pixbuf( GIcon * icon, int size, GtkIconTheme * theme )
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
if ( icon == NULL )
|
|
|
|
return NULL;
|
|
|
|
if ( G_IS_THEMED_ICON (icon) )
|
|
|
|
return get_themed_icon_pixbuf( G_THEMED_ICON( icon ), size, theme );
|
|
|
|
if ( G_IS_FILE_ICON (icon) )
|
|
|
|
return get_file_icon_pixbuf( G_FILE_ICON( icon ), size );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
static GdkPixbuf *
|
2010-05-18 20:55:14 +00:00
|
|
|
icon_cache_get_mime_type_icon( IconCache * icon_cache, const char * mime_type )
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
GIcon * icon;
|
|
|
|
const char * key = NULL;
|
|
|
|
GdkPixbuf * pixbuf;
|
|
|
|
|
|
|
|
icon = g_content_type_get_icon( mime_type );
|
|
|
|
key = _icon_cache_get_icon_key( icon );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
if (key == NULL)
|
|
|
|
key = VOID_PIXBUF_KEY;
|
|
|
|
|
|
|
|
pixbuf = g_hash_table_lookup( icon_cache->cache, key );
|
|
|
|
if (pixbuf != NULL) {
|
|
|
|
g_object_ref( pixbuf );
|
2009-11-25 04:42:42 +00:00
|
|
|
g_object_unref( G_OBJECT( icon ) );
|
2009-03-02 23:31:01 +00:00
|
|
|
return pixbuf;
|
|
|
|
}
|
2009-07-27 18:48:21 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
pixbuf = _get_icon_pixbuf( icon, icon_cache->icon_size, icon_cache->icon_theme );
|
2009-07-27 18:48:21 +00:00
|
|
|
if( pixbuf != NULL )
|
|
|
|
g_hash_table_insert( icon_cache->cache, (gpointer) key, g_object_ref( pixbuf ) );
|
2009-11-25 04:42:42 +00:00
|
|
|
|
|
|
|
g_object_unref( G_OBJECT( icon ) );
|
2010-05-18 20:55:14 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
2009-09-27 17:47:45 +00:00
|
|
|
#else /* USE_GIO_ICONS */
|
2009-03-03 17:58:25 +00:00
|
|
|
|
|
|
|
static GdkPixbuf *
|
2010-05-18 20:55:14 +00:00
|
|
|
icon_cache_get_mime_type_icon( IconCache * icon_cache, const char * mime_type )
|
2009-03-03 17:58:25 +00:00
|
|
|
{
|
|
|
|
GdkPixbuf * pixbuf;
|
|
|
|
const char * stock_name;
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
if( !strcmp( mime_type, UNKNOWN_MIME_TYPE ) )
|
|
|
|
stock_name = GTK_STOCK_MISSING_IMAGE;
|
|
|
|
else if( !strcmp( mime_type, DIRECTORY_MIME_TYPE ) )
|
2009-03-03 17:58:25 +00:00
|
|
|
stock_name = GTK_STOCK_DIRECTORY;
|
|
|
|
else
|
|
|
|
stock_name = GTK_STOCK_FILE;
|
|
|
|
|
|
|
|
pixbuf = g_hash_table_lookup( icon_cache->cache, stock_name );
|
|
|
|
|
|
|
|
if( pixbuf != NULL )
|
|
|
|
{
|
|
|
|
g_object_ref( pixbuf );
|
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
pixbuf = gtk_icon_theme_load_icon( icon_cache->icon_theme,
|
|
|
|
stock_name,
|
|
|
|
icon_cache->icon_size,
|
|
|
|
0, NULL );
|
|
|
|
g_hash_table_insert( icon_cache->cache, (gpointer) stock_name, g_object_ref( pixbuf ));
|
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2010-01-05 23:47:50 +00:00
|
|
|
GdkPixbuf *
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_get_mime_type_icon( const char * mime_type,
|
|
|
|
GtkIconSize icon_size,
|
|
|
|
GtkWidget * for_widget )
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
int n;
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
switch ( icon_size ) {
|
2009-05-13 15:54:04 +00:00
|
|
|
case GTK_ICON_SIZE_MENU: n = 1; break;
|
|
|
|
case GTK_ICON_SIZE_SMALL_TOOLBAR: n = 2; break;
|
|
|
|
case GTK_ICON_SIZE_LARGE_TOOLBAR: n = 3; break;
|
|
|
|
case GTK_ICON_SIZE_BUTTON: n = 4; break;
|
|
|
|
case GTK_ICON_SIZE_DND: n = 5; break;
|
2010-01-05 23:47:50 +00:00
|
|
|
case GTK_ICON_SIZE_DIALOG: n = 6; break;
|
2009-05-13 15:54:04 +00:00
|
|
|
default /*GTK_ICON_SIZE_INVALID*/: n = 0; break;
|
2010-05-18 20:55:14 +00:00
|
|
|
}
|
2009-03-02 23:31:01 +00:00
|
|
|
|
|
|
|
if( icon_cache[n] == NULL )
|
|
|
|
icon_cache[n] = icon_cache_new( for_widget, icon_size );
|
|
|
|
|
|
|
|
return icon_cache_get_mime_type_icon( icon_cache[n], mime_type );
|
2010-01-05 23:47:50 +00:00
|
|
|
}
|
2009-03-02 23:31:01 +00:00
|
|
|
|
|
|
|
|
2010-01-05 23:47:50 +00:00
|
|
|
const char *
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_get_mime_type_from_filename( const char * file G_GNUC_UNUSED )
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2009-09-27 17:47:45 +00:00
|
|
|
#ifdef USE_GIO_ICONS
|
2009-04-23 15:45:30 +00:00
|
|
|
char * tmp = g_content_type_guess( file, NULL, 0, NULL );
|
|
|
|
const char * ret = get_static_string( tmp );
|
|
|
|
g_free( tmp );
|
|
|
|
return ret;
|
2009-03-03 00:55:03 +00:00
|
|
|
#else
|
|
|
|
return "uncertain";
|
|
|
|
#endif
|
2009-03-03 17:58:25 +00:00
|
|
|
}
|