New tests for last interfaces
[platform/upstream/at-spi2-atk.git] / tests / dummyatk / my-atk-selection.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; https://wiki.gnome.org/Accessibility)
4  *
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <atk/atk.h>
26
27 #include "my-atk-object.h"
28 #include "my-atk-selection.h"
29
30 static void atk_selection_interface_init (AtkSelectionIface *iface);
31
32 G_DEFINE_TYPE_WITH_CODE (MyAtkSelection,
33                          my_atk_selection,
34                          MY_TYPE_ATK_OBJECT,
35                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION,
36                              atk_selection_interface_init));
37
38 static void
39 my_atk_selection_init (MyAtkSelection *obj)
40 {
41 }
42
43 static gboolean
44 my_atk_selection_add_selection (AtkSelection *selection, gint i)
45 {
46   MyAtkSelection *self = MY_ATK_SELECTION(selection);
47   if (!self)
48     return FALSE;
49
50   AtkObject *child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
51   AtkStateSet *ss = atk_object_ref_state_set (child);
52   atk_state_set_add_state (ss, ATK_STATE_SELECTED);
53   return atk_state_set_contains_state (ss, ATK_STATE_SELECTED);
54 }
55
56 static gboolean
57 my_atk_selection_clear_selection (AtkSelection *selection)
58 {
59   MyAtkSelection *self = MY_ATK_SELECTION(selection);
60   if (!self)
61     return FALSE;
62   AtkObject *child = NULL;
63   AtkStateSet *states = NULL;
64   int i;
65   int childs = atk_object_get_n_accessible_children (ATK_OBJECT (selection));
66
67   for (i=0; i<childs; i++) {
68     child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
69     states = atk_object_ref_state_set (child);
70     atk_state_set_remove_state (states, ATK_STATE_SELECTED);
71   }
72   return TRUE;
73 }
74
75 static AtkObject*
76 my_atk_selection_ref_selection (AtkSelection *selection, gint no)
77 {
78   MyAtkSelection *self = MY_ATK_SELECTION(selection);
79   if (!self)
80     return FALSE;
81   AtkObject *child = NULL;
82   AtkStateSet *states = NULL;
83   GArray *array = g_array_new (FALSE, FALSE, sizeof (AtkObject *));
84   int i;
85   int childs = atk_object_get_n_accessible_children (ATK_OBJECT (selection));
86
87   for (i=0; i<childs; i++) {
88     child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
89     states = atk_object_ref_state_set (child);
90     if (atk_state_set_contains_state (states, ATK_STATE_SELECTED))
91       g_array_append_val (array, child);
92   }
93
94   return g_array_index (array, AtkObject *, no);
95 }
96
97 static gint
98 my_atk_selection_get_selection_count (AtkSelection *selection)
99 {
100   MyAtkSelection *self = MY_ATK_SELECTION(selection);
101   if (!self)
102     return FALSE;
103   AtkObject *child = NULL;
104   AtkStateSet *states = NULL;
105   int i, ret=0;
106   int childs = atk_object_get_n_accessible_children (ATK_OBJECT (selection));
107
108   for (i=0; i<childs; i++) {
109     child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
110     states = atk_object_ref_state_set (child);
111     if (atk_state_set_contains_state (states, ATK_STATE_SELECTED))
112       ret++;
113
114   }
115   return ret;
116 }
117
118 static gboolean
119 my_atk_selection_is_child_selected (AtkSelection *selection, gint i)
120 {
121   MyAtkSelection *self = MY_ATK_SELECTION(selection);
122   if (!self)
123     return FALSE;
124   AtkObject *child = NULL;
125   AtkStateSet *states = NULL;
126   child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
127   states = atk_object_ref_state_set (child);
128   if (atk_state_set_contains_state (states, ATK_STATE_SELECTED))
129     return TRUE;
130   return FALSE;
131 }
132
133 static gboolean
134 my_atk_selection_remove_selection (AtkSelection *selection, gint no)
135 {
136   MyAtkSelection *self = MY_ATK_SELECTION(selection);
137   AtkObject *child = NULL;
138   AtkStateSet *states = NULL;
139   GArray *array = NULL;
140   AtkObject *o = NULL;
141   int i;
142   int childs;
143   gboolean ret = FALSE;
144
145   if (!self)
146     return FALSE;
147   array = g_array_new (FALSE, FALSE, sizeof (AtkObject *));
148   childs = atk_object_get_n_accessible_children (ATK_OBJECT (selection));
149
150   for (i=0; i<childs; i++) {
151     child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
152     states = atk_object_ref_state_set (child);
153     if (atk_state_set_contains_state (states, ATK_STATE_SELECTED))
154       g_array_append_val (array, child);
155   }
156   g_object_unref (states);
157
158   o = g_array_index (array, AtkObject *, no);
159   states = atk_object_ref_state_set (o);
160   atk_state_set_remove_state (states, ATK_STATE_SELECTED);
161
162   ret = !atk_state_set_contains_state (states, ATK_STATE_SELECTED);
163   g_object_unref (states);
164   g_object_unref (o);
165   g_object_unref (self);
166   g_array_free (array, TRUE);
167
168   return ret;
169 }
170
171 static gboolean
172 my_atk_selection_select_all_selection (AtkSelection *selection)
173 {
174   MyAtkSelection *self = MY_ATK_SELECTION(selection);
175   AtkObject *child = NULL;
176   AtkStateSet *states = NULL;
177   int i;
178   int childs;
179
180   if (!self)
181     return FALSE;
182   childs = atk_object_get_n_accessible_children (ATK_OBJECT (selection));
183
184   for (i=0; i<childs; i++) {
185     child = atk_object_ref_accessible_child (ATK_OBJECT (selection), i);
186     states = atk_object_ref_state_set (child);
187     atk_state_set_add_state (states, ATK_STATE_SELECTED);
188     g_object_unref (states);
189     g_object_unref (child);
190   }
191
192   g_object_unref (self);
193   return TRUE;
194 }
195
196 static void
197 atk_selection_interface_init (AtkSelectionIface *iface)
198 {
199   if(!iface) return;
200
201   iface->add_selection = my_atk_selection_add_selection;
202   iface->clear_selection = my_atk_selection_clear_selection;
203   iface->ref_selection = my_atk_selection_ref_selection;
204   iface->get_selection_count = my_atk_selection_get_selection_count;
205   iface->is_child_selected = my_atk_selection_is_child_selected;
206   iface->remove_selection = my_atk_selection_remove_selection;
207   iface->select_all_selection = my_atk_selection_select_all_selection;
208 }
209
210 static void
211 my_atk_selection_initialize (AtkObject *obj, gpointer data)
212 {
213 }
214
215 static void
216 my_atk_selection_finalize (GObject *obj)
217 {
218 }
219
220 static void
221 my_atk_selection_class_init (MyAtkSelectionClass *my_class)
222 {
223   AtkObjectClass *atk_class = ATK_OBJECT_CLASS(my_class);
224   GObjectClass *gobject_class = G_OBJECT_CLASS(my_class);
225
226   gobject_class->finalize = my_atk_selection_finalize;
227
228   atk_class->initialize = my_atk_selection_initialize;
229 }