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 gint gtk_toggle_button_draw (GtkWidget *widget,
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,
127 static void gtk_toggle_button_get_property (GObject *object,
131 static void gtk_toggle_button_update_state (GtkButton *button);
134 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
135 static void gtk_toggle_button_update (GtkActivatable *activatable,
137 const gchar *property_name);
138 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
141 static GtkActivatableIface *parent_activatable_iface;
142 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
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))
149 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
151 GObjectClass *gobject_class;
152 GtkWidgetClass *widget_class;
153 GtkButtonClass *button_class;
155 gobject_class = G_OBJECT_CLASS (class);
156 widget_class = (GtkWidgetClass*) class;
157 button_class = (GtkButtonClass*) class;
159 gobject_class->set_property = gtk_toggle_button_set_property;
160 gobject_class->get_property = gtk_toggle_button_get_property;
162 widget_class->draw = gtk_toggle_button_draw;
163 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
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;
171 class->toggled = NULL;
173 g_object_class_install_property (gobject_class,
175 g_param_spec_boolean ("active",
177 P_("If the toggle button should be pressed in"),
179 GTK_PARAM_READWRITE));
181 g_object_class_install_property (gobject_class,
183 g_param_spec_boolean ("inconsistent",
185 P_("If the toggle button is in an \"in between\" state"),
187 GTK_PARAM_READWRITE));
189 g_object_class_install_property (gobject_class,
191 g_param_spec_boolean ("draw-indicator",
192 P_("Draw Indicator"),
193 P_("If the toggle part of the button is displayed"),
195 GTK_PARAM_READWRITE));
198 * GtkToggleButton::toggled:
199 * @togglebutton: the object which received the signal.
201 * Should be connected if you wish to perform an action whenever the
202 * #GtkToggleButton's state is changed.
204 toggle_button_signals[TOGGLED] =
205 g_signal_new (I_("toggled"),
206 G_OBJECT_CLASS_TYPE (gobject_class),
208 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
210 _gtk_marshal_VOID__VOID,
213 g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate));
215 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_TOGGLE_BUTTON_ACCESSIBLE);
219 gtk_toggle_button_init (GtkToggleButton *toggle_button)
221 GtkToggleButtonPrivate *priv;
223 toggle_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (toggle_button,
224 GTK_TYPE_TOGGLE_BUTTON,
225 GtkToggleButtonPrivate);
226 priv = toggle_button->priv;
228 priv->active = FALSE;
229 priv->draw_indicator = FALSE;
230 GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE;
234 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
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;
242 gtk_toggle_button_update (GtkActivatable *activatable,
244 const gchar *property_name)
246 GtkToggleButton *button;
248 parent_activatable_iface->update (activatable, action, property_name);
250 button = GTK_TOGGLE_BUTTON (activatable);
252 if (strcmp (property_name, "active") == 0)
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);
262 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
265 GtkToggleButton *button;
267 parent_activatable_iface->sync_action_properties (activatable, action);
269 if (!GTK_IS_TOGGLE_ACTION (action))
272 button = GTK_TOGGLE_BUTTON (activatable);
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);
280 * gtk_toggle_button_new:
282 * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().
284 * Returns: a new toggle button.
287 gtk_toggle_button_new (void)
289 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
293 * gtk_toggle_button_new_with_label:
294 * @label: a string containing the message to be placed in the toggle button.
296 * Creates a new toggle button with a text label.
298 * Returns: a new toggle button.
301 gtk_toggle_button_new_with_label (const gchar *label)
303 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
307 * gtk_toggle_button_new_with_mnemonic:
308 * @label: the text of the button, with an underscore in front of the
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.
315 * Returns: a new #GtkToggleButton
318 gtk_toggle_button_new_with_mnemonic (const gchar *label)
320 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
322 "use-underline", TRUE,
327 gtk_toggle_button_set_property (GObject *object,
334 tb = GTK_TOGGLE_BUTTON (object);
339 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
341 case PROP_INCONSISTENT:
342 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
344 case PROP_DRAW_INDICATOR:
345 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gtk_toggle_button_get_property (GObject *object,
359 GtkToggleButton *tb = GTK_TOGGLE_BUTTON (object);
360 GtkToggleButtonPrivate *priv = tb->priv;
365 g_value_set_boolean (value, priv->active);
367 case PROP_INCONSISTENT:
368 g_value_set_boolean (value, priv->inconsistent);
370 case PROP_DRAW_INDICATOR:
371 g_value_set_boolean (value, priv->draw_indicator);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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
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
389 * This function only affects instances of classes like #GtkCheckButton
390 * and #GtkRadioButton that derive from #GtkToggleButton,
391 * not instances of #GtkToggleButton itself.
394 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
395 gboolean draw_indicator)
397 GtkToggleButtonPrivate *priv;
399 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
401 priv = toggle_button->priv;
403 draw_indicator = draw_indicator ? TRUE : FALSE;
405 if (priv->draw_indicator != draw_indicator)
407 GtkStyleContext *context;
409 priv->draw_indicator = draw_indicator;
410 GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
412 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
413 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
415 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
417 /* Make toggle buttons conditionally have the "button"
418 * class depending on draw_indicator.
420 context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button));
423 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
425 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
430 * gtk_toggle_button_get_mode:
431 * @toggle_button: a #GtkToggleButton
433 * Retrieves whether the button is displayed as a separate indicator
434 * and label. See gtk_toggle_button_set_mode().
436 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
440 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
442 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
444 return toggle_button->priv->draw_indicator;
448 * gtk_toggle_button_set_active:
449 * @toggle_button: a #GtkToggleButton.
450 * @is_active: %TRUE or %FALSE.
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.
457 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
460 GtkToggleButtonPrivate *priv;
462 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
464 priv = toggle_button->priv;
466 is_active = is_active != FALSE;
468 if (priv->active != is_active)
469 gtk_button_clicked (GTK_BUTTON (toggle_button));
473 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
476 toggle_button->priv->active = is_active;
480 * gtk_toggle_button_get_active:
481 * @toggle_button: a #GtkToggleButton.
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.
486 * Returns: a #gboolean value.
489 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
491 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
493 return toggle_button->priv->active;
497 * gtk_toggle_button_toggled:
498 * @toggle_button: a #GtkToggleButton.
500 * Emits the #GtkToggleButton::toggled signal on the
501 * #GtkToggleButton. There is no good reason for an
502 * application ever to call this function.
505 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
507 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
509 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
513 * gtk_toggle_button_set_inconsistent:
514 * @toggle_button: a #GtkToggleButton
515 * @setting: %TRUE if state is inconsistent
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.
528 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
531 GtkToggleButtonPrivate *priv;
533 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
535 priv = toggle_button->priv;
537 setting = setting != FALSE;
539 if (setting != priv->inconsistent)
541 priv->inconsistent = setting;
543 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
544 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
546 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
551 * gtk_toggle_button_get_inconsistent:
552 * @toggle_button: a #GtkToggleButton
554 * Gets the value set by gtk_toggle_button_set_inconsistent().
556 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
559 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
561 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
563 return toggle_button->priv->inconsistent;
567 gtk_toggle_button_draw (GtkWidget *widget,
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);
576 state = gtk_widget_get_state_flags (widget);
578 if (priv->inconsistent)
579 state |= GTK_STATE_FLAG_INCONSISTENT;
580 else if (button->priv->depressed)
581 state |= GTK_STATE_FLAG_ACTIVE;
583 _gtk_button_paint (button, cr,
584 gtk_widget_get_allocated_width (widget),
585 gtk_widget_get_allocated_height (widget),
589 gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
595 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
596 gboolean group_cycling)
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.
603 if (gtk_widget_get_can_focus (widget))
604 gtk_widget_grab_focus (widget);
607 gtk_widget_activate (widget);
613 gtk_toggle_button_pressed (GtkButton *button)
615 button->priv->button_down = TRUE;
617 gtk_toggle_button_update_state (button);
618 gtk_widget_queue_draw (GTK_WIDGET (button));
622 gtk_toggle_button_released (GtkButton *button)
624 if (button->priv->button_down)
626 button->priv->button_down = FALSE;
628 if (button->priv->in_button)
629 gtk_button_clicked (button);
631 gtk_toggle_button_update_state (button);
632 gtk_widget_queue_draw (GTK_WIDGET (button));
637 gtk_toggle_button_clicked (GtkButton *button)
639 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
640 GtkToggleButtonPrivate *priv = toggle_button->priv;
642 priv->active = !priv->active;
644 gtk_toggle_button_toggled (toggle_button);
646 gtk_toggle_button_update_state (button);
648 g_object_notify (G_OBJECT (toggle_button), "active");
650 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
651 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
655 gtk_toggle_button_update_state (GtkButton *button)
657 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
658 GtkToggleButtonPrivate *priv = toggle_button->priv;
659 gboolean depressed, touchscreen;
660 GtkStateFlags new_state = 0;
662 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
663 "gtk-touchscreen-mode", &touchscreen,
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);
671 if (priv->inconsistent)
672 new_state |= GTK_STATE_FLAG_INCONSISTENT;
674 if (priv->inconsistent)
676 else if (button->priv->in_button && button->priv->button_down)
679 depressed = priv->active;
681 if (!touchscreen && button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
682 new_state |= GTK_STATE_FLAG_PRELIGHT;
685 new_state |= GTK_STATE_FLAG_ACTIVE;
687 _gtk_button_set_depressed (button, depressed);
688 gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE);