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"
25 #include "gtkprivate.h"
28 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
32 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
36 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
37 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
38 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
39 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
41 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
43 GdkRectangle *rectangle,
48 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
51 GdkRectangle *background_area,
52 GdkRectangle *cell_area,
53 GdkRectangle *expose_area,
54 GtkCellRendererState flags);
60 PROP_PIXBUF_EXPANDER_OPEN,
61 PROP_PIXBUF_EXPANDER_CLOSED,
68 static gpointer parent_class;
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 gtk_cell_renderer_pixbuf_get_type (void)
86 static GType cell_pixbuf_type = 0;
88 if (!cell_pixbuf_type)
90 static const GTypeInfo cell_pixbuf_info =
92 sizeof (GtkCellRendererPixbufClass),
94 NULL, /* base_finalize */
95 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
96 NULL, /* class_finalize */
97 NULL, /* class_data */
98 sizeof (GtkCellRendererPixbuf),
100 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
104 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf",
105 &cell_pixbuf_info, 0);
108 return cell_pixbuf_type;
112 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
114 GtkCellRendererPixbufPrivate *priv;
116 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
117 priv->stock_size = GTK_ICON_SIZE_MENU;
121 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
123 GObjectClass *object_class = G_OBJECT_CLASS (class);
124 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
126 parent_class = g_type_class_peek_parent (class);
128 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
130 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
131 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
133 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
134 cell_class->render = gtk_cell_renderer_pixbuf_render;
136 g_object_class_install_property (object_class,
138 g_param_spec_object ("pixbuf",
140 P_("The pixbuf to render"),
142 GTK_PARAM_READWRITE));
144 g_object_class_install_property (object_class,
145 PROP_PIXBUF_EXPANDER_OPEN,
146 g_param_spec_object ("pixbuf-expander-open",
147 P_("Pixbuf Expander Open"),
148 P_("Pixbuf for open expander"),
150 GTK_PARAM_READWRITE));
152 g_object_class_install_property (object_class,
153 PROP_PIXBUF_EXPANDER_CLOSED,
154 g_param_spec_object ("pixbuf-expander-closed",
155 P_("Pixbuf Expander Closed"),
156 P_("Pixbuf for closed expander"),
158 GTK_PARAM_READWRITE));
160 g_object_class_install_property (object_class,
162 g_param_spec_string ("stock-id",
164 P_("The stock ID of the stock icon to render"),
166 GTK_PARAM_READWRITE));
168 g_object_class_install_property (object_class,
170 g_param_spec_uint ("stock-size",
172 P_("The GtkIconSize value that specifies the size of the rendered icon"),
176 GTK_PARAM_READWRITE));
178 g_object_class_install_property (object_class,
180 g_param_spec_string ("stock-detail",
182 P_("Render detail to pass to the theme engine"),
184 GTK_PARAM_READWRITE));
187 * GtkCellRendererPixbuf:follow-state:
189 * Specifies whether the rendered pixbuf should be colorized
190 * according to the #GtkCellRendererState.
194 g_object_class_install_property (object_class,
196 g_param_spec_boolean ("follow-state",
198 P_("Whether the rendered pixbuf should be "
199 "colorized according to the state"),
201 GTK_PARAM_READWRITE));
204 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
208 gtk_cell_renderer_pixbuf_finalize (GObject *object)
210 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
211 GtkCellRendererPixbufPrivate *priv;
213 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
215 if (cellpixbuf->pixbuf)
216 g_object_unref (cellpixbuf->pixbuf);
219 g_free (priv->stock_id);
221 if (priv->stock_detail)
222 g_free (priv->stock_detail);
224 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
228 gtk_cell_renderer_pixbuf_get_property (GObject *object,
233 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
234 GtkCellRendererPixbufPrivate *priv;
236 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
241 g_value_set_object (value,
242 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
244 case PROP_PIXBUF_EXPANDER_OPEN:
245 g_value_set_object (value,
246 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
248 case PROP_PIXBUF_EXPANDER_CLOSED:
249 g_value_set_object (value,
250 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
253 g_value_set_string (value, priv->stock_id);
255 case PROP_STOCK_SIZE:
256 g_value_set_uint (value, priv->stock_size);
258 case PROP_STOCK_DETAIL:
259 g_value_set_string (value, priv->stock_detail);
261 case PROP_FOLLOW_STATE:
262 g_value_set_boolean (value, priv->follow_state);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
272 gtk_cell_renderer_pixbuf_set_property (GObject *object,
278 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
279 GtkCellRendererPixbufPrivate *priv;
281 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
286 pixbuf = (GdkPixbuf*) g_value_get_object (value);
288 g_object_ref (pixbuf);
289 if (cellpixbuf->pixbuf)
290 g_object_unref (cellpixbuf->pixbuf);
291 cellpixbuf->pixbuf = pixbuf;
293 case PROP_PIXBUF_EXPANDER_OPEN:
294 pixbuf = (GdkPixbuf*) g_value_get_object (value);
296 g_object_ref (pixbuf);
297 if (cellpixbuf->pixbuf_expander_open)
298 g_object_unref (cellpixbuf->pixbuf_expander_open);
299 cellpixbuf->pixbuf_expander_open = pixbuf;
301 case PROP_PIXBUF_EXPANDER_CLOSED:
302 pixbuf = (GdkPixbuf*) g_value_get_object (value);
304 g_object_ref (pixbuf);
305 if (cellpixbuf->pixbuf_expander_closed)
306 g_object_unref (cellpixbuf->pixbuf_expander_closed);
307 cellpixbuf->pixbuf_expander_closed = pixbuf;
312 if (cellpixbuf->pixbuf)
314 g_object_unref (cellpixbuf->pixbuf);
315 cellpixbuf->pixbuf = NULL;
317 g_free (priv->stock_id);
319 priv->stock_id = g_strdup (g_value_get_string (value));
321 case PROP_STOCK_SIZE:
322 priv->stock_size = g_value_get_uint (value);
324 case PROP_STOCK_DETAIL:
325 if (priv->stock_detail)
326 g_free (priv->stock_detail);
327 priv->stock_detail = g_strdup (g_value_get_string (value));
329 case PROP_FOLLOW_STATE:
330 priv->follow_state = g_value_get_boolean (value);
333 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
337 if (cellpixbuf->pixbuf && priv->stock_id)
339 g_object_unref (cellpixbuf->pixbuf);
340 cellpixbuf->pixbuf = NULL;
345 * gtk_cell_renderer_pixbuf_new:
347 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
348 * parameters using object properties. Object properties can be set
349 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
350 * can bind a property to a value in a #GtkTreeModel. For example, you
351 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
352 * in the model, thus rendering a different image in each row of the
355 * Return value: the new cell renderer
358 gtk_cell_renderer_pixbuf_new (void)
360 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
364 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
367 GtkCellRendererPixbufPrivate *priv;
369 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
371 if (cellpixbuf->pixbuf)
372 g_object_unref (cellpixbuf->pixbuf);
374 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
381 create_colorized_pixbuf (GdkPixbuf *src,
385 gint width, height, has_alpha, src_row_stride, dst_row_stride;
386 gint red_value, green_value, blue_value;
387 guchar *target_pixels;
388 guchar *original_pixels;
393 red_value = new_color->red / 255.0;
394 green_value = new_color->green / 255.0;
395 blue_value = new_color->blue / 255.0;
397 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
398 gdk_pixbuf_get_has_alpha (src),
399 gdk_pixbuf_get_bits_per_sample (src),
400 gdk_pixbuf_get_width (src),
401 gdk_pixbuf_get_height (src));
403 has_alpha = gdk_pixbuf_get_has_alpha (src);
404 width = gdk_pixbuf_get_width (src);
405 height = gdk_pixbuf_get_height (src);
406 src_row_stride = gdk_pixbuf_get_rowstride (src);
407 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
408 target_pixels = gdk_pixbuf_get_pixels (dest);
409 original_pixels = gdk_pixbuf_get_pixels (src);
411 for (i = 0; i < height; i++) {
412 pixdest = target_pixels + i*dst_row_stride;
413 pixsrc = original_pixels + i*src_row_stride;
414 for (j = 0; j < width; j++) {
415 *pixdest++ = (*pixsrc++ * red_value) >> 8;
416 *pixdest++ = (*pixsrc++ * green_value) >> 8;
417 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
419 *pixdest++ = *pixsrc++;
428 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
430 GdkRectangle *cell_area,
436 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
437 GtkCellRendererPixbufPrivate *priv;
438 gint pixbuf_width = 0;
439 gint pixbuf_height = 0;
443 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
445 if (!cellpixbuf->pixbuf && priv->stock_id)
446 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
448 if (cellpixbuf->pixbuf)
450 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
451 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
453 if (cellpixbuf->pixbuf_expander_open)
455 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
456 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
458 if (cellpixbuf->pixbuf_expander_closed)
460 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
461 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
464 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
465 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
467 if (x_offset) *x_offset = 0;
468 if (y_offset) *y_offset = 0;
470 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
474 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
475 1.0 - cell->xalign : cell->xalign) *
476 (cell_area->width - calc_width - 2 * cell->xpad));
477 *x_offset = MAX (*x_offset, 0) + cell->xpad;
481 *y_offset = (cell->yalign *
482 (cell_area->height - calc_height - 2 * cell->ypad));
483 *y_offset = MAX (*y_offset, 0) + cell->ypad;
491 *height = calc_height;
495 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
498 GdkRectangle *background_area,
499 GdkRectangle *cell_area,
500 GdkRectangle *expose_area,
501 GtkCellRendererState flags)
504 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
505 GtkCellRendererPixbufPrivate *priv;
507 GdkPixbuf *invisible = NULL;
508 GdkPixbuf *colorized = NULL;
509 GdkRectangle pix_rect;
510 GdkRectangle draw_rect;
513 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
515 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
521 pix_rect.x += cell_area->x;
522 pix_rect.y += cell_area->y;
523 pix_rect.width -= cell->xpad * 2;
524 pix_rect.height -= cell->ypad * 2;
526 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
527 !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
530 pixbuf = cellpixbuf->pixbuf;
532 if (cell->is_expander)
534 if (cell->is_expanded &&
535 cellpixbuf->pixbuf_expander_open != NULL)
536 pixbuf = cellpixbuf->pixbuf_expander_open;
537 else if (!cell->is_expanded &&
538 cellpixbuf->pixbuf_expander_closed != NULL)
539 pixbuf = cellpixbuf->pixbuf_expander_closed;
545 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
547 GtkIconSource *source;
549 source = gtk_icon_source_new ();
550 gtk_icon_source_set_pixbuf (source, pixbuf);
551 /* The size here is arbitrary; since size isn't
552 * wildcarded in the source, it isn't supposed to be
553 * scaled by the engine function
555 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
556 gtk_icon_source_set_size_wildcarded (source, FALSE);
558 invisible = gtk_style_render_icon (widget->style,
560 gtk_widget_get_direction (widget),
561 GTK_STATE_INSENSITIVE,
565 "gtkcellrendererpixbuf");
567 gtk_icon_source_free (source);
571 else if (priv->follow_state &&
572 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
576 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
578 if (GTK_WIDGET_HAS_FOCUS (widget))
579 state = GTK_STATE_SELECTED;
581 state = GTK_STATE_ACTIVE;
584 state = GTK_STATE_PRELIGHT;
586 colorized = create_colorized_pixbuf (pixbuf,
587 &widget->style->base[state]);
592 cr = gdk_cairo_create (window);
594 gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
595 gdk_cairo_rectangle (cr, &draw_rect);
601 g_object_unref (invisible);
604 g_object_unref (colorized);
607 #define __GTK_CELL_RENDERER_PIXBUF_C__
608 #include "gtkaliasdef.c"