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 "gtkprivate.h"
36 #define DEFAULT_LEFT_POS 4
37 #define DEFAULT_TOP_POS 4
38 #define DEFAULT_SPACING 7
53 static gint gtk_toggle_button_expose (GtkWidget *widget,
54 GdkEventExpose *event);
55 static gboolean gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
56 gboolean group_cycling);
57 static void gtk_toggle_button_pressed (GtkButton *button);
58 static void gtk_toggle_button_released (GtkButton *button);
59 static void gtk_toggle_button_clicked (GtkButton *button);
60 static void gtk_toggle_button_set_property (GObject *object,
64 static void gtk_toggle_button_get_property (GObject *object,
68 static void gtk_toggle_button_update_state (GtkButton *button);
70 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
72 G_DEFINE_TYPE (GtkToggleButton, gtk_toggle_button, GTK_TYPE_BUTTON)
75 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
77 GObjectClass *gobject_class;
78 GtkWidgetClass *widget_class;
79 GtkButtonClass *button_class;
81 gobject_class = G_OBJECT_CLASS (class);
82 widget_class = (GtkWidgetClass*) class;
83 button_class = (GtkButtonClass*) class;
85 gobject_class->set_property = gtk_toggle_button_set_property;
86 gobject_class->get_property = gtk_toggle_button_get_property;
88 widget_class->expose_event = gtk_toggle_button_expose;
89 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
91 button_class->pressed = gtk_toggle_button_pressed;
92 button_class->released = gtk_toggle_button_released;
93 button_class->clicked = gtk_toggle_button_clicked;
94 button_class->enter = gtk_toggle_button_update_state;
95 button_class->leave = gtk_toggle_button_update_state;
97 class->toggled = NULL;
99 g_object_class_install_property (gobject_class,
101 g_param_spec_boolean ("active",
103 P_("If the toggle button should be pressed in or not"),
105 GTK_PARAM_READWRITE));
107 g_object_class_install_property (gobject_class,
109 g_param_spec_boolean ("inconsistent",
111 P_("If the toggle button is in an \"in between\" state"),
113 GTK_PARAM_READWRITE));
115 g_object_class_install_property (gobject_class,
117 g_param_spec_boolean ("draw-indicator",
118 P_("Draw Indicator"),
119 P_("If the toggle part of the button is displayed"),
121 GTK_PARAM_READWRITE));
123 toggle_button_signals[TOGGLED] =
124 g_signal_new (I_("toggled"),
125 G_OBJECT_CLASS_TYPE (gobject_class),
127 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
129 _gtk_marshal_VOID__VOID,
134 gtk_toggle_button_init (GtkToggleButton *toggle_button)
136 toggle_button->active = FALSE;
137 toggle_button->draw_indicator = FALSE;
138 GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
143 gtk_toggle_button_new (void)
145 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
149 gtk_toggle_button_new_with_label (const gchar *label)
151 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
155 * gtk_toggle_button_new_with_mnemonic:
156 * @label: the text of the button, with an underscore in front of the
158 * @returns: a new #GtkToggleButton
160 * Creates a new #GtkToggleButton containing a label. The label
161 * will be created using gtk_label_new_with_mnemonic(), so underscores
162 * in @label indicate the mnemonic for the button.
165 gtk_toggle_button_new_with_mnemonic (const gchar *label)
167 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
169 "use-underline", TRUE,
174 gtk_toggle_button_set_property (GObject *object,
181 tb = GTK_TOGGLE_BUTTON (object);
186 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
188 case PROP_INCONSISTENT:
189 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
191 case PROP_DRAW_INDICATOR:
192 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
200 gtk_toggle_button_get_property (GObject *object,
207 tb = GTK_TOGGLE_BUTTON (object);
212 g_value_set_boolean (value, tb->active);
214 case PROP_INCONSISTENT:
215 g_value_set_boolean (value, tb->inconsistent);
217 case PROP_DRAW_INDICATOR:
218 g_value_set_boolean (value, tb->draw_indicator);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 * gtk_toggle_button_set_mode:
228 * @toggle_button: a #GtkToggleButton
229 * @draw_indicator: if %TRUE, draw the button as a separate indicator
230 * and label; if %FALSE, draw the button like a normal button
232 * Sets whether the button is displayed as a separate indicator and label.
233 * You can call this function on a checkbutton or a radiobutton with
234 * @draw_indicator = %FALSE to make the button look like a normal button
236 * This function only affects instances of classes like #GtkCheckButton
237 * and #GtkRadioButton that derive from #GtkToggleButton,
238 * not instances of #GtkToggleButton itself.
241 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
242 gboolean draw_indicator)
244 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
246 draw_indicator = draw_indicator ? TRUE : FALSE;
248 if (toggle_button->draw_indicator != draw_indicator)
250 toggle_button->draw_indicator = draw_indicator;
251 GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
253 if (GTK_WIDGET_VISIBLE (toggle_button))
254 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
256 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
261 * gtk_toggle_button_get_mode:
262 * @toggle_button: a #GtkToggleButton
264 * Retrieves whether the button is displayed as a separate indicator
265 * and label. See gtk_toggle_button_set_mode().
267 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
271 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
273 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
275 return toggle_button->draw_indicator;
279 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
282 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
284 is_active = is_active != FALSE;
286 if (toggle_button->active != is_active)
287 gtk_button_clicked (GTK_BUTTON (toggle_button));
292 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
294 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
296 return (toggle_button->active) ? TRUE : FALSE;
301 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
303 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
305 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
309 * gtk_toggle_button_set_inconsistent:
310 * @toggle_button: a #GtkToggleButton
311 * @setting: %TRUE if state is inconsistent
313 * If the user has selected a range of elements (such as some text or
314 * spreadsheet cells) that are affected by a toggle button, and the
315 * current values in that range are inconsistent, you may want to
316 * display the toggle in an "in between" state. This function turns on
317 * "in between" display. Normally you would turn off the inconsistent
318 * state again if the user toggles the toggle button. This has to be
319 * done manually, gtk_toggle_button_set_inconsistent() only affects
320 * visual appearance, it doesn't affect the semantics of the button.
324 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
327 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
329 setting = setting != FALSE;
331 if (setting != toggle_button->inconsistent)
333 toggle_button->inconsistent = setting;
335 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
336 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
338 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
343 * gtk_toggle_button_get_inconsistent:
344 * @toggle_button: a #GtkToggleButton
346 * Gets the value set by gtk_toggle_button_set_inconsistent().
348 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
351 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
353 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
355 return toggle_button->inconsistent;
359 gtk_toggle_button_expose (GtkWidget *widget,
360 GdkEventExpose *event)
362 if (GTK_WIDGET_DRAWABLE (widget))
364 GtkWidget *child = GTK_BIN (widget)->child;
365 GtkButton *button = GTK_BUTTON (widget);
366 GtkStateType state_type;
367 GtkShadowType shadow_type;
369 state_type = GTK_WIDGET_STATE (widget);
371 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
373 if (state_type == GTK_STATE_ACTIVE)
374 state_type = GTK_STATE_NORMAL;
375 shadow_type = GTK_SHADOW_ETCHED_IN;
378 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
380 _gtk_button_paint (button, &event->area, state_type, shadow_type,
381 "togglebutton", "togglebuttondefault");
384 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
391 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
392 gboolean group_cycling)
395 * We override the standard implementation in
396 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
397 * if there is no mnemonic conflict.
399 if (GTK_WIDGET_CAN_FOCUS (widget))
400 gtk_widget_grab_focus (widget);
403 gtk_widget_activate (widget);
409 gtk_toggle_button_pressed (GtkButton *button)
411 button->button_down = TRUE;
413 gtk_toggle_button_update_state (button);
414 gtk_widget_queue_draw (GTK_WIDGET (button));
418 gtk_toggle_button_released (GtkButton *button)
420 if (button->button_down)
422 button->button_down = FALSE;
424 if (button->in_button)
425 gtk_button_clicked (button);
427 gtk_toggle_button_update_state (button);
428 gtk_widget_queue_draw (GTK_WIDGET (button));
433 gtk_toggle_button_clicked (GtkButton *button)
435 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
436 toggle_button->active = !toggle_button->active;
438 gtk_toggle_button_toggled (toggle_button);
440 gtk_toggle_button_update_state (button);
442 g_object_notify (G_OBJECT (toggle_button), "active");
446 gtk_toggle_button_update_state (GtkButton *button)
448 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
449 gboolean depressed, touchscreen;
450 GtkStateType new_state;
452 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
453 "gtk-touchscreen-mode", &touchscreen,
456 if (toggle_button->inconsistent)
458 else if (button->in_button && button->button_down)
461 depressed = toggle_button->active;
463 if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
464 new_state = GTK_STATE_PRELIGHT;
466 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
468 _gtk_button_set_depressed (button, depressed);
469 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
472 #define __GTK_TOGGLE_BUTTON_C__
473 #include "gtkaliasdef.c"