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_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
42 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
44 GdkRectangle *rectangle,
49 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
52 GdkRectangle *background_area,
53 GdkRectangle *cell_area,
54 GdkRectangle *expose_area,
55 GtkCellRendererState flags);
61 PROP_PIXBUF_EXPANDER_OPEN,
62 PROP_PIXBUF_EXPANDER_CLOSED,
71 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
73 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
74 struct _GtkCellRendererPixbufPrivate
77 GtkIconSize stock_size;
79 gboolean follow_state;
84 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
87 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
89 GtkCellRendererPixbufPrivate *priv;
91 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
92 priv->stock_size = GTK_ICON_SIZE_MENU;
96 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
98 GObjectClass *object_class = G_OBJECT_CLASS (class);
99 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
101 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
103 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
104 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
106 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
107 cell_class->render = gtk_cell_renderer_pixbuf_render;
109 g_object_class_install_property (object_class,
111 g_param_spec_object ("pixbuf",
113 P_("The pixbuf to render"),
115 GTK_PARAM_READWRITE));
117 g_object_class_install_property (object_class,
118 PROP_PIXBUF_EXPANDER_OPEN,
119 g_param_spec_object ("pixbuf-expander-open",
120 P_("Pixbuf Expander Open"),
121 P_("Pixbuf for open expander"),
123 GTK_PARAM_READWRITE));
125 g_object_class_install_property (object_class,
126 PROP_PIXBUF_EXPANDER_CLOSED,
127 g_param_spec_object ("pixbuf-expander-closed",
128 P_("Pixbuf Expander Closed"),
129 P_("Pixbuf for closed expander"),
131 GTK_PARAM_READWRITE));
133 g_object_class_install_property (object_class,
135 g_param_spec_string ("stock-id",
137 P_("The stock ID of the stock icon to render"),
139 GTK_PARAM_READWRITE));
141 g_object_class_install_property (object_class,
143 g_param_spec_uint ("stock-size",
145 P_("The GtkIconSize value that specifies the size of the rendered icon"),
149 GTK_PARAM_READWRITE));
151 g_object_class_install_property (object_class,
153 g_param_spec_string ("stock-detail",
155 P_("Render detail to pass to the theme engine"),
157 GTK_PARAM_READWRITE));
161 * GtkCellRendererPixbuf:icon-name:
163 * The name of the themed icon to display.
164 * This property only has an effect if not overridden by "stock_id"
165 * or "pixbuf" properties.
169 g_object_class_install_property (object_class,
171 g_param_spec_string ("icon-name",
173 P_("The name of the icon from the icon theme"),
175 GTK_PARAM_READWRITE));
178 * GtkCellRendererPixbuf:follow-state:
180 * Specifies whether the rendered pixbuf should be colorized
181 * according to the #GtkCellRendererState.
185 g_object_class_install_property (object_class,
187 g_param_spec_boolean ("follow-state",
189 P_("Whether the rendered pixbuf should be "
190 "colorized according to the state"),
192 GTK_PARAM_READWRITE));
195 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
199 gtk_cell_renderer_pixbuf_finalize (GObject *object)
201 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
202 GtkCellRendererPixbufPrivate *priv;
204 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
206 if (cellpixbuf->pixbuf)
207 g_object_unref (cellpixbuf->pixbuf);
208 if (cellpixbuf->pixbuf_expander_open)
209 g_object_unref (cellpixbuf->pixbuf_expander_open);
210 if (cellpixbuf->pixbuf_expander_closed)
211 g_object_unref (cellpixbuf->pixbuf_expander_closed);
213 g_free (priv->stock_id);
214 g_free (priv->stock_detail);
215 g_free (priv->icon_name);
217 (* G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize) (object);
221 gtk_cell_renderer_pixbuf_get_property (GObject *object,
226 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
227 GtkCellRendererPixbufPrivate *priv;
229 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
234 g_value_set_object (value, G_OBJECT (cellpixbuf->pixbuf));
236 case PROP_PIXBUF_EXPANDER_OPEN:
237 g_value_set_object (value, G_OBJECT (cellpixbuf->pixbuf_expander_open));
239 case PROP_PIXBUF_EXPANDER_CLOSED:
240 g_value_set_object (value, G_OBJECT (cellpixbuf->pixbuf_expander_closed));
243 g_value_set_string (value, priv->stock_id);
245 case PROP_STOCK_SIZE:
246 g_value_set_uint (value, priv->stock_size);
248 case PROP_STOCK_DETAIL:
249 g_value_set_string (value, priv->stock_detail);
251 case PROP_FOLLOW_STATE:
252 g_value_set_boolean (value, priv->follow_state);
255 g_value_set_string (value, priv->icon_name);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
265 gtk_cell_renderer_pixbuf_set_property (GObject *object,
270 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
271 GtkCellRendererPixbufPrivate *priv;
273 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
278 if (cellpixbuf->pixbuf)
279 g_object_unref (cellpixbuf->pixbuf);
280 cellpixbuf->pixbuf = (GdkPixbuf*) g_value_dup_object (value);
281 if (cellpixbuf->pixbuf)
285 g_free (priv->stock_id);
286 priv->stock_id = NULL;
287 g_object_notify (object, "stock-id");
291 g_free (priv->icon_name);
292 priv->icon_name = NULL;
293 g_object_notify (object, "icon-name");
297 case PROP_PIXBUF_EXPANDER_OPEN:
298 if (cellpixbuf->pixbuf_expander_open)
299 g_object_unref (cellpixbuf->pixbuf_expander_open);
300 cellpixbuf->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
302 case PROP_PIXBUF_EXPANDER_CLOSED:
303 if (cellpixbuf->pixbuf_expander_closed)
304 g_object_unref (cellpixbuf->pixbuf_expander_closed);
305 cellpixbuf->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
310 if (cellpixbuf->pixbuf)
312 g_object_unref (cellpixbuf->pixbuf);
313 cellpixbuf->pixbuf = NULL;
314 g_object_notify (object, "pixbuf");
316 g_free (priv->stock_id);
318 priv->stock_id = g_value_dup_string (value);
321 if (cellpixbuf->pixbuf)
323 g_object_unref (cellpixbuf->pixbuf);
324 cellpixbuf->pixbuf = NULL;
325 g_object_notify (object, "pixbuf");
329 g_free (priv->icon_name);
330 priv->icon_name = NULL;
331 g_object_notify (object, "icon-name");
335 case PROP_STOCK_SIZE:
336 priv->stock_size = g_value_get_uint (value);
338 case PROP_STOCK_DETAIL:
339 if (priv->stock_detail)
340 g_free (priv->stock_detail);
341 priv->stock_detail = g_value_dup_string (value);
346 if (cellpixbuf->pixbuf)
348 g_object_unref (cellpixbuf->pixbuf);
349 cellpixbuf->pixbuf = NULL;
350 g_object_notify (object, "pixbuf");
352 g_free (priv->icon_name);
354 priv->icon_name = g_value_dup_string (value);
357 if (cellpixbuf->pixbuf)
359 g_object_unref (cellpixbuf->pixbuf);
360 cellpixbuf->pixbuf = NULL;
361 g_object_notify (object, "pixbuf");
365 g_free (priv->stock_id);
366 priv->stock_id = NULL;
367 g_object_notify (object, "stock-id");
371 case PROP_FOLLOW_STATE:
372 priv->follow_state = g_value_get_boolean (value);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
381 * gtk_cell_renderer_pixbuf_new:
383 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
384 * parameters using object properties. Object properties can be set
385 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
386 * can bind a property to a value in a #GtkTreeModel. For example, you
387 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
388 * in the model, thus rendering a different image in each row of the
391 * Return value: the new cell renderer
394 gtk_cell_renderer_pixbuf_new (void)
396 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
400 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
403 GtkCellRendererPixbufPrivate *priv;
405 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
407 if (cellpixbuf->pixbuf)
408 g_object_unref (cellpixbuf->pixbuf);
410 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
415 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
419 gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
422 GtkCellRendererPixbufPrivate *priv;
424 GtkIconTheme *icon_theme;
425 GtkSettings *settings;
427 GError *error = NULL;
429 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
431 if (cellpixbuf->pixbuf)
432 g_object_unref (cellpixbuf->pixbuf);
434 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
435 icon_theme = gtk_icon_theme_get_for_screen (screen);
436 settings = gtk_settings_get_for_screen (screen);
438 if (!gtk_icon_size_lookup_for_settings (settings,
442 g_warning ("Invalid icon size %u\n", priv->stock_size);
447 gtk_icon_theme_load_icon (icon_theme,
449 MIN (width, height), 0, &error);
450 if (!cellpixbuf->pixbuf)
452 g_warning ("could not load image: %s\n", error->message);
453 g_error_free (error);
456 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
460 create_colorized_pixbuf (GdkPixbuf *src,
464 gint width, height, has_alpha, src_row_stride, dst_row_stride;
465 gint red_value, green_value, blue_value;
466 guchar *target_pixels;
467 guchar *original_pixels;
472 red_value = new_color->red / 255.0;
473 green_value = new_color->green / 255.0;
474 blue_value = new_color->blue / 255.0;
476 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
477 gdk_pixbuf_get_has_alpha (src),
478 gdk_pixbuf_get_bits_per_sample (src),
479 gdk_pixbuf_get_width (src),
480 gdk_pixbuf_get_height (src));
482 has_alpha = gdk_pixbuf_get_has_alpha (src);
483 width = gdk_pixbuf_get_width (src);
484 height = gdk_pixbuf_get_height (src);
485 src_row_stride = gdk_pixbuf_get_rowstride (src);
486 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
487 target_pixels = gdk_pixbuf_get_pixels (dest);
488 original_pixels = gdk_pixbuf_get_pixels (src);
490 for (i = 0; i < height; i++) {
491 pixdest = target_pixels + i*dst_row_stride;
492 pixsrc = original_pixels + i*src_row_stride;
493 for (j = 0; j < width; j++) {
494 *pixdest++ = (*pixsrc++ * red_value) >> 8;
495 *pixdest++ = (*pixsrc++ * green_value) >> 8;
496 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
498 *pixdest++ = *pixsrc++;
507 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
509 GdkRectangle *cell_area,
515 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
516 GtkCellRendererPixbufPrivate *priv;
517 gint pixbuf_width = 0;
518 gint pixbuf_height = 0;
522 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
524 if (!cellpixbuf->pixbuf)
527 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
528 else if (priv->icon_name)
529 gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (cellpixbuf, widget);
532 if (cellpixbuf->pixbuf)
534 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
535 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
537 if (cellpixbuf->pixbuf_expander_open)
539 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
540 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
542 if (cellpixbuf->pixbuf_expander_closed)
544 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
545 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
548 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
549 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
551 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
555 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
556 (1.0 - cell->xalign) : cell->xalign) *
557 (cell_area->width - calc_width));
558 *x_offset = MAX (*x_offset, 0);
562 *y_offset = (cell->yalign *
563 (cell_area->height - calc_height));
564 *y_offset = MAX (*y_offset, 0);
569 if (x_offset) *x_offset = 0;
570 if (y_offset) *y_offset = 0;
577 *height = calc_height;
581 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
584 GdkRectangle *background_area,
585 GdkRectangle *cell_area,
586 GdkRectangle *expose_area,
587 GtkCellRendererState flags)
590 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
591 GtkCellRendererPixbufPrivate *priv;
593 GdkPixbuf *invisible = NULL;
594 GdkPixbuf *colorized = NULL;
595 GdkRectangle pix_rect;
596 GdkRectangle draw_rect;
599 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
601 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
607 pix_rect.x += cell_area->x + cell->xpad;
608 pix_rect.y += cell_area->y + cell->ypad;
609 pix_rect.width -= cell->xpad * 2;
610 pix_rect.height -= cell->ypad * 2;
612 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
613 !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
616 pixbuf = cellpixbuf->pixbuf;
618 if (cell->is_expander)
620 if (cell->is_expanded &&
621 cellpixbuf->pixbuf_expander_open != NULL)
622 pixbuf = cellpixbuf->pixbuf_expander_open;
623 else if (!cell->is_expanded &&
624 cellpixbuf->pixbuf_expander_closed != NULL)
625 pixbuf = cellpixbuf->pixbuf_expander_closed;
631 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
633 GtkIconSource *source;
635 source = gtk_icon_source_new ();
636 gtk_icon_source_set_pixbuf (source, pixbuf);
637 /* The size here is arbitrary; since size isn't
638 * wildcarded in the source, it isn't supposed to be
639 * scaled by the engine function
641 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
642 gtk_icon_source_set_size_wildcarded (source, FALSE);
644 invisible = gtk_style_render_icon (widget->style,
646 gtk_widget_get_direction (widget),
647 GTK_STATE_INSENSITIVE,
651 "gtkcellrendererpixbuf");
653 gtk_icon_source_free (source);
657 else if (priv->follow_state &&
658 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
662 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
664 if (GTK_WIDGET_HAS_FOCUS (widget))
665 state = GTK_STATE_SELECTED;
667 state = GTK_STATE_ACTIVE;
670 state = GTK_STATE_PRELIGHT;
672 colorized = create_colorized_pixbuf (pixbuf,
673 &widget->style->base[state]);
678 cr = gdk_cairo_create (window);
680 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
681 gdk_cairo_rectangle (cr, &draw_rect);
687 g_object_unref (invisible);
690 g_object_unref (colorized);
693 #define __GTK_CELL_RENDERER_PIXBUF_C__
694 #include "gtkaliasdef.c"