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_init (GtkCellRendererPixbuf *celltext);
38 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
39 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
40 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
42 static void gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
44 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
46 GdkRectangle *rectangle,
51 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
54 GdkRectangle *background_area,
55 GdkRectangle *cell_area,
56 GdkRectangle *expose_area,
57 GtkCellRendererState flags);
63 PROP_PIXBUF_EXPANDER_OPEN,
64 PROP_PIXBUF_EXPANDER_CLOSED,
72 static gpointer parent_class;
75 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
77 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
78 struct _GtkCellRendererPixbufPrivate
81 GtkIconSize stock_size;
83 gboolean follow_state;
90 gtk_cell_renderer_pixbuf_get_type (void)
92 static GType cell_pixbuf_type = 0;
94 if (!cell_pixbuf_type)
96 static const GTypeInfo cell_pixbuf_info =
98 sizeof (GtkCellRendererPixbufClass),
100 NULL, /* base_finalize */
101 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
102 NULL, /* class_finalize */
103 NULL, /* class_data */
104 sizeof (GtkCellRendererPixbuf),
106 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
110 g_type_register_static (GTK_TYPE_CELL_RENDERER, I_("GtkCellRendererPixbuf"),
111 &cell_pixbuf_info, 0);
114 return cell_pixbuf_type;
118 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
120 GtkCellRendererPixbufPrivate *priv;
122 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
123 priv->stock_size = GTK_ICON_SIZE_MENU;
127 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
129 GObjectClass *object_class = G_OBJECT_CLASS (class);
130 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
132 parent_class = g_type_class_peek_parent (class);
134 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
136 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
137 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
139 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
140 cell_class->render = gtk_cell_renderer_pixbuf_render;
142 g_object_class_install_property (object_class,
144 g_param_spec_object ("pixbuf",
146 P_("The pixbuf to render"),
148 GTK_PARAM_READWRITE));
150 g_object_class_install_property (object_class,
151 PROP_PIXBUF_EXPANDER_OPEN,
152 g_param_spec_object ("pixbuf-expander-open",
153 P_("Pixbuf Expander Open"),
154 P_("Pixbuf for open expander"),
156 GTK_PARAM_READWRITE));
158 g_object_class_install_property (object_class,
159 PROP_PIXBUF_EXPANDER_CLOSED,
160 g_param_spec_object ("pixbuf-expander-closed",
161 P_("Pixbuf Expander Closed"),
162 P_("Pixbuf for closed expander"),
164 GTK_PARAM_READWRITE));
166 g_object_class_install_property (object_class,
168 g_param_spec_string ("stock-id",
170 P_("The stock ID of the stock icon to render"),
172 GTK_PARAM_READWRITE));
174 g_object_class_install_property (object_class,
176 g_param_spec_uint ("stock-size",
178 P_("The GtkIconSize value that specifies the size of the rendered icon"),
182 GTK_PARAM_READWRITE));
184 g_object_class_install_property (object_class,
186 g_param_spec_string ("stock-detail",
188 P_("Render detail to pass to the theme engine"),
190 GTK_PARAM_READWRITE));
194 * GtkCellRendererPixbuf:icon-name:
196 * The name of the themed icon to display.
197 * This property only has an effect if not overridden by "stock_id"
198 * or "pixbuf" properties.
202 g_object_class_install_property (object_class,
204 g_param_spec_string ("icon-name",
206 P_("The name of the icon from the icon theme"),
208 GTK_PARAM_READWRITE));
211 * GtkCellRendererPixbuf:follow-state:
213 * Specifies whether the rendered pixbuf should be colorized
214 * according to the #GtkCellRendererState.
218 g_object_class_install_property (object_class,
220 g_param_spec_boolean ("follow-state",
222 P_("Whether the rendered pixbuf should be "
223 "colorized according to the state"),
225 GTK_PARAM_READWRITE));
228 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
232 gtk_cell_renderer_pixbuf_finalize (GObject *object)
234 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
235 GtkCellRendererPixbufPrivate *priv;
237 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
239 if (cellpixbuf->pixbuf)
240 g_object_unref (cellpixbuf->pixbuf);
241 if (cellpixbuf->pixbuf_expander_open)
242 g_object_unref (cellpixbuf->pixbuf_expander_open);
243 if (cellpixbuf->pixbuf_expander_closed)
244 g_object_unref (cellpixbuf->pixbuf_expander_closed);
246 g_free (priv->stock_id);
247 g_free (priv->stock_detail);
248 g_free (priv->icon_name);
250 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
254 gtk_cell_renderer_pixbuf_get_property (GObject *object,
259 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
260 GtkCellRendererPixbufPrivate *priv;
262 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
267 g_value_set_object (value,
268 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
270 case PROP_PIXBUF_EXPANDER_OPEN:
271 g_value_set_object (value,
272 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
274 case PROP_PIXBUF_EXPANDER_CLOSED:
275 g_value_set_object (value,
276 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
279 g_value_set_string (value, priv->stock_id);
281 case PROP_STOCK_SIZE:
282 g_value_set_uint (value, priv->stock_size);
284 case PROP_STOCK_DETAIL:
285 g_value_set_string (value, priv->stock_detail);
287 case PROP_FOLLOW_STATE:
288 g_value_set_boolean (value, priv->follow_state);
291 g_value_set_string (value, priv->icon_name);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
301 gtk_cell_renderer_pixbuf_set_property (GObject *object,
307 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
308 GtkCellRendererPixbufPrivate *priv;
310 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
315 pixbuf = (GdkPixbuf*) g_value_get_object (value);
317 g_object_ref (pixbuf);
318 if (cellpixbuf->pixbuf)
319 g_object_unref (cellpixbuf->pixbuf);
320 cellpixbuf->pixbuf = pixbuf;
322 case PROP_PIXBUF_EXPANDER_OPEN:
323 pixbuf = (GdkPixbuf*) g_value_get_object (value);
325 g_object_ref (pixbuf);
326 if (cellpixbuf->pixbuf_expander_open)
327 g_object_unref (cellpixbuf->pixbuf_expander_open);
328 cellpixbuf->pixbuf_expander_open = pixbuf;
330 case PROP_PIXBUF_EXPANDER_CLOSED:
331 pixbuf = (GdkPixbuf*) g_value_get_object (value);
333 g_object_ref (pixbuf);
334 if (cellpixbuf->pixbuf_expander_closed)
335 g_object_unref (cellpixbuf->pixbuf_expander_closed);
336 cellpixbuf->pixbuf_expander_closed = pixbuf;
341 if (cellpixbuf->pixbuf)
343 g_object_unref (cellpixbuf->pixbuf);
344 cellpixbuf->pixbuf = NULL;
346 g_free (priv->stock_id);
347 g_free (priv->icon_name);
348 priv->icon_name = NULL;
350 priv->stock_id = g_strdup (g_value_get_string (value));
352 case PROP_STOCK_SIZE:
353 priv->stock_size = g_value_get_uint (value);
355 case PROP_STOCK_DETAIL:
356 if (priv->stock_detail)
357 g_free (priv->stock_detail);
358 priv->stock_detail = g_strdup (g_value_get_string (value));
363 if (cellpixbuf->pixbuf)
365 g_object_unref (cellpixbuf->pixbuf);
366 cellpixbuf->pixbuf = NULL;
368 g_free (priv->stock_id);
369 g_free (priv->icon_name);
371 priv->icon_name = g_strdup (g_value_get_string (value));
373 case PROP_FOLLOW_STATE:
374 priv->follow_state = g_value_get_boolean (value);
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
381 if (cellpixbuf->pixbuf && priv->stock_id)
383 g_object_unref (cellpixbuf->pixbuf);
384 cellpixbuf->pixbuf = NULL;
389 * gtk_cell_renderer_pixbuf_new:
391 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
392 * parameters using object properties. Object properties can be set
393 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
394 * can bind a property to a value in a #GtkTreeModel. For example, you
395 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
396 * in the model, thus rendering a different image in each row of the
399 * Return value: the new cell renderer
402 gtk_cell_renderer_pixbuf_new (void)
404 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
408 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
411 GtkCellRendererPixbufPrivate *priv;
413 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
415 if (cellpixbuf->pixbuf)
416 g_object_unref (cellpixbuf->pixbuf);
418 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
425 gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
428 GtkCellRendererPixbufPrivate *priv;
430 GtkIconTheme *icon_theme;
431 GtkSettings *settings;
433 GError *error = NULL;
435 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
437 if (cellpixbuf->pixbuf)
438 g_object_unref (cellpixbuf->pixbuf);
440 screen = gtk_widget_get_screen (GTK_WIDGET (widget));
441 icon_theme = gtk_icon_theme_get_for_screen (screen);
442 settings = gtk_settings_get_for_screen (screen);
444 if (!gtk_icon_size_lookup_for_settings (settings,
448 g_warning ("Invalid icon size %d\n", priv->stock_size);
453 gtk_icon_theme_load_icon (icon_theme,
455 MIN (width, height), 0, &error);
456 if (!cellpixbuf->pixbuf)
458 g_warning ("could not load image: %s\n", error->message);
459 g_error_free (error);
464 create_colorized_pixbuf (GdkPixbuf *src,
468 gint width, height, has_alpha, src_row_stride, dst_row_stride;
469 gint red_value, green_value, blue_value;
470 guchar *target_pixels;
471 guchar *original_pixels;
476 red_value = new_color->red / 255.0;
477 green_value = new_color->green / 255.0;
478 blue_value = new_color->blue / 255.0;
480 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
481 gdk_pixbuf_get_has_alpha (src),
482 gdk_pixbuf_get_bits_per_sample (src),
483 gdk_pixbuf_get_width (src),
484 gdk_pixbuf_get_height (src));
486 has_alpha = gdk_pixbuf_get_has_alpha (src);
487 width = gdk_pixbuf_get_width (src);
488 height = gdk_pixbuf_get_height (src);
489 src_row_stride = gdk_pixbuf_get_rowstride (src);
490 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
491 target_pixels = gdk_pixbuf_get_pixels (dest);
492 original_pixels = gdk_pixbuf_get_pixels (src);
494 for (i = 0; i < height; i++) {
495 pixdest = target_pixels + i*dst_row_stride;
496 pixsrc = original_pixels + i*src_row_stride;
497 for (j = 0; j < width; j++) {
498 *pixdest++ = (*pixsrc++ * red_value) >> 8;
499 *pixdest++ = (*pixsrc++ * green_value) >> 8;
500 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
502 *pixdest++ = *pixsrc++;
511 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
513 GdkRectangle *cell_area,
519 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
520 GtkCellRendererPixbufPrivate *priv;
521 gint pixbuf_width = 0;
522 gint pixbuf_height = 0;
526 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
528 if (!cellpixbuf->pixbuf)
531 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
532 else if (priv->icon_name)
533 gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (cellpixbuf, widget);
536 if (cellpixbuf->pixbuf)
538 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
539 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
541 if (cellpixbuf->pixbuf_expander_open)
543 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
544 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
546 if (cellpixbuf->pixbuf_expander_closed)
548 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
549 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
552 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
553 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
555 if (x_offset) *x_offset = 0;
556 if (y_offset) *y_offset = 0;
558 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
562 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
563 1.0 - cell->xalign : cell->xalign) *
564 (cell_area->width - calc_width - 2 * cell->xpad));
565 *x_offset = MAX (*x_offset, 0) + cell->xpad;
569 *y_offset = (cell->yalign *
570 (cell_area->height - calc_height - 2 * cell->ypad));
571 *y_offset = MAX (*y_offset, 0) + cell->ypad;
579 *height = calc_height;
583 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
586 GdkRectangle *background_area,
587 GdkRectangle *cell_area,
588 GdkRectangle *expose_area,
589 GtkCellRendererState flags)
592 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
593 GtkCellRendererPixbufPrivate *priv;
595 GdkPixbuf *invisible = NULL;
596 GdkPixbuf *colorized = NULL;
597 GdkRectangle pix_rect;
598 GdkRectangle draw_rect;
601 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
603 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
609 pix_rect.x += cell_area->x;
610 pix_rect.y += cell_area->y;
611 pix_rect.width -= cell->xpad * 2;
612 pix_rect.height -= cell->ypad * 2;
614 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
615 !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
618 pixbuf = cellpixbuf->pixbuf;
620 if (cell->is_expander)
622 if (cell->is_expanded &&
623 cellpixbuf->pixbuf_expander_open != NULL)
624 pixbuf = cellpixbuf->pixbuf_expander_open;
625 else if (!cell->is_expanded &&
626 cellpixbuf->pixbuf_expander_closed != NULL)
627 pixbuf = cellpixbuf->pixbuf_expander_closed;
633 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
635 GtkIconSource *source;
637 source = gtk_icon_source_new ();
638 gtk_icon_source_set_pixbuf (source, pixbuf);
639 /* The size here is arbitrary; since size isn't
640 * wildcarded in the source, it isn't supposed to be
641 * scaled by the engine function
643 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
644 gtk_icon_source_set_size_wildcarded (source, FALSE);
646 invisible = gtk_style_render_icon (widget->style,
648 gtk_widget_get_direction (widget),
649 GTK_STATE_INSENSITIVE,
653 "gtkcellrendererpixbuf");
655 gtk_icon_source_free (source);
659 else if (priv->follow_state &&
660 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
664 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
666 if (GTK_WIDGET_HAS_FOCUS (widget))
667 state = GTK_STATE_SELECTED;
669 state = GTK_STATE_ACTIVE;
672 state = GTK_STATE_PRELIGHT;
674 colorized = create_colorized_pixbuf (pixbuf,
675 &widget->style->base[state]);
680 cr = gdk_cairo_create (window);
682 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
683 gdk_cairo_rectangle (cr, &draw_rect);
689 g_object_unref (invisible);
692 g_object_unref (colorized);
695 #define __GTK_CELL_RENDERER_PIXBUF_C__
696 #include "gtkaliasdef.c"