2.38.0
[platform/upstream/at-spi2-atk.git] / tests / dummyatk / my-atk-action.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 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 <string.h>
25 #include <atk/atk.h>
26
27 #include "my-atk-object.h"
28 #include "my-atk-action.h"
29
30 typedef struct _MyAtkActionInfo MyAtkActionInfo;
31
32 struct _MyAtkActionInfo {
33   gchar *name;
34   gchar *description;
35   gchar *keybinding;
36
37   MyAtkActionFunc do_action_func;
38 };
39
40 static void atk_action_interface_init (AtkActionIface *iface);
41
42 G_DEFINE_TYPE_WITH_CODE (MyAtkAction,
43                          my_atk_action,
44                          MY_TYPE_ATK_OBJECT,
45                          G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION,
46                              atk_action_interface_init));
47
48 #define MY_ATK_ACTION_GET_PRIVATE(obj) \
49   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MY_TYPE_ATK_ACTION, MyAtkActionPrivate))
50
51 struct _MyAtkActionPrivate {
52   GQueue *action_queue;
53   guint action_idle_handler;
54   GList  *action_list;
55   GList *children;
56 };
57
58 static void
59 my_atk_action_initialize (AtkObject *obj, gpointer data)
60 {
61 }
62
63 static void
64 my_atk_action_init (MyAtkAction *action_obj)
65 {
66   MyAtkActionPrivate *priv = MY_ATK_ACTION_GET_PRIVATE (action_obj);
67   action_obj->priv = priv;
68   priv->action_queue = NULL;
69   priv->action_idle_handler = 0;
70   priv->action_list = NULL;
71   priv->children = NULL;
72 }
73
74 static void
75 my_atk_action_finalize (GObject *object)
76 {
77 }
78
79 static void
80 my_atk_action_class_init (MyAtkActionClass *my_class)
81 {
82   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (my_class);
83   GObjectClass *gobject_class = G_OBJECT_CLASS (my_class);
84
85   gobject_class->finalize = my_atk_action_finalize;
86
87   atk_class->initialize = my_atk_action_initialize;
88
89   g_type_class_add_private (gobject_class, sizeof (MyAtkActionPrivate));
90 }
91
92 static MyAtkActionInfo *
93 _my_atk_action_get_action_info (MyAtkAction *action, gint i)
94 {
95   MyAtkActionPrivate *priv = NULL;
96   MyAtkActionInfo *node_data = NULL;
97
98   g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
99
100   priv = action->priv;
101
102   if (priv->action_list == NULL)
103     return NULL;
104
105   node_data = g_list_nth_data (priv->action_list, i);
106
107   g_return_val_if_fail (node_data, NULL);
108
109   return node_data;
110 }
111
112 static const gchar*
113 my_atk_action_description_get (AtkAction *action, gint i)
114 {
115   MyAtkAction *my_action = NULL;
116   MyAtkActionInfo *info = NULL;
117
118   g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
119   my_action = MY_ATK_ACTION (action);
120
121   info = _my_atk_action_get_action_info (my_action, i);
122
123   if (info == NULL)
124     return NULL;
125
126   return strdup (info->description);
127 }
128
129 static gboolean
130 my_atk_action_description_set (AtkAction *action, gint i, const char *des)
131 {
132   MyAtkAction *my_action = NULL;
133   MyAtkActionInfo *info = NULL;
134
135   g_return_val_if_fail (MY_IS_ATK_ACTION (action), FALSE);
136   my_action = MY_ATK_ACTION (action);
137
138   info = _my_atk_action_get_action_info (my_action, i);
139
140   if (info == NULL)
141     return FALSE;
142
143   g_free (info->description);
144   info->description = g_strdup (des);
145
146   return TRUE;
147 }
148
149 static const gchar*
150 my_atk_action_name_get (AtkAction *action, gint i)
151 {
152   MyAtkAction *my_action = NULL;
153   MyAtkActionInfo *info = NULL;
154
155   g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
156   my_action = MY_ATK_ACTION (action);
157
158   info = _my_atk_action_get_action_info (my_action, i);
159
160   if (info == NULL)
161     return NULL;
162
163   return strdup (info->name);
164 }
165
166 static const gchar*
167 my_atk_action_localized_name_get (AtkAction *action, gint i)
168 {
169   MyAtkAction *my_action = NULL;
170   MyAtkActionInfo *info = NULL;
171
172   g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
173   my_action = MY_ATK_ACTION (action);
174
175   info = _my_atk_action_get_action_info (my_action, i);
176
177   if (info == NULL)
178     return NULL;
179
180   return strdup (info->name);
181 }
182
183 static gint
184 my_atk_action_get_n_actions (AtkAction *action)
185 {
186   MyAtkAction *action_obj = NULL;
187   MyAtkActionPrivate *priv = NULL;
188
189   action_obj = MY_ATK_ACTION (action);
190   priv = action_obj->priv;
191
192   return g_list_length (priv->action_list);
193 }
194
195 static const gchar *
196 my_atk_action_get_keybinding (AtkAction *action, gint i)
197 {
198   MyAtkAction *my_action = NULL;
199   MyAtkActionInfo *info = NULL;
200
201   g_return_val_if_fail (MY_IS_ATK_ACTION (action), NULL);
202   my_action = MY_ATK_ACTION (action);
203
204   info = _my_atk_action_get_action_info (my_action, i);
205
206   if (info == NULL)
207     return NULL;
208
209   return strdup (info->keybinding);
210 }
211
212 void perform_action (AtkObject *obj)
213 {
214   AtkStateSet *state_set1 = atk_object_ref_state_set (obj);
215   atk_state_set_add_state (state_set1, ATK_STATE_ACTIVE);
216 }
217
218 static gboolean
219 my_atk_action_do_action (AtkAction *action, gint i)
220 {
221   g_return_val_if_fail (MY_IS_ATK_ACTION (action), FALSE);
222
223   perform_action (ATK_OBJECT (action));
224
225   return FALSE;
226 }
227
228 guint my_atk_action_add_action (MyAtkAction *action,
229                                 const gchar *action_name,
230                                 const gchar *action_description,
231                                 const gchar *action_keybinding)
232 {
233   MyAtkActionInfo *info = NULL;
234   MyAtkActionPrivate *priv = NULL;
235
236   g_return_val_if_fail (MY_IS_ATK_ACTION (action), -1);
237
238   priv = action->priv;
239
240   info = g_slice_new (MyAtkActionInfo);
241   info->name = g_strdup (action_name);
242   info->description = g_strdup (action_description);
243   info->keybinding = g_strdup (action_keybinding);
244
245   priv->action_list = g_list_append (priv->action_list, info);
246
247   return g_list_length (priv->action_list);
248 }
249
250 static void
251 atk_action_interface_init (AtkActionIface *iface)
252 {
253   g_return_if_fail (iface);
254
255   iface->do_action          = my_atk_action_do_action;
256
257   iface->get_n_actions      = my_atk_action_get_n_actions;
258   iface->get_description    = my_atk_action_description_get;
259   iface->get_keybinding     = my_atk_action_get_keybinding;
260   iface->get_name           = my_atk_action_name_get;
261   iface->set_description    = my_atk_action_description_set;
262   iface->get_localized_name = my_atk_action_localized_name_get;
263 }