1 #include <Elementary.h>
4 #define DEFAULT_FIRST_INTERVAL 0.85
5 typedef struct _Widget_Data Widget_Data;
10 double interval, first_interval;
11 Eina_Bool seconds : 1;
14 Elm_Clock_Edit_Mode digedit;
15 int hrs, min, sec, timediff;
16 Evas_Object *digit[6];
19 Ecore_Timer *ticker, *spin;
24 Eina_Bool seconds : 1;
27 Elm_Clock_Edit_Mode digedit;
31 static const char *widtype = NULL;
32 static void _del_hook(Evas_Object *obj);
33 static void _theme_hook(Evas_Object *obj);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
35 static Eina_Bool _ticker(void *data);
36 static Eina_Bool _signal_clock_val_up(void *data);
37 static Eina_Bool _signal_clock_val_down(void *data);
38 static void _time_update(Evas_Object *obj);
40 static const char SIG_CHANGED[] = "changed";
42 static const Evas_Smart_Cb_Description _signals[] = {
49 _del_hook(Evas_Object *obj)
51 Widget_Data *wd = elm_widget_data_get(obj);
54 for (i = 0; i < 6; i++)
56 if (wd->digit[i]) evas_object_del(wd->digit[i]);
58 if (wd->ampm) evas_object_del(wd->ampm);
59 if (wd->ticker) ecore_timer_del(wd->ticker);
60 if (wd->spin) ecore_timer_del(wd->spin);
65 _theme_hook(Evas_Object *obj)
67 Widget_Data *wd = elm_widget_data_get(obj);
69 if (elm_widget_focus_get(obj))
70 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
72 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
73 wd->cur.am_pm = !wd->cur.am_pm; /* hack - force update */
78 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
80 Widget_Data *wd = elm_widget_data_get(obj);
82 if (elm_widget_focus_get(obj))
84 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
85 evas_object_focus_set(wd->clk, EINA_TRUE);
89 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
90 evas_object_focus_set(wd->clk, EINA_FALSE);
95 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
97 Widget_Data *wd = elm_widget_data_get(obj);
100 edje_object_signal_emit(wd->clk, emission, source);
101 for (i = 0; i < 6; i++)
104 edje_object_signal_emit(wd->digit[i], emission, source);
109 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
111 Widget_Data *wd = elm_widget_data_get(obj);
114 edje_object_signal_callback_add(wd->clk, emission, source, func_cb, data);
115 for (i = 0; i < 6; i++)
118 edje_object_signal_callback_add(wd->digit[i], emission, source,
124 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
126 Widget_Data *wd = elm_widget_data_get(obj);
128 for (i = 0; i < 6; i++)
130 edje_object_signal_callback_del_full(wd->digit[i], emission, source,
133 edje_object_signal_callback_del_full(wd->clk, emission, source, func_cb,
138 _timediff_set(Widget_Data *wd)
140 struct timeval timev;
143 gettimeofday(&timev, NULL);
144 tt = (time_t)(timev.tv_sec);
147 wd->timediff = (((wd->hrs - tm->tm_hour) * 60 +
148 wd->min - tm->tm_min) * 60) + wd->sec - tm->tm_sec;
154 Widget_Data *wd = elm_widget_data_get(data);
156 struct timeval timev;
159 if (!wd) return ECORE_CALLBACK_CANCEL;
160 gettimeofday(&timev, NULL);
161 t = ((double)(1000000 - timev.tv_usec)) / 1000000.0;
162 wd->ticker = ecore_timer_add(t, _ticker, data);
165 tt = (time_t)(timev.tv_sec) + wd->timediff;
170 wd->hrs = tm->tm_hour;
171 wd->min = tm->tm_min;
172 wd->sec = tm->tm_sec;
176 return ECORE_CALLBACK_CANCEL;
180 _signal_clock_val_up(void *data)
182 Widget_Data *wd = elm_widget_data_get(data);
183 if (!wd) goto clock_val_up_exit_on_error;
184 if (!wd->edit) goto clock_val_up_cancel;
185 if (!wd->sel_obj) goto clock_val_up_cancel;
186 if (wd->sel_obj == wd->digit[0])
188 wd->hrs = wd->hrs + 10;
189 if (wd->hrs >= 24) wd->hrs -= 24;
191 if (wd->sel_obj == wd->digit[1])
193 wd->hrs = wd->hrs + 1;
194 if (wd->hrs >= 24) wd->hrs -= 24;
196 if (wd->sel_obj == wd->digit[2])
198 wd->min = wd->min + 10;
199 if (wd->min >= 60) wd->min -= 60;
201 if (wd->sel_obj == wd->digit[3])
203 wd->min = wd->min + 1;
204 if (wd->min >= 60) wd->min -= 60;
206 if (wd->sel_obj == wd->digit[4])
208 wd->sec = wd->sec + 10;
209 if (wd->sec >= 60) wd->sec -= 60;
211 if (wd->sel_obj == wd->digit[5])
213 wd->sec = wd->sec + 1;
214 if (wd->sec >= 60) wd->sec -= 60;
216 if (wd->sel_obj == wd->ampm)
218 wd->hrs = wd->hrs + 12;
219 if (wd->hrs > 23) wd->hrs -= 24;
221 wd->interval = wd->interval / 1.05;
222 ecore_timer_interval_set(wd->spin, wd->interval);
224 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
225 return ECORE_CALLBACK_RENEW;
228 clock_val_up_exit_on_error:
229 return ECORE_CALLBACK_CANCEL;
233 _signal_clock_val_down(void *data)
235 Widget_Data *wd = elm_widget_data_get(data);
236 if (!wd) goto clock_val_down_exit_on_error;
237 if (!wd->edit) goto clock_val_down_cancel;
238 if (!wd->sel_obj) goto clock_val_down_cancel;
239 if (wd->sel_obj == wd->digit[0])
241 wd->hrs = wd->hrs - 10;
242 if (wd->hrs < 0) wd->hrs += 24;
244 if (wd->sel_obj == wd->digit[1])
246 wd->hrs = wd->hrs - 1;
247 if (wd->hrs < 0) wd->hrs += 24;
249 if (wd->sel_obj == wd->digit[2])
251 wd->min = wd->min - 10;
252 if (wd->min < 0) wd->min += 60;
254 if (wd->sel_obj == wd->digit[3])
256 wd->min = wd->min - 1;
257 if (wd->min < 0) wd->min += 60;
259 if (wd->sel_obj == wd->digit[4])
261 wd->sec = wd->sec - 10;
262 if (wd->sec < 0) wd->sec += 60;
264 if (wd->sel_obj == wd->digit[5])
266 wd->sec = wd->sec - 1;
267 if (wd->sec < 0) wd->sec += 60;
269 if (wd->sel_obj == wd->ampm)
271 wd->hrs = wd->hrs - 12;
272 if (wd->hrs < 0) wd->hrs += 24;
274 wd->interval = wd->interval / 1.05;
275 ecore_timer_interval_set(wd->spin, wd->interval);
277 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
278 return ECORE_CALLBACK_RENEW;
279 clock_val_down_cancel:
281 clock_val_down_exit_on_error:
282 return ECORE_CALLBACK_CANCEL;
286 _signal_clock_val_up_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
288 Widget_Data *wd = elm_widget_data_get(data);
290 wd->interval = wd->first_interval;
292 if (wd->spin) ecore_timer_del(wd->spin);
293 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_up, data);
294 _signal_clock_val_up(data);
298 _signal_clock_val_down_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
300 Widget_Data *wd = elm_widget_data_get(data);
302 wd->interval = wd->first_interval;
304 if (wd->spin) ecore_timer_del(wd->spin);
305 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_down, data);
306 _signal_clock_val_down(data);
310 _signal_clock_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
312 Widget_Data *wd = elm_widget_data_get(data);
314 if (wd->spin) ecore_timer_del(wd->spin);
320 _time_update(Evas_Object *obj)
322 Widget_Data *wd = elm_widget_data_get(obj);
323 Edje_Message_Int msg;
325 const char *style = elm_widget_style_get(obj);
327 if ((wd->cur.seconds != wd->seconds) || (wd->cur.am_pm != wd->am_pm) ||
328 (wd->cur.edit != wd->edit) || (wd->cur.digedit != wd->digedit))
333 for (i = 0; i < 6; i++)
337 evas_object_del(wd->digit[i]);
343 evas_object_del(wd->ampm);
347 if ((wd->seconds) && (wd->am_pm))
348 _elm_theme_object_set(obj, wd->clk, "clock", "base-all", style);
349 else if (wd->seconds)
350 _elm_theme_object_set(obj, wd->clk, "clock", "base-seconds", style);
352 _elm_theme_object_set(obj, wd->clk, "clock", "base-am_pm", style);
354 _elm_theme_object_set(obj, wd->clk, "clock", "base", style);
355 edje_object_scale_set(wd->clk, elm_widget_scale_get(obj) *
358 for (i = 0; i < 6; i++)
362 if ((!wd->seconds) && (i >= 4)) break;
363 wd->digit[i] = edje_object_add(evas_object_evas_get(wd->clk));
364 _elm_theme_object_set(obj, wd->digit[i], "clock", "flipdigit", style);
365 edje_object_scale_set(wd->digit[i], elm_widget_scale_get(obj) *
367 if ((wd->edit) && (wd->digedit & (1 << i)))
368 edje_object_signal_emit(wd->digit[i], "elm,state,edit,on", "elm");
369 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,start",
370 "", _signal_clock_val_up_start, obj);
371 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,stop",
372 "", _signal_clock_val_change_stop, obj);
373 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,start",
374 "", _signal_clock_val_down_start, obj);
375 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,stop",
376 "", _signal_clock_val_change_stop, obj);
378 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
379 edje_object_size_min_restricted_calc(wd->digit[i], &mw, &mh, mw, mh);
380 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
381 edje_extern_object_min_size_set(wd->digit[i], mw, mh);
382 snprintf(buf, sizeof(buf), "d%i", i);
383 edje_object_part_swallow(wd->clk , buf, wd->digit[i]);
384 evas_object_show(wd->digit[i]);
388 wd->ampm = edje_object_add(evas_object_evas_get(wd->clk));
389 _elm_theme_object_set(obj, wd->ampm, "clock", "flipampm", style);
390 edje_object_scale_set(wd->ampm, elm_widget_scale_get(obj) *
393 edje_object_signal_emit(wd->ampm, "elm,state,edit,on", "elm");
394 edje_object_signal_callback_add(wd->ampm, "elm,action,up,start",
395 "", _signal_clock_val_up_start, obj);
396 edje_object_signal_callback_add(wd->ampm, "elm,action,up,stop",
397 "", _signal_clock_val_change_stop, obj);
398 edje_object_signal_callback_add(wd->ampm, "elm,action,down,start",
399 "", _signal_clock_val_down_start, obj);
400 edje_object_signal_callback_add(wd->ampm, "elm,action,down,stop",
401 "", _signal_clock_val_change_stop, obj);
403 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
404 edje_object_size_min_restricted_calc(wd->ampm, &mw, &mh, mw, mh);
405 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
406 edje_extern_object_min_size_set(wd->ampm, mw, mh);
407 edje_object_part_swallow(wd->clk , "ampm", wd->ampm);
408 evas_object_show(wd->ampm);
411 edje_object_size_min_calc(wd->clk, &mw, &mh);
412 evas_object_size_hint_min_set(obj, mw, mh);
418 wd->cur.seconds = wd->seconds;
419 wd->cur.am_pm = wd->am_pm;
420 wd->cur.edit = wd->edit;
421 wd->cur.digedit = wd->digedit;
423 if (wd->hrs != wd->cur.hrs)
426 int d1, d2, dc1, dc2;
433 if (hrs > 12) hrs -= 12;
436 else if (!hrs) hrs = 12;
440 dc1 = wd->cur.hrs / 10;
441 dc2 = wd->cur.hrs % 10;
445 edje_object_message_send(wd->digit[0], EDJE_MESSAGE_INT, 1, &msg);
450 edje_object_message_send(wd->digit[1], EDJE_MESSAGE_INT, 1, &msg);
454 if (wd->min != wd->cur.min)
456 int d1, d2, dc1, dc2;
460 dc1 = wd->cur.min / 10;
461 dc2 = wd->cur.min % 10;
465 edje_object_message_send(wd->digit[2], EDJE_MESSAGE_INT, 1, &msg);
470 edje_object_message_send(wd->digit[3], EDJE_MESSAGE_INT, 1, &msg);
472 wd->cur.min = wd->min;
476 if (wd->sec != wd->cur.sec)
478 int d1, d2, dc1, dc2;
482 dc1 = wd->cur.sec / 10;
483 dc2 = wd->cur.sec % 10;
487 edje_object_message_send(wd->digit[4], EDJE_MESSAGE_INT, 1, &msg);
492 edje_object_message_send(wd->digit[5], EDJE_MESSAGE_INT, 1, &msg);
494 wd->cur.sec = wd->sec;
502 if (wd->hrs >= 12) ampm = 1;
503 if (ampm != wd->cur.ampm)
505 if (wd->cur.ampm != ampm)
508 edje_object_message_send(wd->ampm, EDJE_MESSAGE_INT, 1, &msg);
518 elm_clock_add(Evas_Object *parent)
524 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
526 ELM_SET_WIDTYPE(widtype, "clock");
527 elm_widget_type_set(obj, "clock");
528 elm_widget_sub_object_add(parent, obj);
529 elm_widget_data_set(obj, wd);
530 elm_widget_del_hook_set(obj, _del_hook);
531 elm_widget_theme_hook_set(obj, _theme_hook);
532 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
533 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
534 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
535 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
536 elm_widget_can_focus_set(obj, EINA_TRUE);
538 wd->clk = edje_object_add(e);
539 elm_widget_resize_object_set(obj, wd->clk);
542 wd->cur.seconds = EINA_TRUE;
543 wd->cur.am_pm = EINA_TRUE;
544 wd->cur.edit = EINA_TRUE;
545 wd->cur.digedit = ELM_CLOCK_EDIT_DEFAULT;
546 wd->first_interval = DEFAULT_FIRST_INTERVAL;
552 evas_object_smart_callbacks_descriptions_set(obj, _signals);
558 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
560 ELM_CHECK_WIDTYPE(obj, widtype);
561 Widget_Data *wd = elm_widget_data_get(obj);
571 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
573 ELM_CHECK_WIDTYPE(obj, widtype);
574 Widget_Data *wd = elm_widget_data_get(obj);
576 if (hrs) *hrs = wd->hrs;
577 if (min) *min = wd->min;
578 if (sec) *sec = wd->sec;
582 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
584 ELM_CHECK_WIDTYPE(obj, widtype);
585 Widget_Data *wd = elm_widget_data_get(obj);
590 if ((edit) && (wd->digedit == ELM_CLOCK_EDIT_DEFAULT))
591 elm_clock_edit_mode_set(obj, ELM_CLOCK_EDIT_ALL);
597 elm_clock_edit_get(const Evas_Object *obj)
599 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
600 Widget_Data *wd = elm_widget_data_get(obj);
601 if (!wd) return EINA_FALSE;
606 elm_clock_edit_mode_set(Evas_Object *obj, Elm_Clock_Edit_Mode digedit)
608 ELM_CHECK_WIDTYPE(obj, widtype);
609 Widget_Data *wd = elm_widget_data_get(obj);
611 wd->digedit = digedit;
612 if (digedit == ELM_CLOCK_EDIT_DEFAULT)
613 elm_clock_edit_set(obj, EINA_FALSE);
618 EAPI Elm_Clock_Edit_Mode
619 elm_clock_edit_mode_get(const Evas_Object *obj)
621 ELM_CHECK_WIDTYPE(obj, widtype) 0;
622 Widget_Data *wd = elm_widget_data_get(obj);
628 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
630 ELM_CHECK_WIDTYPE(obj, widtype);
631 Widget_Data *wd = elm_widget_data_get(obj);
638 elm_clock_show_am_pm_get(const Evas_Object *obj)
640 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
641 Widget_Data *wd = elm_widget_data_get(obj);
642 if (!wd) return EINA_FALSE;
647 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
649 ELM_CHECK_WIDTYPE(obj, widtype);
650 Widget_Data *wd = elm_widget_data_get(obj);
652 wd->seconds = !!seconds;
657 elm_clock_show_seconds_get(const Evas_Object *obj)
659 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
660 Widget_Data *wd = elm_widget_data_get(obj);
661 if (!wd) return EINA_FALSE;
666 elm_clock_first_interval_set(Evas_Object *obj, double interval)
668 ELM_CHECK_WIDTYPE(obj, widtype);
669 Widget_Data *wd = elm_widget_data_get(obj);
671 wd->first_interval = !!interval;
675 elm_clock_first_interval_get(const Evas_Object *obj)
677 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
678 Widget_Data *wd = elm_widget_data_get(obj);
680 return wd->first_interval;