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.
23 #include "gtkcellrendererpixbuf.h"
24 #include "gtkiconfactory.h"
27 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
31 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
35 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
36 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
37 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
38 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
40 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
42 GdkRectangle *rectangle,
47 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
50 GdkRectangle *background_area,
51 GdkRectangle *cell_area,
52 GdkRectangle *expose_area,
53 GtkCellRendererState flags);
59 PROP_PIXBUF_EXPANDER_OPEN,
60 PROP_PIXBUF_EXPANDER_CLOSED,
66 static gpointer parent_class;
69 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
71 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
72 struct _GtkCellRendererPixbufPrivate
75 GtkIconSize stock_size;
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 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
210 gtk_cell_renderer_pixbuf_get_property (GObject *object,
215 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
216 GtkCellRendererPixbufPrivate *priv;
218 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
223 g_value_set_object (value,
224 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
226 case PROP_PIXBUF_EXPANDER_OPEN:
227 g_value_set_object (value,
228 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
230 case PROP_PIXBUF_EXPANDER_CLOSED:
231 g_value_set_object (value,
232 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
235 g_value_set_string (value, priv->stock_id);
237 case PROP_STOCK_SIZE:
238 g_value_set_uint (value, priv->stock_size);
240 case PROP_STOCK_DETAIL:
241 g_value_set_string (value, priv->stock_detail);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
251 gtk_cell_renderer_pixbuf_set_property (GObject *object,
257 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
258 GtkCellRendererPixbufPrivate *priv;
260 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
265 pixbuf = (GdkPixbuf*) g_value_get_object (value);
267 g_object_ref (pixbuf);
268 if (cellpixbuf->pixbuf)
269 g_object_unref (cellpixbuf->pixbuf);
270 cellpixbuf->pixbuf = pixbuf;
272 case PROP_PIXBUF_EXPANDER_OPEN:
273 pixbuf = (GdkPixbuf*) g_value_get_object (value);
275 g_object_ref (pixbuf);
276 if (cellpixbuf->pixbuf_expander_open)
277 g_object_unref (cellpixbuf->pixbuf_expander_open);
278 cellpixbuf->pixbuf_expander_open = pixbuf;
280 case PROP_PIXBUF_EXPANDER_CLOSED:
281 pixbuf = (GdkPixbuf*) g_value_get_object (value);
283 g_object_ref (pixbuf);
284 if (cellpixbuf->pixbuf_expander_closed)
285 g_object_unref (cellpixbuf->pixbuf_expander_closed);
286 cellpixbuf->pixbuf_expander_closed = pixbuf;
291 if (cellpixbuf->pixbuf)
293 g_object_unref (cellpixbuf->pixbuf);
294 cellpixbuf->pixbuf = NULL;
296 g_free (priv->stock_id);
298 priv->stock_id = g_strdup (g_value_get_string (value));
300 case PROP_STOCK_SIZE:
301 priv->stock_size = g_value_get_uint (value);
303 case PROP_STOCK_DETAIL:
304 if (priv->stock_detail)
305 g_free (priv->stock_detail);
306 priv->stock_detail = g_strdup (g_value_get_string (value));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
313 if (cellpixbuf->pixbuf && priv->stock_id)
315 g_object_unref (cellpixbuf->pixbuf);
316 cellpixbuf->pixbuf = NULL;
321 * gtk_cell_renderer_pixbuf_new:
323 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
324 * parameters using object properties. Object properties can be set
325 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
326 * can bind a property to a value in a #GtkTreeModel. For example, you
327 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
328 * in the model, thus rendering a different image in each row of the
331 * Return value: the new cell renderer
334 gtk_cell_renderer_pixbuf_new (void)
336 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
340 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
343 GtkCellRendererPixbufPrivate *priv;
345 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
347 if (cellpixbuf->pixbuf)
348 g_object_unref (cellpixbuf->pixbuf);
350 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
357 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
359 GdkRectangle *cell_area,
365 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
366 GtkCellRendererPixbufPrivate *priv;
367 gint pixbuf_width = 0;
368 gint pixbuf_height = 0;
372 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
374 if (!cellpixbuf->pixbuf && priv->stock_id)
375 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
377 if (cellpixbuf->pixbuf)
379 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
380 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
382 if (cellpixbuf->pixbuf_expander_open)
384 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
385 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
387 if (cellpixbuf->pixbuf_expander_closed)
389 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
390 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
393 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
394 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
396 if (x_offset) *x_offset = 0;
397 if (y_offset) *y_offset = 0;
399 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
403 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
404 1.0 - cell->xalign : cell->xalign) *
405 (cell_area->width - calc_width - 2 * cell->xpad));
406 *x_offset = MAX (*x_offset, 0) + cell->xpad;
410 *y_offset = (cell->yalign *
411 (cell_area->height - calc_height - 2 * cell->ypad));
412 *y_offset = MAX (*y_offset, 0) + cell->ypad;
420 *height = calc_height;
424 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
427 GdkRectangle *background_area,
428 GdkRectangle *cell_area,
429 GdkRectangle *expose_area,
430 GtkCellRendererState flags)
433 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
434 GtkCellRendererPixbufPrivate *priv;
436 GdkPixbuf *invisible = NULL;
437 GdkRectangle pix_rect;
438 GdkRectangle draw_rect;
439 gboolean stock_pixbuf = FALSE;
441 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
443 pixbuf = cellpixbuf->pixbuf;
444 if (cell->is_expander)
446 if (cell->is_expanded &&
447 cellpixbuf->pixbuf_expander_open != NULL)
448 pixbuf = cellpixbuf->pixbuf_expander_open;
449 else if (! cell->is_expanded &&
450 cellpixbuf->pixbuf_expander_closed != NULL)
451 pixbuf = cellpixbuf->pixbuf_expander_closed;
454 if (!pixbuf && !priv->stock_id)
456 else if (!pixbuf && priv->stock_id)
459 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
466 pixbuf = cellpixbuf->pixbuf;
468 pix_rect.x += cell_area->x;
469 pix_rect.y += cell_area->y;
470 pix_rect.width -= cell->xpad * 2;
471 pix_rect.height -= cell->ypad * 2;
473 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
475 GtkIconSource *source;
477 source = gtk_icon_source_new ();
478 gtk_icon_source_set_pixbuf (source, pixbuf);
479 /* The size here is arbitrary; since size isn't
480 * wildcarded in the souce, it isn't supposed to be
481 * scaled by the engine function
483 gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
484 gtk_icon_source_set_size_wildcarded (source, FALSE);
486 invisible = gtk_style_render_icon (widget->style,
488 gtk_widget_get_direction (widget),
489 GTK_STATE_INSENSITIVE,
493 "gtkcellrendererpixbuf");
495 gtk_icon_source_free (source);
500 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
501 gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
502 gdk_draw_pixbuf (window,
503 widget->style->black_gc,
505 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
506 draw_rect.x - pix_rect.x,
507 draw_rect.y - pix_rect.y,
512 GDK_RGB_DITHER_NORMAL,
516 g_object_unref (invisible);