2009-27-09 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / tests / dummyatk / my-atk-selection.c
1 /* This file contains both declaration and definition of the MyAtkSelection,
2  * a GObject that implements the AtkSelectionIface interface.
3  */
4
5 #include <glib-object.h>
6 #include <atk/atk.h> 
7
8 #include "my-atk-object.h"
9 #include "my-atk-selection.h"
10
11 /******************************************************************/
12 /*                    Implementation                              */
13 /******************************************************************/
14 static GObjectClass *parent_class_simple_selection = NULL;
15
16 /* Implementation of the AtkSelectionIface interface. */
17 static gboolean
18 simple_selection_add_selection (AtkSelection *selection, gint i)
19 {
20     MyAtkSelection* self = (MyAtkSelection*)selection;
21     if ((!self) || self->disposed)
22     {
23         return FALSE;
24     }
25     
26     if ((i >= 0) && (i < TEST_SELECTION_NCHILDREN))
27     {
28         /* If the child is not selected, select it and send the signal */
29         if (!self->is_selected[i])
30         {
31             self->is_selected[i] = TRUE;
32             g_signal_emit_by_name ((gpointer)self, "selection-changed");
33         }
34                 
35         return TRUE;    
36     }
37     else
38     {
39         return FALSE;
40     }    
41     
42 }
43
44 static gboolean 
45 simple_selection_clear_selection (AtkSelection *selection)
46 {
47     MyAtkSelection* self = (MyAtkSelection*)selection;
48     if ((!self) || self->disposed)
49     {
50         return FALSE;
51     }
52     
53     /* clear selection */
54     {
55         gboolean changed = FALSE;
56         int i;
57         for (i = 0; i < TEST_SELECTION_NCHILDREN; ++i)
58         {
59             changed |= self->is_selected[i];
60             self->is_selected[i] = FALSE;
61         }
62         
63         if (changed)
64         {
65             g_signal_emit_by_name ((gpointer)self, "selection-changed");
66         }
67     }
68     
69     return TRUE;
70 }
71
72 static AtkObject* 
73 simple_selection_ref_selection (AtkSelection *selection, gint i)
74 {
75     int pos;
76     int nsel;
77     MyAtkSelection* self = (MyAtkSelection*)selection;
78     if ((!self) || self->disposed)
79     {
80         return NULL;
81     }
82     
83     nsel = 0;
84     for (pos = 0; pos < TEST_SELECTION_NCHILDREN; ++pos)
85     {
86         if (self->is_selected[pos])
87         {
88             if (i == nsel)
89             {
90                 g_object_ref (G_OBJECT (self->child[pos]));
91                 return ATK_OBJECT(self->child[pos]);
92             }
93             ++nsel;
94         }
95     }
96             
97     return NULL;
98 }
99
100 static gint 
101 simple_selection_get_selection_count (AtkSelection *selection)
102 {
103     MyAtkSelection* self = (MyAtkSelection*)selection;
104     
105     int cnt = 0;
106     int i;
107     
108     if ((!self) || self->disposed)
109     {
110         return 0;
111     }
112     
113     for (i = 0; i < TEST_SELECTION_NCHILDREN; ++i)
114     {
115         if (self->is_selected[i]) 
116         {
117             ++cnt;
118         }
119     }
120     
121      return cnt;
122 }
123
124 static gboolean 
125 simple_selection_is_child_selected (AtkSelection *selection, gint i)
126 {
127     MyAtkSelection* self = (MyAtkSelection*)selection;
128     if ((!self) || self->disposed)
129     {
130         return FALSE;
131     }
132     
133     if ((i >= 0) && (i < TEST_SELECTION_NCHILDREN))
134     {
135         return (self->is_selected[i]);
136     }
137     else
138     {
139         return FALSE;
140     }
141 }
142
143 static gboolean 
144 simple_selection_remove_selection (AtkSelection *selection, gint i)
145 {
146     int pos;
147     int nsel;
148     
149     MyAtkSelection* self = (MyAtkSelection*)selection;
150     if ((!self) || self->disposed)
151     {
152         return FALSE;
153     }
154     
155     nsel = 0;
156     for (pos = 0; pos < TEST_SELECTION_NCHILDREN; ++pos)
157     {
158         if (self->is_selected[pos])
159         {
160             if (i == nsel)
161             {
162                 self->is_selected[pos] = FALSE;
163                 g_signal_emit_by_name ((gpointer)self, "selection-changed");
164                 return TRUE;
165             }
166             ++nsel;
167         }
168     }
169     
170     return TRUE;
171 }
172
173 static gboolean 
174 simple_selection_select_all_selection (AtkSelection *selection)
175 {
176     MyAtkSelection* self = (MyAtkSelection*)selection;
177     if ((!self) || self->disposed)
178     {
179         return FALSE;
180     }
181     
182     if (!self->multisel_supported)
183     {
184         return FALSE;
185     }
186     
187     /* select all */
188     {
189         gboolean changed = FALSE;
190         int i;
191         for (i = 0; i < TEST_SELECTION_NCHILDREN; ++i)
192         {
193             changed |= !self->is_selected[i];
194             self->is_selected[i] = TRUE;
195         }
196         
197         if (changed)
198         {
199             g_signal_emit_by_name ((gpointer)self, "selection-changed");
200         }
201     }
202     
203     return TRUE;
204 }
205
206 /******************************************************************/
207 static void
208 simple_selection_interface_init (gpointer g_iface, gpointer iface_data)
209 {
210     AtkSelectionIface *klass = (AtkSelectionIface *)g_iface;
211     
212     /* set up overrides here */
213     klass->add_selection = 
214         (gboolean (*) (AtkSelection *selection, gint i)) simple_selection_add_selection;
215     
216     klass->clear_selection = 
217         (gboolean (*) (AtkSelection *selection)) simple_selection_clear_selection;
218     
219     klass->ref_selection = 
220         (AtkObject* (*) (AtkSelection *selection, gint i)) simple_selection_ref_selection;
221     
222     klass->get_selection_count = 
223         (gint (*) (AtkSelection *selection)) simple_selection_get_selection_count;
224     
225     klass->is_child_selected = 
226         (gboolean (*) (AtkSelection *selection, gint i)) simple_selection_is_child_selected;
227     
228     klass->remove_selection = 
229         (gboolean (*) (AtkSelection *selection, gint i)) simple_selection_remove_selection;
230     
231     klass->select_all_selection = 
232         (gboolean (*) (AtkSelection *selection)) simple_selection_select_all_selection;
233 }
234
235 static void
236 simple_selection_instance_init (GTypeInstance *instance, gpointer g_class)
237 {
238     MyAtkSelection *self = (MyAtkSelection *)instance;
239     int i;
240     
241     self->disposed = FALSE;
242     self->multisel_supported = TRUE;
243     for (i = 0; i < TEST_SELECTION_NCHILDREN; ++i)
244     {
245         self->child[i] = MY_ATK_OBJECT (g_object_new (MY_TYPE_ATK_OBJECT, NULL));
246         self->child[i]->id = i;
247         self->is_selected[i] = FALSE; /* not selected by default */
248     }
249 }
250
251 static void
252 my_atk_selection_dispose (GObject *obj)
253 {
254     MyAtkSelection *self = (MyAtkSelection *)obj;
255     int i;
256
257     if (self->disposed) 
258     {
259         return;
260     }
261     
262     /* Make sure dispose does not run twice. */
263     self->disposed = TRUE;
264
265     for (i = 0; i < TEST_SELECTION_NCHILDREN; ++i)
266     {
267         g_object_unref (G_OBJECT (self->child[i]));
268     }
269
270     /* Chain up to the parent class */
271     G_OBJECT_CLASS (parent_class_simple_selection)->dispose (obj);
272 }
273
274 static void
275 my_atk_selection_finalize (GObject *obj)
276 {
277     /*MyAtkSelection *self = (MyAtkSelection *)obj;
278     if (self)
279     {
280     }*/
281     
282     /* Chain up to the parent class */
283     G_OBJECT_CLASS (parent_class_simple_selection)->finalize (obj);
284 }
285
286 static void
287 my_atk_selection_class_init (gpointer g_class, gpointer g_class_data)
288 {
289     GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
290     MyAtkSelectionClass *klass = MY_ATK_SELECTION_CLASS (g_class);
291
292     gobject_class->dispose = my_atk_selection_dispose;
293     gobject_class->finalize = my_atk_selection_finalize;
294
295     parent_class_simple_selection = g_type_class_peek_parent (klass);
296 }
297
298 GType 
299 my_atk_selection_get_type (void)
300 {
301     static GType type = 0;
302     if (type == 0) 
303     {
304         static const GTypeInfo info = 
305         {
306             sizeof (MyAtkSelectionClass),
307             NULL,   /* base_init */
308             NULL,   /* base_finalize */
309             my_atk_selection_class_init, /* class_init */
310             NULL,   /* class_finalize */
311             NULL,   /* class_data */
312             sizeof (MyAtkSelection),
313             0,      /* n_preallocs */
314             simple_selection_instance_init    /* instance_init */
315         };
316                 
317         static const GInterfaceInfo iface_info = 
318         {
319             (GInterfaceInitFunc) simple_selection_interface_init,    /* interface_init */
320             NULL,                                       /* interface_finalize */
321             NULL                                        /* interface_data */
322         };
323         type = g_type_register_static (MY_TYPE_ATK_OBJECT,
324                                        "MyAtkSelectionType",
325                                        &info, 0);
326         g_type_add_interface_static (type,
327                                      ATK_TYPE_SELECTION,
328                                      &iface_info);
329     }
330     return type;
331 }
332