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"
47 #include "gtkprivate.h"
48 #include "gtktoggleaction.h"
49 #include "gtkwidget.h"
50 #include "gtkmarshalers.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,
88 static guint signals[LAST_SIGNAL] = { 0 };
90 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
92 static GType gtk_switch_accessible_factory_get_type (void);
94 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
96 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
97 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
98 gtk_switch_activatable_interface_init));
101 gtk_switch_button_press (GtkWidget *widget,
102 GdkEventButton *event)
104 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
105 GtkAllocation allocation;
107 gtk_widget_get_allocation (widget, &allocation);
111 /* if the event occurred in the "off" area, then this
114 if (event->x <= allocation.width / 2)
116 priv->in_press = TRUE;
120 priv->offset = event->x - allocation.width / 2;
124 /* if the event occurred in the "on" area, then this
127 if (event->x >= allocation.width / 2)
129 priv->in_press = TRUE;
133 priv->offset = event->x;
136 priv->drag_start = event->x;
138 g_object_get (gtk_widget_get_settings (widget),
139 "gtk-dnd-drag-threshold", &priv->drag_threshold,
146 gtk_switch_motion (GtkWidget *widget,
147 GdkEventMotion *event)
149 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
151 /* if this is a direct toggle we don't handle motion */
155 if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
158 if (event->state & GDK_BUTTON1_MASK)
160 gint position = event->x - priv->offset;
161 GtkAllocation allocation;
162 GtkStyleContext *context;
166 context = gtk_widget_get_style_context (widget);
167 state = gtk_widget_get_state_flags (widget);
168 gtk_style_context_get_padding (context, state, &padding);
169 gtk_widget_get_allocation (widget, &allocation);
171 /* constrain the handle within the trough width */
172 if (position > (allocation.width / 2 - padding.right))
173 priv->handle_x = allocation.width / 2 - padding.right;
174 else if (position < padding.left)
175 priv->handle_x = padding.left;
177 priv->handle_x = position;
179 priv->is_dragging = TRUE;
181 /* we need to redraw the handle */
182 gtk_widget_queue_draw (widget);
191 gtk_switch_button_release (GtkWidget *widget,
192 GdkEventButton *event)
194 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
195 GtkAllocation allocation;
197 gtk_widget_get_allocation (widget, &allocation);
199 /* dragged toggles have a "soft" grab, so we don't care whether we
200 * are in the switch or not when the button is released; we do care
201 * for direct toggles, instead
203 if (!priv->is_dragging && !priv->in_switch)
209 priv->in_press = FALSE;
210 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
215 /* toggle the switch if the handle was clicked but a drag had not been
217 if (!priv->is_dragging && !priv->in_press)
219 gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
225 if (priv->is_dragging)
227 priv->is_dragging = FALSE;
229 /* if half the handle passed the middle of the switch, then we
230 * consider it to be on
232 if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
234 gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
235 priv->handle_x = allocation.width / 2;
239 gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
243 gtk_widget_queue_draw (widget);
252 gtk_switch_enter (GtkWidget *widget,
253 GdkEventCrossing *event)
255 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
257 if (event->window == priv->event_window)
258 priv->in_switch = TRUE;
264 gtk_switch_leave (GtkWidget *widget,
265 GdkEventCrossing *event)
267 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
269 if (event->window == priv->event_window)
270 priv->in_switch = FALSE;
276 gtk_switch_activate (GtkSwitch *sw)
278 GtkSwitchPrivate *priv = sw->priv;
280 gtk_switch_set_active (sw, !priv->is_active);
284 gtk_switch_get_preferred_width (GtkWidget *widget,
288 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
289 GtkStyleContext *context;
291 GtkBorder padding, border;
292 gint width, slider_width, focus_width, focus_pad;
294 PangoRectangle logical_rect;
296 context = gtk_widget_get_style_context (widget);
297 state = gtk_widget_get_state_flags (widget);
300 state |= GTK_STATE_FLAG_ACTIVE;
302 gtk_style_context_save (context);
304 gtk_style_context_set_state (context, state);
305 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
306 gtk_style_context_get_padding (context, state, &padding);
308 width = padding.left + padding.right;
310 gtk_style_context_restore (context);
312 gtk_widget_style_get (widget,
313 "slider-width", &slider_width,
314 "focus-line-width", &focus_width,
315 "focus-padding", &focus_pad,
318 width += 2 * (focus_width + focus_pad);
320 /* Translators: if the "on" state label requires more than three
321 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
324 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
325 pango_layout_get_extents (layout, NULL, &logical_rect);
326 pango_extents_to_pixels (&logical_rect, NULL);
327 width += MAX (logical_rect.width, slider_width);
329 /* Translators: if the "off" state label requires more than three
330 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
332 pango_layout_set_text (layout, C_("switch", "OFF"), -1);
333 pango_layout_get_extents (layout, NULL, &logical_rect);
334 pango_extents_to_pixels (&logical_rect, NULL);
335 width += MAX (logical_rect.width, slider_width);
337 g_object_unref (layout);
347 gtk_switch_get_preferred_height (GtkWidget *widget,
351 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
352 GtkStyleContext *context;
355 gint height, focus_width, focus_pad;
357 PangoRectangle logical_rect;
360 context = gtk_widget_get_style_context (widget);
361 state = gtk_widget_get_state_flags (widget);
364 state |= GTK_STATE_FLAG_ACTIVE;
366 gtk_style_context_save (context);
368 gtk_style_context_set_state (context, state);
369 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
370 gtk_style_context_get_padding (context, state, &padding);
372 height = padding.top + padding.bottom;
374 gtk_style_context_restore (context);
376 gtk_widget_style_get (widget,
377 "focus-line-width", &focus_width,
378 "focus-padding", &focus_pad,
381 height += 2 * (focus_width + focus_pad);
383 str = g_strdup_printf ("%s%s",
385 C_("switch", "OFF"));
387 layout = gtk_widget_create_pango_layout (widget, str);
388 pango_layout_get_extents (layout, NULL, &logical_rect);
389 pango_extents_to_pixels (&logical_rect, NULL);
390 height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
392 g_object_unref (layout);
403 gtk_switch_size_allocate (GtkWidget *widget,
404 GtkAllocation *allocation)
406 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
408 gtk_widget_set_allocation (widget, allocation);
410 if (gtk_widget_get_realized (widget))
411 gdk_window_move_resize (priv->event_window,
419 gtk_switch_realize (GtkWidget *widget)
421 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
422 GdkWindow *parent_window;
423 GdkWindowAttr attributes;
424 gint attributes_mask;
425 GtkAllocation allocation;
427 gtk_widget_set_realized (widget, TRUE);
428 parent_window = gtk_widget_get_parent_window (widget);
429 gtk_widget_set_window (widget, parent_window);
430 g_object_ref (parent_window);
432 gtk_widget_get_allocation (widget, &allocation);
434 attributes.window_type = GDK_WINDOW_CHILD;
435 attributes.wclass = GDK_INPUT_ONLY;
436 attributes.x = allocation.x;
437 attributes.y = allocation.y;
438 attributes.width = allocation.width;
439 attributes.height = allocation.height;
440 attributes.event_mask = gtk_widget_get_events (widget);
441 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
442 GDK_BUTTON_RELEASE_MASK |
443 GDK_BUTTON1_MOTION_MASK |
444 GDK_POINTER_MOTION_HINT_MASK |
445 GDK_POINTER_MOTION_MASK |
446 GDK_ENTER_NOTIFY_MASK |
447 GDK_LEAVE_NOTIFY_MASK);
448 attributes_mask = GDK_WA_X | GDK_WA_Y;
450 priv->event_window = gdk_window_new (parent_window,
453 gdk_window_set_user_data (priv->event_window, widget);
457 gtk_switch_unrealize (GtkWidget *widget)
459 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
461 if (priv->event_window != NULL)
463 gdk_window_set_user_data (priv->event_window, NULL);
464 gdk_window_destroy (priv->event_window);
465 priv->event_window = NULL;
468 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
472 gtk_switch_map (GtkWidget *widget)
474 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
476 GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
478 if (priv->event_window)
479 gdk_window_show (priv->event_window);
483 gtk_switch_unmap (GtkWidget *widget)
485 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
487 if (priv->event_window)
488 gdk_window_hide (priv->event_window);
490 GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
494 gtk_switch_paint_handle (GtkWidget *widget,
498 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
499 GtkStyleContext *context = gtk_widget_get_style_context (widget);
502 state = gtk_widget_get_state_flags (widget);
505 state |= GTK_STATE_FLAG_ACTIVE;
507 gtk_style_context_save (context);
508 gtk_style_context_set_state (context, state);
509 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
511 gtk_render_slider (context, cr,
513 box->width, box->height,
514 GTK_ORIENTATION_HORIZONTAL);
516 gtk_style_context_restore (context);
520 gtk_switch_draw (GtkWidget *widget,
523 GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
524 GtkStyleContext *context;
528 gint label_x, label_y;
531 gint focus_width, focus_pad;
532 gint x, y, width, height;
534 gtk_widget_style_get (widget,
535 "focus-line-width", &focus_width,
536 "focus-padding", &focus_pad,
539 context = gtk_widget_get_style_context (widget);
540 state = gtk_widget_get_state_flags (widget);
543 state |= GTK_STATE_FLAG_ACTIVE;
545 gtk_style_context_save (context);
547 gtk_style_context_set_state (context, state);
548 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
550 gtk_style_context_get_padding (context, state, &padding);
552 gtk_style_context_restore (context);
556 width = gtk_widget_get_allocated_width (widget);
557 height = gtk_widget_get_allocated_height (widget);
559 if (gtk_widget_has_focus (widget))
560 gtk_render_focus (context, cr, x, y, width, height);
562 x += focus_width + focus_pad;
563 y += focus_width + focus_pad;
564 width -= 2 * (focus_width + focus_pad);
565 height -= 2 * (focus_width + focus_pad);
567 gtk_style_context_save (context);
568 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
569 gtk_style_context_set_state (context, state);
571 gtk_render_background (context, cr, x, y, width, height);
572 gtk_render_frame (context, cr, x, y, width, height);
574 width -= padding.left + padding.right;
575 height -= padding.top + padding.bottom;
581 handle.width = width / 2;
582 handle.height = height;
584 /* Translators: if the "on" state label requires more than three
585 * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
588 layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
589 pango_layout_get_extents (layout, NULL, &rect);
590 pango_extents_to_pixels (&rect, NULL);
592 label_x = x + ((width / 2) - rect.width) / 2;
593 label_y = y + (height - rect.height) / 2;
595 gtk_render_layout (context, cr, label_x, label_y, layout);
597 g_object_unref (layout);
599 /* Translators: if the "off" state label requires more than three
600 * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
602 layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
603 pango_layout_get_extents (layout, NULL, &rect);
604 pango_extents_to_pixels (&rect, NULL);
606 label_x = x + (width / 2) + ((width / 2) - rect.width) / 2;
607 label_y = y + (height - rect.height) / 2;
609 gtk_render_layout (context, cr, label_x, label_y, layout);
611 g_object_unref (layout);
613 if (priv->is_dragging)
614 handle.x = x + priv->handle_x;
615 else if (priv->is_active)
616 handle.x = x + width - handle.width;
620 gtk_style_context_restore (context);
622 gtk_switch_paint_handle (widget, cr, &handle);
628 gtk_switch_get_accessible (GtkWidget *widget)
630 static gboolean first_time = TRUE;
632 if (G_UNLIKELY (first_time))
634 _gtk_accessible_set_factory_type (GTK_TYPE_SWITCH,
635 gtk_switch_accessible_factory_get_type ());
639 return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
643 gtk_switch_set_related_action (GtkSwitch *sw,
646 GtkSwitchPrivate *priv = sw->priv;
648 if (priv->action == action)
651 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
653 priv->action = action;
657 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
658 gboolean use_appearance)
660 GtkSwitchPrivate *priv = sw->priv;
662 if (priv->use_action_appearance != use_appearance)
664 priv->use_action_appearance = use_appearance;
666 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
671 gtk_switch_set_property (GObject *gobject,
676 GtkSwitch *sw = GTK_SWITCH (gobject);
681 gtk_switch_set_active (sw, g_value_get_boolean (value));
684 case PROP_RELATED_ACTION:
685 gtk_switch_set_related_action (sw, g_value_get_object (value));
688 case PROP_USE_ACTION_APPEARANCE:
689 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
693 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
698 gtk_switch_get_property (GObject *gobject,
703 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
708 g_value_set_boolean (value, priv->is_active);
711 case PROP_RELATED_ACTION:
712 g_value_set_object (value, priv->action);
715 case PROP_USE_ACTION_APPEARANCE:
716 g_value_set_boolean (value, priv->use_action_appearance);
720 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
725 gtk_switch_dispose (GObject *object)
727 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
731 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
735 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
739 gtk_switch_class_init (GtkSwitchClass *klass)
741 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
742 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
743 gpointer activatable_iface;
745 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
747 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
748 switch_props[PROP_RELATED_ACTION] =
749 g_param_spec_override ("related-action",
750 g_object_interface_find_property (activatable_iface,
753 switch_props[PROP_USE_ACTION_APPEARANCE] =
754 g_param_spec_override ("use-action-appearance",
755 g_object_interface_find_property (activatable_iface,
756 "use-action-appearance"));
761 * Whether the #GtkSwitch widget is in its on or off state.
763 switch_props[PROP_ACTIVE] =
764 g_param_spec_boolean ("active",
766 P_("Whether the switch is on or off"),
768 GTK_PARAM_READWRITE);
770 gobject_class->set_property = gtk_switch_set_property;
771 gobject_class->get_property = gtk_switch_get_property;
772 gobject_class->dispose = gtk_switch_dispose;
774 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
776 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
777 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
778 widget_class->size_allocate = gtk_switch_size_allocate;
779 widget_class->realize = gtk_switch_realize;
780 widget_class->unrealize = gtk_switch_unrealize;
781 widget_class->map = gtk_switch_map;
782 widget_class->unmap = gtk_switch_unmap;
783 widget_class->draw = gtk_switch_draw;
784 widget_class->button_press_event = gtk_switch_button_press;
785 widget_class->button_release_event = gtk_switch_button_release;
786 widget_class->motion_notify_event = gtk_switch_motion;
787 widget_class->enter_notify_event = gtk_switch_enter;
788 widget_class->leave_notify_event = gtk_switch_leave;
789 widget_class->get_accessible = gtk_switch_get_accessible;
791 klass->activate = gtk_switch_activate;
794 * GtkSwitch:slider-width:
796 * The minimum width of the #GtkSwitch handle, in pixels.
798 gtk_widget_class_install_style_property (widget_class,
799 g_param_spec_int ("slider-width",
801 P_("The minimum width of the handle"),
802 DEFAULT_SLIDER_WIDTH, G_MAXINT,
803 DEFAULT_SLIDER_WIDTH,
804 GTK_PARAM_READABLE));
807 * GtkSwitch::activate:
808 * @widget: the object which received the signal.
810 * The ::activate signal on GtkSwitch is an action signal and
811 * emitting it causes the switch to animate.
812 * Applications should never connect to this signal, but use the
813 * notify::active signal.
816 g_signal_new (I_("activate"),
817 G_OBJECT_CLASS_TYPE (gobject_class),
818 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
819 G_STRUCT_OFFSET (GtkSwitchClass, activate),
821 _gtk_marshal_VOID__VOID,
823 widget_class->activate_signal = signals[ACTIVATE];
828 gtk_switch_init (GtkSwitch *self)
830 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
831 self->priv->use_action_appearance = TRUE;
832 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
833 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
839 * Creates a new #GtkSwitch widget.
841 * Return value: the newly created #GtkSwitch instance
846 gtk_switch_new (void)
848 return g_object_new (GTK_TYPE_SWITCH, NULL);
852 * gtk_switch_set_active:
854 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
856 * Changes the state of @sw to the desired one.
861 gtk_switch_set_active (GtkSwitch *sw,
864 GtkSwitchPrivate *priv;
866 g_return_if_fail (GTK_IS_SWITCH (sw));
868 is_active = !!is_active;
872 if (priv->is_active != is_active)
874 AtkObject *accessible;
876 GtkStyleContext *context;
878 widget = GTK_WIDGET (sw);
879 priv->is_active = is_active;
881 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
884 gtk_action_activate (priv->action);
886 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
887 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
889 if (gtk_widget_get_realized (widget))
891 context = gtk_widget_get_style_context (widget);
892 gtk_style_context_notify_state_change (context,
893 gtk_widget_get_window (widget),
894 NULL, GTK_STATE_ACTIVE, is_active);
897 gtk_widget_queue_draw (GTK_WIDGET (sw));
902 * gtk_switch_get_active:
905 * Gets whether the #GtkSwitch is in its "on" or "off" state.
907 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
912 gtk_switch_get_active (GtkSwitch *sw)
914 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
916 return sw->priv->is_active;
920 gtk_switch_update (GtkActivatable *activatable,
922 const gchar *property_name)
924 if (strcmp (property_name, "visible") == 0)
926 if (gtk_action_is_visible (action))
927 gtk_widget_show (GTK_WIDGET (activatable));
929 gtk_widget_hide (GTK_WIDGET (activatable));
931 else if (strcmp (property_name, "sensitive") == 0)
933 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
935 else if (strcmp (property_name, "active") == 0)
937 gtk_action_block_activate (action);
938 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
939 gtk_action_unblock_activate (action);
944 gtk_switch_sync_action_properties (GtkActivatable *activatable,
950 if (gtk_action_is_visible (action))
951 gtk_widget_show (GTK_WIDGET (activatable));
953 gtk_widget_hide (GTK_WIDGET (activatable));
955 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
957 gtk_action_block_activate (action);
958 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
959 gtk_action_unblock_activate (action);
963 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
965 iface->update = gtk_switch_update;
966 iface->sync_action_properties = gtk_switch_sync_action_properties;
969 /* accessibility: object */
971 typedef struct _GtkSwitchAccessible GtkSwitchAccessible;
972 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
974 struct _GtkSwitchAccessible
976 GtkAccessible object;
982 static void atk_action_interface_init (AtkActionIface *iface);
984 ATK_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_SWITCH,
985 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
988 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
990 AtkStateSet *state_set;
993 state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
995 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
999 if (gtk_switch_get_active (GTK_SWITCH (widget)))
1000 atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
1006 gtk_switch_accessible_finalize (GObject *obj)
1008 GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)obj;
1010 g_free (accessible->description);
1012 if (accessible->action_idle)
1013 g_source_remove (accessible->action_idle);
1015 G_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->finalize (obj);
1019 _gtk_switch_accessible_initialize (AtkObject *accessible,
1022 ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
1024 atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
1025 atk_object_set_name (accessible, C_("light switch widget", "Switch"));
1026 atk_object_set_description (accessible, _("Switches between on and off states"));
1030 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
1032 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1033 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
1035 object_class->finalize = gtk_switch_accessible_finalize;
1037 atk_class->initialize = _gtk_switch_accessible_initialize;
1038 atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
1042 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
1044 self->description = NULL;
1045 self->action_idle = 0;
1048 /* accessibility: action interface */
1051 gtk_switch_action_get_n_actions (AtkAction *action)
1056 static const gchar *
1057 gtk_switch_action_get_name (AtkAction *action,
1063 static const gchar *
1064 gtk_switch_action_get_description (AtkAction *action,
1067 GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1069 return accessible->description;
1073 gtk_switch_action_set_description (AtkAction *action,
1075 const gchar *description)
1077 GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1079 g_free (accessible->description);
1080 accessible->description = g_strdup (description);
1086 idle_do_action (gpointer data)
1088 GtkSwitchAccessible *accessible = data;
1092 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (data));
1093 sw = GTK_SWITCH (widget);
1095 accessible->action_idle = 0;
1097 if (widget == NULL ||
1098 !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1101 gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
1107 gtk_switch_action_do_action (AtkAction *action,
1110 GtkSwitchAccessible *accessible;
1113 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1117 if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1120 accessible = (GtkSwitchAccessible *)action;
1122 if (!accessible->action_idle)
1123 accessible->action_idle = gdk_threads_add_idle (idle_do_action, accessible);
1129 atk_action_interface_init (AtkActionIface *iface)
1131 iface->do_action = gtk_switch_action_do_action;
1132 iface->get_n_actions = gtk_switch_action_get_n_actions;
1133 iface->get_name = gtk_switch_action_get_name;
1134 iface->get_description = gtk_switch_action_get_description;
1135 iface->set_description = gtk_switch_action_set_description;
1138 /* accessibility: factory */
1140 typedef AtkObjectFactoryClass GtkSwitchAccessibleFactoryClass;
1141 typedef AtkObjectFactory GtkSwitchAccessibleFactory;
1143 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
1144 gtk_switch_accessible_factory,
1145 ATK_TYPE_OBJECT_FACTORY);
1148 gtk_switch_accessible_factory_get_accessible_type (void)
1150 return _gtk_switch_accessible_get_type ();
1154 gtk_switch_accessible_factory_create_accessible (GObject *obj)
1156 AtkObject *accessible;
1158 accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1159 atk_object_initialize (accessible, obj);
1165 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1167 klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1168 klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1172 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)