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"
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 #define STATIC_STRINGS G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
137 g_object_class_install_property (object_class,
139 g_param_spec_object ("pixbuf",
141 P_("The pixbuf to render"),
143 G_PARAM_READWRITE | STATIC_STRINGS));
145 g_object_class_install_property (object_class,
146 PROP_PIXBUF_EXPANDER_OPEN,
147 g_param_spec_object ("pixbuf-expander-open",
148 P_("Pixbuf Expander Open"),
149 P_("Pixbuf for open expander"),
151 G_PARAM_READWRITE | STATIC_STRINGS));
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"),
159 G_PARAM_READWRITE | STATIC_STRINGS));
161 g_object_class_install_property (object_class,
163 g_param_spec_string ("stock-id",
165 P_("The stock ID of the stock icon to render"),
167 G_PARAM_READWRITE | STATIC_STRINGS));
169 g_object_class_install_property (object_class,
171 g_param_spec_uint ("stock-size",
173 P_("The GtkIconSize value that specifies the size of the rendered icon"),
177 G_PARAM_READWRITE | STATIC_STRINGS));
179 g_object_class_install_property (object_class,
181 g_param_spec_string ("stock-detail",
183 P_("Render detail to pass to the theme engine"),
185 G_PARAM_READWRITE | STATIC_STRINGS));
188 * GtkCellRendererPixbuf:follow-state:
190 * Specifies whether the rendered pixbuf should be colorized
191 * according to the #GtkCellRendererState.
195 g_object_class_install_property (object_class,
197 g_param_spec_boolean ("follow-state",
199 P_("Whether the rendered pixbuf should be "
200 "colorized according to the state"),
202 G_PARAM_READWRITE | STATIC_STRINGS));
205 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
209 gtk_cell_renderer_pixbuf_finalize (GObject *object)
211 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
212 GtkCellRendererPixbufPrivate *priv;
214 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
216 if (cellpixbuf->pixbuf)
217 g_object_unref (cellpixbuf->pixbuf);
220 g_free (priv->stock_id);
222 if (priv->stock_detail)
223 g_free (priv->stock_detail);
225 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
229 gtk_cell_renderer_pixbuf_get_property (GObject *object,
234 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
235 GtkCellRendererPixbufPrivate *priv;
237 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
242 g_value_set_object (value,
243 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
245 case PROP_PIXBUF_EXPANDER_OPEN:
246 g_value_set_object (value,
247 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
249 case PROP_PIXBUF_EXPANDER_CLOSED:
250 g_value_set_object (value,
251 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
254 g_value_set_string (value, priv->stock_id);
256 case PROP_STOCK_SIZE:
257 g_value_set_uint (value, priv->stock_size);
259 case PROP_STOCK_DETAIL:
260 g_value_set_string (value, priv->stock_detail);
262 case PROP_FOLLOW_STATE:
263 g_value_set_boolean (value, priv->follow_state);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
273 gtk_cell_renderer_pixbuf_set_property (GObject *object,
279 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
280 GtkCellRendererPixbufPrivate *priv;
282 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
287 pixbuf = (GdkPixbuf*) g_value_get_object (value);
289 g_object_ref (pixbuf);
290 if (cellpixbuf->pixbuf)
291 g_object_unref (cellpixbuf->pixbuf);
292 cellpixbuf->pixbuf = pixbuf;
294 case PROP_PIXBUF_EXPANDER_OPEN:
295 pixbuf = (GdkPixbuf*) g_value_get_object (value);
297 g_object_ref (pixbuf);
298 if (cellpixbuf->pixbuf_expander_open)
299 g_object_unref (cellpixbuf->pixbuf_expander_open);
300 cellpixbuf->pixbuf_expander_open = pixbuf;
302 case PROP_PIXBUF_EXPANDER_CLOSED:
303 pixbuf = (GdkPixbuf*) g_value_get_object (value);
305 g_object_ref (pixbuf);
306 if (cellpixbuf->pixbuf_expander_closed)
307 g_object_unref (cellpixbuf->pixbuf_expander_closed);
308 cellpixbuf->pixbuf_expander_closed = pixbuf;
313 if (cellpixbuf->pixbuf)
315 g_object_unref (cellpixbuf->pixbuf);
316 cellpixbuf->pixbuf = NULL;
318 g_free (priv->stock_id);
320 priv->stock_id = g_strdup (g_value_get_string (value));
322 case PROP_STOCK_SIZE:
323 priv->stock_size = g_value_get_uint (value);
325 case PROP_STOCK_DETAIL:
326 if (priv->stock_detail)
327 g_free (priv->stock_detail);
328 priv->stock_detail = g_strdup (g_value_get_string (value));
330 case PROP_FOLLOW_STATE:
331 priv->follow_state = g_value_get_boolean (value);
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
338 if (cellpixbuf->pixbuf && priv->stock_id)
340 g_object_unref (cellpixbuf->pixbuf);
341 cellpixbuf->pixbuf = NULL;
346 * gtk_cell_renderer_pixbuf_new:
348 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
349 * parameters using object properties. Object properties can be set
350 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
351 * can bind a property to a value in a #GtkTreeModel. For example, you
352 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
353 * in the model, thus rendering a different image in each row of the
356 * Return value: the new cell renderer
359 gtk_cell_renderer_pixbuf_new (void)
361 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
365 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
368 GtkCellRendererPixbufPrivate *priv;
370 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
372 if (cellpixbuf->pixbuf)
373 g_object_unref (cellpixbuf->pixbuf);
375 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
382 create_colorized_pixbuf (GdkPixbuf *src,
386 gint width, height, has_alpha, src_row_stride, dst_row_stride;
387 gint red_value, green_value, blue_value;
388 guchar *target_pixels;
389 guchar *original_pixels;
394 red_value = new_color->red / 255.0;
395 green_value = new_color->green / 255.0;
396 blue_value = new_color->blue / 255.0;
398 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
399 gdk_pixbuf_get_has_alpha (src),
400 gdk_pixbuf_get_bits_per_sample (src),
401 gdk_pixbuf_get_width (src),
402 gdk_pixbuf_get_height (src));
404 has_alpha = gdk_pixbuf_get_has_alpha (src);
405 width = gdk_pixbuf_get_width (src);
406 height = gdk_pixbuf_get_height (src);
407 src_row_stride = gdk_pixbuf_get_rowstride (src);
408 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
409 target_pixels = gdk_pixbuf_get_pixels (dest);
410 original_pixels = gdk_pixbuf_get_pixels (src);
412 for (i = 0; i < height; i++) {
413 pixdest = target_pixels + i*dst_row_stride;
414 pixsrc = original_pixels + i*src_row_stride;
415 for (j = 0; j < width; j++) {
416 *pixdest++ = (*pixsrc++ * red_value) >> 8;
417 *pixdest++ = (*pixsrc++ * green_value) >> 8;
418 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
420 *pixdest++ = *pixsrc++;
429 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
431 GdkRectangle *cell_area,
437 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
438 GtkCellRendererPixbufPrivate *priv;
439 gint pixbuf_width = 0;
440 gint pixbuf_height = 0;
444 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
446 if (!cellpixbuf->pixbuf && priv->stock_id)
447 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
449 if (cellpixbuf->pixbuf)
451 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
452 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
454 if (cellpixbuf->pixbuf_expander_open)
456 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
457 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
459 if (cellpixbuf->pixbuf_expander_closed)
461 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
462 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
465 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
466 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
468 if (x_offset) *x_offset = 0;
469 if (y_offset) *y_offset = 0;
471 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
475 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
476 1.0 - cell->xalign : cell->xalign) *
477 (cell_area->width - calc_width - 2 * cell->xpad));
478 *x_offset = MAX (*x_offset, 0) + cell->xpad;
482 *y_offset = (cell->yalign *
483 (cell_area->height - calc_height - 2 * cell->ypad));
484 *y_offset = MAX (*y_offset, 0) + cell->ypad;
492 *height = calc_height;
496 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
499 GdkRectangle *background_area,
500 GdkRectangle *cell_area,
501 GdkRectangle *expose_area,
502 GtkCellRendererState flags)
505 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
506 GtkCellRendererPixbufPrivate *priv;
508 GdkPixbuf *invisible = NULL;
509 GdkPixbuf *colorized = NULL;
510 GdkRectangle pix_rect;
511 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 gdk_draw_pixbuf (window,
593 widget->style->black_gc,
595 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
596 draw_rect.x - pix_rect.x,
597 draw_rect.y - pix_rect.y,
602 GDK_RGB_DITHER_NORMAL,
606 g_object_unref (invisible);
609 g_object_unref (colorized);
612 #define __GTK_CELL_RENDERER_PIXBUF_C__
613 #include "gtkaliasdef.c"