]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderer.c
marshaller fixes.
[~andy/gtk] / gtk / gtkcellrenderer.c
1 /* gtkcellrenderer.c
2  * Copyright (C) 2000  Red Hat, Inc. Jonathan Blandford
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 "gtkcellrenderer.h"
21 #include "gtkintl.h"
22
23 static void gtk_cell_renderer_init       (GtkCellRenderer      *cell);
24 static void gtk_cell_renderer_class_init (GtkCellRendererClass *class);
25 static void gtk_cell_renderer_get_property  (GObject              *object,
26                                              guint                 param_id,
27                                              GValue               *value,
28                                              GParamSpec           *pspec);
29 static void gtk_cell_renderer_set_property  (GObject              *object,
30                                              guint                 param_id,
31                                              const GValue         *value,
32                                              GParamSpec           *pspec);
33
34
35 enum {
36   PROP_ZERO,
37   PROP_VISIBLE,
38   PROP_XALIGN,
39   PROP_YALIGN,
40   PROP_XPAD,
41   PROP_YPAD,
42 };
43
44
45 GtkType
46 gtk_cell_renderer_get_type (void)
47 {
48   static GtkType cell_type = 0;
49
50   if (!cell_type)
51     {
52       static const GTypeInfo cell_info =
53       {
54         sizeof (GtkCellRendererClass),
55         NULL,           /* base_init */
56         NULL,           /* base_finalize */
57         (GClassInitFunc) gtk_cell_renderer_class_init,
58         NULL,           /* class_finalize */
59         NULL,           /* class_data */
60         sizeof (GtkCellRenderer),
61         0,
62         (GInstanceInitFunc) gtk_cell_renderer_init,
63       };
64
65       cell_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkCellRenderer", &cell_info, 0);
66     }
67
68   return cell_type;
69 }
70
71 static void
72 gtk_cell_renderer_init (GtkCellRenderer *cell)
73 {
74   /* FIXME remove on port to GtkObject */
75   gtk_object_ref (GTK_OBJECT (cell));
76   gtk_object_sink (GTK_OBJECT (cell));
77
78   cell->visible = TRUE;
79   cell->xalign = 0.5;
80   cell->yalign = 0.5;
81   cell->xpad = 0;
82   cell->ypad = 0;
83 }
84
85 static void
86 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
87 {
88   GObjectClass *object_class = G_OBJECT_CLASS (class);
89
90   object_class->get_property = gtk_cell_renderer_get_property;
91   object_class->set_property = gtk_cell_renderer_set_property;
92
93   class->render = NULL;
94   class->get_size = NULL;
95
96   g_object_class_install_property (object_class,
97                                    PROP_VISIBLE,
98                                    g_param_spec_boolean ("visible",
99                                                          _("visible"),
100                                                          _("Display the cell"),
101                                                          TRUE,
102                                                          G_PARAM_READABLE |
103                                                          G_PARAM_WRITABLE));
104   
105   g_object_class_install_property (object_class,
106                                    PROP_XALIGN,
107                                    g_param_spec_float ("xalign",
108                                                        _("xalign"),
109                                                        _("The x-align."),
110                                                        0.0,
111                                                        1.0,
112                                                        0.0,
113                                                        G_PARAM_READABLE |
114                                                        G_PARAM_WRITABLE));
115   
116   g_object_class_install_property (object_class,
117                                    PROP_YALIGN,
118                                    g_param_spec_float ("yalign",
119                                                        _("yalign"),
120                                                        _("The y-align."),
121                                                        0.0,
122                                                        1.0,
123                                                        0.5,
124                                                        G_PARAM_READABLE |
125                                                        G_PARAM_WRITABLE));
126   
127   g_object_class_install_property (object_class,
128                                    PROP_XPAD,
129                                    g_param_spec_uint ("xpad",
130                                                       _("xpad"),
131                                                       _("The xpad."),
132                                                       0,
133                                                       100,
134                                                       2,
135                                                       G_PARAM_READABLE |
136                                                       G_PARAM_WRITABLE));
137   
138   g_object_class_install_property (object_class,
139                                    PROP_YPAD,
140                                    g_param_spec_uint ("ypad",
141                                                       _("ypad"),
142                                                       _("The ypad."),
143                                                       0,
144                                                       100,
145                                                       2,
146                                                       G_PARAM_READABLE |
147                                                       G_PARAM_WRITABLE));
148 }
149
150 static void
151 gtk_cell_renderer_get_property (GObject     *object,
152                                 guint        param_id,
153                                 GValue      *value,
154                                 GParamSpec  *pspec)
155 {
156   GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
157
158   switch (param_id)
159     {
160     case PROP_VISIBLE:
161       g_value_set_boolean (value, cell->visible);
162       break;
163     case PROP_XALIGN:
164       g_value_set_float (value, cell->xalign);
165       break;
166     case PROP_YALIGN:
167       g_value_set_float (value, cell->yalign);
168       break;
169     case PROP_XPAD:
170       g_value_set_float (value, cell->xpad);
171       break;
172     case PROP_YPAD:
173       g_value_set_float (value, cell->ypad);
174       break;
175     default:
176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
177       break;
178     }
179
180 }
181
182 static void
183 gtk_cell_renderer_set_property (GObject      *object,
184                                 guint         param_id,
185                                 const GValue *value,
186                                 GParamSpec   *pspec)
187 {
188   GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
189
190   switch (param_id)
191     {
192     case PROP_VISIBLE:
193       cell->visible = g_value_get_boolean (value);
194       g_object_notify (object, "visible");
195       break;
196     case PROP_XALIGN:
197       cell->xalign = g_value_get_float (value);
198       g_object_notify (object, "xalign");
199       break;
200     case PROP_YALIGN:
201       cell->yalign = g_value_get_float (value);
202       g_object_notify (object, "yalign");
203       break;
204     case PROP_XPAD:
205       cell->xpad = g_value_get_int (value);
206       g_object_notify (object, "xpad");
207       break;
208     case PROP_YPAD:
209       cell->ypad = g_value_get_int (value);
210       g_object_notify (object, "ypad");
211       break;
212     default:
213       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
214       break;
215     }
216 }
217
218 /**
219  * gtk_cell_renderer_get_size:
220  * @cell: a #GtkCellRenderer
221  * @widget: the widget the renderer is rendering to
222  * @width: location to return width needed to render a cell, or %NULL
223  * @height: location to return height needed to render a cell, or %NULL
224  * 
225  * Obtains the width and height needed to render the cell. Used by
226  * view widgets to determine the appropriate size for the cell_area
227  * passed to gtk_cell_renderer_render().
228  **/
229 void
230 gtk_cell_renderer_get_size (GtkCellRenderer *cell,
231                             GtkWidget *widget,
232                             gint      *width,
233                             gint      *height)
234 {
235   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
236   g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size != NULL);
237
238   GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (cell, widget, width, height);
239 }
240
241 /**
242  * gtk_cell_renderer_render:
243  * @cell: a #GtkCellRenderer
244  * @window: a #GdkDrawable to draw to
245  * @widget: the widget owning @window
246  * @background_area: entire cell area (including tree expanders and maybe padding on the sides)
247  * @cell_area: area normally rendered by a cell renderer
248  * @expose_area: area that actually needs updating
249  * @flags: flags that affect rendering
250  *
251  * Invokes the virtual render function of the #GtkCellRenderer. The
252  * three passed-in rectangles are areas of @window. Most renderers
253  * will draw to @cell_area; the xalign, yalign, xpad, and ypad fields
254  * of the #GtkCellRenderer should be honored with respect to
255  * @cell_area. @background_area includes the blank space around the
256  * cell, and also the area containing the tree expander; so the
257  * @background_area rectangles for all cells tile to cover the entire
258  * @window. Cell renderers can use the @background_area to draw custom expanders, for
259  * example. @expose_area is a clip rectangle.
260  * 
261  **/
262 void
263 gtk_cell_renderer_render (GtkCellRenderer     *cell,
264                           GdkWindow           *window,
265                           GtkWidget           *widget,
266                           GdkRectangle        *background_area,
267                           GdkRectangle        *cell_area,
268                           GdkRectangle        *expose_area,
269                           GtkCellRendererState flags)
270 {
271   /* It's actually okay to pass in a NULL cell, as we run into that
272    * a lot
273    */
274   if (cell == NULL)
275     return;
276   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
277   g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
278
279   GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
280                                               window,
281                                               widget,
282                                               background_area,
283                                               cell_area,
284                                               expose_area,
285                                               flags);
286 }
287
288 /**
289  * gtk_cell_renderer_event:
290  * @cell: a #GtkCellRenderer
291  * @event: a #GdkEvent
292  * @widget: widget that received the event
293  * @path: widget-dependent string representation of the event location; e.g. for #GtkTreeView, a string representation of #GtkTreePath
294  * @background_area: background area as passed to gtk_cell_renderer_render()
295  * @cell_area: cell area as passed to gtk_cell_renderer_render()
296  * @flags: render flags
297  * 
298  * Passes an event to the cell renderer for possible processing.  Some
299  * cell renderers may use events; for example, #GtkCellRendererToggle
300  * toggles when it gets a mouse click.
301  * 
302  * Return value: %TRUE if the event was consumed/handled
303  **/
304 gint
305 gtk_cell_renderer_event (GtkCellRenderer     *cell,
306                          GdkEvent            *event,
307                          GtkWidget           *widget,
308                          gchar               *path,
309                          GdkRectangle        *background_area,
310                          GdkRectangle        *cell_area,
311                          GtkCellRendererState flags)
312 {
313   /* It's actually okay to pass in a NULL cell, as we run into that
314    * a lot
315    */
316   if (cell == NULL)
317     return FALSE;
318   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
319   if (GTK_CELL_RENDERER_GET_CLASS (cell)->event == NULL)
320     return FALSE;
321
322   return GTK_CELL_RENDERER_GET_CLASS (cell)->event (cell,
323                                                     event,
324                                                     widget,
325                                                     path,
326                                                     background_area,
327                                                     cell_area,
328                                                     flags);
329 }
330