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"
29 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
33 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
37 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
38 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
40 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
42 GdkRectangle *rectangle,
47 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
50 GdkRectangle *background_area,
51 GdkRectangle *cell_area,
52 GdkRectangle *expose_area,
53 GtkCellRendererState flags);
59 PROP_PIXBUF_EXPANDER_OPEN,
60 PROP_PIXBUF_EXPANDER_CLOSED,
70 struct _GtkCellRendererPixbufPriv
72 GtkIconSize stock_size;
75 GdkPixbuf *pixbuf_expander_open;
76 GdkPixbuf *pixbuf_expander_closed;
80 gboolean follow_state;
88 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
92 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
94 GtkCellRendererPixbufPriv *priv;
96 cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
97 GTK_TYPE_CELL_RENDERER_PIXBUF,
98 GtkCellRendererPixbufPriv);
99 priv = cellpixbuf->priv;
101 priv->stock_size = GTK_ICON_SIZE_MENU;
105 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
107 GObjectClass *object_class = G_OBJECT_CLASS (class);
108 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
110 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
112 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
113 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
115 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
116 cell_class->render = gtk_cell_renderer_pixbuf_render;
118 g_object_class_install_property (object_class,
120 g_param_spec_object ("pixbuf",
122 P_("The pixbuf to render"),
124 GTK_PARAM_READWRITE));
126 g_object_class_install_property (object_class,
127 PROP_PIXBUF_EXPANDER_OPEN,
128 g_param_spec_object ("pixbuf-expander-open",
129 P_("Pixbuf Expander Open"),
130 P_("Pixbuf for open expander"),
132 GTK_PARAM_READWRITE));
134 g_object_class_install_property (object_class,
135 PROP_PIXBUF_EXPANDER_CLOSED,
136 g_param_spec_object ("pixbuf-expander-closed",
137 P_("Pixbuf Expander Closed"),
138 P_("Pixbuf for closed expander"),
140 GTK_PARAM_READWRITE));
142 g_object_class_install_property (object_class,
144 g_param_spec_string ("stock-id",
146 P_("The stock ID of the stock icon to render"),
148 GTK_PARAM_READWRITE));
150 g_object_class_install_property (object_class,
152 g_param_spec_uint ("stock-size",
154 P_("The GtkIconSize value that specifies the size of the rendered icon"),
158 GTK_PARAM_READWRITE));
160 g_object_class_install_property (object_class,
162 g_param_spec_string ("stock-detail",
164 P_("Render detail to pass to the theme engine"),
166 GTK_PARAM_READWRITE));
170 * GtkCellRendererPixbuf:icon-name:
172 * The name of the themed icon to display.
173 * This property only has an effect if not overridden by "stock_id"
174 * or "pixbuf" properties.
178 g_object_class_install_property (object_class,
180 g_param_spec_string ("icon-name",
182 P_("The name of the icon from the icon theme"),
184 GTK_PARAM_READWRITE));
187 * GtkCellRendererPixbuf:follow-state:
189 * Specifies whether the rendered pixbuf should be colorized
190 * according to the #GtkCellRendererState.
194 g_object_class_install_property (object_class,
196 g_param_spec_boolean ("follow-state",
198 P_("Whether the rendered pixbuf should be "
199 "colorized according to the state"),
201 GTK_PARAM_READWRITE));
204 * GtkCellRendererPixbuf:gicon:
206 * The GIcon representing the icon to display.
207 * If the icon theme is changed, the image will be updated
212 g_object_class_install_property (object_class,
214 g_param_spec_object ("gicon",
216 P_("The GIcon being displayed"),
218 GTK_PARAM_READWRITE));
222 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPriv));
226 gtk_cell_renderer_pixbuf_finalize (GObject *object)
228 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
229 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
232 g_object_unref (priv->pixbuf);
233 if (priv->pixbuf_expander_open)
234 g_object_unref (priv->pixbuf_expander_open);
235 if (priv->pixbuf_expander_closed)
236 g_object_unref (priv->pixbuf_expander_closed);
238 g_free (priv->stock_id);
239 g_free (priv->stock_detail);
240 g_free (priv->icon_name);
243 g_object_unref (priv->gicon);
245 G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
249 gtk_cell_renderer_pixbuf_get_property (GObject *object,
254 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
255 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
260 g_value_set_object (value, priv->pixbuf);
262 case PROP_PIXBUF_EXPANDER_OPEN:
263 g_value_set_object (value, priv->pixbuf_expander_open);
265 case PROP_PIXBUF_EXPANDER_CLOSED:
266 g_value_set_object (value, priv->pixbuf_expander_closed);
269 g_value_set_string (value, priv->stock_id);
271 case PROP_STOCK_SIZE:
272 g_value_set_uint (value, priv->stock_size);
274 case PROP_STOCK_DETAIL:
275 g_value_set_string (value, priv->stock_detail);
277 case PROP_FOLLOW_STATE:
278 g_value_set_boolean (value, priv->follow_state);
281 g_value_set_string (value, priv->icon_name);
284 g_value_set_object (value, priv->gicon);
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
293 gtk_cell_renderer_pixbuf_set_property (GObject *object,
298 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
299 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
305 g_object_unref (priv->pixbuf);
306 priv->pixbuf = (GdkPixbuf*) g_value_dup_object (value);
311 g_free (priv->stock_id);
312 priv->stock_id = NULL;
313 g_object_notify (object, "stock-id");
317 g_free (priv->icon_name);
318 priv->icon_name = NULL;
319 g_object_notify (object, "icon-name");
323 g_object_unref (priv->gicon);
325 g_object_notify (object, "gicon");
329 case PROP_PIXBUF_EXPANDER_OPEN:
330 if (priv->pixbuf_expander_open)
331 g_object_unref (priv->pixbuf_expander_open);
332 priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
334 case PROP_PIXBUF_EXPANDER_CLOSED:
335 if (priv->pixbuf_expander_closed)
336 g_object_unref (priv->pixbuf_expander_closed);
337 priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
344 g_object_unref (priv->pixbuf);
346 g_object_notify (object, "pixbuf");
348 g_free (priv->stock_id);
350 priv->stock_id = g_value_dup_string (value);
355 g_object_unref (priv->pixbuf);
357 g_object_notify (object, "pixbuf");
361 g_free (priv->icon_name);
362 priv->icon_name = NULL;
363 g_object_notify (object, "icon-name");
367 g_object_unref (priv->gicon);
369 g_object_notify (object, "gicon");
373 case PROP_STOCK_SIZE:
374 priv->stock_size = g_value_get_uint (value);
376 case PROP_STOCK_DETAIL:
377 g_free (priv->stock_detail);
378 priv->stock_detail = g_value_dup_string (value);
385 g_object_unref (priv->pixbuf);
387 g_object_notify (object, "pixbuf");
389 g_free (priv->icon_name);
391 priv->icon_name = g_value_dup_string (value);
396 g_object_unref (priv->pixbuf);
398 g_object_notify (object, "pixbuf");
402 g_free (priv->stock_id);
403 priv->stock_id = NULL;
404 g_object_notify (object, "stock-id");
408 g_object_unref (priv->gicon);
410 g_object_notify (object, "gicon");
414 case PROP_FOLLOW_STATE:
415 priv->follow_state = g_value_get_boolean (value);
422 g_object_unref (priv->pixbuf);
424 g_object_notify (object, "pixbuf");
426 g_object_unref (priv->gicon);
428 priv->gicon = (GIcon *) g_value_dup_object (value);
433 g_object_unref (priv->pixbuf);
435 g_object_notify (object, "pixbuf");
439 g_free (priv->stock_id);
440 priv->stock_id = NULL;
441 g_object_notify (object, "stock-id");
445 g_free (priv->icon_name);
446 priv->icon_name = NULL;
447 g_object_notify (object, "icon-name");
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
458 * gtk_cell_renderer_pixbuf_new:
460 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
461 * parameters using object properties. Object properties can be set
462 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
463 * can bind a property to a value in a #GtkTreeModel. For example, you
464 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
465 * in the model, thus rendering a different image in each row of the
468 * Return value: the new cell renderer
471 gtk_cell_renderer_pixbuf_new (void)
473 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
477 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
480 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
483 g_object_unref (priv->pixbuf);
485 priv->pixbuf = gtk_widget_render_icon (widget,
490 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
494 gtk_cell_renderer_pixbuf_create_themed_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
497 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
499 GtkIconTheme *icon_theme;
500 GtkSettings *settings;
506 g_object_unref (priv->pixbuf);
510 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
511 icon_theme = gtk_icon_theme_get_for_screen (screen);
512 settings = gtk_settings_get_for_screen (screen);
514 if (!gtk_icon_size_lookup_for_settings (settings,
518 g_warning ("Invalid icon size %u\n", priv->stock_size);
523 info = gtk_icon_theme_lookup_icon (icon_theme,
526 GTK_ICON_LOOKUP_USE_BUILTIN);
527 else if (priv->gicon)
528 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
531 GTK_ICON_LOOKUP_USE_BUILTIN);
539 style = gtk_widget_get_style (GTK_WIDGET (widget));
540 priv->pixbuf = gtk_icon_info_load_symbolic_for_style (info,
545 gtk_icon_info_free (info);
548 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
552 create_symbolic_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
556 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
558 GtkIconTheme *icon_theme;
559 GtkSettings *settings;
564 /* Not a named symbolic icon? */
565 if (priv->icon_name) {
566 if (!g_str_has_suffix (priv->icon_name, "-symbolic"))
568 } else if (priv->gicon) {
569 const gchar * const *names;
570 if (!G_IS_THEMED_ICON (priv->gicon))
572 names = g_themed_icon_get_names (G_THEMED_ICON (priv->gicon));
573 if (names == NULL || !g_str_has_suffix (names[0], "-symbolic"))
579 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
580 icon_theme = gtk_icon_theme_get_for_screen (screen);
581 settings = gtk_settings_get_for_screen (screen);
583 if (!gtk_icon_size_lookup_for_settings (settings,
587 g_warning ("Invalid icon size %u\n", priv->stock_size);
593 info = gtk_icon_theme_lookup_icon (icon_theme,
596 GTK_ICON_LOOKUP_USE_BUILTIN);
597 else if (priv->gicon)
598 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
601 GTK_ICON_LOOKUP_USE_BUILTIN);
609 style = gtk_widget_get_style (GTK_WIDGET (widget));
610 pixbuf = gtk_icon_info_load_symbolic_for_style (info,
613 gtk_icon_info_free (info);
621 create_colorized_pixbuf (GdkPixbuf *src,
625 gint width, height, has_alpha, src_row_stride, dst_row_stride;
626 gint red_value, green_value, blue_value;
627 guchar *target_pixels;
628 guchar *original_pixels;
633 red_value = new_color->red / 255.0;
634 green_value = new_color->green / 255.0;
635 blue_value = new_color->blue / 255.0;
637 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
638 gdk_pixbuf_get_has_alpha (src),
639 gdk_pixbuf_get_bits_per_sample (src),
640 gdk_pixbuf_get_width (src),
641 gdk_pixbuf_get_height (src));
643 has_alpha = gdk_pixbuf_get_has_alpha (src);
644 width = gdk_pixbuf_get_width (src);
645 height = gdk_pixbuf_get_height (src);
646 src_row_stride = gdk_pixbuf_get_rowstride (src);
647 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
648 target_pixels = gdk_pixbuf_get_pixels (dest);
649 original_pixels = gdk_pixbuf_get_pixels (src);
651 for (i = 0; i < height; i++) {
652 pixdest = target_pixels + i*dst_row_stride;
653 pixsrc = original_pixels + i*src_row_stride;
654 for (j = 0; j < width; j++) {
655 *pixdest++ = (*pixsrc++ * red_value) >> 8;
656 *pixdest++ = (*pixsrc++ * green_value) >> 8;
657 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
659 *pixdest++ = *pixsrc++;
668 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
670 GdkRectangle *cell_area,
676 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
677 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
678 gint pixbuf_width = 0;
679 gint pixbuf_height = 0;
687 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
688 else if (priv->icon_name || priv->gicon)
689 gtk_cell_renderer_pixbuf_create_themed_pixbuf (cellpixbuf, widget);
694 pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
695 pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
697 if (priv->pixbuf_expander_open)
699 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
700 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
702 if (priv->pixbuf_expander_closed)
704 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
705 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
708 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
709 calc_width = (gint) xpad * 2 + pixbuf_width;
710 calc_height = (gint) ypad * 2 + pixbuf_height;
712 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
714 gfloat xalign, yalign;
716 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
719 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
720 (1.0 - xalign) : xalign) *
721 (cell_area->width - calc_width));
722 *x_offset = MAX (*x_offset, 0);
726 *y_offset = (yalign *
727 (cell_area->height - calc_height));
728 *y_offset = MAX (*y_offset, 0);
733 if (x_offset) *x_offset = 0;
734 if (y_offset) *y_offset = 0;
741 *height = calc_height;
745 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
748 GdkRectangle *background_area,
749 GdkRectangle *cell_area,
750 GdkRectangle *expose_area,
751 GtkCellRendererState flags)
754 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
755 GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
757 GdkPixbuf *invisible = NULL;
758 GdkPixbuf *colorized = NULL;
759 GdkPixbuf *symbolic = NULL;
760 GdkRectangle pix_rect;
761 GdkRectangle draw_rect;
763 gboolean is_expander;
766 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
772 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
773 pix_rect.x += cell_area->x + xpad;
774 pix_rect.y += cell_area->y + ypad;
775 pix_rect.width -= xpad * 2;
776 pix_rect.height -= ypad * 2;
778 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
779 !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
782 pixbuf = priv->pixbuf;
784 g_object_get (cell, "is-expander", &is_expander, NULL);
787 gboolean is_expanded;
789 g_object_get (cell, "is-expanded", &is_expanded, NULL);
792 priv->pixbuf_expander_open != NULL)
793 pixbuf = priv->pixbuf_expander_open;
794 else if (!is_expanded &&
795 priv->pixbuf_expander_closed != NULL)
796 pixbuf = priv->pixbuf_expander_closed;
802 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
803 !gtk_cell_renderer_get_sensitive (cell))
805 GtkIconSource *source;
807 source = gtk_icon_source_new ();
808 gtk_icon_source_set_pixbuf (source, pixbuf);
809 /* The size here is arbitrary; since size isn't
810 * wildcarded in the source, it isn't supposed to be
811 * scaled by the engine function
813 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
814 gtk_icon_source_set_size_wildcarded (source, FALSE);
816 invisible = gtk_style_render_icon (widget->style,
818 gtk_widget_get_direction (widget),
819 GTK_STATE_INSENSITIVE,
823 "gtkcellrendererpixbuf");
825 gtk_icon_source_free (source);
829 else if (priv->follow_state &&
830 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
834 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
836 if (gtk_widget_has_focus (widget))
837 state = GTK_STATE_SELECTED;
839 state = GTK_STATE_ACTIVE;
842 state = GTK_STATE_PRELIGHT;
844 symbolic = create_symbolic_pixbuf (cellpixbuf, widget, state);
846 colorized = create_colorized_pixbuf (pixbuf,
847 &widget->style->base[state]);
855 cr = gdk_cairo_create (window);
857 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
858 gdk_cairo_rectangle (cr, &draw_rect);
864 g_object_unref (invisible);
867 g_object_unref (colorized);
870 g_object_unref (symbolic);