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