[gthumb] Improve gicon usage for creating mount and application pixbufs



commit 37b25d4891605fee51595c240e8b9ffd4a9feeda
Author: Michael J. Chudobiak <mjc avtechpulse com>
Date:   Sat Jun 6 14:52:41 2009 -0400

    Improve gicon usage for creating mount and application pixbufs
---
 libgthumb/gtk-utils.c |  141 -------------------------------------------------
 src/dlg-open-with.c   |   19 ++++---
 src/gth-location.c    |   38 ++++++-------
 src/gth-viewer.c      |   29 +++++++----
 4 files changed, 48 insertions(+), 179 deletions(-)

diff --git a/libgthumb/gtk-utils.c b/libgthumb/gtk-utils.c
index cf3a6c5..1a371be 100644
--- a/libgthumb/gtk-utils.c
+++ b/libgthumb/gtk-utils.c
@@ -789,147 +789,6 @@ exec_command (const char *application,
 }
 
 
-/* This function from gnome-panel/panel-util.c
- * (C) 1997, 1998, 1999, 2000 The Free Software Foundation
- * Copyright 2000 Helix Code, Inc.
- * Copyright 2000,2001 Eazel, Inc.
- * Copyright 2001 George Lebl
- * Copyright 2002 Sun Microsystems Inc.
- *
- * Authors: George Lebl
- *          Jacob Berkman
- *          Mark McLoughlin
- *
- * Modified by Paolo Bacchilega for the Quick Lounge applet
- */
-static char*
-panel_find_icon (GtkIconTheme  *icon_theme,
-		 const char    *icon_name,
-		 gint           size)
-{
-	char        *retval  = NULL;
-	GtkIconInfo *icon_info = NULL;
-	char        *icon_no_extension;
-	char        *p;
-
-
-	if (icon_name == NULL || strcmp (icon_name, "") == 0)
-		return NULL;
-
-	if (g_path_is_absolute (icon_name)) {
-		if (g_file_test (icon_name, G_FILE_TEST_EXISTS)) {
-			return g_strdup (icon_name);
-		} else {
-			char *basename;
-
-			basename = g_path_get_basename (icon_name);
-			retval = panel_find_icon (icon_theme,
-						  basename,
-						  size);
-			g_free (basename);
-
-			return retval;
-		}
-	}
-
-	/* This is needed because some .desktop files have an icon name *and*
-	 * an extension as icon */
-	icon_no_extension = g_strdup (icon_name);
-	p = strrchr (icon_no_extension, '.');
-	if (p &&
-	    (strcmp (p, ".png") == 0 ||
-	     strcmp (p, ".xpm") == 0 ||
-	     strcmp (p, ".svg") == 0)) {
-	    *p = 0;
-	}
-
-	icon_info = gtk_icon_theme_lookup_icon (icon_theme,
-						icon_no_extension,
-						size,
-						0);
-	g_free (icon_no_extension);
-
-        if (icon_info) {
-		retval = g_strdup (gtk_icon_info_get_filename (icon_info));
-		gtk_icon_info_free (icon_info);
-	}
-        else
-       		retval = NULL;
-
-	return retval;
-}
-
-
-GdkPixbuf *
-create_pixbuf (GtkIconTheme  *icon_theme,
-	       const char    *icon_name,
-	       int            icon_size)
-{
-	char      *icon_path;
-	GdkPixbuf *pixbuf;
-	int        iw;
-	int        ih;
-
-	g_return_val_if_fail (icon_theme != NULL, NULL);
-
-	icon_path = panel_find_icon (icon_theme, icon_name, icon_size);
-	if (icon_path == NULL)
-		return NULL;
-
-	pixbuf = gdk_pixbuf_new_from_file (icon_path, NULL);
-	g_free (icon_path);
-
-	if (pixbuf == NULL)
-		return NULL;
-
-	iw = gdk_pixbuf_get_width (pixbuf);
-	ih = gdk_pixbuf_get_height (pixbuf);
-
-	if ((iw > icon_size) || (ih > icon_size)) {
-		GdkPixbuf *scaled;
-		gdouble    factor;
-		gdouble    max = icon_size;
-		int        new_w, new_h;
-
-		factor = MIN (max / iw, max / ih);
-		new_w  = MAX ((int) (iw * factor), 1);
-		new_h = MAX ((int) (ih * factor), 1);
-
-		scaled = gdk_pixbuf_scale_simple (pixbuf,
-						  new_w,
-						  new_h,
-						  GDK_INTERP_BILINEAR);
-		g_object_unref (pixbuf);
-		pixbuf = scaled;
-	}
-
-	return pixbuf;
-}
-
-
-GtkWidget *
-create_image (GtkIconTheme  *icon_theme,
-	      const char    *icon_path,
-	      int            icon_size)
-{
-	GtkWidget *icon = NULL;
-	GdkPixbuf *pixbuf;
-
-	pixbuf = create_pixbuf (icon_theme, icon_path, icon_size);
-
-	if (pixbuf == NULL)
-		return NULL;
-
-	icon = gtk_image_new ();
-	gtk_image_set_from_pixbuf (GTK_IMAGE (icon), pixbuf);
-	g_object_unref (pixbuf);
-
-	gtk_widget_show (icon);
-
-	return icon;
-}
-
-
 int
 _gtk_widget_get_height (GtkWidget *widget)
 {
diff --git a/src/dlg-open-with.c b/src/dlg-open-with.c
index b4970d8..bc6f267 100644
--- a/src/dlg-open-with.c
+++ b/src/dlg-open-with.c
@@ -344,10 +344,10 @@ dlg_open_with (GtkWindow  *window,
 	for (scan = data->apps; scan; scan = scan->next) {
 		gboolean    found;
 		char        *utf8_name;
-		GThemedIcon *ticon;
-		GStrv       icon_names;
+		GIcon       *ticon;
+		GtkIconInfo *icon_info;
 		GdkPixbuf   *icon;
-
+		
 		app = scan->data;
 
 		found = FALSE;
@@ -371,16 +371,21 @@ dlg_open_with (GtkWindow  *window,
 				       &iter);
 
 		utf8_name = g_locale_to_utf8 (g_app_info_get_name (app), -1, NULL, NULL, NULL);
-		ticon = G_THEMED_ICON (g_app_info_get_icon (app));
-		g_object_get (ticon, "names", &icon_names, NULL);
-		icon = create_pixbuf (theme, icon_names[0], icon_size);		
+
+		ticon = g_app_info_get_icon (app);
+		icon_info = gtk_icon_theme_lookup_by_gicon (theme,
+                                                            ticon,
+                                                            icon_size,
+                                                            0);
+		icon = gtk_icon_info_load_icon (icon_info, NULL);
+		gtk_icon_info_free (icon_info);
+
 		gtk_list_store_set (GTK_LIST_STORE (data->app_model), &iter,
 				    ICON_COLUMN, icon,
 				    TEXT_COLUMN, utf8_name,
 				    DATA_COLUMN, app,
 				    -1);
 		g_free (utf8_name);
-		g_free (icon_names);
 		g_object_unref (ticon);
 	}
 
diff --git a/src/gth-location.c b/src/gth-location.c
index 1b7db69..4c503ea 100644
--- a/src/gth-location.c
+++ b/src/gth-location.c
@@ -524,18 +524,15 @@ get_first_separator_pos (GthLocation *loc,
 }
 
 
-
-/* Get a scaled icon as a GdkPixBuf for the given GMount */
 static GdkPixbuf*
-get_mount_icon (GthLocation *loc,
-                GMount *mount)
+get_mount_icon (GtkWidget  *widget,
+                GMount     *mount)
 {
-        GtkWidget    *widget = GTK_WIDGET (loc);
-        GdkPixbuf    *p;
-        GIcon *gicon;
-        int size;
-        char *icon_path;
+        GdkPixbuf    *pixbuf;
+        GIcon        *gicon;
+        int           size;
         GtkIconTheme *theme;
+	GtkIconInfo  *icon_info;
 
         theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
         size = get_folder_pixbuf_size_for_list (widget);
@@ -547,19 +544,18 @@ get_mount_icon (GthLocation *loc,
         if (gicon == NULL)
                 return NULL;
 
-        icon_path = g_icon_to_string (gicon);
-        if (icon_path == NULL || strcmp (icon_path, "") == 0) {
-                g_free(icon_path);
-                g_object_unref(gicon);
-                return NULL;
-        }
+	icon_info = gtk_icon_theme_lookup_by_gicon (theme,
+                                                    gicon,
+                                                    size,
+                                                    0);
+	pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
 
-        p = create_pixbuf (theme, icon_path, size);
+	gtk_icon_info_free (icon_info);
+        g_object_unref (gicon);
 
-        g_free(icon_path);
-        g_object_unref(gicon);
-        return p;
+        return pixbuf;
 }
+ 
 
 static void
 insert_drive_from_uri (GthLocation *loc,
@@ -618,7 +614,7 @@ update_drives (GthLocation *loc)
 
                 gfile = g_mount_get_root (mount);
 
-                pixbuf = get_mount_icon (loc, mount);
+                pixbuf = get_mount_icon ((GtkWidget *) loc, mount);
                 uri_name = g_mount_get_name (mount);
 
                 uri = g_file_get_uri(gfile);
@@ -696,7 +692,7 @@ update_uri (GthLocation *loc,
                         pixbuf = gdk_pixbuf_new_from_inline (-1, library_19_rgba, FALSE, NULL);
                 else {
                         if (g_file_equal (gfile, base_gfile) && mount != NULL)
-                                pixbuf = get_mount_icon (loc, mount);
+                                pixbuf = get_mount_icon ((GtkWidget *) loc, mount);
                         else
                                 pixbuf = get_icon_for_uri (GTK_WIDGET (loc), uri);
                 }
diff --git a/src/gth-viewer.c b/src/gth-viewer.c
index 6f782f5..b35a6f2 100644
--- a/src/gth-viewer.c
+++ b/src/gth-viewer.c
@@ -739,20 +739,31 @@ viewer_update_open_with_menu (GthViewer *viewer)
 		int           icon_size = get_folder_pixbuf_size_for_list (GTK_WIDGET (viewer));
 
 		for (scan = apps; scan; scan = scan->next) {
-			GAppInfo                *app = scan->data;
-			GtkWidget               *mitem;
-			GThemedIcon             *icon;
-			GStrv                   icon_names;
+			GAppInfo     *app = scan->data;
+			GtkWidget    *mitem;
+			GIcon        *icon;
+			GtkIconInfo  *icon_info;
+			GtkImage     *image;
+			GdkPixbuf    *pixbuf;
 
 			/* do not include gthumb itself */
 			if (strncmp (g_app_info_get_executable (app), "gthumb", 6) == 0)
 				continue;
 
 			mitem = gtk_image_menu_item_new_with_label (g_app_info_get_name (app));
-			icon = G_THEMED_ICON(g_app_info_get_icon (app));
-			g_object_get(icon, "names", &icon_names, NULL);
-
-			gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mitem), create_image (theme, icon_names[0], icon_size));
+			icon = g_app_info_get_icon (app);
+			icon_info = gtk_icon_theme_lookup_by_gicon (theme,
+                                                                    icon,
+                                                                    icon_size,
+                                                                    0);
+			pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
+			image = (GtkImage *) gtk_image_new ();
+			gtk_image_set_from_pixbuf (image, pixbuf);
+			g_object_unref (pixbuf);			
+                        gtk_icon_info_free (icon_info);
+			
+			gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mitem),
+						       (GtkWidget *) image);
 			g_object_set_data (G_OBJECT (mitem), "app", app);
 			g_signal_connect (mitem, "activate",
 					  G_CALLBACK (open_with_menu_item_activate_cb),
@@ -761,8 +772,6 @@ viewer_update_open_with_menu (GthViewer *viewer)
 			gtk_menu_shell_insert (GTK_MENU_SHELL (priv->open_with_popup_menu), mitem, pos++);
 
 			g_object_unref (icon);
-			if (icon_names)
-				g_free (icon_names);
 		}
 		g_list_free (apps);
 	} 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]