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.
23 #include "gtkcellrendererpixbuf.h"
24 #include "gtkiconfactory.h"
27 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
31 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
35 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
36 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
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 GdkRectangle *rectangle,
47 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
50 GdkRectangle *background_area,
51 GdkRectangle *cell_area,
52 GdkRectangle *expose_area,
53 GtkCellRendererState flags);
59 PROP_PIXBUF_EXPANDER_OPEN,
60 PROP_PIXBUF_EXPANDER_CLOSED,
67 static gpointer parent_class;
70 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
72 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
73 struct _GtkCellRendererPixbufPrivate
76 GtkIconSize stock_size;
78 gboolean follow_state;
83 gtk_cell_renderer_pixbuf_get_type (void)
85 static GType cell_pixbuf_type = 0;
87 if (!cell_pixbuf_type)
89 static const GTypeInfo cell_pixbuf_info =
91 sizeof (GtkCellRendererPixbufClass),
93 NULL, /* base_finalize */
94 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
95 NULL, /* class_finalize */
96 NULL, /* class_data */
97 sizeof (GtkCellRendererPixbuf),
99 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
103 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf",
104 &cell_pixbuf_info, 0);
107 return cell_pixbuf_type;
111 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
113 GtkCellRendererPixbufPrivate *priv;
115 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
116 priv->stock_size = GTK_ICON_SIZE_MENU;
120 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
122 GObjectClass *object_class = G_OBJECT_CLASS (class);
123 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
125 parent_class = g_type_class_peek_parent (class);
127 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
129 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
130 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
132 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
133 cell_class->render = gtk_cell_renderer_pixbuf_render;
135 g_object_class_install_property (object_class,
137 g_param_spec_object ("pixbuf",
139 P_("The pixbuf to render"),
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"),
153 g_object_class_install_property (object_class,
154 PROP_PIXBUF_EXPANDER_CLOSED,
155 g_param_spec_object ("pixbuf-expander-closed",
156 P_("Pixbuf Expander Closed"),
157 P_("Pixbuf for closed expander"),
162 g_object_class_install_property (object_class,
164 g_param_spec_string ("stock-id",
166 P_("The stock ID of the stock icon to render"),
170 g_object_class_install_property (object_class,
172 g_param_spec_uint ("stock-size",
174 P_("The GtkIconSize value that specifies the size of the rendered icon"),
180 g_object_class_install_property (object_class,
182 g_param_spec_string ("stock-detail",
184 P_("Render detail to pass to the theme engine"),
189 * GtkCellRendererPixbuf:follow-state:
191 * Specifies whether the rendered pixbuf should be colorized
192 * according to the #GtkCellRendererState.
196 g_object_class_install_property (object_class,
198 g_param_spec_boolean ("follow-state",
200 P_("Whether the rendered pixbuf should be "
201 "colorized according to the state"),
206 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
210 gtk_cell_renderer_pixbuf_finalize (GObject *object)
212 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
213 GtkCellRendererPixbufPrivate *priv;
215 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
217 if (cellpixbuf->pixbuf)
218 g_object_unref (cellpixbuf->pixbuf);
221 g_free (priv->stock_id);
223 if (priv->stock_detail)
224 g_free (priv->stock_detail);
226 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
230 gtk_cell_renderer_pixbuf_get_property (GObject *object,
235 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
236 GtkCellRendererPixbufPrivate *priv;
238 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
243 g_value_set_object (value,
244 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
246 case PROP_PIXBUF_EXPANDER_OPEN:
247 g_value_set_object (value,
248 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
250 case PROP_PIXBUF_EXPANDER_CLOSED:
251 g_value_set_object (value,
252 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
255 g_value_set_string (value, priv->stock_id);
257 case PROP_STOCK_SIZE:
258 g_value_set_uint (value, priv->stock_size);
260 case PROP_STOCK_DETAIL:
261 g_value_set_string (value, priv->stock_detail);
263 case PROP_FOLLOW_STATE:
264 g_value_set_boolean (value, priv->follow_state);
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
274 gtk_cell_renderer_pixbuf_set_property (GObject *object,
280 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
281 GtkCellRendererPixbufPrivate *priv;
283 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
288 pixbuf = (GdkPixbuf*) g_value_get_object (value);
290 g_object_ref (pixbuf);
291 if (cellpixbuf->pixbuf)
292 g_object_unref (cellpixbuf->pixbuf);
293 cellpixbuf->pixbuf = pixbuf;
295 case PROP_PIXBUF_EXPANDER_OPEN:
296 pixbuf = (GdkPixbuf*) g_value_get_object (value);
298 g_object_ref (pixbuf);
299 if (cellpixbuf->pixbuf_expander_open)
300 g_object_unref (cellpixbuf->pixbuf_expander_open);
301 cellpixbuf->pixbuf_expander_open = pixbuf;
303 case PROP_PIXBUF_EXPANDER_CLOSED:
304 pixbuf = (GdkPixbuf*) g_value_get_object (value);
306 g_object_ref (pixbuf);
307 if (cellpixbuf->pixbuf_expander_closed)
308 g_object_unref (cellpixbuf->pixbuf_expander_closed);
309 cellpixbuf->pixbuf_expander_closed = pixbuf;
314 if (cellpixbuf->pixbuf)
316 g_object_unref (cellpixbuf->pixbuf);
317 cellpixbuf->pixbuf = NULL;
319 g_free (priv->stock_id);
321 priv->stock_id = g_strdup (g_value_get_string (value));
323 case PROP_STOCK_SIZE:
324 priv->stock_size = g_value_get_uint (value);
326 case PROP_STOCK_DETAIL:
327 if (priv->stock_detail)
328 g_free (priv->stock_detail);
329 priv->stock_detail = g_strdup (g_value_get_string (value));
331 case PROP_FOLLOW_STATE:
332 priv->follow_state = g_value_get_boolean (value);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
339 if (cellpixbuf->pixbuf && priv->stock_id)
341 g_object_unref (cellpixbuf->pixbuf);
342 cellpixbuf->pixbuf = NULL;
347 * gtk_cell_renderer_pixbuf_new:
349 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
350 * parameters using object properties. Object properties can be set
351 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
352 * can bind a property to a value in a #GtkTreeModel. For example, you
353 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
354 * in the model, thus rendering a different image in each row of the
357 * Return value: the new cell renderer
360 gtk_cell_renderer_pixbuf_new (void)
362 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
366 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
369 GtkCellRendererPixbufPrivate *priv;
371 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
373 if (cellpixbuf->pixbuf)
374 g_object_unref (cellpixbuf->pixbuf);
376 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
383 create_colorized_pixbuf (GdkPixbuf *src,
387 gint width, height, has_alpha, src_row_stride, dst_row_stride;
388 gint red_value, green_value, blue_value;
389 guchar *target_pixels;
390 guchar *original_pixels;
395 red_value = new_color->red / 255.0;
396 green_value = new_color->green / 255.0;
397 blue_value = new_color->blue / 255.0;
399 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
400 gdk_pixbuf_get_has_alpha (src),
401 gdk_pixbuf_get_bits_per_sample (src),
402 gdk_pixbuf_get_width (src),
403 gdk_pixbuf_get_height (src));
405 has_alpha = gdk_pixbuf_get_has_alpha (src);
406 width = gdk_pixbuf_get_width (src);
407 height = gdk_pixbuf_get_height (src);
408 src_row_stride = gdk_pixbuf_get_rowstride (src);
409 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
410 target_pixels = gdk_pixbuf_get_pixels (dest);
411 original_pixels = gdk_pixbuf_get_pixels (src);
413 for (i = 0; i < height; i++) {
414 pixdest = target_pixels + i*dst_row_stride;
415 pixsrc = original_pixels + i*src_row_stride;
416 for (j = 0; j < width; j++) {
417 *pixdest++ = (*pixsrc++ * red_value) >> 8;
418 *pixdest++ = (*pixsrc++ * green_value) >> 8;
419 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
421 *pixdest++ = *pixsrc++;
430 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
432 GdkRectangle *cell_area,
438 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
439 GtkCellRendererPixbufPrivate *priv;
440 gint pixbuf_width = 0;
441 gint pixbuf_height = 0;
445 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
447 if (!cellpixbuf->pixbuf && priv->stock_id)
448 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
450 if (cellpixbuf->pixbuf)
452 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
453 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
455 if (cellpixbuf->pixbuf_expander_open)
457 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
458 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
460 if (cellpixbuf->pixbuf_expander_closed)
462 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
463 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
466 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
467 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
469 if (x_offset) *x_offset = 0;
470 if (y_offset) *y_offset = 0;
472 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
476 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
477 1.0 - cell->xalign : cell->xalign) *
478 (cell_area->width - calc_width - 2 * cell->xpad));
479 *x_offset = MAX (*x_offset, 0) + cell->xpad;
483 *y_offset = (cell->yalign *
484 (cell_area->height - calc_height - 2 * cell->ypad));
485 *y_offset = MAX (*y_offset, 0) + cell->ypad;
493 *height = calc_height;
497 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
500 GdkRectangle *background_area,
501 GdkRectangle *cell_area,
502 GdkRectangle *expose_area,
503 GtkCellRendererState flags)
506 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
507 GtkCellRendererPixbufPrivate *priv;
509 GdkPixbuf *invisible = NULL;
510 GdkPixbuf *colorized = NULL;
511 GdkRectangle pix_rect;
512 GdkRectangle draw_rect;
514 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
516 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
522 pix_rect.x += cell_area->x;
523 pix_rect.y += cell_area->y;
524 pix_rect.width -= cell->xpad * 2;
525 pix_rect.height -= cell->ypad * 2;
527 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
528 !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
531 pixbuf = cellpixbuf->pixbuf;
533 if (cell->is_expander)
535 if (cell->is_expanded &&
536 cellpixbuf->pixbuf_expander_open != NULL)
537 pixbuf = cellpixbuf->pixbuf_expander_open;
538 else if (!cell->is_expanded &&
539 cellpixbuf->pixbuf_expander_closed != NULL)
540 pixbuf = cellpixbuf->pixbuf_expander_closed;
546 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
548 GtkIconSource *source;
550 source = gtk_icon_source_new ();
551 gtk_icon_source_set_pixbuf (source, pixbuf);
552 /* The size here is arbitrary; since size isn't
553 * wildcarded in the source, it isn't supposed to be
554 * scaled by the engine function
556 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
557 gtk_icon_source_set_size_wildcarded (source, FALSE);
559 invisible = gtk_style_render_icon (widget->style,
561 gtk_widget_get_direction (widget),
562 GTK_STATE_INSENSITIVE,
566 "gtkcellrendererpixbuf");
568 gtk_icon_source_free (source);
572 else if (priv->follow_state &&
573 (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
577 if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
579 if (GTK_WIDGET_HAS_FOCUS (widget))
580 state = GTK_STATE_SELECTED;
582 state = GTK_STATE_ACTIVE;
585 state = GTK_STATE_PRELIGHT;
587 colorized = create_colorized_pixbuf (pixbuf,
588 &widget->style->base[state]);
593 gdk_draw_pixbuf (window,
594 widget->style->black_gc,
596 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
597 draw_rect.x - pix_rect.x,
598 draw_rect.y - pix_rect.y,
603 GDK_RGB_DITHER_NORMAL,
607 g_object_unref (invisible);
610 g_object_unref (colorized);