2 * icons.[ch] written by Paolo Bacchilega, who writes:
3 * "There is no problem for me, you can license my code
4 * under whatever licence you wish :)"
6 * $Id: icons.c 11709 2011-01-19 13:48:47Z jordan $
9 #include <string.h> /* strcmp */
15 #if GTK_CHECK_VERSION( 2, 12, 0 )
24 #define VOID_PIXBUF_KEY "void-pixbuf"
28 get_static_string( const char *s
)
30 static GStringChunk
* static_strings
= NULL
;
35 if( static_strings
== NULL
)
36 static_strings
= g_string_chunk_new( 1024 );
38 return g_string_chunk_insert_const( static_strings
, s
);
44 GtkIconTheme
* icon_theme
;
50 static IconCache
*icon_cache
[7] = { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
54 create_void_pixbuf (int width
,
59 p
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
,
64 gdk_pixbuf_fill( p
, 0xFFFFFF00 );
71 get_size_in_pixels( GtkWidget
* widget
,
72 GtkIconSize icon_size
)
76 gtk_icon_size_lookup_for_settings( gtk_widget_get_settings( widget
),
80 return MAX( width
, height
);
85 icon_cache_new (GtkWidget
* for_widget
, int icon_size
)
87 IconCache
* icon_cache
;
89 g_return_val_if_fail( for_widget
!= NULL
, NULL
);
91 icon_cache
= g_new0( IconCache
, 1 );
92 icon_cache
->icon_theme
= gtk_icon_theme_get_for_screen( gtk_widget_get_screen( for_widget
));
93 icon_cache
->icon_size
= get_size_in_pixels( for_widget
, icon_size
);
94 icon_cache
->cache
= g_hash_table_new_full (g_str_hash
, g_str_equal
, NULL
, g_object_unref
);
96 g_hash_table_insert( icon_cache
->cache
, (void*)VOID_PIXBUF_KEY
, create_void_pixbuf( icon_cache
->icon_size
, icon_cache
->icon_size
));
103 _icon_cache_get_icon_key( GIcon
* icon
)
105 const char * key
= NULL
;
107 if ( G_IS_THEMED_ICON( icon
)) {
111 g_object_get( icon
, "names", &icon_names
, NULL
);
112 name
= g_strjoinv( ",", icon_names
);
114 key
= get_static_string( name
);
117 g_strfreev( icon_names
);
119 else if ( G_IS_FILE_ICON( icon
)) {
123 file
= g_file_icon_get_file( G_FILE_ICON( icon
));
124 filename
= g_file_get_path( file
);
126 key
= get_static_string( filename
);
129 g_object_unref( file
);
137 get_themed_icon_pixbuf( GThemedIcon
* icon
,
139 GtkIconTheme
* icon_theme
)
141 char ** icon_names
= NULL
;
142 GtkIconInfo
* icon_info
;
144 GError
* error
= NULL
;
146 g_object_get( icon
, "names", &icon_names
, NULL
);
148 icon_info
= gtk_icon_theme_choose_icon( icon_theme
, (const char **)icon_names
, size
, 0 );
149 if ( icon_info
== NULL
)
150 icon_info
= gtk_icon_theme_lookup_icon( icon_theme
, "text-x-generic", size
, GTK_ICON_LOOKUP_USE_BUILTIN
);
152 pixbuf
= gtk_icon_info_load_icon( icon_info
, &error
);
153 if ( pixbuf
== NULL
) {
154 if( error
&& error
->message
)
155 g_warning( "could not load icon pixbuf: %s\n", error
->message
);
156 g_clear_error( &error
);
159 gtk_icon_info_free( icon_info
);
160 g_strfreev( icon_names
);
167 get_file_icon_pixbuf( GFileIcon
* icon
, int size
)
173 file
= g_file_icon_get_file( icon
);
174 filename
= g_file_get_path( file
);
175 pixbuf
= gdk_pixbuf_new_from_file_at_size( filename
, size
, -1, NULL
);
177 g_object_unref( file
);
184 _get_icon_pixbuf( GIcon
* icon
, int size
, GtkIconTheme
* theme
)
188 if ( G_IS_THEMED_ICON (icon
) )
189 return get_themed_icon_pixbuf( G_THEMED_ICON( icon
), size
, theme
);
190 if ( G_IS_FILE_ICON (icon
) )
191 return get_file_icon_pixbuf( G_FILE_ICON( icon
), size
);
197 icon_cache_get_mime_type_icon( IconCache
* icon_cache
, const char * mime_type
)
200 const char * key
= NULL
;
203 icon
= g_content_type_get_icon( mime_type
);
204 key
= _icon_cache_get_icon_key( icon
);
207 key
= VOID_PIXBUF_KEY
;
209 pixbuf
= g_hash_table_lookup( icon_cache
->cache
, key
);
210 if (pixbuf
!= NULL
) {
211 g_object_ref( pixbuf
);
212 g_object_unref( G_OBJECT( icon
) );
216 pixbuf
= _get_icon_pixbuf( icon
, icon_cache
->icon_size
, icon_cache
->icon_theme
);
218 g_hash_table_insert( icon_cache
->cache
, (gpointer
) key
, g_object_ref( pixbuf
) );
220 g_object_unref( G_OBJECT( icon
) );
225 #else /* USE_GIO_ICONS */
228 icon_cache_get_mime_type_icon( IconCache
* icon_cache
, const char * mime_type
)
231 const char * stock_name
;
233 if( !strcmp( mime_type
, UNKNOWN_MIME_TYPE
) )
234 stock_name
= GTK_STOCK_MISSING_IMAGE
;
235 else if( !strcmp( mime_type
, DIRECTORY_MIME_TYPE
) )
236 stock_name
= GTK_STOCK_DIRECTORY
;
238 stock_name
= GTK_STOCK_FILE
;
240 pixbuf
= g_hash_table_lookup( icon_cache
->cache
, stock_name
);
244 g_object_ref( pixbuf
);
248 pixbuf
= gtk_icon_theme_load_icon( icon_cache
->icon_theme
,
250 icon_cache
->icon_size
,
252 g_hash_table_insert( icon_cache
->cache
, (gpointer
) stock_name
, g_object_ref( pixbuf
));
260 gtr_get_mime_type_icon( const char * mime_type
,
261 GtkIconSize icon_size
,
262 GtkWidget
* for_widget
)
266 switch ( icon_size
) {
267 case GTK_ICON_SIZE_MENU
: n
= 1; break;
268 case GTK_ICON_SIZE_SMALL_TOOLBAR
: n
= 2; break;
269 case GTK_ICON_SIZE_LARGE_TOOLBAR
: n
= 3; break;
270 case GTK_ICON_SIZE_BUTTON
: n
= 4; break;
271 case GTK_ICON_SIZE_DND
: n
= 5; break;
272 case GTK_ICON_SIZE_DIALOG
: n
= 6; break;
273 default /*GTK_ICON_SIZE_INVALID*/: n
= 0; break;
276 if( icon_cache
[n
] == NULL
)
277 icon_cache
[n
] = icon_cache_new( for_widget
, icon_size
);
279 return icon_cache_get_mime_type_icon( icon_cache
[n
], mime_type
);
284 gtr_get_mime_type_from_filename( const char * file G_GNUC_UNUSED
)
287 char * tmp
= g_content_type_guess( file
, NULL
, 0, NULL
);
288 const char * ret
= get_static_string( tmp
);