]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
Change FSF Address
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26
27 #include "gtktogglebutton.h"
28
29 #include "gtkbuttonprivate.h"
30 #include "gtklabel.h"
31 #include "gtkmain.h"
32 #include "gtkmarshalers.h"
33 #include "gtktoggleaction.h"
34 #include "gtkactivatable.h"
35 #include "gtkprivate.h"
36 #include "gtkintl.h"
37 #include "a11y/gtktogglebuttonaccessible.h"
38
39
40 /**
41  * SECTION:gtktogglebutton
42  * @Short_description: Create buttons which retain their state
43  * @Title: GtkToggleButton
44  * @See_also: #GtkButton, #GtkCheckButton, #GtkCheckMenuItem
45  *
46  * A #GtkToggleButton is a #GtkButton which will remain 'pressed-in' when
47  * clicked. Clicking again will cause the toggle button to return to its
48  * normal state.
49  *
50  * A toggle button is created by calling either gtk_toggle_button_new() or
51  * gtk_toggle_button_new_with_label(). If using the former, it is advisable to
52  * pack a widget, (such as a #GtkLabel and/or a #GtkPixmap), into the toggle
53  * button's container. (See #GtkButton for more information).
54  *
55  * The state of a #GtkToggleButton can be set specifically using
56  * gtk_toggle_button_set_active(), and retrieved using
57  * gtk_toggle_button_get_active().
58  *
59  * To simply switch the state of a toggle button, use gtk_toggle_button_toggled().
60  *
61  * <example>
62  * <title>Creating two #GtkToggleButton widgets.</title>
63  * <programlisting>
64  * void make_toggles (void) {
65  *    GtkWidget *dialog, *toggle1, *toggle2;
66  *
67  *    dialog = gtk_dialog_new (<!-- -->);
68  *    toggle1 = gtk_toggle_button_new_with_label ("Hi, i'm a toggle button.");
69  *
70  *    // Makes this toggle button invisible
71  *    gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1), TRUE);
72  *
73  *    g_signal_connect (toggle1, "toggled",
74  *                      G_CALLBACK (output_state), NULL);
75  *    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
76  *                        toggle1, FALSE, FALSE, 2);
77  *
78  *    toggle2 = gtk_toggle_button_new_with_label ("Hi, i'm another toggle button.");
79  *    gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle2), FALSE);
80  *    g_signal_connect (toggle2, "toggled",
81  *                      G_CALLBACK (output_state), NULL);
82  *    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
83  *                        toggle2, FALSE, FALSE, 2);
84  *
85  *    gtk_widget_show_all (dialog);
86  * }
87  * </programlisting>
88  * </example>
89  */
90
91
92 #define DEFAULT_LEFT_POS  4
93 #define DEFAULT_TOP_POS   4
94 #define DEFAULT_SPACING   7
95
96 struct _GtkToggleButtonPrivate
97 {
98   guint active         : 1;
99   guint draw_indicator : 1;
100   guint inconsistent   : 1;
101 };
102
103 enum {
104   TOGGLED,
105   LAST_SIGNAL
106 };
107
108 enum {
109   PROP_0,
110   PROP_ACTIVE,
111   PROP_INCONSISTENT,
112   PROP_DRAW_INDICATOR
113 };
114
115
116 static gint gtk_toggle_button_draw         (GtkWidget            *widget,
117                                             cairo_t              *cr);
118 static gboolean gtk_toggle_button_mnemonic_activate  (GtkWidget            *widget,
119                                                       gboolean              group_cycling);
120 static void gtk_toggle_button_pressed       (GtkButton            *button);
121 static void gtk_toggle_button_released      (GtkButton            *button);
122 static void gtk_toggle_button_clicked       (GtkButton            *button);
123 static void gtk_toggle_button_set_property  (GObject              *object,
124                                              guint                 prop_id,
125                                              const GValue         *value,
126                                              GParamSpec           *pspec);
127 static void gtk_toggle_button_get_property  (GObject              *object,
128                                              guint                 prop_id,
129                                              GValue               *value,
130                                              GParamSpec           *pspec);
131 static void gtk_toggle_button_update_state  (GtkButton            *button);
132
133
134 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface  *iface);
135 static void gtk_toggle_button_update                 (GtkActivatable       *activatable,
136                                                       GtkAction            *action,
137                                                       const gchar          *property_name);
138 static void gtk_toggle_button_sync_action_properties (GtkActivatable       *activatable,
139                                                       GtkAction            *action);
140
141 static GtkActivatableIface *parent_activatable_iface;
142 static guint                toggle_button_signals[LAST_SIGNAL] = { 0 };
143
144 G_DEFINE_TYPE_WITH_CODE (GtkToggleButton, gtk_toggle_button, GTK_TYPE_BUTTON,
145                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
146                                                 gtk_toggle_button_activatable_interface_init))
147
148 static void
149 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
150 {
151   GObjectClass *gobject_class;
152   GtkWidgetClass *widget_class;
153   GtkButtonClass *button_class;
154
155   gobject_class = G_OBJECT_CLASS (class);
156   widget_class = (GtkWidgetClass*) class;
157   button_class = (GtkButtonClass*) class;
158
159   gobject_class->set_property = gtk_toggle_button_set_property;
160   gobject_class->get_property = gtk_toggle_button_get_property;
161
162   widget_class->draw = gtk_toggle_button_draw;
163   widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
164
165   button_class->pressed = gtk_toggle_button_pressed;
166   button_class->released = gtk_toggle_button_released;
167   button_class->clicked = gtk_toggle_button_clicked;
168   button_class->enter = gtk_toggle_button_update_state;
169   button_class->leave = gtk_toggle_button_update_state;
170
171   class->toggled = NULL;
172
173   g_object_class_install_property (gobject_class,
174                                    PROP_ACTIVE,
175                                    g_param_spec_boolean ("active",
176                                                          P_("Active"),
177                                                          P_("If the toggle button should be pressed in"),
178                                                          FALSE,
179                                                          GTK_PARAM_READWRITE));
180
181   g_object_class_install_property (gobject_class,
182                                    PROP_INCONSISTENT,
183                                    g_param_spec_boolean ("inconsistent",
184                                                          P_("Inconsistent"),
185                                                          P_("If the toggle button is in an \"in between\" state"),
186                                                          FALSE,
187                                                          GTK_PARAM_READWRITE));
188
189   g_object_class_install_property (gobject_class,
190                                    PROP_DRAW_INDICATOR,
191                                    g_param_spec_boolean ("draw-indicator",
192                                                          P_("Draw Indicator"),
193                                                          P_("If the toggle part of the button is displayed"),
194                                                          FALSE,
195                                                          GTK_PARAM_READWRITE));
196
197   /**
198    * GtkToggleButton::toggled:
199    * @togglebutton: the object which received the signal.
200    *
201    * Should be connected if you wish to perform an action whenever the
202    * #GtkToggleButton's state is changed.
203    */
204   toggle_button_signals[TOGGLED] =
205     g_signal_new (I_("toggled"),
206                   G_OBJECT_CLASS_TYPE (gobject_class),
207                   G_SIGNAL_RUN_FIRST,
208                   G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
209                   NULL, NULL,
210                   _gtk_marshal_VOID__VOID,
211                   G_TYPE_NONE, 0);
212
213   g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate));
214
215   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_TOGGLE_BUTTON_ACCESSIBLE);
216 }
217
218 static void
219 gtk_toggle_button_init (GtkToggleButton *toggle_button)
220 {
221   GtkToggleButtonPrivate *priv;
222
223   toggle_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (toggle_button,
224                                                      GTK_TYPE_TOGGLE_BUTTON,
225                                                      GtkToggleButtonPrivate);
226   priv = toggle_button->priv;
227
228   priv->active = FALSE;
229   priv->draw_indicator = FALSE;
230   GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE;
231 }
232
233 static void
234 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
235 {
236   parent_activatable_iface = g_type_interface_peek_parent (iface);
237   iface->update = gtk_toggle_button_update;
238   iface->sync_action_properties = gtk_toggle_button_sync_action_properties;
239 }
240
241 static void
242 gtk_toggle_button_update (GtkActivatable *activatable,
243                           GtkAction      *action,
244                           const gchar    *property_name)
245 {
246   GtkToggleButton *button;
247
248   parent_activatable_iface->update (activatable, action, property_name);
249
250   button = GTK_TOGGLE_BUTTON (activatable);
251
252   if (strcmp (property_name, "active") == 0)
253     {
254       gtk_action_block_activate (action);
255       gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
256       gtk_action_unblock_activate (action);
257     }
258
259 }
260
261 static void
262 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
263                                           GtkAction      *action)
264 {
265   GtkToggleButton *button;
266
267   parent_activatable_iface->sync_action_properties (activatable, action);
268
269   if (!GTK_IS_TOGGLE_ACTION (action))
270     return;
271
272   button = GTK_TOGGLE_BUTTON (activatable);
273
274   gtk_action_block_activate (action);
275   gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
276   gtk_action_unblock_activate (action);
277 }
278
279 /**
280  * gtk_toggle_button_new:
281  *
282  * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().
283  *
284  * Returns: a new toggle button.
285  */
286 GtkWidget*
287 gtk_toggle_button_new (void)
288 {
289   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
290 }
291
292 /**
293  * gtk_toggle_button_new_with_label:
294  * @label: a string containing the message to be placed in the toggle button.
295  *
296  * Creates a new toggle button with a text label.
297  *
298  * Returns: a new toggle button.
299  */
300 GtkWidget*
301 gtk_toggle_button_new_with_label (const gchar *label)
302 {
303   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
304 }
305
306 /**
307  * gtk_toggle_button_new_with_mnemonic:
308  * @label: the text of the button, with an underscore in front of the
309  *         mnemonic character
310  *
311  * Creates a new #GtkToggleButton containing a label. The label
312  * will be created using gtk_label_new_with_mnemonic(), so underscores
313  * in @label indicate the mnemonic for the button.
314  *
315  * Returns: a new #GtkToggleButton
316  */
317 GtkWidget*
318 gtk_toggle_button_new_with_mnemonic (const gchar *label)
319 {
320   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, 
321                        "label", label, 
322                        "use-underline", TRUE, 
323                        NULL);
324 }
325
326 static void
327 gtk_toggle_button_set_property (GObject      *object,
328                                 guint         prop_id,
329                                 const GValue *value,
330                                 GParamSpec   *pspec)
331 {
332   GtkToggleButton *tb;
333
334   tb = GTK_TOGGLE_BUTTON (object);
335
336   switch (prop_id)
337     {
338     case PROP_ACTIVE:
339       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
340       break;
341     case PROP_INCONSISTENT:
342       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
343       break;
344     case PROP_DRAW_INDICATOR:
345       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
346       break;
347     default:
348       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349       break;
350     }
351 }
352
353 static void
354 gtk_toggle_button_get_property (GObject      *object,
355                                 guint         prop_id,
356                                 GValue       *value,
357                                 GParamSpec   *pspec)
358 {
359   GtkToggleButton *tb = GTK_TOGGLE_BUTTON (object);
360   GtkToggleButtonPrivate *priv = tb->priv;
361
362   switch (prop_id)
363     {
364     case PROP_ACTIVE:
365       g_value_set_boolean (value, priv->active);
366       break;
367     case PROP_INCONSISTENT:
368       g_value_set_boolean (value, priv->inconsistent);
369       break;
370     case PROP_DRAW_INDICATOR:
371       g_value_set_boolean (value, priv->draw_indicator);
372       break;
373     default:
374       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375       break;
376     }
377 }
378
379 /**
380  * gtk_toggle_button_set_mode:
381  * @toggle_button: a #GtkToggleButton
382  * @draw_indicator: if %TRUE, draw the button as a separate indicator
383  * and label; if %FALSE, draw the button like a normal button
384  *
385  * Sets whether the button is displayed as a separate indicator and label.
386  * You can call this function on a checkbutton or a radiobutton with
387  * @draw_indicator = %FALSE to make the button look like a normal button
388  *
389  * This function only affects instances of classes like #GtkCheckButton
390  * and #GtkRadioButton that derive from #GtkToggleButton,
391  * not instances of #GtkToggleButton itself.
392  */
393 void
394 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
395                             gboolean         draw_indicator)
396 {
397   GtkToggleButtonPrivate *priv;
398
399   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
400
401   priv = toggle_button->priv;
402
403   draw_indicator = draw_indicator ? TRUE : FALSE;
404
405   if (priv->draw_indicator != draw_indicator)
406     {
407       GtkStyleContext *context;
408
409       priv->draw_indicator = draw_indicator;
410       GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
411
412       if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
413         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
414
415       g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
416
417       /* Make toggle buttons conditionally have the "button"
418        * class depending on draw_indicator.
419        */
420       context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button));
421
422       if (draw_indicator)
423         gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
424       else
425         gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
426     }
427 }
428
429 /**
430  * gtk_toggle_button_get_mode:
431  * @toggle_button: a #GtkToggleButton
432  *
433  * Retrieves whether the button is displayed as a separate indicator
434  * and label. See gtk_toggle_button_set_mode().
435  *
436  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
437  *   and label.
438  **/
439 gboolean
440 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
441 {
442   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
443
444   return toggle_button->priv->draw_indicator;
445 }
446
447 /**
448  * gtk_toggle_button_set_active:
449  * @toggle_button: a #GtkToggleButton.
450  * @is_active: %TRUE or %FALSE.
451  *
452  * Sets the status of the toggle button. Set to %TRUE if you want the
453  * GtkToggleButton to be 'pressed in', and %FALSE to raise it.
454  * This action causes the toggled signal to be emitted.
455  */
456 void
457 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
458                               gboolean         is_active)
459 {
460   GtkToggleButtonPrivate *priv;
461
462   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
463
464   priv = toggle_button->priv;
465
466   is_active = is_active != FALSE;
467
468   if (priv->active != is_active)
469     gtk_button_clicked (GTK_BUTTON (toggle_button));
470 }
471
472 void
473 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
474                                gboolean         is_active)
475 {
476   toggle_button->priv->active = is_active;
477 }
478
479 /**
480  * gtk_toggle_button_get_active:
481  * @toggle_button: a #GtkToggleButton.
482  *
483  * Queries a #GtkToggleButton and returns its current state. Returns %TRUE if
484  * the toggle button is pressed in and %FALSE if it is raised.
485  *
486  * Returns: a #gboolean value.
487  */
488 gboolean
489 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
490 {
491   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
492
493   return toggle_button->priv->active;
494 }
495
496 /**
497  * gtk_toggle_button_toggled:
498  * @toggle_button: a #GtkToggleButton.
499  *
500  * Emits the #GtkToggleButton::toggled signal on the
501  * #GtkToggleButton. There is no good reason for an
502  * application ever to call this function.
503  */
504 void
505 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
506 {
507   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
508
509   g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
510 }
511
512 /**
513  * gtk_toggle_button_set_inconsistent:
514  * @toggle_button: a #GtkToggleButton
515  * @setting: %TRUE if state is inconsistent
516  *
517  * If the user has selected a range of elements (such as some text or
518  * spreadsheet cells) that are affected by a toggle button, and the
519  * current values in that range are inconsistent, you may want to
520  * display the toggle in an "in between" state. This function turns on
521  * "in between" display.  Normally you would turn off the inconsistent
522  * state again if the user toggles the toggle button. This has to be
523  * done manually, gtk_toggle_button_set_inconsistent() only affects
524  * visual appearance, it doesn't affect the semantics of the button.
525  * 
526  **/
527 void
528 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
529                                     gboolean         setting)
530 {
531   GtkToggleButtonPrivate *priv;
532
533   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
534
535   priv = toggle_button->priv;
536
537   setting = setting != FALSE;
538
539   if (setting != priv->inconsistent)
540     {
541       priv->inconsistent = setting;
542
543       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
544       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
545
546       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
547     }
548 }
549
550 /**
551  * gtk_toggle_button_get_inconsistent:
552  * @toggle_button: a #GtkToggleButton
553  * 
554  * Gets the value set by gtk_toggle_button_set_inconsistent().
555  * 
556  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
557  **/
558 gboolean
559 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
560 {
561   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
562
563   return toggle_button->priv->inconsistent;
564 }
565
566 static gint
567 gtk_toggle_button_draw (GtkWidget *widget,
568                         cairo_t   *cr)
569 {
570   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
571   GtkToggleButtonPrivate *priv = toggle_button->priv;
572   GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
573   GtkButton *button = GTK_BUTTON (widget);
574   GtkStateType state;
575
576   state = gtk_widget_get_state_flags (widget);
577
578   if (priv->inconsistent)
579     state |= GTK_STATE_FLAG_INCONSISTENT;
580   else if (button->priv->depressed)
581     state |= GTK_STATE_FLAG_ACTIVE;
582
583   _gtk_button_paint (button, cr,
584                      gtk_widget_get_allocated_width (widget),
585                      gtk_widget_get_allocated_height (widget),
586                      state);
587
588   if (child)
589     gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
590
591   return FALSE;
592 }
593
594 static gboolean
595 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
596                                      gboolean   group_cycling)
597 {
598   /*
599    * We override the standard implementation in 
600    * gtk_widget_real_mnemonic_activate() in order to focus the widget even
601    * if there is no mnemonic conflict.
602    */
603   if (gtk_widget_get_can_focus (widget))
604     gtk_widget_grab_focus (widget);
605
606   if (!group_cycling)
607     gtk_widget_activate (widget);
608
609   return TRUE;
610 }
611
612 static void
613 gtk_toggle_button_pressed (GtkButton *button)
614 {
615   button->priv->button_down = TRUE;
616
617   gtk_toggle_button_update_state (button);
618   gtk_widget_queue_draw (GTK_WIDGET (button));
619 }
620
621 static void
622 gtk_toggle_button_released (GtkButton *button)
623 {
624   if (button->priv->button_down)
625     {
626       button->priv->button_down = FALSE;
627
628       if (button->priv->in_button)
629         gtk_button_clicked (button);
630
631       gtk_toggle_button_update_state (button);
632       gtk_widget_queue_draw (GTK_WIDGET (button));
633     }
634 }
635
636 static void
637 gtk_toggle_button_clicked (GtkButton *button)
638 {
639   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
640   GtkToggleButtonPrivate *priv = toggle_button->priv;
641
642   priv->active = !priv->active;
643
644   gtk_toggle_button_toggled (toggle_button);
645
646   gtk_toggle_button_update_state (button);
647
648   g_object_notify (G_OBJECT (toggle_button), "active");
649
650   if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
651     GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
652 }
653
654 static void
655 gtk_toggle_button_update_state (GtkButton *button)
656 {
657   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
658   GtkToggleButtonPrivate *priv = toggle_button->priv;
659   gboolean depressed, touchscreen;
660   GtkStateFlags new_state = 0;
661
662   g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
663                 "gtk-touchscreen-mode", &touchscreen,
664                 NULL);
665
666   new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
667     ~(GTK_STATE_FLAG_INCONSISTENT |
668       GTK_STATE_FLAG_PRELIGHT |
669       GTK_STATE_FLAG_ACTIVE);
670
671   if (priv->inconsistent)
672     new_state |= GTK_STATE_FLAG_INCONSISTENT;
673
674   if (priv->inconsistent)
675     depressed = FALSE;
676   else if (button->priv->in_button && button->priv->button_down)
677     depressed = TRUE;
678   else
679     depressed = priv->active;
680
681   if (!touchscreen && button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
682     new_state |= GTK_STATE_FLAG_PRELIGHT;
683
684   if (depressed)
685     new_state |= GTK_STATE_FLAG_ACTIVE;
686
687   _gtk_button_set_depressed (button, depressed);
688   gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE);
689 }