Le 15/10/2010 00:03, Dominic Hopf a écrit :
> Am Donnerstag, den 14.10.2010, 16:36 +0200 schrieb Colomban Wendling:
>> Hi,
>>
>> I realized that in the "documents" sidebar each and every files have an
>> icon, but they all have the same, no matter what. I had a little time
>> and I decided to play a bit with the idea of having file-type based icons.
>>
>> Such icons could be displayed at a few places:
>>
>> 1) In the Documents sidebar: there is already icons and it would
>> probably ease to find a particular item (if different filetypes are
>> mixed, of course). Anyway I think it's better to have the "right" icons
>> rather than always using the same. (capture joined)
>>
>> 2) In the document notebook popup. This will perhaps ease finding the
>> item, but perhaps adds clutter. Not sure. (capture joined)
>>
>> 3) In the tab title, like GEdit does. I think this one is a bad idea
>> since it is probably useless and only use a little more space.
> 4) In file browser and tree browser plugin, which maybe could use
> functions provided by Geany for showing those icons.
Not sure it's really helpful for *browsers, because you have to know the
file type already. I'd think g_content_type_guess() is probably more
appropriate there since we don't know the content type already, and then
you directly get the icon (with g_content_type_get_icon()).

> I short: I love this idea! :)
Fine then :) Having that icon is fairly easy, but as said, I'm currently
unable to display it reliably in the document sidebar.

Joined 0001-Add-filetype-icon.patch (depends on previous
0001-Add-MIME-type-to-GeanyFiletype.patch) that add filetype->icon, a
16x16 GdkPixbuf icon that represents the file type.

Also joined 0001-Add-filetype-icon-to-the-editor-notebook-popup.patch
that... huh, adds filetype icon to the editor notebook popup entries, to
show how it looks like. (this was suggestion 2).

Regards,
Colomban
>From 9297355ff0b589d37f2683e90bf7dcea602673a7 Mon Sep 17 00:00:00 2001
From: Colomban Wendling <b...@herbesfolles.org>
Date: Thu, 14 Oct 2010 16:32:10 +0200
Subject: [PATCH] Add filetype icon

Filetypes now have an icon based on their MIME type, available as
filetype->icon.

If built against GLib < 2.18, the icon is currently always
GTK_STOCK_FILE.
---
 src/filetypes.c |    6 ++++++
 src/filetypes.h |    1 +
 src/ui_utils.c  |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/ui_utils.h  |    2 ++
 4 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/src/filetypes.c b/src/filetypes.c
index 405338e..5a3fefd 100644
--- a/src/filetypes.c
+++ b/src/filetypes.c
@@ -710,6 +710,7 @@ static GeanyFiletype *filetype_new(void)
 
 	ft->lang = -2;	/* assume no tagmanager parser */
 	ft->project_list_entry = -1; /* no entry */
+	ft->icon = NULL;
 
 	ft->priv = g_new0(GeanyFiletypePrivate, 1);
 	return ft;
@@ -741,6 +742,9 @@ static void filetype_add(GeanyFiletype *ft)
 	g_hash_table_insert(filetypes_hash, ft->name, ft);
 
 	filetypes_by_title = g_slist_insert_sorted(filetypes_by_title, ft, cmp_filetype);
+
+	if (!ft->icon && ft->mime_type)
+		ft->icon = ui_get_mime_icon(ft->mime_type, GTK_ICON_SIZE_MENU);
 }
 
 
@@ -1228,6 +1232,8 @@ static void filetype_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
 	g_free(ft->ftdefcmds);
 	g_free(ft->execcmds);
 	set_error_regex(ft, NULL);
+	if (ft->icon)
+		g_object_unref(ft->icon);
 
 	g_strfreev(ft->pattern);
 	g_free(ft->priv);
diff --git a/src/filetypes.h b/src/filetypes.h
index 50f4c74..e0e767a 100644
--- a/src/filetypes.h
+++ b/src/filetypes.h
@@ -129,6 +129,7 @@ struct GeanyFiletype
 	gchar			 *error_regex_string;
 	GeanyFiletype	 *lexer_filetype;
 	gchar			 *mime_type;
+	GdkPixbuf		 *icon;
 
 	struct GeanyFiletypePrivate	*priv;	/* must be last, append fields before this item */
 #ifdef GEANY_PRIVATE
diff --git a/src/ui_utils.c b/src/ui_utils.c
index a728052..68946df 100644
--- a/src/ui_utils.c
+++ b/src/ui_utils.c
@@ -2453,3 +2453,57 @@ void ui_editable_insert_text_callback(GtkEditable *editable, gchar *new_text,
 		g_signal_stop_emission_by_name(editable, "insert-text");
 }
 
+
+/* gets the icon that applies to a particular MIME type */
+GdkPixbuf *ui_get_mime_icon(const gchar *mime_type, GtkIconSize size)
+{
+	GdkPixbuf *icon = NULL;
+#if defined(HAVE_GIO) && GLIB_CHECK_VERSION(2, 18, 0)
+	gchar *ctype;
+	GIcon *gicon;
+	GtkIconInfo *info;
+	GtkIconTheme *theme;
+	gint real_size = 16;
+
+	switch (size)
+	{
+		default:
+		case GTK_ICON_SIZE_SMALL_TOOLBAR:
+		case GTK_ICON_SIZE_MENU:			real_size = 16; break;
+		case GTK_ICON_SIZE_LARGE_TOOLBAR:
+		case GTK_ICON_SIZE_BUTTON:			real_size = 24; break;
+		case GTK_ICON_SIZE_DND:
+		case GTK_ICON_SIZE_DIALOG:			real_size = 48; break;
+	}
+
+	theme = gtk_icon_theme_get_default();
+	ctype = g_content_type_from_mime_type(mime_type);
+	gicon = g_content_type_get_icon(ctype);
+	info = gtk_icon_theme_lookup_by_gicon(theme, gicon, real_size, 0);
+	g_object_unref(gicon);
+	g_free(ctype);
+
+	if (info)
+	{
+		icon = gtk_icon_info_load_icon(info, NULL);
+		gtk_icon_info_free(info);
+	}
+#else
+	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)
+	{
+		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
+
+	return icon;
+}
+
diff --git a/src/ui_utils.h b/src/ui_utils.h
index c1bc2e9..d1aee0a 100644
--- a/src/ui_utils.h
+++ b/src/ui_utils.h
@@ -321,4 +321,6 @@ 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);
+
 #endif
-- 
1.7.1

>From 16b8b9183f7427230acc387164b2627f613ff27c Mon Sep 17 00:00:00 2001
From: Colomban Wendling <b...@herbesfolles.org>
Date: Fri, 15 Oct 2010 00:28:24 +0200
Subject: [PATCH] Add filetype icon to the editor notebook popup

---
 src/ui_utils.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/ui_utils.c b/src/ui_utils.c
index 68946df..7d3cf5b 100644
--- a/src/ui_utils.c
+++ b/src/ui_utils.c
@@ -2364,7 +2364,7 @@ GtkWidget *ui_label_new_bold(const gchar *text)
  * @since 0.19 */
 void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback callback)
 {
-	GtkWidget *menu_item, *menu_item_label;
+	GtkWidget *menu_item, *menu_item_label, *image;
 	const GdkColor *color;
 	GeanyDocument *doc;
 	guint i, len;
@@ -2378,7 +2378,12 @@ void ui_menu_add_document_items(GtkMenu *menu, GeanyDocument *active, GCallback
 			continue;
 
 		base_name = g_path_get_basename(DOC_FILENAME(doc));
-		menu_item = gtk_menu_item_new_with_label(base_name);
+		menu_item = gtk_image_menu_item_new_with_label(base_name);
+		if (doc->file_type && doc->file_type->icon)
+		{
+			image = gtk_image_new_from_pixbuf(doc->file_type->icon);
+			gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM (menu_item), image);
+		}
 		gtk_widget_show(menu_item);
 		gtk_container_add(GTK_CONTAINER(menu), menu_item);
 		g_signal_connect(menu_item, "activate", callback, doc);
-- 
1.7.1

_______________________________________________
Geany-devel mailing list
Geany-devel@uvena.de
http://lists.uvena.de/cgi-bin/mailman/listinfo/geany-devel

Reply via email to