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 "gtkiconhelperprivate.h"
25 #include "gtkicontheme.h"
27 #include "gtkprivate.h"
28 #include "a11y/gtkimagecellaccessible.h"
32 * SECTION:gtkcellrendererpixbuf
33 * @Short_description: Renders a pixbuf in a cell
34 * @Title: GtkCellRendererPixbuf
36 * A #GtkCellRendererPixbuf can be used to render an image in a cell. It allows
37 * to render either a given #GdkPixbuf (set via the
38 * #GtkCellRendererPixbuf:pixbuf property) or a stock icon (set via the
39 * #GtkCellRendererPixbuf:stock-id property).
41 * To support the tree view, #GtkCellRendererPixbuf also supports rendering two
42 * alternative pixbufs, when the #GtkCellRenderer:is-expander property is %TRUE.
43 * If the #GtkCellRenderer:is-expanded property is %TRUE and the
44 * #GtkCellRendererPixbuf:pixbuf-expander-open property is set to a pixbuf, it
45 * renders that pixbuf, if the #GtkCellRenderer:is-expanded property is %FALSE
46 * and the #GtkCellRendererPixbuf:pixbuf-expander-closed property is set to a
47 * pixbuf, it renders that one.
51 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
55 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
59 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
61 const GdkRectangle *rectangle,
66 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
69 const GdkRectangle *background_area,
70 const GdkRectangle *cell_area,
71 GtkCellRendererState flags);
77 PROP_PIXBUF_EXPANDER_OPEN,
78 PROP_PIXBUF_EXPANDER_CLOSED,
88 struct _GtkCellRendererPixbufPrivate
90 GtkIconHelper *icon_helper;
91 GtkIconSize icon_size;
93 GdkPixbuf *pixbuf_expander_open;
94 GdkPixbuf *pixbuf_expander_closed;
96 gboolean follow_state;
102 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
106 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
108 GtkCellRendererPixbufPrivate *priv;
110 cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
111 GTK_TYPE_CELL_RENDERER_PIXBUF,
112 GtkCellRendererPixbufPrivate);
113 priv = cellpixbuf->priv;
114 priv->icon_helper = _gtk_icon_helper_new ();
115 priv->icon_size = GTK_ICON_SIZE_MENU;
119 gtk_cell_renderer_pixbuf_finalize (GObject *object)
121 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
122 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
124 g_clear_object (&priv->icon_helper);
126 if (priv->pixbuf_expander_open)
127 g_object_unref (priv->pixbuf_expander_open);
128 if (priv->pixbuf_expander_closed)
129 g_object_unref (priv->pixbuf_expander_closed);
131 g_free (priv->stock_detail);
133 G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
137 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
139 GObjectClass *object_class = G_OBJECT_CLASS (class);
140 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
142 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
144 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
145 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
147 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
148 cell_class->render = gtk_cell_renderer_pixbuf_render;
150 g_object_class_install_property (object_class,
152 g_param_spec_object ("pixbuf",
154 P_("The pixbuf to render"),
156 GTK_PARAM_READWRITE));
158 g_object_class_install_property (object_class,
159 PROP_PIXBUF_EXPANDER_OPEN,
160 g_param_spec_object ("pixbuf-expander-open",
161 P_("Pixbuf Expander Open"),
162 P_("Pixbuf for open expander"),
164 GTK_PARAM_READWRITE));
166 g_object_class_install_property (object_class,
167 PROP_PIXBUF_EXPANDER_CLOSED,
168 g_param_spec_object ("pixbuf-expander-closed",
169 P_("Pixbuf Expander Closed"),
170 P_("Pixbuf for closed expander"),
172 GTK_PARAM_READWRITE));
174 g_object_class_install_property (object_class,
176 g_param_spec_string ("stock-id",
178 P_("The stock ID of the stock icon to render"),
180 GTK_PARAM_READWRITE));
182 g_object_class_install_property (object_class,
184 g_param_spec_uint ("stock-size",
186 P_("The GtkIconSize value that specifies the size of the rendered icon"),
190 GTK_PARAM_READWRITE));
192 g_object_class_install_property (object_class,
194 g_param_spec_string ("stock-detail",
196 P_("Render detail to pass to the theme engine"),
198 GTK_PARAM_READWRITE));
202 * GtkCellRendererPixbuf:icon-name:
204 * The name of the themed icon to display.
205 * This property only has an effect if not overridden by "stock_id"
206 * or "pixbuf" properties.
210 g_object_class_install_property (object_class,
212 g_param_spec_string ("icon-name",
214 P_("The name of the icon from the icon theme"),
216 GTK_PARAM_READWRITE));
219 * GtkCellRendererPixbuf:follow-state:
221 * Specifies whether the rendered pixbuf should be colorized
222 * according to the #GtkCellRendererState.
226 g_object_class_install_property (object_class,
228 g_param_spec_boolean ("follow-state",
230 P_("Whether the rendered pixbuf should be "
231 "colorized according to the state"),
233 GTK_PARAM_READWRITE));
236 * GtkCellRendererPixbuf:gicon:
238 * The GIcon representing the icon to display.
239 * If the icon theme is changed, the image will be updated
244 g_object_class_install_property (object_class,
246 g_param_spec_object ("gicon",
248 P_("The GIcon being displayed"),
250 GTK_PARAM_READWRITE));
254 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
256 _gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_IMAGE_CELL_ACCESSIBLE);
260 gtk_cell_renderer_pixbuf_get_property (GObject *object,
265 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
266 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
271 g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
273 case PROP_PIXBUF_EXPANDER_OPEN:
274 g_value_set_object (value, priv->pixbuf_expander_open);
276 case PROP_PIXBUF_EXPANDER_CLOSED:
277 g_value_set_object (value, priv->pixbuf_expander_closed);
280 g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
282 case PROP_STOCK_SIZE:
283 g_value_set_uint (value, priv->icon_size);
285 case PROP_STOCK_DETAIL:
286 g_value_set_string (value, priv->stock_detail);
288 case PROP_FOLLOW_STATE:
289 g_value_set_boolean (value, priv->follow_state);
292 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
295 g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
304 gtk_cell_renderer_pixbuf_reset (GtkCellRendererPixbuf *cellpixbuf)
306 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
307 GtkImageType storage_type = _gtk_icon_helper_get_storage_type (priv->icon_helper);
309 switch (storage_type)
311 case GTK_IMAGE_PIXBUF:
312 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
314 case GTK_IMAGE_STOCK:
315 g_object_notify (G_OBJECT (cellpixbuf), "stock-id");
317 case GTK_IMAGE_ICON_NAME:
318 g_object_notify (G_OBJECT (cellpixbuf), "icon-name");
320 case GTK_IMAGE_GICON:
321 g_object_notify (G_OBJECT (cellpixbuf), "gicon");
323 case GTK_IMAGE_EMPTY:
328 _gtk_icon_helper_clear (priv->icon_helper);
332 gtk_cell_renderer_pixbuf_set_property (GObject *object,
337 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
338 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
343 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
344 _gtk_icon_helper_set_pixbuf (priv->icon_helper, g_value_get_object (value));
346 case PROP_PIXBUF_EXPANDER_OPEN:
347 if (priv->pixbuf_expander_open)
348 g_object_unref (priv->pixbuf_expander_open);
349 priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
351 case PROP_PIXBUF_EXPANDER_CLOSED:
352 if (priv->pixbuf_expander_closed)
353 g_object_unref (priv->pixbuf_expander_closed);
354 priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
357 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
358 _gtk_icon_helper_set_stock_id (priv->icon_helper, g_value_get_string (value), priv->icon_size);
360 case PROP_STOCK_SIZE:
361 priv->icon_size = g_value_get_uint (value);
362 _gtk_icon_helper_set_icon_size (priv->icon_helper, priv->icon_size);
364 case PROP_STOCK_DETAIL:
365 g_free (priv->stock_detail);
366 priv->stock_detail = g_value_dup_string (value);
369 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
370 _gtk_icon_helper_set_icon_name (priv->icon_helper, g_value_get_string (value), priv->icon_size);
372 case PROP_FOLLOW_STATE:
373 priv->follow_state = g_value_get_boolean (value);
376 gtk_cell_renderer_pixbuf_reset (cellpixbuf);
377 _gtk_icon_helper_set_gicon (priv->icon_helper, g_value_get_object (value), priv->icon_size);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
386 * gtk_cell_renderer_pixbuf_new:
388 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
389 * parameters using object properties. Object properties can be set
390 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
391 * can bind a property to a value in a #GtkTreeModel. For example, you
392 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
393 * in the model, thus rendering a different image in each row of the
396 * Return value: the new cell renderer
399 gtk_cell_renderer_pixbuf_new (void)
401 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
405 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
407 const GdkRectangle *cell_area,
413 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
414 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
415 gint pixbuf_width = 0;
416 gint pixbuf_height = 0;
421 if (!_gtk_icon_helper_get_is_empty (priv->icon_helper))
422 _gtk_icon_helper_get_size (priv->icon_helper,
423 gtk_widget_get_style_context (widget),
424 &pixbuf_width, &pixbuf_height);
426 if (priv->pixbuf_expander_open)
428 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
429 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
431 if (priv->pixbuf_expander_closed)
433 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
434 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
437 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
438 calc_width = (gint) xpad * 2 + pixbuf_width;
439 calc_height = (gint) ypad * 2 + pixbuf_height;
441 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
443 gfloat xalign, yalign;
445 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
448 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
449 (1.0 - xalign) : xalign) *
450 (cell_area->width - calc_width));
451 *x_offset = MAX (*x_offset, 0);
455 *y_offset = (yalign *
456 (cell_area->height - calc_height));
457 *y_offset = MAX (*y_offset, 0);
462 if (x_offset) *x_offset = 0;
463 if (y_offset) *y_offset = 0;
470 *height = calc_height;
474 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
477 const GdkRectangle *background_area,
478 const GdkRectangle *cell_area,
479 GtkCellRendererState flags)
482 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
483 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
484 GtkStyleContext *context;
485 GdkRectangle pix_rect;
486 GdkRectangle draw_rect;
487 gboolean is_expander;
490 GtkIconHelper *icon_helper = NULL;
492 gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
498 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
499 pix_rect.x += cell_area->x + xpad;
500 pix_rect.y += cell_area->y + ypad;
501 pix_rect.width -= xpad * 2;
502 pix_rect.height -= ypad * 2;
504 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
507 context = gtk_widget_get_style_context (widget);
508 gtk_style_context_save (context);
510 state = GTK_STATE_FLAG_NORMAL;
512 if (!gtk_widget_get_sensitive (widget) ||
513 !gtk_cell_renderer_get_sensitive (cell))
514 state |= GTK_STATE_FLAG_INSENSITIVE;
515 else if (priv->follow_state &&
516 (flags & (GTK_CELL_RENDERER_SELECTED |
517 GTK_CELL_RENDERER_PRELIT)) != 0)
518 state = gtk_cell_renderer_get_state (cell, widget, flags);
520 gtk_style_context_set_state (context, state);
522 g_object_get (cell, "is-expander", &is_expander, NULL);
525 gboolean is_expanded;
527 g_object_get (cell, "is-expanded", &is_expanded, NULL);
529 if (is_expanded && priv->pixbuf_expander_open != NULL)
531 icon_helper = _gtk_icon_helper_new ();
532 _gtk_icon_helper_set_pixbuf (icon_helper, priv->pixbuf_expander_open);
534 else if (!is_expanded && priv->pixbuf_expander_closed != NULL)
536 icon_helper = _gtk_icon_helper_new ();
537 _gtk_icon_helper_set_pixbuf (icon_helper, priv->pixbuf_expander_closed);
541 if (icon_helper == NULL)
542 icon_helper = g_object_ref (priv->icon_helper);
544 _gtk_icon_helper_draw (icon_helper,
546 pix_rect.x, pix_rect.y);
547 g_object_unref (icon_helper);
549 gtk_style_context_restore (context);