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;
176 g_object_notify (object, "pixbuf");
178 case PROP_PIXBUF_EXPANDER_OPEN:
179 pixbuf = (GdkPixbuf*) g_value_get_object (value);
181 g_object_ref (G_OBJECT (pixbuf));
182 if (cellpixbuf->pixbuf_expander_open)
183 g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_open));
184 cellpixbuf->pixbuf_expander_open = pixbuf;
185 g_object_notify (object, "pixbuf_expander_open");
187 case PROP_PIXBUF_EXPANDER_CLOSED:
188 pixbuf = (GdkPixbuf*) g_value_get_object (value);
190 g_object_ref (G_OBJECT (pixbuf));
191 if (cellpixbuf->pixbuf_expander_closed)
192 g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_closed));
193 cellpixbuf->pixbuf_expander_closed = pixbuf;
194 g_object_notify (object, "pixbuf_expander_closed");
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
203 * gtk_cell_renderer_pixbuf_new:
205 * Creates a new #GtkCellRendererPixbuf. Adjust rendering
206 * parameters using object properties. Object properties can be set
207 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
208 * can bind a property to a value in a #GtkTreeModel. For example, you
209 * can bind the "pixbuf" property on the cell renderer to a pixbuf value
210 * in the model, thus rendering a different image in each row of the
213 * Return value: the new cell renderer
216 gtk_cell_renderer_pixbuf_new (void)
218 return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
222 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
224 GdkRectangle *cell_area,
230 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
231 gint pixbuf_width = 0;
232 gint pixbuf_height = 0;
236 if (cellpixbuf->pixbuf)
238 pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
239 pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
241 if (cellpixbuf->pixbuf_expander_open)
243 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
244 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
246 if (cellpixbuf->pixbuf_expander_closed)
248 pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
249 pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
252 calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
253 calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
255 if (x_offset) *x_offset = 0;
256 if (y_offset) *y_offset = 0;
258 if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
262 *x_offset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - calc_width - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
263 *x_offset = MAX (*x_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
267 *y_offset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - calc_height - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
268 *y_offset = MAX (*y_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
276 *height = calc_height;
280 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
283 GdkRectangle *background_area,
284 GdkRectangle *cell_area,
285 GdkRectangle *expose_area,
289 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
291 GdkRectangle pix_rect;
292 GdkRectangle draw_rect;
294 pixbuf = cellpixbuf->pixbuf;
295 if (cell->is_expander)
297 if (cell->is_expanded &&
298 cellpixbuf->pixbuf_expander_open != NULL)
299 pixbuf = cellpixbuf->pixbuf_expander_open;
300 else if (! cell->is_expanded &&
301 cellpixbuf->pixbuf_expander_closed != NULL)
302 pixbuf = cellpixbuf->pixbuf_expander_closed;
308 gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
314 pix_rect.x += cell_area->x;
315 pix_rect.y += cell_area->y;
316 pix_rect.width -= cell->xpad * 2;
317 pix_rect.height -= cell->ypad * 2;
319 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
320 gdk_pixbuf_render_to_drawable_alpha (pixbuf,
322 /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
323 draw_rect.x - pix_rect.x,
324 draw_rect.y - pix_rect.y,
329 GDK_PIXBUF_ALPHA_FULL,
331 GDK_RGB_DITHER_NORMAL,