1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
27 #include "gtktogglebutton.h"
29 #include "gtkbuttonprivate.h"
32 #include "gtkmarshalers.h"
33 #include "gtktoggleaction.h"
34 #include "gtkactivatable.h"
35 #include "gtkprivate.h"
37 #include "a11y/gtktogglebuttonaccessible.h"
41 * SECTION:gtktogglebutton
42 * @Short_description: Create buttons which retain their state
43 * @Title: GtkToggleButton
44 * @See_also: #GtkButton, #GtkCheckButton, #GtkCheckMenuItem
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
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).
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().
59 * To simply switch the state of a toggle button, use gtk_toggle_button_toggled().
62 * <title>Creating two #GtkToggleButton widgets.</title>
64 * void make_toggles (void) {
65 * GtkWidget *dialog, *toggle1, *toggle2;
67 * dialog = gtk_dialog_new (<!-- -->);
68 * toggle1 = gtk_toggle_button_new_with_label ("Hi, i'm a toggle button.");
70 * // Makes this toggle button invisible
71 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1), TRUE);
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);
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);
85 * gtk_widget_show_all (dialog);
92 #define DEFAULT_LEFT_POS 4
93 #define DEFAULT_TOP_POS 4
94 #define DEFAULT_SPACING 7
96 struct _GtkToggleButtonPrivate
99 guint draw_indicator : 1;
100 guint inconsistent : 1;
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,
125 static void gtk_toggle_button_get_property (GObject *object,
129 static void gtk_toggle_button_update_state (GtkButton *button);
132 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
133 static void gtk_toggle_button_update (GtkActivatable *activatable,
135 const gchar *property_name);
136 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
139 static GtkActivatableIface *parent_activatable_iface;
140 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
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))
147 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
149 GObjectClass *gobject_class;
150 GtkWidgetClass *widget_class;
151 GtkButtonClass *button_class;
153 gobject_class = G_OBJECT_CLASS (class);
154 widget_class = (GtkWidgetClass*) class;
155 button_class = (GtkButtonClass*) class;
157 gobject_class->set_property = gtk_toggle_button_set_property;
158 gobject_class->get_property = gtk_toggle_button_get_property;
160 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
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;
168 class->toggled = NULL;
170 g_object_class_install_property (gobject_class,
172 g_param_spec_boolean ("active",
174 P_("If the toggle button should be pressed in"),
176 GTK_PARAM_READWRITE));
178 g_object_class_install_property (gobject_class,
180 g_param_spec_boolean ("inconsistent",
182 P_("If the toggle button is in an \"in between\" state"),
184 GTK_PARAM_READWRITE));
186 g_object_class_install_property (gobject_class,
188 g_param_spec_boolean ("draw-indicator",
189 P_("Draw Indicator"),
190 P_("If the toggle part of the button is displayed"),
192 GTK_PARAM_READWRITE));
195 * GtkToggleButton::toggled:
196 * @togglebutton: the object which received the signal.
198 * Should be connected if you wish to perform an action whenever the
199 * #GtkToggleButton's state is changed.
201 toggle_button_signals[TOGGLED] =
202 g_signal_new (I_("toggled"),
203 G_OBJECT_CLASS_TYPE (gobject_class),
205 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
207 _gtk_marshal_VOID__VOID,
210 g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate));
212 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_TOGGLE_BUTTON_ACCESSIBLE);
216 gtk_toggle_button_init (GtkToggleButton *toggle_button)
218 GtkToggleButtonPrivate *priv;
220 toggle_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (toggle_button,
221 GTK_TYPE_TOGGLE_BUTTON,
222 GtkToggleButtonPrivate);
223 priv = toggle_button->priv;
225 priv->active = FALSE;
226 priv->draw_indicator = FALSE;
227 GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE;
231 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
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;
239 gtk_toggle_button_update (GtkActivatable *activatable,
241 const gchar *property_name)
243 GtkToggleButton *button;
245 parent_activatable_iface->update (activatable, action, property_name);
247 button = GTK_TOGGLE_BUTTON (activatable);
249 if (strcmp (property_name, "active") == 0)
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);
259 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
262 GtkToggleButton *button;
264 parent_activatable_iface->sync_action_properties (activatable, action);
266 if (!GTK_IS_TOGGLE_ACTION (action))
269 button = GTK_TOGGLE_BUTTON (activatable);
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);
277 * gtk_toggle_button_new:
279 * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().
281 * Returns: a new toggle button.
284 gtk_toggle_button_new (void)
286 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
290 * gtk_toggle_button_new_with_label:
291 * @label: a string containing the message to be placed in the toggle button.
293 * Creates a new toggle button with a text label.
295 * Returns: a new toggle button.
298 gtk_toggle_button_new_with_label (const gchar *label)
300 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
304 * gtk_toggle_button_new_with_mnemonic:
305 * @label: the text of the button, with an underscore in front of the
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.
312 * Returns: a new #GtkToggleButton
315 gtk_toggle_button_new_with_mnemonic (const gchar *label)
317 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
319 "use-underline", TRUE,
324 gtk_toggle_button_set_property (GObject *object,
331 tb = GTK_TOGGLE_BUTTON (object);
336 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
338 case PROP_INCONSISTENT:
339 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
341 case PROP_DRAW_INDICATOR:
342 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 gtk_toggle_button_get_property (GObject *object,
356 GtkToggleButton *tb = GTK_TOGGLE_BUTTON (object);
357 GtkToggleButtonPrivate *priv = tb->priv;
362 g_value_set_boolean (value, priv->active);
364 case PROP_INCONSISTENT:
365 g_value_set_boolean (value, priv->inconsistent);
367 case PROP_DRAW_INDICATOR:
368 g_value_set_boolean (value, priv->draw_indicator);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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
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
386 * This function only affects instances of classes like #GtkCheckButton
387 * and #GtkRadioButton that derive from #GtkToggleButton,
388 * not instances of #GtkToggleButton itself.
391 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
392 gboolean draw_indicator)
394 GtkToggleButtonPrivate *priv;
396 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
398 priv = toggle_button->priv;
400 draw_indicator = draw_indicator ? TRUE : FALSE;
402 if (priv->draw_indicator != draw_indicator)
404 GtkStyleContext *context;
406 priv->draw_indicator = draw_indicator;
407 GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
409 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
410 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
412 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
414 /* Make toggle buttons conditionally have the "button"
415 * class depending on draw_indicator.
417 context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button));
420 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
422 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
427 * gtk_toggle_button_get_mode:
428 * @toggle_button: a #GtkToggleButton
430 * Retrieves whether the button is displayed as a separate indicator
431 * and label. See gtk_toggle_button_set_mode().
433 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
437 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
439 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
441 return toggle_button->priv->draw_indicator;
445 * gtk_toggle_button_set_active:
446 * @toggle_button: a #GtkToggleButton.
447 * @is_active: %TRUE or %FALSE.
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 toggled signal to be emitted.
454 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
457 GtkToggleButtonPrivate *priv;
459 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
461 priv = toggle_button->priv;
463 is_active = is_active != FALSE;
465 if (priv->active != is_active)
466 gtk_button_clicked (GTK_BUTTON (toggle_button));
470 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
473 toggle_button->priv->active = is_active;
477 * gtk_toggle_button_get_active:
478 * @toggle_button: a #GtkToggleButton.
480 * Queries a #GtkToggleButton and returns its current state. Returns %TRUE if
481 * the toggle button is pressed in and %FALSE if it is raised.
483 * Returns: a #gboolean value.
486 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
488 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
490 return toggle_button->priv->active;
494 * gtk_toggle_button_toggled:
495 * @toggle_button: a #GtkToggleButton.
497 * Emits the #GtkToggleButton::toggled signal on the
498 * #GtkToggleButton. There is no good reason for an
499 * application ever to call this function.
502 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
504 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
506 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
510 * gtk_toggle_button_set_inconsistent:
511 * @toggle_button: a #GtkToggleButton
512 * @setting: %TRUE if state is inconsistent
514 * If the user has selected a range of elements (such as some text or
515 * spreadsheet cells) that are affected by a toggle button, and the
516 * current values in that range are inconsistent, you may want to
517 * display the toggle in an "in between" state. This function turns on
518 * "in between" display. Normally you would turn off the inconsistent
519 * state again if the user toggles the toggle button. This has to be
520 * done manually, gtk_toggle_button_set_inconsistent() only affects
521 * visual appearance, it doesn't affect the semantics of the button.
525 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
528 GtkToggleButtonPrivate *priv;
530 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
532 priv = toggle_button->priv;
534 setting = setting != FALSE;
536 if (setting != priv->inconsistent)
538 priv->inconsistent = setting;
540 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
541 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
543 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
548 * gtk_toggle_button_get_inconsistent:
549 * @toggle_button: a #GtkToggleButton
551 * Gets the value set by gtk_toggle_button_set_inconsistent().
553 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
556 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
558 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
560 return toggle_button->priv->inconsistent;
564 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
565 gboolean group_cycling)
568 * We override the standard implementation in
569 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
570 * if there is no mnemonic conflict.
572 if (gtk_widget_get_can_focus (widget))
573 gtk_widget_grab_focus (widget);
576 gtk_widget_activate (widget);
582 gtk_toggle_button_pressed (GtkButton *button)
584 button->priv->button_down = TRUE;
586 gtk_toggle_button_update_state (button);
587 gtk_widget_queue_draw (GTK_WIDGET (button));
591 gtk_toggle_button_released (GtkButton *button)
593 if (button->priv->button_down)
595 button->priv->button_down = FALSE;
597 if (button->priv->in_button)
598 gtk_button_clicked (button);
600 gtk_toggle_button_update_state (button);
601 gtk_widget_queue_draw (GTK_WIDGET (button));
606 gtk_toggle_button_clicked (GtkButton *button)
608 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
609 GtkToggleButtonPrivate *priv = toggle_button->priv;
611 priv->active = !priv->active;
613 gtk_toggle_button_toggled (toggle_button);
615 gtk_toggle_button_update_state (button);
617 g_object_notify (G_OBJECT (toggle_button), "active");
619 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
620 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
624 gtk_toggle_button_update_state (GtkButton *button)
626 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
627 GtkToggleButtonPrivate *priv = toggle_button->priv;
629 GtkStateFlags new_state = 0;
631 new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
632 ~(GTK_STATE_FLAG_INCONSISTENT |
633 GTK_STATE_FLAG_PRELIGHT |
634 GTK_STATE_FLAG_ACTIVE);
636 if (priv->inconsistent)
637 new_state |= GTK_STATE_FLAG_INCONSISTENT;
639 if (priv->inconsistent)
641 else if (button->priv->in_button && button->priv->button_down)
644 depressed = priv->active;
646 if (button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
647 new_state |= GTK_STATE_FLAG_PRELIGHT;
650 new_state |= GTK_STATE_FLAG_ACTIVE;
652 _gtk_button_set_depressed (button, depressed);
653 gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE);