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)
281 if (cellpixbuf->pixbuf)
283 g_object_unref (cellpixbuf->pixbuf);
284 cellpixbuf->pixbuf = NULL;
286 g_free (cellinfo->stock_id);
288 cellinfo->stock_id = g_strdup (g_value_get_string (value));
290 case PROP_STOCK_SIZE:
291 cellinfo->stock_size = g_value_get_enum (value);
293 case PROP_STOCK_DETAIL:
294 if (cellinfo->stock_detail)
295 g_free (cellinfo->stock_detail);
296 cellinfo->stock_detail = g_strdup (g_value_get_string (value));
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
303 if (cellpixbuf->pixbuf && cellinfo->stock_id)
305 g_object_unref (cellpixbuf->pixbuf);
306 cellpixbuf->pixbuf = NULL;
311 * gtk_cell_renderer_pixbuf_new:
313 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
314 * parameters using object properties. Object properties can be set
315 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
316 * can bind a property to a value in a #GtkTreeModel. For example, you
317 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
318 * in the model, thus rendering a different image in each row of the
321 * Return value: the new cell renderer
324 gtk_cell_renderer_pixbuf_new (void)
326 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
330 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
333 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cellpixbuf), CELLINFO_KEY);
335 if (cellpixbuf->pixbuf)
336 g_object_unref (cellpixbuf->pixbuf);
338 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
340 cellinfo->stock_size,
341 cellinfo->stock_detail);
345 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
347 GdkRectangle *cell_area,
353 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
354 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
355 gint pixbuf_width = 0;
356 gint pixbuf_height = 0;
360 if (!cellpixbuf->pixbuf && cellinfo->stock_id)
361 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
363 if (cellpixbuf->pixbuf)
365 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
366 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
368 if (cellpixbuf->pixbuf_expander_open)
370 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
371 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
373 if (cellpixbuf->pixbuf_expander_closed)
375 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
376 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
379 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
380 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
382 if (x_offset) *x_offset = 0;
383 if (y_offset) *y_offset = 0;
385 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
389 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
390 1.0 - cell->xalign : cell->xalign) *
391 (cell_area->width - calc_width - 2 * cell->xpad));
392 *x_offset = MAX (*x_offset, 0) + cell->xpad;
396 *y_offset = (cell->yalign *
397 (cell_area->height - calc_height - 2 * cell->ypad));
398 *y_offset = MAX (*y_offset, 0) + cell->ypad;
406 *height = calc_height;
410 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
413 GdkRectangle *background_area,
414 GdkRectangle *cell_area,
415 GdkRectangle *expose_area,
416 GtkCellRendererState flags)
419 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
420 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
422 GdkRectangle pix_rect;
423 GdkRectangle draw_rect;
424 gboolean stock_pixbuf = FALSE;
426 pixbuf = cellpixbuf->pixbuf;
427 if (cell->is_expander)
429 if (cell->is_expanded &&
430 cellpixbuf->pixbuf_expander_open != NULL)
431 pixbuf = cellpixbuf->pixbuf_expander_open;
432 else if (! cell->is_expanded &&
433 cellpixbuf->pixbuf_expander_closed != NULL)
434 pixbuf = cellpixbuf->pixbuf_expander_closed;
437 if (!pixbuf && !cellinfo->stock_id)
439 else if (!pixbuf && cellinfo->stock_id)
442 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
449 pixbuf = cellpixbuf->pixbuf;
451 pix_rect.x += cell_area->x;
452 pix_rect.y += cell_area->y;
453 pix_rect.width -= cell->xpad * 2;
454 pix_rect.height -= cell->ypad * 2;
456 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
457 gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
458 gdk_draw_pixbuf (window,
459 widget->style->black_gc,
461 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
462 draw_rect.x - pix_rect.x,
463 draw_rect.y - pix_rect.y,
468 GDK_RGB_DITHER_NORMAL,