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,
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 GtkType 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,
95 cell_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf", &cell_pixbuf_info, 0);
98 return cell_pixbuf_type;
102 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
104 GtkCellRendererPixbufInfo *cellinfo;
106 cellinfo = g_new0 (GtkCellRendererPixbufInfo, 1);
107 cellinfo->stock_size = GTK_ICON_SIZE_MENU;
108 g_object_set_data (G_OBJECT (cellpixbuf), CELLINFO_KEY, cellinfo);
112 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
114 GObjectClass *object_class = G_OBJECT_CLASS (class);
115 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
117 parent_class = g_type_class_peek_parent (class);
119 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
121 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
122 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
124 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
125 cell_class->render = gtk_cell_renderer_pixbuf_render;
127 g_object_class_install_property (object_class,
129 g_param_spec_object ("pixbuf",
131 _("The pixbuf to render."),
136 g_object_class_install_property (object_class,
137 PROP_PIXBUF_EXPANDER_OPEN,
138 g_param_spec_object ("pixbuf_expander_open",
139 _("Pixbuf Expander Open"),
140 _("Pixbuf for open expander."),
145 g_object_class_install_property (object_class,
146 PROP_PIXBUF_EXPANDER_CLOSED,
147 g_param_spec_object ("pixbuf_expander_closed",
148 _("Pixbuf Expander Closed"),
149 _("Pixbuf for closed expander."),
154 g_object_class_install_property (object_class,
156 g_param_spec_string ("stock_id",
158 _("The stock ID of the stock icon to render"),
162 g_object_class_install_property (object_class,
164 g_param_spec_enum ("stock_size",
166 _("The size of the rendered icon"),
171 g_object_class_install_property (object_class,
173 g_param_spec_string ("stock_detail",
175 _("Render detail to pass to the theme engine"),
182 gtk_cell_renderer_pixbuf_finalize (GObject *object)
184 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
185 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
187 if (cellpixbuf->pixbuf && cellinfo->stock_id)
188 g_object_unref (cellpixbuf->pixbuf);
190 if (cellinfo->stock_id)
191 g_free (cellinfo->stock_id);
193 if (cellinfo->stock_detail)
194 g_free (cellinfo->stock_detail);
197 g_object_set_data (object, CELLINFO_KEY, NULL);
199 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
203 gtk_cell_renderer_pixbuf_get_property (GObject *object,
208 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
209 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
214 g_value_set_object (value,
215 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
217 case PROP_PIXBUF_EXPANDER_OPEN:
218 g_value_set_object (value,
219 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
221 case PROP_PIXBUF_EXPANDER_CLOSED:
222 g_value_set_object (value,
223 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
226 g_value_set_string (value, cellinfo->stock_id);
228 case PROP_STOCK_SIZE:
229 g_value_set_enum (value, cellinfo->stock_size);
231 case PROP_STOCK_DETAIL:
232 g_value_set_string (value, cellinfo->stock_detail);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
242 gtk_cell_renderer_pixbuf_set_property (GObject *object,
248 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
249 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
254 pixbuf = (GdkPixbuf*) g_value_get_object (value);
256 g_object_ref (G_OBJECT (pixbuf));
257 if (cellpixbuf->pixbuf)
258 g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
259 cellpixbuf->pixbuf = pixbuf;
261 case PROP_PIXBUF_EXPANDER_OPEN:
262 pixbuf = (GdkPixbuf*) g_value_get_object (value);
264 g_object_ref (G_OBJECT (pixbuf));
265 if (cellpixbuf->pixbuf_expander_open)
266 g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_open));
267 cellpixbuf->pixbuf_expander_open = pixbuf;
269 case PROP_PIXBUF_EXPANDER_CLOSED:
270 pixbuf = (GdkPixbuf*) g_value_get_object (value);
272 g_object_ref (G_OBJECT (pixbuf));
273 if (cellpixbuf->pixbuf_expander_closed)
274 g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_closed));
275 cellpixbuf->pixbuf_expander_closed = pixbuf;
278 if (cellinfo->stock_id)
279 g_free (cellinfo->stock_id);
280 cellinfo->stock_id = g_strdup (g_value_get_string (value));
281 g_object_notify (G_OBJECT (object), "stock_id");
283 case PROP_STOCK_SIZE:
284 cellinfo->stock_size = g_value_get_enum (value);
285 g_object_notify (G_OBJECT (object), "stock_size");
287 case PROP_STOCK_DETAIL:
288 if (cellinfo->stock_detail)
289 g_free (cellinfo->stock_detail);
290 cellinfo->stock_detail = g_strdup (g_value_get_string (value));
291 g_object_notify (G_OBJECT (object), "stock_detail");
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
298 if (cellpixbuf->pixbuf && cellinfo->stock_id)
300 g_object_unref (cellpixbuf->pixbuf);
301 cellpixbuf->pixbuf = NULL;
306 * gtk_cell_renderer_pixbuf_new:
308 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
309 * parameters using object properties. Object properties can be set
310 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
311 * can bind a property to a value in a #GtkTreeModel. For example, you
312 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
313 * in the model, thus rendering a different image in each row of the
316 * Return value: the new cell renderer
319 gtk_cell_renderer_pixbuf_new (void)
321 return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
325 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
328 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cellpixbuf), CELLINFO_KEY);
330 if (cellpixbuf->pixbuf)
331 g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
333 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
335 cellinfo->stock_size,
336 cellinfo->stock_detail);
340 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
342 GdkRectangle *cell_area,
348 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
349 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
350 gint pixbuf_width = 0;
351 gint pixbuf_height = 0;
355 if (!cellpixbuf->pixbuf && cellinfo->stock_id)
356 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
358 if (cellpixbuf->pixbuf)
360 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
361 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
363 if (cellpixbuf->pixbuf_expander_open)
365 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
366 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
368 if (cellpixbuf->pixbuf_expander_closed)
370 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
371 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
374 calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
375 calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
377 if (x_offset) *x_offset = 0;
378 if (y_offset) *y_offset = 0;
380 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
384 *x_offset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - calc_width - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
385 *x_offset = MAX (*x_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
389 *y_offset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - calc_height - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
390 *y_offset = MAX (*y_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
398 *height = calc_height;
402 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
405 GdkRectangle *background_area,
406 GdkRectangle *cell_area,
407 GdkRectangle *expose_area,
411 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
412 GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
414 GdkRectangle pix_rect;
415 GdkRectangle draw_rect;
416 gboolean stock_pixbuf = FALSE;
418 pixbuf = cellpixbuf->pixbuf;
419 if (cell->is_expander)
421 if (cell->is_expanded &&
422 cellpixbuf->pixbuf_expander_open != NULL)
423 pixbuf = cellpixbuf->pixbuf_expander_open;
424 else if (! cell->is_expanded &&
425 cellpixbuf->pixbuf_expander_closed != NULL)
426 pixbuf = cellpixbuf->pixbuf_expander_closed;
429 if (!pixbuf && !cellinfo->stock_id)
431 else if (!pixbuf && cellinfo->stock_id)
434 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
441 pixbuf = cellpixbuf->pixbuf;
443 pix_rect.x += cell_area->x;
444 pix_rect.y += cell_area->y;
445 pix_rect.width -= cell->xpad * 2;
446 pix_rect.height -= cell->ypad * 2;
448 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
449 gdk_pixbuf_render_to_drawable_alpha (pixbuf,
451 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
452 draw_rect.x - pix_rect.x,
453 draw_rect.y - pix_rect.y,
458 GDK_PIXBUF_ALPHA_FULL,
460 GDK_RGB_DITHER_NORMAL,