]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkswitchaccessible.c
b49b22354c2729176c7a48edb3198718affd1248
[~andy/gtk] / gtk / a11y / gtkswitchaccessible.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2010  Intel Corporation
4  * Copyright (C) 2010  RedHat, Inc.
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Author:
22  *      Emmanuele Bassi <ebassi@linux.intel.com>
23  *      Matthias Clasen <mclasen@redhat.com>
24  *
25  * Based on similar code from Mx.
26  */
27
28 #include "config.h"
29
30 #include <gtk/gtk.h>
31 #include "gtkintl.h"
32 #include "gtkswitchaccessible.h"
33
34
35 static void atk_action_interface_init (AtkActionIface *iface);
36
37 G_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
38                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
39
40 static AtkStateSet *
41 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
42 {
43   AtkStateSet *state_set;
44   GtkWidget *widget;
45
46   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
47
48   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
49   if (widget == NULL)
50     return state_set;
51
52   if (gtk_switch_get_active (GTK_SWITCH (widget)))
53     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
54
55   return state_set;
56 }
57
58 static void
59 gtk_switch_accessible_initialize (AtkObject *accessible,
60                                   gpointer   widget)
61 {
62   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
63
64   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
65   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
66   atk_object_set_description (accessible, _("Switches between on and off states"));
67 }
68
69 static void
70 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
71 {
72   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
73
74   atk_class->initialize = gtk_switch_accessible_initialize;
75   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
76 }
77
78 static void
79 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
80 {
81 }
82
83 static gint
84 gtk_switch_action_get_n_actions (AtkAction *action)
85 {
86   return 1;
87 }
88
89 static const gchar *
90 gtk_switch_action_get_name (AtkAction *action,
91                             gint       i)
92 {
93   if (i != 0)
94     return NULL;
95
96   return "toggle";
97 }
98
99 static gboolean
100 gtk_switch_action_do_action (AtkAction *action,
101                              gint       i)
102 {
103   GtkSwitch *sw;
104   GtkWidget *widget;
105
106   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
107   if (widget == NULL)
108     return FALSE;
109
110   if (i != 0)
111     return FALSE;
112
113   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
114     return FALSE;
115
116   sw = GTK_SWITCH (widget);
117   gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
118
119   return TRUE;
120 }
121
122 static void
123 atk_action_interface_init (AtkActionIface *iface)
124 {
125   iface->do_action = gtk_switch_action_do_action;
126   iface->get_n_actions = gtk_switch_action_get_n_actions;
127   iface->get_name = gtk_switch_action_get_name;
128 }