Issues deteced by Static Code Analysis fixed
[platform/core/uifw/eail.git] / eail / eail / eail_naviframe_page.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_naviframe_page.c
22  * @brief EailNaviframePage implementation
23  */
24
25 #include "eail_naviframe_page.h"
26 #include "eail_naviframe.h"
27 #include "eail_factory.h"
28 #include "eail_widget.h"
29 #include "eail_priv.h"
30
31 static void atk_component_interface_init(AtkComponentIface *iface);
32
33 /**
34  * Defines EailNaviframePage type
35  */
36 G_DEFINE_TYPE_WITH_CODE(EailNaviframePage,
37                         eail_naviframe_page,
38                         ATK_TYPE_OBJECT,
39                         G_IMPLEMENT_INTERFACE(ATK_TYPE_COMPONENT,
40                                               atk_component_interface_init));
41
42 /**
43  * @brief Creates a new instance of an accessible naviframe page
44  *
45  * @param naviframe AtkObject instance
46  * @param navi_tab_item Elm_Object_Item instance
47  * @returns AtkObject representing the accessible naviframe page
48  */
49 AtkObject *
50 eail_naviframe_page_new(AtkObject *naviframe, Elm_Object_Item *navi_tab_item)
51 {
52    AtkObject *atk_object;
53    EailNaviframePage *page;
54    Evas_Object *o, *widget;
55
56    g_return_val_if_fail(EAIL_IS_NAVIFRAME(naviframe), NULL);
57
58    widget = eail_widget_get_widget(EAIL_WIDGET(naviframe));
59
60    g_return_val_if_fail(widget != NULL, NULL);
61
62    atk_object = g_object_new(EAIL_TYPE_NAVIFRAME_PAGE, NULL);
63    page = EAIL_NAVIFRAME_PAGE(atk_object);
64
65    page->naviframe = widget;
66
67    page->page = navi_tab_item;
68
69    page->child_count = 0;
70
71    o = elm_object_item_part_content_get(page->page, "prev_btn");
72    if (o && elm_object_widget_check(o))
73      page->content[page->child_count++] = o;
74
75    o = elm_object_item_part_content_get(page->page, "icon");
76    if (o && elm_object_widget_check(o))
77      page->content[page->child_count++] = o;
78
79    o = elm_object_item_part_content_get(page->page, "next_btn");
80    if (o && elm_object_widget_check(o))
81      page->content[page->child_count++] = o;
82
83    o = elm_object_item_part_content_get(page->page, "default");
84    if (o && elm_object_widget_check(o))
85      page->content[page->child_count++] = o;
86
87    page->parent_naviframe = naviframe;
88
89    atk_object->layer = ATK_LAYER_WIDGET;
90
91    return atk_object;
92 }
93
94 /**
95  * @brief Returns the accessible name if assigned, title or subtitle otherwise
96  *
97  * Implementation of atk_object_get_name from AtkObject.
98  *
99  * @param obj AtkObject instance
100  * @returns string representing the accessible name if assigned, title or subtitle otherwise
101  */
102 static const char *
103 eail_naviframe_page_name_get(AtkObject *obj)
104 {
105    g_return_val_if_fail(EAIL_IS_NAVIFRAME_PAGE(obj), NULL);
106
107    EailNaviframePage *page = EAIL_NAVIFRAME_PAGE(obj);
108    if (page->name != NULL) return page->name;
109
110    const char *title = elm_object_item_part_text_get(page->page, "default");
111    const char *subtitle = elm_object_item_part_text_get(page->page, "subtitle");
112
113    if (subtitle != NULL)
114      page->name = eina_stringshare_printf("%s\n%s", title, subtitle);
115    else
116      page->name = eina_stringshare_add(title);
117
118    return page->name;
119 }
120
121 /**
122  * @brief Initiates EailNaviframePage
123  *
124  * @param naviframe_page EailNaviframePage instance
125  */
126 static void
127 eail_naviframe_page_init(EailNaviframePage *naviframe_page)
128 {
129 }
130
131 /**
132  * @brief Initializes EailNaviframPage
133  *
134  * @param obj AtkObject instance
135  * @param data user data
136  */
137 static void
138 eail_naviframe_page_initialize(AtkObject *obj, gpointer data)
139 {
140    ATK_OBJECT_CLASS(eail_naviframe_page_parent_class)->initialize(obj, data);
141
142    obj->role = ATK_ROLE_PAGE_TAB;
143    obj->layer = ATK_LAYER_WIDGET;
144 }
145
146 /**
147  * @brief Finalizes object
148  *
149  * @param obj AtkObject instance
150  */
151 static void
152 eail_naviframe_page_finalize(GObject *obj)
153 {
154    EailNaviframePage *page = EAIL_NAVIFRAME_PAGE(obj);
155
156    eina_stringshare_del(page->name);
157
158    if (page->page)
159      eail_factory_unregister_item_from_cache(page->page);
160
161    G_OBJECT_CLASS(eail_naviframe_page_parent_class)->finalize(obj);
162 }
163
164 /**
165  * @brief Gets the number of accessible children of obj
166  *
167  * @param obj AtkObject instance
168  *
169  * @returns integer representing the number of children
170  */
171 static gint
172 eail_naviframe_page_n_children_get(AtkObject *obj)
173 {
174    EailNaviframePage *page;
175
176    g_return_val_if_fail(EAIL_IS_NAVIFRAME_PAGE(obj), 0);
177    page = EAIL_NAVIFRAME_PAGE(obj);
178
179    return page->child_count;
180 }
181
182 /**
183  * @brief Gets obj's parent
184  *
185  * @param obj AtkObject instance
186  *
187  * @returns AtkObject representing the parent of obj
188  */
189 static AtkObject *
190 eail_naviframe_page_parent_get(AtkObject *obj)
191 {
192    EailNaviframePage *page;
193
194    g_return_val_if_fail(EAIL_IS_NAVIFRAME_PAGE(obj), NULL);
195
196    page = EAIL_NAVIFRAME_PAGE(obj);
197
198    return ATK_OBJECT(page->parent_naviframe);
199 }
200
201 /**
202  * @brief Gets a reference to the specified child of obj
203  *
204  * @param obj AtkObject instance
205  * @param i child index
206  *
207  * @returns AtkObject representing the specified child
208  */
209 static AtkObject *
210 eail_naviframe_page_ref_child(AtkObject *obj, gint i)
211 {
212    EailNaviframePage *page;
213    AtkObject *atk_obj_child = NULL;
214
215    g_return_val_if_fail(EAIL_IS_NAVIFRAME_PAGE(obj), NULL);
216
217    page = EAIL_NAVIFRAME_PAGE(obj);
218    if (i >= page->child_count) return NULL;
219
220    atk_obj_child = eail_factory_get_accessible(page->content[i]);
221
222    g_object_ref(atk_obj_child);
223    return atk_obj_child;
224 }
225
226 /**
227  * @brief Gets the index of obj in parent object
228  *
229  * @param obj AtkObject instance
230  *
231  * @returns integer representing the index of object
232  */
233 static gint
234 eail_naviframe_page_index_in_parent_get(AtkObject *obj)
235 {
236    EailNaviframePage *page = NULL;
237    Eina_List *list = NULL;
238    gint pos = -1, i = 0;
239
240    if (!EAIL_IS_NAVIFRAME_PAGE(obj))
241      {
242         ERR("Not a naviframe page");
243         return pos;
244      }
245
246    page = EAIL_NAVIFRAME_PAGE(obj);
247    if (!page->naviframe) return pos;
248
249    list = elm_naviframe_items_get(page->naviframe);
250    for (i = 0; i < eina_list_count(list); ++i)
251      {
252         if (page->page == eina_list_nth(list, i))
253           {
254              pos = i;
255              break;
256           }
257      }
258
259    eina_list_free(list);
260    return pos;
261 }
262
263 /**
264  * @brief Gets the state set of the accessible object
265  *
266  * @param obj AtkObject instance
267  *
268  * @returns AtkStateSet representing the state set of the accessible object
269  */
270 static AtkStateSet *
271 eail_naviframe_page_ref_state_set(AtkObject *obj)
272 {
273    EailNaviframePage *page;
274    AtkStateSet *state_set;
275    Evas_Object *widget;
276
277    g_return_val_if_fail(EAIL_IS_NAVIFRAME_PAGE(obj), NULL);
278
279    page = EAIL_NAVIFRAME_PAGE(obj);
280    if (!page->naviframe) return NULL;
281
282    widget = elm_object_part_content_get(page->naviframe, "default");
283
284    state_set = atk_state_set_new();
285
286    /* only item-page on top is visible*/
287    if (elm_naviframe_top_item_get(page->naviframe) == page->page)
288      {
289         atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
290         atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
291      }
292
293    if (!elm_object_disabled_get(widget))
294      atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
295
296    return state_set;
297 }
298
299 /**
300  * @brief EailNaviframePage class initializer
301  *
302  * @param klass EailNaviframePageClass instance
303  */
304 static void
305 eail_naviframe_page_class_init(EailNaviframePageClass *klass)
306 {
307    GObjectClass *g_object_class = G_OBJECT_CLASS(klass);
308    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
309
310    atk_class->initialize = eail_naviframe_page_initialize;
311    atk_class->get_name = eail_naviframe_page_name_get;
312    atk_class->get_parent = eail_naviframe_page_parent_get;
313    atk_class->get_n_children = eail_naviframe_page_n_children_get;
314    atk_class->ref_child = eail_naviframe_page_ref_child;
315    atk_class->ref_state_set = eail_naviframe_page_ref_state_set;
316    atk_class->get_index_in_parent = eail_naviframe_page_index_in_parent_get;
317
318    g_object_class->finalize = eail_naviframe_page_finalize;
319 }
320
321 /**
322  * @brief Gets the rectangle which gives the extent of the component
323  *
324  * Implementation of get_extents from AtkComponent interface.
325  *
326  * @param component AtkComponent instance
327  * @param [out] x x coordinate
328  * @param [out] y y coordinate
329  * @param [out] width width of the rectangle
330  * @param [out] height height of the rectangle
331  * @param coord_type specifies whether the coordinates are relative to the screen or to the component's top level window
332  */
333 static void
334 eail_naviframe_page_get_extents(AtkComponent *component,
335                                 gint *x,
336                                 gint *y,
337                                 gint *width,
338                                 gint *height,
339                                 AtkCoordType coord_type)
340 {
341    EailNaviframePage *page;
342    Evas_Object *widget;
343    g_return_if_fail(EAIL_IS_NAVIFRAME_PAGE(component));
344
345    page = EAIL_NAVIFRAME_PAGE(component);
346    *x = *y = *width = *height = G_MININT;
347    if (!page->naviframe) return;
348
349    widget = page->naviframe;
350
351    evas_object_geometry_get(widget, x, y, width, height);
352    if (coord_type == ATK_XY_SCREEN)
353      {
354         int ee_x, ee_y;
355
356         Ecore_Evas *ee = ecore_evas_ecore_evas_get(
357            evas_object_evas_get(widget));
358         ecore_evas_geometry_get(ee, &ee_x, &ee_y, NULL, NULL);
359         *x += ee_x;
360         *y += ee_y;
361      }
362 }
363
364 /**
365  * @brief AtkComponent interface initialization
366  *
367  * @param iface AtkComponentIface instance
368  */
369 static void atk_component_interface_init(AtkComponentIface *iface)
370 {
371    g_return_if_fail(iface != NULL);
372
373    iface->get_extents = eail_naviframe_page_get_extents;
374 }