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"
42 * SECTION:gtktogglebutton
43 * @Short_description: Create buttons which retain their state
44 * @Title: GtkToggleButton
45 * @See_also: #GtkButton, #GtkCheckButton, #GtkCheckMenuItem
47 * A #GtkToggleButton is a #GtkButton which will remain 'pressed-in' when
48 * clicked. Clicking again will cause the toggle button to return to its
51 * A toggle button is created by calling either gtk_toggle_button_new() or
52 * gtk_toggle_button_new_with_label(). If using the former, it is advisable to
53 * pack a widget, (such as a #GtkLabel and/or a #GtkPixmap), into the toggle
54 * button's container. (See #GtkButton for more information).
56 * The state of a #GtkToggleButton can be set specifically using
57 * gtk_toggle_button_set_active(), and retrieved using
58 * gtk_toggle_button_get_active().
60 * To simply switch the state of a toggle button, use gtk_toggle_button_toggled().
63 * <title>Creating two #GtkToggleButton widgets.</title>
65 * void make_toggles (void) {
66 * GtkWidget *dialog, *toggle1, *toggle2;
68 * dialog = gtk_dialog_new (<!-- -->);
69 * toggle1 = gtk_toggle_button_new_with_label ("Hi, i'm a toggle button.");
71 * // Makes this toggle button invisible
72 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1), TRUE);
74 * g_signal_connect (toggle1, "toggled",
75 * G_CALLBACK (output_state), NULL);
76 * gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
77 * toggle1, FALSE, FALSE, 2);
79 * toggle2 = gtk_toggle_button_new_with_label ("Hi, i'm another toggle button.");
80 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle2), FALSE);
81 * g_signal_connect (toggle2, "toggled",
82 * G_CALLBACK (output_state), NULL);
83 * gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
84 * toggle2, FALSE, FALSE, 2);
86 * gtk_widget_show_all (dialog);
93 #define DEFAULT_LEFT_POS 4
94 #define DEFAULT_TOP_POS 4
95 #define DEFAULT_SPACING 7
97 struct _GtkToggleButtonPrivate
100 guint draw_indicator : 1;
101 guint inconsistent : 1;
117 static gint gtk_toggle_button_draw (GtkWidget *widget,
119 static gboolean gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
120 gboolean group_cycling);
121 static void gtk_toggle_button_pressed (GtkButton *button);
122 static void gtk_toggle_button_released (GtkButton *button);
123 static void gtk_toggle_button_clicked (GtkButton *button);
124 static void gtk_toggle_button_set_property (GObject *object,
128 static void gtk_toggle_button_get_property (GObject *object,
132 static void gtk_toggle_button_update_state (GtkButton *button);
135 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
136 static void gtk_toggle_button_update (GtkActivatable *activatable,
138 const gchar *property_name);
139 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
142 static GtkActivatableIface *parent_activatable_iface;
143 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
145 G_DEFINE_TYPE_WITH_CODE (GtkToggleButton, gtk_toggle_button, GTK_TYPE_BUTTON,
146 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
147 gtk_toggle_button_activatable_interface_init))
150 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
152 GObjectClass *gobject_class;
153 GtkWidgetClass *widget_class;
154 GtkButtonClass *button_class;
156 gobject_class = G_OBJECT_CLASS (class);
157 widget_class = (GtkWidgetClass*) class;
158 button_class = (GtkButtonClass*) class;
160 gobject_class->set_property = gtk_toggle_button_set_property;
161 gobject_class->get_property = gtk_toggle_button_get_property;
163 widget_class->draw = gtk_toggle_button_draw;
164 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
166 button_class->pressed = gtk_toggle_button_pressed;
167 button_class->released = gtk_toggle_button_released;
168 button_class->clicked = gtk_toggle_button_clicked;
169 button_class->enter = gtk_toggle_button_update_state;
170 button_class->leave = gtk_toggle_button_update_state;
172 class->toggled = NULL;
174 g_object_class_install_property (gobject_class,
176 g_param_spec_boolean ("active",
178 P_("If the toggle button should be pressed in"),
180 GTK_PARAM_READWRITE));
182 g_object_class_install_property (gobject_class,
184 g_param_spec_boolean ("inconsistent",
186 P_("If the toggle button is in an \"in between\" state"),
188 GTK_PARAM_READWRITE));
190 g_object_class_install_property (gobject_class,
192 g_param_spec_boolean ("draw-indicator",
193 P_("Draw Indicator"),
194 P_("If the toggle part of the button is displayed"),
196 GTK_PARAM_READWRITE));
199 * GtkToggleButton::toggled:
200 * @togglebutton: the object which received the signal.
202 * Should be connected if you wish to perform an action whenever the
203 * #GtkToggleButton's state is changed.
205 toggle_button_signals[TOGGLED] =
206 g_signal_new (I_("toggled"),
207 G_OBJECT_CLASS_TYPE (gobject_class),
209 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
211 _gtk_marshal_VOID__VOID,
214 g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate));
218 gtk_toggle_button_init (GtkToggleButton *toggle_button)
220 GtkToggleButtonPrivate *priv;
222 toggle_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (toggle_button,
223 GTK_TYPE_TOGGLE_BUTTON,
224 GtkToggleButtonPrivate);
225 priv = toggle_button->priv;
227 priv->active = FALSE;
228 priv->draw_indicator = FALSE;
229 GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE;
233 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
235 parent_activatable_iface = g_type_interface_peek_parent (iface);
236 iface->update = gtk_toggle_button_update;
237 iface->sync_action_properties = gtk_toggle_button_sync_action_properties;
241 gtk_toggle_button_update (GtkActivatable *activatable,
243 const gchar *property_name)
245 GtkToggleButton *button;
247 parent_activatable_iface->update (activatable, action, property_name);
249 button = GTK_TOGGLE_BUTTON (activatable);
251 if (strcmp (property_name, "active") == 0)
253 gtk_action_block_activate (action);
254 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
255 gtk_action_unblock_activate (action);
261 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
264 GtkToggleButton *button;
266 parent_activatable_iface->sync_action_properties (activatable, action);
268 if (!GTK_IS_TOGGLE_ACTION (action))
271 button = GTK_TOGGLE_BUTTON (activatable);
273 gtk_action_block_activate (action);
274 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
275 gtk_action_unblock_activate (action);
279 * gtk_toggle_button_new:
281 * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().
283 * Returns: a new toggle button.
286 gtk_toggle_button_new (void)
288 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
292 * gtk_toggle_button_new_with_label:
293 * @label: a string containing the message to be placed in the toggle button.
295 * Creates a new toggle button with a text label.
297 * Returns: a new toggle button.
300 gtk_toggle_button_new_with_label (const gchar *label)
302 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
306 * gtk_toggle_button_new_with_mnemonic:
307 * @label: the text of the button, with an underscore in front of the
309 * @returns: a new #GtkToggleButton
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.
316 gtk_toggle_button_new_with_mnemonic (const gchar *label)
318 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
320 "use-underline", TRUE,
325 gtk_toggle_button_set_property (GObject *object,
332 tb = GTK_TOGGLE_BUTTON (object);
337 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
339 case PROP_INCONSISTENT:
340 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
342 case PROP_DRAW_INDICATOR:
343 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
352 gtk_toggle_button_get_property (GObject *object,
357 GtkToggleButton *tb = GTK_TOGGLE_BUTTON (object);
358 GtkToggleButtonPrivate *priv = tb->priv;
363 g_value_set_boolean (value, priv->active);
365 case PROP_INCONSISTENT:
366 g_value_set_boolean (value, priv->inconsistent);
368 case PROP_DRAW_INDICATOR:
369 g_value_set_boolean (value, priv->draw_indicator);
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 * gtk_toggle_button_set_mode:
379 * @toggle_button: a #GtkToggleButton
380 * @draw_indicator: if %TRUE, draw the button as a separate indicator
381 * and label; if %FALSE, draw the button like a normal button
383 * Sets whether the button is displayed as a separate indicator and label.
384 * You can call this function on a checkbutton or a radiobutton with
385 * @draw_indicator = %FALSE to make the button look like a normal button
387 * This function only affects instances of classes like #GtkCheckButton
388 * and #GtkRadioButton that derive from #GtkToggleButton,
389 * not instances of #GtkToggleButton itself.
392 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
393 gboolean draw_indicator)
395 GtkToggleButtonPrivate *priv;
397 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
399 priv = toggle_button->priv;
401 draw_indicator = draw_indicator ? TRUE : FALSE;
403 if (priv->draw_indicator != draw_indicator)
405 GtkStyleContext *context;
407 priv->draw_indicator = draw_indicator;
408 GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
410 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
411 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
413 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
415 /* Make toggle buttons conditionally have the "button"
416 * class depending on draw_indicator.
418 context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button));
421 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
423 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
428 * gtk_toggle_button_get_mode:
429 * @toggle_button: a #GtkToggleButton
431 * Retrieves whether the button is displayed as a separate indicator
432 * and label. See gtk_toggle_button_set_mode().
434 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
438 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
440 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
442 return toggle_button->priv->draw_indicator;
446 * gtk_toggle_button_set_active:
447 * @toggle_button: a #GtkToggleButton.
448 * @is_active: %TRUE or %FALSE.
450 * Sets the status of the toggle button. Set to %TRUE if you want the
451 * GtkToggleButton to be 'pressed in', and %FALSE to raise it.
452 * This action causes the toggled signal to be emitted.
455 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
458 GtkToggleButtonPrivate *priv;
460 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
462 priv = toggle_button->priv;
464 is_active = is_active != FALSE;
466 if (priv->active != is_active)
467 gtk_button_clicked (GTK_BUTTON (toggle_button));
471 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
474 toggle_button->priv->active = is_active;
478 * gtk_toggle_button_get_active:
479 * @toggle_button: a #GtkToggleButton.
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.
484 * Returns: a #gboolean value.
487 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
489 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
491 return toggle_button->priv->active;
495 * gtk_toggle_button_toggled:
496 * @toggle_button: a #GtkToggleButton.
498 * Emits the #GtkToggleButton::toggled signal on the
499 * #GtkToggleButton. There is no good reason for an
500 * application ever to call this function.
503 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
505 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
507 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
511 * gtk_toggle_button_set_inconsistent:
512 * @toggle_button: a #GtkToggleButton
513 * @setting: %TRUE if state is inconsistent
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.
526 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
529 GtkToggleButtonPrivate *priv;
531 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
533 priv = toggle_button->priv;
535 setting = setting != FALSE;
537 if (setting != priv->inconsistent)
539 priv->inconsistent = setting;
541 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
542 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
544 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
549 * gtk_toggle_button_get_inconsistent:
550 * @toggle_button: a #GtkToggleButton
552 * Gets the value set by gtk_toggle_button_set_inconsistent().
554 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
557 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
559 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
561 return toggle_button->priv->inconsistent;
565 gtk_toggle_button_draw (GtkWidget *widget,
568 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
569 GtkToggleButtonPrivate *priv = toggle_button->priv;
570 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
571 GtkButton *button = GTK_BUTTON (widget);
574 state = gtk_widget_get_state_flags (widget);
576 if (priv->inconsistent)
577 state |= GTK_STATE_FLAG_INCONSISTENT;
578 else if (button->priv->depressed)
579 state |= GTK_STATE_FLAG_ACTIVE;
581 _gtk_button_paint (button, cr,
582 gtk_widget_get_allocated_width (widget),
583 gtk_widget_get_allocated_height (widget),
587 gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
593 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
594 gboolean group_cycling)
597 * We override the standard implementation in
598 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
599 * if there is no mnemonic conflict.
601 if (gtk_widget_get_can_focus (widget))
602 gtk_widget_grab_focus (widget);
605 gtk_widget_activate (widget);
611 gtk_toggle_button_pressed (GtkButton *button)
613 button->priv->button_down = TRUE;
615 gtk_toggle_button_update_state (button);
616 gtk_widget_queue_draw (GTK_WIDGET (button));
620 gtk_toggle_button_released (GtkButton *button)
622 if (button->priv->button_down)
624 button->priv->button_down = FALSE;
626 if (button->priv->in_button)
627 gtk_button_clicked (button);
629 gtk_toggle_button_update_state (button);
630 gtk_widget_queue_draw (GTK_WIDGET (button));
635 gtk_toggle_button_clicked (GtkButton *button)
637 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
638 GtkToggleButtonPrivate *priv = toggle_button->priv;
640 priv->active = !priv->active;
642 gtk_toggle_button_toggled (toggle_button);
644 gtk_toggle_button_update_state (button);
646 g_object_notify (G_OBJECT (toggle_button), "active");
648 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
649 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
653 gtk_toggle_button_update_state (GtkButton *button)
655 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
656 GtkToggleButtonPrivate *priv = toggle_button->priv;
657 gboolean depressed, touchscreen;
658 GtkStateFlags new_state = 0;
660 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
661 "gtk-touchscreen-mode", &touchscreen,
664 new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
665 ~(GTK_STATE_FLAG_INCONSISTENT |
666 GTK_STATE_FLAG_PRELIGHT |
667 GTK_STATE_FLAG_ACTIVE);
669 if (priv->inconsistent)
670 new_state |= GTK_STATE_FLAG_INCONSISTENT;
672 if (priv->inconsistent)
674 else if (button->priv->in_button && button->priv->button_down)
677 depressed = priv->active;
679 if (!touchscreen && button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
680 new_state |= GTK_STATE_FLAG_PRELIGHT;
683 new_state |= GTK_STATE_FLAG_ACTIVE;
685 _gtk_button_set_depressed (button, depressed);
686 gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE);