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