2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; https://wiki.gnome.org/Accessibility)
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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.
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.
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.
26 #include "my-atk-object.h"
28 GType my_atk_object_get_type (void);
30 G_DEFINE_TYPE (MyAtkObject,
34 void my_atk_object_add_child (MyAtkObject* parent,
37 g_ptr_array_add (parent->children, child);
38 g_object_ref_sink (child);
40 atk_object_set_parent (ATK_OBJECT (child), ATK_OBJECT (parent));
42 g_signal_emit_by_name (parent, "children-changed::add",
43 parent->children->len - 1,
47 void my_atk_object_remove_child (MyAtkObject* parent,
51 for (i = parent->children->len - 1; i >= 0; i--) {
52 if (g_ptr_array_index (parent->children, i) == child)
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);
60 static void my_atk_object_set_parent(AtkObject *accessible, AtkObject *parent)
62 MyAtkObject *self = MY_ATK_OBJECT (accessible);
63 AtkObject *parent_old = atk_object_get_parent (accessible);
65 if (parent_old == parent)
68 AtkObjectClass *klass = ATK_OBJECT_CLASS (my_atk_object_parent_class);
69 klass->set_parent (accessible, parent);
71 if (parent_old != NULL)
72 my_atk_object_remove_child (MY_ATK_OBJECT (parent_old), self);
75 static gint my_atk_object_get_n_children (AtkObject *accessible)
77 MyAtkObject *self = MY_ATK_OBJECT (accessible);
78 return self->children->len;
81 static AtkObject* my_atk_object_ref_child (AtkObject *accessible, gint i)
83 MyAtkObject *self = MY_ATK_OBJECT (accessible);
85 g_return_val_if_fail (i >= 0 || i <= self->children->len, NULL);
87 AtkObject* child = ATK_OBJECT (g_ptr_array_index (self->children, i));
89 return (child == NULL) ? NULL : g_object_ref (child);
92 static gint my_atk_object_get_index_in_parent (AtkObject *accessible)
94 AtkObject *parent = atk_object_get_parent (accessible);
95 if (parent == NULL) return -1; /*root object so no parent*/
97 MyAtkObject *parent_my = MY_ATK_OBJECT (parent);
99 int i = parent_my->children->len;
101 if (g_ptr_array_index (parent_my->children,i) == accessible)
105 g_return_val_if_fail (i>=0, -1);
110 static AtkRelationSet *my_atk_object_ref_relation_set (AtkObject* accessible)
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));
118 static AtkStateSet *my_atk_object_ref_state_set (AtkObject *accessible)
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));
126 static AtkAttributeSet *my_atk_object_get_attributes (AtkObject *accessible)
128 AtkAttributeSet *attributes;
131 attr = g_malloc (sizeof (AtkAttribute));
132 attr->name = g_strdup ("atspi");
133 attr->value = g_strdup ("test");
135 attributes = g_slist_append (NULL, attr);
140 static void my_atk_object_init (MyAtkObject *self)
142 self->children = g_ptr_array_new_full (10, g_object_unref);
145 static void my_atk_object_class_init (MyAtkObjectClass *my_class)
147 AtkObjectClass *object_class = ATK_OBJECT_CLASS (my_class);
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;