Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_toolbar.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_toolbar.c
22  * @brief EailToolbar implementation
23  */
24
25 #include <atk/atk.h>
26 #include <Elementary.h>
27
28 #include "eail_toolbar.h"
29 #include "eail_factory.h"
30 #include "eail_item_parent.h"
31 #include "eail_priv.h"
32
33 static void eail_item_parent_interface_init(EailItemParentIface *iface);
34
35 /**
36  * @brief Definition of EailToolbar as GObject
37  *
38  * EailToolbar is extended EAIL_TYPE_WIDGET with EAIL_TYPE_ITEM_PARENT interface
39  * implemented
40  */
41 G_DEFINE_TYPE_WITH_CODE(EailToolbar, eail_toolbar, EAIL_TYPE_WIDGET,
42                         G_IMPLEMENT_INTERFACE(EAIL_TYPE_ITEM_PARENT,
43                                               eail_item_parent_interface_init)
44 );
45
46 /**
47  * @brief Implementation of the *AtkObject* interface
48  *
49  * Basic initialization for AtkObject (parent initialization, role definition
50  * etc.).
51  *
52  * @param obj AtkObject instance
53  * @param data additional data passed to initialization
54  */
55 static void
56 eail_toolbar_initialize(AtkObject *obj, gpointer data)
57 {
58    ATK_OBJECT_CLASS(eail_toolbar_parent_class) ->initialize(obj, data);
59    obj->role = ATK_ROLE_TOOL_BAR;
60 }
61
62 /**
63  * @brief EailToolbar instance initializer
64  *
65  * @param toolbar EailToolbar instance
66  */
67 static void
68 eail_toolbar_init(EailToolbar *toolbar)
69 {
70
71 }
72
73 /**
74  * @brief Gets toolbar items
75  *
76  * @param toolbar EailToolbar instance
77  * @return Eina_List representing the list of Elm_Object_Item objects
78  * */
79 static Eina_List *
80 eail_toolbar_get_items(EailToolbar *toolbar)
81 {
82    Eina_List *items = NULL;
83    Elm_Object_Item *item;
84    Evas_Object *widget = eail_widget_get_widget(EAIL_WIDGET(toolbar));
85
86    item = elm_toolbar_first_item_get(widget);
87    while (item)
88      {
89         items = eina_list_append(items, item);
90         item = elm_toolbar_item_next_get(item);
91      }
92
93    return items;
94 }
95
96 /**
97  * @brief Gets the number of accessible children of the accessible
98  *
99  * Implementation of AtkObject->get_n_children callback.
100  *
101  * @param obj AtkObject instance
102  *
103  * @returns integer representing the number of accessible children of
104  * the accessible
105  */
106 static gint
107 eail_toolbar_get_n_children(AtkObject *obj)
108 {
109    gint n_items;
110    Eina_List *items;
111
112    items = eail_toolbar_get_items(EAIL_TOOLBAR(obj));
113    n_items = eina_list_count(items);
114
115    eina_list_free(items);
116
117    return n_items;
118 }
119
120 /**
121  * @brief Gets a reference to the specified accessible child of the object.
122  *
123  * The accessible children are 0-based so the first accessible child is at index 0,
124  * the second at index 1 and so on.
125  *
126  * Implementation of AtkObject->ref_child callback.
127  *
128  * @param obj AtkObject instance
129  * @param i index of a child
130  *
131  * @returns AtkObject representing the specified accessible child of the
132  * accessible
133  */
134 static AtkObject *
135 eail_toolbar_ref_child(AtkObject *obj, gint i)
136 {
137    Eina_List *items;
138    AtkObject *child = NULL;
139
140    items = eail_toolbar_get_items(EAIL_TOOLBAR(obj) );
141    if (eina_list_count(items) > i)
142      {
143         child = eail_factory_get_item_atk_obj
144                              (eina_list_nth(items, i), ATK_ROLE_MENU_ITEM, obj);
145
146         g_object_ref(child);
147      }
148    else
149      ERR("Tried to ref child with index %d out of bounds!", i);
150
151    eina_list_free(items);
152
153    return child;
154 }
155
156 /**
157  * @brief Destructor for EailToolbar object
158  *
159  * @param object GObject corresponding to EailToolbar
160  */
161 static void
162 eail_toolbar_finalize(GObject *object)
163 {
164    G_OBJECT_CLASS(eail_toolbar_parent_class)->finalize(object);
165 }
166
167 /**
168  * @brief Initializer for EailToolbar GObject class
169  *
170  * Defines callbacks for base AtkObject.
171  *
172  * @param klass EailToolbarClass instance
173  */
174 static void
175 eail_toolbar_class_init(EailToolbarClass *klass)
176 {
177    AtkObjectClass *class = ATK_OBJECT_CLASS(klass);
178    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
179    class->initialize = eail_toolbar_initialize;
180    class->get_n_children = eail_toolbar_get_n_children;
181    class->ref_child = eail_toolbar_ref_child;
182    gobject_class->finalize = eail_toolbar_finalize;
183 }
184
185 /**
186  * @brief Gets the object of item
187  *
188  * @param parent EailItemParent instance
189  * @param item EailItem instance
190  * @returns Evas_Object representing the item
191  */
192 static Evas_Object *
193 eail_toolbar_get_evas_obj(EailItemParent   *parent,
194                           EailItem         *item)
195 {
196    Elm_Object_Item *it = eail_item_get_item(item);
197
198    if (!it)
199      {
200         ERR("Error. Could not get Elm_Object_item from EailItem");
201         return NULL;
202      }
203
204    return elm_toolbar_item_object_get(it);
205 }
206
207 /**
208  * @brief Gets supported actions
209  *
210  * Implementation of EailItemParent->get_actions_supported callback.
211  *
212  * @param parent EailItemParent instance
213  * @param item EailItem instance
214  * @returns integer representing supported actions
215  */
216 static gint
217 eail_toolbar_get_actions_supported(EailItemParent   *parent,
218                                    EailItem         *item)
219 {
220    return EAIL_ACTION_SUPPORTED_CLICK;
221 }
222
223 /**
224  * @brief Gets the accessible name of the item
225  *
226  * @param parent EailItemParent instance
227  * @param item EailItem instance
228  *
229  * @returns string representing the accessible name of the item
230  */
231 static const gchar *
232 eail_toolbar_item_name_get(EailItemParent *parent, EailItem *item)
233 {
234    Elm_Object_Item *it = eail_item_get_item(item);
235    if (!it) return NULL;
236
237    return elm_object_item_part_text_get(it, NULL);
238 }
239
240 /**
241  * @brief Gets index in parent
242  *
243  * @param parent EailItemParent instance
244  * @param item EailItem child instance
245  *
246  * @returns int representing the index in parent
247  */
248 static gint
249 eail_toolbar_get_item_index_in_parent(EailItemParent *parent, EailItem *item)
250 {
251    Elm_Object_Item *it = eail_item_get_item(item);
252    Eina_List *items;
253    gint index = -1;
254    int i = 0;
255
256    if (!it) return -1;
257
258    items = eail_toolbar_get_items(EAIL_TOOLBAR(ATK_OBJECT(parent)));
259    for (i=0; i<eina_list_count(items); ++i)
260    {
261       if (eina_list_nth(items, i) == it)
262       {
263          index = i;
264          break;
265       }
266    }
267
268    eina_list_free(items);
269    return index;
270 }
271
272 /**
273  * @brief Initialization of EailItemParentIface interface callbacks
274  *
275  * Function called upon instance creation. It initializes EailItemParent
276  * interface implementation i.e hooks method pointers in the interface structure
277  * to the implementing class's implementation.
278  *
279  * @param iface EailItemParent interface
280  */
281 static void
282 eail_item_parent_interface_init(EailItemParentIface *iface)
283 {
284    iface->get_item_name            = eail_toolbar_item_name_get;
285    iface->get_evas_obj             = eail_toolbar_get_evas_obj;
286    iface->get_actions_supported    = eail_toolbar_get_actions_supported;
287    iface->get_item_index_in_parent = eail_toolbar_get_item_index_in_parent;
288 }