Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_plug.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_plug.c
22  * @brief EailPlug implementation
23  */
24
25 #include <Elementary.h>
26
27 #include "eail_plug.h"
28
29 static void atk_action_interface_init(AtkActionIface *iface);
30
31 /**
32  * @brief Definition of EailPlug as GObject
33  *
34  * EailPlug is extended EailWidget with AtkAction interface implemented
35  */
36 G_DEFINE_TYPE_WITH_CODE(EailPlug,
37                         eail_plug,
38                         EAIL_TYPE_WIDGET,
39                         G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION,
40                                               atk_action_interface_init));
41
42 /*
43  * Implementation of the *AtkObject* interface
44  */
45
46 /**
47  * @brief EailPlug object initialization
48  *
49  * @param obj AtkObject instance
50  * @param data user set additional initialization data
51  */
52 static void
53 eail_plug_initialize(AtkObject *obj, gpointer data)
54 {
55    ATK_OBJECT_CLASS(eail_plug_parent_class)->initialize(obj, data);
56
57    obj->role = ATK_ROLE_IMAGE;
58 }
59
60 /**
61  * @brief Class destructor
62  *
63  * @param object GObject instance
64  */
65 static void
66 eail_plug_finalize(GObject *object)
67 {
68    EailPlug *plug = EAIL_PLUG(object);
69
70    if (plug->click_description) free(plug->click_description);
71
72    G_OBJECT_CLASS(eail_plug_parent_class)->finalize(object);
73 }
74
75 /**
76  * @brief EailPlug instance initialization
77  *
78  * @param plug EailPlug instance
79  */
80 static void
81 eail_plug_init(EailPlug *plug)
82 {
83    plug->click_description = NULL;
84 }
85
86 /**
87  * @brief GObject type initialization function
88  *
89  * @param klass EailPlugClass instance
90  */
91 static void
92 eail_plug_class_init(EailPlugClass *klass)
93 {
94    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
95    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
96
97    atk_class->initialize = eail_plug_initialize;
98    gobject_class->finalize = eail_plug_finalize;
99 }
100
101 /*
102  * Implementation of the *AtkAction* interface
103  */
104
105 /**
106  * @brief Gets the number of accessible actions available on the object
107  *
108  * If there are more than one, the first one is considered
109  * the "default" action of the object.
110  *
111  * Implementation of get_n_actions from AtkAction interface.
112  *
113  * @param action AtkAction instance
114  *
115  * @returns integer representing the number of actions supported by EailPlug
116  */
117 static int
118 eail_plug_n_actions_get(AtkAction *action)
119 {
120    return 1;
121 }
122
123 /**
124  * @brief Gets the description of specified action of the object
125  *
126  * Implementation of get_description from AtkAction interface.
127  *
128  * @param action AtkAction instance
129  * @param i action index
130  *
131  * @return string representing the description of the specified action
132  */
133 static const char*
134 eail_plug_description_get(AtkAction *action,
135                             gint i)
136 {
137    const char *action_description;
138    EailPlug *plug = EAIL_PLUG(action);
139
140    switch (i)
141      {
142       case 0:
143          action_description = plug->click_description;
144          break;
145       default:
146          action_description = NULL;
147          break;
148      }
149
150    return action_description;
151 }
152
153 /**
154  * @brief Sets a description of the specified action of the object
155  *
156  * Implementation of set_description from AtkAction interface.
157  *
158  * @param action AtkAction instance
159  * @param i action index
160  * @param description action description
161  *
162  * @return TRUE on success, FALSE otherwise
163  */
164 static gboolean
165 eail_plug_description_set(AtkAction *action,
166                             gint i,
167                             const char *description)
168 {
169    EailPlug *plug = EAIL_PLUG(action);
170    char **value;
171
172    switch (i)
173      {
174       case 0:
175          value = &plug->click_description;
176          break;
177       default:
178          value = NULL;
179          break;
180      }
181
182    if (value)
183      {
184         free(*value);
185         *value = g_strdup(description);
186         return TRUE;
187      }
188
189    return FALSE;
190 }
191
192 /**
193  * @brief Gets the name of the specified action of the object
194  *
195  * Implementation of get_name from AtkAction interface.
196  *
197  * @param action AtkAction instance
198  * @param i action index
199  *
200  * @return string representing the name of the specified action
201  */
202 static const char*
203 eail_plug_action_name_get(AtkAction *action,
204                             int i)
205 {
206    const char* action_name;
207
208    switch (i)
209      {
210       case 0:
211          action_name = "click";
212          break;
213       default:
214          action_name = NULL;
215          break;
216      }
217
218    return action_name;
219 }
220
221 /**
222  * @brief Performs the specified action on the object
223  *
224  * Implementation of do_action from AtkAction interface.
225  *
226  * @param action AtkAction instance
227  * @param i action index
228  *
229  * @return TRUE on success, FALSE otherwise
230  */
231 static gboolean
232 eail_plug_do_action(AtkAction *action,
233                       int i)
234 {
235    const char *action_name;
236    Evas_Object *widget;
237
238    widget = eail_widget_get_widget(EAIL_WIDGET(action));
239    if (!widget) return FALSE;
240
241    if ((elm_object_disabled_get(widget)) || (!evas_object_visible_get(widget)))
242      return FALSE;
243
244    action_name = atk_action_get_name(action, i);
245    if (!action_name) return FALSE;
246
247    if (!g_strcmp0(action_name, "click"))
248      evas_object_smart_callback_call(widget, "clicked", NULL);
249    else
250      return FALSE;
251
252    return TRUE;
253 }
254
255 /**
256  * @brief AtkAction interface initializer
257  *
258  * @param iface AtkAction interface
259  */
260 static void
261 atk_action_interface_init(AtkActionIface *iface)
262 {
263    g_return_if_fail(iface != NULL);
264
265    iface->get_n_actions   = eail_plug_n_actions_get;
266    iface->get_description = eail_plug_description_get;
267    iface->set_description = eail_plug_description_set;
268    iface->get_name        = eail_plug_action_name_get;
269    iface->do_action       = eail_plug_do_action;
270 }