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;
65 #define CELLINFO_KEY "gtk-cell-renderer-pixbuf-info"
67 typedef struct _GtkCellRendererPixbufInfo GtkCellRendererPixbufInfo;
68 struct _GtkCellRendererPixbufInfo
71 GtkIconSize stock_size;
76 gtk_cell_renderer_pixbuf_get_type (void)
78 static GType cell_pixbuf_type = 0;
80 if (!cell_pixbuf_type)
82 static const GTypeInfo cell_pixbuf_info =
84 sizeof (GtkCellRendererPixbufClass),
86 NULL, /* base_finalize */
87 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
88 NULL, /* class_finalize */
89 NULL, /* class_data */
90 sizeof (GtkCellRendererPixbuf),
92 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
96 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf",
97 &cell_pixbuf_info, 0);
100 return cell_pixbuf_type;
104 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
106 GtkCellRendererPixbufInfo *cellinfo;
108 cellinfo = g_new0 (GtkCellRendererPixbufInfo, 1);
109 cellinfo->stock_size = GTK_ICON_SIZE_MENU;
110 g_object_set_data (G_OBJECT (cellpixbuf), CELLINFO_KEY, cellinfo);
114 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
116 GObjectClass *object_class = G_OBJECT_CLASS (class);
117 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
119 parent_class = g_type_class_peek_parent (class);
121 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
123 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
124 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
126 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
127 cell_class->render = gtk_cell_renderer_pixbuf_render;
129 g_object_class_install_property (object_class,
131 g_param_spec_object ("pixbuf",
133 _("The pixbuf to render"),
138 g_object_class_install_property (object_class,
139 PROP_PIXBUF_EXPANDER_OPEN,
140 g_param_spec_object ("pixbuf_expander_open",
141 _("Pixbuf Expander Open"),
142 _("Pixbuf for open expander"),
147 g_object_class_install_property (object_class,
148 PROP_PIXBUF_EXPANDER_CLOSED,
149 g_param_spec_object ("pixbuf_expander_closed",
150 _("Pixbuf Expander Closed"),
151 _("Pixbuf for closed expander"),
156 g_object_class_install_property (object_class,
158 g_param_spec_string ("stock_id",
160 _("The stock ID of the stock icon to render"),
164 g_object_class_install_property (object_class,
166 g_param_spec_enum ("stock_size",
168 _("The size of the rendered icon"),
173 g_object_class_install_property (object_class,
175 g_param_spec_string ("stock_detail",
177 _("Render detail to pass to the theme engine"),
183 gtk_cell_renderer_pixbuf_finalize (GObject *object)
185 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
186 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
188 if (cellpixbuf->pixbuf && cellinfo->stock_id)
189 g_object_unref (cellpixbuf->pixbuf);
191 if (cellinfo->stock_id)
192 g_free (cellinfo->stock_id);
194 if (cellinfo->stock_detail)
195 g_free (cellinfo->stock_detail);
198 g_object_set_data (object, CELLINFO_KEY, NULL);
200 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
204 gtk_cell_renderer_pixbuf_get_property (GObject *object,
209 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
210 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
215 g_value_set_object (value,
216 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
218 case PROP_PIXBUF_EXPANDER_OPEN:
219 g_value_set_object (value,
220 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
222 case PROP_PIXBUF_EXPANDER_CLOSED:
223 g_value_set_object (value,
224 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
227 g_value_set_string (value, cellinfo->stock_id);
229 case PROP_STOCK_SIZE:
230 g_value_set_enum (value, cellinfo->stock_size);
232 case PROP_STOCK_DETAIL:
233 g_value_set_string (value, cellinfo->stock_detail);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243 gtk_cell_renderer_pixbuf_set_property (GObject *object,
249 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
250 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
255 pixbuf = (GdkPixbuf*) g_value_get_object (value);
257 g_object_ref (pixbuf);
258 if (cellpixbuf->pixbuf)
259 g_object_unref (cellpixbuf->pixbuf);
260 cellpixbuf->pixbuf = pixbuf;
262 case PROP_PIXBUF_EXPANDER_OPEN:
263 pixbuf = (GdkPixbuf*) g_value_get_object (value);
265 g_object_ref (pixbuf);
266 if (cellpixbuf->pixbuf_expander_open)
267 g_object_unref (cellpixbuf->pixbuf_expander_open);
268 cellpixbuf->pixbuf_expander_open = pixbuf;
270 case PROP_PIXBUF_EXPANDER_CLOSED:
271 pixbuf = (GdkPixbuf*) g_value_get_object (value);
273 g_object_ref (pixbuf);
274 if (cellpixbuf->pixbuf_expander_closed)
275 g_object_unref (cellpixbuf->pixbuf_expander_closed);
276 cellpixbuf->pixbuf_expander_closed = pixbuf;
279 if (cellinfo->stock_id)
280 g_free (cellinfo->stock_id);
281 cellinfo->stock_id = g_strdup (g_value_get_string (value));
282 g_object_notify (G_OBJECT (object), "stock_id");
284 case PROP_STOCK_SIZE:
285 cellinfo->stock_size = g_value_get_enum (value);
286 g_object_notify (G_OBJECT (object), "stock_size");
288 case PROP_STOCK_DETAIL:
289 if (cellinfo->stock_detail)
290 g_free (cellinfo->stock_detail);
291 cellinfo->stock_detail = g_strdup (g_value_get_string (value));
292 g_object_notify (G_OBJECT (object), "stock_detail");
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
299 if (cellpixbuf->pixbuf && cellinfo->stock_id)
301 g_object_unref (cellpixbuf->pixbuf);
302 cellpixbuf->pixbuf = NULL;
307 * gtk_cell_renderer_pixbuf_new:
309 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
310 * parameters using object properties. Object properties can be set
311 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
312 * can bind a property to a value in a #GtkTreeModel. For example, you
313 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
314 * in the model, thus rendering a different image in each row of the
317 * Return value: the new cell renderer
320 gtk_cell_renderer_pixbuf_new (void)
322 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
326 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
329 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cellpixbuf), CELLINFO_KEY);
331 if (cellpixbuf->pixbuf)
332 g_object_unref (cellpixbuf->pixbuf);
334 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
336 cellinfo->stock_size,
337 cellinfo->stock_detail);
341 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
343 GdkRectangle *cell_area,
349 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
350 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
351 gint pixbuf_width = 0;
352 gint pixbuf_height = 0;
356 if (!cellpixbuf->pixbuf && cellinfo->stock_id)
357 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
359 if (cellpixbuf->pixbuf)
361 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
362 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
364 if (cellpixbuf->pixbuf_expander_open)
366 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
367 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
369 if (cellpixbuf->pixbuf_expander_closed)
371 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
372 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
375 calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
376 calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
378 if (x_offset) *x_offset = 0;
379 if (y_offset) *y_offset = 0;
381 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
385 *x_offset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - calc_width - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
386 *x_offset = MAX (*x_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
390 *y_offset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - calc_height - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
391 *y_offset = MAX (*y_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
399 *height = calc_height;
403 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
406 GdkRectangle *background_area,
407 GdkRectangle *cell_area,
408 GdkRectangle *expose_area,
409 GtkCellRendererState flags)
412 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
413 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
415 GdkRectangle pix_rect;
416 GdkRectangle draw_rect;
417 gboolean stock_pixbuf = FALSE;
419 pixbuf = cellpixbuf->pixbuf;
420 if (cell->is_expander)
422 if (cell->is_expanded &&
423 cellpixbuf->pixbuf_expander_open != NULL)
424 pixbuf = cellpixbuf->pixbuf_expander_open;
425 else if (! cell->is_expanded &&
426 cellpixbuf->pixbuf_expander_closed != NULL)
427 pixbuf = cellpixbuf->pixbuf_expander_closed;
430 if (!pixbuf && !cellinfo->stock_id)
432 else if (!pixbuf && cellinfo->stock_id)
435 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
442 pixbuf = cellpixbuf->pixbuf;
444 pix_rect.x += cell_area->x;
445 pix_rect.y += cell_area->y;
446 pix_rect.width -= cell->xpad * 2;
447 pix_rect.height -= cell->ypad * 2;
449 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
450 gdk_draw_pixbuf (window,
451 widget->style->black_gc,
453 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
454 draw_rect.x - pix_rect.x,
455 draw_rect.y - pix_rect.y,
460 GDK_RGB_DITHER_NORMAL,