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 GtkStyleContext *context;
314 gint width, slider_width, focus_width, focus_pad;
316 PangoRectangle logical_rect;
318 context = gtk_widget_get_style_context (widget);
319 state = gtk_style_context_get_state (context);
321 gtk_style_context_save (context);
323 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
324 gtk_style_context_get_padding (context, state, &padding);
326 width = padding.left + padding.right;
328 gtk_style_context_restore (context);
330 gtk_widget_style_get (widget,
331 "slider-width", &slider_width,
332 "focus-line-width", &focus_width,
333 "focus-padding", &focus_pad,
336 width += 2 * (focus_width + focus_pad);
338 /* Translators: if the "on" state label requires more than three
339 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
342 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
343 pango_layout_get_extents (layout, NULL, &logical_rect);
344 pango_extents_to_pixels (&logical_rect, NULL);
345 width += MAX (logical_rect.width, slider_width);
347 /* Translators: if the "off" state label requires more than three
348 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
350 pango_layout_set_text (layout, C_("switch", "OFF"), -1);
351 pango_layout_get_extents (layout, NULL, &logical_rect);
352 pango_extents_to_pixels (&logical_rect, NULL);
353 width += MAX (logical_rect.width, slider_width);
355 g_object_unref (layout);
365 gtk_switch_get_preferred_height (GtkWidget *widget,
369 GtkStyleContext *context;
372 gint height, focus_width, focus_pad;
374 PangoRectangle logical_rect;
377 context = gtk_widget_get_style_context (widget);
378 state = gtk_style_context_get_state (context);
380 gtk_style_context_save (context);
382 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
383 gtk_style_context_get_padding (context, state, &padding);
385 height = padding.top + padding.bottom;
387 gtk_style_context_restore (context);
389 gtk_widget_style_get (widget,
390 "focus-line-width", &focus_width,
391 "focus-padding", &focus_pad,
394 height += 2 * (focus_width + focus_pad);
396 str = g_strdup_printf ("%s%s",
398 C_("switch", "OFF"));
400 layout = gtk_widget_create_pango_layout (widget, str);
401 pango_layout_get_extents (layout, NULL, &logical_rect);
402 pango_extents_to_pixels (&logical_rect, NULL);
403 height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
405 g_object_unref (layout);
416 gtk_switch_size_allocate (GtkWidget *widget,
417 GtkAllocation *allocation)
419 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
421 gtk_widget_set_allocation (widget, allocation);
423 if (gtk_widget_get_realized (widget))
424 gdk_window_move_resize (priv->event_window,
432 gtk_switch_realize (GtkWidget *widget)
434 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
435 GdkWindow *parent_window;
436 GdkWindowAttr attributes;
437 gint attributes_mask;
438 GtkAllocation allocation;
440 gtk_widget_set_realized (widget, TRUE);
441 parent_window = gtk_widget_get_parent_window (widget);
442 gtk_widget_set_window (widget, parent_window);
443 g_object_ref (parent_window);
445 gtk_widget_get_allocation (widget, &allocation);
447 attributes.window_type = GDK_WINDOW_CHILD;
448 attributes.wclass = GDK_INPUT_ONLY;
449 attributes.x = allocation.x;
450 attributes.y = allocation.y;
451 attributes.width = allocation.width;
452 attributes.height = allocation.height;
453 attributes.event_mask = gtk_widget_get_events (widget);
454 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
455 GDK_BUTTON_RELEASE_MASK |
456 GDK_BUTTON1_MOTION_MASK |
457 GDK_POINTER_MOTION_HINT_MASK |
458 GDK_POINTER_MOTION_MASK |
459 GDK_ENTER_NOTIFY_MASK |
460 GDK_LEAVE_NOTIFY_MASK);
461 attributes_mask = GDK_WA_X | GDK_WA_Y;
463 priv->event_window = gdk_window_new (parent_window,
466 gdk_window_set_user_data (priv->event_window, widget);
470 gtk_switch_unrealize (GtkWidget *widget)
472 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
474 if (priv->event_window != NULL)
476 gdk_window_set_user_data (priv->event_window, NULL);
477 gdk_window_destroy (priv->event_window);
478 priv->event_window = NULL;
481 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
485 gtk_switch_map (GtkWidget *widget)
487 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
489 GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
491 if (priv->event_window)
492 gdk_window_show (priv->event_window);
496 gtk_switch_unmap (GtkWidget *widget)
498 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
500 if (priv->event_window)
501 gdk_window_hide (priv->event_window);
503 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
507 gtk_switch_paint_handle (GtkWidget *widget,
511 GtkStyleContext *context = gtk_widget_get_style_context (widget);
513 gtk_style_context_save (context);
514 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
516 gtk_render_slider (context, cr,
518 box->width, box->height,
519 GTK_ORIENTATION_HORIZONTAL);
521 gtk_style_context_restore (context);
525 gtk_switch_draw (GtkWidget *widget,
528 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
529 GtkStyleContext *context;
532 PangoFontDescription *desc;
533 const PangoFontDescription *style_desc;
535 gint label_x, label_y;
538 gint focus_width, focus_pad;
539 gint x, y, width, height;
540 gint font_size, style_font_size;
542 gtk_widget_style_get (widget,
543 "focus-line-width", &focus_width,
544 "focus-padding", &focus_pad,
547 context = gtk_widget_get_style_context (widget);
548 state = gtk_widget_get_state_flags (widget);
550 gtk_style_context_save (context);
552 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
554 gtk_style_context_get_padding (context, state, &padding);
556 gtk_style_context_restore (context);
560 width = gtk_widget_get_allocated_width (widget);
561 height = gtk_widget_get_allocated_height (widget);
563 if (gtk_widget_has_visible_focus (widget))
564 gtk_render_focus (context, cr, x, y, width, height);
566 x += focus_width + focus_pad;
567 y += focus_width + focus_pad;
568 width -= 2 * (focus_width + focus_pad);
569 height -= 2 * (focus_width + focus_pad);
571 gtk_style_context_save (context);
572 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
574 gtk_render_background (context, cr, x, y, width, height);
575 gtk_render_frame (context, cr, x, y, width, height);
577 width -= padding.left + padding.right;
578 height -= padding.top + padding.bottom;
584 handle.width = width / 2;
585 handle.height = height;
587 /* Translators: if the "on" state label requires more than three
588 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
591 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
593 /* FIXME: this should be really done in the theme, but overriding font size
594 * from it doesn't currently work. So we have to hardcode this here and
595 * below for the "OFF" label.
597 desc = pango_font_description_new ();
599 style_desc = gtk_style_context_get_font (context, state);
600 style_font_size = pango_font_description_get_size (style_desc);
601 font_size = MAX (style_font_size - 1 * PANGO_SCALE, ceil (style_font_size * PANGO_SCALE_SMALL));
603 pango_font_description_set_size (desc, font_size);
605 pango_layout_set_font_description (layout, desc);
607 pango_layout_get_extents (layout, NULL, &rect);
608 pango_extents_to_pixels (&rect, NULL);
610 label_x = x + ((width / 2) - rect.width) / 2;
611 label_y = y + (height - rect.height) / 2;
613 gtk_render_layout (context, cr, label_x, label_y, layout);
615 g_object_unref (layout);
617 /* Translators: if the "off" state label requires more than three
618 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
620 layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
621 pango_layout_set_font_description (layout, desc);
623 pango_layout_get_extents (layout, NULL, &rect);
624 pango_extents_to_pixels (&rect, NULL);
626 label_x = x + (width / 2) + ((width / 2) - rect.width) / 2;
627 label_y = y + (height - rect.height) / 2;
629 gtk_render_layout (context, cr, label_x, label_y, layout);
631 g_object_unref (layout);
633 if (priv->is_dragging)
634 handle.x = x + priv->handle_x;
635 else if (priv->is_active)
636 handle.x = x + width - handle.width;
640 gtk_style_context_restore (context);
642 gtk_switch_paint_handle (widget, cr, &handle);
644 pango_font_description_free (desc);
650 gtk_switch_set_related_action (GtkSwitch *sw,
653 GtkSwitchPrivate *priv = sw->priv;
655 if (priv->action == action)
658 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
660 priv->action = action;
664 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
665 gboolean use_appearance)
667 GtkSwitchPrivate *priv = sw->priv;
669 if (priv->use_action_appearance != use_appearance)
671 priv->use_action_appearance = use_appearance;
673 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
678 gtk_switch_update_action_observer (GtkSwitch *sw)
682 /* we are the only owner so this will clear all the signals */
683 g_clear_object (&sw->priv->action_observer);
685 window = gtk_widget_get_toplevel (GTK_WIDGET (sw));
687 if (GTK_IS_APPLICATION_WINDOW (window) && sw->priv->action_name)
689 GSimpleActionObserver *observer;
691 observer = gtk_application_window_create_observer (GTK_APPLICATION_WINDOW (window),
692 sw->priv->action_name,
693 sw->priv->action_target);
695 g_object_bind_property (observer, "active", sw, "active", G_BINDING_SYNC_CREATE);
696 g_object_bind_property (observer, "enabled", sw, "sensitive", G_BINDING_SYNC_CREATE);
698 sw->priv->action_observer = observer;
703 gtk_switch_set_action_name (GtkActionable *actionable,
704 const gchar *action_name)
706 GtkSwitch *sw = GTK_SWITCH (actionable);
708 g_return_if_fail (GTK_IS_SWITCH (sw));
710 g_free (sw->priv->action_name);
711 sw->priv->action_name = g_strdup (action_name);
713 gtk_switch_update_action_observer (sw);
715 g_object_notify (G_OBJECT (sw), "action-name");
719 gtk_switch_set_action_target_value (GtkActionable *actionable,
720 GVariant *action_target)
722 GtkSwitch *sw = GTK_SWITCH (actionable);
724 g_return_if_fail (GTK_IS_SWITCH (sw));
726 if (action_target != sw->priv->action_target &&
727 (!action_target || !sw->priv->action_target ||
728 !g_variant_equal (action_target, sw->priv->action_target)))
730 if (sw->priv->action_target)
731 g_variant_unref (sw->priv->action_target);
733 sw->priv->action_target = NULL;
736 sw->priv->action_target = g_variant_ref_sink (action_target);
738 gtk_switch_update_action_observer (sw);
740 g_object_notify (G_OBJECT (sw), "action-target");
745 gtk_switch_get_action_name (GtkActionable *actionable)
747 GtkSwitch *sw = GTK_SWITCH (actionable);
749 return sw->priv->action_name;
753 gtk_switch_get_action_target_value (GtkActionable *actionable)
755 GtkSwitch *sw = GTK_SWITCH (actionable);
757 return sw->priv->action_target;
761 gtk_switch_actionable_iface_init (GtkActionableInterface *iface)
763 iface->get_action_name = gtk_switch_get_action_name;
764 iface->set_action_name = gtk_switch_set_action_name;
765 iface->get_action_target_value = gtk_switch_get_action_target_value;
766 iface->set_action_target_value = gtk_switch_set_action_target_value;
770 gtk_switch_hierarchy_changed (GtkWidget *widget,
771 GtkWidget *previous_toplevel)
773 GtkSwitch *sw = GTK_SWITCH (widget);
774 GtkWidgetClass *parent_class;
776 parent_class = GTK_WIDGET_CLASS (gtk_switch_parent_class);
777 if (parent_class->hierarchy_changed)
778 parent_class->hierarchy_changed (widget, previous_toplevel);
780 if (sw->priv->action_name)
784 toplevel = gtk_widget_get_toplevel (widget);
786 if (toplevel != previous_toplevel)
787 gtk_switch_update_action_observer (sw);
792 gtk_switch_set_property (GObject *gobject,
797 GtkSwitch *sw = GTK_SWITCH (gobject);
802 gtk_switch_set_active (sw, g_value_get_boolean (value));
805 case PROP_RELATED_ACTION:
806 gtk_switch_set_related_action (sw, g_value_get_object (value));
809 case PROP_USE_ACTION_APPEARANCE:
810 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
813 case PROP_ACTION_NAME:
814 gtk_switch_set_action_name (GTK_ACTIONABLE (sw), g_value_get_string (value));
817 case PROP_ACTION_TARGET:
818 gtk_switch_set_action_target_value (GTK_ACTIONABLE (sw), g_value_get_variant (value));
822 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
827 gtk_switch_get_property (GObject *gobject,
832 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
837 g_value_set_boolean (value, priv->is_active);
840 case PROP_RELATED_ACTION:
841 g_value_set_object (value, priv->action);
844 case PROP_USE_ACTION_APPEARANCE:
845 g_value_set_boolean (value, priv->use_action_appearance);
848 case PROP_ACTION_NAME:
849 g_value_set_string (value, priv->action_name);
852 case PROP_ACTION_TARGET:
853 g_value_set_variant (value, priv->action_target);
857 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
862 gtk_switch_dispose (GObject *object)
864 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
866 g_clear_object (&priv->action_observer);
868 if (priv->action_name)
870 g_free (priv->action_name);
871 priv->action_name = NULL;
876 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
880 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
884 gtk_switch_class_init (GtkSwitchClass *klass)
886 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
887 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
888 gpointer activatable_iface;
890 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
892 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
893 switch_props[PROP_RELATED_ACTION] =
894 g_param_spec_override ("related-action",
895 g_object_interface_find_property (activatable_iface,
898 switch_props[PROP_USE_ACTION_APPEARANCE] =
899 g_param_spec_override ("use-action-appearance",
900 g_object_interface_find_property (activatable_iface,
901 "use-action-appearance"));
906 * Whether the #GtkSwitch widget is in its on or off state.
908 switch_props[PROP_ACTIVE] =
909 g_param_spec_boolean ("active",
911 P_("Whether the switch is on or off"),
913 GTK_PARAM_READWRITE);
915 gobject_class->set_property = gtk_switch_set_property;
916 gobject_class->get_property = gtk_switch_get_property;
917 gobject_class->dispose = gtk_switch_dispose;
919 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
921 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
922 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
923 widget_class->size_allocate = gtk_switch_size_allocate;
924 widget_class->realize = gtk_switch_realize;
925 widget_class->unrealize = gtk_switch_unrealize;
926 widget_class->map = gtk_switch_map;
927 widget_class->unmap = gtk_switch_unmap;
928 widget_class->draw = gtk_switch_draw;
929 widget_class->button_press_event = gtk_switch_button_press;
930 widget_class->button_release_event = gtk_switch_button_release;
931 widget_class->motion_notify_event = gtk_switch_motion;
932 widget_class->enter_notify_event = gtk_switch_enter;
933 widget_class->leave_notify_event = gtk_switch_leave;
934 widget_class->hierarchy_changed = gtk_switch_hierarchy_changed;
936 klass->activate = gtk_switch_activate;
939 * GtkSwitch:slider-width:
941 * The minimum width of the #GtkSwitch handle, in pixels.
943 gtk_widget_class_install_style_property (widget_class,
944 g_param_spec_int ("slider-width",
946 P_("The minimum width of the handle"),
947 DEFAULT_SLIDER_WIDTH, G_MAXINT,
948 DEFAULT_SLIDER_WIDTH,
949 GTK_PARAM_READABLE));
952 * GtkSwitch::activate:
953 * @widget: the object which received the signal.
955 * The ::activate signal on GtkSwitch is an action signal and
956 * emitting it causes the switch to animate.
957 * Applications should never connect to this signal, but use the
958 * notify::active signal.
961 g_signal_new (I_("activate"),
962 G_OBJECT_CLASS_TYPE (gobject_class),
963 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
964 G_STRUCT_OFFSET (GtkSwitchClass, activate),
966 _gtk_marshal_VOID__VOID,
968 widget_class->activate_signal = signals[ACTIVATE];
970 g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
971 g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
973 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SWITCH_ACCESSIBLE);
977 gtk_switch_init (GtkSwitch *self)
979 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
980 self->priv->use_action_appearance = TRUE;
981 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
982 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
988 * Creates a new #GtkSwitch widget.
990 * Return value: the newly created #GtkSwitch instance
995 gtk_switch_new (void)
997 return g_object_new (GTK_TYPE_SWITCH, NULL);
1001 * gtk_switch_set_active:
1003 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
1005 * Changes the state of @sw to the desired one.
1010 gtk_switch_set_active (GtkSwitch *sw,
1013 GtkSwitchPrivate *priv;
1015 g_return_if_fail (GTK_IS_SWITCH (sw));
1017 is_active = !!is_active;
1021 if (priv->is_active != is_active)
1023 AtkObject *accessible;
1025 priv->is_active = is_active;
1027 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
1029 if (priv->action_observer)
1030 g_simple_action_observer_activate (priv->action_observer);
1033 gtk_action_activate (priv->action);
1035 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
1036 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
1038 if (priv->is_active)
1039 gtk_widget_set_state_flags (GTK_WIDGET (sw), GTK_STATE_FLAG_ACTIVE, FALSE);
1041 gtk_widget_unset_state_flags (GTK_WIDGET (sw), GTK_STATE_FLAG_ACTIVE);
1043 gtk_widget_queue_draw (GTK_WIDGET (sw));
1048 * gtk_switch_get_active:
1051 * Gets whether the #GtkSwitch is in its "on" or "off" state.
1053 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
1058 gtk_switch_get_active (GtkSwitch *sw)
1060 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
1062 return sw->priv->is_active;
1066 gtk_switch_update (GtkActivatable *activatable,
1068 const gchar *property_name)
1070 if (strcmp (property_name, "visible") == 0)
1072 if (gtk_action_is_visible (action))
1073 gtk_widget_show (GTK_WIDGET (activatable));
1075 gtk_widget_hide (GTK_WIDGET (activatable));
1077 else if (strcmp (property_name, "sensitive") == 0)
1079 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1081 else if (strcmp (property_name, "active") == 0)
1083 gtk_action_block_activate (action);
1084 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1085 gtk_action_unblock_activate (action);
1090 gtk_switch_sync_action_properties (GtkActivatable *activatable,
1096 if (gtk_action_is_visible (action))
1097 gtk_widget_show (GTK_WIDGET (activatable));
1099 gtk_widget_hide (GTK_WIDGET (activatable));
1101 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1103 gtk_action_block_activate (action);
1104 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
1105 gtk_action_unblock_activate (action);
1109 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
1111 iface->update = gtk_switch_update;
1112 iface->sync_action_properties = gtk_switch_sync_action_properties;