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 GtkContainerClass *container_class;
80 GtkButtonClass *button_class;
82 gobject_class = G_OBJECT_CLASS (class);
83 widget_class = (GtkWidgetClass*) class;
84 container_class = (GtkContainerClass*) class;
85 button_class = (GtkButtonClass*) class;
87 gobject_class->set_property = gtk_toggle_button_set_property;
88 gobject_class->get_property = gtk_toggle_button_get_property;
90 widget_class->expose_event = gtk_toggle_button_expose;
91 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
93 button_class->pressed = gtk_toggle_button_pressed;
94 button_class->released = gtk_toggle_button_released;
95 button_class->clicked = gtk_toggle_button_clicked;
96 button_class->enter = gtk_toggle_button_update_state;
97 button_class->leave = gtk_toggle_button_update_state;
99 class->toggled = NULL;
101 g_object_class_install_property (gobject_class,
103 g_param_spec_boolean ("active",
105 P_("If the toggle button should be pressed in or not"),
107 GTK_PARAM_READWRITE));
109 g_object_class_install_property (gobject_class,
111 g_param_spec_boolean ("inconsistent",
113 P_("If the toggle button is in an \"in between\" state"),
115 GTK_PARAM_READWRITE));
117 g_object_class_install_property (gobject_class,
119 g_param_spec_boolean ("draw-indicator",
120 P_("Draw Indicator"),
121 P_("If the toggle part of the button is displayed"),
123 GTK_PARAM_READWRITE));
125 toggle_button_signals[TOGGLED] =
126 g_signal_new (I_("toggled"),
127 G_OBJECT_CLASS_TYPE (gobject_class),
129 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
131 _gtk_marshal_VOID__VOID,
136 gtk_toggle_button_init (GtkToggleButton *toggle_button)
138 toggle_button->active = FALSE;
139 toggle_button->draw_indicator = FALSE;
140 GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
145 gtk_toggle_button_new (void)
147 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
151 gtk_toggle_button_new_with_label (const gchar *label)
153 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
157 * gtk_toggle_button_new_with_mnemonic:
158 * @label: the text of the button, with an underscore in front of the
160 * @returns: a new #GtkToggleButton
162 * Creates a new #GtkToggleButton containing a label. The label
163 * will be created using gtk_label_new_with_mnemonic(), so underscores
164 * in @label indicate the mnemonic for the button.
167 gtk_toggle_button_new_with_mnemonic (const gchar *label)
169 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
171 "use-underline", TRUE,
176 gtk_toggle_button_set_property (GObject *object,
183 tb = GTK_TOGGLE_BUTTON (object);
188 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
190 case PROP_INCONSISTENT:
191 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
193 case PROP_DRAW_INDICATOR:
194 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
202 gtk_toggle_button_get_property (GObject *object,
209 tb = GTK_TOGGLE_BUTTON (object);
214 g_value_set_boolean (value, tb->active);
216 case PROP_INCONSISTENT:
217 g_value_set_boolean (value, tb->inconsistent);
219 case PROP_DRAW_INDICATOR:
220 g_value_set_boolean (value, tb->draw_indicator);
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 * gtk_toggle_button_set_mode:
230 * @toggle_button: a #GtkToggleButton
231 * @draw_indicator: if %TRUE, draw the button as a separate indicator
232 * and label; if %FALSE, draw the button like a normal button
234 * Sets whether the button is displayed as a separate indicator and label.
235 * You can call this function on a checkbutton or a radiobutton with
236 * @draw_indicator = %FALSE to make the button look like a normal button
238 * This function only affects instances of classes like #GtkCheckButton
239 * and #GtkRadioButton that derive from #GtkToggleButton,
240 * not instances of #GtkToggleButton itself.
243 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
244 gboolean draw_indicator)
248 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
250 widget = GTK_WIDGET (toggle_button);
252 draw_indicator = draw_indicator ? TRUE : FALSE;
254 if (toggle_button->draw_indicator != draw_indicator)
256 toggle_button->draw_indicator = draw_indicator;
257 GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
259 if (GTK_WIDGET_VISIBLE (toggle_button))
260 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
262 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
267 * gtk_toggle_button_get_mode:
268 * @toggle_button: a #GtkToggleButton
270 * Retrieves whether the button is displayed as a separate indicator
271 * and label. See gtk_toggle_button_set_mode().
273 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
277 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
279 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
281 return toggle_button->draw_indicator;
285 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
288 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
290 is_active = is_active != FALSE;
292 if (toggle_button->active != is_active)
293 gtk_button_clicked (GTK_BUTTON (toggle_button));
298 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
300 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
302 return (toggle_button->active) ? TRUE : FALSE;
307 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
309 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
311 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
315 * gtk_toggle_button_set_inconsistent:
316 * @toggle_button: a #GtkToggleButton
317 * @setting: %TRUE if state is inconsistent
319 * If the user has selected a range of elements (such as some text or
320 * spreadsheet cells) that are affected by a toggle button, and the
321 * current values in that range are inconsistent, you may want to
322 * display the toggle in an "in between" state. This function turns on
323 * "in between" display. Normally you would turn off the inconsistent
324 * state again if the user toggles the toggle button. This has to be
325 * done manually, gtk_toggle_button_set_inconsistent() only affects
326 * visual appearance, it doesn't affect the semantics of the button.
330 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
333 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
335 setting = setting != FALSE;
337 if (setting != toggle_button->inconsistent)
339 toggle_button->inconsistent = setting;
341 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
342 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
344 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
349 * gtk_toggle_button_get_inconsistent:
350 * @toggle_button: a #GtkToggleButton
352 * Gets the value set by gtk_toggle_button_set_inconsistent().
354 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
357 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
359 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
361 return toggle_button->inconsistent;
365 gtk_toggle_button_expose (GtkWidget *widget,
366 GdkEventExpose *event)
368 if (GTK_WIDGET_DRAWABLE (widget))
370 GtkWidget *child = GTK_BIN (widget)->child;
371 GtkButton *button = GTK_BUTTON (widget);
372 GtkStateType state_type;
373 GtkShadowType shadow_type;
375 state_type = GTK_WIDGET_STATE (widget);
377 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
379 if (state_type == GTK_STATE_ACTIVE)
380 state_type = GTK_STATE_NORMAL;
381 shadow_type = GTK_SHADOW_ETCHED_IN;
384 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
386 _gtk_button_paint (button, &event->area, state_type, shadow_type,
387 "togglebutton", "togglebuttondefault");
390 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
397 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
398 gboolean group_cycling)
401 * We override the standard implementation in
402 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
403 * if there is no mnemonic conflict.
405 if (GTK_WIDGET_CAN_FOCUS (widget))
406 gtk_widget_grab_focus (widget);
409 gtk_widget_activate (widget);
415 gtk_toggle_button_pressed (GtkButton *button)
417 button->button_down = TRUE;
419 gtk_toggle_button_update_state (button);
420 gtk_widget_queue_draw (GTK_WIDGET (button));
424 gtk_toggle_button_released (GtkButton *button)
426 if (button->button_down)
428 button->button_down = FALSE;
430 if (button->in_button)
431 gtk_button_clicked (button);
433 gtk_toggle_button_update_state (button);
434 gtk_widget_queue_draw (GTK_WIDGET (button));
439 gtk_toggle_button_clicked (GtkButton *button)
441 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
442 toggle_button->active = !toggle_button->active;
444 gtk_toggle_button_toggled (toggle_button);
446 gtk_toggle_button_update_state (button);
448 g_object_notify (G_OBJECT (toggle_button), "active");
452 gtk_toggle_button_update_state (GtkButton *button)
454 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
455 gboolean depressed, touchscreen;
456 GtkStateType new_state;
458 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
459 "gtk-touchscreen-mode", &touchscreen,
462 if (toggle_button->inconsistent)
464 else if (button->in_button && button->button_down)
467 depressed = toggle_button->active;
469 if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
470 new_state = GTK_STATE_PRELIGHT;
472 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
474 _gtk_button_set_depressed (button, depressed);
475 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
478 #define __GTK_TOGGLE_BUTTON_C__
479 #include "gtkaliasdef.c"