Unify getting an image for an item

This commit is contained in:
Christian Dywan 2012-02-14 00:15:48 +01:00
parent e268ffecfc
commit cc7cf39150
4 changed files with 53 additions and 50 deletions

View file

@ -355,8 +355,6 @@ katze_array_action_generate_menu (KatzeArrayAction* array_action,
gint summand; gint summand;
KatzeItem* item; KatzeItem* item;
GtkWidget* menuitem; GtkWidget* menuitem;
const gchar* icon_name;
GdkPixbuf* icon;
GtkWidget* image; GtkWidget* image;
GtkWidget* submenu; GtkWidget* submenu;
@ -385,18 +383,7 @@ katze_array_action_generate_menu (KatzeArrayAction* array_action,
} }
menuitem = katze_image_menu_item_new_ellipsized ( menuitem = katze_image_menu_item_new_ellipsized (
katze_item_get_name (item)); katze_item_get_name (item));
if ((icon_name = katze_item_get_icon (item)) && *icon_name) image = katze_item_get_image (item);
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);
}
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
gtk_image_menu_item_set_always_show_image ( gtk_image_menu_item_set_always_show_image (
GTK_IMAGE_MENU_ITEM (menuitem), TRUE); GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
@ -552,7 +539,6 @@ katze_array_action_item_notify_cb (KatzeItem* item,
const gchar* property; const gchar* property;
const gchar* title; const gchar* title;
const gchar* desc; const gchar* desc;
GdkPixbuf* icon;
GtkWidget* image; GtkWidget* image;
if (!G_IS_PARAM_SPEC_STRING (pspec)) 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")) else if (KATZE_ITEM_IS_BOOKMARK (item) && !strcmp (property, "uri"))
{ {
icon = katze_load_cached_icon (katze_item_get_uri (item), GTK_WIDGET (toolitem)); image = katze_item_get_image (item);
image = gtk_image_new_from_pixbuf (icon);
g_object_unref (icon);
gtk_widget_show (image); gtk_widget_show (image);
gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image); gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image);
} }
else if (!strcmp (property, "icon")) else if (!strcmp (property, "icon"))
{ {
image = gtk_image_new_from_icon_name (katze_item_get_icon (item), image = katze_item_get_image (item);
GTK_ICON_SIZE_MENU);
gtk_widget_show (image); gtk_widget_show (image);
gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), 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; KatzeArrayAction* array_action;
GtkWidget* menuitem; GtkWidget* menuitem;
const gchar* icon_name;
GtkWidget* image; GtkWidget* image;
GdkPixbuf* icon;
array_action = g_object_get_data (G_OBJECT (proxy), "KatzeArrayAction"); array_action = g_object_get_data (G_OBJECT (proxy), "KatzeArrayAction");
menuitem = katze_image_menu_item_new_ellipsized ( menuitem = katze_image_menu_item_new_ellipsized (
katze_item_get_name (item)); katze_item_get_name (item));
if ((icon_name = katze_item_get_icon (item)) && *icon_name) image = katze_item_get_image (item);
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);
}
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
gtk_image_menu_item_set_always_show_image ( gtk_image_menu_item_set_always_show_image (
GTK_IMAGE_MENU_ITEM (menuitem), TRUE); 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* uri;
const gchar* desc; const gchar* desc;
GtkToolItem* toolitem; GtkToolItem* toolitem;
GdkPixbuf* icon;
GtkWidget* image; GtkWidget* image;
GtkWidget* label; GtkWidget* label;
@ -686,20 +655,12 @@ katze_array_action_create_tool_item_for (KatzeArrayAction* array_action,
return gtk_separator_tool_item_new (); return gtk_separator_tool_item_new ();
if (KATZE_ITEM_IS_FOLDER (item)) if (KATZE_ITEM_IS_FOLDER (item))
{
toolitem = gtk_toggle_tool_button_new (); toolitem = gtk_toggle_tool_button_new ();
icon = gtk_widget_render_icon (GTK_WIDGET (toolitem),
GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL);
}
else else
{
toolitem = gtk_tool_button_new (NULL, ""); toolitem = gtk_tool_button_new (NULL, "");
icon = katze_load_cached_icon (uri, GTK_WIDGET (toolitem));
}
g_signal_connect (toolitem, "create-menu-proxy", g_signal_connect (toolitem, "create-menu-proxy",
G_CALLBACK (katze_array_action_proxy_create_menu_proxy_cb), item); G_CALLBACK (katze_array_action_proxy_create_menu_proxy_cb), item);
image = gtk_image_new_from_pixbuf (icon); image = katze_item_get_image (item);
g_object_unref (icon);
gtk_widget_show (image); gtk_widget_show (image);
gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image); gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (toolitem), image);
label = gtk_label_new (NULL); label = gtk_label_new (NULL);

View file

@ -417,6 +417,46 @@ katze_item_set_icon (KatzeItem* item,
g_object_notify (G_OBJECT (item), "icon"); 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: * katze_item_get_token:
* @item: a #KatzeItem * @item: a #KatzeItem

View file

@ -12,7 +12,7 @@
#ifndef __KATZE_ITEM_H__ #ifndef __KATZE_ITEM_H__
#define __KATZE_ITEM_H__ #define __KATZE_ITEM_H__
#include <glib-object.h> #include <gtk/gtk.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -91,6 +91,9 @@ void
katze_item_set_icon (KatzeItem* item, katze_item_set_icon (KatzeItem* item,
const gchar* icon); const gchar* icon);
GtkWidget*
katze_item_get_image (KatzeItem* item);
const gchar* const gchar*
katze_item_get_token (KatzeItem* item); katze_item_get_token (KatzeItem* item);

View file

@ -663,6 +663,9 @@ midori_view_apply_icon (MidoriView* view,
const gchar* icon_name) const gchar* icon_name)
{ {
katze_item_set_icon (view->item, 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); katze_object_assign (view->icon, icon);
g_object_notify (G_OBJECT (view), "icon"); g_object_notify (G_OBJECT (view), "icon");
@ -677,11 +680,7 @@ midori_view_apply_icon (MidoriView* view,
} }
if (view->menu_item) if (view->menu_item)
{ {
GtkWidget* image; GtkWidget* image = katze_item_get_image (view->item);
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);
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (view->menu_item), image); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (view->menu_item), image);
} }
} }