05b542c49dcaa7eff3fb99f5996922e8285e7996
[platform/upstream/at-spi2-atk.git] / tests / dummyatk / my-atk-object.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; https://wiki.gnome.org/Accessibility)
4  *
5  * Copyright (c) 2014 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 Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <stdio.h>
24 #include <atk/atk.h>
25
26 #include "my-atk-object.h"
27
28 GType my_atk_object_get_type (void);
29
30 G_DEFINE_TYPE (MyAtkObject,
31                my_atk_object,
32                ATK_TYPE_OBJECT);
33
34 void my_atk_object_add_child (MyAtkObject* parent,
35                               MyAtkObject* child)
36 {
37   g_ptr_array_add (parent->children, child);
38   g_object_ref_sink (child);
39
40   atk_object_set_parent (ATK_OBJECT (child), ATK_OBJECT (parent));
41
42   g_signal_emit_by_name (parent, "children-changed::add",
43                          parent->children->len - 1,
44                          child);
45 }
46
47 void my_atk_object_remove_child (MyAtkObject* parent,
48                                  MyAtkObject* child)
49 {
50   gint i;
51   for (i = parent->children->len - 1; i >= 0; i--) {
52     if (g_ptr_array_index (parent->children, i) == child)
53       break;
54   }
55   g_return_if_fail (i < 0);
56   g_ptr_array_remove_index (parent->children, i);
57   g_signal_emit_by_name (parent, "children-changed::remove", i, child);
58 }
59
60 static void my_atk_object_set_parent(AtkObject *accessible, AtkObject *parent)
61 {
62   MyAtkObject *self = MY_ATK_OBJECT (accessible);
63   AtkObject *parent_old = atk_object_get_parent (accessible);
64
65   if (parent_old == parent)
66     return;
67
68   AtkObjectClass *klass = ATK_OBJECT_CLASS (my_atk_object_parent_class);
69   klass->set_parent (accessible, parent);
70
71   if (parent_old != NULL)
72     my_atk_object_remove_child (MY_ATK_OBJECT (parent_old), self);
73 }
74
75 static gint my_atk_object_get_n_children (AtkObject *accessible)
76 {
77   MyAtkObject *self = MY_ATK_OBJECT (accessible);
78   return self->children->len;
79 }
80
81 static AtkObject* my_atk_object_ref_child (AtkObject *accessible, gint i)
82 {
83   MyAtkObject *self = MY_ATK_OBJECT (accessible);
84
85   g_return_val_if_fail (i >= 0 || i <= self->children->len, NULL);
86
87   AtkObject* child = ATK_OBJECT (g_ptr_array_index (self->children, i));
88
89   return (child == NULL) ? NULL : g_object_ref (child);
90 }
91
92 static gint my_atk_object_get_index_in_parent (AtkObject *accessible)
93 {
94   AtkObject *parent = atk_object_get_parent (accessible);
95   if (parent == NULL) return -1; /*root object so no parent*/
96
97   MyAtkObject *parent_my = MY_ATK_OBJECT (parent);
98
99   int i = parent_my->children->len;
100   for (; i>=0; i--) {
101     if (g_ptr_array_index (parent_my->children,i) == accessible)
102       break;
103   }
104
105   g_return_val_if_fail (i>=0, -1);
106
107   return i;
108 }
109
110 static AtkRelationSet *my_atk_object_ref_relation_set (AtkObject* accessible)
111 {
112   MyAtkObject *obj = MY_ATK_OBJECT (accessible);
113   if (obj->relation_set == NULL)
114     obj->relation_set = atk_relation_set_new ();
115   return g_object_ref (ATK_RELATION_SET (obj->relation_set));
116 }
117
118 static AtkStateSet *my_atk_object_ref_state_set (AtkObject *accessible)
119 {
120   MyAtkObject *obj = MY_ATK_OBJECT (accessible);
121   if (obj->state_set == NULL)
122     obj->state_set = atk_state_set_new ();
123   return g_object_ref (ATK_STATE_SET (obj->state_set));
124 }
125
126 static AtkAttributeSet *my_atk_object_get_attributes (AtkObject *accessible)
127 {
128   AtkAttributeSet *attributes;
129   AtkAttribute *attr;
130
131   attr = g_malloc (sizeof (AtkAttribute));
132   attr->name = g_strdup ("atspi");
133   attr->value = g_strdup ("test");
134
135   attributes = g_slist_append (NULL, attr);
136
137   return attributes;
138 }
139
140 static void my_atk_object_init (MyAtkObject *self)
141 {
142   self->children = g_ptr_array_new_full (10, g_object_unref);
143 }
144
145 static void my_atk_object_class_init (MyAtkObjectClass *my_class)
146 {
147   AtkObjectClass *object_class = ATK_OBJECT_CLASS (my_class);
148
149   object_class->set_parent = my_atk_object_set_parent;
150   object_class->get_n_children = my_atk_object_get_n_children;
151   object_class->ref_child = my_atk_object_ref_child;
152   object_class->get_index_in_parent = my_atk_object_get_index_in_parent;
153   object_class->ref_state_set = my_atk_object_ref_state_set;
154   object_class->get_attributes = my_atk_object_get_attributes;
155   object_class->ref_relation_set = my_atk_object_ref_relation_set;
156
157 }