cellrendererpixbuf: Create iconhelper on-demand

Instead of storing one in the priv structure, only store an image
definition there.

This will allow future refactorings of the icon helper.
This commit is contained in:
Benjamin Otte 2015-12-08 11:27:37 +01:00
parent 01387428a3
commit 866e7dc733

View File

@ -87,8 +87,8 @@ enum {
struct _GtkCellRendererPixbufPrivate
{
GtkIconHelper *icon_helper;
GtkIconSize icon_size;
GtkImageDefinition *image_def;
GtkIconSize icon_size;
GdkPixbuf *pixbuf_expander_open;
GdkPixbuf *pixbuf_expander_closed;
@ -108,8 +108,7 @@ gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
cellpixbuf->priv = gtk_cell_renderer_pixbuf_get_instance_private (cellpixbuf);
priv = cellpixbuf->priv;
priv->icon_helper = _gtk_icon_helper_new ();
_gtk_icon_helper_set_force_scale_pixbuf (priv->icon_helper, TRUE);
priv->image_def = gtk_image_definition_new_empty ();
priv->icon_size = GTK_ICON_SIZE_MENU;
priv->follow_state = TRUE;
}
@ -120,7 +119,7 @@ gtk_cell_renderer_pixbuf_finalize (GObject *object)
GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
g_clear_object (&priv->icon_helper);
gtk_image_definition_unref (priv->image_def);
if (priv->pixbuf_expander_open)
g_object_unref (priv->pixbuf_expander_open);
@ -284,7 +283,7 @@ gtk_cell_renderer_pixbuf_get_property (GObject *object,
switch (param_id)
{
case PROP_PIXBUF:
g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
g_value_set_object (value, gtk_image_definition_get_pixbuf (priv->image_def));
break;
case PROP_PIXBUF_EXPANDER_OPEN:
g_value_set_object (value, priv->pixbuf_expander_open);
@ -293,10 +292,10 @@ gtk_cell_renderer_pixbuf_get_property (GObject *object,
g_value_set_object (value, priv->pixbuf_expander_closed);
break;
case PROP_SURFACE:
g_value_set_boxed (value, _gtk_icon_helper_peek_surface (priv->icon_helper));
g_value_set_boxed (value, gtk_image_definition_get_surface (priv->image_def));
break;
case PROP_STOCK_ID:
g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
g_value_set_string (value, gtk_image_definition_get_stock (priv->image_def));
break;
case PROP_STOCK_SIZE:
g_value_set_uint (value, priv->icon_size);
@ -308,10 +307,10 @@ gtk_cell_renderer_pixbuf_get_property (GObject *object,
g_value_set_boolean (value, priv->follow_state);
break;
case PROP_ICON_NAME:
g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
g_value_set_string (value, gtk_image_definition_get_icon_name (priv->image_def));
break;
case PROP_GICON:
g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
g_value_set_object (value, gtk_image_definition_get_gicon (priv->image_def));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@ -320,14 +319,10 @@ gtk_cell_renderer_pixbuf_get_property (GObject *object,
}
static void
notify_old_storage_type (GtkCellRendererPixbuf *cellpixbuf,
GtkImageType new_storage_type,
GtkImageType old_storage_type)
notify_storage_type (GtkCellRendererPixbuf *cellpixbuf,
GtkImageType storage_type)
{
if (new_storage_type == old_storage_type)
return;
switch (old_storage_type)
switch (storage_type)
{
case GTK_IMAGE_SURFACE:
g_object_notify (G_OBJECT (cellpixbuf), "surface");
@ -344,12 +339,35 @@ notify_old_storage_type (GtkCellRendererPixbuf *cellpixbuf,
case GTK_IMAGE_GICON:
g_object_notify (G_OBJECT (cellpixbuf), "gicon");
break;
case GTK_IMAGE_EMPTY:
default:
case GTK_IMAGE_ANIMATION:
g_assert_not_reached ();
case GTK_IMAGE_EMPTY:
break;
}
}
static void
take_image_definition (GtkCellRendererPixbuf *cellpixbuf,
GtkImageDefinition *def)
{
GtkCellRendererPixbufPrivate *priv;
GtkImageType old_storage_type, new_storage_type;
if (def == NULL)
def = gtk_image_definition_new_empty ();
priv = cellpixbuf->priv;
old_storage_type = gtk_image_definition_get_storage_type (priv->image_def);
new_storage_type = gtk_image_definition_get_storage_type (def);
if (new_storage_type != old_storage_type)
notify_storage_type (cellpixbuf, old_storage_type);
gtk_image_definition_unref (priv->image_def);
priv->image_def = def;
}
static void
gtk_cell_renderer_pixbuf_set_property (GObject *object,
guint param_id,
@ -358,13 +376,11 @@ gtk_cell_renderer_pixbuf_set_property (GObject *object,
{
GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
GtkImageType old_storage_type = _gtk_icon_helper_get_storage_type (priv->icon_helper);
switch (param_id)
{
case PROP_PIXBUF:
notify_old_storage_type (cellpixbuf, GTK_IMAGE_PIXBUF, old_storage_type);
_gtk_icon_helper_set_pixbuf (priv->icon_helper, g_value_get_object (value));
take_image_definition (cellpixbuf, gtk_image_definition_new_pixbuf (g_value_get_object (value), 1));
break;
case PROP_PIXBUF_EXPANDER_OPEN:
if (priv->pixbuf_expander_open)
@ -377,31 +393,26 @@ gtk_cell_renderer_pixbuf_set_property (GObject *object,
priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
break;
case PROP_SURFACE:
notify_old_storage_type (cellpixbuf, GTK_IMAGE_SURFACE, old_storage_type);
_gtk_icon_helper_set_surface (priv->icon_helper, g_value_get_boxed (value));
take_image_definition (cellpixbuf, gtk_image_definition_new_surface (g_value_get_boxed (value)));
break;
case PROP_STOCK_ID:
notify_old_storage_type (cellpixbuf, GTK_IMAGE_STOCK, old_storage_type);
_gtk_icon_helper_set_stock_id (priv->icon_helper, g_value_get_string (value), priv->icon_size);
take_image_definition (cellpixbuf, gtk_image_definition_new_stock (g_value_get_string (value)));
break;
case PROP_STOCK_SIZE:
priv->icon_size = g_value_get_uint (value);
_gtk_icon_helper_set_icon_size (priv->icon_helper, priv->icon_size);
break;
case PROP_STOCK_DETAIL:
g_free (priv->stock_detail);
priv->stock_detail = g_value_dup_string (value);
break;
case PROP_ICON_NAME:
notify_old_storage_type (cellpixbuf, GTK_IMAGE_ICON_NAME, old_storage_type);
_gtk_icon_helper_set_icon_name (priv->icon_helper, g_value_get_string (value), priv->icon_size);
take_image_definition (cellpixbuf, gtk_image_definition_new_icon_name (g_value_get_string (value)));
break;
case PROP_FOLLOW_STATE:
priv->follow_state = g_value_get_boolean (value);
break;
case PROP_GICON:
notify_old_storage_type (cellpixbuf, GTK_IMAGE_GICON, old_storage_type);
_gtk_icon_helper_set_gicon (priv->icon_helper, g_value_get_object (value), priv->icon_size);
take_image_definition (cellpixbuf, gtk_image_definition_new_gicon (g_value_get_object (value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@ -428,6 +439,20 @@ gtk_cell_renderer_pixbuf_new (void)
return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
}
static GtkIconHelper *
create_icon_helper (GtkCellRendererPixbuf *cellpixbuf)
{
GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
GtkIconHelper *helper;
helper = _gtk_icon_helper_new ();
_gtk_icon_helper_set_force_scale_pixbuf (helper, TRUE);
_gtk_icon_helper_set_definition (helper, priv->image_def);
_gtk_icon_helper_set_icon_size (helper, priv->icon_size);
return helper;
}
static void
gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
@ -445,16 +470,19 @@ gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
gint calc_height;
gint xpad, ypad;
GtkStyleContext *context;
GtkIconHelper *icon_helper;
context = gtk_widget_get_style_context (widget);
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
icon_helper = create_icon_helper (cellpixbuf);
if (!_gtk_icon_helper_get_is_empty (priv->icon_helper))
_gtk_icon_helper_get_size (priv->icon_helper,
if (!_gtk_icon_helper_get_is_empty (icon_helper))
_gtk_icon_helper_get_size (icon_helper,
gtk_widget_get_style_context (widget),
&pixbuf_width, &pixbuf_height);
g_object_unref (icon_helper);
gtk_style_context_restore (context);
if (priv->pixbuf_expander_open)
@ -562,7 +590,7 @@ gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
}
if (icon_helper == NULL)
icon_helper = g_object_ref (priv->icon_helper);
icon_helper = create_icon_helper (cellpixbuf);
_gtk_icon_helper_set_window (icon_helper,
gtk_widget_get_window (widget));