Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_flip.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_flip.c
22  *
23  * @brief EailFlip implementation
24  */
25
26 #include <Elementary.h>
27
28 #include "eail_flip.h"
29 #include "eail_utils.h"
30 #include "eail_priv.h"
31
32 static void atk_action_interface_init(AtkActionIface *iface);
33
34 /**
35  * @brief Define EailFlip GObject type
36  */
37 G_DEFINE_TYPE_WITH_CODE(EailFlip,
38                         eail_flip,
39                         EAIL_TYPE_WIDGET,
40                         G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION,
41                                               atk_action_interface_init));
42
43 /**
44  * @brief Handler for event which is raised when flip's visible data is changed
45  *
46  * @param data data passed to callback
47  * @param obj Evas_Object that raised event
48  * @param event_info additional event info
49  */
50 void
51 _eail_flip_handle_changed_event(void *data,
52                                 Evas_Object *obj,
53                                 void *event_info)
54 {
55    eail_emit_atk_signal
56                   (ATK_OBJECT(data), "visible-data-changed", ATK_TYPE_OBJECT);
57 }
58
59 /**
60  * @brief EailFlip object initialization
61  *
62  * @param obj AtkObject instance
63  * @param data user set additional initialization data
64  */
65 static void
66 eail_flip_initialize(AtkObject *obj, gpointer data)
67 {
68    Evas_Object *nested_widget = NULL;
69    ATK_OBJECT_CLASS(eail_flip_parent_class)->initialize(obj, data);
70
71    obj->role = ATK_ROLE_PAGE_TAB_LIST;
72
73    g_return_if_fail(EAIL_IS_WIDGET(obj));
74    nested_widget = eail_widget_get_widget(EAIL_WIDGET(obj));
75    if (!nested_widget)
76      {
77         ERR("No evas object inside EailWidget was found");
78         return;
79      }
80
81    evas_object_smart_callback_add(nested_widget, "animate,done",
82                                   _eail_flip_handle_changed_event, obj);
83 }
84
85 /**
86  * @brief Class destructor
87  *
88  * @param object GObject instance
89  */
90 static void
91 eail_flip_finalize(GObject *object)
92 {
93    EailFlip *flip = EAIL_FLIP(object);
94
95    if (flip->flip_description) free(flip->flip_description);
96
97    G_OBJECT_CLASS(eail_flip_parent_class)->finalize(object);
98 }
99
100 /**
101  * @brief EailFlip instance initialization
102  *
103  * @param flip EailFlip instance
104  */
105 static void
106 eail_flip_init(EailFlip *flip)
107 {
108    flip->flip_description = NULL;
109 }
110
111 /**
112  * @brief Gets widget's children
113  *
114  * @param widget EailWidget instance
115  *
116  * @return Eina_List representing the list of children,
117  * or NULL if widget has no children
118  */
119 static Eina_List *
120 eail_flip_get_widget_children(EailWidget *widget)
121 {
122    Eina_List *list = NULL;
123    Evas_Object *child, *obj;
124
125    obj = eail_widget_get_widget(EAIL_WIDGET(widget));
126
127    if (obj)
128      {
129         if (EINA_TRUE == elm_flip_front_visible_get(obj))
130           child = elm_object_part_content_get(obj, "front");
131         else
132           child = elm_object_part_content_get(obj, "back");
133
134         if (child && elm_object_widget_check(child))
135           list = eina_list_append(list, child);
136      }
137
138    return list;
139 }
140
141 /**
142  * @brief GObject type initialization function
143  *
144  * @param klass EailFlipClass instance
145  */
146 static void
147 eail_flip_class_init(EailFlipClass *klass)
148 {
149    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
150    EailWidgetClass *widget_class = EAIL_WIDGET_CLASS(klass);
151    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
152
153    atk_class->initialize = eail_flip_initialize;
154    widget_class->get_widget_children = eail_flip_get_widget_children;
155    gobject_class->finalize = eail_flip_finalize;
156 }
157
158 /*
159  * Implementation of the *AtkAction* interface
160  */
161
162 /**
163  * @brief Gets the number of accessible actions available on the object
164  *
165  * If there are more than one, the first one is considered the "default" action of the object.
166  *
167  * Implementation of get_n_actions from AtkAction interface.
168  *
169  * @param action AtkAction instance
170  * @returns integer representing the number of implemented actions
171  */
172 static int
173 eail_flip_n_actions_get(AtkAction *action)
174 {
175    return 1;
176 }
177
178 /**
179  * @brief Gets the description string of the specified action
180  *
181  * Implementation of get_description from AtkAction interface.
182  *
183  * @param action AtkAction instance
184  * @param i action index
185  *
186  * @return string representing the specified action's description
187  */
188 static const char*
189 eail_flip_description_get(AtkAction *action,
190                           gint i)
191 {
192    EailFlip *flip;
193    const char *action_description;
194
195    flip = EAIL_FLIP(action);
196    if (!flip) return NULL;
197
198    switch (i)
199      {
200       case 0:
201          action_description = flip->flip_description;
202          break;
203       default:
204          action_description = NULL;
205          break;
206      }
207
208    return action_description;
209 }
210
211 /**
212  * @brief Sets a description of the specified action of the object
213  *
214  * Implementation of set_description from AtkAction interface.
215  *
216  * @param action AtkAction instance
217  * @param i action index
218  * @param description action's description
219  *
220  * @return TRUE on success, FALSE otherwise
221  */
222 static gboolean
223 eail_flip_description_set(AtkAction *action,
224                           gint i,
225                           const char *description)
226 {
227    EailFlip *flip;
228    char **value;
229
230    flip = EAIL_FLIP(action);
231    if (!flip) return FALSE;
232
233    switch (i)
234      {
235       case 0:
236          value = &flip->flip_description;
237          break;
238       default:
239          value = NULL;
240          break;
241      }
242
243    if (value)
244      {
245         free(*value);
246         *value = g_strdup(description);
247         return TRUE;
248      }
249
250    return FALSE;
251 }
252
253 /**
254  * @brief Gets the name string of the specified action
255  *
256  * Implementation of get_name from AtkAction interface.
257  *
258  * @param action AtkAction instance
259  * @param i action index
260  *
261  * @return string representing the specified action's name
262  */
263 static const char*
264 eail_flip_action_name_get(AtkAction *action,
265                          int i)
266 {
267    const char* action_name;
268
269    switch (i)
270      {
271       case 0:
272          action_name = "flip";
273          break;
274       default:
275          action_name = NULL;
276          break;
277      }
278
279    return action_name;
280 }
281
282 /**
283  * @brief Performs the specified action on the object
284  *
285  * Implementation of do_action from AtkAction interface.
286  *
287  * @param action AtkAction instance
288  * @param i action index
289  *
290  * @return TRUE on success, FALSE otherwise
291  */
292 static gboolean
293 eail_flip_do_action(AtkAction *action,
294                     int i)
295 {
296    Evas_Object *widget;
297
298    widget = eail_widget_get_widget(EAIL_WIDGET(action));
299    if (!widget) return FALSE;
300
301    if ((elm_object_disabled_get(widget)) || (!evas_object_visible_get(widget)))
302      return FALSE;
303
304    const char *action_name = atk_action_get_name(action, i);
305    if (!action_name) return FALSE;
306
307    if (EINA_TRUE == elm_flip_front_visible_get(widget))
308      elm_flip_go_to(widget, EINA_FALSE, ELM_FLIP_CUBE_DOWN);  // turn down
309    else
310      elm_flip_go_to(widget, EINA_TRUE, ELM_FLIP_CUBE_UP);     // turn up
311
312    return TRUE;
313 }
314
315 /**
316  * @brief AtkAction interface initializer
317  *
318  * @param iface AtkActionIface instance
319  */
320 static void
321 atk_action_interface_init(AtkActionIface *iface)
322 {
323    g_return_if_fail(iface != NULL);
324
325    iface->get_n_actions   = eail_flip_n_actions_get;
326    iface->get_description = eail_flip_description_get;
327    iface->set_description = eail_flip_description_set;
328    iface->get_name        = eail_flip_action_name_get;
329    iface->do_action       = eail_flip_do_action;
330 }