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
91 GtkIconSize stock_size;
94 GdkPixbuf *pixbuf_expander_open;
95 GdkPixbuf *pixbuf_expander_closed;
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;
581 /* Not a named symbolic icon? */
582 if (priv->icon_name) {
583 if (!g_str_has_suffix (priv->icon_name, "-symbolic"))
585 } else if (priv->gicon) {
586 const gchar * const *names;
587 if (!G_IS_THEMED_ICON (priv->gicon))
589 names = g_themed_icon_get_names (G_THEMED_ICON (priv->gicon));
590 if (names == NULL || !g_str_has_suffix (names[0], "-symbolic"))
596 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
597 icon_theme = gtk_icon_theme_get_for_screen (screen);
598 settings = gtk_settings_get_for_screen (screen);
600 if (!gtk_icon_size_lookup_for_settings (settings,
604 g_warning ("Invalid icon size %u\n", priv->stock_size);
610 info = gtk_icon_theme_lookup_icon (icon_theme,
613 GTK_ICON_LOOKUP_USE_BUILTIN);
614 else if (priv->gicon)
615 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
618 GTK_ICON_LOOKUP_USE_BUILTIN);
624 GtkStyleContext *context;
626 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
628 gtk_style_context_save (context);
629 gtk_style_context_set_state (context, state);
630 pixbuf = gtk_icon_info_load_symbolic_for_context (info,
635 gtk_style_context_restore (context);
636 gtk_icon_info_free (info);
645 create_colorized_pixbuf (GdkPixbuf *src,
649 gint width, height, has_alpha, src_row_stride, dst_row_stride;
650 gint red_value, green_value, blue_value;
651 guchar *target_pixels;
652 guchar *original_pixels;
657 red_value = (new_color->red * 65535.0) / 255.0;
658 green_value = (new_color->green * 65535.0) / 255.0;
659 blue_value = (new_color->blue * 65535.0) / 255.0;
661 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
662 gdk_pixbuf_get_has_alpha (src),
663 gdk_pixbuf_get_bits_per_sample (src),
664 gdk_pixbuf_get_width (src),
665 gdk_pixbuf_get_height (src));
667 has_alpha = gdk_pixbuf_get_has_alpha (src);
668 width = gdk_pixbuf_get_width (src);
669 height = gdk_pixbuf_get_height (src);
670 src_row_stride = gdk_pixbuf_get_rowstride (src);
671 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
672 target_pixels = gdk_pixbuf_get_pixels (dest);
673 original_pixels = gdk_pixbuf_get_pixels (src);
675 for (i = 0; i < height; i++) {
676 pixdest = target_pixels + i*dst_row_stride;
677 pixsrc = original_pixels + i*src_row_stride;
678 for (j = 0; j < width; j++) {
679 *pixdest++ = (*pixsrc++ * red_value) >> 8;
680 *pixdest++ = (*pixsrc++ * green_value) >> 8;
681 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
683 *pixdest++ = *pixsrc++;
692 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
694 const GdkRectangle *cell_area,
700 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
701 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
702 gint pixbuf_width = 0;
703 gint pixbuf_height = 0;
711 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
712 else if (priv->icon_name || priv->gicon)
713 gtk_cell_renderer_pixbuf_create_themed_pixbuf (cellpixbuf, widget);
718 pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
719 pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
721 if (priv->pixbuf_expander_open)
723 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
724 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
726 if (priv->pixbuf_expander_closed)
728 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
729 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
732 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
733 calc_width = (gint) xpad * 2 + pixbuf_width;
734 calc_height = (gint) ypad * 2 + pixbuf_height;
736 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
738 gfloat xalign, yalign;
740 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
743 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
744 (1.0 - xalign) : xalign) *
745 (cell_area->width - calc_width));
746 *x_offset = MAX (*x_offset, 0);
750 *y_offset = (yalign *
751 (cell_area->height - calc_height));
752 *y_offset = MAX (*y_offset, 0);
757 if (x_offset) *x_offset = 0;
758 if (y_offset) *y_offset = 0;
765 *height = calc_height;
769 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
772 const GdkRectangle *background_area,
773 const GdkRectangle *cell_area,
774 GtkCellRendererState flags)
777 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
778 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
779 GtkStyleContext *context;
781 GdkPixbuf *invisible = NULL;
782 GdkPixbuf *colorized = NULL;
783 GdkPixbuf *symbolic = NULL;
784 GdkRectangle pix_rect;
785 GdkRectangle draw_rect;
786 gboolean is_expander;
789 gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
795 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
796 pix_rect.x += cell_area->x + xpad;
797 pix_rect.y += cell_area->y + ypad;
798 pix_rect.width -= xpad * 2;
799 pix_rect.height -= ypad * 2;
801 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
804 pixbuf = priv->pixbuf;
806 g_object_get (cell, "is-expander", &is_expander, NULL);
809 gboolean is_expanded;
811 g_object_get (cell, "is-expanded", &is_expanded, NULL);
814 priv->pixbuf_expander_open != NULL)
815 pixbuf = priv->pixbuf_expander_open;
816 else if (!is_expanded &&
817 priv->pixbuf_expander_closed != NULL)
818 pixbuf = priv->pixbuf_expander_closed;
824 context = gtk_widget_get_style_context (widget);
826 if (!gtk_widget_get_sensitive (widget) ||
827 !gtk_cell_renderer_get_sensitive (cell))
829 GtkIconSource *source;
831 source = gtk_icon_source_new ();
832 gtk_icon_source_set_pixbuf (source, pixbuf);
833 /* The size here is arbitrary; since size isn't
834 * wildcarded in the source, it isn't supposed to be
835 * scaled by the engine function
837 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
838 gtk_icon_source_set_size_wildcarded (source, FALSE);
840 gtk_style_context_save (context);
841 gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
843 pixbuf = invisible = gtk_render_icon_pixbuf (context, source,
846 gtk_style_context_restore (context);
847 gtk_icon_source_free (source);
849 else if (priv->follow_state &&
850 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
854 state = gtk_cell_renderer_get_state (cell, widget, flags);
855 symbolic = create_symbolic_pixbuf (cellpixbuf, widget, state);
861 gtk_style_context_get_background_color (context, state, &color);
862 pixbuf = colorized = create_colorized_pixbuf (pixbuf, &color);
868 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
869 gdk_cairo_rectangle (cr, &draw_rect);
873 g_object_unref (invisible);
876 g_object_unref (colorized);
879 g_object_unref (symbolic);