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.
21 #include "gtkcellrendererpixbuf.h"
24 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
28 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
32 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
33 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
34 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
35 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
37 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
39 GdkRectangle *rectangle,
44 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
47 GdkRectangle *background_area,
48 GdkRectangle *cell_area,
49 GdkRectangle *expose_area,
50 GtkCellRendererState flags);
56 PROP_PIXBUF_EXPANDER_OPEN,
57 PROP_PIXBUF_EXPANDER_CLOSED,
63 static gpointer parent_class;
66 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
68 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
69 struct _GtkCellRendererPixbufPrivate
72 GtkIconSize stock_size;
78 gtk_cell_renderer_pixbuf_get_type (void)
80 static GType cell_pixbuf_type = 0;
82 if (!cell_pixbuf_type)
84 static const GTypeInfo cell_pixbuf_info =
86 sizeof (GtkCellRendererPixbufClass),
88 NULL, /* base_finalize */
89 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
90 NULL, /* class_finalize */
91 NULL, /* class_data */
92 sizeof (GtkCellRendererPixbuf),
94 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
98 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf",
99 &cell_pixbuf_info, 0);
102 return cell_pixbuf_type;
106 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
108 GtkCellRendererPixbufPrivate *priv;
110 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
111 priv->stock_size = GTK_ICON_SIZE_MENU;
115 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
117 GObjectClass *object_class = G_OBJECT_CLASS (class);
118 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
120 parent_class = g_type_class_peek_parent (class);
122 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
124 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
125 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
127 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
128 cell_class->render = gtk_cell_renderer_pixbuf_render;
130 g_object_class_install_property (object_class,
132 g_param_spec_object ("pixbuf",
134 _("The pixbuf to render"),
139 g_object_class_install_property (object_class,
140 PROP_PIXBUF_EXPANDER_OPEN,
141 g_param_spec_object ("pixbuf_expander_open",
142 _("Pixbuf Expander Open"),
143 _("Pixbuf for open expander"),
148 g_object_class_install_property (object_class,
149 PROP_PIXBUF_EXPANDER_CLOSED,
150 g_param_spec_object ("pixbuf_expander_closed",
151 _("Pixbuf Expander Closed"),
152 _("Pixbuf for closed expander"),
157 g_object_class_install_property (object_class,
159 g_param_spec_string ("stock_id",
161 _("The stock ID of the stock icon to render"),
165 g_object_class_install_property (object_class,
167 g_param_spec_enum ("stock_size",
169 _("The size of the rendered icon"),
174 g_object_class_install_property (object_class,
176 g_param_spec_string ("stock_detail",
178 _("Render detail to pass to the theme engine"),
182 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
186 gtk_cell_renderer_pixbuf_finalize (GObject *object)
188 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
189 GtkCellRendererPixbufPrivate *priv;
191 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
193 if (cellpixbuf->pixbuf && priv->stock_id)
194 g_object_unref (cellpixbuf->pixbuf);
197 g_free (priv->stock_id);
199 if (priv->stock_detail)
200 g_free (priv->stock_detail);
202 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
206 gtk_cell_renderer_pixbuf_get_property (GObject *object,
211 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
212 GtkCellRendererPixbufPrivate *priv;
214 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
219 g_value_set_object (value,
220 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
222 case PROP_PIXBUF_EXPANDER_OPEN:
223 g_value_set_object (value,
224 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
226 case PROP_PIXBUF_EXPANDER_CLOSED:
227 g_value_set_object (value,
228 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
231 g_value_set_string (value, priv->stock_id);
233 case PROP_STOCK_SIZE:
234 g_value_set_enum (value, priv->stock_size);
236 case PROP_STOCK_DETAIL:
237 g_value_set_string (value, priv->stock_detail);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
247 gtk_cell_renderer_pixbuf_set_property (GObject *object,
253 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
254 GtkCellRendererPixbufPrivate *priv;
256 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
261 pixbuf = (GdkPixbuf*) g_value_get_object (value);
263 g_object_ref (pixbuf);
264 if (cellpixbuf->pixbuf)
265 g_object_unref (cellpixbuf->pixbuf);
266 cellpixbuf->pixbuf = pixbuf;
268 case PROP_PIXBUF_EXPANDER_OPEN:
269 pixbuf = (GdkPixbuf*) g_value_get_object (value);
271 g_object_ref (pixbuf);
272 if (cellpixbuf->pixbuf_expander_open)
273 g_object_unref (cellpixbuf->pixbuf_expander_open);
274 cellpixbuf->pixbuf_expander_open = pixbuf;
276 case PROP_PIXBUF_EXPANDER_CLOSED:
277 pixbuf = (GdkPixbuf*) g_value_get_object (value);
279 g_object_ref (pixbuf);
280 if (cellpixbuf->pixbuf_expander_closed)
281 g_object_unref (cellpixbuf->pixbuf_expander_closed);
282 cellpixbuf->pixbuf_expander_closed = pixbuf;
287 if (cellpixbuf->pixbuf)
289 g_object_unref (cellpixbuf->pixbuf);
290 cellpixbuf->pixbuf = NULL;
292 g_free (priv->stock_id);
294 priv->stock_id = g_strdup (g_value_get_string (value));
296 case PROP_STOCK_SIZE:
297 priv->stock_size = g_value_get_enum (value);
299 case PROP_STOCK_DETAIL:
300 if (priv->stock_detail)
301 g_free (priv->stock_detail);
302 priv->stock_detail = g_strdup (g_value_get_string (value));
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
309 if (cellpixbuf->pixbuf && priv->stock_id)
311 g_object_unref (cellpixbuf->pixbuf);
312 cellpixbuf->pixbuf = NULL;
317 * gtk_cell_renderer_pixbuf_new:
319 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
320 * parameters using object properties. Object properties can be set
321 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
322 * can bind a property to a value in a #GtkTreeModel. For example, you
323 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
324 * in the model, thus rendering a different image in each row of the
327 * Return value: the new cell renderer
330 gtk_cell_renderer_pixbuf_new (void)
332 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
336 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
339 GtkCellRendererPixbufPrivate *priv;
341 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
343 if (cellpixbuf->pixbuf)
344 g_object_unref (cellpixbuf->pixbuf);
346 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
353 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
355 GdkRectangle *cell_area,
361 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
362 GtkCellRendererPixbufPrivate *priv;
363 gint pixbuf_width = 0;
364 gint pixbuf_height = 0;
368 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
370 if (!cellpixbuf->pixbuf && priv->stock_id)
371 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
373 if (cellpixbuf->pixbuf)
375 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
376 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
378 if (cellpixbuf->pixbuf_expander_open)
380 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
381 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
383 if (cellpixbuf->pixbuf_expander_closed)
385 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
386 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
389 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
390 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
392 if (x_offset) *x_offset = 0;
393 if (y_offset) *y_offset = 0;
395 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
399 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
400 1.0 - cell->xalign : cell->xalign) *
401 (cell_area->width - calc_width - 2 * cell->xpad));
402 *x_offset = MAX (*x_offset, 0) + cell->xpad;
406 *y_offset = (cell->yalign *
407 (cell_area->height - calc_height - 2 * cell->ypad));
408 *y_offset = MAX (*y_offset, 0) + cell->ypad;
416 *height = calc_height;
420 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
423 GdkRectangle *background_area,
424 GdkRectangle *cell_area,
425 GdkRectangle *expose_area,
426 GtkCellRendererState flags)
429 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
430 GtkCellRendererPixbufPrivate *priv;
432 GdkRectangle pix_rect;
433 GdkRectangle draw_rect;
434 gboolean stock_pixbuf = FALSE;
436 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
438 pixbuf = cellpixbuf->pixbuf;
439 if (cell->is_expander)
441 if (cell->is_expanded &&
442 cellpixbuf->pixbuf_expander_open != NULL)
443 pixbuf = cellpixbuf->pixbuf_expander_open;
444 else if (! cell->is_expanded &&
445 cellpixbuf->pixbuf_expander_closed != NULL)
446 pixbuf = cellpixbuf->pixbuf_expander_closed;
449 if (!pixbuf && !priv->stock_id)
451 else if (!pixbuf && priv->stock_id)
454 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
461 pixbuf = cellpixbuf->pixbuf;
463 pix_rect.x += cell_area->x;
464 pix_rect.y += cell_area->y;
465 pix_rect.width -= cell->xpad * 2;
466 pix_rect.height -= cell->ypad * 2;
468 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
469 gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
470 gdk_draw_pixbuf (window,
471 widget->style->black_gc,
473 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
474 draw_rect.x - pix_rect.x,
475 draw_rect.y - pix_rect.y,
480 GDK_RGB_DITHER_NORMAL,