1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
9 double interval, first_interval;
10 Eina_Bool seconds : 1;
13 Elm_Clock_Digedit digedit;
14 int hrs, min, sec, timediff;
15 Evas_Object *digit[6];
18 Ecore_Timer *ticker, *spin;
23 Eina_Bool seconds : 1;
26 Elm_Clock_Digedit digedit;
30 static const char *widtype = NULL;
31 static void _del_hook(Evas_Object *obj);
32 static void _theme_hook(Evas_Object *obj);
33 static void _on_focus_hook(void *data, Evas_Object *obj);
34 static Eina_Bool _ticker(void *data);
35 static Eina_Bool _signal_clock_val_up(void *data);
36 static Eina_Bool _signal_clock_val_down(void *data);
37 static void _time_update(Evas_Object *obj);
39 static const char SIG_CHANGED[] = "changed";
41 static const Evas_Smart_Cb_Description _signals[] = {
48 _del_hook(Evas_Object *obj)
50 Widget_Data *wd = elm_widget_data_get(obj);
53 for (i = 0; i < 6; i++)
55 if (wd->digit[i]) evas_object_del(wd->digit[i]);
57 if (wd->ampm) evas_object_del(wd->ampm);
58 if (wd->ticker) ecore_timer_del(wd->ticker);
59 if (wd->spin) ecore_timer_del(wd->spin);
64 _theme_hook(Evas_Object *obj)
66 Widget_Data *wd = elm_widget_data_get(obj);
68 if (elm_widget_focus_get(obj))
69 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
71 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
72 wd->cur.am_pm = !wd->cur.am_pm; /* hack - force update */
77 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
81 if (elm_widget_focus_get(obj))
83 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
84 evas_object_focus_set(wd->clk, EINA_TRUE);
88 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
89 evas_object_focus_set(wd->clk, EINA_FALSE);
94 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
96 Widget_Data *wd = elm_widget_data_get(obj);
99 edje_object_signal_emit(wd->clk, emission, source);
100 for (i = 0; i < 6; i++)
103 edje_object_signal_emit(wd->digit[i], emission, source);
108 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
110 Widget_Data *wd = elm_widget_data_get(obj);
113 edje_object_signal_callback_add(wd->clk, emission, source, func_cb, data);
114 for (i = 0; i < 6; i++)
117 edje_object_signal_callback_add(wd->digit[i], emission, source,
123 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
125 Widget_Data *wd = elm_widget_data_get(obj);
127 for (i = 0; i < 6; i++)
129 edje_object_signal_callback_del_full(wd->digit[i], emission, source,
132 edje_object_signal_callback_del_full(wd->clk, emission, source, func_cb,
137 _timediff_set(Widget_Data *wd)
139 struct timeval timev;
142 gettimeofday(&timev, NULL);
143 tt = (time_t)(timev.tv_sec);
145 tm = calloc(1, sizeof(struct tm));
147 localtime_r(&tt, tm);
148 wd->timediff = (((wd->hrs - tm->tm_hour) * 60 +
149 wd->min - tm->tm_min) * 60) + wd->sec - tm->tm_sec;
156 Widget_Data *wd = elm_widget_data_get(data);
158 struct timeval timev;
161 if (!wd) return ECORE_CALLBACK_CANCEL;
162 gettimeofday(&timev, NULL);
163 t = ((double)(1000000 - timev.tv_usec)) / 1000000.0;
164 wd->ticker = ecore_timer_add(t, _ticker, data);
167 tt = (time_t)(timev.tv_sec) + wd->timediff;
169 tm = calloc(1, sizeof(struct tm));
170 if (!tm) return ECORE_CALLBACK_CANCEL;
171 localtime_r(&tt, tm);
172 wd->hrs = tm->tm_hour;
173 wd->min = tm->tm_min;
174 wd->sec = tm->tm_sec;
179 return ECORE_CALLBACK_CANCEL;
183 _signal_clock_val_up(void *data)
185 Widget_Data *wd = elm_widget_data_get(data);
186 if (!wd) goto clock_val_up_exit_on_error;
187 if (!wd->edit) goto clock_val_up_cancel;
188 if (!wd->sel_obj) goto clock_val_up_cancel;
189 if (wd->sel_obj == wd->digit[0])
191 wd->hrs = wd->hrs + 10;
192 if (wd->hrs >= 24) wd->hrs -= 24;
194 if (wd->sel_obj == wd->digit[1])
196 wd->hrs = wd->hrs + 1;
197 if (wd->hrs >= 24) wd->hrs -= 24;
199 if (wd->sel_obj == wd->digit[2])
201 wd->min = wd->min + 10;
202 if (wd->min >= 60) wd->min -= 60;
204 if (wd->sel_obj == wd->digit[3])
206 wd->min = wd->min + 1;
207 if (wd->min >= 60) wd->min -= 60;
209 if (wd->sel_obj == wd->digit[4])
211 wd->sec = wd->sec + 10;
212 if (wd->sec >= 60) wd->sec -= 60;
214 if (wd->sel_obj == wd->digit[5])
216 wd->sec = wd->sec + 1;
217 if (wd->sec >= 60) wd->sec -= 60;
219 if (wd->sel_obj == wd->ampm)
221 wd->hrs = wd->hrs + 12;
222 if (wd->hrs > 23) wd->hrs -= 24;
224 wd->interval = wd->interval / 1.05;
225 ecore_timer_interval_set(wd->spin, wd->interval);
227 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
228 return ECORE_CALLBACK_RENEW;
231 clock_val_up_exit_on_error:
232 return ECORE_CALLBACK_CANCEL;
236 _signal_clock_val_down(void *data)
238 Widget_Data *wd = elm_widget_data_get(data);
239 if (!wd) goto clock_val_down_exit_on_error;
240 if (!wd->edit) goto clock_val_down_cancel;
241 if (!wd->sel_obj) goto clock_val_down_cancel;
242 if (wd->sel_obj == wd->digit[0])
244 wd->hrs = wd->hrs - 10;
245 if (wd->hrs < 0) wd->hrs += 24;
247 if (wd->sel_obj == wd->digit[1])
249 wd->hrs = wd->hrs - 1;
250 if (wd->hrs < 0) wd->hrs += 24;
252 if (wd->sel_obj == wd->digit[2])
254 wd->min = wd->min - 10;
255 if (wd->min < 0) wd->min += 60;
257 if (wd->sel_obj == wd->digit[3])
259 wd->min = wd->min - 1;
260 if (wd->min < 0) wd->min += 60;
262 if (wd->sel_obj == wd->digit[4])
264 wd->sec = wd->sec - 10;
265 if (wd->sec < 0) wd->sec += 60;
267 if (wd->sel_obj == wd->digit[5])
269 wd->sec = wd->sec - 1;
270 if (wd->sec < 0) wd->sec += 60;
272 if (wd->sel_obj == wd->ampm)
274 wd->hrs = wd->hrs - 12;
275 if (wd->hrs < 0) wd->hrs += 24;
277 wd->interval = wd->interval / 1.05;
278 ecore_timer_interval_set(wd->spin, wd->interval);
280 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
281 return ECORE_CALLBACK_RENEW;
282 clock_val_down_cancel:
284 clock_val_down_exit_on_error:
285 return ECORE_CALLBACK_CANCEL;
289 _signal_clock_val_up_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
291 Widget_Data *wd = elm_widget_data_get(data);
293 wd->interval = wd->first_interval;
295 if (wd->spin) ecore_timer_del(wd->spin);
296 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_up, data);
297 _signal_clock_val_up(data);
301 _signal_clock_val_down_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
303 Widget_Data *wd = elm_widget_data_get(data);
305 wd->interval = wd->first_interval;
307 if (wd->spin) ecore_timer_del(wd->spin);
308 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_down, data);
309 _signal_clock_val_down(data);
313 _signal_clock_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
315 Widget_Data *wd = elm_widget_data_get(data);
317 if (wd->spin) ecore_timer_del(wd->spin);
323 _time_update(Evas_Object *obj)
325 Widget_Data *wd = elm_widget_data_get(obj);
326 Edje_Message_Int msg;
328 const char *style = elm_widget_style_get(obj);
330 if ((wd->cur.seconds != wd->seconds) || (wd->cur.am_pm != wd->am_pm) ||
331 (wd->cur.edit != wd->edit) || (wd->cur.digedit != wd->digedit))
336 for (i = 0; i < 6; i++)
340 evas_object_del(wd->digit[i]);
346 evas_object_del(wd->ampm);
350 if ((wd->seconds) && (wd->am_pm))
351 _elm_theme_object_set(obj, wd->clk, "clock", "base-all", style);
352 else if (wd->seconds)
353 _elm_theme_object_set(obj, wd->clk, "clock", "base-seconds", style);
355 _elm_theme_object_set(obj, wd->clk, "clock", "base-am_pm", style);
357 _elm_theme_object_set(obj, wd->clk, "clock", "base", style);
358 edje_object_scale_set(wd->clk, elm_widget_scale_get(obj) *
361 for (i = 0; i < 6; i++)
365 if ((!wd->seconds) && (i >= 4)) break;
366 wd->digit[i] = edje_object_add(evas_object_evas_get(wd->clk));
367 _elm_theme_object_set(obj, wd->digit[i], "clock", "flipdigit", style);
368 edje_object_scale_set(wd->digit[i], elm_widget_scale_get(obj) *
370 if ((wd->edit) && (wd->digedit & (1 << i)))
371 edje_object_signal_emit(wd->digit[i], "elm,state,edit,on", "elm");
372 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,start",
373 "", _signal_clock_val_up_start, obj);
374 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,stop",
375 "", _signal_clock_val_change_stop, obj);
376 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,start",
377 "", _signal_clock_val_down_start, obj);
378 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,stop",
379 "", _signal_clock_val_change_stop, obj);
381 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
382 edje_object_size_min_restricted_calc(wd->digit[i], &mw, &mh, mw, mh);
383 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
384 edje_extern_object_min_size_set(wd->digit[i], mw, mh);
385 snprintf(buf, sizeof(buf), "d%i", i);
386 edje_object_part_swallow(wd->clk , buf, wd->digit[i]);
387 evas_object_show(wd->digit[i]);
391 wd->ampm = edje_object_add(evas_object_evas_get(wd->clk));
392 _elm_theme_object_set(obj, wd->ampm, "clock", "flipampm", style);
393 edje_object_scale_set(wd->ampm, elm_widget_scale_get(obj) *
396 edje_object_signal_emit(wd->ampm, "elm,state,edit,on", "elm");
397 edje_object_signal_callback_add(wd->ampm, "elm,action,up,start",
398 "", _signal_clock_val_up_start, obj);
399 edje_object_signal_callback_add(wd->ampm, "elm,action,up,stop",
400 "", _signal_clock_val_change_stop, obj);
401 edje_object_signal_callback_add(wd->ampm, "elm,action,down,start",
402 "", _signal_clock_val_down_start, obj);
403 edje_object_signal_callback_add(wd->ampm, "elm,action,down,stop",
404 "", _signal_clock_val_change_stop, obj);
406 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
407 edje_object_size_min_restricted_calc(wd->ampm, &mw, &mh, mw, mh);
408 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
409 edje_extern_object_min_size_set(wd->ampm, mw, mh);
410 edje_object_part_swallow(wd->clk , "ampm", wd->ampm);
411 evas_object_show(wd->ampm);
414 edje_object_size_min_calc(wd->clk, &mw, &mh);
415 evas_object_size_hint_min_set(obj, mw, mh);
421 wd->cur.seconds = wd->seconds;
422 wd->cur.am_pm = wd->am_pm;
423 wd->cur.edit = wd->edit;
424 wd->cur.digedit = wd->digedit;
426 if (wd->hrs != wd->cur.hrs)
429 int d1, d2, dc1, dc2;
436 if (hrs > 12) hrs -= 12;
439 else if (!hrs) hrs = 12;
443 dc1 = wd->cur.hrs / 10;
444 dc2 = wd->cur.hrs % 10;
448 edje_object_message_send(wd->digit[0], EDJE_MESSAGE_INT, 1, &msg);
453 edje_object_message_send(wd->digit[1], EDJE_MESSAGE_INT, 1, &msg);
457 if (wd->min != wd->cur.min)
459 int d1, d2, dc1, dc2;
463 dc1 = wd->cur.min / 10;
464 dc2 = wd->cur.min % 10;
468 edje_object_message_send(wd->digit[2], EDJE_MESSAGE_INT, 1, &msg);
473 edje_object_message_send(wd->digit[3], EDJE_MESSAGE_INT, 1, &msg);
475 wd->cur.min = wd->min;
479 if (wd->sec != wd->cur.sec)
481 int d1, d2, dc1, dc2;
485 dc1 = wd->cur.sec / 10;
486 dc2 = wd->cur.sec % 10;
490 edje_object_message_send(wd->digit[4], EDJE_MESSAGE_INT, 1, &msg);
495 edje_object_message_send(wd->digit[5], EDJE_MESSAGE_INT, 1, &msg);
497 wd->cur.sec = wd->sec;
505 if (wd->hrs >= 12) ampm = 1;
506 if (ampm != wd->cur.ampm)
508 if (wd->cur.ampm != ampm)
511 edje_object_message_send(wd->ampm, EDJE_MESSAGE_INT, 1, &msg);
521 elm_clock_add(Evas_Object *parent)
527 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
529 ELM_SET_WIDTYPE(widtype, "clock");
530 elm_widget_type_set(obj, "clock");
531 elm_widget_sub_object_add(parent, obj);
532 elm_widget_data_set(obj, wd);
533 elm_widget_del_hook_set(obj, _del_hook);
534 elm_widget_theme_hook_set(obj, _theme_hook);
535 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
536 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
537 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
538 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
539 elm_widget_can_focus_set(obj, EINA_TRUE);
541 wd->clk = edje_object_add(e);
542 elm_widget_resize_object_set(obj, wd->clk);
545 wd->cur.seconds = EINA_TRUE;
546 wd->cur.am_pm = EINA_TRUE;
547 wd->cur.edit = EINA_TRUE;
548 wd->cur.digedit = ELM_CLOCK_NONE;
549 wd->first_interval = 0.85;
555 evas_object_smart_callbacks_descriptions_set(obj, _signals);
561 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
563 ELM_CHECK_WIDTYPE(obj, widtype);
564 Widget_Data *wd = elm_widget_data_get(obj);
574 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
576 ELM_CHECK_WIDTYPE(obj, widtype);
577 Widget_Data *wd = elm_widget_data_get(obj);
579 if (hrs) *hrs = wd->hrs;
580 if (min) *min = wd->min;
581 if (sec) *sec = wd->sec;
585 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
587 ELM_CHECK_WIDTYPE(obj, widtype);
588 Widget_Data *wd = elm_widget_data_get(obj);
593 if ((edit) && (wd->digedit == ELM_CLOCK_NONE))
594 elm_clock_digit_edit_set(obj, ELM_CLOCK_ALL);
600 elm_clock_edit_get(const Evas_Object *obj)
602 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
603 Widget_Data *wd = elm_widget_data_get(obj);
604 if (!wd) return EINA_FALSE;
609 elm_clock_digit_edit_set(Evas_Object *obj, Elm_Clock_Digedit digedit)
611 ELM_CHECK_WIDTYPE(obj, widtype);
612 Widget_Data *wd = elm_widget_data_get(obj);
614 wd->digedit = digedit;
615 if (digedit == ELM_CLOCK_NONE)
616 elm_clock_edit_set(obj, EINA_FALSE);
621 EAPI Elm_Clock_Digedit
622 elm_clock_digit_edit_get(const Evas_Object *obj)
624 ELM_CHECK_WIDTYPE(obj, widtype) 0;
625 Widget_Data *wd = elm_widget_data_get(obj);
631 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
633 ELM_CHECK_WIDTYPE(obj, widtype);
634 Widget_Data *wd = elm_widget_data_get(obj);
641 elm_clock_show_am_pm_get(const Evas_Object *obj)
643 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
644 Widget_Data *wd = elm_widget_data_get(obj);
645 if (!wd) return EINA_FALSE;
650 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
652 ELM_CHECK_WIDTYPE(obj, widtype);
653 Widget_Data *wd = elm_widget_data_get(obj);
655 wd->seconds = seconds;
660 elm_clock_show_seconds_get(const Evas_Object *obj)
662 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
663 Widget_Data *wd = elm_widget_data_get(obj);
664 if (!wd) return EINA_FALSE;
669 elm_clock_interval_set(Evas_Object *obj, double interval)
671 ELM_CHECK_WIDTYPE(obj, widtype);
672 Widget_Data *wd = elm_widget_data_get(obj);
674 wd->first_interval = interval;
678 elm_clock_interval_get(const Evas_Object *obj)
680 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
681 Widget_Data *wd = elm_widget_data_get(obj);
683 return wd->first_interval;