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"
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 static GType gtk_switch_accessible_factory_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_get_accessible (GtkWidget *widget)
666 static gboolean first_time = TRUE;
668 if (G_UNLIKELY (first_time))
670 _gtk_accessible_set_factory_type (GTK_TYPE_SWITCH,
671 gtk_switch_accessible_factory_get_type ());
675 return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
679 gtk_switch_set_related_action (GtkSwitch *sw,
682 GtkSwitchPrivate *priv = sw->priv;
684 if (priv->action == action)
687 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
689 priv->action = action;
693 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
694 gboolean use_appearance)
696 GtkSwitchPrivate *priv = sw->priv;
698 if (priv->use_action_appearance != use_appearance)
700 priv->use_action_appearance = use_appearance;
702 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
707 gtk_switch_set_property (GObject *gobject,
712 GtkSwitch *sw = GTK_SWITCH (gobject);
717 gtk_switch_set_active (sw, g_value_get_boolean (value));
720 case PROP_RELATED_ACTION:
721 gtk_switch_set_related_action (sw, g_value_get_object (value));
724 case PROP_USE_ACTION_APPEARANCE:
725 gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
729 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
734 gtk_switch_get_property (GObject *gobject,
739 GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
744 g_value_set_boolean (value, priv->is_active);
747 case PROP_RELATED_ACTION:
748 g_value_set_object (value, priv->action);
751 case PROP_USE_ACTION_APPEARANCE:
752 g_value_set_boolean (value, priv->use_action_appearance);
756 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
761 gtk_switch_dispose (GObject *object)
763 GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
767 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
771 G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
775 gtk_switch_class_init (GtkSwitchClass *klass)
777 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
778 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
779 gpointer activatable_iface;
781 g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
783 activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
784 switch_props[PROP_RELATED_ACTION] =
785 g_param_spec_override ("related-action",
786 g_object_interface_find_property (activatable_iface,
789 switch_props[PROP_USE_ACTION_APPEARANCE] =
790 g_param_spec_override ("use-action-appearance",
791 g_object_interface_find_property (activatable_iface,
792 "use-action-appearance"));
797 * Whether the #GtkSwitch widget is in its on or off state.
799 switch_props[PROP_ACTIVE] =
800 g_param_spec_boolean ("active",
802 P_("Whether the switch is on or off"),
804 GTK_PARAM_READWRITE);
806 gobject_class->set_property = gtk_switch_set_property;
807 gobject_class->get_property = gtk_switch_get_property;
808 gobject_class->dispose = gtk_switch_dispose;
810 g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
812 widget_class->get_preferred_width = gtk_switch_get_preferred_width;
813 widget_class->get_preferred_height = gtk_switch_get_preferred_height;
814 widget_class->size_allocate = gtk_switch_size_allocate;
815 widget_class->realize = gtk_switch_realize;
816 widget_class->unrealize = gtk_switch_unrealize;
817 widget_class->map = gtk_switch_map;
818 widget_class->unmap = gtk_switch_unmap;
819 widget_class->draw = gtk_switch_draw;
820 widget_class->button_press_event = gtk_switch_button_press;
821 widget_class->button_release_event = gtk_switch_button_release;
822 widget_class->motion_notify_event = gtk_switch_motion;
823 widget_class->enter_notify_event = gtk_switch_enter;
824 widget_class->leave_notify_event = gtk_switch_leave;
825 widget_class->get_accessible = gtk_switch_get_accessible;
827 klass->activate = gtk_switch_activate;
830 * GtkSwitch:slider-width:
832 * The minimum width of the #GtkSwitch handle, in pixels.
834 gtk_widget_class_install_style_property (widget_class,
835 g_param_spec_int ("slider-width",
837 P_("The minimum width of the handle"),
838 DEFAULT_SLIDER_WIDTH, G_MAXINT,
839 DEFAULT_SLIDER_WIDTH,
840 GTK_PARAM_READABLE));
843 * GtkSwitch::activate:
844 * @widget: the object which received the signal.
846 * The ::activate signal on GtkSwitch is an action signal and
847 * emitting it causes the switch to animate.
848 * Applications should never connect to this signal, but use the
849 * notify::active signal.
852 g_signal_new (I_("activate"),
853 G_OBJECT_CLASS_TYPE (gobject_class),
854 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
855 G_STRUCT_OFFSET (GtkSwitchClass, activate),
857 _gtk_marshal_VOID__VOID,
859 widget_class->activate_signal = signals[ACTIVATE];
864 gtk_switch_init (GtkSwitch *self)
866 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
867 self->priv->use_action_appearance = TRUE;
868 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
869 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
875 * Creates a new #GtkSwitch widget.
877 * Return value: the newly created #GtkSwitch instance
882 gtk_switch_new (void)
884 return g_object_new (GTK_TYPE_SWITCH, NULL);
888 * gtk_switch_set_active:
890 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
892 * Changes the state of @sw to the desired one.
897 gtk_switch_set_active (GtkSwitch *sw,
900 GtkSwitchPrivate *priv;
902 g_return_if_fail (GTK_IS_SWITCH (sw));
904 is_active = !!is_active;
908 if (priv->is_active != is_active)
910 AtkObject *accessible;
912 GtkStyleContext *context;
914 widget = GTK_WIDGET (sw);
915 priv->is_active = is_active;
917 g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
920 gtk_action_activate (priv->action);
922 accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
923 atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
925 if (gtk_widget_get_realized (widget))
927 context = gtk_widget_get_style_context (widget);
928 gtk_style_context_notify_state_change (context,
929 gtk_widget_get_window (widget),
930 NULL, GTK_STATE_ACTIVE, is_active);
933 gtk_widget_queue_draw (GTK_WIDGET (sw));
938 * gtk_switch_get_active:
941 * Gets whether the #GtkSwitch is in its "on" or "off" state.
943 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
948 gtk_switch_get_active (GtkSwitch *sw)
950 g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
952 return sw->priv->is_active;
956 gtk_switch_update (GtkActivatable *activatable,
958 const gchar *property_name)
960 if (strcmp (property_name, "visible") == 0)
962 if (gtk_action_is_visible (action))
963 gtk_widget_show (GTK_WIDGET (activatable));
965 gtk_widget_hide (GTK_WIDGET (activatable));
967 else if (strcmp (property_name, "sensitive") == 0)
969 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
971 else if (strcmp (property_name, "active") == 0)
973 gtk_action_block_activate (action);
974 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
975 gtk_action_unblock_activate (action);
980 gtk_switch_sync_action_properties (GtkActivatable *activatable,
986 if (gtk_action_is_visible (action))
987 gtk_widget_show (GTK_WIDGET (activatable));
989 gtk_widget_hide (GTK_WIDGET (activatable));
991 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
993 gtk_action_block_activate (action);
994 gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
995 gtk_action_unblock_activate (action);
999 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
1001 iface->update = gtk_switch_update;
1002 iface->sync_action_properties = gtk_switch_sync_action_properties;
1005 /* accessibility: object */
1007 typedef struct _GtkSwitchAccessible GtkSwitchAccessible;
1008 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
1010 struct _GtkSwitchAccessible
1012 GtkAccessible object;
1018 static void atk_action_interface_init (AtkActionIface *iface);
1020 ATK_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_SWITCH,
1021 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
1023 static AtkStateSet *
1024 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
1026 AtkStateSet *state_set;
1029 state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
1031 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1035 if (gtk_switch_get_active (GTK_SWITCH (widget)))
1036 atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
1042 gtk_switch_accessible_finalize (GObject *obj)
1044 GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)obj;
1046 g_free (accessible->description);
1048 if (accessible->action_idle)
1049 g_source_remove (accessible->action_idle);
1051 G_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->finalize (obj);
1055 _gtk_switch_accessible_initialize (AtkObject *accessible,
1058 ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
1060 atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
1061 atk_object_set_name (accessible, C_("light switch widget", "Switch"));
1062 atk_object_set_description (accessible, _("Switches between on and off states"));
1066 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
1068 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1069 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
1071 object_class->finalize = gtk_switch_accessible_finalize;
1073 atk_class->initialize = _gtk_switch_accessible_initialize;
1074 atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
1078 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
1080 self->description = NULL;
1081 self->action_idle = 0;
1084 /* accessibility: action interface */
1087 gtk_switch_action_get_n_actions (AtkAction *action)
1092 static const gchar *
1093 gtk_switch_action_get_name (AtkAction *action,
1099 static const gchar *
1100 gtk_switch_action_get_description (AtkAction *action,
1103 GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1105 return accessible->description;
1109 gtk_switch_action_set_description (AtkAction *action,
1111 const gchar *description)
1113 GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1115 g_free (accessible->description);
1116 accessible->description = g_strdup (description);
1122 idle_do_action (gpointer data)
1124 GtkSwitchAccessible *accessible = data;
1128 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (data));
1129 sw = GTK_SWITCH (widget);
1131 accessible->action_idle = 0;
1133 if (widget == NULL ||
1134 !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1137 gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
1143 gtk_switch_action_do_action (AtkAction *action,
1146 GtkSwitchAccessible *accessible;
1149 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1153 if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1156 accessible = (GtkSwitchAccessible *)action;
1158 if (!accessible->action_idle)
1159 accessible->action_idle = gdk_threads_add_idle (idle_do_action, accessible);
1165 atk_action_interface_init (AtkActionIface *iface)
1167 iface->do_action = gtk_switch_action_do_action;
1168 iface->get_n_actions = gtk_switch_action_get_n_actions;
1169 iface->get_name = gtk_switch_action_get_name;
1170 iface->get_description = gtk_switch_action_get_description;
1171 iface->set_description = gtk_switch_action_set_description;
1174 /* accessibility: factory */
1176 typedef AtkObjectFactoryClass GtkSwitchAccessibleFactoryClass;
1177 typedef AtkObjectFactory GtkSwitchAccessibleFactory;
1179 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
1180 gtk_switch_accessible_factory,
1181 ATK_TYPE_OBJECT_FACTORY);
1184 gtk_switch_accessible_factory_get_accessible_type (void)
1186 return _gtk_switch_accessible_get_type ();
1190 gtk_switch_accessible_factory_create_accessible (GObject *obj)
1192 AtkObject *accessible;
1194 accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1195 atk_object_initialize (accessible, obj);
1201 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1203 klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1204 klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1208 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)