From cc7cf39150ee95100195d77b9e9291dae8fd419c Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Tue, 14 Feb 2012 00:15:48 +0100 Subject: [PATCH] Unify getting an image for an item --- katze/katze-arrayaction.c | 49 ++++----------------------------------- katze/katze-item.c | 40 ++++++++++++++++++++++++++++++++ katze/katze-item.h | 5 +++- midori/midori-view.c | 9 ++++--- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/katze/katze-arrayaction.c b/katze/katze-arrayaction.c index 5ce84f52..e69e1e75 100644 --- a/katze/katze-arrayaction.c +++ b/katze/katze-arrayaction.c @@ -355,8 +355,6 @@ katze_array_action_generate_menu (KatzeArrayAction* array_action, gint summand; KatzeItem* item; GtkWidget* menuitem; - const gchar* icon_name; - GdkPixbuf* icon; GtkWidget* image; GtkWidget* submenu; @@ -385,18 +383,7 @@ katze_array_action_generate_menu (KatzeArrayAction* array_action, } menuitem = katze_image_menu_item_new_ellipsized ( katze_item_get_name (item)); - if ((icon_name = katze_item_get_icon (item)) && *icon_name) - image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); - else - { - if (KATZE_ITEM_IS_FOLDER (item)) - icon = gtk_widget_render_icon (menuitem, - GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL); - else - icon = katze_load_cached_icon (katze_item_get_uri (item), proxy); - image = gtk_image_new_from_pixbuf (icon); - g_object_unref (icon); - } + image = katze_item_get_image (item); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image); gtk_image_menu_item_set_always_show_image ( GTK_IMAGE_MENU_ITEM (menuitem), TRUE); @@ -552,7 +539,6 @@ katze_array_action_item_notify_cb (KatzeItem* item, const gchar* property; const gchar* title; const gchar* desc; - GdkPixbuf* icon; GtkWidget* image; if (!G_IS_PARAM_SPEC_STRING (pspec)) @@ -579,16 +565,13 @@ katze_array_action_item_notify_cb (KatzeItem* item, } else if (KATZE_ITEM_IS_BOOKMARK (item) && !strcmp (property, "uri")) { - icon = katze_load_cached_icon (katze_item_get_uri (item), GTK_WIDGET (toolitem)); - image = gtk_image_new_from_pixbuf (icon); - g_object_unref (icon); + image = katze_item_get_image (item); gtk_widget_show (image); gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image); } else if (!strcmp (property, "icon")) { - image = gtk_image_new_from_icon_name (katze_item_get_icon (item), - GTK_ICON_SIZE_MENU); + image = katze_item_get_image (item); gtk_widget_show (image); gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image); } @@ -600,25 +583,12 @@ katze_array_action_proxy_create_menu_proxy_cb (GtkWidget* proxy, { KatzeArrayAction* array_action; GtkWidget* menuitem; - const gchar* icon_name; GtkWidget* image; - GdkPixbuf* icon; array_action = g_object_get_data (G_OBJECT (proxy), "KatzeArrayAction"); menuitem = katze_image_menu_item_new_ellipsized ( katze_item_get_name (item)); - if ((icon_name = katze_item_get_icon (item)) && *icon_name) - image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); - else - { - if (KATZE_ITEM_IS_FOLDER (item)) - icon = gtk_widget_render_icon (menuitem, - GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL); - else - icon = katze_load_cached_icon (katze_item_get_uri (item), proxy); - image = gtk_image_new_from_pixbuf (icon); - g_object_unref (icon); - } + image = katze_item_get_image (item); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image); gtk_image_menu_item_set_always_show_image ( GTK_IMAGE_MENU_ITEM (menuitem), TRUE); @@ -674,7 +644,6 @@ katze_array_action_create_tool_item_for (KatzeArrayAction* array_action, const gchar* uri; const gchar* desc; GtkToolItem* toolitem; - GdkPixbuf* icon; GtkWidget* image; GtkWidget* label; @@ -686,20 +655,12 @@ katze_array_action_create_tool_item_for (KatzeArrayAction* array_action, return gtk_separator_tool_item_new (); if (KATZE_ITEM_IS_FOLDER (item)) - { toolitem = gtk_toggle_tool_button_new (); - icon = gtk_widget_render_icon (GTK_WIDGET (toolitem), - GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL); - } else - { toolitem = gtk_tool_button_new (NULL, ""); - icon = katze_load_cached_icon (uri, GTK_WIDGET (toolitem)); - } g_signal_connect (toolitem, "create-menu-proxy", G_CALLBACK (katze_array_action_proxy_create_menu_proxy_cb), item); - image = gtk_image_new_from_pixbuf (icon); - g_object_unref (icon); + image = katze_item_get_image (item); gtk_widget_show (image); gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image); label = gtk_label_new (NULL); diff --git a/katze/katze-item.c b/katze/katze-item.c index ed54857f..af708d37 100644 --- a/katze/katze-item.c +++ b/katze/katze-item.c @@ -417,6 +417,46 @@ katze_item_set_icon (KatzeItem* item, g_object_notify (G_OBJECT (item), "icon"); } +/** + * katze_item_get_image: + * @item: a #KatzeItem + * + * Retrieves a #GtkImage fit to display @item. + * + * Return value: the icon of the item + * + * Since: 0.4.4 + **/ +GtkWidget* +katze_item_get_image (KatzeItem* item) +{ + GtkWidget* image; + GdkPixbuf* pixbuf; + const gchar* icon; + + g_return_val_if_fail (KATZE_IS_ITEM (item), NULL); + + if (KATZE_ITEM_IS_FOLDER (item)) + image = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU); + else if ((pixbuf = g_object_get_data (G_OBJECT (item), "pixbuf"))) + image = gtk_image_new_from_pixbuf (pixbuf); + else if ((icon = katze_item_get_icon (item)) && !strchr (icon, '/')) + image = gtk_image_new_from_icon_name (icon, GTK_ICON_SIZE_MENU); + else + { + if (!(pixbuf = katze_load_cached_icon (icon, NULL))) + pixbuf = katze_load_cached_icon (item->uri, NULL); + if (pixbuf) + { + image = gtk_image_new_from_pixbuf (pixbuf); + g_object_unref (pixbuf); + } + else + image = gtk_image_new_from_stock (GTK_STOCK_FILE, GTK_ICON_SIZE_MENU); + } + return image; +} + /** * katze_item_get_token: * @item: a #KatzeItem diff --git a/katze/katze-item.h b/katze/katze-item.h index 40c250f8..ceb7f3cf 100644 --- a/katze/katze-item.h +++ b/katze/katze-item.h @@ -12,7 +12,7 @@ #ifndef __KATZE_ITEM_H__ #define __KATZE_ITEM_H__ -#include +#include G_BEGIN_DECLS @@ -91,6 +91,9 @@ void katze_item_set_icon (KatzeItem* item, const gchar* icon); +GtkWidget* +katze_item_get_image (KatzeItem* item); + const gchar* katze_item_get_token (KatzeItem* item); diff --git a/midori/midori-view.c b/midori/midori-view.c index ff593f51..440bf384 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -663,6 +663,9 @@ midori_view_apply_icon (MidoriView* view, const gchar* icon_name) { katze_item_set_icon (view->item, icon_name); + /* katze_item_get_image knows about this pixbuf */ + g_object_set_data_full (G_OBJECT (view->item), "pixbuf", g_object_ref (icon), + (GDestroyNotify)g_object_unref); katze_object_assign (view->icon, icon); g_object_notify (G_OBJECT (view), "icon"); @@ -677,11 +680,7 @@ midori_view_apply_icon (MidoriView* view, } if (view->menu_item) { - GtkWidget* image; - if (icon_name && !strchr (icon_name, '/')) - image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); - else - image = gtk_image_new_from_pixbuf (view->icon); + GtkWidget* image = katze_item_get_image (view->item); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (view->menu_item), image); } } -- 2.39.5