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