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.
10 * Signals that you can add callbacks for are:
12 * "changed" - the user changed the time
15 typedef struct _Widget_Data Widget_Data;
20 double interval, first_interval;
21 Eina_Bool seconds : 1;
24 Elm_Clock_Digedit digedit;
25 int hrs, min, sec, timediff;
26 Evas_Object *digit[6];
29 Ecore_Timer *ticker, *spin;
34 Eina_Bool seconds : 1;
37 Elm_Clock_Digedit digedit;
41 static const char *widtype = NULL;
42 static void _del_hook(Evas_Object *obj);
43 static void _theme_hook(Evas_Object *obj);
44 static void _on_focus_hook(void *data, Evas_Object *obj);
45 static Eina_Bool _ticker(void *data);
46 static Eina_Bool _signal_clock_val_up(void *data);
47 static Eina_Bool _signal_clock_val_down(void *data);
48 static void _time_update(Evas_Object *obj);
50 static const char SIG_CHANGED[] = "changed";
52 static const Evas_Smart_Cb_Description _signals[] = {
59 _del_hook(Evas_Object *obj)
61 Widget_Data *wd = elm_widget_data_get(obj);
64 for (i = 0; i < 6; i++)
66 if (wd->digit[i]) evas_object_del(wd->digit[i]);
68 if (wd->ampm) evas_object_del(wd->ampm);
69 if (wd->ticker) ecore_timer_del(wd->ticker);
70 if (wd->spin) ecore_timer_del(wd->spin);
75 _theme_hook(Evas_Object *obj)
77 Widget_Data *wd = elm_widget_data_get(obj);
79 if (elm_widget_focus_get(obj))
80 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
82 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
83 wd->cur.am_pm = !wd->cur.am_pm; /* hack - force update */
88 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
90 Widget_Data *wd = elm_widget_data_get(obj);
92 if (elm_widget_focus_get(obj))
94 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
95 evas_object_focus_set(wd->clk, EINA_TRUE);
99 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
100 evas_object_focus_set(wd->clk, EINA_FALSE);
105 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
107 Widget_Data *wd = elm_widget_data_get(obj);
110 edje_object_signal_emit(wd->clk, emission, source);
111 for (i = 0; i < 6; i++)
114 edje_object_signal_emit(wd->digit[i], emission, source);
119 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
121 Widget_Data *wd = elm_widget_data_get(obj);
124 edje_object_signal_callback_add(wd->clk, emission, source, func_cb, data);
125 for (i = 0; i < 6; i++)
128 edje_object_signal_callback_add(wd->digit[i], emission, source,
134 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
136 Widget_Data *wd = elm_widget_data_get(obj);
138 for (i = 0; i < 6; i++)
140 edje_object_signal_callback_del_full(wd->digit[i], emission, source,
143 edje_object_signal_callback_del_full(wd->clk, emission, source, func_cb,
148 _timediff_set(Widget_Data *wd)
150 struct timeval timev;
153 gettimeofday(&timev, NULL);
154 tt = (time_t)(timev.tv_sec);
157 wd->timediff = (((wd->hrs - tm->tm_hour) * 60 +
158 wd->min - tm->tm_min) * 60) + wd->sec - tm->tm_sec;
164 Widget_Data *wd = elm_widget_data_get(data);
166 struct timeval timev;
169 if (!wd) return ECORE_CALLBACK_CANCEL;
170 gettimeofday(&timev, NULL);
171 t = ((double)(1000000 - timev.tv_usec)) / 1000000.0;
172 wd->ticker = ecore_timer_add(t, _ticker, data);
175 tt = (time_t)(timev.tv_sec) + wd->timediff;
180 wd->hrs = tm->tm_hour;
181 wd->min = tm->tm_min;
182 wd->sec = tm->tm_sec;
186 return ECORE_CALLBACK_CANCEL;
190 _signal_clock_val_up(void *data)
192 Widget_Data *wd = elm_widget_data_get(data);
193 if (!wd) goto clock_val_up_exit_on_error;
194 if (!wd->edit) goto clock_val_up_cancel;
195 if (!wd->sel_obj) goto clock_val_up_cancel;
196 if (wd->sel_obj == wd->digit[0])
198 wd->hrs = wd->hrs + 10;
199 if (wd->hrs >= 24) wd->hrs -= 24;
201 if (wd->sel_obj == wd->digit[1])
203 wd->hrs = wd->hrs + 1;
204 if (wd->hrs >= 24) wd->hrs -= 24;
206 if (wd->sel_obj == wd->digit[2])
208 wd->min = wd->min + 10;
209 if (wd->min >= 60) wd->min -= 60;
211 if (wd->sel_obj == wd->digit[3])
213 wd->min = wd->min + 1;
214 if (wd->min >= 60) wd->min -= 60;
216 if (wd->sel_obj == wd->digit[4])
218 wd->sec = wd->sec + 10;
219 if (wd->sec >= 60) wd->sec -= 60;
221 if (wd->sel_obj == wd->digit[5])
223 wd->sec = wd->sec + 1;
224 if (wd->sec >= 60) wd->sec -= 60;
226 if (wd->sel_obj == wd->ampm)
228 wd->hrs = wd->hrs + 12;
229 if (wd->hrs > 23) wd->hrs -= 24;
231 wd->interval = wd->interval / 1.05;
232 ecore_timer_interval_set(wd->spin, wd->interval);
234 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
235 return ECORE_CALLBACK_RENEW;
238 clock_val_up_exit_on_error:
239 return ECORE_CALLBACK_CANCEL;
243 _signal_clock_val_down(void *data)
245 Widget_Data *wd = elm_widget_data_get(data);
246 if (!wd) goto clock_val_down_exit_on_error;
247 if (!wd->edit) goto clock_val_down_cancel;
248 if (!wd->sel_obj) goto clock_val_down_cancel;
249 if (wd->sel_obj == wd->digit[0])
251 wd->hrs = wd->hrs - 10;
252 if (wd->hrs < 0) wd->hrs += 24;
254 if (wd->sel_obj == wd->digit[1])
256 wd->hrs = wd->hrs - 1;
257 if (wd->hrs < 0) wd->hrs += 24;
259 if (wd->sel_obj == wd->digit[2])
261 wd->min = wd->min - 10;
262 if (wd->min < 0) wd->min += 60;
264 if (wd->sel_obj == wd->digit[3])
266 wd->min = wd->min - 1;
267 if (wd->min < 0) wd->min += 60;
269 if (wd->sel_obj == wd->digit[4])
271 wd->sec = wd->sec - 10;
272 if (wd->sec < 0) wd->sec += 60;
274 if (wd->sel_obj == wd->digit[5])
276 wd->sec = wd->sec - 1;
277 if (wd->sec < 0) wd->sec += 60;
279 if (wd->sel_obj == wd->ampm)
281 wd->hrs = wd->hrs - 12;
282 if (wd->hrs < 0) wd->hrs += 24;
284 wd->interval = wd->interval / 1.05;
285 ecore_timer_interval_set(wd->spin, wd->interval);
287 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
288 return ECORE_CALLBACK_RENEW;
289 clock_val_down_cancel:
291 clock_val_down_exit_on_error:
292 return ECORE_CALLBACK_CANCEL;
296 _signal_clock_val_up_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
298 Widget_Data *wd = elm_widget_data_get(data);
300 wd->interval = wd->first_interval;
302 if (wd->spin) ecore_timer_del(wd->spin);
303 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_up, data);
304 _signal_clock_val_up(data);
308 _signal_clock_val_down_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
310 Widget_Data *wd = elm_widget_data_get(data);
312 wd->interval = wd->first_interval;
314 if (wd->spin) ecore_timer_del(wd->spin);
315 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_down, data);
316 _signal_clock_val_down(data);
320 _signal_clock_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
322 Widget_Data *wd = elm_widget_data_get(data);
324 if (wd->spin) ecore_timer_del(wd->spin);
330 _time_update(Evas_Object *obj)
332 Widget_Data *wd = elm_widget_data_get(obj);
333 Edje_Message_Int msg;
335 const char *style = elm_widget_style_get(obj);
337 if ((wd->cur.seconds != wd->seconds) || (wd->cur.am_pm != wd->am_pm) ||
338 (wd->cur.edit != wd->edit) || (wd->cur.digedit != wd->digedit))
343 for (i = 0; i < 6; i++)
347 evas_object_del(wd->digit[i]);
353 evas_object_del(wd->ampm);
357 if ((wd->seconds) && (wd->am_pm))
358 _elm_theme_object_set(obj, wd->clk, "clock", "base-all", style);
359 else if (wd->seconds)
360 _elm_theme_object_set(obj, wd->clk, "clock", "base-seconds", style);
362 _elm_theme_object_set(obj, wd->clk, "clock", "base-am_pm", style);
364 _elm_theme_object_set(obj, wd->clk, "clock", "base", style);
365 edje_object_scale_set(wd->clk, elm_widget_scale_get(obj) *
368 for (i = 0; i < 6; i++)
372 if ((!wd->seconds) && (i >= 4)) break;
373 wd->digit[i] = edje_object_add(evas_object_evas_get(wd->clk));
374 _elm_theme_object_set(obj, wd->digit[i], "clock", "flipdigit", style);
375 edje_object_scale_set(wd->digit[i], elm_widget_scale_get(obj) *
377 if ((wd->edit) && (wd->digedit & (1 << i)))
378 edje_object_signal_emit(wd->digit[i], "elm,state,edit,on", "elm");
379 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,start",
380 "", _signal_clock_val_up_start, obj);
381 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,stop",
382 "", _signal_clock_val_change_stop, obj);
383 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,start",
384 "", _signal_clock_val_down_start, obj);
385 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,stop",
386 "", _signal_clock_val_change_stop, obj);
388 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
389 edje_object_size_min_restricted_calc(wd->digit[i], &mw, &mh, mw, mh);
390 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
391 edje_extern_object_min_size_set(wd->digit[i], mw, mh);
392 snprintf(buf, sizeof(buf), "d%i", i);
393 edje_object_part_swallow(wd->clk , buf, wd->digit[i]);
394 evas_object_show(wd->digit[i]);
398 wd->ampm = edje_object_add(evas_object_evas_get(wd->clk));
399 _elm_theme_object_set(obj, wd->ampm, "clock", "flipampm", style);
400 edje_object_scale_set(wd->ampm, elm_widget_scale_get(obj) *
403 edje_object_signal_emit(wd->ampm, "elm,state,edit,on", "elm");
404 edje_object_signal_callback_add(wd->ampm, "elm,action,up,start",
405 "", _signal_clock_val_up_start, obj);
406 edje_object_signal_callback_add(wd->ampm, "elm,action,up,stop",
407 "", _signal_clock_val_change_stop, obj);
408 edje_object_signal_callback_add(wd->ampm, "elm,action,down,start",
409 "", _signal_clock_val_down_start, obj);
410 edje_object_signal_callback_add(wd->ampm, "elm,action,down,stop",
411 "", _signal_clock_val_change_stop, obj);
413 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
414 edje_object_size_min_restricted_calc(wd->ampm, &mw, &mh, mw, mh);
415 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
416 edje_extern_object_min_size_set(wd->ampm, mw, mh);
417 edje_object_part_swallow(wd->clk , "ampm", wd->ampm);
418 evas_object_show(wd->ampm);
421 edje_object_size_min_calc(wd->clk, &mw, &mh);
422 evas_object_size_hint_min_set(obj, mw, mh);
428 wd->cur.seconds = wd->seconds;
429 wd->cur.am_pm = wd->am_pm;
430 wd->cur.edit = wd->edit;
431 wd->cur.digedit = wd->digedit;
433 if (wd->hrs != wd->cur.hrs)
436 int d1, d2, dc1, dc2;
443 if (hrs > 12) hrs -= 12;
446 else if (!hrs) hrs = 12;
450 dc1 = wd->cur.hrs / 10;
451 dc2 = wd->cur.hrs % 10;
455 edje_object_message_send(wd->digit[0], EDJE_MESSAGE_INT, 1, &msg);
460 edje_object_message_send(wd->digit[1], EDJE_MESSAGE_INT, 1, &msg);
464 if (wd->min != wd->cur.min)
466 int d1, d2, dc1, dc2;
470 dc1 = wd->cur.min / 10;
471 dc2 = wd->cur.min % 10;
475 edje_object_message_send(wd->digit[2], EDJE_MESSAGE_INT, 1, &msg);
480 edje_object_message_send(wd->digit[3], EDJE_MESSAGE_INT, 1, &msg);
482 wd->cur.min = wd->min;
486 if (wd->sec != wd->cur.sec)
488 int d1, d2, dc1, dc2;
492 dc1 = wd->cur.sec / 10;
493 dc2 = wd->cur.sec % 10;
497 edje_object_message_send(wd->digit[4], EDJE_MESSAGE_INT, 1, &msg);
502 edje_object_message_send(wd->digit[5], EDJE_MESSAGE_INT, 1, &msg);
504 wd->cur.sec = wd->sec;
512 if (wd->hrs >= 12) ampm = 1;
513 if (ampm != wd->cur.ampm)
515 if (wd->cur.ampm != ampm)
518 edje_object_message_send(wd->ampm, EDJE_MESSAGE_INT, 1, &msg);
528 * Add a new clock to the parent
530 * @param parent The parent object
532 * This function inserts a clock widget on a given canvas to show a
538 elm_clock_add(Evas_Object *parent)
544 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
546 ELM_SET_WIDTYPE(widtype, "clock");
547 elm_widget_type_set(obj, "clock");
548 elm_widget_sub_object_add(parent, obj);
549 elm_widget_data_set(obj, wd);
550 elm_widget_del_hook_set(obj, _del_hook);
551 elm_widget_theme_hook_set(obj, _theme_hook);
552 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
553 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
554 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
555 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
556 elm_widget_can_focus_set(obj, EINA_TRUE);
558 wd->clk = edje_object_add(e);
559 elm_widget_resize_object_set(obj, wd->clk);
562 wd->cur.seconds = EINA_TRUE;
563 wd->cur.am_pm = EINA_TRUE;
564 wd->cur.edit = EINA_TRUE;
565 wd->cur.digedit = ELM_CLOCK_NONE;
566 wd->first_interval = 0.85;
572 evas_object_smart_callbacks_descriptions_set(obj, _signals);
580 * @param obj The clock object
581 * @param hrs The hours to set
582 * @param min The minutes to set
583 * @param sec The secondes to set
585 * This function updates the time that is showed by the clock widget
590 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
592 ELM_CHECK_WIDTYPE(obj, widtype);
593 Widget_Data *wd = elm_widget_data_get(obj);
605 * @param obj The clock object
606 * @param hrs Pointer to the variable to get the hour of this clock
608 * @param min Pointer to the variable to get the minute of this clock
610 * @param sec Pointer to the variable to get the second of this clock
613 * This function gets the time set of the clock widget and returns it
614 * on the variables passed as the arguments to function
619 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
621 ELM_CHECK_WIDTYPE(obj, widtype);
622 Widget_Data *wd = elm_widget_data_get(obj);
624 if (hrs) *hrs = wd->hrs;
625 if (min) *min = wd->min;
626 if (sec) *sec = wd->sec;
630 * Set if the clock settings can be edited
632 * @param obj The clock object
633 * @param edit Bool option for edited (1 = yes, 0 = no)
635 * This function sets if the clock settings can be edited or not.
636 * By default or if digit_edit option was previously set to ELM_CLOCK_NONE,
637 * all digits are editable. To choose what digits to make editable
638 * use elm_clock_digit_edit_set().
643 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
645 ELM_CHECK_WIDTYPE(obj, widtype);
646 Widget_Data *wd = elm_widget_data_get(obj);
651 if ((edit) && (wd->digedit == ELM_CLOCK_NONE))
652 elm_clock_digit_edit_set(obj, ELM_CLOCK_ALL);
658 * Get if the clock settings can be edited
660 * @param obj The clock object
661 * @return Bool option for edited (1 = yes, 0 = no)
663 * This function gets if the clock settings can be edited or not.
668 elm_clock_edit_get(const Evas_Object *obj)
670 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
671 Widget_Data *wd = elm_widget_data_get(obj);
672 if (!wd) return EINA_FALSE;
677 * Set what digits of the clock are editable
679 * @param obj The clock object
680 * @param digedit Bit mask indicating the digits to edit
682 * If the digedit param is ELM_CLOCK_NONE, editing will be disabled.
687 elm_clock_digit_edit_set(Evas_Object *obj, Elm_Clock_Digedit digedit)
689 ELM_CHECK_WIDTYPE(obj, widtype);
690 Widget_Data *wd = elm_widget_data_get(obj);
692 wd->digedit = digedit;
693 if (digedit == ELM_CLOCK_NONE)
694 elm_clock_edit_set(obj, EINA_FALSE);
700 * Get what digits of the clock are editable
702 * @param obj The clock object
703 * @return Bit mask indicating the digits.
707 EAPI Elm_Clock_Digedit
708 elm_clock_digit_edit_get(const Evas_Object *obj)
710 ELM_CHECK_WIDTYPE(obj, widtype) 0;
711 Widget_Data *wd = elm_widget_data_get(obj);
717 * Set if the clock shows hours in military or am/pm mode
719 * @param obj The clock object
720 * @param am_pm Bool option for the hours mode
721 * (1 = am/pm, 0 = military)
723 * This function sets the clock to show hours in military or am/pm
724 * mode. Some countries like Brazil the military mode (00-24h-format)
725 * is used in opposition to the USA where the am/pm mode is more
731 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
733 ELM_CHECK_WIDTYPE(obj, widtype);
734 Widget_Data *wd = elm_widget_data_get(obj);
741 * Get if the clock shows hours in military or am/pm mode
743 * @param obj The clock object
744 * @return Bool option for the hours mode
745 * (1 = am/pm, 0 = military)
747 * This function gets if the clock show hours in military or am/pm
748 * mode. Some countries like Brazil the military mode (00-24h-format)
749 * is used in opposition to the USA where the am/pm mode is more
755 elm_clock_show_am_pm_get(const Evas_Object *obj)
757 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
758 Widget_Data *wd = elm_widget_data_get(obj);
759 if (!wd) return EINA_FALSE;
764 * Set if the clock shows hour with the seconds
766 * @param obj The clock object
767 * @param seconds Bool option for the show seconds
768 * (1 = show seconds, 0 = not show seconds)
770 * This function sets the clock to show or not to show the elapsed
776 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
778 ELM_CHECK_WIDTYPE(obj, widtype);
779 Widget_Data *wd = elm_widget_data_get(obj);
781 wd->seconds = seconds;
786 * Get if the clock shows hour with the seconds
788 * @param obj The clock object
789 * @return Bool option for the show seconds
790 * (1 = show seconds, 0 = not show seconds)
792 * This function gets if the clock show or not show the elapsed
798 elm_clock_show_seconds_get(const Evas_Object *obj)
800 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
801 Widget_Data *wd = elm_widget_data_get(obj);
802 if (!wd) return EINA_FALSE;
807 * Set the interval for the clock
809 * @param obj The clock object
810 * @param interval The interval value in seconds
812 * The interval value is decreased while the user increments or decrements
813 * the clock value. The next interval value is the previous interval / 1.05,
814 * so it speed up a bit. Default value is 0.85 seconds.
819 elm_clock_interval_set(Evas_Object *obj, double interval)
821 ELM_CHECK_WIDTYPE(obj, widtype);
822 Widget_Data *wd = elm_widget_data_get(obj);
824 wd->first_interval = interval;
828 * Get the interval of the clock
830 * @param obj The clock object
831 * @return The value of the first interval in seconds
833 * The interval value is decreased while the user increments or decrements
834 * the clock value. The next interval value is the previous interval / 1.05,
835 * so it speed up a bit. Default value is 0.85 seconds.
840 elm_clock_interval_get(const Evas_Object *obj)
842 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
843 Widget_Data *wd = elm_widget_data_get(obj);
845 return wd->first_interval;