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