]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererpixbuf.c
7f344dcbedb6be304cc86d6f48e7b57410eb0c9c
[~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                                                         P_("Pixbuf Object"),
134                                                         P_("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                                                         P_("Pixbuf Expander Open"),
143                                                         P_("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                                                         P_("Pixbuf Expander Closed"),
152                                                         P_("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                                                         P_("Stock ID"),
161                                                         P_("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_uint ("stock_size",
168                                                       P_("Size"),
169                                                       P_("The GtkIconSize value that specifies the size of the rendered icon"),
170                                                       0,
171                                                       G_MAXUINT,
172                                                       GTK_ICON_SIZE_MENU,
173                                                       G_PARAM_READWRITE));
174
175   g_object_class_install_property (object_class,
176                                    PROP_STOCK_DETAIL,
177                                    g_param_spec_string ("stock_detail",
178                                                         P_("Detail"),
179                                                         P_("Render detail to pass to the theme engine"),
180                                                         NULL,
181                                                         G_PARAM_READWRITE));
182
183   g_type_class_add_private (object_class, sizeof (GtkCellRendererPixbufPrivate));
184 }
185
186 static void
187 gtk_cell_renderer_pixbuf_finalize (GObject *object)
188 {
189   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
190   GtkCellRendererPixbufPrivate *priv;
191
192   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
193
194   if (cellpixbuf->pixbuf && priv->stock_id)
195     g_object_unref (cellpixbuf->pixbuf);
196
197   if (priv->stock_id)
198     g_free (priv->stock_id);
199
200   if (priv->stock_detail)
201     g_free (priv->stock_detail);
202
203   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
204 }
205
206 static void
207 gtk_cell_renderer_pixbuf_get_property (GObject        *object,
208                                        guint           param_id,
209                                        GValue         *value,
210                                        GParamSpec     *pspec)
211 {
212   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
213   GtkCellRendererPixbufPrivate *priv;
214
215   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
216   
217   switch (param_id)
218     {
219     case PROP_PIXBUF:
220       g_value_set_object (value,
221                           cellpixbuf->pixbuf ? G_OBJECT (cellpixbuf->pixbuf) : NULL);
222       break;
223     case PROP_PIXBUF_EXPANDER_OPEN:
224       g_value_set_object (value,
225                           cellpixbuf->pixbuf_expander_open ? G_OBJECT (cellpixbuf->pixbuf_expander_open) : NULL);
226       break;
227     case PROP_PIXBUF_EXPANDER_CLOSED:
228       g_value_set_object (value,
229                           cellpixbuf->pixbuf_expander_closed ? G_OBJECT (cellpixbuf->pixbuf_expander_closed) : NULL);
230       break;
231     case PROP_STOCK_ID:
232       g_value_set_string (value, priv->stock_id);
233       break;
234     case PROP_STOCK_SIZE:
235       g_value_set_uint (value, priv->stock_size);
236       break;
237     case PROP_STOCK_DETAIL:
238       g_value_set_string (value, priv->stock_detail);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
242       break;
243     }
244 }
245
246
247 static void
248 gtk_cell_renderer_pixbuf_set_property (GObject      *object,
249                                        guint         param_id,
250                                        const GValue *value,
251                                        GParamSpec   *pspec)
252 {
253   GdkPixbuf *pixbuf;
254   GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
255   GtkCellRendererPixbufPrivate *priv;
256
257   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
258   
259   switch (param_id)
260     {
261     case PROP_PIXBUF:
262       pixbuf = (GdkPixbuf*) g_value_get_object (value);
263       if (pixbuf)
264         g_object_ref (pixbuf);
265       if (cellpixbuf->pixbuf)
266         g_object_unref (cellpixbuf->pixbuf);
267       cellpixbuf->pixbuf = pixbuf;
268       break;
269     case PROP_PIXBUF_EXPANDER_OPEN:
270       pixbuf = (GdkPixbuf*) g_value_get_object (value);
271       if (pixbuf)
272         g_object_ref (pixbuf);
273       if (cellpixbuf->pixbuf_expander_open)
274         g_object_unref (cellpixbuf->pixbuf_expander_open);
275       cellpixbuf->pixbuf_expander_open = pixbuf;
276       break;
277     case PROP_PIXBUF_EXPANDER_CLOSED:
278       pixbuf = (GdkPixbuf*) g_value_get_object (value);
279       if (pixbuf)
280         g_object_ref (pixbuf);
281       if (cellpixbuf->pixbuf_expander_closed)
282         g_object_unref (cellpixbuf->pixbuf_expander_closed);
283       cellpixbuf->pixbuf_expander_closed = pixbuf;
284       break;
285     case PROP_STOCK_ID:
286       if (priv->stock_id)
287         {
288           if (cellpixbuf->pixbuf)
289             {
290               g_object_unref (cellpixbuf->pixbuf);
291               cellpixbuf->pixbuf = NULL;
292             }
293           g_free (priv->stock_id);
294         }
295       priv->stock_id = g_strdup (g_value_get_string (value));
296       break;
297     case PROP_STOCK_SIZE:
298       priv->stock_size = g_value_get_uint (value);
299       break;
300     case PROP_STOCK_DETAIL:
301       if (priv->stock_detail)
302         g_free (priv->stock_detail);
303       priv->stock_detail = g_strdup (g_value_get_string (value));
304       break;
305     default:
306       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
307       break;
308     }
309
310   if (cellpixbuf->pixbuf && priv->stock_id)
311     {
312       g_object_unref (cellpixbuf->pixbuf);
313       cellpixbuf->pixbuf = NULL;
314     }
315 }
316
317 /**
318  * gtk_cell_renderer_pixbuf_new:
319  * 
320  * Creates a new #GtkCellRendererPixbuf. Adjust rendering
321  * parameters using object properties. Object properties can be set
322  * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
323  * can bind a property to a value in a #GtkTreeModel. For example, you
324  * can bind the "pixbuf" property on the cell renderer to a pixbuf value
325  * in the model, thus rendering a different image in each row of the
326  * #GtkTreeView.
327  * 
328  * Return value: the new cell renderer
329  **/
330 GtkCellRenderer *
331 gtk_cell_renderer_pixbuf_new (void)
332 {
333   return g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF, NULL);
334 }
335
336 static void
337 gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
338                                               GtkWidget             *widget)
339 {
340   GtkCellRendererPixbufPrivate *priv;
341
342   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
343
344   if (cellpixbuf->pixbuf)
345     g_object_unref (cellpixbuf->pixbuf);
346
347   cellpixbuf->pixbuf = gtk_widget_render_icon (widget,
348                                                priv->stock_id,
349                                                priv->stock_size,
350                                                priv->stock_detail);
351 }
352
353 static void
354 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
355                                    GtkWidget       *widget,
356                                    GdkRectangle    *cell_area,
357                                    gint            *x_offset,
358                                    gint            *y_offset,
359                                    gint            *width,
360                                    gint            *height)
361 {
362   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
363   GtkCellRendererPixbufPrivate *priv;
364   gint pixbuf_width  = 0;
365   gint pixbuf_height = 0;
366   gint calc_width;
367   gint calc_height;
368
369   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
370
371   if (!cellpixbuf->pixbuf && priv->stock_id)
372     gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
373
374   if (cellpixbuf->pixbuf)
375     {
376       pixbuf_width  = gdk_pixbuf_get_width (cellpixbuf->pixbuf);
377       pixbuf_height = gdk_pixbuf_get_height (cellpixbuf->pixbuf);
378     }
379   if (cellpixbuf->pixbuf_expander_open)
380     {
381       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_open));
382       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_open));
383     }
384   if (cellpixbuf->pixbuf_expander_closed)
385     {
386       pixbuf_width  = MAX (pixbuf_width, gdk_pixbuf_get_width (cellpixbuf->pixbuf_expander_closed));
387       pixbuf_height = MAX (pixbuf_height, gdk_pixbuf_get_height (cellpixbuf->pixbuf_expander_closed));
388     }
389   
390   calc_width  = (gint) cell->xpad * 2 + pixbuf_width;
391   calc_height = (gint) cell->ypad * 2 + pixbuf_height;
392   
393   if (x_offset) *x_offset = 0;
394   if (y_offset) *y_offset = 0;
395
396   if (cell_area && pixbuf_width > 0 && pixbuf_height > 0)
397     {
398       if (x_offset)
399         {
400           *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
401                         1.0 - cell->xalign : cell->xalign) * 
402                        (cell_area->width - calc_width - 2 * cell->xpad));
403           *x_offset = MAX (*x_offset, 0) + cell->xpad;
404         }
405       if (y_offset)
406         {
407           *y_offset = (cell->yalign *
408                        (cell_area->height - calc_height - 2 * cell->ypad));
409           *y_offset = MAX (*y_offset, 0) + cell->ypad;
410         }
411     }
412
413   if (width)
414     *width = calc_width;
415   
416   if (height)
417     *height = calc_height;
418 }
419
420 static void
421 gtk_cell_renderer_pixbuf_render (GtkCellRenderer      *cell,
422                                  GdkWindow            *window,
423                                  GtkWidget            *widget,
424                                  GdkRectangle         *background_area,
425                                  GdkRectangle         *cell_area,
426                                  GdkRectangle         *expose_area,
427                                  GtkCellRendererState  flags)
428
429 {
430   GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
431   GtkCellRendererPixbufPrivate *priv;
432   GdkPixbuf *pixbuf;
433   GdkRectangle pix_rect;
434   GdkRectangle draw_rect;
435   gboolean stock_pixbuf = FALSE;
436
437   priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
438
439   pixbuf = cellpixbuf->pixbuf;
440   if (cell->is_expander)
441     {
442       if (cell->is_expanded &&
443           cellpixbuf->pixbuf_expander_open != NULL)
444         pixbuf = cellpixbuf->pixbuf_expander_open;
445       else if (! cell->is_expanded &&
446                cellpixbuf->pixbuf_expander_closed != NULL)
447         pixbuf = cellpixbuf->pixbuf_expander_closed;
448     }
449
450   if (!pixbuf && !priv->stock_id)
451     return;
452   else if (!pixbuf && priv->stock_id)
453     stock_pixbuf = TRUE;
454
455   gtk_cell_renderer_pixbuf_get_size (cell, widget, cell_area,
456                                      &pix_rect.x,
457                                      &pix_rect.y,
458                                      &pix_rect.width,
459                                      &pix_rect.height);
460
461   if (stock_pixbuf)
462     pixbuf = cellpixbuf->pixbuf;
463   
464   pix_rect.x += cell_area->x;
465   pix_rect.y += cell_area->y;
466   pix_rect.width  -= cell->xpad * 2;
467   pix_rect.height -= cell->ypad * 2;
468
469   if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) &&
470       gdk_rectangle_intersect (expose_area, &draw_rect, &draw_rect))
471     gdk_draw_pixbuf (window,
472                      widget->style->black_gc,
473                      pixbuf,
474                      /* pixbuf 0, 0 is at pix_rect.x, pix_rect.y */
475                      draw_rect.x - pix_rect.x,
476                      draw_rect.y - pix_rect.y,
477                      draw_rect.x,
478                      draw_rect.y,
479                      draw_rect.width,
480                      draw_rect.height,
481                      GDK_RGB_DITHER_NORMAL,
482                      0, 0);
483 }