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 P_("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 P_("Pixbuf Expander Open"),
143 P_("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 P_("Pixbuf Expander Closed"),
152 P_("Pixbuf for closed expander"),
157 g_object_class_install_property (object_class,
159 g_param_spec_string ("stock_id",
161 P_("The stock ID of the stock icon to render"),
165 g_object_class_install_property (object_class,
167 g_param_spec_uint ("stock_size",
169 P_("The GtkIconSize value that specifies the size of the rendered icon"),
175 g_object_class_install_property (object_class,
177 g_param_spec_string ("stock_detail",
179 P_("Render detail to pass to the theme engine"),
183 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
187 gtk_cell_renderer_pixbuf_finalize (GObject *object)
189 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
190 GtkCellRendererPixbufPrivate *priv;
192 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
194 if (cellpixbuf->pixbuf && priv->stock_id)
195 g_object_unref (cellpixbuf->pixbuf);
198 g_free (priv->stock_id);
200 if (priv->stock_detail)
201 g_free (priv->stock_detail);
203 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
207 gtk_cell_renderer_pixbuf_get_property (GObject *object,
212 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
213 GtkCellRendererPixbufPrivate *priv;
215 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
220 g_value_set_object (value,
221 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
223 case PROP_PIXBUF_EXPANDER_OPEN:
224 g_value_set_object (value,
225 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
227 case PROP_PIXBUF_EXPANDER_CLOSED:
228 g_value_set_object (value,
229 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
232 g_value_set_string (value, priv->stock_id);
234 case PROP_STOCK_SIZE:
235 g_value_set_uint (value, priv->stock_size);
237 case PROP_STOCK_DETAIL:
238 g_value_set_string (value, priv->stock_detail);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
248 gtk_cell_renderer_pixbuf_set_property (GObject *object,
254 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
255 GtkCellRendererPixbufPrivate *priv;
257 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
262 pixbuf = (GdkPixbuf*) g_value_get_object (value);
264 g_object_ref (pixbuf);
265 if (cellpixbuf->pixbuf)
266 g_object_unref (cellpixbuf->pixbuf);
267 cellpixbuf->pixbuf = pixbuf;
269 case PROP_PIXBUF_EXPANDER_OPEN:
270 pixbuf = (GdkPixbuf*) g_value_get_object (value);
272 g_object_ref (pixbuf);
273 if (cellpixbuf->pixbuf_expander_open)
274 g_object_unref (cellpixbuf->pixbuf_expander_open);
275 cellpixbuf->pixbuf_expander_open = pixbuf;
277 case PROP_PIXBUF_EXPANDER_CLOSED:
278 pixbuf = (GdkPixbuf*) g_value_get_object (value);
280 g_object_ref (pixbuf);
281 if (cellpixbuf->pixbuf_expander_closed)
282 g_object_unref (cellpixbuf->pixbuf_expander_closed);
283 cellpixbuf->pixbuf_expander_closed = pixbuf;
288 if (cellpixbuf->pixbuf)
290 g_object_unref (cellpixbuf->pixbuf);
291 cellpixbuf->pixbuf = NULL;
293 g_free (priv->stock_id);
295 priv->stock_id = g_strdup (g_value_get_string (value));
297 case PROP_STOCK_SIZE:
298 priv->stock_size = g_value_get_uint (value);
300 case PROP_STOCK_DETAIL:
301 if (priv->stock_detail)
302 g_free (priv->stock_detail);
303 priv->stock_detail = g_strdup (g_value_get_string (value));
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
310 if (cellpixbuf->pixbuf && priv->stock_id)
312 g_object_unref (cellpixbuf->pixbuf);
313 cellpixbuf->pixbuf = NULL;
318 * gtk_cell_renderer_pixbuf_new:
320 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
321 * parameters using object properties. Object properties can be set
322 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
323 * can bind a property to a value in a #GtkTreeModel. For example, you
324 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
325 * in the model, thus rendering a different image in each row of the
328 * Return value: the new cell renderer
331 gtk_cell_renderer_pixbuf_new (void)
333 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
337 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
340 GtkCellRendererPixbufPrivate *priv;
342 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
344 if (cellpixbuf->pixbuf)
345 g_object_unref (cellpixbuf->pixbuf);
347 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
354 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
356 GdkRectangle *cell_area,
362 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
363 GtkCellRendererPixbufPrivate *priv;
364 gint pixbuf_width = 0;
365 gint pixbuf_height = 0;
369 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
371 if (!cellpixbuf->pixbuf && priv->stock_id)
372 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
374 if (cellpixbuf->pixbuf)
376 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
377 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
379 if (cellpixbuf->pixbuf_expander_open)
381 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
382 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
384 if (cellpixbuf->pixbuf_expander_closed)
386 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
387 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
390 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
391 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
393 if (x_offset) *x_offset = 0;
394 if (y_offset) *y_offset = 0;
396 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
400 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
401 1.0 - cell->xalign : cell->xalign) *
402 (cell_area->width - calc_width - 2 * cell->xpad));
403 *x_offset = MAX (*x_offset, 0) + cell->xpad;
407 *y_offset = (cell->yalign *
408 (cell_area->height - calc_height - 2 * cell->ypad));
409 *y_offset = MAX (*y_offset, 0) + cell->ypad;
417 *height = calc_height;
421 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
424 GdkRectangle *background_area,
425 GdkRectangle *cell_area,
426 GdkRectangle *expose_area,
427 GtkCellRendererState flags)
430 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
431 GtkCellRendererPixbufPrivate *priv;
433 GdkRectangle pix_rect;
434 GdkRectangle draw_rect;
435 gboolean stock_pixbuf = FALSE;
437 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
439 pixbuf = cellpixbuf->pixbuf;
440 if (cell->is_expander)
442 if (cell->is_expanded &&
443 cellpixbuf->pixbuf_expander_open != NULL)
444 pixbuf = cellpixbuf->pixbuf_expander_open;
445 else if (! cell->is_expanded &&
446 cellpixbuf->pixbuf_expander_closed != NULL)
447 pixbuf = cellpixbuf->pixbuf_expander_closed;
450 if (!pixbuf && !priv->stock_id)
452 else if (!pixbuf && priv->stock_id)
455 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
462 pixbuf = cellpixbuf->pixbuf;
464 pix_rect.x += cell_area->x;
465 pix_rect.y += cell_area->y;
466 pix_rect.width -= cell->xpad * 2;
467 pix_rect.height -= cell->ypad * 2;
469 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
470 gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
471 gdk_draw_pixbuf (window,
472 widget->style->black_gc,
474 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
475 draw_rect.x - pix_rect.x,
476 draw_rect.y - pix_rect.y,
481 GDK_RGB_DITHER_NORMAL,