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