]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
Minor documentation fixes
[~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_finalize   (GObject                    *object);
35 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
36                                                           GtkWidget             *widget);
37 static void gtk_cell_renderer_pixbuf_get_size   (GtkCellRenderer            *cell,
38                                                  GtkWidget                  *widget,
39                                                  GdkRectangle               *rectangle,
40                                                  gint                       *x_offset,
41                                                  gint                       *y_offset,
42                                                  gint                       *width,
43                                                  gint                       *height);
44 static void gtk_cell_renderer_pixbuf_render     (GtkCellRenderer            *cell,
45                                                  GdkWindow                  *window,
46                                                  GtkWidget                  *widget,
47                                                  GdkRectangle               *background_area,
48                                                  GdkRectangle               *cell_area,
49                                                  GdkRectangle               *expose_area,
50                                                  guint                       flags);
51
52
53 enum {
54         PROP_ZERO,
55         PROP_PIXBUF,
56         PROP_PIXBUF_EXPANDER_OPEN,
57         PROP_PIXBUF_EXPANDER_CLOSED,
58         PROP_STOCK_ID,
59         PROP_STOCK_SIZE,
60         PROP_STOCK_DETAIL
61 };
62
63 static gpointer parent_class;
64
65 #define CELLINFO_KEY "gtk-cell-renderer-pixbuf-info"
66
67 typedef struct _GtkCellRendererPixbufInfo GtkCellRendererPixbufInfo;
68 struct _GtkCellRendererPixbufInfo
69 {
70   gchar *stock_id;
71   GtkIconSize stock_size;
72   gchar *stock_detail;
73 };
74
75 GtkType
76 gtk_cell_renderer_pixbuf_get_type (void)
77 {
78         static GtkType cell_pixbuf_type = 0;
79
80         if (!cell_pixbuf_type)
81         {
82                 static const GTypeInfo cell_pixbuf_info =
83                 {
84                         sizeof (GtkCellRendererPixbufClass),
85                         NULL,           /* base_init */
86                         NULL,           /* base_finalize */
87                         (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
88                         NULL,           /* class_finalize */
89                         NULL,           /* class_data */
90                         sizeof (GtkCellRendererPixbuf),
91                         0,              /* n_preallocs */
92                         (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
93                 };
94
95                 cell_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf", &cell_pixbuf_info, 0);
96         }
97
98         return cell_pixbuf_type;
99 }
100
101 static void
102 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
103 {
104         GtkCellRendererPixbufInfo *cellinfo;
105
106         cellinfo = g_new0 (GtkCellRendererPixbufInfo, 1);
107         cellinfo->stock_size = GTK_ICON_SIZE_MENU;
108         g_object_set_data (G_OBJECT (cellpixbuf), CELLINFO_KEY, cellinfo);
109 }
110
111 static void
112 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
113 {
114         GObjectClass *object_class = G_OBJECT_CLASS (class);
115         GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
116
117         parent_class = g_type_class_peek_parent (class);
118
119         object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
120
121         object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
122         object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
123
124         cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
125         cell_class->render = gtk_cell_renderer_pixbuf_render;
126
127         g_object_class_install_property (object_class,
128                                          PROP_PIXBUF,
129                                          g_param_spec_object ("pixbuf",
130                                                               _("Pixbuf Object"),
131                                                               _("The pixbuf to render"),
132                                                               GDK_TYPE_PIXBUF,
133                                                               G_PARAM_READABLE |
134                                                               G_PARAM_WRITABLE));
135
136         g_object_class_install_property (object_class,
137                                          PROP_PIXBUF_EXPANDER_OPEN,
138                                          g_param_spec_object ("pixbuf_expander_open",
139                                                               _("Pixbuf Expander Open"),
140                                                               _("Pixbuf for open expander"),
141                                                               GDK_TYPE_PIXBUF,
142                                                               G_PARAM_READABLE |
143                                                               G_PARAM_WRITABLE));
144
145         g_object_class_install_property (object_class,
146                                          PROP_PIXBUF_EXPANDER_CLOSED,
147                                          g_param_spec_object ("pixbuf_expander_closed",
148                                                               _("Pixbuf Expander Closed"),
149                                                               _("Pixbuf for closed expander"),
150                                                               GDK_TYPE_PIXBUF,
151                                                               G_PARAM_READABLE |
152                                                               G_PARAM_WRITABLE));
153
154         g_object_class_install_property (object_class,
155                                          PROP_STOCK_ID,
156                                          g_param_spec_string ("stock_id",
157                                                               _("Stock ID"),
158                                                               _("The stock ID of the stock icon to render"),
159                                                               NULL,
160                                                               G_PARAM_READWRITE));
161
162         g_object_class_install_property (object_class,
163                                          PROP_STOCK_SIZE,
164                                          g_param_spec_enum ("stock_size",
165                                                             _("Size"),
166                                                             _("The size of the rendered icon"),
167                                                             GTK_TYPE_ICON_SIZE,
168                                                             GTK_ICON_SIZE_MENU,
169                                                             G_PARAM_READWRITE));
170
171         g_object_class_install_property (object_class,
172                                          PROP_STOCK_DETAIL,
173                                          g_param_spec_string ("stock_detail",
174                                                               _("Detail"),
175                                                               _("Render detail to pass to the theme engine"),
176                                                               NULL,
177                                                               G_PARAM_READWRITE));
178
179 }
180
181 static void
182 gtk_cell_renderer_pixbuf_finalize (GObject *object)
183 {
184   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
185   GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
186
187   if (cellpixbuf->pixbuf && cellinfo->stock_id)
188     g_object_unref (cellpixbuf->pixbuf);
189
190   if (cellinfo->stock_id)
191     g_free (cellinfo->stock_id);
192
193   if (cellinfo->stock_detail)
194     g_free (cellinfo->stock_detail);
195
196   g_free (cellinfo);
197   g_object_set_data (object, CELLINFO_KEY, NULL);
198
199   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
200 }
201
202 static void
203 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
204                                        guint           param_id,
205                                        GValue         *value,
206                                        GParamSpec     *pspec)
207 {
208   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
209   GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
210   
211   switch (param_id)
212     {
213     case PROP_PIXBUF:
214       g_value_set_object (value,
215                           cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
216       break;
217     case PROP_PIXBUF_EXPANDER_OPEN:
218       g_value_set_object (value,
219                           cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
220       break;
221     case PROP_PIXBUF_EXPANDER_CLOSED:
222       g_value_set_object (value,
223                           cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
224       break;
225     case PROP_STOCK_ID:
226       g_value_set_string (value, cellinfo->stock_id);
227       break;
228     case PROP_STOCK_SIZE:
229       g_value_set_enum (value, cellinfo->stock_size);
230       break;
231     case PROP_STOCK_DETAIL:
232       g_value_set_string (value, cellinfo->stock_detail);
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
236       break;
237     }
238 }
239
240
241 static void
242 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
243                                        guint         param_id,
244                                        const GValue *value,
245                                        GParamSpec   *pspec)
246 {
247   GdkPixbuf *pixbuf;
248   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
249   GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (object, CELLINFO_KEY);
250   
251   switch (param_id)
252     {
253     case PROP_PIXBUF:
254       pixbuf = (GdkPixbuf*) g_value_get_object (value);
255       if (pixbuf)
256         g_object_ref (G_OBJECT (pixbuf));
257       if (cellpixbuf->pixbuf)
258         g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
259       cellpixbuf->pixbuf = pixbuf;
260       break;
261     case PROP_PIXBUF_EXPANDER_OPEN:
262       pixbuf = (GdkPixbuf*) g_value_get_object (value);
263       if (pixbuf)
264         g_object_ref (G_OBJECT (pixbuf));
265       if (cellpixbuf->pixbuf_expander_open)
266         g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_open));
267       cellpixbuf->pixbuf_expander_open = pixbuf;
268       break;
269     case PROP_PIXBUF_EXPANDER_CLOSED:
270       pixbuf = (GdkPixbuf*) g_value_get_object (value);
271       if (pixbuf)
272         g_object_ref (G_OBJECT (pixbuf));
273       if (cellpixbuf->pixbuf_expander_closed)
274         g_object_unref (G_OBJECT (cellpixbuf->pixbuf_expander_closed));
275       cellpixbuf->pixbuf_expander_closed = pixbuf;
276       break;
277     case PROP_STOCK_ID:
278       if (cellinfo->stock_id)
279         g_free (cellinfo->stock_id);
280       cellinfo->stock_id = g_strdup (g_value_get_string (value));
281       g_object_notify (G_OBJECT (object), "stock_id");
282       break;
283     case PROP_STOCK_SIZE:
284       cellinfo->stock_size = g_value_get_enum (value);
285       g_object_notify (G_OBJECT (object), "stock_size");
286       break;
287     case PROP_STOCK_DETAIL:
288       if (cellinfo->stock_detail)
289         g_free (cellinfo->stock_detail);
290       cellinfo->stock_detail = g_strdup (g_value_get_string (value));
291       g_object_notify (G_OBJECT (object), "stock_detail");
292       break;
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
295       break;
296     }
297
298   if (cellpixbuf->pixbuf && cellinfo->stock_id)
299     {
300       g_object_unref (cellpixbuf->pixbuf);
301       cellpixbuf->pixbuf = NULL;
302     }
303 }
304
305 /**
306  * gtk_cell_renderer_pixbuf_new:
307  * 
308  * Creates a new #GtkCellRendererPixbuf. Adjust rendering
309  * parameters using object properties. Object properties can be set
310  * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
311  * can bind a property to a value in a #GtkTreeModel. For example, you
312  * can bind the "pixbuf" property on the cell renderer to a pixbuf value
313  * in the model, thus rendering a different image in each row of the
314  * #GtkTreeView.
315  * 
316  * Return value: the new cell renderer
317  **/
318 GtkCellRenderer *
319 gtk_cell_renderer_pixbuf_new (void)
320 {
321   return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
322 }
323
324 static void
325 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
326                                               GtkWidget             *widget)
327 {
328   GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cellpixbuf), CELLINFO_KEY);
329
330   if (cellpixbuf->pixbuf)
331     g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
332
333   cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
334                                                cellinfo->stock_id,
335                                                cellinfo->stock_size,
336                                                cellinfo->stock_detail);
337 }
338
339 static void
340 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
341                                    GtkWidget       *widget,
342                                    GdkRectangle    *cell_area,
343                                    gint            *x_offset,
344                                    gint            *y_offset,
345                                    gint            *width,
346                                    gint            *height)
347 {
348   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
349   GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
350   gint pixbuf_width = 0;
351   gint pixbuf_height = 0;
352   gint calc_width;
353   gint calc_height;
354
355   if (!cellpixbuf->pixbuf && cellinfo->stock_id)
356     gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
357
358   if (cellpixbuf->pixbuf)
359     {
360       pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
361       pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
362     }
363   if (cellpixbuf->pixbuf_expander_open)
364     {
365       pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
366       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
367     }
368   if (cellpixbuf->pixbuf_expander_closed)
369     {
370       pixbuf_width = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
371       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
372     }
373   
374   calc_width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 + pixbuf_width;
375   calc_height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 + pixbuf_height;
376   
377   if (x_offset) *x_offset = 0;
378   if (y_offset) *y_offset = 0;
379
380   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
381     {
382       if (x_offset)
383         {
384           *x_offset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - calc_width - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
385           *x_offset = MAX (*x_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
386         }
387       if (y_offset)
388         {
389           *y_offset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - calc_height - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
390           *y_offset = MAX (*y_offset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
391         }
392     }
393
394   if (calc_width)
395     *width = calc_width;
396   
397   if (height)
398     *height = calc_height;
399 }
400
401 static void
402 gtk_cell_renderer_pixbuf_render (GtkCellRenderer    *cell,
403                                  GdkWindow          *window,
404                                  GtkWidget          *widget,
405                                  GdkRectangle       *background_area,
406                                  GdkRectangle       *cell_area,
407                                  GdkRectangle       *expose_area,
408                                  guint               flags)
409
410 {
411   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
412   GtkCellRendererPixbufInfo *cellinfo = g_object_get_data (G_OBJECT (cell), CELLINFO_KEY);
413   GdkPixbuf *pixbuf;
414   GdkRectangle pix_rect;
415   GdkRectangle draw_rect;
416   gboolean stock_pixbuf = FALSE;
417
418   pixbuf = cellpixbuf->pixbuf;
419   if (cell->is_expander)
420     {
421       if (cell->is_expanded &&
422           cellpixbuf->pixbuf_expander_open != NULL)
423         pixbuf = cellpixbuf->pixbuf_expander_open;
424       else if (! cell->is_expanded &&
425                cellpixbuf->pixbuf_expander_closed != NULL)
426         pixbuf = cellpixbuf->pixbuf_expander_closed;
427     }
428
429   if (!pixbuf && !cellinfo->stock_id)
430     return;
431   else if (!pixbuf && cellinfo->stock_id)
432     stock_pixbuf = TRUE;
433
434   gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
435                                      &pix_rect.x,
436                                      &pix_rect.y,
437                                      &pix_rect.width,
438                                      &pix_rect.height);
439
440   if (stock_pixbuf)
441     pixbuf = cellpixbuf->pixbuf;
442   
443   pix_rect.x += cell_area->x;
444   pix_rect.y += cell_area->y;
445   pix_rect.width -= cell->xpad * 2;
446   pix_rect.height -= cell->ypad * 2;
447
448   if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
449     gdk_pixbuf_render_to_drawable_alpha (pixbuf,
450                                          window,
451                                          /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
452                                          draw_rect.x - pix_rect.x,
453                                          draw_rect.y - pix_rect.y,
454                                          draw_rect.x,
455                                          draw_rect.y,
456                                          draw_rect.width,
457                                          draw_rect.height,
458                                          GDK_PIXBUF_ALPHA_FULL,
459                                          0,
460                                          GDK_RGB_DITHER_NORMAL,
461                                          0, 0);
462 }