1 /* gtkcellrendererpixbuf.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcellrendererpixbuf.h"
23 #include "gtkiconfactory.h"
24 #include "gtkicontheme.h"
26 #include "gtkprivate.h"
30 * SECTION:gtkcellrendererpixbuf
31 * @Short_description: Renders a pixbuf in a cell
32 * @Title: GtkCellRendererPixbuf
34 * A #GtkCellRendererPixbuf can be used to render an image in a cell. It allows
35 * to render either a given #GdkPixbuf (set via the
36 * #GtkCellRendererPixbuf:pixbuf property) or a stock icon (set via the
37 * #GtkCellRendererPixbuf:stock-id property).
39 * To support the tree view, #GtkCellRendererPixbuf also supports rendering two
40 * alternative pixbufs, when the #GtkCellRenderer:is-expander property is %TRUE.
41 * If the #GtkCellRenderer:is-expanded property is %TRUE and the
42 * #GtkCellRendererPixbuf:pixbuf-expander-open property is set to a pixbuf, it
43 * renders that pixbuf, if the #GtkCellRenderer:is-expanded property is %FALSE
44 * and the #GtkCellRendererPixbuf:pixbuf-expander-closed property is set to a
45 * pixbuf, it renders that one.
49 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
53 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
57 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
58 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
60 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
62 const GdkRectangle *rectangle,
67 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
70 const GdkRectangle *background_area,
71 const GdkRectangle *cell_area,
72 GtkCellRendererState flags);
78 PROP_PIXBUF_EXPANDER_OPEN,
79 PROP_PIXBUF_EXPANDER_CLOSED,
89 struct _GtkCellRendererPixbufPrivate
92 GdkPixbuf *pixbuf_expander_open;
93 GdkPixbuf *pixbuf_expander_closed;
97 GtkIconSize stock_size;
99 gboolean follow_state;
107 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
111 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
113 GtkCellRendererPixbufPrivate *priv;
115 cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
116 GTK_TYPE_CELL_RENDERER_PIXBUF,
117 GtkCellRendererPixbufPrivate);
118 priv = cellpixbuf->priv;
120 priv->stock_size = GTK_ICON_SIZE_MENU;
124 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
126 GObjectClass *object_class = G_OBJECT_CLASS (class);
127 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
129 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
131 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
132 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
134 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
135 cell_class->render = gtk_cell_renderer_pixbuf_render;
137 g_object_class_install_property (object_class,
139 g_param_spec_object ("pixbuf",
141 P_("The pixbuf to render"),
143 GTK_PARAM_READWRITE));
145 g_object_class_install_property (object_class,
146 PROP_PIXBUF_EXPANDER_OPEN,
147 g_param_spec_object ("pixbuf-expander-open",
148 P_("Pixbuf Expander Open"),
149 P_("Pixbuf for open expander"),
151 GTK_PARAM_READWRITE));
153 g_object_class_install_property (object_class,
154 PROP_PIXBUF_EXPANDER_CLOSED,
155 g_param_spec_object ("pixbuf-expander-closed",
156 P_("Pixbuf Expander Closed"),
157 P_("Pixbuf for closed expander"),
159 GTK_PARAM_READWRITE));
161 g_object_class_install_property (object_class,
163 g_param_spec_string ("stock-id",
165 P_("The stock ID of the stock icon to render"),
167 GTK_PARAM_READWRITE));
169 g_object_class_install_property (object_class,
171 g_param_spec_uint ("stock-size",
173 P_("The GtkIconSize value that specifies the size of the rendered icon"),
177 GTK_PARAM_READWRITE));
179 g_object_class_install_property (object_class,
181 g_param_spec_string ("stock-detail",
183 P_("Render detail to pass to the theme engine"),
185 GTK_PARAM_READWRITE));
189 * GtkCellRendererPixbuf:icon-name:
191 * The name of the themed icon to display.
192 * This property only has an effect if not overridden by "stock_id"
193 * or "pixbuf" properties.
197 g_object_class_install_property (object_class,
199 g_param_spec_string ("icon-name",
201 P_("The name of the icon from the icon theme"),
203 GTK_PARAM_READWRITE));
206 * GtkCellRendererPixbuf:follow-state:
208 * Specifies whether the rendered pixbuf should be colorized
209 * according to the #GtkCellRendererState.
213 g_object_class_install_property (object_class,
215 g_param_spec_boolean ("follow-state",
217 P_("Whether the rendered pixbuf should be "
218 "colorized according to the state"),
220 GTK_PARAM_READWRITE));
223 * GtkCellRendererPixbuf:gicon:
225 * The GIcon representing the icon to display.
226 * If the icon theme is changed, the image will be updated
231 g_object_class_install_property (object_class,
233 g_param_spec_object ("gicon",
235 P_("The GIcon being displayed"),
237 GTK_PARAM_READWRITE));
241 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
245 gtk_cell_renderer_pixbuf_finalize (GObject *object)
247 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
248 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
251 g_object_unref (priv->pixbuf);
252 if (priv->pixbuf_expander_open)
253 g_object_unref (priv->pixbuf_expander_open);
254 if (priv->pixbuf_expander_closed)
255 g_object_unref (priv->pixbuf_expander_closed);
257 g_free (priv->stock_id);
258 g_free (priv->stock_detail);
259 g_free (priv->icon_name);
262 g_object_unref (priv->gicon);
264 G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
268 gtk_cell_renderer_pixbuf_get_property (GObject *object,
273 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
274 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
279 g_value_set_object (value, priv->pixbuf);
281 case PROP_PIXBUF_EXPANDER_OPEN:
282 g_value_set_object (value, priv->pixbuf_expander_open);
284 case PROP_PIXBUF_EXPANDER_CLOSED:
285 g_value_set_object (value, priv->pixbuf_expander_closed);
288 g_value_set_string (value, priv->stock_id);
290 case PROP_STOCK_SIZE:
291 g_value_set_uint (value, priv->stock_size);
293 case PROP_STOCK_DETAIL:
294 g_value_set_string (value, priv->stock_detail);
296 case PROP_FOLLOW_STATE:
297 g_value_set_boolean (value, priv->follow_state);
300 g_value_set_string (value, priv->icon_name);
303 g_value_set_object (value, priv->gicon);
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
312 gtk_cell_renderer_pixbuf_set_property (GObject *object,
317 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
318 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
324 g_object_unref (priv->pixbuf);
325 priv->pixbuf = (GdkPixbuf*) g_value_dup_object (value);
330 g_free (priv->stock_id);
331 priv->stock_id = NULL;
332 g_object_notify (object, "stock-id");
336 g_free (priv->icon_name);
337 priv->icon_name = NULL;
338 g_object_notify (object, "icon-name");
342 g_object_unref (priv->gicon);
344 g_object_notify (object, "gicon");
348 case PROP_PIXBUF_EXPANDER_OPEN:
349 if (priv->pixbuf_expander_open)
350 g_object_unref (priv->pixbuf_expander_open);
351 priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
353 case PROP_PIXBUF_EXPANDER_CLOSED:
354 if (priv->pixbuf_expander_closed)
355 g_object_unref (priv->pixbuf_expander_closed);
356 priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
363 g_object_unref (priv->pixbuf);
365 g_object_notify (object, "pixbuf");
367 g_free (priv->stock_id);
369 priv->stock_id = g_value_dup_string (value);
374 g_object_unref (priv->pixbuf);
376 g_object_notify (object, "pixbuf");
380 g_free (priv->icon_name);
381 priv->icon_name = NULL;
382 g_object_notify (object, "icon-name");
386 g_object_unref (priv->gicon);
388 g_object_notify (object, "gicon");
392 case PROP_STOCK_SIZE:
393 priv->stock_size = g_value_get_uint (value);
395 case PROP_STOCK_DETAIL:
396 g_free (priv->stock_detail);
397 priv->stock_detail = g_value_dup_string (value);
404 g_object_unref (priv->pixbuf);
406 g_object_notify (object, "pixbuf");
408 g_free (priv->icon_name);
410 priv->icon_name = g_value_dup_string (value);
415 g_object_unref (priv->pixbuf);
417 g_object_notify (object, "pixbuf");
421 g_free (priv->stock_id);
422 priv->stock_id = NULL;
423 g_object_notify (object, "stock-id");
427 g_object_unref (priv->gicon);
429 g_object_notify (object, "gicon");
433 case PROP_FOLLOW_STATE:
434 priv->follow_state = g_value_get_boolean (value);
441 g_object_unref (priv->pixbuf);
443 g_object_notify (object, "pixbuf");
445 g_object_unref (priv->gicon);
447 priv->gicon = (GIcon *) g_value_dup_object (value);
452 g_object_unref (priv->pixbuf);
454 g_object_notify (object, "pixbuf");
458 g_free (priv->stock_id);
459 priv->stock_id = NULL;
460 g_object_notify (object, "stock-id");
464 g_free (priv->icon_name);
465 priv->icon_name = NULL;
466 g_object_notify (object, "icon-name");
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
477 * gtk_cell_renderer_pixbuf_new:
479 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
480 * parameters using object properties. Object properties can be set
481 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
482 * can bind a property to a value in a #GtkTreeModel. For example, you
483 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
484 * in the model, thus rendering a different image in each row of the
487 * Return value: the new cell renderer
490 gtk_cell_renderer_pixbuf_new (void)
492 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
496 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
499 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
502 g_object_unref (priv->pixbuf);
504 priv->pixbuf = gtk_widget_render_icon_pixbuf (widget,
508 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
512 gtk_cell_renderer_pixbuf_create_themed_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
515 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
517 GtkIconTheme *icon_theme;
518 GtkSettings *settings;
524 g_object_unref (priv->pixbuf);
528 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
529 icon_theme = gtk_icon_theme_get_for_screen (screen);
530 settings = gtk_settings_get_for_screen (screen);
532 if (!gtk_icon_size_lookup_for_settings (settings,
536 g_warning ("Invalid icon size %u\n", priv->stock_size);
541 info = gtk_icon_theme_lookup_icon (icon_theme,
544 GTK_ICON_LOOKUP_USE_BUILTIN);
545 else if (priv->gicon)
546 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
549 GTK_ICON_LOOKUP_USE_BUILTIN);
555 GtkStyleContext *context;
557 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
558 priv->pixbuf = gtk_icon_info_load_symbolic_for_context (info,
562 gtk_icon_info_free (info);
565 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
569 create_symbolic_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
573 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
575 GtkIconTheme *icon_theme;
576 GtkSettings *settings;
580 gboolean is_symbolic;
582 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
583 icon_theme = gtk_icon_theme_get_for_screen (screen);
584 settings = gtk_settings_get_for_screen (screen);
586 if (!gtk_icon_size_lookup_for_settings (settings,
590 g_warning ("Invalid icon size %u\n", priv->stock_size);
596 info = gtk_icon_theme_lookup_icon (icon_theme,
599 GTK_ICON_LOOKUP_USE_BUILTIN);
600 else if (priv->gicon)
601 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
604 GTK_ICON_LOOKUP_USE_BUILTIN);
610 GtkStyleContext *context;
612 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
614 gtk_style_context_save (context);
615 gtk_style_context_set_state (context, state);
616 pixbuf = gtk_icon_info_load_symbolic_for_context (info,
621 gtk_style_context_restore (context);
622 gtk_icon_info_free (info);
625 g_clear_object (&pixbuf);
634 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
636 const GdkRectangle *cell_area,
642 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
643 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
644 gint pixbuf_width = 0;
645 gint pixbuf_height = 0;
653 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
654 else if (priv->icon_name || priv->gicon)
655 gtk_cell_renderer_pixbuf_create_themed_pixbuf (cellpixbuf, widget);
660 pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
661 pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
663 if (priv->pixbuf_expander_open)
665 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
666 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
668 if (priv->pixbuf_expander_closed)
670 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
671 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
674 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
675 calc_width = (gint) xpad * 2 + pixbuf_width;
676 calc_height = (gint) ypad * 2 + pixbuf_height;
678 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
680 gfloat xalign, yalign;
682 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
685 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
686 (1.0 - xalign) : xalign) *
687 (cell_area->width - calc_width));
688 *x_offset = MAX (*x_offset, 0);
692 *y_offset = (yalign *
693 (cell_area->height - calc_height));
694 *y_offset = MAX (*y_offset, 0);
699 if (x_offset) *x_offset = 0;
700 if (y_offset) *y_offset = 0;
707 *height = calc_height;
711 transform_pixbuf_state (GdkPixbuf *pixbuf,
712 GtkStyleContext *context)
714 GtkIconSource *source;
717 source = gtk_icon_source_new ();
718 gtk_icon_source_set_pixbuf (source, pixbuf);
719 /* The size here is arbitrary; since size isn't
720 * wildcarded in the source, it isn't supposed to be
721 * scaled by the engine function
723 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
724 gtk_icon_source_set_size_wildcarded (source, FALSE);
726 retval = gtk_render_icon_pixbuf (context, source,
729 gtk_icon_source_free (source);
735 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
738 const GdkRectangle *background_area,
739 const GdkRectangle *cell_area,
740 GtkCellRendererState flags)
743 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
744 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
745 GtkStyleContext *context;
746 GdkPixbuf *pixbuf, *stated;
747 GdkRectangle pix_rect;
748 GdkRectangle draw_rect;
749 gboolean is_expander;
754 gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
757 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
758 x += cell_area->x + xpad;
759 y += cell_area->y + ypad;
761 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
764 pixbuf = priv->pixbuf;
766 g_object_get (cell, "is-expander", &is_expander, NULL);
769 gboolean is_expanded;
771 g_object_get (cell, "is-expanded", &is_expanded, NULL);
774 priv->pixbuf_expander_open != NULL)
775 pixbuf = priv->pixbuf_expander_open;
776 else if (!is_expanded &&
777 priv->pixbuf_expander_closed != NULL)
778 pixbuf = priv->pixbuf_expander_closed;
784 g_object_ref (pixbuf);
786 context = gtk_widget_get_style_context (widget);
787 gtk_style_context_save (context);
789 state = GTK_STATE_FLAG_NORMAL;
791 if (!gtk_widget_get_sensitive (widget) ||
792 !gtk_cell_renderer_get_sensitive (cell))
793 state |= GTK_STATE_FLAG_INSENSITIVE;
794 else if (priv->follow_state &&
795 (flags & (GTK_CELL_RENDERER_SELECTED |
796 GTK_CELL_RENDERER_PRELIT)) != 0)
797 state = gtk_cell_renderer_get_state (cell, widget, flags);
799 if (state != GTK_STATE_FLAG_NORMAL)
801 stated = create_symbolic_pixbuf (cellpixbuf, widget, state);
804 stated = transform_pixbuf_state (pixbuf, context);
806 g_object_unref (pixbuf);
810 gtk_render_icon (context, cr, pixbuf, x, y);
812 gtk_style_context_restore (context);
813 g_object_unref (pixbuf);