[geany/geany] 01e7fe: Use GIcon for the filetype icon rather than rendered pixbuf

Colomban Wendling git-noreply at xxxxx
Mon Apr 14 18:42:05 UTC 2014


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Tue, 15 Oct 2013 13:56:44 UTC
Commit:      01e7fe4b79d0697ac2d03ab5ba18bd92a664ffc2
             https://github.com/geany/geany/commit/01e7fe4b79d0697ac2d03ab5ba18bd92a664ffc2

Log Message:
-----------
Use GIcon for the filetype icon rather than rendered pixbuf

This allows to re-use the icon more easily since it's not rendered at
a particular size but simply an icon description.  It also allows for
implicit icon updating when the theme changes if the display code
implements it (and GTK widgets does).


Modified Paths:
--------------
    src/dialogs.c
    src/filetypes.c
    src/filetypes.h
    src/main.c
    src/sidebar.c
    src/ui_utils.c
    src/ui_utils.h

Modified: src/dialogs.c
6 files changed, 2 insertions(+), 4 deletions(-)
===================================================================
@@ -1146,7 +1146,6 @@ void dialogs_show_file_properties(GeanyDocument *doc)
 	GtkWidget *dialog, *label, *image, *check;
 	gchar *file_size, *title, *base_name, *time_changed, *time_modified, *time_accessed, *enctext;
 	gchar *short_name;
-	GdkPixbuf *pixbuf;
 #ifdef HAVE_SYS_TYPES_H
 	struct stat st;
 	off_t filesize;
@@ -1220,9 +1219,8 @@ void dialogs_show_file_properties(GeanyDocument *doc)
 	gtk_label_set_text(GTK_LABEL(label), base_name);
 
 	image = ui_lookup_widget(dialog, "file_type_image");
-	pixbuf = ui_get_mime_icon(doc->file_type->mime_type, GTK_ICON_SIZE_BUTTON);
-	gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
-	g_object_unref(pixbuf);
+	gtk_image_set_from_gicon(GTK_IMAGE(image), doc->file_type->icon,
+			GTK_ICON_SIZE_BUTTON);
 
 	label = ui_lookup_widget(dialog, "file_type_label");
 	gtk_label_set_text(GTK_LABEL(label), doc->file_type->title);


Modified: src/filetypes.c
23 files changed, 2 insertions(+), 21 deletions(-)
===================================================================
@@ -1384,11 +1384,9 @@ void filetypes_load_config(guint ft_id, gboolean reload)
 	load_settings(ft_id, config, config_home);
 	highlighting_init_styles(ft_id, config, config_home);
 
-	if (reload && ft->icon)
-	{
+	if (ft->icon)
 		g_object_unref(ft->icon);
-		ft->icon = NULL;
-	}
+	ft->icon = ui_get_mime_icon(ft->mime_type);
 
 	g_key_file_free(config);
 	g_key_file_free(config_home);
@@ -1798,20 +1796,3 @@ gboolean filetype_get_comment_open_close(const GeanyFiletype *ft, gboolean singl
 
 	return !EMPTY(*co);
 }
-
-
-/* gets the filetype icon, possibly rendering it if not yet done
- * Returns the filetype's icon, should not be freed
- * 
- * This requires the mime_type setting so has to be done after the filetype
- * is loaded, but we don't necessarily have GTK in filetypes_load_config(),
- * so use a helper function that renders the icon if needed */
-GdkPixbuf *filetype_get_icon(GeanyFiletype *ft)
-{
-	g_return_val_if_fail(ft != NULL, NULL);
-
-	if (! ft->icon)
-		ft->icon = ui_get_mime_icon(ft->mime_type, GTK_ICON_SIZE_MENU);
-
-	return ft->icon;
-}


Modified: src/filetypes.h
4 files changed, 1 insertions(+), 3 deletions(-)
===================================================================
@@ -139,7 +139,7 @@ struct GeanyFiletype
 	gchar			 *error_regex_string;
 	GeanyFiletype	 *lexer_filetype;
 	gchar			 *mime_type;
-	GdkPixbuf		 *icon;
+	GIcon			 *icon;
 	gchar			 *comment_single; /* single-line comment */
 	/* filetype indent settings, -1 if not set */
 	gint			  indent_type;
@@ -218,8 +218,6 @@ gboolean filetypes_parse_error_message(GeanyFiletype *ft, const gchar *message,
 gboolean filetype_get_comment_open_close(const GeanyFiletype *ft, gboolean single_first,
 		const gchar **co, const gchar **cc);
 
-GdkPixbuf *filetype_get_icon(GeanyFiletype *ft);
-
 G_END_DECLS
 
 #endif


Modified: src/main.c
4 files changed, 4 insertions(+), 0 deletions(-)
===================================================================
@@ -1027,6 +1027,10 @@ gint main(gint argc, gchar **argv)
 	gint config_dir_result;
 	const gchar *locale;
 
+#if ! GLIB_CHECK_VERSION(2, 36, 0)
+	g_type_init();
+#endif
+
 	log_handlers_init();
 
 	app = g_new0(GeanyApp, 1);


Modified: src/sidebar.c
18 files changed, 9 insertions(+), 9 deletions(-)
===================================================================
@@ -274,7 +274,7 @@ static void prepare_openfiles(void)
 
 	/* store the icon and the short filename to show, and the index as reference,
 	 * the colour (black/red/green) and the full name for the tooltip */
-	store_openfiles = gtk_tree_store_new(5, GDK_TYPE_PIXBUF, G_TYPE_STRING,
+	store_openfiles = gtk_tree_store_new(5, G_TYPE_ICON, G_TYPE_STRING,
 		G_TYPE_POINTER, GDK_TYPE_COLOR, G_TYPE_STRING);
 	gtk_tree_view_set_model(GTK_TREE_VIEW(tv.tree_openfiles), GTK_TREE_MODEL(store_openfiles));
 
@@ -285,11 +285,12 @@ static void prepare_openfiles(void)
 		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 
 	icon_renderer = gtk_cell_renderer_pixbuf_new();
+	g_object_set(icon_renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
 	text_renderer = gtk_cell_renderer_text_new();
 	g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
 	column = gtk_tree_view_column_new();
 	gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
-	gtk_tree_view_column_set_attributes(column, icon_renderer, "pixbuf", DOCUMENTS_ICON, NULL);
+	gtk_tree_view_column_set_attributes(column, icon_renderer, "gicon", DOCUMENTS_ICON, NULL);
 	gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
 	gtk_tree_view_column_set_attributes(column, text_renderer, "text", DOCUMENTS_SHORTNAME,
 		"foreground-gdk", DOCUMENTS_COLOR, NULL);
@@ -411,7 +412,7 @@ static GtkTreeIter *get_doc_parent(GeanyDocument *doc)
 	gchar *dirname = NULL;
 	static GtkTreeIter parent;
 	GtkTreeModel *model = GTK_TREE_MODEL(store_openfiles);
-	static GdkPixbuf *dir_icon = NULL;
+	static GIcon *dir_icon = NULL;
 
 	if (!documents_show_paths)
 		return NULL;
@@ -434,7 +435,7 @@ static GtkTreeIter *get_doc_parent(GeanyDocument *doc)
 	}
 	/* no match, add dir parent */
 	if (!dir_icon)
-		dir_icon = ui_get_mime_icon("inode/directory", GTK_ICON_SIZE_MENU);
+		dir_icon = ui_get_mime_icon("inode/directory");
 
 	gtk_tree_store_append(store_openfiles, &parent, NULL);
 	gtk_tree_store_set(store_openfiles, &parent, DOCUMENTS_ICON, dir_icon,
@@ -455,8 +456,7 @@ void sidebar_openfiles_add(GeanyDocument *doc)
 	GtkTreeIter *parent = get_doc_parent(doc);
 	gchar *basename;
 	const GdkColor *color = document_get_status_color(doc);
-	static GdkPixbuf *file_icon = NULL;
-	GdkPixbuf *ft_icon = doc->file_type ? filetype_get_icon(doc->file_type) : NULL;
+	static GIcon *file_icon = NULL;
 
 	gtk_tree_store_append(store_openfiles, iter, parent);
 
@@ -471,11 +471,11 @@ void sidebar_openfiles_add(GeanyDocument *doc)
 		gtk_tree_path_free(path);
 	}
 	if (!file_icon)
-		file_icon = ui_get_mime_icon("text/plain", GTK_ICON_SIZE_MENU);
+		file_icon = ui_get_mime_icon("text/plain");
 
 	basename = g_path_get_basename(DOC_FILENAME(doc));
 	gtk_tree_store_set(store_openfiles, iter,
-		DOCUMENTS_ICON, ft_icon ? ft_icon : file_icon,
+		DOCUMENTS_ICON, (doc->file_type && doc->file_type->icon) ? doc->file_type->icon : file_icon,
 		DOCUMENTS_SHORTNAME, basename, DOCUMENTS_DOCUMENT, doc, DOCUMENTS_COLOR, color,
 		DOCUMENTS_FILENAME, DOC_FILENAME(doc), -1);
 	g_free(basename);
@@ -506,7 +506,7 @@ void sidebar_openfiles_update(GeanyDocument *doc)
 	{
 		/* just update color and the icon */
 		const GdkColor *color = document_get_status_color(doc);
-		GdkPixbuf *icon = filetype_get_icon(doc->file_type);
+		GIcon *icon = doc->file_type->icon;
 
 		gtk_tree_store_set(store_openfiles, iter, DOCUMENTS_COLOR, color, -1);
 		if (icon)


Modified: src/ui_utils.c
49 files changed, 8 insertions(+), 41 deletions(-)
===================================================================
@@ -2587,7 +2587,7 @@ void ui_menu_add_document_items_sorted(GtkMenu *menu, GeanyDocument *active,
 
 		base_name = g_path_get_basename(DOC_FILENAME(doc));
 		menu_item = gtk_image_menu_item_new_with_label(base_name);
-		image = gtk_image_new_from_pixbuf(filetype_get_icon(doc->file_type));
+		image = gtk_image_new_from_gicon(doc->file_type->icon, GTK_ICON_SIZE_MENU);
 		gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
 
 		gtk_widget_show(menu_item);
@@ -2665,57 +2665,24 @@ void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
 
 
 /* gets the icon that applies to a particular MIME type */
-GdkPixbuf *ui_get_mime_icon(const gchar *mime_type, GtkIconSize size)
+GIcon *ui_get_mime_icon(const gchar *mime_type)
 {
-	GdkPixbuf *icon = NULL;
+	GIcon *icon = NULL;
 	gchar *ctype;
-	GIcon *gicon;
-	GtkIconInfo *info;
-	GtkIconTheme *theme;
-	gint real_size;
 
-	if (!gtk_icon_size_lookup(size, &real_size, NULL))
-	{
-		g_return_val_if_reached(NULL);
-	}
 	ctype = g_content_type_from_mime_type(mime_type);
-	if (ctype != NULL)
-	{
-		gicon = g_content_type_get_icon(ctype);
-		theme = gtk_icon_theme_get_default();
-		info = gtk_icon_theme_lookup_by_gicon(theme, gicon, real_size, 0);
-		g_object_unref(gicon);
-		g_free(ctype);
-
-		if (info != NULL)
-		{
-			icon = gtk_icon_info_load_icon(info, NULL);
-			gtk_icon_info_free(info);
-		}
-	}
+	if (ctype)
+		icon = g_content_type_get_icon(ctype);
 
-	/* fallback for builds with GIO < 2.18 or if icon lookup failed, like it might happen on Windows */
-	if (icon == NULL)
+	/* fallback if icon lookup failed, like it might happen on Windows (?) */
+	if (! icon)
 	{
 		const gchar *stock_id = GTK_STOCK_FILE;
-		GtkIconSet *icon_set;
 
 		if (strstr(mime_type, "directory"))
 			stock_id = GTK_STOCK_DIRECTORY;
 
-		icon_set = gtk_icon_factory_lookup_default(stock_id);
-		if (icon_set)
-		{
-#if GTK_CHECK_VERSION(3, 0, 0)
-			GtkStyleContext *ctx = gtk_style_context_new();
-			icon = gtk_icon_set_render_icon_pixbuf(icon_set, ctx, size);
-			g_object_unref(ctx);
-#else
-			icon = gtk_icon_set_render_icon(icon_set, gtk_widget_get_default_style(),
-				gtk_widget_get_default_direction(),
-				GTK_STATE_NORMAL, size, NULL, NULL);
-#endif
-		}
+		icon = g_themed_icon_new(stock_id);
 	}
 	return icon;
 }


Modified: src/ui_utils.h
2 files changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -334,7 +334,7 @@ gboolean ui_is_keyval_enter_or_return(guint keyval);
 
 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value);
 
-GdkPixbuf *ui_get_mime_icon(const gchar *mime_type, GtkIconSize size);
+GIcon *ui_get_mime_icon(const gchar *mime_type);
 
 void ui_focus_current_document(void);
 



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list