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 GtkStyleContext *context;
334 priv->draw_indicator = draw_indicator;
335 GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator;
337 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
338 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
340 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
342 /* Make toggle buttons conditionally have the "button"
343 * class depending on draw_indicator.
345 context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button));
348 gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON);
350 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
355 * gtk_toggle_button_get_mode:
356 * @toggle_button: a #GtkToggleButton
358 * Retrieves whether the button is displayed as a separate indicator
359 * and label. See gtk_toggle_button_set_mode().
361 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
365 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
367 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
369 return toggle_button->priv->draw_indicator;
373 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
376 GtkToggleButtonPrivate *priv;
378 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
380 priv = toggle_button->priv;
382 is_active = is_active != FALSE;
384 if (priv->active != is_active)
385 gtk_button_clicked (GTK_BUTTON (toggle_button));
389 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
392 toggle_button->priv->active = is_active;
396 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
398 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
400 return toggle_button->priv->active;
405 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
407 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
409 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
413 * gtk_toggle_button_set_inconsistent:
414 * @toggle_button: a #GtkToggleButton
415 * @setting: %TRUE if state is inconsistent
417 * If the user has selected a range of elements (such as some text or
418 * spreadsheet cells) that are affected by a toggle button, and the
419 * current values in that range are inconsistent, you may want to
420 * display the toggle in an "in between" state. This function turns on
421 * "in between" display. Normally you would turn off the inconsistent
422 * state again if the user toggles the toggle button. This has to be
423 * done manually, gtk_toggle_button_set_inconsistent() only affects
424 * visual appearance, it doesn't affect the semantics of the button.
428 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
431 GtkToggleButtonPrivate *priv;
433 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
435 priv = toggle_button->priv;
437 setting = setting != FALSE;
439 if (setting != priv->inconsistent)
441 priv->inconsistent = setting;
443 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
444 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
446 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
451 * gtk_toggle_button_get_inconsistent:
452 * @toggle_button: a #GtkToggleButton
454 * Gets the value set by gtk_toggle_button_set_inconsistent().
456 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
459 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
461 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
463 return toggle_button->priv->inconsistent;
467 gtk_toggle_button_draw (GtkWidget *widget,
470 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
471 GtkToggleButtonPrivate *priv = toggle_button->priv;
472 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
473 GtkButton *button = GTK_BUTTON (widget);
474 GtkStateType state_type;
475 GtkShadowType shadow_type;
477 state_type = gtk_widget_get_state (widget);
479 if (priv->inconsistent)
481 if (state_type == GTK_STATE_ACTIVE)
482 state_type = GTK_STATE_NORMAL;
483 shadow_type = GTK_SHADOW_ETCHED_IN;
486 shadow_type = button->priv->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
488 _gtk_button_paint (button, cr,
489 gtk_widget_get_allocated_width (widget),
490 gtk_widget_get_allocated_height (widget),
491 state_type, shadow_type,
492 "togglebutton", "togglebuttondefault");
495 gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
501 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
502 gboolean group_cycling)
505 * We override the standard implementation in
506 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
507 * if there is no mnemonic conflict.
509 if (gtk_widget_get_can_focus (widget))
510 gtk_widget_grab_focus (widget);
513 gtk_widget_activate (widget);
519 gtk_toggle_button_pressed (GtkButton *button)
521 button->priv->button_down = TRUE;
523 gtk_toggle_button_update_state (button);
524 gtk_widget_queue_draw (GTK_WIDGET (button));
528 gtk_toggle_button_released (GtkButton *button)
530 if (button->priv->button_down)
532 button->priv->button_down = FALSE;
534 if (button->priv->in_button)
535 gtk_button_clicked (button);
537 gtk_toggle_button_update_state (button);
538 gtk_widget_queue_draw (GTK_WIDGET (button));
543 gtk_toggle_button_clicked (GtkButton *button)
545 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
546 GtkToggleButtonPrivate *priv = toggle_button->priv;
548 priv->active = !priv->active;
550 gtk_toggle_button_toggled (toggle_button);
552 gtk_toggle_button_update_state (button);
554 g_object_notify (G_OBJECT (toggle_button), "active");
556 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
557 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
561 gtk_toggle_button_update_state (GtkButton *button)
563 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
564 GtkToggleButtonPrivate *priv = toggle_button->priv;
565 gboolean depressed, touchscreen;
566 GtkStateFlags new_state = 0;
568 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
569 "gtk-touchscreen-mode", &touchscreen,
572 if (priv->inconsistent)
573 new_state |= GTK_STATE_FLAG_INCONSISTENT;
575 if (priv->inconsistent)
577 else if (button->priv->in_button && button->priv->button_down)
580 depressed = priv->active;
582 if (!touchscreen && button->priv->in_button && (!button->priv->button_down || priv->draw_indicator))
583 new_state |= GTK_STATE_FLAG_PRELIGHT;
586 new_state |= GTK_STATE_FLAG_ACTIVE;
588 _gtk_button_set_depressed (button, depressed);
589 gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE);