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/.
30 #include "gtkmarshalers.h"
31 #include "gtktogglebutton.h"
32 #include "gtktoggleaction.h"
33 #include "gtkactivatable.h"
34 #include "gtkprivate.h"
38 #define DEFAULT_LEFT_POS 4
39 #define DEFAULT_TOP_POS 4
40 #define DEFAULT_SPACING 7
55 static gint gtk_toggle_button_expose (GtkWidget *widget,
56 GdkEventExpose *event);
57 static gboolean gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
58 gboolean group_cycling);
59 static void gtk_toggle_button_pressed (GtkButton *button);
60 static void gtk_toggle_button_released (GtkButton *button);
61 static void gtk_toggle_button_clicked (GtkButton *button);
62 static void gtk_toggle_button_set_property (GObject *object,
66 static void gtk_toggle_button_get_property (GObject *object,
70 static void gtk_toggle_button_update_state (GtkButton *button);
73 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
74 static void gtk_toggle_button_update (GtkActivatable *activatable,
76 const gchar *property_name);
77 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
80 static GtkActivatableIface *parent_activatable_iface;
81 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
83 G_DEFINE_TYPE_WITH_CODE (GtkToggleButton, gtk_toggle_button, GTK_TYPE_BUTTON,
84 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
85 gtk_toggle_button_activatable_interface_init))
88 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
90 GObjectClass *gobject_class;
91 GtkWidgetClass *widget_class;
92 GtkButtonClass *button_class;
94 gobject_class = G_OBJECT_CLASS (class);
95 widget_class = (GtkWidgetClass*) class;
96 button_class = (GtkButtonClass*) class;
98 gobject_class->set_property = gtk_toggle_button_set_property;
99 gobject_class->get_property = gtk_toggle_button_get_property;
101 widget_class->expose_event = gtk_toggle_button_expose;
102 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
104 button_class->pressed = gtk_toggle_button_pressed;
105 button_class->released = gtk_toggle_button_released;
106 button_class->clicked = gtk_toggle_button_clicked;
107 button_class->enter = gtk_toggle_button_update_state;
108 button_class->leave = gtk_toggle_button_update_state;
110 class->toggled = NULL;
112 g_object_class_install_property (gobject_class,
114 g_param_spec_boolean ("active",
116 P_("If the toggle button should be pressed in or not"),
118 GTK_PARAM_READWRITE));
120 g_object_class_install_property (gobject_class,
122 g_param_spec_boolean ("inconsistent",
124 P_("If the toggle button is in an \"in between\" state"),
126 GTK_PARAM_READWRITE));
128 g_object_class_install_property (gobject_class,
130 g_param_spec_boolean ("draw-indicator",
131 P_("Draw Indicator"),
132 P_("If the toggle part of the button is displayed"),
134 GTK_PARAM_READWRITE));
136 toggle_button_signals[TOGGLED] =
137 g_signal_new (I_("toggled"),
138 G_OBJECT_CLASS_TYPE (gobject_class),
140 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
142 _gtk_marshal_VOID__VOID,
147 gtk_toggle_button_init (GtkToggleButton *toggle_button)
149 toggle_button->active = FALSE;
150 toggle_button->draw_indicator = FALSE;
151 GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
155 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
157 parent_activatable_iface = g_type_interface_peek_parent (iface);
158 iface->update = gtk_toggle_button_update;
159 iface->sync_action_properties = gtk_toggle_button_sync_action_properties;
163 gtk_toggle_button_update (GtkActivatable *activatable,
165 const gchar *property_name)
167 GtkToggleButton *button;
169 parent_activatable_iface->update (activatable, action, property_name);
171 button = GTK_TOGGLE_BUTTON (activatable);
173 if (strcmp (property_name, "active") == 0)
175 gtk_action_block_activate (action);
176 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
177 gtk_action_unblock_activate (action);
183 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
186 GtkToggleButton *button;
188 parent_activatable_iface->sync_action_properties (activatable, action);
190 if (!GTK_IS_TOGGLE_ACTION (action))
193 button = GTK_TOGGLE_BUTTON (activatable);
195 gtk_action_block_activate (action);
196 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
197 gtk_action_unblock_activate (action);
202 gtk_toggle_button_new (void)
204 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
208 gtk_toggle_button_new_with_label (const gchar *label)
210 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
214 * gtk_toggle_button_new_with_mnemonic:
215 * @label: the text of the button, with an underscore in front of the
217 * @returns: a new #GtkToggleButton
219 * Creates a new #GtkToggleButton containing a label. The label
220 * will be created using gtk_label_new_with_mnemonic(), so underscores
221 * in @label indicate the mnemonic for the button.
224 gtk_toggle_button_new_with_mnemonic (const gchar *label)
226 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
228 "use-underline", TRUE,
233 gtk_toggle_button_set_property (GObject *object,
240 tb = GTK_TOGGLE_BUTTON (object);
245 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
247 case PROP_INCONSISTENT:
248 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
250 case PROP_DRAW_INDICATOR:
251 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 gtk_toggle_button_get_property (GObject *object,
267 tb = GTK_TOGGLE_BUTTON (object);
272 g_value_set_boolean (value, tb->active);
274 case PROP_INCONSISTENT:
275 g_value_set_boolean (value, tb->inconsistent);
277 case PROP_DRAW_INDICATOR:
278 g_value_set_boolean (value, tb->draw_indicator);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 * gtk_toggle_button_set_mode:
288 * @toggle_button: a #GtkToggleButton
289 * @draw_indicator: if %TRUE, draw the button as a separate indicator
290 * and label; if %FALSE, draw the button like a normal button
292 * Sets whether the button is displayed as a separate indicator and label.
293 * You can call this function on a checkbutton or a radiobutton with
294 * @draw_indicator = %FALSE to make the button look like a normal button
296 * This function only affects instances of classes like #GtkCheckButton
297 * and #GtkRadioButton that derive from #GtkToggleButton,
298 * not instances of #GtkToggleButton itself.
301 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
302 gboolean draw_indicator)
304 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
306 draw_indicator = draw_indicator ? TRUE : FALSE;
308 if (toggle_button->draw_indicator != draw_indicator)
310 toggle_button->draw_indicator = draw_indicator;
311 GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
313 if (GTK_WIDGET_VISIBLE (toggle_button))
314 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
316 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
321 * gtk_toggle_button_get_mode:
322 * @toggle_button: a #GtkToggleButton
324 * Retrieves whether the button is displayed as a separate indicator
325 * and label. See gtk_toggle_button_set_mode().
327 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
331 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
333 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
335 return toggle_button->draw_indicator;
339 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
342 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
344 is_active = is_active != FALSE;
346 if (toggle_button->active != is_active)
347 gtk_button_clicked (GTK_BUTTON (toggle_button));
352 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
354 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
356 return (toggle_button->active) ? TRUE : FALSE;
361 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
363 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
365 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
369 * gtk_toggle_button_set_inconsistent:
370 * @toggle_button: a #GtkToggleButton
371 * @setting: %TRUE if state is inconsistent
373 * If the user has selected a range of elements (such as some text or
374 * spreadsheet cells) that are affected by a toggle button, and the
375 * current values in that range are inconsistent, you may want to
376 * display the toggle in an "in between" state. This function turns on
377 * "in between" display. Normally you would turn off the inconsistent
378 * state again if the user toggles the toggle button. This has to be
379 * done manually, gtk_toggle_button_set_inconsistent() only affects
380 * visual appearance, it doesn't affect the semantics of the button.
384 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
387 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
389 setting = setting != FALSE;
391 if (setting != toggle_button->inconsistent)
393 toggle_button->inconsistent = setting;
395 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
396 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
398 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
403 * gtk_toggle_button_get_inconsistent:
404 * @toggle_button: a #GtkToggleButton
406 * Gets the value set by gtk_toggle_button_set_inconsistent().
408 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
411 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
413 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
415 return toggle_button->inconsistent;
419 gtk_toggle_button_expose (GtkWidget *widget,
420 GdkEventExpose *event)
422 if (gtk_widget_is_drawable (widget))
424 GtkWidget *child = GTK_BIN (widget)->child;
425 GtkButton *button = GTK_BUTTON (widget);
426 GtkStateType state_type;
427 GtkShadowType shadow_type;
429 state_type = GTK_WIDGET_STATE (widget);
431 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
433 if (state_type == GTK_STATE_ACTIVE)
434 state_type = GTK_STATE_NORMAL;
435 shadow_type = GTK_SHADOW_ETCHED_IN;
438 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
440 _gtk_button_paint (button, &event->area, state_type, shadow_type,
441 "togglebutton", "togglebuttondefault");
444 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
451 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
452 gboolean group_cycling)
455 * We override the standard implementation in
456 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
457 * if there is no mnemonic conflict.
459 if (gtk_widget_get_can_focus (widget))
460 gtk_widget_grab_focus (widget);
463 gtk_widget_activate (widget);
469 gtk_toggle_button_pressed (GtkButton *button)
471 button->button_down = TRUE;
473 gtk_toggle_button_update_state (button);
474 gtk_widget_queue_draw (GTK_WIDGET (button));
478 gtk_toggle_button_released (GtkButton *button)
480 if (button->button_down)
482 button->button_down = FALSE;
484 if (button->in_button)
485 gtk_button_clicked (button);
487 gtk_toggle_button_update_state (button);
488 gtk_widget_queue_draw (GTK_WIDGET (button));
493 gtk_toggle_button_clicked (GtkButton *button)
495 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
496 toggle_button->active = !toggle_button->active;
498 gtk_toggle_button_toggled (toggle_button);
500 gtk_toggle_button_update_state (button);
502 g_object_notify (G_OBJECT (toggle_button), "active");
504 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
505 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
509 gtk_toggle_button_update_state (GtkButton *button)
511 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
512 gboolean depressed, touchscreen;
513 GtkStateType new_state;
515 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
516 "gtk-touchscreen-mode", &touchscreen,
519 if (toggle_button->inconsistent)
521 else if (button->in_button && button->button_down)
524 depressed = toggle_button->active;
526 if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
527 new_state = GTK_STATE_PRELIGHT;
529 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
531 _gtk_button_set_depressed (button, depressed);
532 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
535 #define __GTK_TOGGLE_BUTTON_C__
536 #include "gtkaliasdef.c"