]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
32409d5a8f6edf15dfbf7ea808aa9a631307e668
[~andy/gtk] / gtk / gtktogglebutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtklabel.h"
28 #include "gtkmain.h"
29 #include "gtkmarshalers.h"
30 #include "gtksignal.h"
31 #include "gtktogglebutton.h"
32 #include "gtkintl.h"
33
34 #define DEFAULT_LEFT_POS  4
35 #define DEFAULT_TOP_POS   4
36 #define DEFAULT_SPACING   7
37
38 enum {
39   TOGGLED,
40   LAST_SIGNAL
41 };
42
43 enum {
44   PROP_0,
45   PROP_ACTIVE,
46   PROP_INCONSISTENT,
47   PROP_DRAW_INDICATOR
48 };
49
50
51 static void gtk_toggle_button_class_init    (GtkToggleButtonClass *klass);
52 static void gtk_toggle_button_init          (GtkToggleButton      *toggle_button);
53 static gint gtk_toggle_button_expose        (GtkWidget            *widget,
54                                              GdkEventExpose       *event);
55 static void gtk_toggle_button_pressed       (GtkButton            *button);
56 static void gtk_toggle_button_released      (GtkButton            *button);
57 static void gtk_toggle_button_clicked       (GtkButton            *button);
58 static void gtk_toggle_button_set_property  (GObject              *object,
59                                              guint                 prop_id,
60                                              const GValue         *value,
61                                              GParamSpec           *pspec);
62 static void gtk_toggle_button_get_property  (GObject              *object,
63                                              guint                 prop_id,
64                                              GValue               *value,
65                                              GParamSpec           *pspec);
66 static void gtk_toggle_button_update_state  (GtkButton            *button);
67
68 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
69 static GtkContainerClass *parent_class = NULL;
70
71 GtkType
72 gtk_toggle_button_get_type (void)
73 {
74   static GtkType toggle_button_type = 0;
75
76   if (!toggle_button_type)
77     {
78       static const GtkTypeInfo toggle_button_info =
79       {
80         "GtkToggleButton",
81         sizeof (GtkToggleButton),
82         sizeof (GtkToggleButtonClass),
83         (GtkClassInitFunc) gtk_toggle_button_class_init,
84         (GtkObjectInitFunc) gtk_toggle_button_init,
85         /* reserved_1 */ NULL,
86         /* reserved_2 */ NULL,
87         (GtkClassInitFunc) NULL,
88       };
89
90       toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
91     }
92
93   return toggle_button_type;
94 }
95
96 static void
97 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
98 {
99   GtkObjectClass *object_class;
100   GObjectClass   *gobject_class;
101   GtkWidgetClass *widget_class;
102   GtkContainerClass *container_class;
103   GtkButtonClass *button_class;
104
105   object_class = (GtkObjectClass*) class;
106   gobject_class = G_OBJECT_CLASS (class);
107   widget_class = (GtkWidgetClass*) class;
108   container_class = (GtkContainerClass*) class;
109   button_class = (GtkButtonClass*) class;
110
111   parent_class = gtk_type_class (GTK_TYPE_BUTTON);
112
113
114   gobject_class->set_property = gtk_toggle_button_set_property;
115   gobject_class->get_property = gtk_toggle_button_get_property;
116
117   widget_class->expose_event = gtk_toggle_button_expose;
118
119   button_class->pressed = gtk_toggle_button_pressed;
120   button_class->released = gtk_toggle_button_released;
121   button_class->clicked = gtk_toggle_button_clicked;
122   button_class->enter = gtk_toggle_button_update_state;
123   button_class->leave = gtk_toggle_button_update_state;
124
125   class->toggled = NULL;
126
127   g_object_class_install_property (gobject_class,
128                                    PROP_ACTIVE,
129                                    g_param_spec_boolean ("active",
130                                                          _("Active"),
131                                                          _("If the toggle button should be pressed in or not"),
132                                                          FALSE,
133                                                          G_PARAM_READWRITE));
134
135   g_object_class_install_property (gobject_class,
136                                    PROP_INCONSISTENT,
137                                    g_param_spec_boolean ("inconsistent",
138                                                          _("Inconsistent"),
139                                                          _("If the toggle button is in an \"in between\" state."),
140                                                          FALSE,
141                                                          G_PARAM_READWRITE));
142
143   g_object_class_install_property (gobject_class,
144                                    PROP_DRAW_INDICATOR,
145                                    g_param_spec_boolean ("draw_indicator",
146                                                          _("Draw Indicator"),
147                                                          _("If the toggle part of the button is displayed"),
148                                                          FALSE,
149                                                          G_PARAM_READWRITE));
150
151   toggle_button_signals[TOGGLED] =
152     gtk_signal_new ("toggled",
153                     GTK_RUN_FIRST,
154                     GTK_CLASS_TYPE (object_class),
155                     GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
156                     _gtk_marshal_VOID__VOID,
157                     GTK_TYPE_NONE, 0);
158 }
159
160 static void
161 gtk_toggle_button_init (GtkToggleButton *toggle_button)
162 {
163   toggle_button->active = FALSE;
164   toggle_button->draw_indicator = FALSE;
165   GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
166 }
167
168
169 GtkWidget*
170 gtk_toggle_button_new (void)
171 {
172   return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
173 }
174
175 GtkWidget*
176 gtk_toggle_button_new_with_label (const gchar *label)
177 {
178   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
179 }
180
181 /**
182  * gtk_toggle_button_new_with_mnemonic:
183  * @label: the text of the button, with an underscore in front of the
184  *         mnemonic character
185  * @returns: a new #GtkToggleButton
186  *
187  * Creates a new #GtkToggleButton containing a label. The label
188  * will be created using gtk_label_new_with_mnemonic(), so underscores
189  * in @label indicate the mnemonic for the button.
190  **/
191 GtkWidget*
192 gtk_toggle_button_new_with_mnemonic (const gchar *label)
193 {
194   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
195 }
196
197 static void
198 gtk_toggle_button_set_property (GObject      *object,
199                                 guint         prop_id,
200                                 const GValue *value,
201                                 GParamSpec   *pspec)
202 {
203   GtkToggleButton *tb;
204
205   tb = GTK_TOGGLE_BUTTON (object);
206
207   switch (prop_id)
208     {
209     case PROP_ACTIVE:
210       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
211       break;
212     case PROP_INCONSISTENT:
213       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
214       break;
215     case PROP_DRAW_INDICATOR:
216       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
217       break;
218     default:
219       break;
220     }
221 }
222
223 static void
224 gtk_toggle_button_get_property (GObject      *object,
225                                 guint         prop_id,
226                                 GValue       *value,
227                                 GParamSpec   *pspec)
228 {
229   GtkToggleButton *tb;
230
231   tb = GTK_TOGGLE_BUTTON (object);
232
233   switch (prop_id)
234     {
235     case PROP_ACTIVE:
236       g_value_set_boolean (value, tb->active);
237       break;
238     case PROP_INCONSISTENT:
239       g_value_set_boolean (value, tb->inconsistent);
240       break;
241     case PROP_DRAW_INDICATOR:
242       g_value_set_boolean (value, tb->draw_indicator);
243       break;
244     default:
245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246       break;
247     }
248 }
249
250 void
251 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
252                             gboolean         draw_indicator)
253 {
254   GtkWidget *widget;
255
256   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
257
258   widget = GTK_WIDGET (toggle_button);
259
260   draw_indicator = draw_indicator ? TRUE : FALSE;
261
262   if (toggle_button->draw_indicator != draw_indicator)
263     {
264       toggle_button->draw_indicator = draw_indicator;
265       GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
266       
267       if (GTK_WIDGET_VISIBLE (toggle_button))
268         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
269
270       g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
271     }
272 }
273
274 /**
275  * gtk_toggle_button_get_mode:
276  * @toggle_button: a #GtkToggleButton
277  *
278  * Retrieves whether the button is displayed as a separate indicator
279  * and label. See gtk_toggle_button_set_mode().
280  *
281  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
282  *   and label.
283  **/
284 gboolean
285 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
286 {
287   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
288
289   return toggle_button->draw_indicator;
290 }
291
292 void
293 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
294                               gboolean         is_active)
295 {
296   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
297
298   is_active = is_active != FALSE;
299
300   if (toggle_button->active != is_active)
301     gtk_button_clicked (GTK_BUTTON (toggle_button));
302 }
303
304
305 gboolean
306 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
307 {
308   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
309
310   return (toggle_button->active) ? TRUE : FALSE;
311 }
312
313
314 void
315 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
316 {
317   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
318
319   gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
320 }
321
322 /**
323  * gtk_toggle_button_set_inconsistent:
324  * @toggle_button: a #GtkToggleButton
325  * @setting: %TRUE if state is inconsistent
326  *
327  * If the user has selected a range of elements (such as some text or
328  * spreadsheet cells) that are affected by a toggle button, and the
329  * current values in that range are inconsistent, you may want to
330  * display the toggle in an "in between" state. This function turns on
331  * "in between" display.  Normally you would turn off the inconsistent
332  * state again if the user toggles the toggle button. This has to be
333  * done manually, gtk_toggle_button_set_inconsistent() only affects
334  * visual appearance, it doesn't affect the semantics of the button.
335  * 
336  **/
337 void
338 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
339                                     gboolean         setting)
340 {
341   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
342   
343   setting = setting != FALSE;
344
345   if (setting != toggle_button->inconsistent)
346     {
347       toggle_button->inconsistent = setting;
348       
349       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
350       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
351
352       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
353     }
354 }
355
356 /**
357  * gtk_toggle_button_get_inconsistent:
358  * @toggle_button: a #GtkToggleButton
359  * 
360  * Gets the value set by gtk_toggle_button_set_inconsistent().
361  * 
362  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
363  **/
364 gboolean
365 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
366 {
367   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
368
369   return toggle_button->inconsistent;
370 }
371
372 static gint
373 gtk_toggle_button_expose (GtkWidget      *widget,
374                           GdkEventExpose *event)
375 {
376   if (GTK_WIDGET_DRAWABLE (widget))
377     {
378       GtkWidget *child = GTK_BIN (widget)->child;
379       GtkButton *button = GTK_BUTTON (widget);
380       GtkStateType state_type;
381       GtkShadowType shadow_type;
382
383       state_type = GTK_WIDGET_STATE (widget);
384       
385       if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
386         {
387           if (state_type == GTK_STATE_ACTIVE)
388             state_type = GTK_STATE_NORMAL;
389           shadow_type = GTK_SHADOW_ETCHED_IN;
390         }
391       else
392         shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
393
394       _gtk_button_paint (button, &event->area, state_type, shadow_type,
395                          "togglebutton", "togglebuttondefault");
396
397       if (child)
398         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
399     }
400   
401   return FALSE;
402 }
403
404 static void
405 gtk_toggle_button_pressed (GtkButton *button)
406 {
407   button->button_down = TRUE;
408
409   gtk_toggle_button_update_state (button);
410   gtk_widget_queue_draw (GTK_WIDGET (button));
411 }
412
413 static void
414 gtk_toggle_button_released (GtkButton *button)
415 {
416   if (button->button_down)
417     {
418       button->button_down = FALSE;
419
420       if (button->in_button)
421         gtk_button_clicked (button);
422
423       gtk_toggle_button_update_state (button);
424       gtk_widget_queue_draw (GTK_WIDGET (button));
425     }
426 }
427
428 static void
429 gtk_toggle_button_clicked (GtkButton *button)
430 {
431   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
432   toggle_button->active = !toggle_button->active;
433
434   gtk_toggle_button_toggled (toggle_button);
435
436   gtk_toggle_button_update_state (button);
437
438   g_object_notify (G_OBJECT (toggle_button), "active");
439 }
440
441 static void
442 gtk_toggle_button_update_state (GtkButton *button)
443 {
444   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
445   gboolean depressed;
446   GtkStateType new_state;
447
448   if (toggle_button->inconsistent)
449     depressed = FALSE;
450   else if (button->in_button && button->button_down)
451     depressed = TRUE;
452   else
453     depressed = toggle_button->active;
454       
455   if (button->in_button && (!button->button_down || toggle_button->draw_indicator))
456     new_state = GTK_STATE_PRELIGHT;
457   else
458     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
459
460   _gtk_button_set_depressed (button, depressed); 
461   gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
462 }