]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
gtk/gtkcellrendererpixbuf.c: use accessor functions to access GtkWidget
[~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 "gtkiconfactory.h"
24 #include "gtkicontheme.h"
25 #include "gtkintl.h"
26 #include "gtkprivate.h"
27
28
29 static void gtk_cell_renderer_pixbuf_get_property  (GObject                    *object,
30                                                     guint                       param_id,
31                                                     GValue                     *value,
32                                                     GParamSpec                 *pspec);
33 static void gtk_cell_renderer_pixbuf_set_property  (GObject                    *object,
34                                                     guint                       param_id,
35                                                     const GValue               *value,
36                                                     GParamSpec                 *pspec);
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_0,
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   PROP_FOLLOW_STATE,
65   PROP_ICON_NAME,
66   PROP_GICON
67 };
68
69
70 struct _GtkCellRendererPixbufPriv
71 {
72   GtkIconSize stock_size;
73
74   GdkPixbuf *pixbuf;
75   GdkPixbuf *pixbuf_expander_open;
76   GdkPixbuf *pixbuf_expander_closed;
77
78   GIcon *gicon;
79
80   gboolean follow_state;
81
82   gchar *stock_id;
83   gchar *stock_detail;
84   gchar *icon_name;
85 };
86
87
88 G_DEFINE_TYPE (GtkCellRendererPixbuf, gtk_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER)
89
90
91 static void
92 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
93 {
94   GtkCellRendererPixbufPriv *priv;
95
96   cellpixbuf->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellpixbuf,
97                                                   GTK_TYPE_CELL_RENDERER_PIXBUF,
98                                                   GtkCellRendererPixbufPriv);
99   priv = cellpixbuf->priv;
100
101   priv->stock_size = GTK_ICON_SIZE_MENU;
102 }
103
104 static void
105 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
106 {
107   GObjectClass *object_class = G_OBJECT_CLASS (class);
108   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
109
110   object_class->finalize = gtk_cell_renderer_pixbuf_finalize;
111
112   object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
113   object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
114
115   cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
116   cell_class->render = gtk_cell_renderer_pixbuf_render;
117
118   g_object_class_install_property (object_class,
119                                    PROP_PIXBUF,
120                                    g_param_spec_object ("pixbuf",
121                                                         P_("Pixbuf Object"),
122                                                         P_("The pixbuf to render"),
123                                                         GDK_TYPE_PIXBUF,
124                                                         GTK_PARAM_READWRITE));
125
126   g_object_class_install_property (object_class,
127                                    PROP_PIXBUF_EXPANDER_OPEN,
128                                    g_param_spec_object ("pixbuf-expander-open",
129                                                         P_("Pixbuf Expander Open"),
130                                                         P_("Pixbuf for open expander"),
131                                                         GDK_TYPE_PIXBUF,
132                                                         GTK_PARAM_READWRITE));
133
134   g_object_class_install_property (object_class,
135                                    PROP_PIXBUF_EXPANDER_CLOSED,
136                                    g_param_spec_object ("pixbuf-expander-closed",
137                                                         P_("Pixbuf Expander Closed"),
138                                                         P_("Pixbuf for closed expander"),
139                                                         GDK_TYPE_PIXBUF,
140                                                         GTK_PARAM_READWRITE));
141
142   g_object_class_install_property (object_class,
143                                    PROP_STOCK_ID,
144                                    g_param_spec_string ("stock-id",
145                                                         P_("Stock ID"),
146                                                         P_("The stock ID of the stock icon to render"),
147                                                         NULL,
148                                                         GTK_PARAM_READWRITE));
149
150   g_object_class_install_property (object_class,
151                                    PROP_STOCK_SIZE,
152                                    g_param_spec_uint ("stock-size",
153                                                       P_("Size"),
154                                                       P_("The GtkIconSize value that specifies the size of the rendered icon"),
155                                                       0,
156                                                       G_MAXUINT,
157                                                       GTK_ICON_SIZE_MENU,
158                                                       GTK_PARAM_READWRITE));
159
160   g_object_class_install_property (object_class,
161                                    PROP_STOCK_DETAIL,
162                                    g_param_spec_string ("stock-detail",
163                                                         P_("Detail"),
164                                                         P_("Render detail to pass to the theme engine"),
165                                                         NULL,
166                                                         GTK_PARAM_READWRITE));
167
168   
169   /**
170    * GtkCellRendererPixbuf:icon-name:
171    *
172    * The name of the themed icon to display.
173    * This property only has an effect if not overridden by "stock_id" 
174    * or "pixbuf" properties.
175    *
176    * Since: 2.8 
177    */
178   g_object_class_install_property (object_class,
179                                    PROP_ICON_NAME,
180                                    g_param_spec_string ("icon-name",
181                                                         P_("Icon Name"),
182                                                         P_("The name of the icon from the icon theme"),
183                                                         NULL,
184                                                         GTK_PARAM_READWRITE));
185
186   /**
187    * GtkCellRendererPixbuf:follow-state:
188    *
189    * Specifies whether the rendered pixbuf should be colorized
190    * according to the #GtkCellRendererState.
191    *
192    * Since: 2.8
193    */
194   g_object_class_install_property (object_class,
195                                    PROP_FOLLOW_STATE,
196                                    g_param_spec_boolean ("follow-state",
197                                                          P_("Follow State"),
198                                                          P_("Whether the rendered pixbuf should be "
199                                                             "colorized according to the state"),
200                                                          FALSE,
201                                                          GTK_PARAM_READWRITE));
202
203   /**
204    * GtkCellRendererPixbuf:gicon:
205    *
206    * The GIcon representing the icon to display.
207    * If the icon theme is changed, the image will be updated
208    * automatically.
209    *
210    * Since: 2.14
211    */
212   g_object_class_install_property (object_class,
213                                    PROP_GICON,
214                                    g_param_spec_object ("gicon",
215                                                         P_("Icon"),
216                                                         P_("The GIcon being displayed"),
217                                                         G_TYPE_ICON,
218                                                         GTK_PARAM_READWRITE));
219
220
221
222   g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPriv));
223 }
224
225 static void
226 gtk_cell_renderer_pixbuf_finalize (GObject *object)
227 {
228   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
229   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
230
231   if (priv->pixbuf)
232     g_object_unref (priv->pixbuf);
233   if (priv->pixbuf_expander_open)
234     g_object_unref (priv->pixbuf_expander_open);
235   if (priv->pixbuf_expander_closed)
236     g_object_unref (priv->pixbuf_expander_closed);
237
238   g_free (priv->stock_id);
239   g_free (priv->stock_detail);
240   g_free (priv->icon_name);
241
242   if (priv->gicon)
243     g_object_unref (priv->gicon);
244
245   G_OBJECT_CLASS (gtk_cell_renderer_pixbuf_parent_class)->finalize (object);
246 }
247
248 static void
249 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
250                                        guint           param_id,
251                                        GValue         *value,
252                                        GParamSpec     *pspec)
253 {
254   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
255   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
256
257   switch (param_id)
258     {
259     case PROP_PIXBUF:
260       g_value_set_object (value, priv->pixbuf);
261       break;
262     case PROP_PIXBUF_EXPANDER_OPEN:
263       g_value_set_object (value, priv->pixbuf_expander_open);
264       break;
265     case PROP_PIXBUF_EXPANDER_CLOSED:
266       g_value_set_object (value, priv->pixbuf_expander_closed);
267       break;
268     case PROP_STOCK_ID:
269       g_value_set_string (value, priv->stock_id);
270       break;
271     case PROP_STOCK_SIZE:
272       g_value_set_uint (value, priv->stock_size);
273       break;
274     case PROP_STOCK_DETAIL:
275       g_value_set_string (value, priv->stock_detail);
276       break;
277     case PROP_FOLLOW_STATE:
278       g_value_set_boolean (value, priv->follow_state);
279       break;
280     case PROP_ICON_NAME:
281       g_value_set_string (value, priv->icon_name);
282       break;
283     case PROP_GICON:
284       g_value_set_object (value, priv->gicon);
285       break;
286     default:
287       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
288       break;
289     }
290 }
291
292 static void
293 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
294                                        guint         param_id,
295                                        const GValue *value,
296                                        GParamSpec   *pspec)
297 {
298   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
299   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
300
301   switch (param_id)
302     {
303     case PROP_PIXBUF:
304       if (priv->pixbuf)
305         g_object_unref (priv->pixbuf);
306       priv->pixbuf = (GdkPixbuf*) g_value_dup_object (value);
307       if (priv->pixbuf)
308         {
309           if (priv->stock_id)
310             {
311               g_free (priv->stock_id);
312               priv->stock_id = NULL;
313               g_object_notify (object, "stock-id");
314             }
315           if (priv->icon_name)
316             {
317               g_free (priv->icon_name);
318               priv->icon_name = NULL;
319               g_object_notify (object, "icon-name");
320             }
321           if (priv->gicon)
322             {
323               g_object_unref (priv->gicon);
324               priv->gicon = NULL;
325               g_object_notify (object, "gicon");
326             }
327         }
328       break;
329     case PROP_PIXBUF_EXPANDER_OPEN:
330       if (priv->pixbuf_expander_open)
331         g_object_unref (priv->pixbuf_expander_open);
332       priv->pixbuf_expander_open = (GdkPixbuf*) g_value_dup_object (value);
333       break;
334     case PROP_PIXBUF_EXPANDER_CLOSED:
335       if (priv->pixbuf_expander_closed)
336         g_object_unref (priv->pixbuf_expander_closed);
337       priv->pixbuf_expander_closed = (GdkPixbuf*) g_value_dup_object (value);
338       break;
339     case PROP_STOCK_ID:
340       if (priv->stock_id)
341         {
342           if (priv->pixbuf)
343             {
344               g_object_unref (priv->pixbuf);
345               priv->pixbuf = NULL;
346               g_object_notify (object, "pixbuf");
347             }
348           g_free (priv->stock_id);
349         }
350       priv->stock_id = g_value_dup_string (value);
351       if (priv->stock_id)
352         {
353           if (priv->pixbuf)
354             {
355               g_object_unref (priv->pixbuf);
356               priv->pixbuf = NULL;
357               g_object_notify (object, "pixbuf");
358             }
359           if (priv->icon_name)
360             {
361               g_free (priv->icon_name);
362               priv->icon_name = NULL;
363               g_object_notify (object, "icon-name");
364             }
365           if (priv->gicon)
366             {
367               g_object_unref (priv->gicon);
368               priv->gicon = NULL;
369               g_object_notify (object, "gicon");
370             }
371         }
372       break;
373     case PROP_STOCK_SIZE:
374       priv->stock_size = g_value_get_uint (value);
375       break;
376     case PROP_STOCK_DETAIL:
377       g_free (priv->stock_detail);
378       priv->stock_detail = g_value_dup_string (value);
379       break;
380     case PROP_ICON_NAME:
381       if (priv->icon_name)
382         {
383           if (priv->pixbuf)
384             {
385               g_object_unref (priv->pixbuf);
386               priv->pixbuf = NULL;
387               g_object_notify (object, "pixbuf");
388             }
389           g_free (priv->icon_name);
390         }
391       priv->icon_name = g_value_dup_string (value);
392       if (priv->icon_name)
393         {
394           if (priv->pixbuf)
395             {
396               g_object_unref (priv->pixbuf);
397               priv->pixbuf = NULL;
398               g_object_notify (object, "pixbuf");
399             }
400           if (priv->stock_id)
401             {
402               g_free (priv->stock_id);
403               priv->stock_id = NULL;
404               g_object_notify (object, "stock-id");
405             }
406           if (priv->gicon)
407             {
408               g_object_unref (priv->gicon);
409               priv->gicon = NULL;
410               g_object_notify (object, "gicon");
411             }
412         }
413       break;
414     case PROP_FOLLOW_STATE:
415       priv->follow_state = g_value_get_boolean (value);
416       break;
417     case PROP_GICON:
418       if (priv->gicon)
419         {
420           if (priv->pixbuf)
421             {
422               g_object_unref (priv->pixbuf);
423               priv->pixbuf = NULL;
424               g_object_notify (object, "pixbuf");
425             }
426           g_object_unref (priv->gicon);
427         }
428       priv->gicon = (GIcon *) g_value_dup_object (value);
429       if (priv->gicon)
430         {
431           if (priv->pixbuf)
432             {
433               g_object_unref (priv->pixbuf);
434               priv->pixbuf = NULL;
435               g_object_notify (object, "pixbuf");
436             }
437           if (priv->stock_id)
438             {
439               g_free (priv->stock_id);
440               priv->stock_id = NULL;
441               g_object_notify (object, "stock-id");
442             }
443           if (priv->icon_name)
444             {
445               g_free (priv->icon_name);
446               priv->icon_name = NULL;
447               g_object_notify (object, "icon-name");
448             }
449         }
450       break;
451     default:
452       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
453       break;
454     }
455 }
456
457 /**
458  * gtk_cell_renderer_pixbuf_new:
459  * 
460  * Creates a new #GtkCellRendererPixbuf. Adjust rendering
461  * parameters using object properties. Object properties can be set
462  * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
463  * can bind a property to a value in a #GtkTreeModel. For example, you
464  * can bind the "pixbuf" property on the cell renderer to a pixbuf value
465  * in the model, thus rendering a different image in each row of the
466  * #GtkTreeView.
467  * 
468  * Return value: the new cell renderer
469  **/
470 GtkCellRenderer *
471 gtk_cell_renderer_pixbuf_new (void)
472 {
473   return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
474 }
475
476 static void
477 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
478                                               GtkWidget             *widget)
479 {
480   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
481
482   if (priv->pixbuf)
483     g_object_unref (priv->pixbuf);
484
485   priv->pixbuf = gtk_widget_render_icon (widget,
486                                                priv->stock_id,
487                                                priv->stock_size,
488                                                priv->stock_detail);
489
490   g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
491 }
492
493 static void 
494 gtk_cell_renderer_pixbuf_create_themed_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
495                                                GtkWidget             *widget)
496 {
497   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
498   GdkScreen *screen;
499   GtkIconTheme *icon_theme;
500   GtkSettings *settings;
501   gint width, height;
502   GtkIconInfo *info;
503
504   if (priv->pixbuf)
505     {
506       g_object_unref (priv->pixbuf);
507       priv->pixbuf = NULL;
508     }
509
510   screen = gtk_widget_get_screen (GTK_WIDGET (widget));
511   icon_theme = gtk_icon_theme_get_for_screen (screen);
512   settings = gtk_settings_get_for_screen (screen);
513
514   if (!gtk_icon_size_lookup_for_settings (settings,
515                                           priv->stock_size,
516                                           &width, &height))
517     {
518       g_warning ("Invalid icon size %u\n", priv->stock_size);
519       width = height = 24;
520     }
521
522   if (priv->icon_name)
523     info = gtk_icon_theme_lookup_icon (icon_theme,
524                                        priv->icon_name,
525                                        MIN (width, height),
526                                        GTK_ICON_LOOKUP_USE_BUILTIN);
527   else if (priv->gicon)
528     info = gtk_icon_theme_lookup_by_gicon (icon_theme,
529                                            priv->gicon,
530                                            MIN (width, height),
531                                            GTK_ICON_LOOKUP_USE_BUILTIN);
532   else
533     info = NULL;
534
535   if (info)
536     {
537       GtkStyle *style;
538
539       style = gtk_widget_get_style (GTK_WIDGET (widget));
540       priv->pixbuf = gtk_icon_info_load_symbolic_for_style (info,
541                                                             style,
542                                                             GTK_STATE_NORMAL,
543                                                             NULL,
544                                                             NULL);
545       gtk_icon_info_free (info);
546     }
547
548   g_object_notify (G_OBJECT (cellpixbuf), "pixbuf");
549 }
550
551 static GdkPixbuf *
552 create_symbolic_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
553                         GtkWidget             *widget,
554                         GtkStateType           state)
555 {
556   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
557   GdkScreen *screen;
558   GtkIconTheme *icon_theme;
559   GtkSettings *settings;
560   gint width, height;
561   GtkIconInfo *info;
562   GdkPixbuf *pixbuf;
563
564   /* Not a named symbolic icon? */
565   if (priv->icon_name) {
566     if (!g_str_has_suffix (priv->icon_name, "-symbolic"))
567       return NULL;
568   } else if (priv->gicon) {
569     const gchar * const *names;
570     if (!G_IS_THEMED_ICON (priv->gicon))
571       return NULL;
572     names = g_themed_icon_get_names (G_THEMED_ICON (priv->gicon));
573     if (names == NULL || !g_str_has_suffix (names[0], "-symbolic"))
574       return NULL;
575   } else {
576     return NULL;
577   }
578
579   screen = gtk_widget_get_screen (GTK_WIDGET (widget));
580   icon_theme = gtk_icon_theme_get_for_screen (screen);
581   settings = gtk_settings_get_for_screen (screen);
582
583   if (!gtk_icon_size_lookup_for_settings (settings,
584                                           priv->stock_size,
585                                           &width, &height))
586     {
587       g_warning ("Invalid icon size %u\n", priv->stock_size);
588       width = height = 24;
589     }
590
591
592   if (priv->icon_name)
593     info = gtk_icon_theme_lookup_icon (icon_theme,
594                                        priv->icon_name,
595                                        MIN (width, height),
596                                        GTK_ICON_LOOKUP_USE_BUILTIN);
597   else if (priv->gicon)
598     info = gtk_icon_theme_lookup_by_gicon (icon_theme,
599                                            priv->gicon,
600                                            MIN (width, height),
601                                            GTK_ICON_LOOKUP_USE_BUILTIN);
602   else
603     return NULL;
604
605   if (info)
606     {
607       GtkStyle *style;
608
609       style = gtk_widget_get_style (GTK_WIDGET (widget));
610       pixbuf = gtk_icon_info_load_symbolic_for_style (info,
611                                                       style, state,
612                                                       NULL, NULL);
613       gtk_icon_info_free (info);
614       return pixbuf;
615     }
616
617   return NULL;
618 }
619
620 static GdkPixbuf *
621 create_colorized_pixbuf (GdkPixbuf *src, 
622                          GdkColor  *new_color)
623 {
624   gint i, j;
625   gint width, height, has_alpha, src_row_stride, dst_row_stride;
626   gint red_value, green_value, blue_value;
627   guchar *target_pixels;
628   guchar *original_pixels;
629   guchar *pixsrc;
630   guchar *pixdest;
631   GdkPixbuf *dest;
632   
633   red_value = new_color->red / 255.0;
634   green_value = new_color->green / 255.0;
635   blue_value = new_color->blue / 255.0;
636   
637   dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
638                          gdk_pixbuf_get_has_alpha (src),
639                          gdk_pixbuf_get_bits_per_sample (src),
640                          gdk_pixbuf_get_width (src),
641                          gdk_pixbuf_get_height (src));
642   
643   has_alpha = gdk_pixbuf_get_has_alpha (src);
644   width = gdk_pixbuf_get_width (src);
645   height = gdk_pixbuf_get_height (src);
646   src_row_stride = gdk_pixbuf_get_rowstride (src);
647   dst_row_stride = gdk_pixbuf_get_rowstride (dest);
648   target_pixels = gdk_pixbuf_get_pixels (dest);
649   original_pixels = gdk_pixbuf_get_pixels (src);
650   
651   for (i = 0; i < height; i++) {
652     pixdest = target_pixels + i*dst_row_stride;
653     pixsrc = original_pixels + i*src_row_stride;
654     for (j = 0; j < width; j++) {               
655       *pixdest++ = (*pixsrc++ * red_value) >> 8;
656       *pixdest++ = (*pixsrc++ * green_value) >> 8;
657       *pixdest++ = (*pixsrc++ * blue_value) >> 8;
658       if (has_alpha) {
659         *pixdest++ = *pixsrc++;
660       }
661     }
662   }
663   return dest;
664 }
665
666
667 static void
668 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
669                                    GtkWidget       *widget,
670                                    GdkRectangle    *cell_area,
671                                    gint            *x_offset,
672                                    gint            *y_offset,
673                                    gint            *width,
674                                    gint            *height)
675 {
676   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
677   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
678   gint pixbuf_width  = 0;
679   gint pixbuf_height = 0;
680   gint calc_width;
681   gint calc_height;
682   gint xpad, ypad;
683
684   if (!priv->pixbuf)
685     {
686       if (priv->stock_id)
687         gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
688       else if (priv->icon_name || priv->gicon)
689         gtk_cell_renderer_pixbuf_create_themed_pixbuf (cellpixbuf, widget);
690     }
691   
692   if (priv->pixbuf)
693     {
694       pixbuf_width  = gdk_pixbuf_get_width (priv->pixbuf);
695       pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
696     }
697   if (priv->pixbuf_expander_open)
698     {
699       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_open));
700       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_open));
701     }
702   if (priv->pixbuf_expander_closed)
703     {
704       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (priv->pixbuf_expander_closed));
705       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (priv->pixbuf_expander_closed));
706     }
707
708   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
709   calc_width  = (gint) xpad * 2 + pixbuf_width;
710   calc_height = (gint) ypad * 2 + pixbuf_height;
711   
712   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
713     {
714       gfloat xalign, yalign;
715
716       gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
717       if (x_offset)
718         {
719           *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
720                         (1.0 - xalign) : xalign) *
721                        (cell_area->width - calc_width));
722           *x_offset = MAX (*x_offset, 0);
723         }
724       if (y_offset)
725         {
726           *y_offset = (yalign *
727                        (cell_area->height - calc_height));
728           *y_offset = MAX (*y_offset, 0);
729         }
730     }
731   else
732     {
733       if (x_offset) *x_offset = 0;
734       if (y_offset) *y_offset = 0;
735     }
736
737   if (width)
738     *width = calc_width;
739   
740   if (height)
741     *height = calc_height;
742 }
743
744 static void
745 gtk_cell_renderer_pixbuf_render (GtkCellRenderer      *cell,
746                                  GdkWindow            *window,
747                                  GtkWidget            *widget,
748                                  GdkRectangle         *background_area,
749                                  GdkRectangle         *cell_area,
750                                  GdkRectangle         *expose_area,
751                                  GtkCellRendererState  flags)
752
753 {
754   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
755   GtkCellRendererPixbufPriv *priv = cellpixbuf->priv;
756   GdkPixbuf *pixbuf;
757   GdkPixbuf *invisible = NULL;
758   GdkPixbuf *colorized = NULL;
759   GdkPixbuf *symbolic = NULL;
760   GdkRectangle pix_rect;
761   GdkRectangle draw_rect;
762   cairo_t *cr;
763   gboolean is_expander;
764   gint xpad, ypad;
765
766   gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
767                                      &pix_rect.x,
768                                      &pix_rect.y,
769                                      &pix_rect.width,
770                                      &pix_rect.height);
771
772   gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
773   pix_rect.x += cell_area->x + xpad;
774   pix_rect.y += cell_area->y + ypad;
775   pix_rect.width  -= xpad * 2;
776   pix_rect.height -= ypad * 2;
777
778   if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) ||
779       !gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
780     return;
781
782   pixbuf = priv->pixbuf;
783
784   g_object_get (cell, "is-expander", &is_expander, NULL);
785   if (is_expander)
786     {
787       gboolean is_expanded;
788
789       g_object_get (cell, "is-expanded", &is_expanded, NULL);
790
791       if (is_expanded &&
792           priv->pixbuf_expander_open != NULL)
793         pixbuf = priv->pixbuf_expander_open;
794       else if (!is_expanded &&
795                priv->pixbuf_expander_closed != NULL)
796         pixbuf = priv->pixbuf_expander_closed;
797     }
798
799   if (!pixbuf)
800     return;
801
802   if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
803       !gtk_cell_renderer_get_sensitive (cell))
804     {
805       GtkIconSource *source;
806       
807       source = gtk_icon_source_new ();
808       gtk_icon_source_set_pixbuf (source, pixbuf);
809       /* The size here is arbitrary; since size isn't
810        * wildcarded in the source, it isn't supposed to be
811        * scaled by the engine function
812        */
813       gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
814       gtk_icon_source_set_size_wildcarded (source, FALSE);
815
816      invisible = gtk_style_render_icon (gtk_widget_get_style (widget),
817                                         source,
818                                         gtk_widget_get_direction (widget),
819                                         GTK_STATE_INSENSITIVE,
820                                         /* arbitrary */
821                                         (GtkIconSize)-1,
822                                         widget,
823                                         "gtkcellrendererpixbuf");
824      
825      gtk_icon_source_free (source);
826      
827      pixbuf = invisible;
828     }
829   else if (priv->follow_state && 
830            (flags & (GTK_CELL_RENDERER_SELECTED|GTK_CELL_RENDERER_PRELIT)) != 0)
831     {
832       GtkStateType state;
833
834       if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
835         {
836           if (gtk_widget_has_focus (widget))
837             state = GTK_STATE_SELECTED;
838           else
839             state = GTK_STATE_ACTIVE;
840         }
841       else
842         state = GTK_STATE_PRELIGHT;
843
844       symbolic = create_symbolic_pixbuf (cellpixbuf, widget, state);
845       if (!symbolic) {
846         colorized = create_colorized_pixbuf (pixbuf,
847                                              &gtk_widget_get_style (widget)->base[state]);
848
849         pixbuf = colorized;
850       } else {
851         pixbuf = symbolic;
852       }
853     }
854
855   cr = gdk_cairo_create (window);
856   
857   gdk_cairo_set_source_pixbuf (cr, pixbuf, pix_rect.x, pix_rect.y);
858   gdk_cairo_rectangle (cr, &draw_rect);
859   cairo_fill (cr);
860
861   cairo_destroy (cr);
862   
863   if (invisible)
864     g_object_unref (invisible);
865
866   if (colorized)
867     g_object_unref (colorized);
868
869   if (symbolic)
870     g_object_unref (symbolic);
871 }