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 g_free (priv->stock_detail);
340 priv->stock_detail = g_value_dup_string (value);
345 if (cellpixbuf->pixbuf)
347 g_object_unref (cellpixbuf->pixbuf);
348 cellpixbuf->pixbuf = NULL;
349 g_object_notify (object, "pixbuf");
351 g_free (priv->icon_name);
353 priv->icon_name = g_value_dup_string (value);
356 if (cellpixbuf->pixbuf)
358 g_object_unref (cellpixbuf->pixbuf);
359 cellpixbuf->pixbuf = NULL;
360 g_object_notify (object, "pixbuf");
364 g_free (priv->stock_id);
365 priv->stock_id = NULL;
366 g_object_notify (object, "stock-id");
370 case PROP_FOLLOW_STATE:
371 priv->follow_state = g_value_get_boolean (value);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
380 * gtk_cell_renderer_pixbuf_new:
382 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
383 * parameters using object properties. Object properties can be set
384 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
385 * can bind a property to a value in a #GtkTreeModel. For example, you
386 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
387 * in the model, thus rendering a different image in each row of the
390 * Return value: the new cell renderer
393 gtk_cell_renderer_pixbuf_new (void)
395 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
399 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
402 GtkCellRendererPixbufPrivate *priv;
404 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
406 if (cellpixbuf->pixbuf)
407 g_object_unref (cellpixbuf->pixbuf);
409 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
414 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
418 gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
421 GtkCellRendererPixbufPrivate *priv;
423 GtkIconTheme *icon_theme;
424 GtkSettings *settings;
426 GError *error = NULL;
428 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
430 if (cellpixbuf->pixbuf)
431 g_object_unref (cellpixbuf->pixbuf);
433 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
434 icon_theme = gtk_icon_theme_get_for_screen (screen);
435 settings = gtk_settings_get_for_screen (screen);
437 if (!gtk_icon_size_lookup_for_settings (settings,
441 g_warning ("Invalid icon size %u\n", priv->stock_size);
446 gtk_icon_theme_load_icon (icon_theme,
448 MIN (width, height), 0, &error);
449 if (!cellpixbuf->pixbuf)
451 g_warning ("could not load image: %s\n", error->message);
452 g_error_free (error);
455 g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
459 create_colorized_pixbuf (GdkPixbuf *src,
463 gint width, height, has_alpha, src_row_stride, dst_row_stride;
464 gint red_value, green_value, blue_value;
465 guchar *target_pixels;
466 guchar *original_pixels;
471 red_value = new_color->red / 255.0;
472 green_value = new_color->green / 255.0;
473 blue_value = new_color->blue / 255.0;
475 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
476 gdk_pixbuf_get_has_alpha (src),
477 gdk_pixbuf_get_bits_per_sample (src),
478 gdk_pixbuf_get_width (src),
479 gdk_pixbuf_get_height (src));
481 has_alpha = gdk_pixbuf_get_has_alpha (src);
482 width = gdk_pixbuf_get_width (src);
483 height = gdk_pixbuf_get_height (src);
484 src_row_stride = gdk_pixbuf_get_rowstride (src);
485 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
486 target_pixels = gdk_pixbuf_get_pixels (dest);
487 original_pixels = gdk_pixbuf_get_pixels (src);
489 for (i = 0; i < height; i++) {
490 pixdest = target_pixels + i*dst_row_stride;
491 pixsrc = original_pixels + i*src_row_stride;
492 for (j = 0; j < width; j++) {
493 *pixdest++ = (*pixsrc++ * red_value) >> 8;
494 *pixdest++ = (*pixsrc++ * green_value) >> 8;
495 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
497 *pixdest++ = *pixsrc++;
506 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
508 GdkRectangle *cell_area,
514 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
515 GtkCellRendererPixbufPrivate *priv;
516 gint pixbuf_width = 0;
517 gint pixbuf_height = 0;
521 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
523 if (!cellpixbuf->pixbuf)
526 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
527 else if (priv->icon_name)
528 gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (cellpixbuf, widget);
531 if (cellpixbuf->pixbuf)
533 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
534 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
536 if (cellpixbuf->pixbuf_expander_open)
538 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
539 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
541 if (cellpixbuf->pixbuf_expander_closed)
543 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
544 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
547 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
548 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
550 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
554 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
555 (1.0 - cell->xalign) : cell->xalign) *
556 (cell_area->width - calc_width));
557 *x_offset = MAX (*x_offset, 0);
561 *y_offset = (cell->yalign *
562 (cell_area->height - calc_height));
563 *y_offset = MAX (*y_offset, 0);
568 if (x_offset) *x_offset = 0;
569 if (y_offset) *y_offset = 0;
576 *height = calc_height;
580 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
583 GdkRectangle *background_area,
584 GdkRectangle *cell_area,
585 GdkRectangle *expose_area,
586 GtkCellRendererState flags)
589 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
590 GtkCellRendererPixbufPrivate *priv;
592 GdkPixbuf *invisible = NULL;
593 GdkPixbuf *colorized = NULL;
594 GdkRectangle pix_rect;
595 GdkRectangle draw_rect;
598 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
600 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
606 pix_rect.x += cell_area->x + cell->xpad;
607 pix_rect.y += cell_area->y + cell->ypad;
608 pix_rect.width -= cell->xpad * 2;
609 pix_rect.height -= cell->ypad * 2;
611 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
612 !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
615 pixbuf = cellpixbuf->pixbuf;
617 if (cell->is_expander)
619 if (cell->is_expanded &&
620 cellpixbuf->pixbuf_expander_open != NULL)
621 pixbuf = cellpixbuf->pixbuf_expander_open;
622 else if (!cell->is_expanded &&
623 cellpixbuf->pixbuf_expander_closed != NULL)
624 pixbuf = cellpixbuf->pixbuf_expander_closed;
630 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
632 GtkIconSource *source;
634 source = gtk_icon_source_new ();
635 gtk_icon_source_set_pixbuf (source, pixbuf);
636 /* The size here is arbitrary; since size isn't
637 * wildcarded in the source, it isn't supposed to be
638 * scaled by the engine function
640 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
641 gtk_icon_source_set_size_wildcarded (source, FALSE);
643 invisible = gtk_style_render_icon (widget->style,
645 gtk_widget_get_direction (widget),
646 GTK_STATE_INSENSITIVE,
650 "gtkcellrendererpixbuf");
652 gtk_icon_source_free (source);
656 else if (priv->follow_state &&
657 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
661 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
663 if (GTK_WIDGET_HAS_FOCUS (widget))
664 state = GTK_STATE_SELECTED;
666 state = GTK_STATE_ACTIVE;
669 state = GTK_STATE_PRELIGHT;
671 colorized = create_colorized_pixbuf (pixbuf,
672 &widget->style->base[state]);
677 cr = gdk_cairo_create (window);
679 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
680 gdk_cairo_rectangle (cr, &draw_rect);
686 g_object_unref (invisible);
689 g_object_unref (colorized);
692 #define __GTK_CELL_RENDERER_PIXBUF_C__
693 #include "gtkaliasdef.c"