]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
General property notification cleanup.
[~andy/gtk] / gtk / gtkcellrendererpixbuf.c
1 /* gtkcellrendererpixbuf.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdlib.h>
21 #include "gtkcellrendererpixbuf.h"
22 #include "gtkintl.h"
23
24 static void gtk_cell_renderer_pixbuf_get_property  (GObject                    *object,
25                                                     guint                       param_id,
26                                                     GValue                     *value,
27                                                     GParamSpec                 *pspec);
28 static void gtk_cell_renderer_pixbuf_set_property  (GObject                    *object,
29                                                     guint                       param_id,
30                                                     const GValue               *value,
31                                                     GParamSpec                 *pspec);
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,
35                                                  GtkWidget                  *widget,
36                                                  GdkRectangle               *rectangle,
37                                                  gint                       *x_offset,
38                                                  gint                       *y_offset,
39                                                  gint                       *width,
40                                                  gint                       *height);
41 static void gtk_cell_renderer_pixbuf_render     (GtkCellRenderer            *cell,
42                                                  GdkWindow                  *window,
43                                                  GtkWidget                  *widget,
44                                                  GdkRectangle               *background_area,
45                                                  GdkRectangle               *cell_area,
46                                                  GdkRectangle               *expose_area,
47                                                  guint                       flags);
48
49
50 enum {
51         PROP_ZERO,
52         PROP_PIXBUF,
53         PROP_PIXBUF_EXPANDER_OPEN,
54         PROP_PIXBUF_EXPANDER_CLOSED
55 };
56
57
58 GtkType
59 gtk_cell_renderer_pixbuf_get_type (void)
60 {
61         static GtkType cell_pixbuf_type = 0;
62
63         if (!cell_pixbuf_type)
64         {
65                 static const GTypeInfo cell_pixbuf_info =
66                 {
67                         sizeof (GtkCellRendererPixbufClass),
68                         NULL,           /* base_init */
69                         NULL,           /* base_finalize */
70                         (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
71                         NULL,           /* class_finalize */
72                         NULL,           /* class_data */
73                         sizeof (GtkCellRendererPixbuf),
74                         0,              /* n_preallocs */
75                         (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
76                 };
77
78                 cell_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf", &cell_pixbuf_info, 0);
79         }
80
81         return cell_pixbuf_type;
82 }
83
84 static void
85 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
86 {
87 }
88
89 static void
90 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
91 {
92         GObjectClass *object_class = G_OBJECT_CLASS (class);
93         GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
94
95         object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
96         object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
97
98         cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
99         cell_class->render = gtk_cell_renderer_pixbuf_render;
100
101         g_object_class_install_property (object_class,
102                                          PROP_PIXBUF,
103                                          g_param_spec_object ("pixbuf",
104                                                               _("Pixbuf Object"),
105                                                               _("The pixbuf to render."),
106                                                               GDK_TYPE_PIXBUF,
107                                                               G_PARAM_READABLE |
108                                                               G_PARAM_WRITABLE));
109
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."),
115                                                               GDK_TYPE_PIXBUF,
116                                                               G_PARAM_READABLE |
117                                                               G_PARAM_WRITABLE));
118
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."),
124                                                               GDK_TYPE_PIXBUF,
125                                                               G_PARAM_READABLE |
126                                                               G_PARAM_WRITABLE));
127 }
128
129 static void
130 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
131                                        guint           param_id,
132                                        GValue         *value,
133                                        GParamSpec     *pspec)
134 {
135   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
136   
137   switch (param_id)
138     {
139     case PROP_PIXBUF:
140       g_value_set_object (value,
141                           cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
142       break;
143     case PROP_PIXBUF_EXPANDER_OPEN:
144       g_value_set_object (value,
145                           cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
146       break;
147     case PROP_PIXBUF_EXPANDER_CLOSED:
148       g_value_set_object (value,
149                           cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
150       break;
151     default:
152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
153       break;
154     }
155 }
156
157
158 static void
159 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
160                                        guint         param_id,
161                                        const GValue *value,
162                                        GParamSpec   *pspec)
163 {
164   GdkPixbuf *pixbuf;
165   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
166   
167   switch (param_id)
168     {
169     case PROP_PIXBUF:
170       pixbuf = (GdkPixbuf*) g_value_get_object (value);
171       if (pixbuf)
172         g_object_ref (G_OBJECT (pixbuf));
173       if (cellpixbuf->pixbuf)
174         g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
175       cellpixbuf->pixbuf = pixbuf;
176       break;
177     case PROP_PIXBUF_EXPANDER_OPEN:
178       pixbuf = (GdkPixbuf*) g_value_get_object (value);
179       if (pixbuf)
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;
184       break;
185     case PROP_PIXBUF_EXPANDER_CLOSED:
186       pixbuf = (GdkPixbuf*) g_value_get_object (value);
187       if (pixbuf)
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;
192       break;
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
195       break;
196     }
197 }
198
199 /**
200  * gtk_cell_renderer_pixbuf_new:
201  * 
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
208  * #GtkTreeView.
209  * 
210  * Return value: the new cell renderer
211  **/
212 GtkCellRenderer *
213 gtk_cell_renderer_pixbuf_new (void)
214 {
215   return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
216 }
217
218 static void
219 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
220                                    GtkWidget       *widget,
221                                    GdkRectangle    *cell_area,
222                                    gint            *x_offset,
223                                    gint            *y_offset,
224                                    gint            *width,
225                                    gint            *height)
226 {
227   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
228   gint pixbuf_width = 0;
229   gint pixbuf_height = 0;
230   gint calc_width;
231   gint calc_height;
232
233   if (cellpixbuf->pixbuf)
234     {
235       pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
236       pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
237     }
238   if (cellpixbuf->pixbuf_expander_open)
239     {
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));
242     }
243   if (cellpixbuf->pixbuf_expander_closed)
244     {
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));
247     }
248   
249   calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
250   calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
251   
252   if (x_offset) *x_offset = 0;
253   if (y_offset) *y_offset = 0;
254
255   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
256     {
257       if (x_offset)
258         {
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;
261         }
262       if (y_offset)
263         {
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;
266         }
267     }
268
269   if (calc_width)
270     *width = calc_width;
271   
272   if (height)
273     *height = calc_height;
274 }
275
276 static void
277 gtk_cell_renderer_pixbuf_render (GtkCellRenderer    *cell,
278                                  GdkWindow          *window,
279                                  GtkWidget          *widget,
280                                  GdkRectangle       *background_area,
281                                  GdkRectangle       *cell_area,
282                                  GdkRectangle       *expose_area,
283                                  guint               flags)
284
285 {
286   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
287   GdkPixbuf *pixbuf;
288   GdkRectangle pix_rect;
289   GdkRectangle draw_rect;
290
291   pixbuf = cellpixbuf->pixbuf;
292   if (cell->is_expander)
293     {
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;
300     }
301
302   if (!pixbuf)
303     return;
304
305   gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
306                                      &pix_rect.x,
307                                      &pix_rect.y,
308                                      &pix_rect.width,
309                                      &pix_rect.height);
310   
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;
315   
316   if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
317     gdk_pixbuf_render_to_drawable_alpha (pixbuf,
318                                          window,
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,
322                                          draw_rect.x,
323                                          draw_rect.y,
324                                          draw_rect.width,
325                                          draw_rect.height,
326                                          GDK_PIXBUF_ALPHA_FULL,
327                                          0,
328                                          GDK_RGB_DITHER_NORMAL,
329                                          0, 0);
330 }