]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19 #include <stdlib.h>
20 #include "gtkcellrendererpixbuf.h"
21 #include "gtkiconfactory.h"
22 #include "gtkiconhelperprivate.h"
23 #include "gtkicontheme.h"
24 #include "gtkintl.h"
25 #include "gtkprivate.h"
26 #include "a11y/gtkimagecellaccessible.h"
27
28
29 /**
30  * SECTION:gtkcellrendererpixbuf
31  * @Short_description: Renders a pixbuf in a cell
32  * @Title: GtkCellRendererPixbuf
33  *
34  * A #GtkCellRendererPixbuf can be used to render an image in a cell. It allows
35  * to render either a given #GdkPixbuf (set via the
36  * #GtkCellRendererPixbuf:pixbuf property) or a stock icon (set via the
37  * #GtkCellRendererPixbuf:stock-id property).
38  *
39  * To support the tree view, #GtkCellRendererPixbuf also supports rendering two
40  * alternative pixbufs, when the #GtkCellRenderer:is-expander property is %TRUE.
41  * If the #GtkCellRenderer:is-expanded property is %TRUE and the
42  * #GtkCellRendererPixbuf:pixbuf-expander-open property is set to a pixbuf, it
43  * renders that pixbuf, if the #GtkCellRenderer:is-expanded property is %FALSE
44  * and the #GtkCellRendererPixbuf:pixbuf-expander-closed property is set to a
45  * pixbuf, it renders that one.
46  */
47
48
49 static void gtk_cell_renderer_pixbuf_get_property  (GObject                    *object,
50                                                     guint                       param_id,
51                                                     GValue                     *value,
52                                                     GParamSpec                 *pspec);
53 static void gtk_cell_renderer_pixbuf_set_property  (GObject                    *object,
54                                                     guint                       param_id,
55                                                     const GValue               *value,
56                                                     GParamSpec                 *pspec);
57 static void gtk_cell_renderer_pixbuf_get_size   (GtkCellRenderer            *cell,
58                                                  GtkWidget                  *widget,
59                                                  const GdkRectangle         *rectangle,
60                                                  gint                       *x_offset,
61                                                  gint                       *y_offset,
62                                                  gint                       *width,
63                                                  gint                       *height);
64 static void gtk_cell_renderer_pixbuf_render     (GtkCellRenderer            *cell,
65                                                  cairo_t                    *cr,
66                                                  GtkWidget                  *widget,
67                                                  const GdkRectangle         *background_area,
68                                                  const GdkRectangle         *cell_area,
69                                                  GtkCellRendererState        flags);
70
71
72 enum {
73   PROP_0,
74   PROP_PIXBUF,
75   PROP_PIXBUF_EXPANDER_OPEN,
76   PROP_PIXBUF_EXPANDER_CLOSED,
77   PROP_STOCK_ID,
78   PROP_STOCK_SIZE,
79   PROP_STOCK_DETAIL,
80   PROP_FOLLOW_STATE,
81   PROP_ICON_NAME,
82   PROP_GICON
83 };
84
85
86 struct _GtkCellRendererPixbufPrivate
87 {
88   GtkIconHelper *icon_helper;
89   GtkIconSize    icon_size;
90
91   GdkPixbuf *pixbuf_expander_open;
92   GdkPixbuf *pixbuf_expander_closed;
93
94   gboolean follow_state;
95
96   gchar *stock_detail;
97 };
98
99
100 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
101
102
103 static void
104 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
105 {
106   GtkCellRendererPixbufPrivate *priv;
107
108   cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
109                                                   GTK_TYPE_CELL_RENDERER_PIXBUF,
110                                                   GtkCellRendererPixbufPrivate);
111   priv = cellpixbuf->priv;
112   priv->icon_helper = _gtk_icon_helper_new ();
113   priv->icon_size = GTK_ICON_SIZE_MENU;
114 }
115
116 static void
117 gtk_cell_renderer_pixbuf_finalize (GObject *object)
118 {
119   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
120   GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
121
122   g_clear_object (&priv->icon_helper);
123
124   if (priv->pixbuf_expander_open)
125     g_object_unref (priv->pixbuf_expander_open);
126   if (priv->pixbuf_expander_closed)
127     g_object_unref (priv->pixbuf_expander_closed);
128
129   g_free (priv->stock_detail);
130
131   G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
132 }
133
134 static void
135 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
136 {
137   GObjectClass *object_class = G_OBJECT_CLASS (class);
138   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
139
140   object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
141
142   object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
143   object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
144
145   cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
146   cell_class->render = gtk_cell_renderer_pixbuf_render;
147
148   g_object_class_install_property (object_class,
149                                    PROP_PIXBUF,
150                                    g_param_spec_object ("pixbuf",
151                                                         P_("Pixbuf Object"),
152                                                         P_("The pixbuf to render"),
153                                                         GDK_TYPE_PIXBUF,
154                                                         GTK_PARAM_READWRITE));
155
156   g_object_class_install_property (object_class,
157                                    PROP_PIXBUF_EXPANDER_OPEN,
158                                    g_param_spec_object ("pixbuf-expander-open",
159                                                         P_("Pixbuf Expander Open"),
160                                                         P_("Pixbuf for open expander"),
161                                                         GDK_TYPE_PIXBUF,
162                                                         GTK_PARAM_READWRITE));
163
164   g_object_class_install_property (object_class,
165                                    PROP_PIXBUF_EXPANDER_CLOSED,
166                                    g_param_spec_object ("pixbuf-expander-closed",
167                                                         P_("Pixbuf Expander Closed"),
168                                                         P_("Pixbuf for closed expander"),
169                                                         GDK_TYPE_PIXBUF,
170                                                         GTK_PARAM_READWRITE));
171
172   g_object_class_install_property (object_class,
173                                    PROP_STOCK_ID,
174                                    g_param_spec_string ("stock-id",
175                                                         P_("Stock ID"),
176                                                         P_("The stock ID of the stock icon to render"),
177                                                         NULL,
178                                                         GTK_PARAM_READWRITE));
179
180   g_object_class_install_property (object_class,
181                                    PROP_STOCK_SIZE,
182                                    g_param_spec_uint ("stock-size",
183                                                       P_("Size"),
184                                                       P_("The GtkIconSize value that specifies the size of the rendered icon"),
185                                                       0,
186                                                       G_MAXUINT,
187                                                       GTK_ICON_SIZE_MENU,
188                                                       GTK_PARAM_READWRITE));
189
190   g_object_class_install_property (object_class,
191                                    PROP_STOCK_DETAIL,
192                                    g_param_spec_string ("stock-detail",
193                                                         P_("Detail"),
194                                                         P_("Render detail to pass to the theme engine"),
195                                                         NULL,
196                                                         GTK_PARAM_READWRITE));
197
198   
199   /**
200    * GtkCellRendererPixbuf:icon-name:
201    *
202    * The name of the themed icon to display.
203    * This property only has an effect if not overridden by "stock_id" 
204    * or "pixbuf" properties.
205    *
206    * Since: 2.8 
207    */
208   g_object_class_install_property (object_class,
209                                    PROP_ICON_NAME,
210                                    g_param_spec_string ("icon-name",
211                                                         P_("Icon Name"),
212                                                         P_("The name of the icon from the icon theme"),
213                                                         NULL,
214                                                         GTK_PARAM_READWRITE));
215
216   /**
217    * GtkCellRendererPixbuf:follow-state:
218    *
219    * Specifies whether the rendered pixbuf should be colorized
220    * according to the #GtkCellRendererState.
221    *
222    * Since: 2.8
223    */
224   g_object_class_install_property (object_class,
225                                    PROP_FOLLOW_STATE,
226                                    g_param_spec_boolean ("follow-state",
227                                                          P_("Follow State"),
228                                                          P_("Whether the rendered pixbuf should be "
229                                                             "colorized according to the state"),
230                                                          FALSE,
231                                                          GTK_PARAM_READWRITE));
232
233   /**
234    * GtkCellRendererPixbuf:gicon:
235    *
236    * The GIcon representing the icon to display.
237    * If the icon theme is changed, the image will be updated
238    * automatically.
239    *
240    * Since: 2.14
241    */
242   g_object_class_install_property (object_class,
243                                    PROP_GICON,
244                                    g_param_spec_object ("gicon",
245                                                         P_("Icon"),
246                                                         P_("The GIcon being displayed"),
247                                                         G_TYPE_ICON,
248                                                         GTK_PARAM_READWRITE));
249
250
251
252   g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
253
254   gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_IMAGE_CELL_ACCESSIBLE);
255 }
256
257 static void
258 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
259                                        guint           param_id,
260                                        GValue         *value,
261                                        GParamSpec     *pspec)
262 {
263   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
264   GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
265
266   switch (param_id)
267     {
268     case PROP_PIXBUF:
269       g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
270       break;
271     case PROP_PIXBUF_EXPANDER_OPEN:
272       g_value_set_object (value, priv->pixbuf_expander_open);
273       break;
274     case PROP_PIXBUF_EXPANDER_CLOSED:
275       g_value_set_object (value, priv->pixbuf_expander_closed);
276       break;
277     case PROP_STOCK_ID:
278       g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
279       break;
280     case PROP_STOCK_SIZE:
281       g_value_set_uint (value, priv->icon_size);
282       break;
283     case PROP_STOCK_DETAIL:
284       g_value_set_string (value, priv->stock_detail);
285       break;
286     case PROP_FOLLOW_STATE:
287       g_value_set_boolean (value, priv->follow_state);
288       break;
289     case PROP_ICON_NAME:
290       g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
291       break;
292     case PROP_GICON:
293       g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
294       break;
295     default:
296       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
297       break;
298     }
299 }
300
301 static void
302 gtk_cell_renderer_pixbuf_reset (GtkCellRendererPixbuf *cellpixbuf)
303 {
304   GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
305   GtkImageType storage_type = _gtk_icon_helper_get_storage_type (priv->icon_helper);
306
307   switch (storage_type)
308     {
309     case GTK_IMAGE_PIXBUF:
310       g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
311       break;
312     case GTK_IMAGE_STOCK:
313       g_object_notify (G_OBJECT (cellpixbuf), "stock-id");      
314       break;
315     case GTK_IMAGE_ICON_NAME:
316       g_object_notify (G_OBJECT (cellpixbuf), "icon-name");
317       break;
318     case GTK_IMAGE_GICON:
319       g_object_notify (G_OBJECT (cellpixbuf), "gicon");
320       break;
321     case GTK_IMAGE_EMPTY:
322     default:
323       break;
324     }
325
326   _gtk_icon_helper_clear (priv->icon_helper);
327 }
328
329 static void
330 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
331                                        guint         param_id,
332                                        const GValue *value,
333                                        GParamSpec   *pspec)
334 {
335   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
336   GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
337
338   switch (param_id)
339     {
340     case PROP_PIXBUF:
341       gtk_cell_renderer_pixbuf_reset (cellpixbuf);
342       _gtk_icon_helper_set_pixbuf (priv->icon_helper, g_value_get_object (value));
343       break;
344     case PROP_PIXBUF_EXPANDER_OPEN:
345       if (priv->pixbuf_expander_open)
346         g_object_unref (priv->pixbuf_expander_open);
347       priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
348       break;
349     case PROP_PIXBUF_EXPANDER_CLOSED:
350       if (priv->pixbuf_expander_closed)
351         g_object_unref (priv->pixbuf_expander_closed);
352       priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
353       break;
354     case PROP_STOCK_ID:
355       gtk_cell_renderer_pixbuf_reset (cellpixbuf);
356       _gtk_icon_helper_set_stock_id (priv->icon_helper, g_value_get_string (value), priv->icon_size);
357       break;
358     case PROP_STOCK_SIZE:
359       priv->icon_size = g_value_get_uint (value);
360       _gtk_icon_helper_set_icon_size (priv->icon_helper, priv->icon_size);
361       break;
362     case PROP_STOCK_DETAIL:
363       g_free (priv->stock_detail);
364       priv->stock_detail = g_value_dup_string (value);
365       break;
366     case PROP_ICON_NAME:
367       gtk_cell_renderer_pixbuf_reset (cellpixbuf);
368       _gtk_icon_helper_set_icon_name (priv->icon_helper, g_value_get_string (value), priv->icon_size);
369       break;
370     case PROP_FOLLOW_STATE:
371       priv->follow_state = g_value_get_boolean (value);
372       break;
373     case PROP_GICON:
374       gtk_cell_renderer_pixbuf_reset (cellpixbuf);
375       _gtk_icon_helper_set_gicon (priv->icon_helper, g_value_get_object (value), priv->icon_size);
376       break;
377     default:
378       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
379       break;
380     }
381 }
382
383 /**
384  * gtk_cell_renderer_pixbuf_new:
385  * 
386  * Creates a new #GtkCellRendererPixbuf. Adjust rendering
387  * parameters using object properties. Object properties can be set
388  * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
389  * can bind a property to a value in a #GtkTreeModel. For example, you
390  * can bind the "pixbuf" property on the cell renderer to a pixbuf value
391  * in the model, thus rendering a different image in each row of the
392  * #GtkTreeView.
393  * 
394  * Return value: the new cell renderer
395  **/
396 GtkCellRenderer *
397 gtk_cell_renderer_pixbuf_new (void)
398 {
399   return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
400 }
401
402 static void
403 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer    *cell,
404                                    GtkWidget          *widget,
405                                    const GdkRectangle *cell_area,
406                                    gint               *x_offset,
407                                    gint               *y_offset,
408                                    gint               *width,
409                                    gint               *height)
410 {
411   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
412   GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
413   gint pixbuf_width  = 0;
414   gint pixbuf_height = 0;
415   gint calc_width;
416   gint calc_height;
417   gint xpad, ypad;
418   GtkStyleContext *context;
419
420   context = gtk_widget_get_style_context (widget);
421   gtk_style_context_save (context);
422   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
423
424   if (!_gtk_icon_helper_get_is_empty (priv->icon_helper))
425     _gtk_icon_helper_get_size (priv->icon_helper, 
426                                gtk_widget_get_style_context (widget),
427                                &pixbuf_width, &pixbuf_height);
428
429   gtk_style_context_restore (context);
430
431   if (priv->pixbuf_expander_open)
432     {
433       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
434       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
435     }
436   if (priv->pixbuf_expander_closed)
437     {
438       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
439       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
440     }
441
442   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
443   calc_width  = (gint) xpad * 2 + pixbuf_width;
444   calc_height = (gint) ypad * 2 + pixbuf_height;
445   
446   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
447     {
448       gfloat xalign, yalign;
449
450       gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
451       if (x_offset)
452         {
453           *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
454                         (1.0 - xalign) : xalign) *
455                        (cell_area->width - calc_width));
456           *x_offset = MAX (*x_offset, 0);
457         }
458       if (y_offset)
459         {
460           *y_offset = (yalign *
461                        (cell_area->height - calc_height));
462           *y_offset = MAX (*y_offset, 0);
463         }
464     }
465   else
466     {
467       if (x_offset) *x_offset = 0;
468       if (y_offset) *y_offset = 0;
469     }
470
471   if (width)
472     *width = calc_width;
473   
474   if (height)
475     *height = calc_height;
476 }
477
478 static void
479 gtk_cell_renderer_pixbuf_render (GtkCellRenderer      *cell,
480                                  cairo_t              *cr,
481                                  GtkWidget            *widget,
482                                  const GdkRectangle   *background_area,
483                                  const GdkRectangle   *cell_area,
484                                  GtkCellRendererState  flags)
485
486 {
487   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
488   GtkCellRendererPixbufPrivate *priv = cellpixbuf->priv;
489   GtkStyleContext *context;
490   GdkRectangle pix_rect;
491   GdkRectangle draw_rect;
492   gboolean is_expander;
493   gint xpad, ypad;
494   GtkStateFlags state;
495   GtkIconHelper *icon_helper = NULL;
496
497   gtk_cell_renderer_pixbuf_get_size (cell, widget, (GdkRectangle *) cell_area,
498                                      &pix_rect.x, 
499                                      &pix_rect.y,
500                                      &pix_rect.width,
501                                      &pix_rect.height);
502
503   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
504   pix_rect.x += cell_area->x + xpad;
505   pix_rect.y += cell_area->y + ypad;
506   pix_rect.width -= xpad * 2;
507   pix_rect.height -= ypad * 2;
508
509   if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
510     return;
511
512   context = gtk_widget_get_style_context (widget);
513   gtk_style_context_save (context);
514
515   state = GTK_STATE_FLAG_NORMAL;
516
517   if (!gtk_widget_get_sensitive (widget) ||
518       !gtk_cell_renderer_get_sensitive (cell))
519     state |= GTK_STATE_FLAG_INSENSITIVE;
520   else if (priv->follow_state && 
521            (flags & (GTK_CELL_RENDERER_SELECTED |
522                      GTK_CELL_RENDERER_PRELIT)) != 0)
523     state = gtk_cell_renderer_get_state (cell, widget, flags);
524
525   gtk_style_context_set_state (context, state);
526   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
527
528   g_object_get (cell, "is-expander", &is_expander, NULL);
529   if (is_expander)
530     {
531       gboolean is_expanded;
532
533       g_object_get (cell, "is-expanded", &is_expanded, NULL);
534
535       if (is_expanded && priv->pixbuf_expander_open != NULL)
536         {
537           icon_helper = _gtk_icon_helper_new ();
538           _gtk_icon_helper_set_pixbuf (icon_helper, priv->pixbuf_expander_open);
539         }
540       else if (!is_expanded && priv->pixbuf_expander_closed != NULL)
541         {
542           icon_helper = _gtk_icon_helper_new ();
543           _gtk_icon_helper_set_pixbuf (icon_helper, priv->pixbuf_expander_closed);
544         }
545     }
546
547   if (icon_helper == NULL)
548     icon_helper = g_object_ref (priv->icon_helper);
549
550   _gtk_icon_helper_draw (icon_helper,
551                          context, cr,
552                          pix_rect.x, pix_rect.y);
553   g_object_unref (icon_helper);
554
555   gtk_style_context_restore (context);
556 }