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