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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
29 #include "gtktogglebutton.h"
31 #include "gtkbuttonprivate.h"
34 #include "gtkmarshalers.h"
35 #include "gtktoggleaction.h"
36 #include "gtkactivatable.h"
37 #include "gtkprivate.h"
39 #include "a11y/gtktogglebuttonaccessible.h"
43 * SECTION:gtktogglebutton
44 * @Short_description: Create buttons which retain their state
45 * @Title: GtkToggleButton
46 * @See_also: #GtkButton, #GtkCheckButton, #GtkCheckMenuItem
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
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).
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().
61 * To simply switch the state of a toggle button, use gtk_toggle_button_toggled().
64 * <title>Creating two #GtkToggleButton widgets.</title>
66 * void make_toggles (void) {
67 * GtkWidget *dialog, *toggle1, *toggle2;
69 * dialog = gtk_dialog_new (<!-- -->);
70 * toggle1 = gtk_toggle_button_new_with_label ("Hi, i'm a toggle button.");
72 * // Makes this toggle button invisible
73 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1), TRUE);
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);
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);
87 * gtk_widget_show_all (dialog);
94 #define DEFAULT_LEFT_POS 4
95 #define DEFAULT_TOP_POS 4
96 #define DEFAULT_SPACING 7
98 struct _GtkToggleButtonPrivate
101 guint draw_indicator : 1;
102 guint inconsistent : 1;
118 static gint gtk_toggle_button_draw (GtkWidget *widget,
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,
129 static void gtk_toggle_button_get_property (GObject *object,
133 static void gtk_toggle_button_update_state (GtkButton *button);
136 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
137 static void gtk_toggle_button_update (GtkActivatable *activatable,
139 const gchar *property_name);
140 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
143 static GtkActivatableIface *parent_activatable_iface;
144 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
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))
151 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
153 GObjectClass *gobject_class;
154 GtkWidgetClass *widget_class;
155 GtkButtonClass *button_class;
157 gobject_class = G_OBJECT_CLASS (class);
158 widget_class = (GtkWidgetClass*) class;
159 button_class = (GtkButtonClass*) class;
161 gobject_class->set_property = gtk_toggle_button_set_property;
162 gobject_class->get_property = gtk_toggle_button_get_property;
164 widget_class->draw = gtk_toggle_button_draw;
165 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
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;
173 class->toggled = NULL;
175 g_object_class_install_property (gobject_class,
177 g_param_spec_boolean ("active",
179 P_("If the toggle button should be pressed in"),
181 GTK_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class,
185 g_param_spec_boolean ("inconsistent",
187 P_("If the toggle button is in an \"in between\" state"),
189 GTK_PARAM_READWRITE));
191 g_object_class_install_property (gobject_class,
193 g_param_spec_boolean ("draw-indicator",
194 P_("Draw Indicator"),
195 P_("If the toggle part of the button is displayed"),
197 GTK_PARAM_READWRITE));
200 * GtkToggleButton::toggled:
201 * @togglebutton: the object which received the signal.
203 * Should be connected if you wish to perform an action whenever the
204 * #GtkToggleButton's state is changed.
206 toggle_button_signals[TOGGLED] =
207 g_signal_new (I_("toggled"),
208 G_OBJECT_CLASS_TYPE (gobject_class),
210 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
212 _gtk_marshal_VOID__VOID,
215 g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate));
217 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_TOGGLE_BUTTON_ACCESSIBLE);
221 gtk_toggle_button_init (GtkToggleButton *toggle_button)
223 GtkToggleButtonPrivate *priv;
225 toggle_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (toggle_button,
226 GTK_TYPE_TOGGLE_BUTTON,
227 GtkToggleButtonPrivate);
228 priv = toggle_button->priv;
230 priv->active = FALSE;
231 priv->draw_indicator = FALSE;
232 GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE;
236 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
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;
244 gtk_toggle_button_update (GtkActivatable *activatable,
246 const gchar *property_name)
248 GtkToggleButton *button;
250 parent_activatable_iface->update (activatable, action, property_name);
252 button = GTK_TOGGLE_BUTTON (activatable);
254 if (strcmp (property_name, "active") == 0)
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);
264 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
267 GtkToggleButton *button;
269 parent_activatable_iface->sync_action_properties (activatable, action);
271 if (!GTK_IS_TOGGLE_ACTION (action))
274 button = GTK_TOGGLE_BUTTON (activatable);
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);
282 * gtk_toggle_button_new:
284 * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().
286 * Returns: a new toggle button.
289 gtk_toggle_button_new (void)
291 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
295 * gtk_toggle_button_new_with_label:
296 * @label: a string containing the message to be placed in the toggle button.
298 * Creates a new toggle button with a text label.
300 * Returns: a new toggle button.
303 gtk_toggle_button_new_with_label (const gchar *label)
305 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
309 * gtk_toggle_button_new_with_mnemonic:
310 * @label: the text of the button, with an underscore in front of the
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.
317 * Returns: a new #GtkToggleButton
320 gtk_toggle_button_new_with_mnemonic (const gchar *label)
322 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
324 "use-underline", TRUE,
329 gtk_toggle_button_set_property (GObject *object,
336 tb = GTK_TOGGLE_BUTTON (object);
341 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
343 case PROP_INCONSISTENT:
344 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
346 case PROP_DRAW_INDICATOR:
347 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 gtk_toggle_button_get_property (GObject *object,
361 GtkToggleButton *tb = GTK_TOGGLE_BUTTON (object);
362 GtkToggleButtonPrivate *priv = tb->priv;
367 g_value_set_boolean (value, priv->active);
369 case PROP_INCONSISTENT:
370 g_value_set_boolean (value, priv->inconsistent);
372 case PROP_DRAW_INDICATOR:
373 g_value_set_boolean (value, priv->draw_indicator);
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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
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
391 * This function only affects instances of classes like #GtkCheckButton
392 * and #GtkRadioButton that derive from #GtkToggleButton,
393 * not instances of #GtkToggleButton itself.
396 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
397 gboolean draw_indicator)
399 GtkToggleButtonPrivate *priv;
401 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
403 priv = toggle_button->priv;
405 draw_indicator = draw_indicator ? TRUE : FALSE;
407 if (priv->draw_indicator != draw_indicator)
409 GtkStyleContext *context;
411 priv->draw_indicator = draw_indicator;
412 GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
414 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
415 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
417 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
419 /* Make toggle buttons conditionally have the "button"
420 * class depending on draw_indicator.
422 context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button));
425 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
427 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
432 * gtk_toggle_button_get_mode:
433 * @toggle_button: a #GtkToggleButton
435 * Retrieves whether the button is displayed as a separate indicator
436 * and label. See gtk_toggle_button_set_mode().
438 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
442 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
444 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
446 return toggle_button->priv->draw_indicator;
450 * gtk_toggle_button_set_active:
451 * @toggle_button: a #GtkToggleButton.
452 * @is_active: %TRUE or %FALSE.
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.
459 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
462 GtkToggleButtonPrivate *priv;
464 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
466 priv = toggle_button->priv;
468 is_active = is_active != FALSE;
470 if (priv->active != is_active)
471 gtk_button_clicked (GTK_BUTTON (toggle_button));
475 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
478 toggle_button->priv->active = is_active;
482 * gtk_toggle_button_get_active:
483 * @toggle_button: a #GtkToggleButton.
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.
488 * Returns: a #gboolean value.
491 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
493 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
495 return toggle_button->priv->active;
499 * gtk_toggle_button_toggled:
500 * @toggle_button: a #GtkToggleButton.
502 * Emits the #GtkToggleButton::toggled signal on the
503 * #GtkToggleButton. There is no good reason for an
504 * application ever to call this function.
507 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
509 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
511 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
515 * gtk_toggle_button_set_inconsistent:
516 * @toggle_button: a #GtkToggleButton
517 * @setting: %TRUE if state is inconsistent
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.
530 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
533 GtkToggleButtonPrivate *priv;
535 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
537 priv = toggle_button->priv;
539 setting = setting != FALSE;
541 if (setting != priv->inconsistent)
543 priv->inconsistent = setting;
545 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
546 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
548 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
553 * gtk_toggle_button_get_inconsistent:
554 * @toggle_button: a #GtkToggleButton
556 * Gets the value set by gtk_toggle_button_set_inconsistent().
558 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
561 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
563 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
565 return toggle_button->priv->inconsistent;
569 gtk_toggle_button_draw (GtkWidget *widget,
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);
578 state = gtk_widget_get_state_flags (widget);
580 if (priv->inconsistent)
581 state |= GTK_STATE_FLAG_INCONSISTENT;
582 else if (button->priv->depressed)
583 state |= GTK_STATE_FLAG_ACTIVE;
585 _gtk_button_paint (button, cr,
586 gtk_widget_get_allocated_width (widget),
587 gtk_widget_get_allocated_height (widget),
591 gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
597 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
598 gboolean group_cycling)
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.
605 if (gtk_widget_get_can_focus (widget))
606 gtk_widget_grab_focus (widget);
609 gtk_widget_activate (widget);
615 gtk_toggle_button_pressed (GtkButton *button)
617 button->priv->button_down = TRUE;
619 gtk_toggle_button_update_state (button);
620 gtk_widget_queue_draw (GTK_WIDGET (button));
624 gtk_toggle_button_released (GtkButton *button)
626 if (button->priv->button_down)
628 button->priv->button_down = FALSE;
630 if (button->priv->in_button)
631 gtk_button_clicked (button);
633 gtk_toggle_button_update_state (button);
634 gtk_widget_queue_draw (GTK_WIDGET (button));
639 gtk_toggle_button_clicked (GtkButton *button)
641 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
642 GtkToggleButtonPrivate *priv = toggle_button->priv;
644 priv->active = !priv->active;
646 gtk_toggle_button_toggled (toggle_button);
648 gtk_toggle_button_update_state (button);
650 g_object_notify (G_OBJECT (toggle_button), "active");
652 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
653 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
657 gtk_toggle_button_update_state (GtkButton *button)
659 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
660 GtkToggleButtonPrivate *priv = toggle_button->priv;
661 gboolean depressed, touchscreen;
662 GtkStateFlags new_state = 0;
664 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
665 "gtk-touchscreen-mode", &touchscreen,
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);
673 if (priv->inconsistent)
674 new_state |= GTK_STATE_FLAG_INCONSISTENT;
676 if (priv->inconsistent)
678 else if (button->priv->in_button && button->priv->button_down)
681 depressed = priv->active;
683 if (!touchscreen && button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
684 new_state |= GTK_STATE_FLAG_PRELIGHT;
687 new_state |= GTK_STATE_FLAG_ACTIVE;
689 _gtk_button_set_depressed (button, depressed);
690 gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE);