Debian Codegraph @

gtk/a11y/gtkflowboxaccessible.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 "gtkflowboxaccessibleprivate.h"
21
22 #include "gtk/gtkflowbox.h"
23
24 static void atk_selection_interface_init (AtkSelectionIface *iface);
25
26 G_DEFINE_TYPE_WITH_CODE (GtkFlowBoxAccessible, gtk_flow_box_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
27 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
28
29 static void
30 gtk_flow_box_accessible_init (GtkFlowBoxAccessible *accessible)
31 {
32 }
33
34 static void
35 gtk_flow_box_accessible_initialize (AtkObject *obj,
36 gpointer data)
37 {
38 ATK_OBJECT_CLASS (gtk_flow_box_accessible_parent_class)->initialize (obj, data);
39
40 obj->role = ATK_ROLE_TABLE;
41 }
42
43 static AtkStateSet*
44 gtk_flow_box_accessible_ref_state_set (AtkObject *obj)
45 {
46 AtkStateSet *state_set;
47 GtkWidget *widget;
48
49 state_set = ATK_OBJECT_CLASS (gtk_flow_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_flow_box_accessible_class_init (GtkFlowBoxAccessibleClass *klass)
60 {
61 AtkObjectClass *object_class = ATK_OBJECT_CLASS (klass);
62
63 object_class->initialize = gtk_flow_box_accessible_initialize;
64 object_class->ref_state_set = gtk_flow_box_accessible_ref_state_set;
65 }
66
67 static gboolean
68 gtk_flow_box_accessible_add_selection (AtkSelection *selection,
69 gint idx)
70 {
71 GtkWidget *box;
72 GList *children;
73 GtkWidget *child;
74
75 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
76 if (box == NULL)
77 return FALSE;
78
79 children = gtk_container_get_children (GTK_CONTAINER (box));
80 child = g_list_nth_data (children, idx);
81 g_list_free (children);
82 if (child)
83 {
84 gtk_flow_box_select_child (GTK_FLOW_BOX (box), GTK_FLOW_BOX_CHILD (child));
85 return TRUE;
86 }
87 return FALSE;
88 }
89
90 static gboolean
91 gtk_flow_box_accessible_remove_selection (AtkSelection *selection,
92 gint idx)
93 {
94 GtkWidget *box;
95 GList *children;
96 GtkWidget *child;
97
98 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
99 if (box == NULL)
100 return FALSE;
101
102 children = gtk_container_get_children (GTK_CONTAINER (box));
103 child = g_list_nth_data (children, idx);
104 g_list_free (children);
105 if (child)
106 {
107 gtk_flow_box_unselect_child (GTK_FLOW_BOX (box), GTK_FLOW_BOX_CHILD (child));
108 return TRUE;
109 }
110 return FALSE;
111 }
112
113 static gboolean
114 gtk_flow_box_accessible_clear_selection (AtkSelection *selection)
115 {
116 GtkWidget *box;
117
118 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
119 if (box == NULL)
120 return FALSE;
121
122 gtk_flow_box_unselect_all (GTK_FLOW_BOX (box));
123 return TRUE;
124 }
125
126 static gboolean
127 gtk_flow_box_accessible_select_all (AtkSelection *selection)
128 {
129 GtkWidget *box;
130
131 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
132 if (box == NULL)
133 return FALSE;
134
135 gtk_flow_box_select_all (GTK_FLOW_BOX (box));
136 return TRUE;
137 }
138
139 typedef struct
140 {
141 gint idx;
142 GtkWidget *child;
143 } FindSelectedData;
144
145 static void
146 find_selected_child (GtkFlowBox *box,
147 GtkFlowBoxChild *child,
148 gpointer data)
149 {
150 FindSelectedData *d = data;
151
152 if (d->idx == 0)
153 {
154 if (d->child == NULL)
155 d->child = GTK_WIDGET (child);
156 }
157 else
158 d->idx -= 1;
159 }
160
161 static AtkObject *
162 gtk_flow_box_accessible_ref_selection (AtkSelection *selection,
163 gint idx)
164 {
165 GtkWidget *box;
166 AtkObject *accessible;
167 FindSelectedData data;
168
169 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
170 if (box == NULL)
171 return NULL;
172
173 data.idx = idx;
174 data.child = NULL;
175 gtk_flow_box_selected_foreach (GTK_FLOW_BOX (box), find_selected_child, &data);
176
177 if (data.child == NULL)
178 return NULL;
179
180 accessible = gtk_widget_get_accessible (data.child);
181 g_object_ref (accessible);
182 return accessible;
183 }
184
185 static void
186 count_selected (GtkFlowBox *box,
187 GtkFlowBoxChild *child,
188 gpointer data)
189 {
190 gint *count = data;
191 *count += 1;
192 }
193
194 static gint
195 gtk_flow_box_accessible_get_selection_count (AtkSelection *selection)
196 {
197 GtkWidget *box;
198 gint count;
199
200 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
201 if (box == NULL)
202 return 0;
203
204 count = 0;
205 gtk_flow_box_selected_foreach (GTK_FLOW_BOX (box), count_selected, &count);
206
207 return count;
208 }
209
210 static gboolean
211 gtk_flow_box_accessible_is_child_selected (AtkSelection *selection,
212 gint idx)
213 {
214 GtkWidget *box;
215 GtkFlowBoxChild *child;
216
217 box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
218 if (box == NULL)
219 return FALSE;
220
221 child = gtk_flow_box_get_child_at_index (GTK_FLOW_BOX (box), idx);
222
223 return gtk_flow_box_child_is_selected (child);
224 }
225
226 static void atk_selection_interface_init (AtkSelectionIface *iface)
227 {
228 iface->add_selection = gtk_flow_box_accessible_add_selection;
229 iface->remove_selection = gtk_flow_box_accessible_remove_selection;
230 iface->clear_selection = gtk_flow_box_accessible_clear_selection;
231 iface->ref_selection = gtk_flow_box_accessible_ref_selection;
232 iface->get_selection_count = gtk_flow_box_accessible_get_selection_count;
233 iface->is_child_selected = gtk_flow_box_accessible_is_child_selected;
234 iface->select_all_selection = gtk_flow_box_accessible_select_all;
235 }
236
237 void
238 _gtk_flow_box_accessible_selection_changed (GtkWidget *box)
239 {
240 AtkObject *accessible;
241 accessible = gtk_widget_get_accessible (box);
242 g_signal_emit_by_name (accessible, "selection-changed");
243 }
244
245 void
246 _gtk_flow_box_accessible_update_cursor (GtkWidget *box,
247 GtkWidget *child)
248 {
249 AtkObject *accessible;
250 AtkObject *descendant;
251 accessible = gtk_widget_get_accessible (box);
252 descendant = child ? gtk_widget_get_accessible (child) : NULL;
253 g_signal_emit_by_name (accessible, "active-descendant-changed", descendant);
254 }