Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_datetime.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_datetime.c
22  *
23  * @brief EailDatetime implementation
24  */
25
26 #include <Elementary.h>
27
28 #include "eail_datetime.h"
29 #include "eail_utils.h"
30
31 static void atk_value_interface_init(AtkValueIface *iface);
32
33 /**
34  * @brief Define EailDatetime GObject type
35  */
36 G_DEFINE_TYPE_WITH_CODE(EailDatetime,
37                         eail_datetime,
38                         EAIL_TYPE_WIDGET,
39                         G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE,
40                                               atk_value_interface_init));
41
42 /**
43  * @brief handler for changed event
44  *
45  * @param data passed to callback
46  * @param obj object that raised event
47  * @param event_info additional event info
48  */
49
50 void
51 _eail_datetime_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 EailDatetime object initialization
61  *
62  * @param obj EailDatetime instance
63  * @param data additional initialization data
64  */
65 static void
66 eail_datetime_initialize(AtkObject *obj, gpointer data)
67 {
68    Evas_Object *nested_widget = NULL;
69    ATK_OBJECT_CLASS(eail_datetime_parent_class)->initialize(obj, data);
70
71    obj->role = ATK_ROLE_DATE_EDITOR;
72    g_return_if_fail(EAIL_IS_WIDGET(obj));
73
74    nested_widget = eail_widget_get_widget(EAIL_WIDGET(obj));
75    evas_object_smart_callback_add(nested_widget, "changed",
76                                   _eail_datetime_handle_changed_event, obj);
77 }
78
79 /**
80  * @brief EailDatetime instance initialization
81  *
82  * @param datetime EailDatetime instance
83  */
84 static void
85 eail_datetime_init(EailDatetime *datetime)
86 {
87 }
88
89 /**
90  * @brief GObject type initialization function
91  *
92  * @param klass EailDatetimeClass instance
93  */
94 static void
95 eail_datetime_class_init(EailDatetimeClass *klass)
96 {
97    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
98
99    atk_class->initialize = eail_datetime_initialize;
100 }
101
102 /**
103  * @brief Gets obj's current value
104  *
105  * Example:
106  * @code
107  * GValue value;
108  * struct tm *t;
109  * time_t time;
110  *
111  * //get current datetime value
112  * atk_value_get_minimum_value(obj, &value);
113  *
114  * //get time
115  * time = g_value_get_int(&value);
116  *
117  * //convert time to time structure (struct tm)
118  * t = localtime(&time);
119  * @endcode
120  *
121  * @param obj EailDatetime instance
122  * @param [out] value obj's current value
123  */
124 static void
125 eail_datetime_get_current_value(AtkValue *obj,
126                                 GValue   *value)
127 {
128    Evas_Object *widget;
129    struct tm datetime;
130
131    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
132    if (!widget) return;
133
134    elm_datetime_value_get(widget, &datetime);
135
136    memset(value, 0, sizeof(GValue));
137    g_value_init(value, G_TYPE_INT);
138    g_value_set_int(value, mktime(&datetime));
139 }
140
141 /**
142  * @brief Gets obj's maxiumum value
143  *
144  * @param obj EailDatetime instance
145  * @param [out] value obj's maximum value
146  */
147 static void
148 eail_datetime_get_maximum_value(AtkValue *obj,
149                                 GValue   *value)
150 {
151    Evas_Object *widget;
152    struct tm datetime;
153
154    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
155    if (!widget) return;
156
157    elm_datetime_value_max_get(widget, &datetime);
158
159    memset(value, 0, sizeof(GValue));
160    g_value_init(value, G_TYPE_INT);
161    g_value_set_int(value, mktime(&datetime));
162 }
163
164 /**
165  * @brief Gets obj's minimum value
166  *
167  * @param obj EailDatetime instance
168  * @param [out] value obj's minimum value
169  */
170 static void
171 eail_datetime_get_minimum_value(AtkValue *obj,
172                                 GValue   *value)
173 {
174    Evas_Object *widget;
175    struct tm datetime;
176
177    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
178    if (!widget) return;
179
180    elm_datetime_value_min_get(widget, &datetime);
181
182    memset(value, 0, sizeof(GValue));
183    g_value_init(value, G_TYPE_INT);
184    g_value_set_int(value, mktime(&datetime));
185 }
186
187 /**
188  * @brief Sets obj's value
189  *
190  * Example
191  * @code
192  * struct tm t;
193  * time_t time;
194  * GValue value;
195  *
196  * //Fill time structure with desired values(struct tm)
197  * memset(&t, 0, sizeof(t));
198  * t.tm_sec = 30;
199  * t.tm_min = 20;
200  * t.tm_hour = 10;
201  *
202  * //convert to int representation
203  * time = mktime(&t);
204  *
205  * //copy time to value
206  * memset(&value, 0, sizeof(value));
207  * g_value_init(&value, G_TYPE_INT);
208  * g_value_set_int(&value, time);
209  *
210  * //set time using atk
211  * atk_value_set_current_value(obj, &value);
212  * @endcode
213  *
214  * @param obj EailDatetime instance
215  * @param value EailDatetime new value
216  * @return TRUE if new value was set successfully, FALSE otherwise
217  */
218 static gboolean
219 eail_datetime_set_current_value(AtkValue     *obj,
220                                 const GValue *value)
221 {
222    Evas_Object *widget;
223    struct tm datetime;
224    time_t time;
225
226    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
227    if (!widget) return FALSE;
228
229    time = g_value_get_int(value);
230    localtime_r(&time, &datetime);
231
232    if (EINA_TRUE == elm_datetime_value_set(widget, &datetime))
233      return TRUE;
234    else
235      return FALSE;
236 }
237
238 /**
239  * @brief Gets obj's minimum increment
240  *
241  * @param obj EailDatetime instance
242  * @param [out] value obj's minimum increment
243  */
244 static void
245 eail_datetime_get_minimum_increment(AtkValue *obj,
246                                     GValue   *value)
247 {
248    Evas_Object *widget;
249
250    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
251    if (!widget) return;
252
253    memset(value, 0, sizeof(GValue));
254    g_value_init(value, G_TYPE_INT);
255    g_value_set_int(value, 1);
256 }
257
258 /**
259  * @brief AtkValue interface initializer
260  *
261  * @param iface AtkValueIface instance
262  */
263 static void
264 atk_value_interface_init(AtkValueIface *iface)
265 {
266    g_return_if_fail(iface != NULL);
267
268    iface->get_current_value     = eail_datetime_get_current_value;
269    iface->get_maximum_value     = eail_datetime_get_maximum_value;
270    iface->get_minimum_value     = eail_datetime_get_minimum_value;
271    iface->set_current_value     = eail_datetime_set_current_value;
272    iface->get_minimum_increment = eail_datetime_get_minimum_increment;
273 }