1 #include <Elementary.h>
5 * @defgroup Clock Clock
7 * It's a widget to show clock with animation. The update of time is
8 * shown in an animation like the flip of a sheet.
12 typedef struct _Widget_Data Widget_Data;
17 double interval, first_interval;
18 Eina_Bool seconds : 1;
21 Elm_Clock_Digedit digedit;
22 int hrs, min, sec, timediff;
23 Evas_Object *digit[6];
26 Ecore_Timer *ticker, *spin;
31 Eina_Bool seconds : 1;
34 Elm_Clock_Digedit digedit;
38 static const char *widtype = NULL;
39 static void _del_hook(Evas_Object *obj);
40 static void _theme_hook(Evas_Object *obj);
41 static void _on_focus_hook(void *data, Evas_Object *obj);
42 static Eina_Bool _ticker(void *data);
43 static Eina_Bool _signal_clock_val_up(void *data);
44 static Eina_Bool _signal_clock_val_down(void *data);
45 static void _time_update(Evas_Object *obj);
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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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);
146 wd->timediff = (((wd->hrs - tm->tm_hour) * 60 +
147 wd->min - tm->tm_min) * 60) + wd->sec - tm->tm_sec;
153 Widget_Data *wd = elm_widget_data_get(data);
155 struct timeval timev;
158 if (!wd) return ECORE_CALLBACK_CANCEL;
159 gettimeofday(&timev, NULL);
160 t = ((double)(1000000 - timev.tv_usec)) / 1000000.0;
161 wd->ticker = ecore_timer_add(t, _ticker, data);
164 tt = (time_t)(timev.tv_sec) + wd->timediff;
169 wd->hrs = tm->tm_hour;
170 wd->min = tm->tm_min;
171 wd->sec = tm->tm_sec;
175 return ECORE_CALLBACK_CANCEL;
179 _signal_clock_val_up(void *data)
181 Widget_Data *wd = elm_widget_data_get(data);
182 if (!wd) goto clock_val_up_exit_on_error;
183 if (!wd->edit) goto clock_val_up_cancel;
184 if (!wd->sel_obj) goto clock_val_up_cancel;
185 if (wd->sel_obj == wd->digit[0])
187 wd->hrs = wd->hrs + 10;
188 if (wd->hrs >= 24) wd->hrs -= 24;
190 if (wd->sel_obj == wd->digit[1])
192 wd->hrs = wd->hrs + 1;
193 if (wd->hrs >= 24) wd->hrs -= 24;
195 if (wd->sel_obj == wd->digit[2])
197 wd->min = wd->min + 10;
198 if (wd->min >= 60) wd->min -= 60;
200 if (wd->sel_obj == wd->digit[3])
202 wd->min = wd->min + 1;
203 if (wd->min >= 60) wd->min -= 60;
205 if (wd->sel_obj == wd->digit[4])
207 wd->sec = wd->sec + 10;
208 if (wd->sec >= 60) wd->sec -= 60;
210 if (wd->sel_obj == wd->digit[5])
212 wd->sec = wd->sec + 1;
213 if (wd->sec >= 60) wd->sec -= 60;
215 if (wd->sel_obj == wd->ampm)
217 wd->hrs = wd->hrs + 12;
218 if (wd->hrs > 23) wd->hrs -= 24;
220 wd->interval = wd->interval / 1.05;
221 ecore_timer_interval_set(wd->spin, wd->interval);
223 evas_object_smart_callback_call(data, "changed", NULL);
224 return ECORE_CALLBACK_RENEW;
227 clock_val_up_exit_on_error:
228 return ECORE_CALLBACK_CANCEL;
232 _signal_clock_val_down(void *data)
234 Widget_Data *wd = elm_widget_data_get(data);
235 if (!wd) goto clock_val_down_exit_on_error;
236 if (!wd->edit) goto clock_val_down_cancel;
237 if (!wd->sel_obj) goto clock_val_down_cancel;
238 if (wd->sel_obj == wd->digit[0])
240 wd->hrs = wd->hrs - 10;
241 if (wd->hrs < 0) wd->hrs += 24;
243 if (wd->sel_obj == wd->digit[1])
245 wd->hrs = wd->hrs - 1;
246 if (wd->hrs < 0) wd->hrs += 24;
248 if (wd->sel_obj == wd->digit[2])
250 wd->min = wd->min - 10;
251 if (wd->min < 0) wd->min += 60;
253 if (wd->sel_obj == wd->digit[3])
255 wd->min = wd->min - 1;
256 if (wd->min < 0) wd->min += 60;
258 if (wd->sel_obj == wd->digit[4])
260 wd->sec = wd->sec - 10;
261 if (wd->sec < 0) wd->sec += 60;
263 if (wd->sel_obj == wd->digit[5])
265 wd->sec = wd->sec - 1;
266 if (wd->sec < 0) wd->sec += 60;
268 if (wd->sel_obj == wd->ampm)
270 wd->hrs = wd->hrs - 12;
271 if (wd->hrs < 0) wd->hrs += 24;
273 wd->interval = wd->interval / 1.05;
274 ecore_timer_interval_set(wd->spin, wd->interval);
276 evas_object_smart_callback_call(data, "changed", NULL);
277 return ECORE_CALLBACK_RENEW;
278 clock_val_down_cancel:
280 clock_val_down_exit_on_error:
281 return ECORE_CALLBACK_CANCEL;
285 _signal_clock_val_up_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
287 Widget_Data *wd = elm_widget_data_get(data);
289 wd->interval = wd->first_interval;
291 if (wd->spin) ecore_timer_del(wd->spin);
292 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_up, data);
293 _signal_clock_val_up(data);
297 _signal_clock_val_down_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
299 Widget_Data *wd = elm_widget_data_get(data);
301 wd->interval = wd->first_interval;
303 if (wd->spin) ecore_timer_del(wd->spin);
304 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_down, data);
305 _signal_clock_val_down(data);
309 _signal_clock_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
311 Widget_Data *wd = elm_widget_data_get(data);
313 if (wd->spin) ecore_timer_del(wd->spin);
319 _time_update(Evas_Object *obj)
321 Widget_Data *wd = elm_widget_data_get(obj);
322 Edje_Message_Int msg;
324 const char *style = elm_widget_style_get(obj);
326 if ((wd->cur.seconds != wd->seconds) || (wd->cur.am_pm != wd->am_pm) ||
327 (wd->cur.edit != wd->edit) || (wd->cur.digedit != wd->digedit))
332 for (i = 0; i < 6; i++)
336 evas_object_del(wd->digit[i]);
342 evas_object_del(wd->ampm);
346 if ((wd->seconds) && (wd->am_pm))
347 _elm_theme_object_set(obj, wd->clk, "clock", "base-all", style);
348 else if (wd->seconds)
349 _elm_theme_object_set(obj, wd->clk, "clock", "base-seconds", style);
351 _elm_theme_object_set(obj, wd->clk, "clock", "base-am_pm", style);
353 _elm_theme_object_set(obj, wd->clk, "clock", "base", style);
354 edje_object_scale_set(wd->clk, elm_widget_scale_get(obj) *
357 for (i = 0; i < 6; i++)
361 if ((!wd->seconds) && (i >= 4)) break;
362 wd->digit[i] = edje_object_add(evas_object_evas_get(wd->clk));
363 _elm_theme_object_set(obj, wd->digit[i], "clock", "flipdigit", style);
364 edje_object_scale_set(wd->digit[i], elm_widget_scale_get(obj) *
366 if ((wd->edit) && (wd->digedit & (1 << i)))
367 edje_object_signal_emit(wd->digit[i], "elm,state,edit,on", "elm");
368 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,start",
369 "", _signal_clock_val_up_start, obj);
370 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,stop",
371 "", _signal_clock_val_change_stop, obj);
372 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,start",
373 "", _signal_clock_val_down_start, obj);
374 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,stop",
375 "", _signal_clock_val_change_stop, obj);
377 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
378 edje_object_size_min_restricted_calc(wd->digit[i], &mw, &mh, mw, mh);
379 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
380 edje_extern_object_min_size_set(wd->digit[i], mw, mh);
381 snprintf(buf, sizeof(buf), "d%i", i);
382 edje_object_part_swallow(wd->clk , buf, wd->digit[i]);
383 evas_object_show(wd->digit[i]);
387 wd->ampm = edje_object_add(evas_object_evas_get(wd->clk));
388 _elm_theme_object_set(obj, wd->ampm, "clock", "flipampm", style);
389 edje_object_scale_set(wd->ampm, elm_widget_scale_get(obj) *
392 edje_object_signal_emit(wd->ampm, "elm,state,edit,on", "elm");
393 edje_object_signal_callback_add(wd->ampm, "elm,action,up,start",
394 "", _signal_clock_val_up_start, obj);
395 edje_object_signal_callback_add(wd->ampm, "elm,action,up,stop",
396 "", _signal_clock_val_change_stop, obj);
397 edje_object_signal_callback_add(wd->ampm, "elm,action,down,start",
398 "", _signal_clock_val_down_start, obj);
399 edje_object_signal_callback_add(wd->ampm, "elm,action,down,stop",
400 "", _signal_clock_val_change_stop, obj);
402 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
403 edje_object_size_min_restricted_calc(wd->ampm, &mw, &mh, mw, mh);
404 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
405 edje_extern_object_min_size_set(wd->ampm, mw, mh);
406 edje_object_part_swallow(wd->clk , "ampm", wd->ampm);
407 evas_object_show(wd->ampm);
410 edje_object_size_min_calc(wd->clk, &mw, &mh);
411 evas_object_size_hint_min_set(obj, mw, mh);
417 wd->cur.seconds = wd->seconds;
418 wd->cur.am_pm = wd->am_pm;
419 wd->cur.edit = wd->edit;
420 wd->cur.digedit = wd->digedit;
422 if (wd->hrs != wd->cur.hrs)
425 int d1, d2, dc1, dc2;
432 if (hrs > 12) hrs -= 12;
435 else if (!hrs) hrs = 12;
439 dc1 = wd->cur.hrs / 10;
440 dc2 = wd->cur.hrs % 10;
444 edje_object_message_send(wd->digit[0], EDJE_MESSAGE_INT, 1, &msg);
449 edje_object_message_send(wd->digit[1], EDJE_MESSAGE_INT, 1, &msg);
453 if (wd->min != wd->cur.min)
455 int d1, d2, dc1, dc2;
459 dc1 = wd->cur.min / 10;
460 dc2 = wd->cur.min % 10;
464 edje_object_message_send(wd->digit[2], EDJE_MESSAGE_INT, 1, &msg);
469 edje_object_message_send(wd->digit[3], EDJE_MESSAGE_INT, 1, &msg);
471 wd->cur.min = wd->min;
475 if (wd->sec != wd->cur.sec)
477 int d1, d2, dc1, dc2;
481 dc1 = wd->cur.sec / 10;
482 dc2 = wd->cur.sec % 10;
486 edje_object_message_send(wd->digit[4], EDJE_MESSAGE_INT, 1, &msg);
491 edje_object_message_send(wd->digit[5], EDJE_MESSAGE_INT, 1, &msg);
493 wd->cur.sec = wd->sec;
501 if (wd->hrs >= 12) ampm = 1;
502 if (ampm != wd->cur.ampm)
504 if (wd->cur.ampm != ampm)
507 edje_object_message_send(wd->ampm, EDJE_MESSAGE_INT, 1, &msg);
517 * Add a new clock to the parent
519 * @param parent The parent object
521 * This function inserts a clock widget on a given canvas to show a
527 elm_clock_add(Evas_Object *parent)
533 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
535 wd = ELM_NEW(Widget_Data);
536 e = evas_object_evas_get(parent);
538 obj = elm_widget_add(e);
539 ELM_SET_WIDTYPE(widtype, "clock");
540 elm_widget_type_set(obj, "clock");
541 elm_widget_sub_object_add(parent, obj);
542 elm_widget_data_set(obj, wd);
543 elm_widget_del_hook_set(obj, _del_hook);
544 elm_widget_theme_hook_set(obj, _theme_hook);
545 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
546 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
547 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
548 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
549 elm_widget_can_focus_set(obj, EINA_TRUE);
551 wd->clk = edje_object_add(e);
552 elm_widget_resize_object_set(obj, wd->clk);
555 wd->cur.seconds = EINA_TRUE;
556 wd->cur.am_pm = EINA_TRUE;
557 wd->cur.edit = EINA_TRUE;
558 wd->cur.digedit = ELM_CLOCK_NONE;
559 wd->first_interval = 0.85;
571 * @param obj The clock object
572 * @param hrs The hours to set
573 * @param min The minutes to set
574 * @param sec The secondes to set
576 * This function updates the time that is showed by the clock widget
581 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
583 ELM_CHECK_WIDTYPE(obj, widtype);
584 Widget_Data *wd = elm_widget_data_get(obj);
596 * @param obj The clock object
597 * @param hrs Pointer to the variable to get the hour of this clock
599 * @param min Pointer to the variable to get the minute of this clock
601 * @param sec Pointer to the variable to get the second of this clock
604 * This function gets the time set of the clock widget and returns it
605 * on the variables passed as the arguments to function
610 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
612 ELM_CHECK_WIDTYPE(obj, widtype);
613 Widget_Data *wd = elm_widget_data_get(obj);
615 if (hrs) *hrs = wd->hrs;
616 if (min) *min = wd->min;
617 if (sec) *sec = wd->sec;
621 * Set if the clock settings can be edited
623 * @param obj The clock object
624 * @param edit Bool option for edited (1 = yes, 0 = no)
626 * This function sets if the clock settings can be edited or not.
627 * By default or if digit_edit option was previously set to ELM_CLOCK_NONE,
628 * all digits are editable. To choose what digits to make editable
629 * use elm_clock_digit_edit_set().
634 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
636 ELM_CHECK_WIDTYPE(obj, widtype);
637 Widget_Data *wd = elm_widget_data_get(obj);
642 if ((edit) && (wd->digedit == ELM_CLOCK_NONE))
643 elm_clock_digit_edit_set(obj, ELM_CLOCK_ALL);
649 * Get if the clock settings can be edited
651 * @param obj The clock object
652 * @return Bool option for edited (1 = yes, 0 = no)
654 * This function gets if the clock settings can be edited or not.
659 elm_clock_edit_get(const Evas_Object *obj)
661 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
662 Widget_Data *wd = elm_widget_data_get(obj);
663 if (!wd) return EINA_FALSE;
668 * Set what digits of the clock are editable
670 * @param obj The clock object
671 * @param digedit Bit mask indicating the digits to edit
673 * If the digedit param is ELM_CLOCK_NONE, editing will be disabled.
678 elm_clock_digit_edit_set(Evas_Object *obj, Elm_Clock_Digedit digedit)
680 ELM_CHECK_WIDTYPE(obj, widtype);
681 Widget_Data *wd = elm_widget_data_get(obj);
683 wd->digedit = digedit;
684 if (digedit == ELM_CLOCK_NONE)
685 elm_clock_edit_set(obj, EINA_FALSE);
691 * Get what digits of the clock are editable
693 * @param obj The clock object
694 * @return Bit mask indicating the digits.
698 EAPI Elm_Clock_Digedit
699 elm_clock_digit_edit_get(const Evas_Object *obj)
701 ELM_CHECK_WIDTYPE(obj, widtype) 0;
702 Widget_Data *wd = elm_widget_data_get(obj);
708 * Set if the clock shows hours in military or am/pm mode
710 * @param obj The clock object
711 * @param am_pm Bool option for the hours mode
712 * (1 = am/pm, 0 = military)
714 * This function sets the clock to show hours in military or am/pm
715 * mode. Some countries like Brazil the military mode (00-24h-format)
716 * is used in opposition to the USA where the am/pm mode is more
722 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
724 ELM_CHECK_WIDTYPE(obj, widtype);
725 Widget_Data *wd = elm_widget_data_get(obj);
732 * Get if the clock shows hours in military or am/pm mode
734 * @param obj The clock object
735 * @return Bool option for the hours mode
736 * (1 = am/pm, 0 = military)
738 * This function gets if the clock show hours in military or am/pm
739 * mode. Some countries like Brazil the military mode (00-24h-format)
740 * is used in opposition to the USA where the am/pm mode is more
746 elm_clock_show_am_pm_get(const Evas_Object *obj)
748 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
749 Widget_Data *wd = elm_widget_data_get(obj);
750 if (!wd) return EINA_FALSE;
755 * Set if the clock shows hour with the seconds
757 * @param obj The clock object
758 * @param seconds Bool option for the show seconds
759 * (1 = show seconds, 0 = not show seconds)
761 * This function sets the clock to show or not to show the elapsed
767 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
769 ELM_CHECK_WIDTYPE(obj, widtype);
770 Widget_Data *wd = elm_widget_data_get(obj);
772 wd->seconds = seconds;
777 * Get if the clock shows hour with the seconds
779 * @param obj The clock object
780 * @return Bool option for the show seconds
781 * (1 = show seconds, 0 = not show seconds)
783 * This function gets if the clock show or not show the elapsed
789 elm_clock_show_seconds_get(const Evas_Object *obj)
791 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
792 Widget_Data *wd = elm_widget_data_get(obj);
793 if (!wd) return EINA_FALSE;
798 * Set the interval for the clock
800 * @param obj The clock object
801 * @param interval The interval value in seconds
803 * The interval value is decreased while the user increments or decrements
804 * the clock value. The next interval value is the previous interval / 1.05,
805 * so it speed up a bit. Default value is 0.85 seconds.
810 elm_clock_interval_set(Evas_Object *obj, double interval)
812 ELM_CHECK_WIDTYPE(obj, widtype);
813 Widget_Data *wd = elm_widget_data_get(obj);
815 wd->first_interval = interval;
819 * Get the interval of the clock
821 * @param obj The clock object
822 * @return The value of the first interval in seconds
824 * The interval value is decreased while the user increments or decrements
825 * the clock value. The next interval value is the previous interval / 1.05,
826 * so it speed up a bit. Default value is 0.85 seconds.
831 elm_clock_interval_get(const Evas_Object *obj)
833 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
834 Widget_Data *wd = elm_widget_data_get(obj);
836 return wd->first_interval;