1 /* GTK+ - accessibility implementations
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gtk/gtk.h>
21 #include <glib/gi18n-lib.h>
22 #include "gtkbooleancellaccessible.h"
23 
24 struct _GtkBooleanCellAccessiblePrivate
25 {
26   gboolean cell_value;
27   gboolean cell_sensitive;
28 };
29 
30 static AtkActionIface *parent_action_iface;
31 
32 static gint
gtk_boolean_cell_accessible_get_n_actions(AtkAction * action)33 gtk_boolean_cell_accessible_get_n_actions (AtkAction *action)
34 {
35   return parent_action_iface->get_n_actions (action) + 1;
36 }
37 
38 static const gchar *
gtk_boolean_cell_accessible_get_description(AtkAction * action,gint i)39 gtk_boolean_cell_accessible_get_description (AtkAction *action,
40                                              gint       i)
41 {
42   if (i == 0)
43     return C_("Action description", "Toggles the cell");
44 
45   return parent_action_iface->get_description (action, i - 1);
46 }
47 
48 static const gchar *
gtk_boolean_cell_accessible_action_get_name(AtkAction * action,gint i)49 gtk_boolean_cell_accessible_action_get_name (AtkAction *action,
50                                              gint       i)
51 {
52   if (i == 0)
53     return "toggle";
54 
55   return parent_action_iface->get_description (action, i - 1);
56 }
57 
58 static const gchar *
gtk_boolean_cell_accessible_action_get_localized_name(AtkAction * action,gint i)59 gtk_boolean_cell_accessible_action_get_localized_name (AtkAction *action,
60                                                        gint       i)
61 {
62   if (i == 0)
63     return C_("Action name", "Toggle");
64 
65   return parent_action_iface->get_description (action, i - 1);
66 }
67 
68 static gboolean
gtk_boolean_cell_accessible_do_action(AtkAction * action,gint i)69 gtk_boolean_cell_accessible_do_action (AtkAction *action,
70                                        gint       i)
71 {
72   if (i == 0)
73     return parent_action_iface->do_action (action, 2);
74 
75   return parent_action_iface->do_action (action, i - 1);
76 }
77 
78 static void
gtk_boolean_cell_accessible_action_interface_init(AtkActionIface * iface)79 gtk_boolean_cell_accessible_action_interface_init (AtkActionIface *iface)
80 {
81   parent_action_iface = g_type_interface_peek_parent (iface);
82 
83   iface->do_action = gtk_boolean_cell_accessible_do_action;
84   iface->get_n_actions = gtk_boolean_cell_accessible_get_n_actions;
85   iface->get_description = gtk_boolean_cell_accessible_get_description;
86   iface->get_name = gtk_boolean_cell_accessible_action_get_name;
87   iface->get_localized_name = gtk_boolean_cell_accessible_action_get_localized_name;
88 }
89 
90 
G_DEFINE_TYPE_WITH_CODE(GtkBooleanCellAccessible,gtk_boolean_cell_accessible,GTK_TYPE_RENDERER_CELL_ACCESSIBLE,G_ADD_PRIVATE (GtkBooleanCellAccessible)G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION,gtk_boolean_cell_accessible_action_interface_init))91 G_DEFINE_TYPE_WITH_CODE (GtkBooleanCellAccessible, gtk_boolean_cell_accessible, GTK_TYPE_RENDERER_CELL_ACCESSIBLE,
92                          G_ADD_PRIVATE (GtkBooleanCellAccessible)
93                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, gtk_boolean_cell_accessible_action_interface_init))
94 
95 
96 static AtkStateSet *
97 gtk_boolean_cell_accessible_ref_state_set (AtkObject *accessible)
98 {
99   GtkBooleanCellAccessible *cell = GTK_BOOLEAN_CELL_ACCESSIBLE (accessible);
100   AtkStateSet *state_set;
101 
102   state_set = ATK_OBJECT_CLASS (gtk_boolean_cell_accessible_parent_class)->ref_state_set (accessible);
103 
104   if (cell->priv->cell_value)
105     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
106 
107   if (cell->priv->cell_sensitive)
108     atk_state_set_add_state (state_set, ATK_STATE_SENSITIVE);
109   else
110     atk_state_set_remove_state (state_set, ATK_STATE_SENSITIVE);
111 
112   return state_set;
113 }
114 
115 static void
gtk_boolean_cell_accessible_update_cache(GtkCellAccessible * cell,gboolean emit_signal)116 gtk_boolean_cell_accessible_update_cache (GtkCellAccessible *cell,
117                                           gboolean            emit_signal)
118 {
119   GtkBooleanCellAccessible *boolean_cell = GTK_BOOLEAN_CELL_ACCESSIBLE (cell);
120   gboolean active;
121   gboolean sensitive;
122   GtkCellRenderer *renderer;
123 
124   g_object_get (cell, "renderer", &renderer, NULL);
125   g_object_get (renderer,
126                 "active", &active,
127                 "sensitive", &sensitive,
128                 NULL);
129   g_object_unref (renderer);
130 
131   if (boolean_cell->priv->cell_value != active)
132     {
133       boolean_cell->priv->cell_value = !boolean_cell->priv->cell_value;
134 
135       if (emit_signal)
136         atk_object_notify_state_change (ATK_OBJECT (cell), ATK_STATE_CHECKED, active);
137     }
138 
139   if (boolean_cell->priv->cell_sensitive != sensitive)
140     {
141       boolean_cell->priv->cell_sensitive = !boolean_cell->priv->cell_sensitive;
142 
143       if (emit_signal)
144         atk_object_notify_state_change (ATK_OBJECT (cell), ATK_STATE_SENSITIVE, sensitive);
145     }
146 }
147 
148 static void
gtk_boolean_cell_accessible_class_init(GtkBooleanCellAccessibleClass * klass)149 gtk_boolean_cell_accessible_class_init (GtkBooleanCellAccessibleClass *klass)
150 {
151   GtkCellAccessibleClass *cell_class = GTK_CELL_ACCESSIBLE_CLASS (klass);
152   AtkObjectClass *atkobject_class = ATK_OBJECT_CLASS (klass);
153 
154   atkobject_class->ref_state_set = gtk_boolean_cell_accessible_ref_state_set;
155 
156   cell_class->update_cache = gtk_boolean_cell_accessible_update_cache;
157 }
158 
159 static void
gtk_boolean_cell_accessible_init(GtkBooleanCellAccessible * cell)160 gtk_boolean_cell_accessible_init (GtkBooleanCellAccessible *cell)
161 {
162   cell->priv = gtk_boolean_cell_accessible_get_instance_private (cell);
163 }
164 
165