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);
428 gtk_switch_unrealize (GtkWidget *widget)
430 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
432 if (priv->event_window != NULL)
434 gdk_window_set_user_data (priv->event_window, NULL);
435 gdk_window_destroy (priv->event_window);
436 priv->event_window = NULL;
439 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
443 gtk_switch_map (GtkWidget *widget)
445 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
447 GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
449 if (priv->event_window)
450 gdk_window_show (priv->event_window);
454 gtk_switch_unmap (GtkWidget *widget)
456 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
458 if (priv->event_window)
459 gdk_window_hide (priv->event_window);
461 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
465 gtk_switch_paint_handle (GtkWidget *widget,
469 GtkStyleContext *context = gtk_widget_get_style_context (widget);
472 state = gtk_widget_get_state_flags (widget);
474 gtk_style_context_save (context);
475 gtk_style_context_set_state (context, state);
476 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
478 gtk_render_slider (context, cr,
480 box->width, box->height,
481 GTK_ORIENTATION_HORIZONTAL);
483 gtk_style_context_restore (context);
487 gtk_switch_draw (GtkWidget *widget,
490 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
491 GtkStyleContext *context;
495 gint label_x, label_y;
498 gint focus_width, focus_pad;
499 gint x, y, width, height;
501 gtk_widget_style_get (widget,
502 "focus-line-width", &focus_width,
503 "focus-padding", &focus_pad,
506 context = gtk_widget_get_style_context (widget);
507 state = gtk_widget_get_state_flags (widget);
510 state |= GTK_STATE_FLAG_ACTIVE;
512 gtk_style_context_get_padding (context, state, &padding);
516 width = gtk_widget_get_allocated_width (widget);
517 height = gtk_widget_get_allocated_height (widget);
519 if (gtk_widget_has_focus (widget))
520 gtk_render_focus (context, cr, x, y, width, height);
522 gtk_style_context_save (context);
523 gtk_style_context_set_state (context, state);
525 x += focus_width + focus_pad;
526 y += focus_width + focus_pad;
527 width -= 2 * (focus_width + focus_pad);
528 height -= 2 * (focus_width + focus_pad);
530 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
532 gtk_render_background (context, cr, x, y, width, height);
533 gtk_render_frame (context, cr, x, y, width, height);
535 /* XXX the +1/-1 it's pixel wriggling after checking with the default
538 handle.y = y + padding.top + 1;
539 handle.width = (width - padding.left - padding.right) / 2;
540 handle.height = (height - padding.top - padding.bottom) - 1;
542 /* Translators: if the "on" state label requires more than three
543 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
546 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
547 pango_layout_get_extents (layout, NULL, &rect);
548 pango_extents_to_pixels (&rect, NULL);
550 label_x = x + padding.left
551 + ((width / 2) - rect.width - padding.left - padding.right) / 2;
552 label_y = y + padding.top
553 + (height - rect.height - padding.top - padding.bottom) / 2;
555 gtk_render_layout (context, cr, label_x, label_y, layout);
557 g_object_unref (layout);
559 /* Translators: if the "off" state label requires more than three
560 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
562 layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
563 pango_layout_get_extents (layout, NULL, &rect);
564 pango_extents_to_pixels (&rect, NULL);
566 label_x = x + padding.left
568 + ((width / 2) - rect.width - padding.left - padding.right) / 2;
569 label_y = y + padding.top
570 + (height - rect.height - padding.top - padding.bottom) / 2;
572 gtk_render_layout (context, cr, label_x, label_y, layout);
574 g_object_unref (layout);
576 if (priv->is_dragging)
577 handle.x = x + priv->handle_x;
578 else if (priv->is_active)
579 handle.x = x + width - handle.width - padding.right;
581 handle.x = x + padding.left;
583 gtk_style_context_restore (context);
585 gtk_switch_paint_handle (widget, cr, &handle);
591 gtk_switch_get_accessible (GtkWidget *widget)
593 static gboolean first_time = TRUE;
595 if (G_UNLIKELY (first_time))
597 AtkObjectFactory *factory;
598 AtkRegistry *registry;
600 GType derived_atk_type;
602 /* Figure out whether accessibility is enabled by looking at the
603 * type of the accessible object which would be created for the
604 * parent type of GtkSwitch
606 derived_type = g_type_parent (GTK_TYPE_SWITCH);
608 registry = atk_get_default_registry ();
609 factory = atk_registry_get_factory (registry, derived_type);
610 derived_atk_type = atk_object_factory_get_accessible_type (factory);
611 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
612 atk_registry_set_factory_type (registry,
614 gtk_switch_accessible_factory_get_type ());
619 return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
623 gtk_switch_set_related_action (GtkSwitch *sw,
626 GtkSwitchPrivate *priv = sw->priv;
628 if (priv->action == action)
631 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
633 priv->action = action;
637 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
638 gboolean use_appearance)
640 GtkSwitchPrivate *priv = sw->priv;
642 if (priv->use_action_appearance != use_appearance)
644 priv->use_action_appearance = use_appearance;
646 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
651 gtk_switch_set_property (GObject *gobject,
656 GtkSwitch *sw = GTK_SWITCH (gobject);
661 gtk_switch_set_active (sw, g_value_get_boolean (value));
664 case PROP_RELATED_ACTION:
665 gtk_switch_set_related_action (sw, g_value_get_object (value));
668 case PROP_USE_ACTION_APPEARANCE:
669 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
673 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
678 gtk_switch_get_property (GObject *gobject,
683 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
688 g_value_set_boolean (value, priv->is_active);
691 case PROP_RELATED_ACTION:
692 g_value_set_object (value, priv->action);
695 case PROP_USE_ACTION_APPEARANCE:
696 g_value_set_boolean (value, priv->use_action_appearance);
700 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
705 gtk_switch_dispose (GObject *object)
707 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
711 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
715 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
719 gtk_switch_class_init (GtkSwitchClass *klass)
721 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
722 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
723 gpointer activatable_iface;
725 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
727 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
728 switch_props[PROP_RELATED_ACTION] =
729 g_param_spec_override ("related-action",
730 g_object_interface_find_property (activatable_iface,
733 switch_props[PROP_USE_ACTION_APPEARANCE] =
734 g_param_spec_override ("use-action-appearance",
735 g_object_interface_find_property (activatable_iface,
736 "use-action-appearance"));
741 * Whether the #GtkSwitch widget is in its on or off state.
743 switch_props[PROP_ACTIVE] =
744 g_param_spec_boolean ("active",
746 P_("Whether the switch is on or off"),
748 GTK_PARAM_READWRITE);
750 gobject_class->set_property = gtk_switch_set_property;
751 gobject_class->get_property = gtk_switch_get_property;
752 gobject_class->dispose = gtk_switch_dispose;
754 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
756 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
757 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
758 widget_class->size_allocate = gtk_switch_size_allocate;
759 widget_class->realize = gtk_switch_realize;
760 widget_class->unrealize = gtk_switch_unrealize;
761 widget_class->map = gtk_switch_map;
762 widget_class->unmap = gtk_switch_unmap;
763 widget_class->draw = gtk_switch_draw;
764 widget_class->button_press_event = gtk_switch_button_press;
765 widget_class->button_release_event = gtk_switch_button_release;
766 widget_class->motion_notify_event = gtk_switch_motion;
767 widget_class->enter_notify_event = gtk_switch_enter;
768 widget_class->leave_notify_event = gtk_switch_leave;
769 widget_class->key_release_event = gtk_switch_key_release;
770 widget_class->get_accessible = gtk_switch_get_accessible;
773 * GtkSwitch:slider-width:
775 * The minimum width of the #GtkSwitch handle, in pixels.
777 gtk_widget_class_install_style_property (widget_class,
778 g_param_spec_int ("slider-width",
780 P_("The minimum width of the handle"),
781 DEFAULT_SLIDER_WIDTH, G_MAXINT,
782 DEFAULT_SLIDER_WIDTH,
783 GTK_PARAM_READABLE));
787 gtk_switch_init (GtkSwitch *self)
789 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
790 self->priv->use_action_appearance = TRUE;
791 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
792 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
798 * Creates a new #GtkSwitch widget.
800 * Return value: the newly created #GtkSwitch instance
805 gtk_switch_new (void)
807 return g_object_new (GTK_TYPE_SWITCH, NULL);
811 * gtk_switch_set_active:
813 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
815 * Changes the state of @sw to the desired one.
820 gtk_switch_set_active (GtkSwitch *sw,
823 GtkSwitchPrivate *priv;
825 g_return_if_fail (GTK_IS_SWITCH (sw));
827 is_active = !!is_active;
831 if (priv->is_active != is_active)
833 AtkObject *accessible;
835 GtkStyleContext *context;
837 widget = GTK_WIDGET (sw);
838 priv->is_active = is_active;
840 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
843 gtk_action_activate (priv->action);
845 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
846 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
848 if (gtk_widget_get_realized (widget))
850 context = gtk_widget_get_style_context (widget);
851 gtk_style_context_notify_state_change (context,
852 gtk_widget_get_window (widget),
853 NULL, GTK_STATE_ACTIVE, is_active);
856 gtk_widget_queue_draw (GTK_WIDGET (sw));
861 * gtk_switch_get_active:
864 * Gets whether the #GtkSwitch is in its "on" or "off" state.
866 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
871 gtk_switch_get_active (GtkSwitch *sw)
873 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
875 return sw->priv->is_active;
879 gtk_switch_update (GtkActivatable *activatable,
881 const gchar *property_name)
883 if (strcmp (property_name, "visible") == 0)
885 if (gtk_action_is_visible (action))
886 gtk_widget_show (GTK_WIDGET (activatable));
888 gtk_widget_hide (GTK_WIDGET (activatable));
890 else if (strcmp (property_name, "sensitive") == 0)
892 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
894 else if (strcmp (property_name, "active") == 0)
896 gtk_action_block_activate (action);
897 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
898 gtk_action_unblock_activate (action);
903 gtk_switch_sync_action_properties (GtkActivatable *activatable,
909 if (gtk_action_is_visible (action))
910 gtk_widget_show (GTK_WIDGET (activatable));
912 gtk_widget_hide (GTK_WIDGET (activatable));
914 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
916 gtk_action_block_activate (action);
917 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
918 gtk_action_unblock_activate (action);
922 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
924 iface->update = gtk_switch_update;
925 iface->sync_action_properties = gtk_switch_sync_action_properties;
928 /* accessibility: object */
931 typedef struct _GtkSwitchAccessible GtkSwitchAccessible;
932 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
934 ATK_DEFINE_TYPE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET);
937 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
939 AtkStateSet *state_set;
942 state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
944 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
948 if (gtk_switch_get_active (GTK_SWITCH (widget)))
949 atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
955 _gtk_switch_accessible_initialize (AtkObject *accessible,
958 ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
960 atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
961 atk_object_set_name (accessible, C_("light switch widget", "Switch"));
962 atk_object_set_description (accessible, _("Switches between on and off states"));
966 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
968 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
970 atk_class->initialize = _gtk_switch_accessible_initialize;
971 atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
975 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
979 /* accessibility: factory */
981 typedef AtkObjectFactoryClass GtkSwitchAccessibleFactoryClass;
982 typedef AtkObjectFactory GtkSwitchAccessibleFactory;
984 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
985 gtk_switch_accessible_factory,
986 ATK_TYPE_OBJECT_FACTORY);
989 gtk_switch_accessible_factory_get_accessible_type (void)
991 return _gtk_switch_accessible_get_type ();
995 gtk_switch_accessible_factory_create_accessible (GObject *obj)
997 AtkObject *accessible;
999 accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1000 atk_object_initialize (accessible, obj);
1006 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1008 klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1009 klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1013 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)