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 "gtkmarshalers.h"
30 #include "gtksignal.h"
31 #include "gtktogglebutton.h"
34 #define DEFAULT_LEFT_POS 4
35 #define DEFAULT_TOP_POS 4
36 #define DEFAULT_SPACING 7
51 static void gtk_toggle_button_class_init (GtkToggleButtonClass *klass);
52 static void gtk_toggle_button_init (GtkToggleButton *toggle_button);
53 static gint gtk_toggle_button_expose (GtkWidget *widget,
54 GdkEventExpose *event);
55 static void gtk_toggle_button_pressed (GtkButton *button);
56 static void gtk_toggle_button_released (GtkButton *button);
57 static void gtk_toggle_button_clicked (GtkButton *button);
58 static void gtk_toggle_button_set_property (GObject *object,
62 static void gtk_toggle_button_get_property (GObject *object,
66 static void gtk_toggle_button_update_state (GtkButton *button);
68 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
69 static GtkContainerClass *parent_class = NULL;
72 gtk_toggle_button_get_type (void)
74 static GtkType toggle_button_type = 0;
76 if (!toggle_button_type)
78 static const GtkTypeInfo toggle_button_info =
81 sizeof (GtkToggleButton),
82 sizeof (GtkToggleButtonClass),
83 (GtkClassInitFunc) gtk_toggle_button_class_init,
84 (GtkObjectInitFunc) gtk_toggle_button_init,
85 /* reserved_1 */ NULL,
86 /* reserved_2 */ NULL,
87 (GtkClassInitFunc) NULL,
90 toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
93 return toggle_button_type;
97 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
99 GtkObjectClass *object_class;
100 GObjectClass *gobject_class;
101 GtkWidgetClass *widget_class;
102 GtkContainerClass *container_class;
103 GtkButtonClass *button_class;
105 object_class = (GtkObjectClass*) class;
106 gobject_class = G_OBJECT_CLASS (class);
107 widget_class = (GtkWidgetClass*) class;
108 container_class = (GtkContainerClass*) class;
109 button_class = (GtkButtonClass*) class;
111 parent_class = gtk_type_class (GTK_TYPE_BUTTON);
114 gobject_class->set_property = gtk_toggle_button_set_property;
115 gobject_class->get_property = gtk_toggle_button_get_property;
117 widget_class->expose_event = gtk_toggle_button_expose;
119 button_class->pressed = gtk_toggle_button_pressed;
120 button_class->released = gtk_toggle_button_released;
121 button_class->clicked = gtk_toggle_button_clicked;
122 button_class->enter = gtk_toggle_button_update_state;
123 button_class->leave = gtk_toggle_button_update_state;
125 class->toggled = NULL;
127 g_object_class_install_property (gobject_class,
129 g_param_spec_boolean ("active",
131 _("If the toggle button should be pressed in or not"),
135 g_object_class_install_property (gobject_class,
137 g_param_spec_boolean ("inconsistent",
139 _("If the toggle button is in an \"in between\" state."),
143 g_object_class_install_property (gobject_class,
145 g_param_spec_boolean ("draw_indicator",
147 _("If the toggle part of the button is displayed"),
151 toggle_button_signals[TOGGLED] =
152 gtk_signal_new ("toggled",
154 GTK_CLASS_TYPE (object_class),
155 GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
156 _gtk_marshal_VOID__VOID,
161 gtk_toggle_button_init (GtkToggleButton *toggle_button)
163 toggle_button->active = FALSE;
164 toggle_button->draw_indicator = FALSE;
165 GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
170 gtk_toggle_button_new (void)
172 return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
176 gtk_toggle_button_new_with_label (const gchar *label)
178 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
182 * gtk_toggle_button_new_with_mnemonic:
183 * @label: the text of the button, with an underscore in front of the
185 * @returns: a new #GtkToggleButton
187 * Creates a new #GtkToggleButton containing a label. The label
188 * will be created using gtk_label_new_with_mnemonic(), so underscores
189 * in @label indicate the mnemonic for the button.
192 gtk_toggle_button_new_with_mnemonic (const gchar *label)
194 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
198 gtk_toggle_button_set_property (GObject *object,
205 tb = GTK_TOGGLE_BUTTON (object);
210 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
212 case PROP_INCONSISTENT:
213 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
215 case PROP_DRAW_INDICATOR:
216 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
224 gtk_toggle_button_get_property (GObject *object,
231 tb = GTK_TOGGLE_BUTTON (object);
236 g_value_set_boolean (value, tb->active);
238 case PROP_INCONSISTENT:
239 g_value_set_boolean (value, tb->inconsistent);
241 case PROP_DRAW_INDICATOR:
242 g_value_set_boolean (value, tb->draw_indicator);
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 gtk_toggle_button_update_depress_on_activate (GtkToggleButton *toggle_button)
253 GtkButton *button = GTK_BUTTON (toggle_button);
255 if (toggle_button->draw_indicator || toggle_button->inconsistent)
256 button->depress_on_activate = FALSE;
258 button->depress_on_activate = !toggle_button->active;
262 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
263 gboolean draw_indicator)
267 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
269 widget = GTK_WIDGET (toggle_button);
271 draw_indicator = draw_indicator ? TRUE : FALSE;
273 if (toggle_button->draw_indicator != draw_indicator)
275 toggle_button->draw_indicator = draw_indicator;
277 gtk_toggle_button_update_depress_on_activate (toggle_button);
279 if (GTK_WIDGET_VISIBLE (toggle_button))
280 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
282 g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
287 * gtk_toggle_button_get_mode:
288 * @toggle_button: a #GtkToggleButton
290 * Retrieves whether the button is displayed as a separate indicator
291 * and label. See gtk_toggle_button_set_mode().
293 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
297 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
299 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
301 return toggle_button->draw_indicator;
305 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
308 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
310 is_active = is_active != FALSE;
312 if (toggle_button->active != is_active)
313 gtk_button_clicked (GTK_BUTTON (toggle_button));
318 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
320 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
322 return (toggle_button->active) ? TRUE : FALSE;
327 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
329 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
331 gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
335 * gtk_toggle_button_set_inconsistent:
336 * @toggle_button: a #GtkToggleButton
337 * @setting: %TRUE if state is inconsistent
339 * If the user has selected a range of elements (such as some text or
340 * spreadsheet cells) that are affected by a toggle button, and the
341 * current values in that range are inconsistent, you may want to
342 * display the toggle in an "in between" state. This function turns on
343 * "in between" display. Normally you would turn off the inconsistent
344 * state again if the user toggles the toggle button. This has to be
345 * done manually, gtk_toggle_button_set_inconsistent() only affects
346 * visual appearance, it doesn't affect the semantics of the button.
350 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
353 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
355 setting = setting != FALSE;
357 if (setting != toggle_button->inconsistent)
359 toggle_button->inconsistent = setting;
361 gtk_toggle_button_update_depress_on_activate (toggle_button);
362 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
363 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
365 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
370 * gtk_toggle_button_get_inconsistent:
371 * @toggle_button: a #GtkToggleButton
373 * Gets the value set by gtk_toggle_button_set_inconsistent().
375 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
378 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
380 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
382 return toggle_button->inconsistent;
386 gtk_toggle_button_expose (GtkWidget *widget,
387 GdkEventExpose *event)
389 if (GTK_WIDGET_DRAWABLE (widget))
391 GtkWidget *child = GTK_BIN (widget)->child;
392 GtkButton *button = GTK_BUTTON (widget);
393 GtkStateType state_type;
394 GtkShadowType shadow_type;
396 state_type = GTK_WIDGET_STATE (widget);
398 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
400 if (state_type == GTK_STATE_ACTIVE)
401 state_type = GTK_STATE_NORMAL;
402 shadow_type = GTK_SHADOW_ETCHED_IN;
405 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
407 _gtk_button_paint (button, &event->area, state_type, shadow_type,
408 "togglebutton", "togglebuttondefault");
411 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
418 gtk_toggle_button_pressed (GtkButton *button)
420 button->button_down = TRUE;
422 gtk_toggle_button_update_state (button);
423 gtk_widget_queue_draw (GTK_WIDGET (button));
427 gtk_toggle_button_released (GtkButton *button)
429 if (button->button_down)
431 button->button_down = FALSE;
433 if (button->in_button)
434 gtk_button_clicked (button);
436 gtk_toggle_button_update_state (button);
437 gtk_widget_queue_draw (GTK_WIDGET (button));
442 gtk_toggle_button_clicked (GtkButton *button)
444 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
445 toggle_button->active = !toggle_button->active;
447 gtk_toggle_button_update_depress_on_activate (toggle_button);
449 gtk_toggle_button_toggled (toggle_button);
451 gtk_toggle_button_update_state (button);
453 g_object_notify (G_OBJECT (toggle_button), "active");
457 gtk_toggle_button_update_state (GtkButton *button)
459 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
461 GtkStateType new_state;
463 if (toggle_button->inconsistent)
465 else if (button->in_button && button->button_down)
466 depressed = !toggle_button->active;
468 depressed = toggle_button->active;
470 if (button->in_button && (!button->button_down || toggle_button->draw_indicator))
471 new_state = GTK_STATE_PRELIGHT;
473 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
475 _gtk_button_set_depressed (button, depressed);
476 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);