Debian Codegraph @

gtk/a11y/gtklistboxaccessible.c

raw
1 /*
2 * Copyright (C) 2013 Red Hat, 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 "gtklistboxaccessibleprivate.h"
21
22 #include "gtk/gtklistbox.h"
23
24 static void atk_selection_interface_init (AtkSelectionIface *iface);
25
26 G_DEFINE_TYPE_WITH_CODE (GtkListBoxAccessible, gtk_list_box_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
27 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
28
29 static void
30 gtk_list_box_accessible_init (GtkListBoxAccessible *accessible)
31 {
32 }
33
34 static void
35 gtk_list_box_accessible_initialize (AtkObject *obj,
36 gpointer data)
37 {
38 ATK_OBJECT_CLASS (gtk_list_box_accessible_parent_class)->initialize (obj, data);
39
40 obj->role = ATK_ROLE_LIST_BOX;
41 }
42
43 static AtkStateSet*
44 gtk_list_box_accessible_ref_state_set (AtkObject *obj)
45 {
46 AtkStateSet *state_set;
47 GtkWidget *widget;
48
49 state_set = ATK_OBJECT_CLASS (gtk_list_box_accessible_parent_class)->ref_state_set (obj);
50 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
51
52 if (widget != NULL)
53 atk_state_set_add_state (state_set, ATK_STATE_MANAGES_DESCENDANTS);
54
55 return state_set;
56 }
57
58 static void
59 gtk_list_box_accessible_class_init (GtkListBoxAccessibleClass *klass)
60 {
61 AtkObjectClass *object_class = ATK_OBJECT_CLASS (klass);
62
63 object_class->initialize = gtk_list_box_accessible_initialize;
64 object_class->ref_state_set = gtk_list_box_accessible_ref_state_set;
65 }
66
67 static gboolean
68 gtk_list_box_accessible_add_selection (AtkSelection *selection,
69 gint idx)
70 {
71 GtkWidget *box;
72 GtkListBoxRow *row;
73
74 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
75 if (box == NULL)
76 return FALSE;
77
78 row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (box), idx);
79 if (row)
80 {
81 gtk_list_box_select_row (GTK_LIST_BOX (box), row);
82 return TRUE;
83 }
84 return FALSE;
85 }
86
87 static gboolean
88 gtk_list_box_accessible_remove_selection (AtkSelection *selection,
89 gint idx)
90 {
91 GtkWidget *box;
92 GtkListBoxRow *row;
93
94 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
95 if (box == NULL)
96 return FALSE;
97
98 row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (box), idx);
99 if (row)
100 {
101 gtk_list_box_unselect_row (GTK_LIST_BOX (box), row);
102 return TRUE;
103 }
104 return FALSE;
105 }
106
107 static gboolean
108 gtk_list_box_accessible_clear_selection (AtkSelection *selection)
109 {
110 GtkWidget *box;
111
112 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
113 if (box == NULL)
114 return FALSE;
115
116 gtk_list_box_unselect_all (GTK_LIST_BOX (box));
117 return TRUE;
118 }
119
120 static gboolean
121 gtk_list_box_accessible_select_all (AtkSelection *selection)
122 {
123 GtkWidget *box;
124
125 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
126 if (box == NULL)
127 return FALSE;
128
129 gtk_list_box_select_all (GTK_LIST_BOX (box));
130 return TRUE;
131 }
132
133 typedef struct
134 {
135 gint idx;
136 GtkWidget *row;
137 } FindSelectedData;
138
139 static void
140 find_selected_row (GtkListBox *box,
141 GtkListBoxRow *row,
142 gpointer data)
143 {
144 FindSelectedData *d = data;
145
146 if (d->idx == 0)
147 {
148 if (d->row == NULL)
149 d->row = GTK_WIDGET (row);
150 }
151 else
152 d->idx -= 1;
153 }
154
155 static AtkObject *
156 gtk_list_box_accessible_ref_selection (AtkSelection *selection,
157 gint idx)
158 {
159 GtkWidget *box;
160 AtkObject *accessible;
161 FindSelectedData data;
162
163 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
164 if (box == NULL)
165 return NULL;
166
167 data.idx = idx;
168 data.row = NULL;
169 gtk_list_box_selected_foreach (GTK_LIST_BOX (box), find_selected_row, &data);
170
171 if (data.row == NULL)
172 return NULL;
173
174 accessible = gtk_widget_get_accessible (data.row);
175 g_object_ref (accessible);
176 return accessible;
177 }
178
179 static void
180 count_selected (GtkListBox *box,
181 GtkListBoxRow *row,
182 gpointer data)
183 {
184 gint *count = data;
185 *count += 1;
186 }
187
188 static gint
189 gtk_list_box_accessible_get_selection_count (AtkSelection *selection)
190 {
191 GtkWidget *box;
192 gint count;
193
194 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
195 if (box == NULL)
196 return 0;
197
198 count = 0;
199 gtk_list_box_selected_foreach (GTK_LIST_BOX (box), count_selected, &count);
200
201 return count;
202 }
203
204 static gboolean
205 gtk_list_box_accessible_is_child_selected (AtkSelection *selection,
206 gint idx)
207 {
208 GtkWidget *box;
209 GtkListBoxRow *row;
210
211 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
212 if (box == NULL)
213 return FALSE;
214
215 row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (box), idx);
216
217 return gtk_list_box_row_is_selected (row);
218 }
219
220 static void atk_selection_interface_init (AtkSelectionIface *iface)
221 {
222 iface->add_selection = gtk_list_box_accessible_add_selection;
223 iface->remove_selection = gtk_list_box_accessible_remove_selection;
224 iface->clear_selection = gtk_list_box_accessible_clear_selection;
225 iface->ref_selection = gtk_list_box_accessible_ref_selection;
226 iface->get_selection_count = gtk_list_box_accessible_get_selection_count;
227 iface->is_child_selected = gtk_list_box_accessible_is_child_selected;
228 iface->select_all_selection = gtk_list_box_accessible_select_all;
229 }
230
231 void
232 _gtk_list_box_accessible_selection_changed (GtkListBox *box)
233 {
234 AtkObject *accessible;
235 accessible = gtk_widget_get_accessible (GTK_WIDGET (box));
236 g_signal_emit_by_name (accessible, "selection-changed");
237 }
238
239 void
240 _gtk_list_box_accessible_update_cursor (GtkListBox *box,
241 GtkListBoxRow *row)
242 {
243 AtkObject *accessible;
244 AtkObject *descendant;
245 accessible = gtk_widget_get_accessible (GTK_WIDGET (box));
246 descendant = row ? gtk_widget_get_accessible (GTK_WIDGET (row)) : NULL;
247 g_signal_emit_by_name (accessible, "active-descendant-changed", descendant);
248 }