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 <gdk/gdkkeysyms.h>
45 #include "gtkaccessible.h"
46 #include "gtkactivatable.h"
49 #include "gtkprivate.h"
50 #include "gtktoggleaction.h"
51 #include "gtkwidget.h"
53 #define DEFAULT_SLIDER_WIDTH (36)
54 #define DEFAULT_SLIDER_HEIGHT (22)
56 struct _GtkSwitchPrivate
58 GdkWindow *event_window;
67 guint is_dragging : 1;
70 guint use_action_appearance : 1;
78 PROP_USE_ACTION_APPEARANCE,
82 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
84 static GType gtk_switch_accessible_factory_get_type (void);
86 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
88 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
89 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
90 gtk_switch_activatable_interface_init));
93 gtk_switch_button_press (GtkWidget *widget,
94 GdkEventButton *event)
96 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
97 GtkAllocation allocation;
99 gtk_widget_get_allocation (widget, &allocation);
103 /* if the event occurred in the "off" area, then this
106 if (event->x <= allocation.width / 2)
108 priv->in_press = TRUE;
112 priv->offset = event->x - allocation.width / 2;
116 /* if the event occurred in the "on" area, then this
119 if (event->x >= allocation.width / 2)
121 priv->in_press = TRUE;
125 priv->offset = event->x;
128 priv->drag_start = event->x;
130 g_object_get (gtk_widget_get_settings (widget),
131 "gtk-dnd-drag-threshold", &priv->drag_threshold,
138 gtk_switch_motion (GtkWidget *widget,
139 GdkEventMotion *event)
141 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
143 /* if this is a direct toggle we don't handle motion */
147 if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
150 if (event->state & GDK_BUTTON1_MASK)
152 gint position = event->x - priv->offset;
153 GtkAllocation allocation;
154 GtkStyleContext *context;
158 context = gtk_widget_get_style_context (widget);
159 state = gtk_widget_get_state_flags (widget);
160 gtk_style_context_get_padding (context, state, &padding);
161 gtk_widget_get_allocation (widget, &allocation);
163 /* constrain the handle within the trough width */
164 if (position > (allocation.width / 2 - padding.right))
165 priv->handle_x = allocation.width / 2 - padding.right;
166 else if (position < padding.left)
167 priv->handle_x = padding.left;
169 priv->handle_x = position;
171 priv->is_dragging = TRUE;
173 /* we need to redraw the handle */
174 gtk_widget_queue_draw (widget);
183 gtk_switch_button_release (GtkWidget *widget,
184 GdkEventButton *event)
186 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
187 GtkAllocation allocation;
189 gtk_widget_get_allocation (widget, &allocation);
191 /* dragged toggles have a "soft" grab, so we don't care whether we
192 * are in the switch or not when the button is released; we do care
193 * for direct toggles, instead
195 if (!priv->is_dragging && !priv->in_switch)
201 priv->in_press = FALSE;
202 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
208 if (priv->is_dragging)
210 priv->is_dragging = FALSE;
212 /* if half the handle passed the middle of the switch, then we
213 * consider it to be on
215 if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
217 gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
218 priv->handle_x = allocation.width / 2;
222 gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
226 gtk_widget_queue_draw (widget);
235 gtk_switch_enter (GtkWidget *widget,
236 GdkEventCrossing *event)
238 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
240 if (event->window == priv->event_window)
241 priv->in_switch = TRUE;
247 gtk_switch_leave (GtkWidget *widget,
248 GdkEventCrossing *event)
250 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
252 if (event->window == priv->event_window)
253 priv->in_switch = FALSE;
259 gtk_switch_key_release (GtkWidget *widget,
262 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
264 if (event->keyval == GDK_KEY_Return ||
265 event->keyval == GDK_KEY_KP_Enter ||
266 event->keyval == GDK_KEY_ISO_Enter ||
267 event->keyval == GDK_KEY_space ||
268 event->keyval == GDK_KEY_KP_Space)
270 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
277 gtk_switch_get_preferred_width (GtkWidget *widget,
281 GtkStyleContext *context;
284 gint width, slider_width, focus_width, focus_pad;
286 PangoRectangle logical_rect;
288 context = gtk_widget_get_style_context (widget);
289 state = gtk_widget_get_state_flags (widget);
290 gtk_style_context_get_padding (context, state, &padding);
292 width = padding.left + padding.right;
294 gtk_widget_style_get (widget,
295 "slider-width", &slider_width,
296 "focus-line-width", &focus_width,
297 "focus-padding", &focus_pad,
300 width += 2 * (focus_width + focus_pad);
302 /* Translators: if the "on" state label requires more than three
303 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
306 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
307 pango_layout_get_extents (layout, NULL, &logical_rect);
308 pango_extents_to_pixels (&logical_rect, NULL);
309 width += MAX (logical_rect.width, slider_width);
311 /* Translators: if the "off" state label requires more than three
312 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
314 pango_layout_set_text (layout, C_("switch", "OFF"), -1);
315 pango_layout_get_extents (layout, NULL, &logical_rect);
316 pango_extents_to_pixels (&logical_rect, NULL);
317 width += MAX (logical_rect.width, slider_width);
319 g_object_unref (layout);
329 gtk_switch_get_preferred_height (GtkWidget *widget,
333 GtkStyleContext *context;
336 gint height, focus_width, focus_pad;
338 PangoRectangle logical_rect;
341 context = gtk_widget_get_style_context (widget);
342 state = gtk_widget_get_state_flags (widget);
343 gtk_style_context_get_padding (context, state, &padding);
345 height = padding.top + padding.bottom;
347 gtk_widget_style_get (widget,
348 "focus-line-width", &focus_width,
349 "focus-padding", &focus_pad,
352 height += 2 * (focus_width + focus_pad);
354 str = g_strdup_printf ("%s%s",
356 C_("switch", "OFF"));
358 layout = gtk_widget_create_pango_layout (widget, str);
359 pango_layout_get_extents (layout, NULL, &logical_rect);
360 pango_extents_to_pixels (&logical_rect, NULL);
361 height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
363 g_object_unref (layout);
374 gtk_switch_size_allocate (GtkWidget *widget,
375 GtkAllocation *allocation)
377 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
379 gtk_widget_set_allocation (widget, allocation);
381 if (gtk_widget_get_realized (widget))
382 gdk_window_move_resize (priv->event_window,
390 gtk_switch_realize (GtkWidget *widget)
392 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
393 GdkWindow *parent_window;
394 GdkWindowAttr attributes;
395 gint attributes_mask;
396 GtkAllocation allocation;
398 gtk_widget_set_realized (widget, TRUE);
399 parent_window = gtk_widget_get_parent_window (widget);
400 gtk_widget_set_window (widget, parent_window);
401 g_object_ref (parent_window);
403 gtk_widget_get_allocation (widget, &allocation);
405 attributes.window_type = GDK_WINDOW_CHILD;
406 attributes.wclass = GDK_INPUT_ONLY;
407 attributes.x = allocation.x;
408 attributes.y = allocation.y;
409 attributes.width = allocation.width;
410 attributes.height = allocation.height;
411 attributes.event_mask = gtk_widget_get_events (widget);
412 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
413 GDK_BUTTON_RELEASE_MASK |
414 GDK_BUTTON1_MOTION_MASK |
415 GDK_POINTER_MOTION_HINT_MASK |
416 GDK_POINTER_MOTION_MASK |
417 GDK_ENTER_NOTIFY_MASK |
418 GDK_LEAVE_NOTIFY_MASK);
419 attributes_mask = GDK_WA_X | GDK_WA_Y;
421 priv->event_window = gdk_window_new (parent_window,
424 gdk_window_set_user_data (priv->event_window, widget);
426 gtk_widget_style_attach (widget);
430 gtk_switch_unrealize (GtkWidget *widget)
432 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
434 if (priv->event_window != NULL)
436 gdk_window_set_user_data (priv->event_window, NULL);
437 gdk_window_destroy (priv->event_window);
438 priv->event_window = NULL;
441 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
445 gtk_switch_map (GtkWidget *widget)
447 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
449 GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
451 if (priv->event_window)
452 gdk_window_show (priv->event_window);
456 gtk_switch_unmap (GtkWidget *widget)
458 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
460 if (priv->event_window)
461 gdk_window_hide (priv->event_window);
463 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
467 gtk_switch_paint_handle (GtkWidget *widget,
471 GtkStyleContext *context = gtk_widget_get_style_context (widget);
474 state = gtk_widget_get_state_flags (widget);
476 gtk_style_context_save (context);
477 gtk_style_context_set_state (context, state);
478 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
480 gtk_render_slider (context, cr,
482 box->width, box->height,
483 GTK_ORIENTATION_HORIZONTAL);
485 gtk_style_context_restore (context);
489 gtk_switch_draw (GtkWidget *widget,
492 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
493 GtkStyleContext *context;
497 gint label_x, label_y;
500 gint focus_width, focus_pad;
501 gint x, y, width, height;
503 gtk_widget_style_get (widget,
504 "focus-line-width", &focus_width,
505 "focus-padding", &focus_pad,
508 context = gtk_widget_get_style_context (widget);
509 state = gtk_widget_get_state_flags (widget);
512 state |= GTK_STATE_FLAG_ACTIVE;
514 gtk_style_context_get_padding (context, state, &padding);
518 width = gtk_widget_get_allocated_width (widget);
519 height = gtk_widget_get_allocated_height (widget);
521 if (gtk_widget_has_focus (widget))
522 gtk_render_focus (context, cr, x, y, width, height);
524 gtk_style_context_save (context);
525 gtk_style_context_set_state (context, state);
527 x += focus_width + focus_pad;
528 y += focus_width + focus_pad;
529 width -= 2 * (focus_width + focus_pad);
530 height -= 2 * (focus_width + focus_pad);
532 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
534 gtk_render_background (context, cr, x, y, width, height);
535 gtk_render_frame (context, cr, x, y, width, height);
537 /* XXX the +1/-1 it's pixel wriggling after checking with the default
540 handle.y = y + padding.top + 1;
541 handle.width = (width - padding.left - padding.right) / 2;
542 handle.height = (height - padding.top - padding.bottom) - 1;
544 /* Translators: if the "on" state label requires more than three
545 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
548 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
549 pango_layout_get_extents (layout, NULL, &rect);
550 pango_extents_to_pixels (&rect, NULL);
552 label_x = x + padding.left
553 + ((width / 2) - rect.width - padding.left - padding.right) / 2;
554 label_y = y + padding.top
555 + (height - rect.height - padding.top - padding.bottom) / 2;
557 gtk_render_layout (context, cr, label_x, label_y, layout);
559 g_object_unref (layout);
561 /* Translators: if the "off" state label requires more than three
562 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
564 layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
565 pango_layout_get_extents (layout, NULL, &rect);
566 pango_extents_to_pixels (&rect, NULL);
568 label_x = x + padding.left
570 + ((width / 2) - rect.width - padding.left - padding.right) / 2;
571 label_y = y + padding.top
572 + (height - rect.height - padding.top - padding.bottom) / 2;
574 gtk_render_layout (context, cr, label_x, label_y, layout);
576 g_object_unref (layout);
578 if (priv->is_dragging)
579 handle.x = x + priv->handle_x;
580 else if (priv->is_active)
581 handle.x = x + width - handle.width - padding.right;
583 handle.x = x + padding.left;
585 gtk_style_context_restore (context);
587 gtk_switch_paint_handle (widget, cr, &handle);
593 gtk_switch_get_accessible (GtkWidget *widget)
595 static gboolean first_time = TRUE;
597 if (G_UNLIKELY (first_time))
599 AtkObjectFactory *factory;
600 AtkRegistry *registry;
602 GType derived_atk_type;
604 /* Figure out whether accessibility is enabled by looking at the
605 * type of the accessible object which would be created for the
606 * parent type of GtkSwitch
608 derived_type = g_type_parent (GTK_TYPE_SWITCH);
610 registry = atk_get_default_registry ();
611 factory = atk_registry_get_factory (registry, derived_type);
612 derived_atk_type = atk_object_factory_get_accessible_type (factory);
613 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
614 atk_registry_set_factory_type (registry,
616 gtk_switch_accessible_factory_get_type ());
621 return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
625 gtk_switch_set_related_action (GtkSwitch *sw,
628 GtkSwitchPrivate *priv = sw->priv;
630 if (priv->action == action)
633 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
635 priv->action = action;
639 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
640 gboolean use_appearance)
642 GtkSwitchPrivate *priv = sw->priv;
644 if (priv->use_action_appearance != use_appearance)
646 priv->use_action_appearance = use_appearance;
648 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
653 gtk_switch_set_property (GObject *gobject,
658 GtkSwitch *sw = GTK_SWITCH (gobject);
663 gtk_switch_set_active (sw, g_value_get_boolean (value));
666 case PROP_RELATED_ACTION:
667 gtk_switch_set_related_action (sw, g_value_get_object (value));
670 case PROP_USE_ACTION_APPEARANCE:
671 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
675 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
680 gtk_switch_get_property (GObject *gobject,
685 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
690 g_value_set_boolean (value, priv->is_active);
693 case PROP_RELATED_ACTION:
694 g_value_set_object (value, priv->action);
697 case PROP_USE_ACTION_APPEARANCE:
698 g_value_set_boolean (value, priv->use_action_appearance);
702 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
707 gtk_switch_dispose (GObject *object)
709 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
713 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
717 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
721 gtk_switch_class_init (GtkSwitchClass *klass)
723 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
724 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
725 gpointer activatable_iface;
727 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
729 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
730 switch_props[PROP_RELATED_ACTION] =
731 g_param_spec_override ("related-action",
732 g_object_interface_find_property (activatable_iface,
735 switch_props[PROP_USE_ACTION_APPEARANCE] =
736 g_param_spec_override ("use-action-appearance",
737 g_object_interface_find_property (activatable_iface,
738 "use-action-appearance"));
743 * Whether the #GtkSwitch widget is in its on or off state.
745 switch_props[PROP_ACTIVE] =
746 g_param_spec_boolean ("active",
748 P_("Whether the switch is on or off"),
750 GTK_PARAM_READWRITE);
752 gobject_class->set_property = gtk_switch_set_property;
753 gobject_class->get_property = gtk_switch_get_property;
754 gobject_class->dispose = gtk_switch_dispose;
756 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
758 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
759 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
760 widget_class->size_allocate = gtk_switch_size_allocate;
761 widget_class->realize = gtk_switch_realize;
762 widget_class->unrealize = gtk_switch_unrealize;
763 widget_class->map = gtk_switch_map;
764 widget_class->unmap = gtk_switch_unmap;
765 widget_class->draw = gtk_switch_draw;
766 widget_class->button_press_event = gtk_switch_button_press;
767 widget_class->button_release_event = gtk_switch_button_release;
768 widget_class->motion_notify_event = gtk_switch_motion;
769 widget_class->enter_notify_event = gtk_switch_enter;
770 widget_class->leave_notify_event = gtk_switch_leave;
771 widget_class->key_release_event = gtk_switch_key_release;
772 widget_class->get_accessible = gtk_switch_get_accessible;
775 * GtkSwitch:slider-width:
777 * The minimum width of the #GtkSwitch handle, in pixels.
779 gtk_widget_class_install_style_property (widget_class,
780 g_param_spec_int ("slider-width",
782 P_("The minimum width of the handle"),
783 DEFAULT_SLIDER_WIDTH, G_MAXINT,
784 DEFAULT_SLIDER_WIDTH,
785 GTK_PARAM_READABLE));
789 gtk_switch_init (GtkSwitch *self)
791 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
792 self->priv->use_action_appearance = TRUE;
793 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
794 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
800 * Creates a new #GtkSwitch widget.
802 * Return value: the newly created #GtkSwitch instance
807 gtk_switch_new (void)
809 return g_object_new (GTK_TYPE_SWITCH, NULL);
813 * gtk_switch_set_active:
815 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
817 * Changes the state of @sw to the desired one.
822 gtk_switch_set_active (GtkSwitch *sw,
825 GtkSwitchPrivate *priv;
827 g_return_if_fail (GTK_IS_SWITCH (sw));
829 is_active = !!is_active;
833 if (priv->is_active != is_active)
835 AtkObject *accessible;
837 GtkStyleContext *context;
839 widget = GTK_WIDGET (sw);
840 priv->is_active = is_active;
842 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
845 gtk_action_activate (priv->action);
847 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
848 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
850 if (gtk_widget_get_realized (widget))
852 context = gtk_widget_get_style_context (widget);
853 gtk_style_context_notify_state_change (context,
854 gtk_widget_get_window (widget),
855 NULL, GTK_STATE_ACTIVE, is_active);
858 gtk_widget_queue_draw (GTK_WIDGET (sw));
863 * gtk_switch_get_active:
866 * Gets whether the #GtkSwitch is in its "on" or "off" state.
868 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
873 gtk_switch_get_active (GtkSwitch *sw)
875 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
877 return sw->priv->is_active;
881 gtk_switch_update (GtkActivatable *activatable,
883 const gchar *property_name)
885 if (strcmp (property_name, "visible") == 0)
887 if (gtk_action_is_visible (action))
888 gtk_widget_show (GTK_WIDGET (activatable));
890 gtk_widget_hide (GTK_WIDGET (activatable));
892 else if (strcmp (property_name, "sensitive") == 0)
894 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
896 else if (strcmp (property_name, "active") == 0)
898 gtk_action_block_activate (action);
899 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
900 gtk_action_unblock_activate (action);
905 gtk_switch_sync_action_properties (GtkActivatable *activatable,
911 if (gtk_action_is_visible (action))
912 gtk_widget_show (GTK_WIDGET (activatable));
914 gtk_widget_hide (GTK_WIDGET (activatable));
916 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
918 gtk_action_block_activate (action);
919 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
920 gtk_action_unblock_activate (action);
924 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
926 iface->update = gtk_switch_update;
927 iface->sync_action_properties = gtk_switch_sync_action_properties;
930 /* accessibility: object */
933 typedef struct _GtkSwitchAccessible GtkSwitchAccessible;
934 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
936 ATK_DEFINE_TYPE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET);
939 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
941 AtkStateSet *state_set;
944 state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
946 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
950 if (gtk_switch_get_active (GTK_SWITCH (widget)))
951 atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
957 _gtk_switch_accessible_initialize (AtkObject *accessible,
960 ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
962 atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
963 atk_object_set_name (accessible, C_("light switch widget", "Switch"));
964 atk_object_set_description (accessible, _("Switches between on and off states"));
968 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
970 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
972 atk_class->initialize = _gtk_switch_accessible_initialize;
973 atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
977 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
981 /* accessibility: factory */
983 typedef AtkObjectFactoryClass GtkSwitchAccessibleFactoryClass;
984 typedef AtkObjectFactory GtkSwitchAccessibleFactory;
986 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
987 gtk_switch_accessible_factory,
988 ATK_TYPE_OBJECT_FACTORY);
991 gtk_switch_accessible_factory_get_accessible_type (void)
993 return _gtk_switch_accessible_get_type ();
997 gtk_switch_accessible_factory_create_accessible (GObject *obj)
999 AtkObject *accessible;
1001 accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1002 atk_object_initialize (accessible, obj);
1008 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1010 klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1011 klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1015 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)