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"
25 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
29 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
33 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
34 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
35 static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
36 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
38 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
40 GdkRectangle *rectangle,
45 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
48 GdkRectangle *background_area,
49 GdkRectangle *cell_area,
50 GdkRectangle *expose_area,
51 GtkCellRendererState flags);
57 PROP_PIXBUF_EXPANDER_OPEN,
58 PROP_PIXBUF_EXPANDER_CLOSED,
64 static gpointer parent_class;
67 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
69 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
70 struct _GtkCellRendererPixbufPrivate
73 GtkIconSize stock_size;
79 gtk_cell_renderer_pixbuf_get_type (void)
81 static GType cell_pixbuf_type = 0;
83 if (!cell_pixbuf_type)
85 static const GTypeInfo cell_pixbuf_info =
87 sizeof (GtkCellRendererPixbufClass),
89 NULL, /* base_finalize */
90 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
91 NULL, /* class_finalize */
92 NULL, /* class_data */
93 sizeof (GtkCellRendererPixbuf),
95 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
99 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf",
100 &cell_pixbuf_info, 0);
103 return cell_pixbuf_type;
107 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
109 GtkCellRendererPixbufPrivate *priv;
111 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
112 priv->stock_size = GTK_ICON_SIZE_MENU;
116 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
119 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
121 parent_class = g_type_class_peek_parent (class);
123 object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
125 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
126 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
128 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
129 cell_class->render = gtk_cell_renderer_pixbuf_render;
131 g_object_class_install_property (object_class,
133 g_param_spec_object ("pixbuf",
135 P_("The pixbuf to render"),
140 g_object_class_install_property (object_class,
141 PROP_PIXBUF_EXPANDER_OPEN,
142 g_param_spec_object ("pixbuf_expander_open",
143 P_("Pixbuf Expander Open"),
144 P_("Pixbuf for open expander"),
149 g_object_class_install_property (object_class,
150 PROP_PIXBUF_EXPANDER_CLOSED,
151 g_param_spec_object ("pixbuf_expander_closed",
152 P_("Pixbuf Expander Closed"),
153 P_("Pixbuf for closed expander"),
158 g_object_class_install_property (object_class,
160 g_param_spec_string ("stock_id",
162 P_("The stock ID of the stock icon to render"),
166 g_object_class_install_property (object_class,
168 g_param_spec_uint ("stock_size",
170 P_("The GtkIconSize value that specifies the size of the rendered icon"),
176 g_object_class_install_property (object_class,
178 g_param_spec_string ("stock_detail",
180 P_("Render detail to pass to the theme engine"),
184 g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
188 gtk_cell_renderer_pixbuf_finalize (GObject *object)
190 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
191 GtkCellRendererPixbufPrivate *priv;
193 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
195 if (cellpixbuf->pixbuf && priv->stock_id)
196 g_object_unref (cellpixbuf->pixbuf);
199 g_free (priv->stock_id);
201 if (priv->stock_detail)
202 g_free (priv->stock_detail);
204 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
208 gtk_cell_renderer_pixbuf_get_property (GObject *object,
213 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
214 GtkCellRendererPixbufPrivate *priv;
216 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
221 g_value_set_object (value,
222 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
224 case PROP_PIXBUF_EXPANDER_OPEN:
225 g_value_set_object (value,
226 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
228 case PROP_PIXBUF_EXPANDER_CLOSED:
229 g_value_set_object (value,
230 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
233 g_value_set_string (value, priv->stock_id);
235 case PROP_STOCK_SIZE:
236 g_value_set_uint (value, priv->stock_size);
238 case PROP_STOCK_DETAIL:
239 g_value_set_string (value, priv->stock_detail);
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
249 gtk_cell_renderer_pixbuf_set_property (GObject *object,
255 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
256 GtkCellRendererPixbufPrivate *priv;
258 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
263 pixbuf = (GdkPixbuf*) g_value_get_object (value);
265 g_object_ref (pixbuf);
266 if (cellpixbuf->pixbuf)
267 g_object_unref (cellpixbuf->pixbuf);
268 cellpixbuf->pixbuf = pixbuf;
270 case PROP_PIXBUF_EXPANDER_OPEN:
271 pixbuf = (GdkPixbuf*) g_value_get_object (value);
273 g_object_ref (pixbuf);
274 if (cellpixbuf->pixbuf_expander_open)
275 g_object_unref (cellpixbuf->pixbuf_expander_open);
276 cellpixbuf->pixbuf_expander_open = pixbuf;
278 case PROP_PIXBUF_EXPANDER_CLOSED:
279 pixbuf = (GdkPixbuf*) g_value_get_object (value);
281 g_object_ref (pixbuf);
282 if (cellpixbuf->pixbuf_expander_closed)
283 g_object_unref (cellpixbuf->pixbuf_expander_closed);
284 cellpixbuf->pixbuf_expander_closed = pixbuf;
289 if (cellpixbuf->pixbuf)
291 g_object_unref (cellpixbuf->pixbuf);
292 cellpixbuf->pixbuf = NULL;
294 g_free (priv->stock_id);
296 priv->stock_id = g_strdup (g_value_get_string (value));
298 case PROP_STOCK_SIZE:
299 priv->stock_size = g_value_get_uint (value);
301 case PROP_STOCK_DETAIL:
302 if (priv->stock_detail)
303 g_free (priv->stock_detail);
304 priv->stock_detail = g_strdup (g_value_get_string (value));
307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
311 if (cellpixbuf->pixbuf && priv->stock_id)
313 g_object_unref (cellpixbuf->pixbuf);
314 cellpixbuf->pixbuf = NULL;
319 * gtk_cell_renderer_pixbuf_new:
321 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
322 * parameters using object properties. Object properties can be set
323 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
324 * can bind a property to a value in a #GtkTreeModel. For example, you
325 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
326 * in the model, thus rendering a different image in each row of the
329 * Return value: the new cell renderer
332 gtk_cell_renderer_pixbuf_new (void)
334 return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
338 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
341 GtkCellRendererPixbufPrivate *priv;
343 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
345 if (cellpixbuf->pixbuf)
346 g_object_unref (cellpixbuf->pixbuf);
348 cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
355 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
357 GdkRectangle *cell_area,
363 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
364 GtkCellRendererPixbufPrivate *priv;
365 gint pixbuf_width = 0;
366 gint pixbuf_height = 0;
370 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
372 if (!cellpixbuf->pixbuf && priv->stock_id)
373 gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
375 if (cellpixbuf->pixbuf)
377 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
378 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
380 if (cellpixbuf->pixbuf_expander_open)
382 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
383 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
385 if (cellpixbuf->pixbuf_expander_closed)
387 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
388 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
391 calc_width = (gint) cell->xpad * 2 + pixbuf_width;
392 calc_height = (gint) cell->ypad * 2 + pixbuf_height;
394 if (x_offset) *x_offset = 0;
395 if (y_offset) *y_offset = 0;
397 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
401 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
402 1.0 - cell->xalign : cell->xalign) *
403 (cell_area->width - calc_width - 2 * cell->xpad));
404 *x_offset = MAX (*x_offset, 0) + cell->xpad;
408 *y_offset = (cell->yalign *
409 (cell_area->height - calc_height - 2 * cell->ypad));
410 *y_offset = MAX (*y_offset, 0) + cell->ypad;
418 *height = calc_height;
422 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
425 GdkRectangle *background_area,
426 GdkRectangle *cell_area,
427 GdkRectangle *expose_area,
428 GtkCellRendererState flags)
431 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
432 GtkCellRendererPixbufPrivate *priv;
434 GdkRectangle pix_rect;
435 GdkRectangle draw_rect;
436 gboolean stock_pixbuf = FALSE;
438 priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
440 pixbuf = cellpixbuf->pixbuf;
441 if (cell->is_expander)
443 if (cell->is_expanded &&
444 cellpixbuf->pixbuf_expander_open != NULL)
445 pixbuf = cellpixbuf->pixbuf_expander_open;
446 else if (! cell->is_expanded &&
447 cellpixbuf->pixbuf_expander_closed != NULL)
448 pixbuf = cellpixbuf->pixbuf_expander_closed;
451 if (!pixbuf && !priv->stock_id)
453 else if (!pixbuf && priv->stock_id)
456 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
463 pixbuf = cellpixbuf->pixbuf;
465 pix_rect.x += cell_area->x;
466 pix_rect.y += cell_area->y;
467 pix_rect.width -= cell->xpad * 2;
468 pix_rect.height -= cell->ypad * 2;
470 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
471 gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
472 gdk_draw_pixbuf (window,
473 widget->style->black_gc,
475 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
476 draw_rect.x - pix_rect.x,
477 draw_rect.y - pix_rect.y,
482 GDK_RGB_DITHER_NORMAL,