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 const GdkRectangle *rectangle,
47 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
50 const GdkRectangle *background_area,
51 const GdkRectangle *cell_area,
52 GtkCellRendererState flags);
58 PROP_PIXBUF_EXPANDER_OPEN,
59 PROP_PIXBUF_EXPANDER_CLOSED,
69 struct _GtkCellRendererPixbufPrivate
71 GtkIconSize stock_size;
74 GdkPixbuf *pixbuf_expander_open;
75 GdkPixbuf *pixbuf_expander_closed;
79 gboolean follow_state;
87 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
91 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
93 GtkCellRendererPixbufPrivate *priv;
95 cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
96 GTK_TYPE_CELL_RENDERER_PIXBUF,
97 GtkCellRendererPixbufPrivate);
98 priv = cellpixbuf->priv;
100 priv->stock_size = GTK_ICON_SIZE_MENU;
104 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
106 GObjectClass *object_class = G_OBJECT_CLASS (class);
107 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
109 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
111 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
112 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
114 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
115 cell_class->render = gtk_cell_renderer_pixbuf_render;
117 g_object_class_install_property (object_class,
119 g_param_spec_object ("pixbuf",
121 P_("The pixbuf to render"),
123 GTK_PARAM_READWRITE));
125 g_object_class_install_property (object_class,
126 PROP_PIXBUF_EXPANDER_OPEN,
127 g_param_spec_object ("pixbuf-expander-open",
128 P_("Pixbuf Expander Open"),
129 P_("Pixbuf for open expander"),
131 GTK_PARAM_READWRITE));
133 g_object_class_install_property (object_class,
134 PROP_PIXBUF_EXPANDER_CLOSED,
135 g_param_spec_object ("pixbuf-expander-closed",
136 P_("Pixbuf Expander Closed"),
137 P_("Pixbuf for closed expander"),
139 GTK_PARAM_READWRITE));
141 g_object_class_install_property (object_class,
143 g_param_spec_string ("stock-id",
145 P_("The stock ID of the stock icon to render"),
147 GTK_PARAM_READWRITE));
149 g_object_class_install_property (object_class,
151 g_param_spec_uint ("stock-size",
153 P_("The GtkIconSize value that specifies the size of the rendered icon"),
157 GTK_PARAM_READWRITE));
159 g_object_class_install_property (object_class,
161 g_param_spec_string ("stock-detail",
163 P_("Render detail to pass to the theme engine"),
165 GTK_PARAM_READWRITE));
169 * GtkCellRendererPixbuf:icon-name:
171 * The name of the themed icon to display.
172 * This property only has an effect if not overridden by "stock_id"
173 * or "pixbuf" properties.
177 g_object_class_install_property (object_class,
179 g_param_spec_string ("icon-name",
181 P_("The name of the icon from the icon theme"),
183 GTK_PARAM_READWRITE));
186 * GtkCellRendererPixbuf:follow-state:
188 * Specifies whether the rendered pixbuf should be colorized
189 * according to the #GtkCellRendererState.
193 g_object_class_install_property (object_class,
195 g_param_spec_boolean ("follow-state",
197 P_("Whether the rendered pixbuf should be "
198 "colorized according to the state"),
200 GTK_PARAM_READWRITE));
203 * GtkCellRendererPixbuf:gicon:
205 * The GIcon representing the icon to display.
206 * If the icon theme is changed, the image will be updated
211 g_object_class_install_property (object_class,
213 g_param_spec_object ("gicon",
215 P_("The GIcon being displayed"),
217 GTK_PARAM_READWRITE));
221 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
225 gtk_cell_renderer_pixbuf_finalize (GObject *object)
227 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
228 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
231 g_object_unref (priv->pixbuf);
232 if (priv->pixbuf_expander_open)
233 g_object_unref (priv->pixbuf_expander_open);
234 if (priv->pixbuf_expander_closed)
235 g_object_unref (priv->pixbuf_expander_closed);
237 g_free (priv->stock_id);
238 g_free (priv->stock_detail);
239 g_free (priv->icon_name);
242 g_object_unref (priv->gicon);
244 G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
248 gtk_cell_renderer_pixbuf_get_property (GObject *object,
253 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
254 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
259 g_value_set_object (value, priv->pixbuf);
261 case PROP_PIXBUF_EXPANDER_OPEN:
262 g_value_set_object (value, priv->pixbuf_expander_open);
264 case PROP_PIXBUF_EXPANDER_CLOSED:
265 g_value_set_object (value, priv->pixbuf_expander_closed);
268 g_value_set_string (value, priv->stock_id);
270 case PROP_STOCK_SIZE:
271 g_value_set_uint (value, priv->stock_size);
273 case PROP_STOCK_DETAIL:
274 g_value_set_string (value, priv->stock_detail);
276 case PROP_FOLLOW_STATE:
277 g_value_set_boolean (value, priv->follow_state);
280 g_value_set_string (value, priv->icon_name);
283 g_value_set_object (value, priv->gicon);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
292 gtk_cell_renderer_pixbuf_set_property (GObject *object,
297 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
298 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
304 g_object_unref (priv->pixbuf);
305 priv->pixbuf = (GdkPixbuf*) g_value_dup_object (value);
310 g_free (priv->stock_id);
311 priv->stock_id = NULL;
312 g_object_notify (object, "stock-id");
316 g_free (priv->icon_name);
317 priv->icon_name = NULL;
318 g_object_notify (object, "icon-name");
322 g_object_unref (priv->gicon);
324 g_object_notify (object, "gicon");
328 case PROP_PIXBUF_EXPANDER_OPEN:
329 if (priv->pixbuf_expander_open)
330 g_object_unref (priv->pixbuf_expander_open);
331 priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
333 case PROP_PIXBUF_EXPANDER_CLOSED:
334 if (priv->pixbuf_expander_closed)
335 g_object_unref (priv->pixbuf_expander_closed);
336 priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
343 g_object_unref (priv->pixbuf);
345 g_object_notify (object, "pixbuf");
347 g_free (priv->stock_id);
349 priv->stock_id = g_value_dup_string (value);
354 g_object_unref (priv->pixbuf);
356 g_object_notify (object, "pixbuf");
360 g_free (priv->icon_name);
361 priv->icon_name = NULL;
362 g_object_notify (object, "icon-name");
366 g_object_unref (priv->gicon);
368 g_object_notify (object, "gicon");
372 case PROP_STOCK_SIZE:
373 priv->stock_size = g_value_get_uint (value);
375 case PROP_STOCK_DETAIL:
376 g_free (priv->stock_detail);
377 priv->stock_detail = g_value_dup_string (value);
384 g_object_unref (priv->pixbuf);
386 g_object_notify (object, "pixbuf");
388 g_free (priv->icon_name);
390 priv->icon_name = g_value_dup_string (value);
395 g_object_unref (priv->pixbuf);
397 g_object_notify (object, "pixbuf");
401 g_free (priv->stock_id);
402 priv->stock_id = NULL;
403 g_object_notify (object, "stock-id");
407 g_object_unref (priv->gicon);
409 g_object_notify (object, "gicon");
413 case PROP_FOLLOW_STATE:
414 priv->follow_state = g_value_get_boolean (value);
421 g_object_unref (priv->pixbuf);
423 g_object_notify (object, "pixbuf");
425 g_object_unref (priv->gicon);
427 priv->gicon = (GIcon *) g_value_dup_object (value);
432 g_object_unref (priv->pixbuf);
434 g_object_notify (object, "pixbuf");
438 g_free (priv->stock_id);
439 priv->stock_id = NULL;
440 g_object_notify (object, "stock-id");
444 g_free (priv->icon_name);
445 priv->icon_name = NULL;
446 g_object_notify (object, "icon-name");
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
457 * gtk_cell_renderer_pixbuf_new:
459 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
460 * parameters using object properties. Object properties can be set
461 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
462 * can bind a property to a value in a #GtkTreeModel. For example, you
463 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
464 * in the model, thus rendering a different image in each row of the
467 * Return value: the new cell renderer
470 gtk_cell_renderer_pixbuf_new (void)
472 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
476 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
479 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
482 g_object_unref (priv->pixbuf);
484 priv->pixbuf = gtk_widget_render_icon (widget,
489 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
493 gtk_cell_renderer_pixbuf_create_themed_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
496 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
498 GtkIconTheme *icon_theme;
499 GtkSettings *settings;
505 g_object_unref (priv->pixbuf);
509 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
510 icon_theme = gtk_icon_theme_get_for_screen (screen);
511 settings = gtk_settings_get_for_screen (screen);
513 if (!gtk_icon_size_lookup_for_settings (settings,
517 g_warning ("Invalid icon size %u\n", priv->stock_size);
522 info = gtk_icon_theme_lookup_icon (icon_theme,
525 GTK_ICON_LOOKUP_USE_BUILTIN);
526 else if (priv->gicon)
527 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
530 GTK_ICON_LOOKUP_USE_BUILTIN);
538 style = gtk_widget_get_style (GTK_WIDGET (widget));
539 priv->pixbuf = gtk_icon_info_load_symbolic_for_style (info,
544 gtk_icon_info_free (info);
547 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
551 create_symbolic_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
555 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
557 GtkIconTheme *icon_theme;
558 GtkSettings *settings;
563 /* Not a named symbolic icon? */
564 if (priv->icon_name) {
565 if (!g_str_has_suffix (priv->icon_name, "-symbolic"))
567 } else if (priv->gicon) {
568 const gchar * const *names;
569 if (!G_IS_THEMED_ICON (priv->gicon))
571 names = g_themed_icon_get_names (G_THEMED_ICON (priv->gicon));
572 if (names == NULL || !g_str_has_suffix (names[0], "-symbolic"))
578 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
579 icon_theme = gtk_icon_theme_get_for_screen (screen);
580 settings = gtk_settings_get_for_screen (screen);
582 if (!gtk_icon_size_lookup_for_settings (settings,
586 g_warning ("Invalid icon size %u\n", priv->stock_size);
592 info = gtk_icon_theme_lookup_icon (icon_theme,
595 GTK_ICON_LOOKUP_USE_BUILTIN);
596 else if (priv->gicon)
597 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
600 GTK_ICON_LOOKUP_USE_BUILTIN);
608 style = gtk_widget_get_style (GTK_WIDGET (widget));
609 pixbuf = gtk_icon_info_load_symbolic_for_style (info,
612 gtk_icon_info_free (info);
620 create_colorized_pixbuf (GdkPixbuf *src,
624 gint width, height, has_alpha, src_row_stride, dst_row_stride;
625 gint red_value, green_value, blue_value;
626 guchar *target_pixels;
627 guchar *original_pixels;
632 red_value = new_color->red / 255.0;
633 green_value = new_color->green / 255.0;
634 blue_value = new_color->blue / 255.0;
636 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
637 gdk_pixbuf_get_has_alpha (src),
638 gdk_pixbuf_get_bits_per_sample (src),
639 gdk_pixbuf_get_width (src),
640 gdk_pixbuf_get_height (src));
642 has_alpha = gdk_pixbuf_get_has_alpha (src);
643 width = gdk_pixbuf_get_width (src);
644 height = gdk_pixbuf_get_height (src);
645 src_row_stride = gdk_pixbuf_get_rowstride (src);
646 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
647 target_pixels = gdk_pixbuf_get_pixels (dest);
648 original_pixels = gdk_pixbuf_get_pixels (src);
650 for (i = 0; i < height; i++) {
651 pixdest = target_pixels + i*dst_row_stride;
652 pixsrc = original_pixels + i*src_row_stride;
653 for (j = 0; j < width; j++) {
654 *pixdest++ = (*pixsrc++ * red_value) >> 8;
655 *pixdest++ = (*pixsrc++ * green_value) >> 8;
656 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
658 *pixdest++ = *pixsrc++;
667 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
669 const GdkRectangle *cell_area,
675 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
676 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
677 gint pixbuf_width = 0;
678 gint pixbuf_height = 0;
686 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
687 else if (priv->icon_name || priv->gicon)
688 gtk_cell_renderer_pixbuf_create_themed_pixbuf (cellpixbuf, widget);
693 pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
694 pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
696 if (priv->pixbuf_expander_open)
698 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
699 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
701 if (priv->pixbuf_expander_closed)
703 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
704 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
707 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
708 calc_width = (gint) xpad * 2 + pixbuf_width;
709 calc_height = (gint) ypad * 2 + pixbuf_height;
711 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
713 gfloat xalign, yalign;
715 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
718 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
719 (1.0 - xalign) : xalign) *
720 (cell_area->width - calc_width));
721 *x_offset = MAX (*x_offset, 0);
725 *y_offset = (yalign *
726 (cell_area->height - calc_height));
727 *y_offset = MAX (*y_offset, 0);
732 if (x_offset) *x_offset = 0;
733 if (y_offset) *y_offset = 0;
740 *height = calc_height;
744 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
747 const GdkRectangle *background_area,
748 const GdkRectangle *cell_area,
749 GtkCellRendererState flags)
752 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
753 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
755 GdkPixbuf *invisible = NULL;
756 GdkPixbuf *colorized = NULL;
757 GdkPixbuf *symbolic = NULL;
758 GdkRectangle pix_rect;
759 GdkRectangle draw_rect;
760 gboolean is_expander;
763 gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
769 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
770 pix_rect.x += cell_area->x + xpad;
771 pix_rect.y += cell_area->y + ypad;
772 pix_rect.width -= xpad * 2;
773 pix_rect.height -= ypad * 2;
775 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
778 pixbuf = priv->pixbuf;
780 g_object_get (cell, "is-expander", &is_expander, NULL);
783 gboolean is_expanded;
785 g_object_get (cell, "is-expanded", &is_expanded, NULL);
788 priv->pixbuf_expander_open != NULL)
789 pixbuf = priv->pixbuf_expander_open;
790 else if (!is_expanded &&
791 priv->pixbuf_expander_closed != NULL)
792 pixbuf = priv->pixbuf_expander_closed;
798 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
799 !gtk_cell_renderer_get_sensitive (cell))
801 GtkIconSource *source;
803 source = gtk_icon_source_new ();
804 gtk_icon_source_set_pixbuf (source, pixbuf);
805 /* The size here is arbitrary; since size isn't
806 * wildcarded in the source, it isn't supposed to be
807 * scaled by the engine function
809 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
810 gtk_icon_source_set_size_wildcarded (source, FALSE);
812 invisible = gtk_style_render_icon (gtk_widget_get_style (widget),
814 gtk_widget_get_direction (widget),
815 GTK_STATE_INSENSITIVE,
819 "gtkcellrendererpixbuf");
821 gtk_icon_source_free (source);
825 else if (priv->follow_state &&
826 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
830 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
832 if (gtk_widget_has_focus (widget))
833 state = GTK_STATE_SELECTED;
835 state = GTK_STATE_ACTIVE;
838 state = GTK_STATE_PRELIGHT;
840 symbolic = create_symbolic_pixbuf (cellpixbuf, widget, state);
842 colorized = create_colorized_pixbuf (pixbuf,
843 >k_widget_get_style (widget)->base[state]);
851 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
852 gdk_cairo_rectangle (cr, &draw_rect);
856 g_object_unref (invisible);
859 g_object_unref (colorized);
862 g_object_unref (symbolic);