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