]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
Support insensitive cells in tree views and combo boxes.
[~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 "gtkintl.h"
25
26 static void gtk_cell_renderer_pixbuf_get_property  (GObject                    *object,
27                                                     guint                       param_id,
28                                                     GValue                     *value,
29                                                     GParamSpec                 *pspec);
30 static void gtk_cell_renderer_pixbuf_set_property  (GObject                    *object,
31                                                     guint                       param_id,
32                                                     const GValue               *value,
33                                                     GParamSpec                 *pspec);
34 static void gtk_cell_renderer_pixbuf_init       (GtkCellRendererPixbuf      *celltext);
35 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
36 static void gtk_cell_renderer_pixbuf_finalize   (GObject                    *object);
37 static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
38                                                           GtkWidget             *widget);
39 static void gtk_cell_renderer_pixbuf_get_size   (GtkCellRenderer            *cell,
40                                                  GtkWidget                  *widget,
41                                                  GdkRectangle               *rectangle,
42                                                  gint                       *x_offset,
43                                                  gint                       *y_offset,
44                                                  gint                       *width,
45                                                  gint                       *height);
46 static void gtk_cell_renderer_pixbuf_render     (GtkCellRenderer            *cell,
47                                                  GdkDrawable                *window,
48                                                  GtkWidget                  *widget,
49                                                  GdkRectangle               *background_area,
50                                                  GdkRectangle               *cell_area,
51                                                  GdkRectangle               *expose_area,
52                                                  GtkCellRendererState        flags);
53
54
55 enum {
56         PROP_ZERO,
57         PROP_PIXBUF,
58         PROP_PIXBUF_EXPANDER_OPEN,
59         PROP_PIXBUF_EXPANDER_CLOSED,
60         PROP_STOCK_ID,
61         PROP_STOCK_SIZE,
62         PROP_STOCK_DETAIL
63 };
64
65 static gpointer parent_class;
66
67
68 #define GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_PIXBUF, GtkCellRendererPixbufPrivate))
69
70 typedef struct _GtkCellRendererPixbufPrivate GtkCellRendererPixbufPrivate;
71 struct _GtkCellRendererPixbufPrivate
72 {
73   gchar *stock_id;
74   GtkIconSize stock_size;
75   gchar *stock_detail;
76   GdkPixbuf *insensitive;
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   if (priv->insensitive)
207     g_object_unref (priv->insensitive);
208
209   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
210 }
211
212 static void
213 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
214                                        guint           param_id,
215                                        GValue         *value,
216                                        GParamSpec     *pspec)
217 {
218   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
219   GtkCellRendererPixbufPrivate *priv;
220
221   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
222   
223   switch (param_id)
224     {
225     case PROP_PIXBUF:
226       g_value_set_object (value,
227                           cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
228       break;
229     case PROP_PIXBUF_EXPANDER_OPEN:
230       g_value_set_object (value,
231                           cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
232       break;
233     case PROP_PIXBUF_EXPANDER_CLOSED:
234       g_value_set_object (value,
235                           cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
236       break;
237     case PROP_STOCK_ID:
238       g_value_set_string (value, priv->stock_id);
239       break;
240     case PROP_STOCK_SIZE:
241       g_value_set_uint (value, priv->stock_size);
242       break;
243     case PROP_STOCK_DETAIL:
244       g_value_set_string (value, priv->stock_detail);
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
248       break;
249     }
250 }
251
252
253 static void
254 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
255                                        guint         param_id,
256                                        const GValue *value,
257                                        GParamSpec   *pspec)
258 {
259   GdkPixbuf *pixbuf;
260   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
261   GtkCellRendererPixbufPrivate *priv;
262
263   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
264   
265   switch (param_id)
266     {
267     case PROP_PIXBUF:
268       pixbuf = (GdkPixbuf*) g_value_get_object (value);
269       if (pixbuf)
270         g_object_ref (pixbuf);
271       if (cellpixbuf->pixbuf)
272         g_object_unref (cellpixbuf->pixbuf);
273       cellpixbuf->pixbuf = pixbuf;
274       break;
275     case PROP_PIXBUF_EXPANDER_OPEN:
276       pixbuf = (GdkPixbuf*) g_value_get_object (value);
277       if (pixbuf)
278         g_object_ref (pixbuf);
279       if (cellpixbuf->pixbuf_expander_open)
280         g_object_unref (cellpixbuf->pixbuf_expander_open);
281       cellpixbuf->pixbuf_expander_open = pixbuf;
282       break;
283     case PROP_PIXBUF_EXPANDER_CLOSED:
284       pixbuf = (GdkPixbuf*) g_value_get_object (value);
285       if (pixbuf)
286         g_object_ref (pixbuf);
287       if (cellpixbuf->pixbuf_expander_closed)
288         g_object_unref (cellpixbuf->pixbuf_expander_closed);
289       cellpixbuf->pixbuf_expander_closed = pixbuf;
290       break;
291     case PROP_STOCK_ID:
292       if (priv->stock_id)
293         {
294           if (cellpixbuf->pixbuf)
295             {
296               g_object_unref (cellpixbuf->pixbuf);
297               cellpixbuf->pixbuf = NULL;
298             }
299           g_free (priv->stock_id);
300         }
301       priv->stock_id = g_strdup (g_value_get_string (value));
302       break;
303     case PROP_STOCK_SIZE:
304       priv->stock_size = g_value_get_uint (value);
305       break;
306     case PROP_STOCK_DETAIL:
307       if (priv->stock_detail)
308         g_free (priv->stock_detail);
309       priv->stock_detail = g_strdup (g_value_get_string (value));
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
313       break;
314     }
315
316   if (cellpixbuf->pixbuf && priv->stock_id)
317     {
318       g_object_unref (cellpixbuf->pixbuf);
319       cellpixbuf->pixbuf = NULL;
320     }
321 }
322
323 /**
324  * gtk_cell_renderer_pixbuf_new:
325  * 
326  * Creates a new #GtkCellRendererPixbuf. Adjust rendering
327  * parameters using object properties. Object properties can be set
328  * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
329  * can bind a property to a value in a #GtkTreeModel. For example, you
330  * can bind the "pixbuf" property on the cell renderer to a pixbuf value
331  * in the model, thus rendering a different image in each row of the
332  * #GtkTreeView.
333  * 
334  * Return value: the new cell renderer
335  **/
336 GtkCellRenderer *
337 gtk_cell_renderer_pixbuf_new (void)
338 {
339   return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
340 }
341
342 static void
343 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
344                                               GtkWidget             *widget)
345 {
346   GtkCellRendererPixbufPrivate *priv;
347
348   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
349
350   if (cellpixbuf->pixbuf)
351     g_object_unref (cellpixbuf->pixbuf);
352
353   cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
354                                                priv->stock_id,
355                                                priv->stock_size,
356                                                priv->stock_detail);
357 }
358
359 static void
360 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
361                                    GtkWidget       *widget,
362                                    GdkRectangle    *cell_area,
363                                    gint            *x_offset,
364                                    gint            *y_offset,
365                                    gint            *width,
366                                    gint            *height)
367 {
368   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
369   GtkCellRendererPixbufPrivate *priv;
370   gint pixbuf_width  = 0;
371   gint pixbuf_height = 0;
372   gint calc_width;
373   gint calc_height;
374
375   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
376
377   if (!cellpixbuf->pixbuf && priv->stock_id)
378     gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
379
380   if (cellpixbuf->pixbuf)
381     {
382       pixbuf_width  = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
383       pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
384     }
385   if (cellpixbuf->pixbuf_expander_open)
386     {
387       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
388       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
389     }
390   if (cellpixbuf->pixbuf_expander_closed)
391     {
392       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
393       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
394     }
395   
396   calc_width  = (gint) cell->xpad * 2 + pixbuf_width;
397   calc_height = (gint) cell->ypad * 2 + pixbuf_height;
398   
399   if (x_offset) *x_offset = 0;
400   if (y_offset) *y_offset = 0;
401
402   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
403     {
404       if (x_offset)
405         {
406           *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
407                         1.0 - cell->xalign : cell->xalign) * 
408                        (cell_area->width - calc_width - 2 * cell->xpad));
409           *x_offset = MAX (*x_offset, 0) + cell->xpad;
410         }
411       if (y_offset)
412         {
413           *y_offset = (cell->yalign *
414                        (cell_area->height - calc_height - 2 * cell->ypad));
415           *y_offset = MAX (*y_offset, 0) + cell->ypad;
416         }
417     }
418
419   if (width)
420     *width = calc_width;
421   
422   if (height)
423     *height = calc_height;
424 }
425
426 static void
427 gtk_cell_renderer_pixbuf_render (GtkCellRenderer      *cell,
428                                  GdkWindow            *window,
429                                  GtkWidget            *widget,
430                                  GdkRectangle         *background_area,
431                                  GdkRectangle         *cell_area,
432                                  GdkRectangle         *expose_area,
433                                  GtkCellRendererState  flags)
434
435 {
436   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
437   GtkCellRendererPixbufPrivate *priv;
438   GdkPixbuf *pixbuf;
439   GdkRectangle pix_rect;
440   GdkRectangle draw_rect;
441   gboolean stock_pixbuf = FALSE;
442
443   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
444
445   pixbuf = cellpixbuf->pixbuf;
446   if (cell->is_expander)
447     {
448       if (cell->is_expanded &&
449           cellpixbuf->pixbuf_expander_open != NULL)
450         pixbuf = cellpixbuf->pixbuf_expander_open;
451       else if (! cell->is_expanded &&
452                cellpixbuf->pixbuf_expander_closed != NULL)
453         pixbuf = cellpixbuf->pixbuf_expander_closed;
454     }
455
456   if (!pixbuf && !priv->stock_id)
457     return;
458   else if (!pixbuf && priv->stock_id)
459     stock_pixbuf = TRUE;
460
461   gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
462                                      &pix_rect.x,
463                                      &pix_rect.y,
464                                      &pix_rect.width,
465                                      &pix_rect.height);
466
467   if (stock_pixbuf)
468     pixbuf = cellpixbuf->pixbuf;
469   
470   pix_rect.x += cell_area->x;
471   pix_rect.y += cell_area->y;
472   pix_rect.width  -= cell->xpad * 2;
473   pix_rect.height -= cell->ypad * 2;
474
475   if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE || !cell->sensitive)
476     {
477       if (!priv->insensitive)
478         {
479           GtkIconSource *source;
480       
481           source = gtk_icon_source_new ();
482           gtk_icon_source_set_pixbuf (source, pixbuf);
483           /* The size here is arbitrary; since size isn't
484            * wildcarded in the souce, it isn't supposed to be
485            * scaled by the engine function
486            */
487           gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
488           gtk_icon_source_set_size_wildcarded (source, FALSE);
489           
490           priv->insensitive = gtk_style_render_icon (widget->style,
491                                                      source,
492                                                      gtk_widget_get_direction (widget),
493                                                      GTK_STATE_INSENSITIVE,
494                                                      /* arbitrary */
495                                                      (GtkIconSize)-1,
496                                                      widget,
497                                                      "gtkcellrendererpixbuf");
498
499           gtk_icon_source_free (source);
500         }
501       
502       pixbuf = priv->insensitive;
503     }
504
505   if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
506       gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
507     gdk_draw_pixbuf (window,
508                      widget->style->black_gc,
509                      pixbuf,
510                      /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
511                      draw_rect.x - pix_rect.x,
512                      draw_rect.y - pix_rect.y,
513                      draw_rect.x,
514                      draw_rect.y,
515                      draw_rect.width,
516                      draw_rect.height,
517                      GDK_RGB_DITHER_NORMAL,
518                      0, 0);
519 }