Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_ctxpopup.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * @file eail_ctxpopup.c
22  * @brief EailCtxpopup implementation
23  */
24
25 #include <Elementary.h>
26
27 #include "eail_ctxpopup.h"
28 #include "eail_factory.h"
29 #include "eail_priv.h"
30
31 static void atk_action_interface_init(AtkActionIface *iface);
32
33 /**
34  * @brief Define EailCtxpopup GObject type
35  */
36 G_DEFINE_TYPE_WITH_CODE(EailCtxpopup,
37                         eail_ctxpopup,
38                         EAIL_TYPE_WIDGET,
39                         G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION,
40                                               atk_action_interface_init));
41
42 /**
43  * @brief Initializer for AtkObject
44  *
45  * @param obj AtkObject instance
46  * @param data initialization data
47  */
48 static void
49 eail_ctxpopup_initialize(AtkObject *obj, gpointer data)
50 {
51    ATK_OBJECT_CLASS(eail_ctxpopup_parent_class)->initialize(obj, data);
52
53    obj->role = ATK_ROLE_POPUP_MENU;
54 }
55
56 /**
57  * @brief Destructor for EailCtxpopup class
58  *
59  * @param object GObject instance to be finalized
60  */
61 static void
62 eail_ctxpopup_finalize(GObject *object)
63 {
64    EailCtxpopup *ctxpopup = EAIL_CTXPOPUP(object);
65
66    if (ctxpopup->dismiss_description) free(ctxpopup->dismiss_description);
67
68    G_OBJECT_CLASS(eail_ctxpopup_parent_class)->finalize(object);
69 }
70
71 /**
72  * @brief EailCtxpopup GObject instance initializer
73  *
74  * @param ctxpopup EailCtxpopup instance
75  */
76 static void
77 eail_ctxpopup_init(EailCtxpopup *ctxpopup)
78 {
79    ctxpopup->dismiss_description = NULL;
80 }
81
82 /**
83  * @brief Helper function for getting nested content of elm_popup widget
84  *
85  * @param obj AtkObject instance
86  *
87  * @returns Evas_Object representing the nested widget content
88  * from elm_popup widget
89  */
90 static Evas_Object *
91 _eail_get_nested_widget(AtkObject *obj)
92 {
93    Evas_Object *ctxpopup_widget = NULL, *nested_widget = NULL;
94    /* getting widget of ctxpopup class */
95    ctxpopup_widget = eail_widget_get_widget(EAIL_WIDGET(obj));
96    if (!ctxpopup_widget)
97      {
98         ERR("No widget found for EailCtxpopup object!");
99      }
100
101    nested_widget = elm_object_part_content_get(ctxpopup_widget, "default");
102
103    return nested_widget;
104 }
105
106 /**
107  * @brief Gets the number of accessible children of the accessible
108  *
109  * Implementation AtkObject->get_n_children callback.
110  *
111  * @param obj AtkObject instance
112  *
113  * @returns integer representing the number of accessible children of
114  * the accessible
115  */
116 static gint
117 eail_ctxpopup_get_n_children(AtkObject *obj)
118 {
119    Evas_Object *nested_widget = NULL;
120
121    nested_widget = _eail_get_nested_widget(obj);
122    if (nested_widget)
123      return 1;
124
125    return 0;
126 }
127
128 /**
129  * @brief Gets a reference to the specified accessible child of the object
130  *
131  * The accessible children are 0-based so the first accessible child
132  * is at index 0, the second at index 1 and so on.
133  *
134  * Implementation of AtkObject->ref_child callback.
135  *
136  * @param obj AtkObject instance
137  * @param i index of the child
138  *
139  * @returns AtkObject representing the specified accessible child of the
140  * accessible
141  */
142 static AtkObject *
143 eail_ctxpopup_ref_child(AtkObject *obj, gint i)
144 {
145    Evas_Object *nested_widget = NULL;
146    AtkObject *atk_obj;
147
148    nested_widget = _eail_get_nested_widget(obj);
149    atk_obj = eail_factory_get_accessible(nested_widget);
150
151    if (atk_obj)
152      g_object_ref(atk_obj);
153
154    return atk_obj;
155 }
156
157 /**
158  * @brief Initializer for EailPopup GObject class
159  *
160  * Defines callbacks for base AtkObject.
161  *
162  * @param klass EailCtxpopupClass instance
163  */
164 static void
165 eail_ctxpopup_class_init(EailCtxpopupClass *klass)
166 {
167    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
168    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
169
170    atk_class->initialize = eail_ctxpopup_initialize;
171    atk_class->get_n_children = eail_ctxpopup_get_n_children;
172    atk_class->ref_child = eail_ctxpopup_ref_child;
173    gobject_class->finalize = eail_ctxpopup_finalize;
174 }
175
176 /*
177  * Implementation of the *AtkAction* interface
178  */
179
180 /**
181  * @brief Gets the number of accessible actions available on the object
182  *
183  * If there are more than one, the first one is considered
184  * the "default" action of the object.
185  *
186  * Implementation of get_n_actions from AtkAction interface.
187  *
188  * @param action AtkAction instance
189  *
190  * @return integer representing the number of available actions
191  */
192 static int
193 eail_ctxpopup_n_actions_get(AtkAction *action)
194 {
195    return 1;
196 }
197
198 /**
199  * @brief Gets the description string of the specified action of the object
200  *
201  * Implementation of get_description from AtkAction interface.
202  *
203  * @param action AtkAction instance
204  * @param i action index
205  *
206  * @return string representing the specified action's description
207  */
208 static const char*
209 eail_ctxpopup_description_get(AtkAction *action,
210                               gint i)
211 {
212    EailCtxpopup *ctxpopup;
213    const char *action_description;
214
215    ctxpopup = EAIL_CTXPOPUP(action);
216    if (!ctxpopup) return NULL;
217
218    switch (i)
219      {
220       case 0:
221          action_description = ctxpopup->dismiss_description;
222          break;
223       default:
224          action_description = NULL;
225          break;
226      }
227
228    return action_description;
229 }
230
231 /**
232  * @brief Sets a description of the specified action of the object
233  *
234  * Implementation of set_description from AtkAction interface.
235  *
236  * @param action AtkAction instance
237  * @param i action index
238  * @param description action description
239  *
240  * @return TRUE on success, FALSE otherwise
241  */
242 static gboolean
243 eail_ctxpopup_description_set(AtkAction *action,
244                               gint i,
245                               const char *description)
246 {
247    EailCtxpopup *ctxpopup;
248    char **value;
249
250    ctxpopup = EAIL_CTXPOPUP(action);
251    if (!ctxpopup) return FALSE;
252
253    switch (i)
254      {
255       case 0:
256          value = &ctxpopup->dismiss_description;
257          break;
258       default:
259          value = NULL;
260          break;
261      }
262
263    if (value)
264      {
265         free(*value);
266         *value = g_strdup(description);
267         return TRUE;
268      }
269
270    return FALSE;
271 }
272
273 /**
274  * @brief Gets the name string of the specified action of the object
275  *
276  * Implementation of get_name from AtkAction interface.
277  *
278  * @param action AtkAction instance
279  * @param i action index
280  *
281  * @return string representing the specified action's name
282  */
283 static const char*
284 eail_ctxpopup_action_name_get(AtkAction *action,
285                               int i)
286 {
287    const char* action_name;
288
289    switch (i)
290      {
291       case 0:
292          action_name = "dismiss";
293          break;
294       default:
295          action_name = NULL;
296          break;
297      }
298
299    return action_name;
300 }
301
302 /**
303  * @brief Performs the specified action on the object
304  *
305  * Implementation of do_action from AtkAction interface.
306  *
307  * @param action AtkAction instance
308  * @param i action index
309  *
310  * @return TRUE on success, FALSE otherwise
311  */
312 static gboolean
313 eail_ctxpopup_do_action(AtkAction *action,
314                         int i)
315 {
316    Evas_Object *widget;
317
318    widget = eail_widget_get_widget(EAIL_WIDGET(action));
319    if (!widget) return FALSE;
320
321    if ((elm_object_disabled_get(widget)) || (!evas_object_visible_get(widget)))
322      return FALSE;
323
324    const char *action_name = atk_action_get_name(action, i);
325    if (!action_name) return FALSE;
326
327    if (!g_strcmp0(action_name, "dismiss"))
328      elm_ctxpopup_dismiss(widget);
329    else
330      return FALSE;
331
332    return TRUE;
333 }
334
335 /**
336  * @brief AtkAction interface initializer
337  *
338  * Function called upon instance creation. It initializes AtkAction interface
339  * implementation i.e hooks method pointers in the interface structure
340  * to the implementing class's implementation.
341  *
342  * @param iface AtkActionIface instance
343  */
344 static void
345 atk_action_interface_init(AtkActionIface *iface)
346 {
347    g_return_if_fail(iface != NULL);
348
349    iface->get_n_actions   = eail_ctxpopup_n_actions_get;
350    iface->get_description = eail_ctxpopup_description_get;
351    iface->set_description = eail_ctxpopup_description_set;
352    iface->get_name        = eail_ctxpopup_action_name_get;
353    iface->do_action       = eail_ctxpopup_do_action;
354 }