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 void gtk_toggle_button_class_init (GtkToggleButtonClass *klass);
54 static void gtk_toggle_button_init (GtkToggleButton *toggle_button);
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);
72 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
73 static GtkContainerClass *parent_class = NULL;
76 gtk_toggle_button_get_type (void)
78 static GType toggle_button_type = 0;
80 if (!toggle_button_type)
82 static const GTypeInfo toggle_button_info =
84 sizeof (GtkToggleButtonClass),
86 NULL, /* base_finalize */
87 (GClassInitFunc) gtk_toggle_button_class_init,
88 NULL, /* class_finalize */
89 NULL, /* class_data */
90 sizeof (GtkToggleButton),
92 (GInstanceInitFunc) gtk_toggle_button_init,
96 g_type_register_static (GTK_TYPE_BUTTON, "GtkToggleButton",
97 &toggle_button_info, 0);
100 return toggle_button_type;
104 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
106 GObjectClass *gobject_class;
107 GtkWidgetClass *widget_class;
108 GtkContainerClass *container_class;
109 GtkButtonClass *button_class;
111 gobject_class = G_OBJECT_CLASS (class);
112 widget_class = (GtkWidgetClass*) class;
113 container_class = (GtkContainerClass*) class;
114 button_class = (GtkButtonClass*) class;
116 parent_class = g_type_class_peek_parent (class);
118 gobject_class->set_property = gtk_toggle_button_set_property;
119 gobject_class->get_property = gtk_toggle_button_get_property;
121 widget_class->expose_event = gtk_toggle_button_expose;
122 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
124 button_class->pressed = gtk_toggle_button_pressed;
125 button_class->released = gtk_toggle_button_released;
126 button_class->clicked = gtk_toggle_button_clicked;
127 button_class->enter = gtk_toggle_button_update_state;
128 button_class->leave = gtk_toggle_button_update_state;
130 class->toggled = NULL;
132 g_object_class_install_property (gobject_class,
134 g_param_spec_boolean ("active",
136 P_("If the toggle button should be pressed in or not"),
138 GTK_PARAM_READWRITE));
140 g_object_class_install_property (gobject_class,
142 g_param_spec_boolean ("inconsistent",
144 P_("If the toggle button is in an \"in between\" state"),
146 GTK_PARAM_READWRITE));
148 g_object_class_install_property (gobject_class,
150 g_param_spec_boolean ("draw-indicator",
151 P_("Draw Indicator"),
152 P_("If the toggle part of the button is displayed"),
154 GTK_PARAM_READWRITE));
156 toggle_button_signals[TOGGLED] =
157 g_signal_new ("toggled",
158 G_OBJECT_CLASS_TYPE (gobject_class),
160 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
162 _gtk_marshal_VOID__VOID,
167 gtk_toggle_button_init (GtkToggleButton *toggle_button)
169 toggle_button->active = FALSE;
170 toggle_button->draw_indicator = FALSE;
171 GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
176 gtk_toggle_button_new (void)
178 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
182 gtk_toggle_button_new_with_label (const gchar *label)
184 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
188 * gtk_toggle_button_new_with_mnemonic:
189 * @label: the text of the button, with an underscore in front of the
191 * @returns: a new #GtkToggleButton
193 * Creates a new #GtkToggleButton containing a label. The label
194 * will be created using gtk_label_new_with_mnemonic(), so underscores
195 * in @label indicate the mnemonic for the button.
198 gtk_toggle_button_new_with_mnemonic (const gchar *label)
200 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
204 gtk_toggle_button_set_property (GObject *object,
211 tb = GTK_TOGGLE_BUTTON (object);
216 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
218 case PROP_INCONSISTENT:
219 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
221 case PROP_DRAW_INDICATOR:
222 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
230 gtk_toggle_button_get_property (GObject *object,
237 tb = GTK_TOGGLE_BUTTON (object);
242 g_value_set_boolean (value, tb->active);
244 case PROP_INCONSISTENT:
245 g_value_set_boolean (value, tb->inconsistent);
247 case PROP_DRAW_INDICATOR:
248 g_value_set_boolean (value, tb->draw_indicator);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 * gtk_toggle_button_set_mode:
258 * @toggle_button: a #GtkToggleButton
259 * @draw_indicator: if %TRUE, draw the button as a separate indicator
260 * and label; if %FALSE, draw the button like a normal button
262 * Sets whether the button is displayed as a separate indicator and label.
263 * You can call this function on a checkbutton or a radiobutton with
264 * @draw_indicator = %FALSE to make the button look like a normal button
266 * This function only affects instances of classes like #GtkCheckButton
267 * and #GtkRadioButton that derive from #GtkToggleButton,
268 * not instances of #GtkToggleButton itself.
271 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
272 gboolean draw_indicator)
276 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
278 widget = GTK_WIDGET (toggle_button);
280 draw_indicator = draw_indicator ? TRUE : FALSE;
282 if (toggle_button->draw_indicator != draw_indicator)
284 toggle_button->draw_indicator = draw_indicator;
285 GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
287 if (GTK_WIDGET_VISIBLE (toggle_button))
288 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
290 g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
295 * gtk_toggle_button_get_mode:
296 * @toggle_button: a #GtkToggleButton
298 * Retrieves whether the button is displayed as a separate indicator
299 * and label. See gtk_toggle_button_set_mode().
301 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
305 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
307 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
309 return toggle_button->draw_indicator;
313 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
316 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
318 is_active = is_active != FALSE;
320 if (toggle_button->active != is_active)
321 gtk_button_clicked (GTK_BUTTON (toggle_button));
326 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
328 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
330 return (toggle_button->active) ? TRUE : FALSE;
335 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
337 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
339 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
343 * gtk_toggle_button_set_inconsistent:
344 * @toggle_button: a #GtkToggleButton
345 * @setting: %TRUE if state is inconsistent
347 * If the user has selected a range of elements (such as some text or
348 * spreadsheet cells) that are affected by a toggle button, and the
349 * current values in that range are inconsistent, you may want to
350 * display the toggle in an "in between" state. This function turns on
351 * "in between" display. Normally you would turn off the inconsistent
352 * state again if the user toggles the toggle button. This has to be
353 * done manually, gtk_toggle_button_set_inconsistent() only affects
354 * visual appearance, it doesn't affect the semantics of the button.
358 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
361 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
363 setting = setting != FALSE;
365 if (setting != toggle_button->inconsistent)
367 toggle_button->inconsistent = setting;
369 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
370 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
372 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
377 * gtk_toggle_button_get_inconsistent:
378 * @toggle_button: a #GtkToggleButton
380 * Gets the value set by gtk_toggle_button_set_inconsistent().
382 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
385 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
387 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
389 return toggle_button->inconsistent;
393 gtk_toggle_button_expose (GtkWidget *widget,
394 GdkEventExpose *event)
396 if (GTK_WIDGET_DRAWABLE (widget))
398 GtkWidget *child = GTK_BIN (widget)->child;
399 GtkButton *button = GTK_BUTTON (widget);
400 GtkStateType state_type;
401 GtkShadowType shadow_type;
403 state_type = GTK_WIDGET_STATE (widget);
405 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
407 if (state_type == GTK_STATE_ACTIVE)
408 state_type = GTK_STATE_NORMAL;
409 shadow_type = GTK_SHADOW_ETCHED_IN;
412 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
414 _gtk_button_paint (button, &event->area, state_type, shadow_type,
415 "togglebutton", "togglebuttondefault");
418 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
425 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
426 gboolean group_cycling)
429 * We override the standard implementation in
430 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
431 * if there is no mnemonic conflict.
433 if (GTK_WIDGET_CAN_FOCUS (widget))
434 gtk_widget_grab_focus (widget);
437 gtk_widget_activate (widget);
443 gtk_toggle_button_pressed (GtkButton *button)
445 button->button_down = TRUE;
447 gtk_toggle_button_update_state (button);
448 gtk_widget_queue_draw (GTK_WIDGET (button));
452 gtk_toggle_button_released (GtkButton *button)
454 if (button->button_down)
456 button->button_down = FALSE;
458 if (button->in_button)
459 gtk_button_clicked (button);
461 gtk_toggle_button_update_state (button);
462 gtk_widget_queue_draw (GTK_WIDGET (button));
467 gtk_toggle_button_clicked (GtkButton *button)
469 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
470 toggle_button->active = !toggle_button->active;
472 gtk_toggle_button_toggled (toggle_button);
474 gtk_toggle_button_update_state (button);
476 g_object_notify (G_OBJECT (toggle_button), "active");
480 gtk_toggle_button_update_state (GtkButton *button)
482 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
484 GtkStateType new_state;
486 if (toggle_button->inconsistent)
488 else if (button->in_button && button->button_down)
491 depressed = toggle_button->active;
493 if (button->in_button && (!button->button_down || toggle_button->draw_indicator))
494 new_state = GTK_STATE_PRELIGHT;
496 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
498 _gtk_button_set_depressed (button, depressed);
499 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
502 #define __GTK_TOGGLE_BUTTON_C__
503 #include "gtkaliasdef.c"