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_get_size (GtkCellRenderer *cell,
36 GdkRectangle *rectangle,
41 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
44 GdkRectangle *background_area,
45 GdkRectangle *cell_area,
46 GdkRectangle *expose_area,
53 PROP_PIXBUF_EXPANDER_OPEN,
54 PROP_PIXBUF_EXPANDER_CLOSED
59 gtk_cell_renderer_pixbuf_get_type (void)
61 static GtkType cell_pixbuf_type = 0;
63 if (!cell_pixbuf_type)
65 static const GTypeInfo cell_pixbuf_info =
67 sizeof (GtkCellRendererPixbufClass),
69 NULL, /* base_finalize */
70 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
73 sizeof (GtkCellRendererPixbuf),
75 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
78 cell_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf", &cell_pixbuf_info, 0);
81 return cell_pixbuf_type;
85 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
90 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
92 GObjectClass *object_class = G_OBJECT_CLASS (class);
93 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
95 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
96 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
98 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
99 cell_class->render = gtk_cell_renderer_pixbuf_render;
101 g_object_class_install_property (object_class,
103 g_param_spec_object ("pixbuf",
105 _("The pixbuf to render."),
110 g_object_class_install_property (object_class,
111 PROP_PIXBUF_EXPANDER_OPEN,
112 g_param_spec_object ("pixbuf_expander_open",
113 _("Pixbuf Expander Open"),
114 _("Pixbuf for open expander."),
119 g_object_class_install_property (object_class,
120 PROP_PIXBUF_EXPANDER_CLOSED,
121 g_param_spec_object ("pixbuf_expander_closed",
122 _("Pixbuf Expander Closed"),
123 _("Pixbuf for closed expander."),
130 gtk_cell_renderer_pixbuf_get_property (GObject *object,
135 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
140 g_value_set_object (value,
141 cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
143 case PROP_PIXBUF_EXPANDER_OPEN:
144 g_value_set_object (value,
145 cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
147 case PROP_PIXBUF_EXPANDER_CLOSED:
148 g_value_set_object (value,
149 cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
159 gtk_cell_renderer_pixbuf_set_property (GObject *object,
165 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
170 pixbuf = (GdkPixbuf*) g_value_get_object (value);
172 g_object_ref (G_OBJECT (pixbuf));
173 if (cellpixbuf->pixbuf)
174 g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
175 cellpixbuf->pixbuf = pixbuf;
177 case PROP_PIXBUF_EXPANDER_OPEN:
178 pixbuf = (GdkPixbuf*) g_value_get_object (value);
180 g_object_ref (G_OBJECT (pixbuf));
181 if (cellpixbuf->pixbuf_expander_open)
182 g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_open));
183 cellpixbuf->pixbuf_expander_open = pixbuf;
185 case PROP_PIXBUF_EXPANDER_CLOSED:
186 pixbuf = (GdkPixbuf*) g_value_get_object (value);
188 g_object_ref (G_OBJECT (pixbuf));
189 if (cellpixbuf->pixbuf_expander_closed)
190 g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_closed));
191 cellpixbuf->pixbuf_expander_closed = pixbuf;
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
200 * gtk_cell_renderer_pixbuf_new:
202 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
203 * parameters using object properties. Object properties can be set
204 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
205 * can bind a property to a value in a #GtkTreeModel. For example, you
206 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
207 * in the model, thus rendering a different image in each row of the
210 * Return value: the new cell renderer
213 gtk_cell_renderer_pixbuf_new (void)
215 return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
219 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
221 GdkRectangle *cell_area,
227 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
228 gint pixbuf_width = 0;
229 gint pixbuf_height = 0;
233 if (cellpixbuf->pixbuf)
235 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
236 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
238 if (cellpixbuf->pixbuf_expander_open)
240 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
241 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
243 if (cellpixbuf->pixbuf_expander_closed)
245 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
246 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
249 calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
250 calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
252 if (x_offset) *x_offset = 0;
253 if (y_offset) *y_offset = 0;
255 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
259 *x_offset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - calc_width - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
260 *x_offset = MAX (*x_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
264 *y_offset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - calc_height - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
265 *y_offset = MAX (*y_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
273 *height = calc_height;
277 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
280 GdkRectangle *background_area,
281 GdkRectangle *cell_area,
282 GdkRectangle *expose_area,
286 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
288 GdkRectangle pix_rect;
289 GdkRectangle draw_rect;
291 pixbuf = cellpixbuf->pixbuf;
292 if (cell->is_expander)
294 if (cell->is_expanded &&
295 cellpixbuf->pixbuf_expander_open != NULL)
296 pixbuf = cellpixbuf->pixbuf_expander_open;
297 else if (! cell->is_expanded &&
298 cellpixbuf->pixbuf_expander_closed != NULL)
299 pixbuf = cellpixbuf->pixbuf_expander_closed;
305 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
311 pix_rect.x += cell_area->x;
312 pix_rect.y += cell_area->y;
313 pix_rect.width -= cell->xpad * 2;
314 pix_rect.height -= cell->ypad * 2;
316 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
317 gdk_pixbuf_render_to_drawable_alpha (pixbuf,
319 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
320 draw_rect.x - pix_rect.x,
321 draw_rect.y - pix_rect.y,
326 GDK_PIXBUF_ALPHA_FULL,
328 GDK_RGB_DITHER_NORMAL,