Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_segment_control.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_segment_control.c
22  * @brief EailSegmentControl implementation
23  */
24
25 #include <atk/atk.h>
26 #include <Elementary.h>
27
28 #include "eail_segment_control.h"
29 #include "eail_factory.h"
30 #include "eail_item_parent.h"
31
32 static void atk_selection_interface_init(AtkSelectionIface *iface);
33 static void eail_item_parent_interface_init(EailItemParentIface *iface);
34
35 /**
36  * @brief Definition of EailSegmentControl as GObject
37  *
38  * EailSegmentControl is extended EAIL_TYPE_WIDGET with ATK_TYPE_SELECTION and
39  * EAIL_TYPE_ITEM_PARENT interfaces implemented
40  */
41 G_DEFINE_TYPE_WITH_CODE(EailSegmentControl, eail_segment_control, EAIL_TYPE_WIDGET,
42                         G_IMPLEMENT_INTERFACE(ATK_TYPE_SELECTION,
43                                               atk_selection_interface_init)
44                         G_IMPLEMENT_INTERFACE(EAIL_TYPE_ITEM_PARENT,
45                                             eail_item_parent_interface_init));
46
47 /**
48  * @brief Initializer for AtkObjectClass
49  *
50  * @param obj AtkObject instance
51  * @param data additional initialization data
52  */
53 static void
54 eail_segment_control_initialize(AtkObject *obj, gpointer data)
55 {
56    ATK_OBJECT_CLASS(eail_segment_control_parent_class)->initialize(obj, data);
57    obj->role = ATK_ROLE_LIST;
58 }
59
60 /**
61  * @brief Initializer for GObject class
62  *
63  * @param segment_control EailSegmentControl instance
64  */
65 static void
66 eail_segment_control_init(EailSegmentControl *segment_control)
67 {
68 }
69
70 /**
71  * @brief Gets the number of accessible children of the accessible
72  *
73  * Implementation of AtkObject->get_n_children callback.
74  *
75  * @param obj AtkObject instance
76  *
77  * @returns integer representing the number of accessible children of
78  * the accessible
79  */
80 static gint
81 eail_segment_control_get_n_children(AtkObject *obj)
82 {
83    gint n_items;
84
85    Evas_Object *widget = eail_widget_get_widget(EAIL_WIDGET(obj));
86    n_items = elm_segment_control_item_count_get(widget);
87
88    return n_items;
89 }
90
91 /**
92  * @brief Gets a reference to the specified accessible child of the object.
93  *
94  * The accessible children are 0-based so the first accessible child is
95  * at index 0, the second at index 1 and so on.
96  *
97  * Implementation AtkObject->ref_child callback.
98  *
99  * @param obj AtkObject instance
100  * @param i child index
101  *
102  * @returns AtkObject representing the specified accessible child of the
103  * accessible
104  */
105 static AtkObject *
106 eail_segment_control_ref_child(AtkObject *obj, gint i)
107 {
108    AtkObject *child = NULL;
109    Evas_Object *widget = NULL;
110
111    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
112    if (!widget) return NULL;
113
114    if (elm_segment_control_item_count_get(widget) > i)
115      {
116          child = eail_factory_get_item_atk_obj
117              (elm_segment_control_item_get(widget, i), ATK_ROLE_LIST_ITEM, obj);
118
119         g_object_ref(child);
120      }
121
122    return child;
123 }
124
125 /**
126  * @brief Destructor for Segment Control object
127  * @param object GObject instance
128  */
129 static void
130 eail_segment_control_finalize(GObject *object)
131 {
132    G_OBJECT_CLASS(eail_segment_control_parent_class)->finalize(object);
133 }
134
135 /**
136  * @brief Initializer for GObject EailSegmentControl class
137  *
138  * Defines callbacks for base AtkObject.
139  *
140  * @param klass EailSegmentControlClass instance
141  */
142 static void
143 eail_segment_control_class_init(EailSegmentControlClass *klass)
144 {
145    AtkObjectClass *class = ATK_OBJECT_CLASS(klass);
146    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
147    class->initialize = eail_segment_control_initialize;
148    class->get_n_children = eail_segment_control_get_n_children;
149    class->ref_child = eail_segment_control_ref_child;
150    gobject_class->finalize = eail_segment_control_finalize;
151 }
152
153 /**
154  * @brief Adds the specified accessible child of the object to the object's selection
155  *
156  * Implementation of AtkSelection->add_selection callback.
157  *
158  * @param selection AtkSelection instance
159  * @param i index of object
160  *
161  * @returns TRUE on success, FALSE otherwise
162  */
163 static gboolean
164 eail_selection_add_selection(AtkSelection *selection,
165                              gint i)
166 {
167    g_return_val_if_fail(EAIL_IS_WIDGET(selection), FALSE);
168    Evas_Object *object = eail_widget_get_widget(EAIL_WIDGET(selection));
169    Elm_Object_Item *item = elm_segment_control_item_get(object, i);
170    if (!item) return FALSE;
171    elm_segment_control_item_selected_set(item, EINA_TRUE);
172    return TRUE;
173 }
174
175 /**
176  * @brief Clears the selection in the object so that no children in the object are
177  * selected
178  *
179  * Implementation of AtkSelection->clear_selection callback.
180  *
181  * @param selection AtkSelection instance
182  *
183  * @returns TRUE on success, FALSE otherwise
184  */
185 static gboolean
186 eail_selection_clear_selection(AtkSelection *selection)
187 {
188    g_return_val_if_fail(EAIL_IS_WIDGET(selection), FALSE);
189    Evas_Object *object = eail_widget_get_widget(EAIL_WIDGET(selection));
190    Elm_Object_Item *item = elm_segment_control_item_selected_get(object);
191    elm_segment_control_item_selected_set(item, EINA_FALSE);
192    return TRUE;
193 }
194
195 /**
196  * @brief Gets a reference to the accessible object representing the specified selected
197  * child of the object.
198  *
199  * Note: callers should not rely on NULL or on a zero value
200  * for indication of whether AtkSelectionIface is implemented, they should use
201  * type checking/interface checking macros or the atk_get_accessible_value()
202  * convenience method.
203  *
204  * Implementation of AtkSelection->ref_selection callback.
205  *
206  * @param selection AtkSelection instance
207  * @param i index of object
208  *
209  * @returns AtkObject representing the selected accessible or NULL if
210  * selection does not implement this interface
211  */
212 static AtkObject *
213 eail_selection_ref_selection(AtkSelection *selection,
214                              gint i)
215 {
216    g_return_val_if_fail(EAIL_IS_WIDGET(selection), NULL);
217
218    Evas_Object *object = eail_widget_get_widget(EAIL_WIDGET(selection));
219    Elm_Object_Item *item = elm_segment_control_item_get(object, i);
220    if (!item) return NULL;
221
222    return eail_segment_control_ref_child(ATK_OBJECT(selection), i);
223 }
224
225 /**
226  * @brief Gets the number of accessible children currently selected.
227  *
228  * Note: callers should not rely on NULL or on a zero value for indication
229  * of whether AtkSelectionIface is implemented, they should use type
230  * checking/interface checking macros or the atk_get_accessible_value()
231  * convenience method.
232  *
233  * Implementation of AtkSelection->get_selection_count callback.
234  *
235  * @param selection AtkSelection instance
236  *
237  * @returns integer representing the number of selected elements
238  */
239 static gint
240 eail_selection_get_selection_count(AtkSelection *selection)
241 {
242    Evas_Object *object = eail_widget_get_widget(EAIL_WIDGET(selection));
243    Elm_Object_Item *item = elm_segment_control_item_selected_get(object);
244    if (item) return 1;
245    return 0;
246 }
247
248 /**
249  * @brief Determines if the current child of this object is selected.
250  *
251  * Note: callers should not rely on NULL or on a zero value for indication
252  * of whether AtkSelectionIface is implemented, they should use type
253  * checking/interface checking macros or the atk_get_accessible_value()
254  * convenience method.
255  *
256  * Implementation of AtkSelection->is_child_selected callback.
257  *
258  * @param selection AtkSelection instance
259  * @param i index of object
260  *
261  * @returns gboolean representing whether the specified child is selected or
262  * FALSE if selection does not implement this interface
263  */
264 static gboolean
265 eail_selection_is_child_selected(AtkSelection *selection,
266                                  gint i)
267 {
268    g_return_val_if_fail(EAIL_IS_WIDGET(selection), FALSE);
269    Evas_Object *object = eail_widget_get_widget(EAIL_WIDGET(selection));
270    Elm_Object_Item *selected_item = elm_segment_control_item_selected_get(object);
271    Elm_Object_Item *item = elm_segment_control_item_get(object, i);
272    return (selected_item == item);
273 }
274
275 /**
276  * @brief Removes the specified child of the object from the object's selection
277  *
278  * Implementation of AtkSelection->remove_selection callback.
279  *
280  * @param selection AtkSelection instance
281  * @param i selection index
282  *
283  * @returns TRUE on success, FALSE otherwise
284  */
285 static gboolean
286 eail_selection_remove_selection(AtkSelection *selection,
287                                 gint i)
288 {
289    g_return_val_if_fail(EAIL_IS_WIDGET(selection), FALSE);
290    Evas_Object *object = eail_widget_get_widget(EAIL_WIDGET(selection));
291    Elm_Object_Item *item = elm_segment_control_item_get(object, i);
292    if (!item) return FALSE;
293    elm_segment_control_item_selected_set(item, EINA_FALSE);
294    return TRUE;
295 }
296
297 /**
298  * @brief Causes every child of the object to be selected if the object supports
299  * multiple selections
300  *
301  * Implementation of AtkSelection->select_all_selection callback.
302  *
303  * @param selection AtkSelection instance
304  *
305  * @returns TRUE on success, FALSE otherwise
306  */
307 static gboolean
308 eail_selection_select_all_selection(AtkSelection *selection)
309 {
310    return FALSE;
311 }
312
313 /**
314  * @brief Initializer of AtkSelectionIface interface
315  *
316  * @param iface AtkSelection interface
317  */
318 static void atk_selection_interface_init(AtkSelectionIface *iface)
319 {
320    iface->add_selection = eail_selection_add_selection;
321    iface->clear_selection = eail_selection_clear_selection;
322    iface->ref_selection = eail_selection_ref_selection;
323    iface->get_selection_count = eail_selection_get_selection_count;
324    iface->is_child_selected = eail_selection_is_child_selected;
325    iface->remove_selection = eail_selection_remove_selection;
326    iface->select_all_selection = eail_selection_select_all_selection;
327 }
328
329 /**
330  * @brief Get the name of a segment control's child
331  *
332  * @param parent EailIemParent instance
333  * @param item EailItem instance
334  *
335  * @returns string representing the name of the child
336  */
337 static const gchar *
338 eail_segment_control_item_name_get(EailItemParent *parent, EailItem *item)
339 {
340    Elm_Object_Item *it = eail_item_get_item(item);
341    if (!it) return NULL;
342
343    return elm_object_item_part_text_get(it, NULL);
344 }
345
346 /**
347  * @brief Initialization of EailItemParentIface interface
348  *
349  * @param iface EailItemParent interface
350  */
351 static void
352 eail_item_parent_interface_init(EailItemParentIface *iface)
353 {
354    iface->get_item_name = eail_segment_control_item_name_get;
355 }