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_pixbuf (widget,
488 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
492 gtk_cell_renderer_pixbuf_create_themed_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
495 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
497 GtkIconTheme *icon_theme;
498 GtkSettings *settings;
504 g_object_unref (priv->pixbuf);
508 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
509 icon_theme = gtk_icon_theme_get_for_screen (screen);
510 settings = gtk_settings_get_for_screen (screen);
512 if (!gtk_icon_size_lookup_for_settings (settings,
516 g_warning ("Invalid icon size %u\n", priv->stock_size);
521 info = gtk_icon_theme_lookup_icon (icon_theme,
524 GTK_ICON_LOOKUP_USE_BUILTIN);
525 else if (priv->gicon)
526 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
529 GTK_ICON_LOOKUP_USE_BUILTIN);
535 GtkStyleContext *context;
537 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
538 priv->pixbuf = gtk_icon_info_load_symbolic_for_context (info,
542 gtk_icon_info_free (info);
545 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
549 create_symbolic_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
553 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
555 GtkIconTheme *icon_theme;
556 GtkSettings *settings;
561 /* Not a named symbolic icon? */
562 if (priv->icon_name) {
563 if (!g_str_has_suffix (priv->icon_name, "-symbolic"))
565 } else if (priv->gicon) {
566 const gchar * const *names;
567 if (!G_IS_THEMED_ICON (priv->gicon))
569 names = g_themed_icon_get_names (G_THEMED_ICON (priv->gicon));
570 if (names == NULL || !g_str_has_suffix (names[0], "-symbolic"))
576 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
577 icon_theme = gtk_icon_theme_get_for_screen (screen);
578 settings = gtk_settings_get_for_screen (screen);
580 if (!gtk_icon_size_lookup_for_settings (settings,
584 g_warning ("Invalid icon size %u\n", priv->stock_size);
590 info = gtk_icon_theme_lookup_icon (icon_theme,
593 GTK_ICON_LOOKUP_USE_BUILTIN);
594 else if (priv->gicon)
595 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
598 GTK_ICON_LOOKUP_USE_BUILTIN);
604 GtkStyleContext *context;
606 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
607 pixbuf = gtk_icon_info_load_symbolic_for_context (info,
611 gtk_icon_info_free (info);
619 create_colorized_pixbuf (GdkPixbuf *src,
623 gint width, height, has_alpha, src_row_stride, dst_row_stride;
624 gint red_value, green_value, blue_value;
625 guchar *target_pixels;
626 guchar *original_pixels;
631 red_value = new_color->red / 255.0;
632 green_value = new_color->green / 255.0;
633 blue_value = new_color->blue / 255.0;
635 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
636 gdk_pixbuf_get_has_alpha (src),
637 gdk_pixbuf_get_bits_per_sample (src),
638 gdk_pixbuf_get_width (src),
639 gdk_pixbuf_get_height (src));
641 has_alpha = gdk_pixbuf_get_has_alpha (src);
642 width = gdk_pixbuf_get_width (src);
643 height = gdk_pixbuf_get_height (src);
644 src_row_stride = gdk_pixbuf_get_rowstride (src);
645 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
646 target_pixels = gdk_pixbuf_get_pixels (dest);
647 original_pixels = gdk_pixbuf_get_pixels (src);
649 for (i = 0; i < height; i++) {
650 pixdest = target_pixels + i*dst_row_stride;
651 pixsrc = original_pixels + i*src_row_stride;
652 for (j = 0; j < width; j++) {
653 *pixdest++ = (*pixsrc++ * red_value) >> 8;
654 *pixdest++ = (*pixsrc++ * green_value) >> 8;
655 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
657 *pixdest++ = *pixsrc++;
666 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
668 const GdkRectangle *cell_area,
674 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
675 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
676 gint pixbuf_width = 0;
677 gint pixbuf_height = 0;
685 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
686 else if (priv->icon_name || priv->gicon)
687 gtk_cell_renderer_pixbuf_create_themed_pixbuf (cellpixbuf, widget);
692 pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
693 pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
695 if (priv->pixbuf_expander_open)
697 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
698 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
700 if (priv->pixbuf_expander_closed)
702 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
703 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
706 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
707 calc_width = (gint) xpad * 2 + pixbuf_width;
708 calc_height = (gint) ypad * 2 + pixbuf_height;
710 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
712 gfloat xalign, yalign;
714 gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
717 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
718 (1.0 - xalign) : xalign) *
719 (cell_area->width - calc_width));
720 *x_offset = MAX (*x_offset, 0);
724 *y_offset = (yalign *
725 (cell_area->height - calc_height));
726 *y_offset = MAX (*y_offset, 0);
731 if (x_offset) *x_offset = 0;
732 if (y_offset) *y_offset = 0;
739 *height = calc_height;
743 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
746 const GdkRectangle *background_area,
747 const GdkRectangle *cell_area,
748 GtkCellRendererState flags)
751 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
752 GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
754 GdkPixbuf *invisible = NULL;
755 GdkPixbuf *colorized = NULL;
756 GdkPixbuf *symbolic = NULL;
757 GdkRectangle pix_rect;
758 GdkRectangle draw_rect;
759 gboolean is_expander;
762 gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
768 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
769 pix_rect.x += cell_area->x + xpad;
770 pix_rect.y += cell_area->y + ypad;
771 pix_rect.width -= xpad * 2;
772 pix_rect.height -= ypad * 2;
774 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
777 pixbuf = priv->pixbuf;
779 g_object_get (cell, "is-expander", &is_expander, NULL);
782 gboolean is_expanded;
784 g_object_get (cell, "is-expanded", &is_expanded, NULL);
787 priv->pixbuf_expander_open != NULL)
788 pixbuf = priv->pixbuf_expander_open;
789 else if (!is_expanded &&
790 priv->pixbuf_expander_closed != NULL)
791 pixbuf = priv->pixbuf_expander_closed;
797 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
798 !gtk_cell_renderer_get_sensitive (cell))
800 GtkIconSource *source;
802 source = gtk_icon_source_new ();
803 gtk_icon_source_set_pixbuf (source, pixbuf);
804 /* The size here is arbitrary; since size isn't
805 * wildcarded in the source, it isn't supposed to be
806 * scaled by the engine function
808 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
809 gtk_icon_source_set_size_wildcarded (source, FALSE);
811 invisible = gtk_style_render_icon (gtk_widget_get_style (widget),
813 gtk_widget_get_direction (widget),
814 GTK_STATE_INSENSITIVE,
818 "gtkcellrendererpixbuf");
820 gtk_icon_source_free (source);
824 else if (priv->follow_state &&
825 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
829 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
831 if (gtk_widget_has_focus (widget))
832 state = GTK_STATE_SELECTED;
834 state = GTK_STATE_ACTIVE;
837 state = GTK_STATE_PRELIGHT;
839 symbolic = create_symbolic_pixbuf (cellpixbuf, widget, state);
841 colorized = create_colorized_pixbuf (pixbuf,
842 >k_widget_get_style (widget)->base[state]);
850 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
851 gdk_cairo_rectangle (cr, &draw_rect);
855 g_object_unref (invisible);
858 g_object_unref (colorized);
861 g_object_unref (symbolic);