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, see <http://www.gnu.org/licenses/>.
20 #include "gtkcellrendererpixbuf.h"
21 #include "gtkiconfactory.h"
22 #include "gtkiconhelperprivate.h"
23 #include "gtkicontheme.h"
25 #include "gtkprivate.h"
26 #include "a11y/gtkimagecellaccessible.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_get_size (GtkCellRenderer *cell,
59 const GdkRectangle *rectangle,
64 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
67 const GdkRectangle *background_area,
68 const GdkRectangle *cell_area,
69 GtkCellRendererState flags);
75 PROP_PIXBUF_EXPANDER_OPEN,
76 PROP_PIXBUF_EXPANDER_CLOSED,
86 struct _GtkCellRendererPixbufPrivate
88 GtkIconHelper *icon_helper;
89 GtkIconSize icon_size;
91 GdkPixbuf *pixbuf_expander_open;
92 GdkPixbuf *pixbuf_expander_closed;
94 gboolean follow_state;
100 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
104 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
106 GtkCellRendererPixbufPrivate *priv;
108 cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
109 GTK_TYPE_CELL_RENDERER_PIXBUF,
110 GtkCellRendererPixbufPrivate);
111 priv = cellpixbuf->priv;
112 priv->icon_helper = _gtk_icon_helper_new ();
113 priv->icon_size = GTK_ICON_SIZE_MENU;
117 gtk_cell_renderer_pixbuf_finalize (GObject *object)
119 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
120 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
122 g_clear_object (&priv->icon_helper);
124 if (priv->pixbuf_expander_open)
125 g_object_unref (priv->pixbuf_expander_open);
126 if (priv->pixbuf_expander_closed)
127 g_object_unref (priv->pixbuf_expander_closed);
129 g_free (priv->stock_detail);
131 G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
135 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
137 GObjectClass *object_class = G_OBJECT_CLASS (class);
138 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
140 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
142 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
143 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
145 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
146 cell_class->render = gtk_cell_renderer_pixbuf_render;
148 g_object_class_install_property (object_class,
150 g_param_spec_object ("pixbuf",
152 P_("The pixbuf to render"),
154 GTK_PARAM_READWRITE));
156 g_object_class_install_property (object_class,
157 PROP_PIXBUF_EXPANDER_OPEN,
158 g_param_spec_object ("pixbuf-expander-open",
159 P_("Pixbuf Expander Open"),
160 P_("Pixbuf for open expander"),
162 GTK_PARAM_READWRITE));
164 g_object_class_install_property (object_class,
165 PROP_PIXBUF_EXPANDER_CLOSED,
166 g_param_spec_object ("pixbuf-expander-closed",
167 P_("Pixbuf Expander Closed"),
168 P_("Pixbuf for closed expander"),
170 GTK_PARAM_READWRITE));
172 g_object_class_install_property (object_class,
174 g_param_spec_string ("stock-id",
176 P_("The stock ID of the stock icon to render"),
178 GTK_PARAM_READWRITE));
180 g_object_class_install_property (object_class,
182 g_param_spec_uint ("stock-size",
184 P_("The GtkIconSize value that specifies the size of the rendered icon"),
188 GTK_PARAM_READWRITE));
190 g_object_class_install_property (object_class,
192 g_param_spec_string ("stock-detail",
194 P_("Render detail to pass to the theme engine"),
196 GTK_PARAM_READWRITE));
200 * GtkCellRendererPixbuf:icon-name:
202 * The name of the themed icon to display.
203 * This property only has an effect if not overridden by "stock_id"
204 * or "pixbuf" properties.
208 g_object_class_install_property (object_class,
210 g_param_spec_string ("icon-name",
212 P_("The name of the icon from the icon theme"),
214 GTK_PARAM_READWRITE));
217 * GtkCellRendererPixbuf:follow-state:
219 * Specifies whether the rendered pixbuf should be colorized
220 * according to the #GtkCellRendererState.
224 g_object_class_install_property (object_class,
226 g_param_spec_boolean ("follow-state",
228 P_("Whether the rendered pixbuf should be "
229 "colorized according to the state"),
231 GTK_PARAM_READWRITE));
234 * GtkCellRendererPixbuf:gicon:
236 * The GIcon representing the icon to display.
237 * If the icon theme is changed, the image will be updated
242 g_object_class_install_property (object_class,
244 g_param_spec_object ("gicon",
246 P_("The GIcon being displayed"),
248 GTK_PARAM_READWRITE));
252 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
254 _gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_IMAGE_CELL_ACCESSIBLE);
258 gtk_cell_renderer_pixbuf_get_property (GObject *object,
263 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
264 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
269 g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
271 case PROP_PIXBUF_EXPANDER_OPEN:
272 g_value_set_object (value, priv->pixbuf_expander_open);
274 case PROP_PIXBUF_EXPANDER_CLOSED:
275 g_value_set_object (value, priv->pixbuf_expander_closed);
278 g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
280 case PROP_STOCK_SIZE:
281 g_value_set_uint (value, priv->icon_size);
283 case PROP_STOCK_DETAIL:
284 g_value_set_string (value, priv->stock_detail);
286 case PROP_FOLLOW_STATE:
287 g_value_set_boolean (value, priv->follow_state);
290 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
293 g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
302 gtk_cell_renderer_pixbuf_reset (GtkCellRendererPixbuf *cellpixbuf)
304 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
305 GtkImageType storage_type = _gtk_icon_helper_get_storage_type (priv->icon_helper);
307 switch (storage_type)
309 case GTK_IMAGE_PIXBUF:
310 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
312 case GTK_IMAGE_STOCK:
313 g_object_notify (G_OBJECT (cellpixbuf), "stock-id");
315 case GTK_IMAGE_ICON_NAME:
316 g_object_notify (G_OBJECT (cellpixbuf), "icon-name");
318 case GTK_IMAGE_GICON:
319 g_object_notify (G_OBJECT (cellpixbuf), "gicon");
321 case GTK_IMAGE_EMPTY:
326 _gtk_icon_helper_clear (priv->icon_helper);
330 gtk_cell_renderer_pixbuf_set_property (GObject *object,
335 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
336 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
341 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
342 _gtk_icon_helper_set_pixbuf (priv->icon_helper, g_value_get_object (value));
344 case PROP_PIXBUF_EXPANDER_OPEN:
345 if (priv->pixbuf_expander_open)
346 g_object_unref (priv->pixbuf_expander_open);
347 priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
349 case PROP_PIXBUF_EXPANDER_CLOSED:
350 if (priv->pixbuf_expander_closed)
351 g_object_unref (priv->pixbuf_expander_closed);
352 priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
355 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
356 _gtk_icon_helper_set_stock_id (priv->icon_helper, g_value_get_string (value), priv->icon_size);
358 case PROP_STOCK_SIZE:
359 priv->icon_size = g_value_get_uint (value);
360 _gtk_icon_helper_set_icon_size (priv->icon_helper, priv->icon_size);
362 case PROP_STOCK_DETAIL:
363 g_free (priv->stock_detail);
364 priv->stock_detail = g_value_dup_string (value);
367 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
368 _gtk_icon_helper_set_icon_name (priv->icon_helper, g_value_get_string (value), priv->icon_size);
370 case PROP_FOLLOW_STATE:
371 priv->follow_state = g_value_get_boolean (value);
374 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
375 _gtk_icon_helper_set_gicon (priv->icon_helper, g_value_get_object (value), priv->icon_size);
378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
384 * gtk_cell_renderer_pixbuf_new:
386 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
387 * parameters using object properties. Object properties can be set
388 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
389 * can bind a property to a value in a #GtkTreeModel. For example, you
390 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
391 * in the model, thus rendering a different image in each row of the
394 * Return value: the new cell renderer
397 gtk_cell_renderer_pixbuf_new (void)
399 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
403 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
405 const GdkRectangle *cell_area,
411 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
412 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
413 gint pixbuf_width = 0;
414 gint pixbuf_height = 0;
418 GtkStyleContext *context;
420 context = gtk_widget_get_style_context (widget);
421 gtk_style_context_save (context);
422 gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
424 if (!_gtk_icon_helper_get_is_empty (priv->icon_helper))
425 _gtk_icon_helper_get_size (priv->icon_helper,
426 gtk_widget_get_style_context (widget),
427 &pixbuf_width, &pixbuf_height);
429 gtk_style_context_restore (context);
431 if (priv->pixbuf_expander_open)
433 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
434 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
436 if (priv->pixbuf_expander_closed)
438 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
439 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
442 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
443 calc_width = (gint) xpad * 2 + pixbuf_width;
444 calc_height = (gint) ypad * 2 + pixbuf_height;
446 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
448 gfloat xalign, yalign;
450 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
453 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
454 (1.0 - xalign) : xalign) *
455 (cell_area->width - calc_width));
456 *x_offset = MAX (*x_offset, 0);
460 *y_offset = (yalign *
461 (cell_area->height - calc_height));
462 *y_offset = MAX (*y_offset, 0);
467 if (x_offset) *x_offset = 0;
468 if (y_offset) *y_offset = 0;
475 *height = calc_height;
479 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
482 const GdkRectangle *background_area,
483 const GdkRectangle *cell_area,
484 GtkCellRendererState flags)
487 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
488 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
489 GtkStyleContext *context;
490 GdkRectangle pix_rect;
491 GdkRectangle draw_rect;
492 gboolean is_expander;
495 GtkIconHelper *icon_helper = NULL;
497 gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
503 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
504 pix_rect.x += cell_area->x + xpad;
505 pix_rect.y += cell_area->y + ypad;
506 pix_rect.width -= xpad * 2;
507 pix_rect.height -= ypad * 2;
509 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
512 context = gtk_widget_get_style_context (widget);
513 gtk_style_context_save (context);
515 state = GTK_STATE_FLAG_NORMAL;
517 if (!gtk_widget_get_sensitive (widget) ||
518 !gtk_cell_renderer_get_sensitive (cell))
519 state |= GTK_STATE_FLAG_INSENSITIVE;
520 else if (priv->follow_state &&
521 (flags & (GTK_CELL_RENDERER_SELECTED |
522 GTK_CELL_RENDERER_PRELIT)) != 0)
523 state = gtk_cell_renderer_get_state (cell, widget, flags);
525 gtk_style_context_set_state (context, state);
526 gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
528 g_object_get (cell, "is-expander", &is_expander, NULL);
531 gboolean is_expanded;
533 g_object_get (cell, "is-expanded", &is_expanded, NULL);
535 if (is_expanded && priv->pixbuf_expander_open != NULL)
537 icon_helper = _gtk_icon_helper_new ();
538 _gtk_icon_helper_set_pixbuf (icon_helper, priv->pixbuf_expander_open);
540 else if (!is_expanded && priv->pixbuf_expander_closed != NULL)
542 icon_helper = _gtk_icon_helper_new ();
543 _gtk_icon_helper_set_pixbuf (icon_helper, priv->pixbuf_expander_closed);
547 if (icon_helper == NULL)
548 icon_helper = g_object_ref (priv->icon_helper);
550 _gtk_icon_helper_draw (icon_helper,
552 pix_rect.x, pix_rect.y);
553 g_object_unref (icon_helper);
555 gtk_style_context_restore (context);