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