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 "gtksignal.h"
30 #include "gtktogglebutton.h"
33 #define DEFAULT_LEFT_POS 4
34 #define DEFAULT_TOP_POS 4
35 #define DEFAULT_SPACING 7
50 static void gtk_toggle_button_class_init (GtkToggleButtonClass *klass);
51 static void gtk_toggle_button_init (GtkToggleButton *toggle_button);
52 static void gtk_toggle_button_paint (GtkWidget *widget,
54 static void gtk_toggle_button_size_allocate (GtkWidget *widget,
55 GtkAllocation *allocation);
56 static gint gtk_toggle_button_expose (GtkWidget *widget,
57 GdkEventExpose *event);
58 static void gtk_toggle_button_pressed (GtkButton *button);
59 static void gtk_toggle_button_released (GtkButton *button);
60 static void gtk_toggle_button_clicked (GtkButton *button);
61 static void gtk_toggle_button_enter (GtkButton *button);
62 static void gtk_toggle_button_leave (GtkButton *button);
63 static void gtk_toggle_button_set_property (GObject *object,
67 static void gtk_toggle_button_get_property (GObject *object,
71 static void gtk_toggle_button_leave (GtkButton *button);
72 static void gtk_toggle_button_realize (GtkWidget *widget);
73 static void gtk_toggle_button_unrealize (GtkWidget *widget);
74 static void gtk_toggle_button_map (GtkWidget *widget);
75 static void gtk_toggle_button_unmap (GtkWidget *widget);
77 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
78 static GtkContainerClass *parent_class = NULL;
81 gtk_toggle_button_get_type (void)
83 static GtkType toggle_button_type = 0;
85 if (!toggle_button_type)
87 static const GtkTypeInfo toggle_button_info =
90 sizeof (GtkToggleButton),
91 sizeof (GtkToggleButtonClass),
92 (GtkClassInitFunc) gtk_toggle_button_class_init,
93 (GtkObjectInitFunc) gtk_toggle_button_init,
94 /* reserved_1 */ NULL,
95 /* reserved_2 */ NULL,
96 (GtkClassInitFunc) NULL,
99 toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
102 return toggle_button_type;
106 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
108 GtkObjectClass *object_class;
109 GObjectClass *gobject_class;
110 GtkWidgetClass *widget_class;
111 GtkContainerClass *container_class;
112 GtkButtonClass *button_class;
114 object_class = (GtkObjectClass*) class;
115 gobject_class = G_OBJECT_CLASS (class);
116 widget_class = (GtkWidgetClass*) class;
117 container_class = (GtkContainerClass*) class;
118 button_class = (GtkButtonClass*) class;
120 parent_class = gtk_type_class (GTK_TYPE_BUTTON);
123 gobject_class->set_property = gtk_toggle_button_set_property;
124 gobject_class->get_property = gtk_toggle_button_get_property;
126 widget_class->size_allocate = gtk_toggle_button_size_allocate;
127 widget_class->expose_event = gtk_toggle_button_expose;
128 widget_class->realize = gtk_toggle_button_realize;
129 widget_class->unrealize = gtk_toggle_button_unrealize;
130 widget_class->map = gtk_toggle_button_map;
131 widget_class->unmap = gtk_toggle_button_unmap;
133 button_class->pressed = gtk_toggle_button_pressed;
134 button_class->released = gtk_toggle_button_released;
135 button_class->clicked = gtk_toggle_button_clicked;
136 button_class->enter = gtk_toggle_button_enter;
137 button_class->leave = gtk_toggle_button_leave;
139 class->toggled = NULL;
141 g_object_class_install_property (gobject_class,
143 g_param_spec_boolean ("active",
145 _("If the toggle button should be pressed in or not"),
149 g_object_class_install_property (gobject_class,
151 g_param_spec_boolean ("inconsistent",
153 _("If the toggle button is in an \"in between\" state."),
157 g_object_class_install_property (gobject_class,
159 g_param_spec_boolean ("draw_indicator",
161 _("If the toggle part of the button is displayed"),
165 toggle_button_signals[TOGGLED] =
166 gtk_signal_new ("toggled",
168 GTK_CLASS_TYPE (object_class),
169 GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
170 gtk_marshal_VOID__VOID,
175 gtk_toggle_button_init (GtkToggleButton *toggle_button)
177 toggle_button->active = FALSE;
178 toggle_button->draw_indicator = FALSE;
179 GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
184 gtk_toggle_button_new (void)
186 return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
190 gtk_toggle_button_new_with_label (const gchar *label)
192 GtkWidget *toggle_button;
193 GtkWidget *label_widget;
195 toggle_button = gtk_toggle_button_new ();
196 label_widget = gtk_label_new (label);
197 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
199 gtk_container_add (GTK_CONTAINER (toggle_button), label_widget);
200 gtk_widget_show (label_widget);
202 return toggle_button;
206 * gtk_toggle_button_new_with_mnemonic:
207 * @label: the text of the button, with an underscore in front of the
209 * @returns: a new #GtkToggleButton
211 * Creates a new #GtkToggleButton containing a label. The label
212 * will be created using gtk_label_new_with_mnemonic(), so underscores
213 * in @label indicate the mnemonic for the button.
216 gtk_toggle_button_new_with_mnemonic (const gchar *label)
218 GtkWidget *toggle_button;
219 GtkWidget *label_widget;
221 toggle_button = gtk_toggle_button_new ();
222 label_widget = gtk_label_new_with_mnemonic (label);
223 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
225 gtk_container_add (GTK_CONTAINER (toggle_button), label_widget);
226 gtk_widget_show (label_widget);
228 return toggle_button;
232 gtk_toggle_button_set_property (GObject *object,
239 tb = GTK_TOGGLE_BUTTON (object);
244 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
246 case PROP_INCONSISTENT:
247 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
249 case PROP_DRAW_INDICATOR:
250 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
258 gtk_toggle_button_get_property (GObject *object,
265 tb = GTK_TOGGLE_BUTTON (object);
270 g_value_set_boolean (value, tb->active);
272 case PROP_INCONSISTENT:
273 g_value_set_boolean (value, tb->inconsistent);
275 case PROP_DRAW_INDICATOR:
276 g_value_set_boolean (value, tb->draw_indicator);
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
286 gboolean draw_indicator)
290 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
292 widget = GTK_WIDGET (toggle_button);
294 draw_indicator = draw_indicator ? TRUE : FALSE;
296 if (toggle_button->draw_indicator != draw_indicator)
298 if (GTK_WIDGET_REALIZED (toggle_button))
300 gboolean visible = GTK_WIDGET_VISIBLE (toggle_button);
303 gtk_widget_hide (widget);
305 gtk_widget_unrealize (widget);
306 toggle_button->draw_indicator = draw_indicator;
308 if (toggle_button->draw_indicator)
309 GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
311 GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
313 gtk_widget_realize (widget);
316 gtk_widget_show (widget);
320 toggle_button->draw_indicator = draw_indicator;
322 if (toggle_button->draw_indicator)
323 GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
325 GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
328 if (GTK_WIDGET_VISIBLE (toggle_button))
329 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
331 g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
336 * gtk_toggle_button_get_mode:
337 * @toggle_button: a #GtkToggleButton
339 * Retrieves whether the button is displayed as a separate indicator
340 * and label. See gtk_toggle_button_set_mode().
342 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
346 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
348 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
350 return toggle_button->draw_indicator;
354 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
357 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
359 is_active = is_active != FALSE;
361 if (toggle_button->active != is_active)
362 gtk_button_clicked (GTK_BUTTON (toggle_button));
367 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
369 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
371 return (toggle_button->active) ? TRUE : FALSE;
376 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
378 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
380 gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
384 * gtk_toggle_button_set_inconsistent:
385 * @toggle_button: a #GtkToggleButton
386 * @setting: %TRUE if state is inconsistent
388 * If the user has selected a range of elements (such as some text or
389 * spreadsheet cells) that are affected by a toggle button, and the
390 * current values in that range are inconsistent, you may want to
391 * display the toggle in an "in between" state. This function turns on
392 * "in between" display. Normally you would turn off the inconsistent
393 * state again if the user toggles the toggle button. This has to be
394 * done manually, gtk_toggle_button_set_inconsistent() only affects
395 * visual appearance, it doesn't affect the semantics of the button.
399 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
402 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
404 setting = setting != FALSE;
406 if (setting != toggle_button->inconsistent)
408 toggle_button->inconsistent = setting;
409 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
411 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
416 * gtk_toggle_button_get_inconsistent:
417 * @toggle_button: a #GtkToggleButton
419 * Gets the value set by gtk_toggle_button_set_inconsistent().
421 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
424 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
426 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
428 return toggle_button->inconsistent;
432 gtk_toggle_button_paint (GtkWidget *widget,
436 GtkToggleButton *toggle_button;
437 GtkShadowType shadow_type;
438 GtkStateType state_type;
440 gboolean interior_focus;
443 button = GTK_BUTTON (widget);
444 toggle_button = GTK_TOGGLE_BUTTON (widget);
446 if (GTK_WIDGET_DRAWABLE (widget))
448 gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
452 width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2;
453 height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2;
455 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
456 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
458 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
459 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
461 gtk_paint_box (widget->style, widget->window,
462 GTK_STATE_NORMAL, GTK_SHADOW_IN,
463 area, widget, "togglebuttondefault",
464 x, y, width, height);
467 if (GTK_WIDGET_CAN_DEFAULT (widget))
469 x += widget->style->xthickness;
470 y += widget->style->ythickness;
471 width -= 2 * x + DEFAULT_SPACING;
472 height -= 2 * y + DEFAULT_SPACING;
473 x += DEFAULT_LEFT_POS;
474 y += DEFAULT_TOP_POS;
477 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
485 state_type = GTK_WIDGET_STATE (widget);
487 if (toggle_button->inconsistent)
489 if (state_type == GTK_STATE_ACTIVE)
490 state_type = GTK_STATE_NORMAL;
491 shadow_type = GTK_SHADOW_ETCHED_IN;
493 else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE) ||
494 toggle_button->active)
495 shadow_type = GTK_SHADOW_IN;
497 shadow_type = GTK_SHADOW_OUT;
499 if (button->relief != GTK_RELIEF_NONE ||
500 (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL &&
501 GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE))
502 gtk_paint_box (widget->style, widget->window,
504 shadow_type, area, widget, "togglebutton",
505 x, y, width, height);
507 if (GTK_WIDGET_HAS_FOCUS (widget))
511 x += widget->style->xthickness + 1;
512 y += widget->style->xthickness + 1;
513 width -= 2 * (widget->style->xthickness + 1);
514 height -= 2 * (widget->style->ythickness + 1);
524 gtk_paint_focus (widget->style, widget->window,
525 area, widget, "togglebutton",
526 x, y, width - 1, height - 1);
532 gtk_toggle_button_size_allocate (GtkWidget *widget,
533 GtkAllocation *allocation)
535 if (!GTK_WIDGET_NO_WINDOW (widget) &&
536 GTK_WIDGET_CLASS (parent_class)->size_allocate)
537 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
541 gtk_toggle_button_expose (GtkWidget *widget,
542 GdkEventExpose *event)
544 if (GTK_WIDGET_DRAWABLE (widget))
546 GtkWidget *child = GTK_BIN (widget)->child;
548 gtk_toggle_button_paint (widget, &event->area);
551 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
558 gtk_toggle_button_pressed (GtkButton *button)
560 GtkToggleButton *toggle_button;
561 GtkStateType new_state;
563 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
565 toggle_button = GTK_TOGGLE_BUTTON (button);
567 button->button_down = TRUE;
569 if (toggle_button->active)
570 new_state = (button->in_button ? GTK_STATE_NORMAL : GTK_STATE_ACTIVE);
572 new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
574 if (GTK_WIDGET_STATE (button) != new_state)
575 gtk_widget_set_state (GTK_WIDGET (button), new_state);
579 gtk_toggle_button_released (GtkButton *button)
581 GtkToggleButton *toggle_button;
582 GtkStateType new_state;
584 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
586 if (button->button_down)
588 toggle_button = GTK_TOGGLE_BUTTON (button);
590 button->button_down = FALSE;
592 if (button->in_button)
594 gtk_button_clicked (button);
598 if (toggle_button->active)
599 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
601 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
603 if (GTK_WIDGET_STATE (button) != new_state)
604 gtk_widget_set_state (GTK_WIDGET (button), new_state);
610 gtk_toggle_button_clicked (GtkButton *button)
612 GtkToggleButton *toggle_button;
613 GtkStateType new_state;
615 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
617 toggle_button = GTK_TOGGLE_BUTTON (button);
618 toggle_button->active = !toggle_button->active;
620 gtk_toggle_button_toggled (toggle_button);
622 if (toggle_button->active)
623 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
625 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
627 if (GTK_WIDGET_STATE (button) != new_state)
628 gtk_widget_set_state (GTK_WIDGET (button), new_state);
630 gtk_widget_queue_draw (GTK_WIDGET (button));
632 g_object_notify (G_OBJECT (toggle_button), "active");
636 gtk_toggle_button_enter (GtkButton *button)
638 GtkToggleButton *toggle_button;
639 GtkStateType new_state;
641 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
643 toggle_button = GTK_TOGGLE_BUTTON (button);
645 if (toggle_button->active)
646 new_state = (button->button_down ? GTK_STATE_NORMAL : GTK_STATE_PRELIGHT);
648 new_state = (button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT);
650 if (GTK_WIDGET_STATE (button) != new_state)
651 gtk_widget_set_state (GTK_WIDGET (button), new_state);
655 gtk_toggle_button_leave (GtkButton *button)
657 GtkToggleButton *toggle_button;
658 GtkStateType new_state;
660 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
662 toggle_button = GTK_TOGGLE_BUTTON (button);
664 new_state = (toggle_button->active ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
666 if (GTK_WIDGET_STATE (button) != new_state)
667 gtk_widget_set_state (GTK_WIDGET (button), new_state);
671 gtk_toggle_button_realize (GtkWidget *widget)
673 GtkToggleButton *toggle_button;
674 GdkWindowAttr attributes;
675 gint attributes_mask;
678 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
680 toggle_button = GTK_TOGGLE_BUTTON (widget);
681 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
683 border_width = GTK_CONTAINER (widget)->border_width;
685 attributes.window_type = GDK_WINDOW_CHILD;
686 attributes.x = widget->allocation.x + border_width;
687 attributes.y = widget->allocation.y + border_width;
688 attributes.width = widget->allocation.width - border_width * 2;
689 attributes.height = widget->allocation.height - border_width * 2;
690 attributes.event_mask = gtk_widget_get_events (widget);
691 attributes.event_mask |= (GDK_EXPOSURE_MASK |
692 GDK_BUTTON_PRESS_MASK |
693 GDK_BUTTON_RELEASE_MASK |
694 GDK_ENTER_NOTIFY_MASK |
695 GDK_LEAVE_NOTIFY_MASK);
697 if (GTK_WIDGET_NO_WINDOW (widget))
699 attributes.wclass = GDK_INPUT_ONLY;
700 attributes_mask = GDK_WA_X | GDK_WA_Y;
702 widget->window = gtk_widget_get_parent_window (widget);
703 gdk_window_ref (widget->window);
705 toggle_button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
706 &attributes, attributes_mask);
707 gdk_window_set_user_data (toggle_button->event_window, toggle_button);
711 attributes.wclass = GDK_INPUT_OUTPUT;
712 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
713 attributes.visual = gtk_widget_get_visual (widget);
714 attributes.colormap = gtk_widget_get_colormap (widget);
715 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
716 &attributes, attributes_mask);
717 gdk_window_set_user_data (widget->window, toggle_button);
720 widget->style = gtk_style_attach (widget->style, widget->window);
722 if (!GTK_WIDGET_NO_WINDOW (widget))
723 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
727 gtk_toggle_button_unrealize (GtkWidget *widget)
729 GtkToggleButton *toggle_button;
731 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
733 toggle_button = GTK_TOGGLE_BUTTON (widget);
735 if (GTK_WIDGET_NO_WINDOW (widget))
737 gdk_window_set_user_data (toggle_button->event_window, NULL);
738 gdk_window_destroy (toggle_button->event_window);
739 toggle_button->event_window = NULL;
742 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
743 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
747 gtk_toggle_button_map (GtkWidget *widget)
749 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
751 if (GTK_WIDGET_NO_WINDOW (widget))
752 gdk_window_show (GTK_TOGGLE_BUTTON (widget)->event_window);
754 GTK_WIDGET_CLASS (parent_class)->map (widget);
758 gtk_toggle_button_unmap (GtkWidget *widget)
760 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
762 if (GTK_WIDGET_NO_WINDOW (widget))
763 gdk_window_hide (GTK_TOGGLE_BUTTON (widget)->event_window);
765 GTK_WIDGET_CLASS (parent_class)->unmap (widget);