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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * Emmanuele Bassi <ebassi@linux.intel.com>
23 * Matthias Clasen <mclasen@redhat.com>
25 * Based on similar code from Mx.
30 * @Short_Description: A "light switch" style toggle
32 * @See_Also: #GtkToggleButton
34 * #GtkSwitch is a widget that has two states: on or off. The user can control
35 * which state should be active by clicking the empty area, or by dragging the
41 #include "gtkswitch.h"
43 #include "gtkaccessibleprivate.h"
44 #include "gtkactivatable.h"
46 #include "gtkprivate.h"
47 #include "gtktoggleaction.h"
48 #include "gtkwidget.h"
49 #include "gtkmarshalers.h"
50 #include "a11y/gtkwidgetaccessible.h"
54 #define DEFAULT_SLIDER_WIDTH (36)
55 #define DEFAULT_SLIDER_HEIGHT (22)
57 struct _GtkSwitchPrivate
59 GdkWindow *event_window;
68 guint is_dragging : 1;
71 guint use_action_appearance : 1;
79 PROP_USE_ACTION_APPEARANCE,
89 static guint signals[LAST_SIGNAL] = { 0 };
91 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
93 GType _gtk_switch_accessible_get_type (void);
95 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
97 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
98 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
99 gtk_switch_activatable_interface_init));
102 gtk_switch_button_press (GtkWidget *widget,
103 GdkEventButton *event)
105 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
106 GtkAllocation allocation;
108 gtk_widget_get_allocation (widget, &allocation);
112 /* if the event occurred in the "off" area, then this
115 if (event->x <= allocation.width / 2)
117 priv->in_press = TRUE;
121 priv->offset = event->x - allocation.width / 2;
125 /* if the event occurred in the "on" area, then this
128 if (event->x >= allocation.width / 2)
130 priv->in_press = TRUE;
134 priv->offset = event->x;
137 priv->drag_start = event->x;
139 g_object_get (gtk_widget_get_settings (widget),
140 "gtk-dnd-drag-threshold", &priv->drag_threshold,
147 gtk_switch_motion (GtkWidget *widget,
148 GdkEventMotion *event)
150 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
152 /* if this is a direct toggle we don't handle motion */
156 if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
159 if (event->state & GDK_BUTTON1_MASK)
161 gint position = event->x - priv->offset;
162 GtkAllocation allocation;
163 GtkStyleContext *context;
166 gint width, focus_width, focus_pad;
168 gtk_widget_style_get (widget,
169 "focus-line-width", &focus_width,
170 "focus-padding", &focus_pad,
173 context = gtk_widget_get_style_context (widget);
174 state = gtk_widget_get_state_flags (widget);
176 gtk_style_context_save (context);
177 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
178 gtk_style_context_get_padding (context, state, &padding);
179 gtk_style_context_restore (context);
181 gtk_widget_get_allocation (widget, &allocation);
183 width = allocation.width - 2 * (focus_width + focus_pad);
185 /* constrain the handle within the trough width */
186 if (position > (width / 2) - padding.right)
187 priv->handle_x = width / 2 - padding.right;
188 else if (position < padding.left)
191 priv->handle_x = position;
193 priv->is_dragging = TRUE;
195 /* we need to redraw the handle */
196 gtk_widget_queue_draw (widget);
205 gtk_switch_button_release (GtkWidget *widget,
206 GdkEventButton *event)
208 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
209 GtkAllocation allocation;
211 gtk_widget_get_allocation (widget, &allocation);
213 /* dragged toggles have a "soft" grab, so we don't care whether we
214 * are in the switch or not when the button is released; we do care
215 * for direct toggles, instead
217 if (!priv->is_dragging && !priv->in_switch)
223 priv->in_press = FALSE;
224 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
229 /* toggle the switch if the handle was clicked but a drag had not been
231 if (!priv->is_dragging && !priv->in_press)
233 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
239 if (priv->is_dragging)
241 priv->is_dragging = FALSE;
243 /* if half the handle passed the middle of the switch, then we
244 * consider it to be on
246 if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
248 gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
249 priv->handle_x = allocation.width / 2;
253 gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
257 gtk_widget_queue_draw (widget);
266 gtk_switch_enter (GtkWidget *widget,
267 GdkEventCrossing *event)
269 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
271 if (event->window == priv->event_window)
272 priv->in_switch = TRUE;
278 gtk_switch_leave (GtkWidget *widget,
279 GdkEventCrossing *event)
281 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
283 if (event->window == priv->event_window)
284 priv->in_switch = FALSE;
290 gtk_switch_activate (GtkSwitch *sw)
292 GtkSwitchPrivate *priv = sw->priv;
294 gtk_switch_set_active (sw, !priv->is_active);
298 gtk_switch_get_preferred_width (GtkWidget *widget,
302 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
303 GtkStyleContext *context;
306 gint width, slider_width, focus_width, focus_pad;
308 PangoRectangle logical_rect;
310 context = gtk_widget_get_style_context (widget);
311 state = gtk_widget_get_state_flags (widget);
314 state |= GTK_STATE_FLAG_ACTIVE;
316 gtk_style_context_save (context);
318 gtk_style_context_set_state (context, state);
319 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
320 gtk_style_context_get_padding (context, state, &padding);
322 width = padding.left + padding.right;
324 gtk_style_context_restore (context);
326 gtk_widget_style_get (widget,
327 "slider-width", &slider_width,
328 "focus-line-width", &focus_width,
329 "focus-padding", &focus_pad,
332 width += 2 * (focus_width + focus_pad);
334 /* Translators: if the "on" state label requires more than three
335 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
338 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
339 pango_layout_get_extents (layout, NULL, &logical_rect);
340 pango_extents_to_pixels (&logical_rect, NULL);
341 width += MAX (logical_rect.width, slider_width);
343 /* Translators: if the "off" state label requires more than three
344 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
346 pango_layout_set_text (layout, C_("switch", "OFF"), -1);
347 pango_layout_get_extents (layout, NULL, &logical_rect);
348 pango_extents_to_pixels (&logical_rect, NULL);
349 width += MAX (logical_rect.width, slider_width);
351 g_object_unref (layout);
361 gtk_switch_get_preferred_height (GtkWidget *widget,
365 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
366 GtkStyleContext *context;
369 gint height, focus_width, focus_pad;
371 PangoRectangle logical_rect;
374 context = gtk_widget_get_style_context (widget);
375 state = gtk_widget_get_state_flags (widget);
378 state |= GTK_STATE_FLAG_ACTIVE;
380 gtk_style_context_save (context);
382 gtk_style_context_set_state (context, state);
383 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
384 gtk_style_context_get_padding (context, state, &padding);
386 height = padding.top + padding.bottom;
388 gtk_style_context_restore (context);
390 gtk_widget_style_get (widget,
391 "focus-line-width", &focus_width,
392 "focus-padding", &focus_pad,
395 height += 2 * (focus_width + focus_pad);
397 str = g_strdup_printf ("%s%s",
399 C_("switch", "OFF"));
401 layout = gtk_widget_create_pango_layout (widget, str);
402 pango_layout_get_extents (layout, NULL, &logical_rect);
403 pango_extents_to_pixels (&logical_rect, NULL);
404 height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
406 g_object_unref (layout);
417 gtk_switch_size_allocate (GtkWidget *widget,
418 GtkAllocation *allocation)
420 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
422 gtk_widget_set_allocation (widget, allocation);
424 if (gtk_widget_get_realized (widget))
425 gdk_window_move_resize (priv->event_window,
433 gtk_switch_realize (GtkWidget *widget)
435 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
436 GdkWindow *parent_window;
437 GdkWindowAttr attributes;
438 gint attributes_mask;
439 GtkAllocation allocation;
441 gtk_widget_set_realized (widget, TRUE);
442 parent_window = gtk_widget_get_parent_window (widget);
443 gtk_widget_set_window (widget, parent_window);
444 g_object_ref (parent_window);
446 gtk_widget_get_allocation (widget, &allocation);
448 attributes.window_type = GDK_WINDOW_CHILD;
449 attributes.wclass = GDK_INPUT_ONLY;
450 attributes.x = allocation.x;
451 attributes.y = allocation.y;
452 attributes.width = allocation.width;
453 attributes.height = allocation.height;
454 attributes.event_mask = gtk_widget_get_events (widget);
455 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
456 GDK_BUTTON_RELEASE_MASK |
457 GDK_BUTTON1_MOTION_MASK |
458 GDK_POINTER_MOTION_HINT_MASK |
459 GDK_POINTER_MOTION_MASK |
460 GDK_ENTER_NOTIFY_MASK |
461 GDK_LEAVE_NOTIFY_MASK);
462 attributes_mask = GDK_WA_X | GDK_WA_Y;
464 priv->event_window = gdk_window_new (parent_window,
467 gdk_window_set_user_data (priv->event_window, widget);
471 gtk_switch_unrealize (GtkWidget *widget)
473 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
475 if (priv->event_window != NULL)
477 gdk_window_set_user_data (priv->event_window, NULL);
478 gdk_window_destroy (priv->event_window);
479 priv->event_window = NULL;
482 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
486 gtk_switch_map (GtkWidget *widget)
488 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
490 GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
492 if (priv->event_window)
493 gdk_window_show (priv->event_window);
497 gtk_switch_unmap (GtkWidget *widget)
499 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
501 if (priv->event_window)
502 gdk_window_hide (priv->event_window);
504 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
508 gtk_switch_paint_handle (GtkWidget *widget,
512 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
513 GtkStyleContext *context = gtk_widget_get_style_context (widget);
516 state = gtk_widget_get_state_flags (widget);
519 state |= GTK_STATE_FLAG_ACTIVE;
521 gtk_style_context_save (context);
522 gtk_style_context_set_state (context, state);
523 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
525 gtk_render_slider (context, cr,
527 box->width, box->height,
528 GTK_ORIENTATION_HORIZONTAL);
530 gtk_style_context_restore (context);
534 gtk_switch_draw (GtkWidget *widget,
537 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
538 GtkStyleContext *context;
541 PangoFontDescription *desc;
542 const PangoFontDescription *style_desc;
544 gint label_x, label_y;
547 gint focus_width, focus_pad;
548 gint x, y, width, height;
549 gint font_size, style_font_size;
551 gtk_widget_style_get (widget,
552 "focus-line-width", &focus_width,
553 "focus-padding", &focus_pad,
556 context = gtk_widget_get_style_context (widget);
557 state = gtk_widget_get_state_flags (widget);
560 state |= GTK_STATE_FLAG_ACTIVE;
562 gtk_style_context_save (context);
564 gtk_style_context_set_state (context, state);
565 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
567 gtk_style_context_get_padding (context, state, &padding);
569 gtk_style_context_restore (context);
573 width = gtk_widget_get_allocated_width (widget);
574 height = gtk_widget_get_allocated_height (widget);
576 if (gtk_widget_has_focus (widget))
577 gtk_render_focus (context, cr, x, y, width, height);
579 x += focus_width + focus_pad;
580 y += focus_width + focus_pad;
581 width -= 2 * (focus_width + focus_pad);
582 height -= 2 * (focus_width + focus_pad);
584 gtk_style_context_save (context);
585 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
586 gtk_style_context_set_state (context, state);
588 gtk_render_background (context, cr, x, y, width, height);
589 gtk_render_frame (context, cr, x, y, width, height);
591 width -= padding.left + padding.right;
592 height -= padding.top + padding.bottom;
598 handle.width = width / 2;
599 handle.height = height;
601 /* Translators: if the "on" state label requires more than three
602 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
605 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
607 /* FIXME: this should be really done in the theme, but overriding font size
608 * from it doesn't currently work. So we have to hardcode this here and
609 * below for the "OFF" label.
611 desc = pango_font_description_new ();
613 style_desc = gtk_style_context_get_font (context, state);
614 style_font_size = pango_font_description_get_size (style_desc);
615 font_size = MAX (style_font_size - 1 * PANGO_SCALE, ceil (style_font_size * PANGO_SCALE_SMALL));
617 pango_font_description_set_size (desc, font_size);
619 pango_layout_set_font_description (layout, desc);
621 pango_layout_get_extents (layout, NULL, &rect);
622 pango_extents_to_pixels (&rect, NULL);
624 label_x = x + ((width / 2) - rect.width) / 2;
625 label_y = y + (height - rect.height) / 2;
627 gtk_render_layout (context, cr, label_x, label_y, layout);
629 g_object_unref (layout);
631 /* Translators: if the "off" state label requires more than three
632 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
634 layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
635 pango_layout_set_font_description (layout, desc);
637 pango_layout_get_extents (layout, NULL, &rect);
638 pango_extents_to_pixels (&rect, NULL);
640 label_x = x + (width / 2) + ((width / 2) - rect.width) / 2;
641 label_y = y + (height - rect.height) / 2;
643 gtk_render_layout (context, cr, label_x, label_y, layout);
645 g_object_unref (layout);
647 if (priv->is_dragging)
648 handle.x = x + priv->handle_x;
649 else if (priv->is_active)
650 handle.x = x + width - handle.width;
654 gtk_style_context_restore (context);
656 gtk_switch_paint_handle (widget, cr, &handle);
658 pango_font_description_free (desc);
664 gtk_switch_set_related_action (GtkSwitch *sw,
667 GtkSwitchPrivate *priv = sw->priv;
669 if (priv->action == action)
672 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
674 priv->action = action;
678 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
679 gboolean use_appearance)
681 GtkSwitchPrivate *priv = sw->priv;
683 if (priv->use_action_appearance != use_appearance)
685 priv->use_action_appearance = use_appearance;
687 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
692 gtk_switch_set_property (GObject *gobject,
697 GtkSwitch *sw = GTK_SWITCH (gobject);
702 gtk_switch_set_active (sw, g_value_get_boolean (value));
705 case PROP_RELATED_ACTION:
706 gtk_switch_set_related_action (sw, g_value_get_object (value));
709 case PROP_USE_ACTION_APPEARANCE:
710 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
714 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
719 gtk_switch_get_property (GObject *gobject,
724 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
729 g_value_set_boolean (value, priv->is_active);
732 case PROP_RELATED_ACTION:
733 g_value_set_object (value, priv->action);
736 case PROP_USE_ACTION_APPEARANCE:
737 g_value_set_boolean (value, priv->use_action_appearance);
741 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
746 gtk_switch_dispose (GObject *object)
748 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
752 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
756 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
760 gtk_switch_class_init (GtkSwitchClass *klass)
762 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
763 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
764 gpointer activatable_iface;
766 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
768 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
769 switch_props[PROP_RELATED_ACTION] =
770 g_param_spec_override ("related-action",
771 g_object_interface_find_property (activatable_iface,
774 switch_props[PROP_USE_ACTION_APPEARANCE] =
775 g_param_spec_override ("use-action-appearance",
776 g_object_interface_find_property (activatable_iface,
777 "use-action-appearance"));
782 * Whether the #GtkSwitch widget is in its on or off state.
784 switch_props[PROP_ACTIVE] =
785 g_param_spec_boolean ("active",
787 P_("Whether the switch is on or off"),
789 GTK_PARAM_READWRITE);
791 gobject_class->set_property = gtk_switch_set_property;
792 gobject_class->get_property = gtk_switch_get_property;
793 gobject_class->dispose = gtk_switch_dispose;
795 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
797 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
798 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
799 widget_class->size_allocate = gtk_switch_size_allocate;
800 widget_class->realize = gtk_switch_realize;
801 widget_class->unrealize = gtk_switch_unrealize;
802 widget_class->map = gtk_switch_map;
803 widget_class->unmap = gtk_switch_unmap;
804 widget_class->draw = gtk_switch_draw;
805 widget_class->button_press_event = gtk_switch_button_press;
806 widget_class->button_release_event = gtk_switch_button_release;
807 widget_class->motion_notify_event = gtk_switch_motion;
808 widget_class->enter_notify_event = gtk_switch_enter;
809 widget_class->leave_notify_event = gtk_switch_leave;
811 klass->activate = gtk_switch_activate;
814 * GtkSwitch:slider-width:
816 * The minimum width of the #GtkSwitch handle, in pixels.
818 gtk_widget_class_install_style_property (widget_class,
819 g_param_spec_int ("slider-width",
821 P_("The minimum width of the handle"),
822 DEFAULT_SLIDER_WIDTH, G_MAXINT,
823 DEFAULT_SLIDER_WIDTH,
824 GTK_PARAM_READABLE));
827 * GtkSwitch::activate:
828 * @widget: the object which received the signal.
830 * The ::activate signal on GtkSwitch is an action signal and
831 * emitting it causes the switch to animate.
832 * Applications should never connect to this signal, but use the
833 * notify::active signal.
836 g_signal_new (I_("activate"),
837 G_OBJECT_CLASS_TYPE (gobject_class),
838 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
839 G_STRUCT_OFFSET (GtkSwitchClass, activate),
841 _gtk_marshal_VOID__VOID,
843 widget_class->activate_signal = signals[ACTIVATE];
845 gtk_widget_class_set_accessible_type (widget_class, _gtk_switch_accessible_get_type ());
849 gtk_switch_init (GtkSwitch *self)
851 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
852 self->priv->use_action_appearance = TRUE;
853 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
854 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
860 * Creates a new #GtkSwitch widget.
862 * Return value: the newly created #GtkSwitch instance
867 gtk_switch_new (void)
869 return g_object_new (GTK_TYPE_SWITCH, NULL);
873 * gtk_switch_set_active:
875 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
877 * Changes the state of @sw to the desired one.
882 gtk_switch_set_active (GtkSwitch *sw,
885 GtkSwitchPrivate *priv;
887 g_return_if_fail (GTK_IS_SWITCH (sw));
889 is_active = !!is_active;
893 if (priv->is_active != is_active)
895 AtkObject *accessible;
897 GtkStyleContext *context;
899 widget = GTK_WIDGET (sw);
900 priv->is_active = is_active;
902 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
905 gtk_action_activate (priv->action);
907 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
908 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
910 if (gtk_widget_get_realized (widget))
912 context = gtk_widget_get_style_context (widget);
913 gtk_style_context_notify_state_change (context,
914 gtk_widget_get_window (widget),
915 NULL, GTK_STATE_ACTIVE, is_active);
918 gtk_widget_queue_draw (GTK_WIDGET (sw));
923 * gtk_switch_get_active:
926 * Gets whether the #GtkSwitch is in its "on" or "off" state.
928 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
933 gtk_switch_get_active (GtkSwitch *sw)
935 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
937 return sw->priv->is_active;
941 gtk_switch_update (GtkActivatable *activatable,
943 const gchar *property_name)
945 if (strcmp (property_name, "visible") == 0)
947 if (gtk_action_is_visible (action))
948 gtk_widget_show (GTK_WIDGET (activatable));
950 gtk_widget_hide (GTK_WIDGET (activatable));
952 else if (strcmp (property_name, "sensitive") == 0)
954 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
956 else if (strcmp (property_name, "active") == 0)
958 gtk_action_block_activate (action);
959 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
960 gtk_action_unblock_activate (action);
965 gtk_switch_sync_action_properties (GtkActivatable *activatable,
971 if (gtk_action_is_visible (action))
972 gtk_widget_show (GTK_WIDGET (activatable));
974 gtk_widget_hide (GTK_WIDGET (activatable));
976 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
978 gtk_action_block_activate (action);
979 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
980 gtk_action_unblock_activate (action);
984 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
986 iface->update = gtk_switch_update;
987 iface->sync_action_properties = gtk_switch_sync_action_properties;
990 /* accessibility: object */
992 typedef struct _GtkSwitchAccessible GtkSwitchAccessible;
993 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
995 struct _GtkSwitchAccessible
997 GtkAccessible object;
1003 /* FIXME: We really want to use GailWidgetClass here */
1004 struct _GtkSwitchAccessibleClass
1006 GtkAccessibleClass parent_class;
1012 static void atk_action_interface_init (AtkActionIface *iface);
1014 G_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
1015 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
1017 static AtkStateSet *
1018 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
1020 AtkStateSet *state_set;
1023 state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
1025 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1029 if (gtk_switch_get_active (GTK_SWITCH (widget)))
1030 atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
1036 gtk_switch_accessible_finalize (GObject *obj)
1038 GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)obj;
1040 g_free (accessible->description);
1042 if (accessible->action_idle)
1043 g_source_remove (accessible->action_idle);
1045 G_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->finalize (obj);
1049 _gtk_switch_accessible_initialize (AtkObject *accessible,
1052 ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
1054 atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
1055 atk_object_set_name (accessible, C_("light switch widget", "Switch"));
1056 atk_object_set_description (accessible, _("Switches between on and off states"));
1060 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
1062 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1063 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
1065 object_class->finalize = gtk_switch_accessible_finalize;
1067 atk_class->initialize = _gtk_switch_accessible_initialize;
1068 atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
1072 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
1074 self->description = NULL;
1075 self->action_idle = 0;
1078 /* accessibility: action interface */
1081 gtk_switch_action_get_n_actions (AtkAction *action)
1086 static const gchar *
1087 gtk_switch_action_get_name (AtkAction *action,
1093 static const gchar *
1094 gtk_switch_action_get_description (AtkAction *action,
1097 GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)action;
1099 return accessible->description;
1103 gtk_switch_action_set_description (AtkAction *action,
1105 const gchar *description)
1107 GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1109 g_free (accessible->description);
1110 accessible->description = g_strdup (description);
1116 idle_do_action (gpointer data)
1118 GtkSwitchAccessible *accessible = data;
1122 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (data));
1123 sw = GTK_SWITCH (widget);
1125 accessible->action_idle = 0;
1127 if (widget == NULL ||
1128 !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1131 gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
1137 gtk_switch_action_do_action (AtkAction *action,
1140 GtkSwitchAccessible *accessible;
1143 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1147 if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1150 accessible = (GtkSwitchAccessible *)action;
1152 if (!accessible->action_idle)
1153 accessible->action_idle = gdk_threads_add_idle (idle_do_action, accessible);
1159 atk_action_interface_init (AtkActionIface *iface)
1161 iface->do_action = gtk_switch_action_do_action;
1162 iface->get_n_actions = gtk_switch_action_get_n_actions;
1163 iface->get_name = gtk_switch_action_get_name;
1164 iface->get_description = gtk_switch_action_get_description;
1165 iface->set_description = gtk_switch_action_set_description;