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"
41 #define DEFAULT_LEFT_POS 4
42 #define DEFAULT_TOP_POS 4
43 #define DEFAULT_SPACING 7
45 struct _GtkToggleButtonPrivate
48 guint draw_indicator : 1;
49 guint inconsistent : 1;
65 static gint gtk_toggle_button_draw (GtkWidget *widget,
67 static gboolean gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
68 gboolean group_cycling);
69 static void gtk_toggle_button_pressed (GtkButton *button);
70 static void gtk_toggle_button_released (GtkButton *button);
71 static void gtk_toggle_button_clicked (GtkButton *button);
72 static void gtk_toggle_button_set_property (GObject *object,
76 static void gtk_toggle_button_get_property (GObject *object,
80 static void gtk_toggle_button_update_state (GtkButton *button);
83 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
84 static void gtk_toggle_button_update (GtkActivatable *activatable,
86 const gchar *property_name);
87 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
90 static GtkActivatableIface *parent_activatable_iface;
91 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
93 G_DEFINE_TYPE_WITH_CODE (GtkToggleButton, gtk_toggle_button, GTK_TYPE_BUTTON,
94 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
95 gtk_toggle_button_activatable_interface_init))
98 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
100 GObjectClass *gobject_class;
101 GtkWidgetClass *widget_class;
102 GtkButtonClass *button_class;
104 gobject_class = G_OBJECT_CLASS (class);
105 widget_class = (GtkWidgetClass*) class;
106 button_class = (GtkButtonClass*) class;
108 gobject_class->set_property = gtk_toggle_button_set_property;
109 gobject_class->get_property = gtk_toggle_button_get_property;
111 widget_class->draw = gtk_toggle_button_draw;
112 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
114 button_class->pressed = gtk_toggle_button_pressed;
115 button_class->released = gtk_toggle_button_released;
116 button_class->clicked = gtk_toggle_button_clicked;
117 button_class->enter = gtk_toggle_button_update_state;
118 button_class->leave = gtk_toggle_button_update_state;
120 class->toggled = NULL;
122 g_object_class_install_property (gobject_class,
124 g_param_spec_boolean ("active",
126 P_("If the toggle button should be pressed in"),
128 GTK_PARAM_READWRITE));
130 g_object_class_install_property (gobject_class,
132 g_param_spec_boolean ("inconsistent",
134 P_("If the toggle button is in an \"in between\" state"),
136 GTK_PARAM_READWRITE));
138 g_object_class_install_property (gobject_class,
140 g_param_spec_boolean ("draw-indicator",
141 P_("Draw Indicator"),
142 P_("If the toggle part of the button is displayed"),
144 GTK_PARAM_READWRITE));
146 toggle_button_signals[TOGGLED] =
147 g_signal_new (I_("toggled"),
148 G_OBJECT_CLASS_TYPE (gobject_class),
150 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
152 _gtk_marshal_VOID__VOID,
155 g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate));
159 gtk_toggle_button_init (GtkToggleButton *toggle_button)
161 GtkToggleButtonPrivate *priv;
163 toggle_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (toggle_button,
164 GTK_TYPE_TOGGLE_BUTTON,
165 GtkToggleButtonPrivate);
166 priv = toggle_button->priv;
168 priv->active = FALSE;
169 priv->draw_indicator = FALSE;
170 GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE;
174 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
176 parent_activatable_iface = g_type_interface_peek_parent (iface);
177 iface->update = gtk_toggle_button_update;
178 iface->sync_action_properties = gtk_toggle_button_sync_action_properties;
182 gtk_toggle_button_update (GtkActivatable *activatable,
184 const gchar *property_name)
186 GtkToggleButton *button;
188 parent_activatable_iface->update (activatable, action, property_name);
190 button = GTK_TOGGLE_BUTTON (activatable);
192 if (strcmp (property_name, "active") == 0)
194 gtk_action_block_activate (action);
195 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
196 gtk_action_unblock_activate (action);
202 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
205 GtkToggleButton *button;
207 parent_activatable_iface->sync_action_properties (activatable, action);
209 if (!GTK_IS_TOGGLE_ACTION (action))
212 button = GTK_TOGGLE_BUTTON (activatable);
214 gtk_action_block_activate (action);
215 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
216 gtk_action_unblock_activate (action);
221 gtk_toggle_button_new (void)
223 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
227 gtk_toggle_button_new_with_label (const gchar *label)
229 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
233 * gtk_toggle_button_new_with_mnemonic:
234 * @label: the text of the button, with an underscore in front of the
236 * @returns: a new #GtkToggleButton
238 * Creates a new #GtkToggleButton containing a label. The label
239 * will be created using gtk_label_new_with_mnemonic(), so underscores
240 * in @label indicate the mnemonic for the button.
243 gtk_toggle_button_new_with_mnemonic (const gchar *label)
245 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
247 "use-underline", TRUE,
252 gtk_toggle_button_set_property (GObject *object,
259 tb = GTK_TOGGLE_BUTTON (object);
264 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
266 case PROP_INCONSISTENT:
267 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
269 case PROP_DRAW_INDICATOR:
270 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gtk_toggle_button_get_property (GObject *object,
284 GtkToggleButton *tb = GTK_TOGGLE_BUTTON (object);
285 GtkToggleButtonPrivate *priv = tb->priv;
290 g_value_set_boolean (value, priv->active);
292 case PROP_INCONSISTENT:
293 g_value_set_boolean (value, priv->inconsistent);
295 case PROP_DRAW_INDICATOR:
296 g_value_set_boolean (value, priv->draw_indicator);
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 * gtk_toggle_button_set_mode:
306 * @toggle_button: a #GtkToggleButton
307 * @draw_indicator: if %TRUE, draw the button as a separate indicator
308 * and label; if %FALSE, draw the button like a normal button
310 * Sets whether the button is displayed as a separate indicator and label.
311 * You can call this function on a checkbutton or a radiobutton with
312 * @draw_indicator = %FALSE to make the button look like a normal button
314 * This function only affects instances of classes like #GtkCheckButton
315 * and #GtkRadioButton that derive from #GtkToggleButton,
316 * not instances of #GtkToggleButton itself.
319 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
320 gboolean draw_indicator)
322 GtkToggleButtonPrivate *priv;
324 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
326 priv = toggle_button->priv;
328 draw_indicator = draw_indicator ? TRUE : FALSE;
330 if (priv->draw_indicator != draw_indicator)
332 priv->draw_indicator = draw_indicator;
333 GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
335 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
336 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
338 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
343 * gtk_toggle_button_get_mode:
344 * @toggle_button: a #GtkToggleButton
346 * Retrieves whether the button is displayed as a separate indicator
347 * and label. See gtk_toggle_button_set_mode().
349 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
353 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
355 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
357 return toggle_button->priv->draw_indicator;
361 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
364 GtkToggleButtonPrivate *priv;
366 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
368 priv = toggle_button->priv;
370 is_active = is_active != FALSE;
372 if (priv->active != is_active)
373 gtk_button_clicked (GTK_BUTTON (toggle_button));
377 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
380 toggle_button->priv->active = is_active;
384 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
386 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
388 return toggle_button->priv->active;
393 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
395 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
397 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
401 * gtk_toggle_button_set_inconsistent:
402 * @toggle_button: a #GtkToggleButton
403 * @setting: %TRUE if state is inconsistent
405 * If the user has selected a range of elements (such as some text or
406 * spreadsheet cells) that are affected by a toggle button, and the
407 * current values in that range are inconsistent, you may want to
408 * display the toggle in an "in between" state. This function turns on
409 * "in between" display. Normally you would turn off the inconsistent
410 * state again if the user toggles the toggle button. This has to be
411 * done manually, gtk_toggle_button_set_inconsistent() only affects
412 * visual appearance, it doesn't affect the semantics of the button.
416 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
419 GtkToggleButtonPrivate *priv;
421 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
423 priv = toggle_button->priv;
425 setting = setting != FALSE;
427 if (setting != priv->inconsistent)
429 priv->inconsistent = setting;
431 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
432 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
434 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
439 * gtk_toggle_button_get_inconsistent:
440 * @toggle_button: a #GtkToggleButton
442 * Gets the value set by gtk_toggle_button_set_inconsistent().
444 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
447 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
449 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
451 return toggle_button->priv->inconsistent;
455 gtk_toggle_button_draw (GtkWidget *widget,
458 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
459 GtkToggleButtonPrivate *priv = toggle_button->priv;
460 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
461 GtkButton *button = GTK_BUTTON (widget);
462 GtkStateType state_type;
463 GtkShadowType shadow_type;
465 state_type = gtk_widget_get_state (widget);
467 if (priv->inconsistent)
469 if (state_type == GTK_STATE_ACTIVE)
470 state_type = GTK_STATE_NORMAL;
471 shadow_type = GTK_SHADOW_ETCHED_IN;
474 shadow_type = button->priv->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
476 _gtk_button_paint (button, cr,
477 gtk_widget_get_allocated_width (widget),
478 gtk_widget_get_allocated_height (widget),
479 state_type, shadow_type,
480 "togglebutton", "togglebuttondefault");
483 gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
489 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
490 gboolean group_cycling)
493 * We override the standard implementation in
494 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
495 * if there is no mnemonic conflict.
497 if (gtk_widget_get_can_focus (widget))
498 gtk_widget_grab_focus (widget);
501 gtk_widget_activate (widget);
507 gtk_toggle_button_pressed (GtkButton *button)
509 button->priv->button_down = TRUE;
511 gtk_toggle_button_update_state (button);
512 gtk_widget_queue_draw (GTK_WIDGET (button));
516 gtk_toggle_button_released (GtkButton *button)
518 if (button->priv->button_down)
520 button->priv->button_down = FALSE;
522 if (button->priv->in_button)
523 gtk_button_clicked (button);
525 gtk_toggle_button_update_state (button);
526 gtk_widget_queue_draw (GTK_WIDGET (button));
531 gtk_toggle_button_clicked (GtkButton *button)
533 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
534 GtkToggleButtonPrivate *priv = toggle_button->priv;
536 priv->active = !priv->active;
538 gtk_toggle_button_toggled (toggle_button);
540 gtk_toggle_button_update_state (button);
542 g_object_notify (G_OBJECT (toggle_button), "active");
544 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
545 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
549 gtk_toggle_button_update_state (GtkButton *button)
551 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
552 GtkToggleButtonPrivate *priv = toggle_button->priv;
553 gboolean depressed, touchscreen;
554 GtkStateType new_state;
556 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
557 "gtk-touchscreen-mode", &touchscreen,
560 if (priv->inconsistent)
562 else if (button->priv->in_button && button->priv->button_down)
565 depressed = priv->active;
567 if (!touchscreen && button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
568 new_state = GTK_STATE_PRELIGHT;
570 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
572 _gtk_button_set_depressed (button, depressed);
573 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);