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_activatable_update (GtkActivatable *activatable,
76 const gchar *property_name);
77 static void gtk_toggle_button_activatable_reset (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;
156 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
158 parent_activatable_iface = g_type_interface_peek_parent (iface);
159 iface->update = gtk_toggle_button_activatable_update;
160 iface->reset = gtk_toggle_button_activatable_reset;
164 gtk_toggle_button_activatable_update (GtkActivatable *activatable,
166 const gchar *property_name)
168 GtkToggleButton *button;
170 parent_activatable_iface->update (activatable, action, property_name);
172 button = GTK_TOGGLE_BUTTON (activatable);
174 if (strcmp (property_name, "active") == 0)
176 gtk_action_block_activate (action);
177 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
178 gtk_action_unblock_activate (action);
184 gtk_toggle_button_activatable_reset (GtkActivatable *activatable,
187 GtkToggleButton *button;
189 parent_activatable_iface->reset (activatable, action);
194 button = GTK_TOGGLE_BUTTON (activatable);
196 gtk_action_block_activate (action);
197 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
198 gtk_action_unblock_activate (action);
203 gtk_toggle_button_new (void)
205 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
209 gtk_toggle_button_new_with_label (const gchar *label)
211 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
215 * gtk_toggle_button_new_with_mnemonic:
216 * @label: the text of the button, with an underscore in front of the
218 * @returns: a new #GtkToggleButton
220 * Creates a new #GtkToggleButton containing a label. The label
221 * will be created using gtk_label_new_with_mnemonic(), so underscores
222 * in @label indicate the mnemonic for the button.
225 gtk_toggle_button_new_with_mnemonic (const gchar *label)
227 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
229 "use-underline", TRUE,
234 gtk_toggle_button_set_property (GObject *object,
241 tb = GTK_TOGGLE_BUTTON (object);
246 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
248 case PROP_INCONSISTENT:
249 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
251 case PROP_DRAW_INDICATOR:
252 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 gtk_toggle_button_get_property (GObject *object,
268 tb = GTK_TOGGLE_BUTTON (object);
273 g_value_set_boolean (value, tb->active);
275 case PROP_INCONSISTENT:
276 g_value_set_boolean (value, tb->inconsistent);
278 case PROP_DRAW_INDICATOR:
279 g_value_set_boolean (value, tb->draw_indicator);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 * gtk_toggle_button_set_mode:
289 * @toggle_button: a #GtkToggleButton
290 * @draw_indicator: if %TRUE, draw the button as a separate indicator
291 * and label; if %FALSE, draw the button like a normal button
293 * Sets whether the button is displayed as a separate indicator and label.
294 * You can call this function on a checkbutton or a radiobutton with
295 * @draw_indicator = %FALSE to make the button look like a normal button
297 * This function only affects instances of classes like #GtkCheckButton
298 * and #GtkRadioButton that derive from #GtkToggleButton,
299 * not instances of #GtkToggleButton itself.
302 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
303 gboolean draw_indicator)
305 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
307 draw_indicator = draw_indicator ? TRUE : FALSE;
309 if (toggle_button->draw_indicator != draw_indicator)
311 toggle_button->draw_indicator = draw_indicator;
312 GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
314 if (GTK_WIDGET_VISIBLE (toggle_button))
315 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
317 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
322 * gtk_toggle_button_get_mode:
323 * @toggle_button: a #GtkToggleButton
325 * Retrieves whether the button is displayed as a separate indicator
326 * and label. See gtk_toggle_button_set_mode().
328 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
332 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
334 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
336 return toggle_button->draw_indicator;
340 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
343 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
345 is_active = is_active != FALSE;
347 if (toggle_button->active != is_active)
348 gtk_button_clicked (GTK_BUTTON (toggle_button));
353 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
355 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
357 return (toggle_button->active) ? TRUE : FALSE;
362 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
364 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
366 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
370 * gtk_toggle_button_set_inconsistent:
371 * @toggle_button: a #GtkToggleButton
372 * @setting: %TRUE if state is inconsistent
374 * If the user has selected a range of elements (such as some text or
375 * spreadsheet cells) that are affected by a toggle button, and the
376 * current values in that range are inconsistent, you may want to
377 * display the toggle in an "in between" state. This function turns on
378 * "in between" display. Normally you would turn off the inconsistent
379 * state again if the user toggles the toggle button. This has to be
380 * done manually, gtk_toggle_button_set_inconsistent() only affects
381 * visual appearance, it doesn't affect the semantics of the button.
385 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
388 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
390 setting = setting != FALSE;
392 if (setting != toggle_button->inconsistent)
394 toggle_button->inconsistent = setting;
396 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
397 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
399 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
404 * gtk_toggle_button_get_inconsistent:
405 * @toggle_button: a #GtkToggleButton
407 * Gets the value set by gtk_toggle_button_set_inconsistent().
409 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
412 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
414 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
416 return toggle_button->inconsistent;
420 gtk_toggle_button_expose (GtkWidget *widget,
421 GdkEventExpose *event)
423 if (GTK_WIDGET_DRAWABLE (widget))
425 GtkWidget *child = GTK_BIN (widget)->child;
426 GtkButton *button = GTK_BUTTON (widget);
427 GtkStateType state_type;
428 GtkShadowType shadow_type;
430 state_type = GTK_WIDGET_STATE (widget);
432 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
434 if (state_type == GTK_STATE_ACTIVE)
435 state_type = GTK_STATE_NORMAL;
436 shadow_type = GTK_SHADOW_ETCHED_IN;
439 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
441 _gtk_button_paint (button, &event->area, state_type, shadow_type,
442 "togglebutton", "togglebuttondefault");
445 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
452 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
453 gboolean group_cycling)
456 * We override the standard implementation in
457 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
458 * if there is no mnemonic conflict.
460 if (GTK_WIDGET_CAN_FOCUS (widget))
461 gtk_widget_grab_focus (widget);
464 gtk_widget_activate (widget);
470 gtk_toggle_button_pressed (GtkButton *button)
472 button->button_down = TRUE;
474 gtk_toggle_button_update_state (button);
475 gtk_widget_queue_draw (GTK_WIDGET (button));
479 gtk_toggle_button_released (GtkButton *button)
481 if (button->button_down)
483 button->button_down = FALSE;
485 if (button->in_button)
486 gtk_button_clicked (button);
488 gtk_toggle_button_update_state (button);
489 gtk_widget_queue_draw (GTK_WIDGET (button));
494 gtk_toggle_button_clicked (GtkButton *button)
496 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
497 toggle_button->active = !toggle_button->active;
499 gtk_toggle_button_toggled (toggle_button);
501 gtk_toggle_button_update_state (button);
503 g_object_notify (G_OBJECT (toggle_button), "active");
505 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
506 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
510 gtk_toggle_button_update_state (GtkButton *button)
512 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
513 gboolean depressed, touchscreen;
514 GtkStateType new_state;
516 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
517 "gtk-touchscreen-mode", &touchscreen,
520 if (toggle_button->inconsistent)
522 else if (button->in_button && button->button_down)
525 depressed = toggle_button->active;
527 if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
528 new_state = GTK_STATE_PRELIGHT;
530 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
532 _gtk_button_set_depressed (button, depressed);
533 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
536 #define __GTK_TOGGLE_BUTTON_C__
537 #include "gtkaliasdef.c"