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_set_property (GObject *object,
65 static void gtk_toggle_button_get_property (GObject *object,
69 static void gtk_toggle_button_realize (GtkWidget *widget);
70 static void gtk_toggle_button_unrealize (GtkWidget *widget);
71 static void gtk_toggle_button_map (GtkWidget *widget);
72 static void gtk_toggle_button_unmap (GtkWidget *widget);
73 static void gtk_toggle_button_update_state (GtkButton *button);
75 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
76 static GtkContainerClass *parent_class = NULL;
79 gtk_toggle_button_get_type (void)
81 static GtkType toggle_button_type = 0;
83 if (!toggle_button_type)
85 static const GtkTypeInfo toggle_button_info =
88 sizeof (GtkToggleButton),
89 sizeof (GtkToggleButtonClass),
90 (GtkClassInitFunc) gtk_toggle_button_class_init,
91 (GtkObjectInitFunc) gtk_toggle_button_init,
92 /* reserved_1 */ NULL,
93 /* reserved_2 */ NULL,
94 (GtkClassInitFunc) NULL,
97 toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
100 return toggle_button_type;
104 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
106 GtkObjectClass *object_class;
107 GObjectClass *gobject_class;
108 GtkWidgetClass *widget_class;
109 GtkContainerClass *container_class;
110 GtkButtonClass *button_class;
112 object_class = (GtkObjectClass*) class;
113 gobject_class = G_OBJECT_CLASS (class);
114 widget_class = (GtkWidgetClass*) class;
115 container_class = (GtkContainerClass*) class;
116 button_class = (GtkButtonClass*) class;
118 parent_class = gtk_type_class (GTK_TYPE_BUTTON);
121 gobject_class->set_property = gtk_toggle_button_set_property;
122 gobject_class->get_property = gtk_toggle_button_get_property;
124 widget_class->size_allocate = gtk_toggle_button_size_allocate;
125 widget_class->expose_event = gtk_toggle_button_expose;
126 widget_class->realize = gtk_toggle_button_realize;
127 widget_class->unrealize = gtk_toggle_button_unrealize;
128 widget_class->map = gtk_toggle_button_map;
129 widget_class->unmap = gtk_toggle_button_unmap;
131 button_class->pressed = gtk_toggle_button_pressed;
132 button_class->released = gtk_toggle_button_released;
133 button_class->clicked = gtk_toggle_button_clicked;
134 button_class->enter = gtk_toggle_button_update_state;
135 button_class->leave = gtk_toggle_button_update_state;
137 class->toggled = NULL;
139 g_object_class_install_property (gobject_class,
141 g_param_spec_boolean ("active",
143 _("If the toggle button should be pressed in or not"),
147 g_object_class_install_property (gobject_class,
149 g_param_spec_boolean ("inconsistent",
151 _("If the toggle button is in an \"in between\" state."),
155 g_object_class_install_property (gobject_class,
157 g_param_spec_boolean ("draw_indicator",
159 _("If the toggle part of the button is displayed"),
163 toggle_button_signals[TOGGLED] =
164 gtk_signal_new ("toggled",
166 GTK_CLASS_TYPE (object_class),
167 GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
168 gtk_marshal_VOID__VOID,
173 gtk_toggle_button_init (GtkToggleButton *toggle_button)
175 toggle_button->active = FALSE;
176 toggle_button->draw_indicator = FALSE;
177 GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
182 gtk_toggle_button_new (void)
184 return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
188 gtk_toggle_button_new_with_label (const gchar *label)
190 GtkWidget *toggle_button;
191 GtkWidget *label_widget;
193 toggle_button = gtk_toggle_button_new ();
194 label_widget = gtk_label_new (label);
195 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
197 gtk_container_add (GTK_CONTAINER (toggle_button), label_widget);
198 gtk_widget_show (label_widget);
200 return toggle_button;
204 * gtk_toggle_button_new_with_mnemonic:
205 * @label: the text of the button, with an underscore in front of the
207 * @returns: a new #GtkToggleButton
209 * Creates a new #GtkToggleButton containing a label. The label
210 * will be created using gtk_label_new_with_mnemonic(), so underscores
211 * in @label indicate the mnemonic for the button.
214 gtk_toggle_button_new_with_mnemonic (const gchar *label)
216 GtkWidget *toggle_button;
217 GtkWidget *label_widget;
219 toggle_button = gtk_toggle_button_new ();
220 label_widget = gtk_label_new_with_mnemonic (label);
221 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
223 gtk_container_add (GTK_CONTAINER (toggle_button), label_widget);
224 gtk_widget_show (label_widget);
226 return toggle_button;
230 gtk_toggle_button_set_property (GObject *object,
237 tb = GTK_TOGGLE_BUTTON (object);
242 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
244 case PROP_INCONSISTENT:
245 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
247 case PROP_DRAW_INDICATOR:
248 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
256 gtk_toggle_button_get_property (GObject *object,
263 tb = GTK_TOGGLE_BUTTON (object);
268 g_value_set_boolean (value, tb->active);
270 case PROP_INCONSISTENT:
271 g_value_set_boolean (value, tb->inconsistent);
273 case PROP_DRAW_INDICATOR:
274 g_value_set_boolean (value, tb->draw_indicator);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
284 gboolean draw_indicator)
288 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
290 widget = GTK_WIDGET (toggle_button);
292 draw_indicator = draw_indicator ? TRUE : FALSE;
294 if (toggle_button->draw_indicator != draw_indicator)
296 if (GTK_WIDGET_REALIZED (toggle_button))
298 gboolean visible = GTK_WIDGET_VISIBLE (toggle_button);
301 gtk_widget_hide (widget);
303 gtk_widget_unrealize (widget);
304 toggle_button->draw_indicator = draw_indicator;
306 if (toggle_button->draw_indicator)
307 GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
309 GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
311 gtk_widget_realize (widget);
314 gtk_widget_show (widget);
318 toggle_button->draw_indicator = draw_indicator;
320 if (toggle_button->draw_indicator)
321 GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
323 GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
326 if (GTK_WIDGET_VISIBLE (toggle_button))
327 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
329 g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
334 * gtk_toggle_button_get_mode:
335 * @toggle_button: a #GtkToggleButton
337 * Retrieves whether the button is displayed as a separate indicator
338 * and label. See gtk_toggle_button_set_mode().
340 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
344 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
346 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
348 return toggle_button->draw_indicator;
352 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
355 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
357 is_active = is_active != FALSE;
359 if (toggle_button->active != is_active)
360 gtk_button_clicked (GTK_BUTTON (toggle_button));
365 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
367 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
369 return (toggle_button->active) ? TRUE : FALSE;
374 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
376 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
378 gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
382 * gtk_toggle_button_set_inconsistent:
383 * @toggle_button: a #GtkToggleButton
384 * @setting: %TRUE if state is inconsistent
386 * If the user has selected a range of elements (such as some text or
387 * spreadsheet cells) that are affected by a toggle button, and the
388 * current values in that range are inconsistent, you may want to
389 * display the toggle in an "in between" state. This function turns on
390 * "in between" display. Normally you would turn off the inconsistent
391 * state again if the user toggles the toggle button. This has to be
392 * done manually, gtk_toggle_button_set_inconsistent() only affects
393 * visual appearance, it doesn't affect the semantics of the button.
397 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
400 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
402 setting = setting != FALSE;
404 if (setting != toggle_button->inconsistent)
406 toggle_button->inconsistent = setting;
408 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
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;
494 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
496 if (button->relief != GTK_RELIEF_NONE ||
497 (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL &&
498 GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE))
499 gtk_paint_box (widget->style, widget->window,
501 shadow_type, area, widget, "togglebutton",
502 x, y, width, height);
504 if (GTK_WIDGET_HAS_FOCUS (widget))
508 x += widget->style->xthickness + 1;
509 y += widget->style->xthickness + 1;
510 width -= 2 * (widget->style->xthickness + 1);
511 height -= 2 * (widget->style->ythickness + 1);
521 gtk_paint_focus (widget->style, widget->window,
522 area, widget, "togglebutton",
523 x, y, width - 1, height - 1);
529 gtk_toggle_button_size_allocate (GtkWidget *widget,
530 GtkAllocation *allocation)
532 if (!GTK_WIDGET_NO_WINDOW (widget) &&
533 GTK_WIDGET_CLASS (parent_class)->size_allocate)
534 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
538 gtk_toggle_button_expose (GtkWidget *widget,
539 GdkEventExpose *event)
541 if (GTK_WIDGET_DRAWABLE (widget))
543 GtkWidget *child = GTK_BIN (widget)->child;
545 gtk_toggle_button_paint (widget, &event->area);
548 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
555 gtk_toggle_button_pressed (GtkButton *button)
557 button->button_down = TRUE;
559 gtk_toggle_button_update_state (button);
563 gtk_toggle_button_released (GtkButton *button)
565 if (button->button_down)
567 button->button_down = FALSE;
569 if (button->in_button)
570 gtk_button_clicked (button);
572 gtk_toggle_button_update_state (button);
577 gtk_toggle_button_clicked (GtkButton *button)
579 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
580 toggle_button->active = !toggle_button->active;
582 gtk_toggle_button_toggled (toggle_button);
584 gtk_toggle_button_update_state (button);
586 g_object_notify (G_OBJECT (toggle_button), "active");
590 gtk_toggle_button_realize (GtkWidget *widget)
592 GtkToggleButton *toggle_button;
593 GdkWindowAttr attributes;
594 gint attributes_mask;
597 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
599 toggle_button = GTK_TOGGLE_BUTTON (widget);
600 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
602 border_width = GTK_CONTAINER (widget)->border_width;
604 attributes.window_type = GDK_WINDOW_CHILD;
605 attributes.x = widget->allocation.x + border_width;
606 attributes.y = widget->allocation.y + border_width;
607 attributes.width = widget->allocation.width - border_width * 2;
608 attributes.height = widget->allocation.height - border_width * 2;
609 attributes.event_mask = gtk_widget_get_events (widget);
610 attributes.event_mask |= (GDK_EXPOSURE_MASK |
611 GDK_BUTTON_PRESS_MASK |
612 GDK_BUTTON_RELEASE_MASK |
613 GDK_ENTER_NOTIFY_MASK |
614 GDK_LEAVE_NOTIFY_MASK);
616 if (GTK_WIDGET_NO_WINDOW (widget))
618 attributes.wclass = GDK_INPUT_ONLY;
619 attributes_mask = GDK_WA_X | GDK_WA_Y;
621 widget->window = gtk_widget_get_parent_window (widget);
622 gdk_window_ref (widget->window);
624 toggle_button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
625 &attributes, attributes_mask);
626 gdk_window_set_user_data (toggle_button->event_window, toggle_button);
630 attributes.wclass = GDK_INPUT_OUTPUT;
631 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
632 attributes.visual = gtk_widget_get_visual (widget);
633 attributes.colormap = gtk_widget_get_colormap (widget);
634 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
635 &attributes, attributes_mask);
636 gdk_window_set_user_data (widget->window, toggle_button);
639 widget->style = gtk_style_attach (widget->style, widget->window);
641 if (!GTK_WIDGET_NO_WINDOW (widget))
642 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
646 gtk_toggle_button_unrealize (GtkWidget *widget)
648 GtkToggleButton *toggle_button;
650 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
652 toggle_button = GTK_TOGGLE_BUTTON (widget);
654 if (GTK_WIDGET_NO_WINDOW (widget))
656 gdk_window_set_user_data (toggle_button->event_window, NULL);
657 gdk_window_destroy (toggle_button->event_window);
658 toggle_button->event_window = NULL;
661 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
662 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
666 gtk_toggle_button_map (GtkWidget *widget)
668 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
670 if (GTK_WIDGET_NO_WINDOW (widget))
671 gdk_window_show (GTK_TOGGLE_BUTTON (widget)->event_window);
673 GTK_WIDGET_CLASS (parent_class)->map (widget);
677 gtk_toggle_button_unmap (GtkWidget *widget)
679 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
681 if (GTK_WIDGET_NO_WINDOW (widget))
682 gdk_window_hide (GTK_TOGGLE_BUTTON (widget)->event_window);
684 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
688 gtk_toggle_button_update_state (GtkButton *button)
690 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
692 GtkStateType new_state;
694 if (toggle_button->inconsistent)
696 else if (button->in_button && button->button_down)
697 depressed = !toggle_button->active;
699 depressed = toggle_button->active;
701 if (!button->button_down && button->in_button)
702 new_state = GTK_STATE_PRELIGHT;
704 new_state = depressed ? GTK_STATE_ACTIVE: GTK_STATE_NORMAL;
706 _gtk_button_set_depressed (button, depressed);
707 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);