]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
add 2 properties (PIXBUF_EXPANDER_OPEN and PIXBUF_EXPANDER_CLOSED)
[~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       g_object_notify (object, "pixbuf");
177       break;
178     case PROP_PIXBUF_EXPANDER_OPEN:
179       pixbuf = (GdkPixbuf*) g_value_get_object (value);
180       if (pixbuf)
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");
186       break;
187     case PROP_PIXBUF_EXPANDER_CLOSED:
188       pixbuf = (GdkPixbuf*) g_value_get_object (value);
189       if (pixbuf)
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");
195       break;
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
198       break;
199     }
200 }
201
202 /**
203  * gtk_cell_renderer_pixbuf_new:
204  * 
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
211  * #GtkTreeView.
212  * 
213  * Return value: the new cell renderer
214  **/
215 GtkCellRenderer *
216 gtk_cell_renderer_pixbuf_new (void)
217 {
218   return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
219 }
220
221 static void
222 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
223                                    GtkWidget       *widget,
224                                    GdkRectangle    *cell_area,
225                                    gint            *x_offset,
226                                    gint            *y_offset,
227                                    gint            *width,
228                                    gint            *height)
229 {
230   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
231   gint pixbuf_width = 0;
232   gint pixbuf_height = 0;
233   gint calc_width;
234   gint calc_height;
235
236   if (cellpixbuf->pixbuf)
237     {
238       pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
239       pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
240     }
241   if (cellpixbuf->pixbuf_expander_open)
242     {
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));
245     }
246   if (cellpixbuf->pixbuf_expander_closed)
247     {
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));
250     }
251   
252   calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
253   calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
254   
255   if (x_offset) *x_offset = 0;
256   if (y_offset) *y_offset = 0;
257
258   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
259     {
260       if (x_offset)
261         {
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;
264         }
265       if (y_offset)
266         {
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;
269         }
270     }
271
272   if (calc_width)
273     *width = calc_width;
274   
275   if (height)
276     *height = calc_height;
277 }
278
279 static void
280 gtk_cell_renderer_pixbuf_render (GtkCellRenderer    *cell,
281                                  GdkWindow          *window,
282                                  GtkWidget          *widget,
283                                  GdkRectangle       *background_area,
284                                  GdkRectangle       *cell_area,
285                                  GdkRectangle       *expose_area,
286                                  guint               flags)
287
288 {
289   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
290   GdkPixbuf *pixbuf;
291   guchar *pixels;
292   gint rowstride;
293   GdkRectangle pix_rect;
294   GdkRectangle draw_rect;
295
296   pixbuf = cellpixbuf->pixbuf;
297   if (cell->is_expander)
298     {
299       if (cell->is_expanded &&
300           cellpixbuf->pixbuf_expander_open != NULL)
301         pixbuf = cellpixbuf->pixbuf_expander_open;
302       else if (! cell->is_expanded &&
303                cellpixbuf->pixbuf_expander_closed != NULL)
304         pixbuf = cellpixbuf->pixbuf_expander_closed;
305     }
306
307   if (!pixbuf)
308     return;
309
310   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
311   pixels = gdk_pixbuf_get_pixels (pixbuf);
312
313   gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
314                                      &pix_rect.x,
315                                      &pix_rect.y,
316                                      &pix_rect.width,
317                                      &pix_rect.height);
318   
319   pix_rect.x += cell_area->x;
320   pix_rect.y += cell_area->y;
321   pix_rect.width -= cell->xpad * 2;
322   pix_rect.height -= cell->ypad * 2;
323   
324   if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
325     gdk_pixbuf_render_to_drawable_alpha (pixbuf,
326                                          window,
327                                          /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
328                                          draw_rect.x - pix_rect.x,
329                                          draw_rect.y - pix_rect.y,
330                                          draw_rect.x,
331                                          draw_rect.y,
332                                          draw_rect.width,
333                                          draw_rect.height,
334                                          GDK_PIXBUF_ALPHA_FULL,
335                                          0,
336                                          GDK_RGB_DITHER_NORMAL,
337                                          0, 0);
338 }