1 #include <Elementary.h>
5 * @defgroup Clock Clock
8 * It's a widget to show clock with animation. The update of time is
9 * shown in an animation like the flip of a sheet.
13 typedef struct _Widget_Data Widget_Data;
18 double interval, first_interval;
19 Eina_Bool seconds : 1;
22 Elm_Clock_Digedit digedit;
23 int hrs, min, sec, timediff;
24 Evas_Object *digit[6];
27 Ecore_Timer *ticker, *spin;
32 Eina_Bool seconds : 1;
35 Elm_Clock_Digedit digedit;
39 static const char *widtype = NULL;
40 static void _del_hook(Evas_Object *obj);
41 static void _theme_hook(Evas_Object *obj);
42 static void _on_focus_hook(void *data, Evas_Object *obj);
43 static Eina_Bool _ticker(void *data);
44 static Eina_Bool _signal_clock_val_up(void *data);
45 static Eina_Bool _signal_clock_val_down(void *data);
46 static void _time_update(Evas_Object *obj);
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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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, "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, "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 * Add a new clock to the parent
520 * @param parent The parent object
522 * This function inserts a clock widget on a given canvas to show a
528 elm_clock_add(Evas_Object *parent)
534 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
536 wd = ELM_NEW(Widget_Data);
537 e = evas_object_evas_get(parent);
539 obj = elm_widget_add(e);
540 ELM_SET_WIDTYPE(widtype, "clock");
541 elm_widget_type_set(obj, "clock");
542 elm_widget_sub_object_add(parent, obj);
543 elm_widget_data_set(obj, wd);
544 elm_widget_del_hook_set(obj, _del_hook);
545 elm_widget_theme_hook_set(obj, _theme_hook);
546 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
547 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
548 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
549 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
550 elm_widget_can_focus_set(obj, EINA_TRUE);
552 wd->clk = edje_object_add(e);
553 elm_widget_resize_object_set(obj, wd->clk);
556 wd->cur.seconds = EINA_TRUE;
557 wd->cur.am_pm = EINA_TRUE;
558 wd->cur.edit = EINA_TRUE;
559 wd->cur.digedit = ELM_CLOCK_NONE;
560 wd->first_interval = 0.85;
572 * @param obj The clock object
573 * @param hrs The hours to set
574 * @param min The minutes to set
575 * @param sec The secondes to set
577 * This function updates the time that is showed by the clock widget
582 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
584 ELM_CHECK_WIDTYPE(obj, widtype);
585 Widget_Data *wd = elm_widget_data_get(obj);
597 * @param obj The clock object
598 * @param hrs Pointer to the variable to get the hour of this clock
600 * @param min Pointer to the variable to get the minute of this clock
602 * @param sec Pointer to the variable to get the second of this clock
605 * This function gets the time set of the clock widget and returns it
606 * on the variables passed as the arguments to function
611 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
613 ELM_CHECK_WIDTYPE(obj, widtype);
614 Widget_Data *wd = elm_widget_data_get(obj);
616 if (hrs) *hrs = wd->hrs;
617 if (min) *min = wd->min;
618 if (sec) *sec = wd->sec;
622 * Set if the clock settings can be edited
624 * @param obj The clock object
625 * @param edit Bool option for edited (1 = yes, 0 = no)
627 * This function sets if the clock settings can be edited or not.
628 * By default or if digit_edit option was previously set to ELM_CLOCK_NONE,
629 * all digits are editable. To choose what digits to make editable
630 * use elm_clock_digit_edit_set().
635 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
637 ELM_CHECK_WIDTYPE(obj, widtype);
638 Widget_Data *wd = elm_widget_data_get(obj);
643 if ((edit) && (wd->digedit == ELM_CLOCK_NONE))
644 elm_clock_digit_edit_set(obj, ELM_CLOCK_ALL);
650 * Get if the clock settings can be edited
652 * @param obj The clock object
653 * @return Bool option for edited (1 = yes, 0 = no)
655 * This function gets if the clock settings can be edited or not.
660 elm_clock_edit_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 * Set what digits of the clock are editable
671 * @param obj The clock object
672 * @param digedit Bit mask indicating the digits to edit
674 * If the digedit param is ELM_CLOCK_NONE, editing will be disabled.
679 elm_clock_digit_edit_set(Evas_Object *obj, Elm_Clock_Digedit digedit)
681 ELM_CHECK_WIDTYPE(obj, widtype);
682 Widget_Data *wd = elm_widget_data_get(obj);
684 wd->digedit = digedit;
685 if (digedit == ELM_CLOCK_NONE)
686 elm_clock_edit_set(obj, EINA_FALSE);
692 * Get what digits of the clock are editable
694 * @param obj The clock object
695 * @return Bit mask indicating the digits.
699 EAPI Elm_Clock_Digedit
700 elm_clock_digit_edit_get(const Evas_Object *obj)
702 ELM_CHECK_WIDTYPE(obj, widtype) 0;
703 Widget_Data *wd = elm_widget_data_get(obj);
709 * Set if the clock shows hours in military or am/pm mode
711 * @param obj The clock object
712 * @param am_pm Bool option for the hours mode
713 * (1 = am/pm, 0 = military)
715 * This function sets the clock to show hours in military or am/pm
716 * mode. Some countries like Brazil the military mode (00-24h-format)
717 * is used in opposition to the USA where the am/pm mode is more
723 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
725 ELM_CHECK_WIDTYPE(obj, widtype);
726 Widget_Data *wd = elm_widget_data_get(obj);
733 * Get if the clock shows hours in military or am/pm mode
735 * @param obj The clock object
736 * @return Bool option for the hours mode
737 * (1 = am/pm, 0 = military)
739 * This function gets if the clock show hours in military or am/pm
740 * mode. Some countries like Brazil the military mode (00-24h-format)
741 * is used in opposition to the USA where the am/pm mode is more
747 elm_clock_show_am_pm_get(const Evas_Object *obj)
749 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
750 Widget_Data *wd = elm_widget_data_get(obj);
751 if (!wd) return EINA_FALSE;
756 * Set if the clock shows hour with the seconds
758 * @param obj The clock object
759 * @param seconds Bool option for the show seconds
760 * (1 = show seconds, 0 = not show seconds)
762 * This function sets the clock to show or not to show the elapsed
768 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
770 ELM_CHECK_WIDTYPE(obj, widtype);
771 Widget_Data *wd = elm_widget_data_get(obj);
773 wd->seconds = seconds;
778 * Get if the clock shows hour with the seconds
780 * @param obj The clock object
781 * @return Bool option for the show seconds
782 * (1 = show seconds, 0 = not show seconds)
784 * This function gets if the clock show or not show the elapsed
790 elm_clock_show_seconds_get(const Evas_Object *obj)
792 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
793 Widget_Data *wd = elm_widget_data_get(obj);
794 if (!wd) return EINA_FALSE;
799 * Set the interval for the clock
801 * @param obj The clock object
802 * @param interval The interval value in seconds
804 * The interval value is decreased while the user increments or decrements
805 * the clock value. The next interval value is the previous interval / 1.05,
806 * so it speed up a bit. Default value is 0.85 seconds.
811 elm_clock_interval_set(Evas_Object *obj, double interval)
813 ELM_CHECK_WIDTYPE(obj, widtype);
814 Widget_Data *wd = elm_widget_data_get(obj);
816 wd->first_interval = interval;
820 * Get the interval of the clock
822 * @param obj The clock object
823 * @return The value of the first interval in seconds
825 * The interval value is decreased while the user increments or decrements
826 * the clock value. The next interval value is the previous interval / 1.05,
827 * so it speed up a bit. Default value is 0.85 seconds.
832 elm_clock_interval_get(const Evas_Object *obj)
834 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
835 Widget_Data *wd = elm_widget_data_get(obj);
837 return wd->first_interval;