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.
22 #include "gtkcellrendererpixbuf.h"
23 #include "gtkiconfactory.h"
26 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
30 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
34 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
35 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
36 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
37 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
39 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
41 GdkRectangle *rectangle,
46 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
49 GdkRectangle *background_area,
50 GdkRectangle *cell_area,
51 GdkRectangle *expose_area,
52 GtkCellRendererState flags);
58 PROP_PIXBUF_EXPANDER_OPEN,
59 PROP_PIXBUF_EXPANDER_CLOSED,
65 static gpointer parent_class;
68 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
70 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
71 struct _GtkCellRendererPixbufPrivate
74 GtkIconSize stock_size;
76 GdkPixbuf *insensitive;
81 gtk_cell_renderer_pixbuf_get_type (void)
83 static GType cell_pixbuf_type = 0;
85 if (!cell_pixbuf_type)
87 static const GTypeInfo cell_pixbuf_info =
89 sizeof (GtkCellRendererPixbufClass),
91 NULL, /* base_finalize */
92 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
93 NULL, /* class_finalize */
94 NULL, /* class_data */
95 sizeof (GtkCellRendererPixbuf),
97 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
101 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf",
102 &cell_pixbuf_info, 0);
105 return cell_pixbuf_type;
109 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
111 GtkCellRendererPixbufPrivate *priv;
113 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
114 priv->stock_size = GTK_ICON_SIZE_MENU;
118 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
120 GObjectClass *object_class = G_OBJECT_CLASS (class);
121 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
123 parent_class = g_type_class_peek_parent (class);
125 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
127 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
128 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
130 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
131 cell_class->render = gtk_cell_renderer_pixbuf_render;
133 g_object_class_install_property (object_class,
135 g_param_spec_object ("pixbuf",
137 P_("The pixbuf to render"),
142 g_object_class_install_property (object_class,
143 PROP_PIXBUF_EXPANDER_OPEN,
144 g_param_spec_object ("pixbuf_expander_open",
145 P_("Pixbuf Expander Open"),
146 P_("Pixbuf for open expander"),
151 g_object_class_install_property (object_class,
152 PROP_PIXBUF_EXPANDER_CLOSED,
153 g_param_spec_object ("pixbuf_expander_closed",
154 P_("Pixbuf Expander Closed"),
155 P_("Pixbuf for closed expander"),
160 g_object_class_install_property (object_class,
162 g_param_spec_string ("stock_id",
164 P_("The stock ID of the stock icon to render"),
168 g_object_class_install_property (object_class,
170 g_param_spec_uint ("stock_size",
172 P_("The GtkIconSize value that specifies the size of the rendered icon"),
178 g_object_class_install_property (object_class,
180 g_param_spec_string ("stock_detail",
182 P_("Render detail to pass to the theme engine"),
186 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
190 gtk_cell_renderer_pixbuf_finalize (GObject *object)
192 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
193 GtkCellRendererPixbufPrivate *priv;
195 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
197 if (cellpixbuf->pixbuf && priv->stock_id)
198 g_object_unref (cellpixbuf->pixbuf);
201 g_free (priv->stock_id);
203 if (priv->stock_detail)
204 g_free (priv->stock_detail);
206 if (priv->insensitive)
207 g_object_unref (priv->insensitive);
209 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
213 gtk_cell_renderer_pixbuf_get_property (GObject *object,
218 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
219 GtkCellRendererPixbufPrivate *priv;
221 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
226 g_value_set_object (value,
227 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
229 case PROP_PIXBUF_EXPANDER_OPEN:
230 g_value_set_object (value,
231 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
233 case PROP_PIXBUF_EXPANDER_CLOSED:
234 g_value_set_object (value,
235 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
238 g_value_set_string (value, priv->stock_id);
240 case PROP_STOCK_SIZE:
241 g_value_set_uint (value, priv->stock_size);
243 case PROP_STOCK_DETAIL:
244 g_value_set_string (value, priv->stock_detail);
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
254 gtk_cell_renderer_pixbuf_set_property (GObject *object,
260 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
261 GtkCellRendererPixbufPrivate *priv;
263 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
268 pixbuf = (GdkPixbuf*) g_value_get_object (value);
270 g_object_ref (pixbuf);
271 if (cellpixbuf->pixbuf)
272 g_object_unref (cellpixbuf->pixbuf);
273 cellpixbuf->pixbuf = pixbuf;
275 case PROP_PIXBUF_EXPANDER_OPEN:
276 pixbuf = (GdkPixbuf*) g_value_get_object (value);
278 g_object_ref (pixbuf);
279 if (cellpixbuf->pixbuf_expander_open)
280 g_object_unref (cellpixbuf->pixbuf_expander_open);
281 cellpixbuf->pixbuf_expander_open = pixbuf;
283 case PROP_PIXBUF_EXPANDER_CLOSED:
284 pixbuf = (GdkPixbuf*) g_value_get_object (value);
286 g_object_ref (pixbuf);
287 if (cellpixbuf->pixbuf_expander_closed)
288 g_object_unref (cellpixbuf->pixbuf_expander_closed);
289 cellpixbuf->pixbuf_expander_closed = pixbuf;
294 if (cellpixbuf->pixbuf)
296 g_object_unref (cellpixbuf->pixbuf);
297 cellpixbuf->pixbuf = NULL;
299 g_free (priv->stock_id);
301 priv->stock_id = g_strdup (g_value_get_string (value));
303 case PROP_STOCK_SIZE:
304 priv->stock_size = g_value_get_uint (value);
306 case PROP_STOCK_DETAIL:
307 if (priv->stock_detail)
308 g_free (priv->stock_detail);
309 priv->stock_detail = g_strdup (g_value_get_string (value));
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
316 if (cellpixbuf->pixbuf && priv->stock_id)
318 g_object_unref (cellpixbuf->pixbuf);
319 cellpixbuf->pixbuf = NULL;
324 * gtk_cell_renderer_pixbuf_new:
326 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
327 * parameters using object properties. Object properties can be set
328 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
329 * can bind a property to a value in a #GtkTreeModel. For example, you
330 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
331 * in the model, thus rendering a different image in each row of the
334 * Return value: the new cell renderer
337 gtk_cell_renderer_pixbuf_new (void)
339 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
343 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
346 GtkCellRendererPixbufPrivate *priv;
348 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
350 if (cellpixbuf->pixbuf)
351 g_object_unref (cellpixbuf->pixbuf);
353 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
360 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
362 GdkRectangle *cell_area,
368 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
369 GtkCellRendererPixbufPrivate *priv;
370 gint pixbuf_width = 0;
371 gint pixbuf_height = 0;
375 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
377 if (!cellpixbuf->pixbuf && priv->stock_id)
378 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
380 if (cellpixbuf->pixbuf)
382 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
383 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
385 if (cellpixbuf->pixbuf_expander_open)
387 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
388 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
390 if (cellpixbuf->pixbuf_expander_closed)
392 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
393 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
396 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
397 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
399 if (x_offset) *x_offset = 0;
400 if (y_offset) *y_offset = 0;
402 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
406 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
407 1.0 - cell->xalign : cell->xalign) *
408 (cell_area->width - calc_width - 2 * cell->xpad));
409 *x_offset = MAX (*x_offset, 0) + cell->xpad;
413 *y_offset = (cell->yalign *
414 (cell_area->height - calc_height - 2 * cell->ypad));
415 *y_offset = MAX (*y_offset, 0) + cell->ypad;
423 *height = calc_height;
427 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
430 GdkRectangle *background_area,
431 GdkRectangle *cell_area,
432 GdkRectangle *expose_area,
433 GtkCellRendererState flags)
436 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
437 GtkCellRendererPixbufPrivate *priv;
439 GdkRectangle pix_rect;
440 GdkRectangle draw_rect;
441 gboolean stock_pixbuf = FALSE;
443 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
445 pixbuf = cellpixbuf->pixbuf;
446 if (cell->is_expander)
448 if (cell->is_expanded &&
449 cellpixbuf->pixbuf_expander_open != NULL)
450 pixbuf = cellpixbuf->pixbuf_expander_open;
451 else if (! cell->is_expanded &&
452 cellpixbuf->pixbuf_expander_closed != NULL)
453 pixbuf = cellpixbuf->pixbuf_expander_closed;
456 if (!pixbuf && !priv->stock_id)
458 else if (!pixbuf && priv->stock_id)
461 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
468 pixbuf = cellpixbuf->pixbuf;
470 pix_rect.x += cell_area->x;
471 pix_rect.y += cell_area->y;
472 pix_rect.width -= cell->xpad * 2;
473 pix_rect.height -= cell->ypad * 2;
475 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
477 if (!priv->insensitive)
479 GtkIconSource *source;
481 source = gtk_icon_source_new ();
482 gtk_icon_source_set_pixbuf (source, pixbuf);
483 /* The size here is arbitrary; since size isn't
484 * wildcarded in the souce, it isn't supposed to be
485 * scaled by the engine function
487 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
488 gtk_icon_source_set_size_wildcarded (source, FALSE);
490 priv->insensitive = gtk_style_render_icon (widget->style,
492 gtk_widget_get_direction (widget),
493 GTK_STATE_INSENSITIVE,
497 "gtkcellrendererpixbuf");
499 gtk_icon_source_free (source);
502 pixbuf = priv->insensitive;
505 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
506 gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
507 gdk_draw_pixbuf (window,
508 widget->style->black_gc,
510 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
511 draw_rect.x - pix_rect.x,
512 draw_rect.y - pix_rect.y,
517 GDK_RGB_DITHER_NORMAL,