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