1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2010 Intel Corporation
4 * Copyright (C) 2010 RedHat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 * Emmanuele Bassi <ebassi@linux.intel.com>
21 * Matthias Clasen <mclasen@redhat.com>
23 * Based on similar code from Mx.
28 * @Short_Description: A "light switch" style toggle
30 * @See_Also: #GtkToggleButton
32 * #GtkSwitch is a widget that has two states: on or off. The user can control
33 * which state should be active by clicking the empty area, or by dragging the
39 #include "gtkswitch.h"
41 #include "gtkactivatable.h"
43 #include "gtkprivate.h"
44 #include "gtktoggleaction.h"
45 #include "gtkwidget.h"
46 #include "gtkmarshalers.h"
47 #include "gtkapplicationprivate.h"
48 #include "gtkactionable.h"
49 #include "a11y/gtkswitchaccessible.h"
53 #define DEFAULT_SLIDER_WIDTH (36)
54 #define DEFAULT_SLIDER_HEIGHT (22)
56 struct _GtkSwitchPrivate
58 GdkWindow *event_window;
62 GVariant *action_target;
63 GSimpleActionObserver *action_observer;
71 guint is_dragging : 1;
74 guint use_action_appearance : 1;
82 PROP_USE_ACTION_APPEARANCE,
94 static guint signals[LAST_SIGNAL] = { 0 };
96 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
98 static void gtk_switch_hierarchy_changed (GtkWidget *widget,
99 GtkWidget *previous_toplevel);
101 static void gtk_switch_actionable_iface_init (GtkActionableInterface *iface);
102 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
104 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
105 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE,
106 gtk_switch_actionable_iface_init)
107 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
108 gtk_switch_activatable_interface_init));
111 gtk_switch_button_press (GtkWidget *widget,
112 GdkEventButton *event)
114 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
115 GtkAllocation allocation;
117 gtk_widget_get_allocation (widget, &allocation);
121 /* if the event occurred in the "off" area, then this
124 if (event->x <= allocation.width / 2)
126 priv->in_press = TRUE;
130 priv->offset = event->x - allocation.width / 2;
134 /* if the event occurred in the "on" area, then this
137 if (event->x >= allocation.width / 2)
139 priv->in_press = TRUE;
143 priv->offset = event->x;
146 priv->drag_start = event->x;
148 g_object_get (gtk_widget_get_settings (widget),
149 "gtk-dnd-drag-threshold", &priv->drag_threshold,
156 gtk_switch_motion (GtkWidget *widget,
157 GdkEventMotion *event)
159 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
161 /* if this is a direct toggle we don't handle motion */
165 if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
168 if (event->state & GDK_BUTTON1_MASK)
170 gint position = event->x - priv->offset;
171 GtkAllocation allocation;
172 GtkStyleContext *context;
175 gint width, focus_width, focus_pad;
177 gtk_widget_style_get (widget,
178 "focus-line-width", &focus_width,
179 "focus-padding", &focus_pad,
182 context = gtk_widget_get_style_context (widget);
183 state = gtk_widget_get_state_flags (widget);
185 gtk_style_context_save (context);
186 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
187 gtk_style_context_get_padding (context, state, &padding);
188 gtk_style_context_restore (context);
190 gtk_widget_get_allocation (widget, &allocation);
192 width = allocation.width - 2 * (focus_width + focus_pad);
194 /* constrain the handle within the trough width */
195 if (position > (width / 2) - padding.right)
196 priv->handle_x = width / 2 - padding.right;
197 else if (position < padding.left)
200 priv->handle_x = position;
202 priv->is_dragging = TRUE;
204 /* we need to redraw the handle */
205 gtk_widget_queue_draw (widget);
214 gtk_switch_button_release (GtkWidget *widget,
215 GdkEventButton *event)
217 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
218 GtkAllocation allocation;
220 gtk_widget_get_allocation (widget, &allocation);
222 /* dragged toggles have a "soft" grab, so we don't care whether we
223 * are in the switch or not when the button is released; we do care
224 * for direct toggles, instead
226 if (!priv->is_dragging && !priv->in_switch)
232 priv->in_press = FALSE;
233 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
238 /* toggle the switch if the handle was clicked but a drag had not been
240 if (!priv->is_dragging && !priv->in_press)
242 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
248 if (priv->is_dragging)
250 priv->is_dragging = FALSE;
252 /* if half the handle passed the middle of the switch, then we
253 * consider it to be on
255 if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
257 gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
258 priv->handle_x = allocation.width / 2;
262 gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
266 gtk_widget_queue_draw (widget);
275 gtk_switch_enter (GtkWidget *widget,
276 GdkEventCrossing *event)
278 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
280 if (event->window == priv->event_window)
281 priv->in_switch = TRUE;
287 gtk_switch_leave (GtkWidget *widget,
288 GdkEventCrossing *event)
290 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
292 if (event->window == priv->event_window)
293 priv->in_switch = FALSE;
299 gtk_switch_activate (GtkSwitch *sw)
301 GtkSwitchPrivate *priv = sw->priv;
303 gtk_switch_set_active (sw, !priv->is_active);
307 gtk_switch_get_preferred_width (GtkWidget *widget,
311 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
312 GtkStyleContext *context;
315 gint width, slider_width, focus_width, focus_pad;
317 PangoRectangle logical_rect;
319 context = gtk_widget_get_style_context (widget);
320 state = gtk_widget_get_state_flags (widget);
323 state |= GTK_STATE_FLAG_ACTIVE;
325 gtk_style_context_save (context);
327 gtk_style_context_set_state (context, state);
328 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
329 gtk_style_context_get_padding (context, state, &padding);
331 width = padding.left + padding.right;
333 gtk_style_context_restore (context);
335 gtk_widget_style_get (widget,
336 "slider-width", &slider_width,
337 "focus-line-width", &focus_width,
338 "focus-padding", &focus_pad,
341 width += 2 * (focus_width + focus_pad);
343 /* Translators: if the "on" state label requires more than three
344 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
347 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
348 pango_layout_get_extents (layout, NULL, &logical_rect);
349 pango_extents_to_pixels (&logical_rect, NULL);
350 width += MAX (logical_rect.width, slider_width);
352 /* Translators: if the "off" state label requires more than three
353 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
355 pango_layout_set_text (layout, C_("switch", "OFF"), -1);
356 pango_layout_get_extents (layout, NULL, &logical_rect);
357 pango_extents_to_pixels (&logical_rect, NULL);
358 width += MAX (logical_rect.width, slider_width);
360 g_object_unref (layout);
370 gtk_switch_get_preferred_height (GtkWidget *widget,
374 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
375 GtkStyleContext *context;
378 gint height, focus_width, focus_pad;
380 PangoRectangle logical_rect;
383 context = gtk_widget_get_style_context (widget);
384 state = gtk_widget_get_state_flags (widget);
387 state |= GTK_STATE_FLAG_ACTIVE;
389 gtk_style_context_save (context);
391 gtk_style_context_set_state (context, state);
392 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
393 gtk_style_context_get_padding (context, state, &padding);
395 height = padding.top + padding.bottom;
397 gtk_style_context_restore (context);
399 gtk_widget_style_get (widget,
400 "focus-line-width", &focus_width,
401 "focus-padding", &focus_pad,
404 height += 2 * (focus_width + focus_pad);
406 str = g_strdup_printf ("%s%s",
408 C_("switch", "OFF"));
410 layout = gtk_widget_create_pango_layout (widget, str);
411 pango_layout_get_extents (layout, NULL, &logical_rect);
412 pango_extents_to_pixels (&logical_rect, NULL);
413 height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
415 g_object_unref (layout);
426 gtk_switch_size_allocate (GtkWidget *widget,
427 GtkAllocation *allocation)
429 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
431 gtk_widget_set_allocation (widget, allocation);
433 if (gtk_widget_get_realized (widget))
434 gdk_window_move_resize (priv->event_window,
442 gtk_switch_realize (GtkWidget *widget)
444 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
445 GdkWindow *parent_window;
446 GdkWindowAttr attributes;
447 gint attributes_mask;
448 GtkAllocation allocation;
450 gtk_widget_set_realized (widget, TRUE);
451 parent_window = gtk_widget_get_parent_window (widget);
452 gtk_widget_set_window (widget, parent_window);
453 g_object_ref (parent_window);
455 gtk_widget_get_allocation (widget, &allocation);
457 attributes.window_type = GDK_WINDOW_CHILD;
458 attributes.wclass = GDK_INPUT_ONLY;
459 attributes.x = allocation.x;
460 attributes.y = allocation.y;
461 attributes.width = allocation.width;
462 attributes.height = allocation.height;
463 attributes.event_mask = gtk_widget_get_events (widget);
464 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
465 GDK_BUTTON_RELEASE_MASK |
466 GDK_BUTTON1_MOTION_MASK |
467 GDK_POINTER_MOTION_HINT_MASK |
468 GDK_POINTER_MOTION_MASK |
469 GDK_ENTER_NOTIFY_MASK |
470 GDK_LEAVE_NOTIFY_MASK);
471 attributes_mask = GDK_WA_X | GDK_WA_Y;
473 priv->event_window = gdk_window_new (parent_window,
476 gdk_window_set_user_data (priv->event_window, widget);
480 gtk_switch_unrealize (GtkWidget *widget)
482 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
484 if (priv->event_window != NULL)
486 gdk_window_set_user_data (priv->event_window, NULL);
487 gdk_window_destroy (priv->event_window);
488 priv->event_window = NULL;
491 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
495 gtk_switch_map (GtkWidget *widget)
497 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
499 GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
501 if (priv->event_window)
502 gdk_window_show (priv->event_window);
506 gtk_switch_unmap (GtkWidget *widget)
508 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
510 if (priv->event_window)
511 gdk_window_hide (priv->event_window);
513 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
517 gtk_switch_paint_handle (GtkWidget *widget,
521 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
522 GtkStyleContext *context = gtk_widget_get_style_context (widget);
525 state = gtk_widget_get_state_flags (widget);
528 state |= GTK_STATE_FLAG_ACTIVE;
530 gtk_style_context_save (context);
531 gtk_style_context_set_state (context, state);
532 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
534 gtk_render_slider (context, cr,
536 box->width, box->height,
537 GTK_ORIENTATION_HORIZONTAL);
539 gtk_style_context_restore (context);
543 gtk_switch_draw (GtkWidget *widget,
546 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
547 GtkStyleContext *context;
550 PangoFontDescription *desc;
551 const PangoFontDescription *style_desc;
553 gint label_x, label_y;
556 gint focus_width, focus_pad;
557 gint x, y, width, height;
558 gint font_size, style_font_size;
560 gtk_widget_style_get (widget,
561 "focus-line-width", &focus_width,
562 "focus-padding", &focus_pad,
565 context = gtk_widget_get_style_context (widget);
566 state = gtk_widget_get_state_flags (widget);
569 state |= GTK_STATE_FLAG_ACTIVE;
571 gtk_style_context_save (context);
573 gtk_style_context_set_state (context, state);
574 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
576 gtk_style_context_get_padding (context, state, &padding);
578 gtk_style_context_restore (context);
582 width = gtk_widget_get_allocated_width (widget);
583 height = gtk_widget_get_allocated_height (widget);
585 if (gtk_widget_has_visible_focus (widget))
586 gtk_render_focus (context, cr, x, y, width, height);
588 x += focus_width + focus_pad;
589 y += focus_width + focus_pad;
590 width -= 2 * (focus_width + focus_pad);
591 height -= 2 * (focus_width + focus_pad);
593 gtk_style_context_save (context);
594 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
595 gtk_style_context_set_state (context, state);
597 gtk_render_background (context, cr, x, y, width, height);
598 gtk_render_frame (context, cr, x, y, width, height);
600 width -= padding.left + padding.right;
601 height -= padding.top + padding.bottom;
607 handle.width = width / 2;
608 handle.height = height;
610 /* Translators: if the "on" state label requires more than three
611 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
614 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
616 /* FIXME: this should be really done in the theme, but overriding font size
617 * from it doesn't currently work. So we have to hardcode this here and
618 * below for the "OFF" label.
620 desc = pango_font_description_new ();
622 style_desc = gtk_style_context_get_font (context, state);
623 style_font_size = pango_font_description_get_size (style_desc);
624 font_size = MAX (style_font_size - 1 * PANGO_SCALE, ceil (style_font_size * PANGO_SCALE_SMALL));
626 pango_font_description_set_size (desc, font_size);
628 pango_layout_set_font_description (layout, desc);
630 pango_layout_get_extents (layout, NULL, &rect);
631 pango_extents_to_pixels (&rect, NULL);
633 label_x = x + ((width / 2) - rect.width) / 2;
634 label_y = y + (height - rect.height) / 2;
636 gtk_render_layout (context, cr, label_x, label_y, layout);
638 g_object_unref (layout);
640 /* Translators: if the "off" state label requires more than three
641 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
643 layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
644 pango_layout_set_font_description (layout, desc);
646 pango_layout_get_extents (layout, NULL, &rect);
647 pango_extents_to_pixels (&rect, NULL);
649 label_x = x + (width / 2) + ((width / 2) - rect.width) / 2;
650 label_y = y + (height - rect.height) / 2;
652 gtk_render_layout (context, cr, label_x, label_y, layout);
654 g_object_unref (layout);
656 if (priv->is_dragging)
657 handle.x = x + priv->handle_x;
658 else if (priv->is_active)
659 handle.x = x + width - handle.width;
663 gtk_style_context_restore (context);
665 gtk_switch_paint_handle (widget, cr, &handle);
667 pango_font_description_free (desc);
673 gtk_switch_set_related_action (GtkSwitch *sw,
676 GtkSwitchPrivate *priv = sw->priv;
678 if (priv->action == action)
681 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
683 priv->action = action;
687 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
688 gboolean use_appearance)
690 GtkSwitchPrivate *priv = sw->priv;
692 if (priv->use_action_appearance != use_appearance)
694 priv->use_action_appearance = use_appearance;
696 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
701 gtk_switch_update_action_observer (GtkSwitch *sw)
705 /* we are the only owner so this will clear all the signals */
706 g_clear_object (&sw->priv->action_observer);
708 window = gtk_widget_get_toplevel (GTK_WIDGET (sw));
710 if (GTK_IS_APPLICATION_WINDOW (window) && sw->priv->action_name)
712 GSimpleActionObserver *observer;
714 observer = gtk_application_window_create_observer (GTK_APPLICATION_WINDOW (window),
715 sw->priv->action_name,
716 sw->priv->action_target);
718 g_object_bind_property (observer, "active", sw, "active", G_BINDING_SYNC_CREATE);
719 g_object_bind_property (observer, "enabled", sw, "sensitive", G_BINDING_SYNC_CREATE);
721 sw->priv->action_observer = observer;
726 gtk_switch_set_action_name (GtkActionable *actionable,
727 const gchar *action_name)
729 GtkSwitch *sw = GTK_SWITCH (actionable);
731 g_return_if_fail (GTK_IS_SWITCH (sw));
733 g_free (sw->priv->action_name);
734 sw->priv->action_name = g_strdup (action_name);
736 gtk_switch_update_action_observer (sw);
738 g_object_notify (G_OBJECT (sw), "action-name");
742 gtk_switch_set_action_target_value (GtkActionable *actionable,
743 GVariant *action_target)
745 GtkSwitch *sw = GTK_SWITCH (actionable);
747 g_return_if_fail (GTK_IS_SWITCH (sw));
749 if (action_target != sw->priv->action_target &&
750 (!action_target || !sw->priv->action_target ||
751 !g_variant_equal (action_target, sw->priv->action_target)))
753 if (sw->priv->action_target)
754 g_variant_unref (sw->priv->action_target);
756 sw->priv->action_target = NULL;
759 sw->priv->action_target = g_variant_ref_sink (action_target);
761 gtk_switch_update_action_observer (sw);
763 g_object_notify (G_OBJECT (sw), "action-target");
768 gtk_switch_get_action_name (GtkActionable *actionable)
770 GtkSwitch *sw = GTK_SWITCH (actionable);
772 return sw->priv->action_name;
776 gtk_switch_get_action_target_value (GtkActionable *actionable)
778 GtkSwitch *sw = GTK_SWITCH (actionable);
780 return sw->priv->action_target;
784 gtk_switch_actionable_iface_init (GtkActionableInterface *iface)
786 iface->get_action_name = gtk_switch_get_action_name;
787 iface->set_action_name = gtk_switch_set_action_name;
788 iface->get_action_target_value = gtk_switch_get_action_target_value;
789 iface->set_action_target_value = gtk_switch_set_action_target_value;
793 gtk_switch_hierarchy_changed (GtkWidget *widget,
794 GtkWidget *previous_toplevel)
796 GtkSwitch *sw = GTK_SWITCH (widget);
797 GtkWidgetClass *parent_class;
799 parent_class = GTK_WIDGET_CLASS (gtk_switch_parent_class);
800 if (parent_class->hierarchy_changed)
801 parent_class->hierarchy_changed (widget, previous_toplevel);
803 if (sw->priv->action_name)
807 toplevel = gtk_widget_get_toplevel (widget);
809 if (toplevel != previous_toplevel)
810 gtk_switch_update_action_observer (sw);
815 gtk_switch_set_property (GObject *gobject,
820 GtkSwitch *sw = GTK_SWITCH (gobject);
825 gtk_switch_set_active (sw, g_value_get_boolean (value));
828 case PROP_RELATED_ACTION:
829 gtk_switch_set_related_action (sw, g_value_get_object (value));
832 case PROP_USE_ACTION_APPEARANCE:
833 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
836 case PROP_ACTION_NAME:
837 gtk_switch_set_action_name (GTK_ACTIONABLE (sw), g_value_get_string (value));
840 case PROP_ACTION_TARGET:
841 gtk_switch_set_action_target_value (GTK_ACTIONABLE (sw), g_value_get_variant (value));
845 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
850 gtk_switch_get_property (GObject *gobject,
855 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
860 g_value_set_boolean (value, priv->is_active);
863 case PROP_RELATED_ACTION:
864 g_value_set_object (value, priv->action);
867 case PROP_USE_ACTION_APPEARANCE:
868 g_value_set_boolean (value, priv->use_action_appearance);
871 case PROP_ACTION_NAME:
872 g_value_set_string (value, priv->action_name);
875 case PROP_ACTION_TARGET:
876 g_value_set_variant (value, priv->action_target);
880 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
885 gtk_switch_dispose (GObject *object)
887 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
889 g_clear_object (&priv->action_observer);
891 if (priv->action_name)
893 g_free (priv->action_name);
894 priv->action_name = NULL;
899 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
903 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
907 gtk_switch_class_init (GtkSwitchClass *klass)
909 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
910 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
911 gpointer activatable_iface;
913 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
915 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
916 switch_props[PROP_RELATED_ACTION] =
917 g_param_spec_override ("related-action",
918 g_object_interface_find_property (activatable_iface,
921 switch_props[PROP_USE_ACTION_APPEARANCE] =
922 g_param_spec_override ("use-action-appearance",
923 g_object_interface_find_property (activatable_iface,
924 "use-action-appearance"));
929 * Whether the #GtkSwitch widget is in its on or off state.
931 switch_props[PROP_ACTIVE] =
932 g_param_spec_boolean ("active",
934 P_("Whether the switch is on or off"),
936 GTK_PARAM_READWRITE);
938 gobject_class->set_property = gtk_switch_set_property;
939 gobject_class->get_property = gtk_switch_get_property;
940 gobject_class->dispose = gtk_switch_dispose;
942 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
944 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
945 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
946 widget_class->size_allocate = gtk_switch_size_allocate;
947 widget_class->realize = gtk_switch_realize;
948 widget_class->unrealize = gtk_switch_unrealize;
949 widget_class->map = gtk_switch_map;
950 widget_class->unmap = gtk_switch_unmap;
951 widget_class->draw = gtk_switch_draw;
952 widget_class->button_press_event = gtk_switch_button_press;
953 widget_class->button_release_event = gtk_switch_button_release;
954 widget_class->motion_notify_event = gtk_switch_motion;
955 widget_class->enter_notify_event = gtk_switch_enter;
956 widget_class->leave_notify_event = gtk_switch_leave;
957 widget_class->hierarchy_changed = gtk_switch_hierarchy_changed;
959 klass->activate = gtk_switch_activate;
962 * GtkSwitch:slider-width:
964 * The minimum width of the #GtkSwitch handle, in pixels.
966 gtk_widget_class_install_style_property (widget_class,
967 g_param_spec_int ("slider-width",
969 P_("The minimum width of the handle"),
970 DEFAULT_SLIDER_WIDTH, G_MAXINT,
971 DEFAULT_SLIDER_WIDTH,
972 GTK_PARAM_READABLE));
975 * GtkSwitch::activate:
976 * @widget: the object which received the signal.
978 * The ::activate signal on GtkSwitch is an action signal and
979 * emitting it causes the switch to animate.
980 * Applications should never connect to this signal, but use the
981 * notify::active signal.
984 g_signal_new (I_("activate"),
985 G_OBJECT_CLASS_TYPE (gobject_class),
986 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
987 G_STRUCT_OFFSET (GtkSwitchClass, activate),
989 _gtk_marshal_VOID__VOID,
991 widget_class->activate_signal = signals[ACTIVATE];
993 g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
994 g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
996 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SWITCH_ACCESSIBLE);
1000 gtk_switch_init (GtkSwitch *self)
1002 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
1003 self->priv->use_action_appearance = TRUE;
1004 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
1005 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
1011 * Creates a new #GtkSwitch widget.
1013 * Return value: the newly created #GtkSwitch instance
1018 gtk_switch_new (void)
1020 return g_object_new (GTK_TYPE_SWITCH, NULL);
1024 * gtk_switch_set_active:
1026 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
1028 * Changes the state of @sw to the desired one.
1033 gtk_switch_set_active (GtkSwitch *sw,
1036 GtkSwitchPrivate *priv;
1038 g_return_if_fail (GTK_IS_SWITCH (sw));
1040 is_active = !!is_active;
1044 if (priv->is_active != is_active)
1046 AtkObject *accessible;
1048 GtkStyleContext *context;
1050 widget = GTK_WIDGET (sw);
1051 priv->is_active = is_active;
1053 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
1055 if (priv->action_observer)
1056 g_simple_action_observer_activate (priv->action_observer);
1059 gtk_action_activate (priv->action);
1061 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
1062 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
1064 if (gtk_widget_get_realized (widget))
1066 context = gtk_widget_get_style_context (widget);
1067 gtk_style_context_notify_state_change (context,
1068 gtk_widget_get_window (widget),
1069 NULL, GTK_STATE_ACTIVE, is_active);
1072 gtk_widget_queue_draw (GTK_WIDGET (sw));
1077 * gtk_switch_get_active:
1080 * Gets whether the #GtkSwitch is in its "on" or "off" state.
1082 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
1087 gtk_switch_get_active (GtkSwitch *sw)
1089 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
1091 return sw->priv->is_active;
1095 gtk_switch_update (GtkActivatable *activatable,
1097 const gchar *property_name)
1099 if (strcmp (property_name, "visible") == 0)
1101 if (gtk_action_is_visible (action))
1102 gtk_widget_show (GTK_WIDGET (activatable));
1104 gtk_widget_hide (GTK_WIDGET (activatable));
1106 else if (strcmp (property_name, "sensitive") == 0)
1108 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1110 else if (strcmp (property_name, "active") == 0)
1112 gtk_action_block_activate (action);
1113 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1114 gtk_action_unblock_activate (action);
1119 gtk_switch_sync_action_properties (GtkActivatable *activatable,
1125 if (gtk_action_is_visible (action))
1126 gtk_widget_show (GTK_WIDGET (activatable));
1128 gtk_widget_hide (GTK_WIDGET (activatable));
1130 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1132 gtk_action_block_activate (action);
1133 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1134 gtk_action_unblock_activate (action);
1138 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
1140 iface->update = gtk_switch_update;
1141 iface->sync_action_properties = gtk_switch_sync_action_properties;