]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderertoggle.c
urg, removed implementation of gtk_marshal_VOID__INT_INT_INT_INT. if
[~andy/gtk] / gtk / gtkcellrenderertoggle.c
1 /* gtkcellrenderertoggle.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 <gtk/gtkcellrenderertoggle.h>
22 #include <gtk/gtksignal.h>
23
24 #ifndef _
25 #define _(x) x
26 #endif
27
28
29 static void gtk_cell_renderer_toggle_get_param  (GObject                    *object,
30                                                  guint                       param_id,
31                                                  GValue                     *value,
32                                                  GParamSpec                 *pspec,
33                                                  const gchar                *trailer);
34 static void gtk_cell_renderer_toggle_set_param  (GObject                    *object,
35                                                  guint                       param_id,
36                                                  GValue                     *value,
37                                                  GParamSpec                 *pspec,
38                                                  const gchar                *trailer);
39 static void gtk_cell_renderer_toggle_init       (GtkCellRendererToggle      *celltext);
40 static void gtk_cell_renderer_toggle_class_init (GtkCellRendererToggleClass *class);
41 static void gtk_cell_renderer_toggle_get_size   (GtkCellRenderer            *cell,
42                                                  GtkWidget                  *widget,
43                                                  gint                       *width,
44                                                  gint                       *height);
45 static void gtk_cell_renderer_toggle_render     (GtkCellRenderer            *cell,
46                                                  GdkWindow                  *window,
47                                                  GtkWidget                  *widget,
48                                                  GdkRectangle               *background_area,
49                                                  GdkRectangle               *cell_area,
50                                                  GdkRectangle               *expose_area,
51                                                  guint                       flags);
52 static gint gtk_cell_renderer_toggle_event      (GtkCellRenderer            *cell,
53                                                  GdkEvent                   *event,
54                                                  GtkWidget                  *widget,
55                                                  gchar                      *path,
56                                                  GdkRectangle               *background_area,
57                                                  GdkRectangle               *cell_area,
58                                                  guint                       flags);
59
60
61 enum {
62   TOGGLED,
63   LAST_SIGNAL
64 };
65
66 enum {
67   PROP_ZERO,
68   PROP_STATE,
69   PROP_RADIO
70 };
71
72
73 #define TOGGLE_WIDTH 12
74
75 static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };
76
77
78 GtkType
79 gtk_cell_renderer_toggle_get_type (void)
80 {
81   static GtkType cell_toggle_type = 0;
82
83   if (!cell_toggle_type)
84     {
85       static const GTypeInfo cell_toggle_info =
86       {
87         sizeof (GtkCellRendererToggleClass),
88         NULL,           /* base_init */
89         NULL,           /* base_finalize */
90         (GClassInitFunc) gtk_cell_renderer_toggle_class_init,
91         NULL,           /* class_finalize */
92         NULL,           /* class_data */
93         sizeof (GtkCellRendererToggle),
94         0,              /* n_preallocs */
95         (GInstanceInitFunc) gtk_cell_renderer_toggle_init,
96       };
97
98       cell_toggle_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererToggle", &cell_toggle_info, 0);
99     }
100
101   return cell_toggle_type;
102 }
103
104 static void
105 gtk_cell_renderer_toggle_init (GtkCellRendererToggle *celltoggle)
106 {
107   celltoggle->state = FALSE;
108   celltoggle->radio = FALSE;
109   GTK_CELL_RENDERER (celltoggle)->xpad = 2;
110   GTK_CELL_RENDERER (celltoggle)->ypad = 2;
111 }
112
113 static void
114 gtk_cell_renderer_toggle_class_init (GtkCellRendererToggleClass *class)
115 {
116   GObjectClass *object_class = G_OBJECT_CLASS (class);
117   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
118
119   object_class->get_param = gtk_cell_renderer_toggle_get_param;
120   object_class->set_param = gtk_cell_renderer_toggle_set_param;
121
122   cell_class->get_size = gtk_cell_renderer_toggle_get_size;
123   cell_class->render = gtk_cell_renderer_toggle_render;
124   cell_class->event = gtk_cell_renderer_toggle_event;
125
126   g_object_class_install_param (object_class,
127                                 PROP_STATE,
128                                 g_param_spec_boolean ("state",
129                                                       _("Toggle state"),
130                                                       _("The toggle-state of the button."),
131                                                       FALSE,
132                                                       G_PARAM_READABLE |
133                                                       G_PARAM_WRITABLE));
134
135   g_object_class_install_param (object_class,
136                                 PROP_RADIO,
137                                 g_param_spec_boolean ("radio",
138                                                       _("Radio state"),
139                                                       _("Draw the toggle button as a radio button."),
140                                                       FALSE,
141                                                       G_PARAM_READABLE |
142                                                       G_PARAM_WRITABLE));
143
144
145   toggle_cell_signals[TOGGLED] =
146     gtk_signal_new ("toggled",
147                     GTK_RUN_LAST,
148                     GTK_CLASS_TYPE (object_class),
149                     GTK_SIGNAL_OFFSET (GtkCellRendererToggleClass, toggled),
150                     gtk_marshal_VOID__POINTER,
151                     GTK_TYPE_NONE, 1,
152                     GTK_TYPE_POINTER);
153
154   gtk_object_class_add_signals (GTK_OBJECT_CLASS (object_class), toggle_cell_signals, LAST_SIGNAL);
155 }
156
157 static void
158 gtk_cell_renderer_toggle_get_param (GObject     *object,
159                                     guint        param_id,
160                                     GValue      *value,
161                                     GParamSpec  *pspec,
162                                     const gchar *trailer)
163 {
164   GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
165
166   switch (param_id)
167     {
168     case PROP_STATE:
169       g_value_init (value, G_TYPE_BOOLEAN);
170       g_value_set_boolean (value, celltoggle->state);
171       break;
172     case PROP_RADIO:
173       g_value_init (value, G_TYPE_BOOLEAN);
174       g_value_set_boolean (value, celltoggle->radio);
175       break;
176     default:
177       break;
178     }
179 }
180
181
182 static void
183 gtk_cell_renderer_toggle_set_param (GObject     *object,
184                                     guint        param_id,
185                                     GValue      *value,
186                                     GParamSpec  *pspec,
187                                     const gchar *trailer)
188 {
189   GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
190
191   switch (param_id)
192     {
193     case PROP_STATE:
194       celltoggle->state = g_value_get_boolean (value);
195       break;
196     case PROP_RADIO:
197       celltoggle->radio = g_value_get_boolean (value);
198       break;
199     default:
200       break;
201     }
202 }
203
204 GtkCellRenderer *
205 gtk_cell_renderer_toggle_new (void)
206 {
207   return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_toggle_get_type ()));
208 }
209
210 static void
211 gtk_cell_renderer_toggle_get_size (GtkCellRenderer *cell,
212                                    GtkWidget       *widget,
213                                    gint            *width,
214                                    gint            *height)
215 {
216   if (width)
217     *width = (gint) cell->xpad * 2 + TOGGLE_WIDTH;
218
219   if (height)
220     *height = (gint) cell->ypad * 2 + TOGGLE_WIDTH;
221 }
222
223 static void
224 gtk_cell_renderer_toggle_render (GtkCellRenderer *cell,
225                                  GdkWindow       *window,
226                                  GtkWidget       *widget,
227                                  GdkRectangle    *background_area,
228                                  GdkRectangle    *cell_area,
229                                  GdkRectangle    *expose_area,
230                                  guint            flags)
231 {
232   GtkCellRendererToggle *celltoggle = (GtkCellRendererToggle *) cell;
233   gint width, height;
234   gint real_xoffset, real_yoffset;
235
236   width = MIN (TOGGLE_WIDTH, cell_area->width - cell->xpad * 2);
237   height = MIN (TOGGLE_WIDTH, cell_area->height - cell->ypad * 2);
238
239   if (width <= 0 || height <= 0)
240     return;
241
242   real_xoffset = cell->xalign * (cell_area->width - width - (2 * cell->xpad));
243   real_xoffset = MAX (real_xoffset, 0) + cell->xpad;
244   real_yoffset = cell->yalign * (cell_area->height - height - (2 * cell->ypad));
245   real_yoffset = MAX (real_yoffset, 0) + cell->ypad;
246
247   gdk_gc_set_clip_rectangle (widget->style->black_gc, cell_area);
248
249   if (!celltoggle->radio)
250     {
251       gdk_draw_rectangle (window,
252                           widget->style->black_gc,
253                           FALSE,
254                           cell_area->x + real_xoffset,
255                           cell_area->y + real_yoffset,
256                           width, height);
257       if (celltoggle->state)
258         {
259           gdk_draw_line (window,
260                          widget->style->black_gc,
261                          cell_area->x + real_xoffset,
262                          cell_area->y + real_yoffset,
263                          cell_area->x + real_xoffset + width,
264                          cell_area->y + real_yoffset + height);
265           gdk_draw_line (window,
266                          widget->style->black_gc,
267                          cell_area->x + real_xoffset + width,
268                          cell_area->y + real_yoffset,
269                          cell_area->x + real_xoffset,
270                          cell_area->y + real_yoffset + height);
271         }
272     }
273   else
274     {
275       gdk_draw_arc (window,
276                     widget->style->black_gc,
277                     FALSE,
278                     cell_area->x + real_xoffset,
279                     cell_area->y + real_yoffset,
280                     width,
281                     height,
282                     0, 360*64);
283       if (celltoggle->state)
284         {
285           gdk_draw_arc (window,
286                         widget->style->black_gc,
287                         TRUE,
288                         cell_area->x + real_xoffset + 2,
289                         cell_area->y + real_yoffset + 2,
290                         width - 4,
291                         height - 4,
292                         0, 360*64);
293         }
294     }
295
296
297   gdk_gc_set_clip_rectangle (widget->style->black_gc, NULL);
298 }
299
300 static gint
301 gtk_cell_renderer_toggle_event (GtkCellRenderer *cell,
302                                 GdkEvent        *event,
303                                 GtkWidget       *widget,
304                                 gchar           *path,
305                                 GdkRectangle    *background_area,
306                                 GdkRectangle    *cell_area,
307                                 guint            flags)
308 {
309   gtk_signal_emit (GTK_OBJECT (cell), toggle_cell_signals[TOGGLED], path);
310   return TRUE;
311 }
312
313 void
314 gtk_cell_renderer_toggle_set_radio (GtkCellRendererToggle *toggle,
315                                     gboolean               radio)
316 {
317   g_return_if_fail (toggle != NULL);
318   g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));
319
320   toggle->radio = radio;
321 }