Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_spinner.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_spinner.c
22  * @brief EailSpinner implementation
23  */
24
25 #include <Elementary.h>
26
27 #include "eail_spinner.h"
28 #include "eail_utils.h"
29
30 static void atk_value_interface_init(AtkValueIface *iface);
31
32 /**
33  * @brief EailSpinner type definition
34  */
35 G_DEFINE_TYPE_WITH_CODE(EailSpinner,
36                         eail_spinner,
37                         EAIL_TYPE_WIDGET,
38                         G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE,
39                                               atk_value_interface_init));
40
41 /**
42 * @brief handler for changed event
43 *
44 * @param data passed to callback
45 * @param obj object that raised event
46 * @param event_info additional event info
47 */
48
49 void
50 _eail_spinner_handle_changed_event(void *data,
51                                      Evas_Object *obj,
52                                      void *event_info)
53 {
54    eail_emit_atk_signal
55                   (ATK_OBJECT(data), "visible-data-changed", ATK_TYPE_OBJECT);
56 }
57
58 /**
59  * @brief EailSpinner interface initializer
60  *
61  * @param spinner EailSpinner instance
62  */
63 static void
64 eail_spinner_init(EailSpinner *spinner)
65 {
66 }
67
68 /**
69  * @brief EailSpinner initializer
70  *
71  * @param obj AtkObject instance
72  * @param data initialization data
73  */
74 void
75 eail_spinner_initialize(AtkObject *obj, gpointer data)
76 {
77    Evas_Object *nested_widget = NULL;
78    ATK_OBJECT_CLASS(eail_spinner_parent_class)->initialize(obj, data);
79
80    obj->role = ATK_ROLE_SPIN_BUTTON;
81    g_return_if_fail(EAIL_IS_WIDGET(obj));
82
83    nested_widget = eail_widget_get_widget(EAIL_WIDGET(obj));
84    evas_object_smart_callback_add(nested_widget, "changed",
85                                   _eail_spinner_handle_changed_event, obj);
86 }
87
88 /**
89  * @brief Gets the accessible name of the accessible
90  *
91  * @param accessible AtkObject instance
92  * @return string representing the accessible name of the accessible
93  */
94 const gchar*
95 eail_spinner_get_name(AtkObject *accessible)
96 {
97    Evas_Object *widget;
98    const gchar *name;
99
100    name = ATK_OBJECT_CLASS(eail_spinner_parent_class)->get_name(accessible);
101    if (name) return name;
102
103    widget = eail_widget_get_widget(EAIL_WIDGET(accessible));
104    if (widget)
105      name = (const gchar*)elm_object_text_get(widget);
106
107    return name;
108 }
109
110 /**
111  * @brief EailSpinner class initializer
112  *
113  * @param klass EailSpinnerClass instance
114  */
115 static void
116 eail_spinner_class_init(EailSpinnerClass *klass)
117 {
118    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
119
120    atk_class->initialize = eail_spinner_initialize;
121    atk_class->get_name = eail_spinner_get_name;
122 }
123
124 /**
125  * @brief Gets the current value of obj
126  *
127  * @param obj AtkValue instance
128  * @param [out] value obj's current value
129  */
130 static void
131 eail_spinner_get_current_value(AtkValue *obj,
132                                GValue   *value)
133 {
134    Evas_Object *widget;
135
136    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
137    if (!widget) return;
138
139    memset(value, 0, sizeof(GValue));
140    g_value_init(value, G_TYPE_DOUBLE);
141    g_value_set_double(value, elm_spinner_value_get(widget));
142 }
143
144 /**
145  * @brief Gets the maximum value of obj
146  *
147  * @param obj AtkValue instance
148  * @param [out] value obj's maxiumum value
149  */
150 static void
151 eail_spinner_get_maximum_value(AtkValue *obj,
152                                GValue   *value)
153 {
154    Evas_Object *widget;
155    double max;
156
157    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
158    if (!widget) return;
159
160    memset(value, 0, sizeof(GValue));
161    g_value_init(value, G_TYPE_DOUBLE);
162    elm_spinner_min_max_get(widget, NULL, &max);
163    g_value_set_double(value, max);
164 }
165
166 /**
167  * @brief Gets the minimum value of obj
168  *
169  * @param obj AtkValue instance
170  * @param [out] value obj's minimum value
171  */
172 static void
173 eail_spinner_get_minimum_value(AtkValue *obj,
174                                GValue   *value)
175 {
176    Evas_Object *widget;
177    double min;
178
179    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
180    if (!widget) return;
181
182    memset(value, 0, sizeof(GValue));
183    g_value_init(value, G_TYPE_DOUBLE);
184    elm_spinner_min_max_get(widget, &min, NULL);
185    g_value_set_double(value, min);
186 }
187
188 /**
189  * @brief Sets the current value of obj
190  *
191  * @param obj AtkValue instance
192  * @param value obj's new value
193  * @return TRUE if new value was set successfully, FALSE otherwise
194  */
195 static gboolean
196 eail_spinner_set_current_value(AtkValue     *obj,
197                                const GValue *value)
198 {
199    Evas_Object *widget;
200    double min, max, val;
201
202    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
203    if (!widget) return FALSE;
204
205    val = g_value_get_double(value);
206    elm_spinner_min_max_get(widget, &min, &max);
207
208    if (min > val || max < val)
209      return FALSE;
210
211    elm_spinner_value_set(widget, val);
212
213    return TRUE;
214 }
215
216 /**
217  * @brief Gets the minimum increment of obj
218  *
219  * @param obj AtkValue instance
220  * @param [out] value obj's minimum increment
221  */
222 static void
223 eail_spinner_get_minimum_increment(AtkValue *obj,
224                                    GValue *value)
225 {
226    Evas_Object *widget;
227
228    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
229    if (!widget) return;
230
231    memset(value, 0, sizeof(GValue));
232    g_value_init(value, G_TYPE_DOUBLE);
233    g_value_set_double(value, elm_spinner_step_get(widget));
234 }
235
236 /**
237  * @brief AtkValue interface initializer
238  *
239  * @param iface AtkValueIface instance
240  */
241 static void
242 atk_value_interface_init(AtkValueIface *iface)
243 {
244    g_return_if_fail(iface != NULL);
245
246    iface->get_current_value = eail_spinner_get_current_value;
247    iface->get_maximum_value = eail_spinner_get_maximum_value;
248    iface->get_minimum_value = eail_spinner_get_minimum_value;
249    iface->set_current_value = eail_spinner_set_current_value;
250    iface->get_minimum_increment = eail_spinner_get_minimum_increment;
251 }