Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_clock.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_clock.c
22  * @brief EailClock implementation
23  */
24
25 #include <Elementary.h>
26
27 #include "eail_clock.h"
28 #include "eail_utils.h"
29
30 static void atk_value_interface_init(AtkValueIface *iface);
31
32 /**
33  * @brief Define EailClock GObject type
34  */
35 G_DEFINE_TYPE_WITH_CODE(EailClock,
36                         eail_clock,
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_clock_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  * Implementation of the *AtkObject* interface
60  */
61
62 /**
63  * @brief EailClock object initialization
64  *
65  * @param obj EailClock object
66  * @param data additional initialization data
67  */
68 static void
69 eail_clock_initialize(AtkObject *obj, gpointer data)
70 {
71    Evas_Object *nested_widget = NULL;
72    ATK_OBJECT_CLASS(eail_clock_parent_class)->initialize(obj, data);
73
74    obj->role = ATK_ROLE_TEXT;
75    g_return_if_fail(EAIL_IS_WIDGET(obj));
76
77    nested_widget = eail_widget_get_widget(EAIL_WIDGET(obj));
78    evas_object_smart_callback_add(nested_widget, "changed",
79                                   _eail_clock_handle_changed_event, obj);
80 }
81
82 /**
83  * @brief EailClock instance initialization
84  *
85  * @param clock EailClock instance
86  */
87 static void
88 eail_clock_init(EailClock *clock)
89 {
90 }
91
92 /**
93  * @brief GObject type initialization function
94  *
95  * @param klass EailClockClass instance
96  */
97 static void
98 eail_clock_class_init(EailClockClass *klass)
99 {
100    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
101
102    atk_class->initialize = eail_clock_initialize;
103 }
104
105 /*
106  * Implementation of the *AtkValue* interface
107  */
108
109 /**
110  * @brief Gets obj's current value
111  *
112  * @param obj EailClock instance
113  * @param [out] value obj's current value
114  */
115 static void
116 eail_clock_get_current_value(AtkValue *obj,
117                              GValue   *value)
118 {
119    Evas_Object *widget;
120    int hour, minute, second;
121    int current_second_day;
122
123    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
124    if (!widget) return;
125
126    elm_clock_time_get(widget, &hour, &minute, &second);
127
128    current_second_day = (3600 * hour) + (60 * minute) + second;
129
130    memset(value, 0, sizeof(GValue));
131    g_value_init(value, G_TYPE_INT);
132    g_value_set_int(value, current_second_day);
133 }
134
135 /**
136  * @brief Gets obj's maxiumum value
137  *
138  * @param obj EailClock instance
139  * @param [out] value obj's maxiumum value
140  */
141 static void
142 eail_clock_get_maximum_value(AtkValue *obj,
143                              GValue   *value)
144 {
145    Evas_Object *widget;
146
147    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
148    if (!widget) return;
149
150    memset(value, 0, sizeof(GValue));
151    g_value_init(value, G_TYPE_INT);
152    g_value_set_int(value, 24 * 60 * 60 - 1);  // 24h - 1s
153 }
154
155 /**
156  * @brief Gets obj's minimum value
157  *
158  * @param obj EailClock instance
159  * @param [out] value obj's minimum value
160  */
161 static void
162 eail_clock_get_minimum_value(AtkValue *obj,
163                              GValue   *value)
164 {
165    Evas_Object *widget;
166
167    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
168    if (!widget) return;
169
170    memset(value, 0, sizeof(GValue));
171    g_value_init(value, G_TYPE_INT);
172    g_value_set_int(value, 0);                 // 0 - seconds
173 }
174
175 /**
176  * @brief Sets obj's value
177  *
178  * @param obj EailClock instance
179  * @param value obj's new value
180  * @return TRUE if new value was set successfully, FALSE otherwise
181  */
182 static gboolean
183 eail_clock_set_current_value(AtkValue     *obj,
184                              const GValue *value)
185 {
186    Evas_Object *widget;
187    int hour, minute, second;
188    int current_second_day;
189
190    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
191    if (!widget) return FALSE;
192
193    current_second_day = g_value_get_int(value);
194
195    if (0 > current_second_day)
196      return FALSE;
197
198    hour = current_second_day / 3600;
199    minute = (current_second_day % 3600) / 60;
200    second = current_second_day % 60;
201
202    if (23 < hour || 59 < minute || 59 < second)
203      return FALSE;
204
205    elm_clock_time_set(widget, hour, minute, second);
206
207    return TRUE;
208 }
209
210 /**
211  * @brief Gets obj's minimum increment
212  *
213  * @param obj EailClock instance
214  * @param [out] value obj's minimum increment
215  */
216 static void
217 eail_clock_get_minimum_increment(AtkValue *obj,
218                                  GValue   *value)
219 {
220    Evas_Object *widget;
221
222    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
223    if (!widget) return;
224
225    memset(value, 0, sizeof(GValue));
226    g_value_init(value, G_TYPE_INT);
227    g_value_set_int(value, 1);
228 }
229
230 /**
231  * @brief Initialization of AtkValue interface
232  *
233  * @brief Function called upon instance creation.
234  *
235  * It initializes AtkValue interface
236  * implementation i.e hooks method pointers in the interface structure
237  * to the implementing class's implementation.
238  *
239  * @param iface AtkObject that implements AtkValueInterface
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_clock_get_current_value;
247    iface->get_maximum_value     = eail_clock_get_maximum_value;
248    iface->get_minimum_value     = eail_clock_get_minimum_value;
249    iface->set_current_value     = eail_clock_set_current_value;
250    iface->get_minimum_increment = eail_clock_get_minimum_increment;
251 }