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);
51 _del_hook(Evas_Object *obj)
53 Widget_Data *wd = elm_widget_data_get(obj);
56 for (i = 0; i < 6; i++)
58 if (wd->digit[i]) evas_object_del(wd->digit[i]);
60 if (wd->ampm) evas_object_del(wd->ampm);
61 if (wd->ticker) ecore_timer_del(wd->ticker);
62 if (wd->spin) ecore_timer_del(wd->spin);
67 _theme_hook(Evas_Object *obj)
69 Widget_Data *wd = elm_widget_data_get(obj);
71 if (elm_widget_focus_get(obj))
72 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
74 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
75 wd->cur.am_pm = !wd->cur.am_pm; /* hack - force update */
80 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
82 Widget_Data *wd = elm_widget_data_get(obj);
84 if (elm_widget_focus_get(obj))
86 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
87 evas_object_focus_set(wd->clk, EINA_TRUE);
91 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
92 evas_object_focus_set(wd->clk, EINA_FALSE);
97 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
99 Widget_Data *wd = elm_widget_data_get(obj);
102 edje_object_signal_emit(wd->clk, emission, source);
103 for (i = 0; i < 6; i++)
106 edje_object_signal_emit(wd->digit[i], emission, source);
111 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
113 Widget_Data *wd = elm_widget_data_get(obj);
116 edje_object_signal_callback_add(wd->clk, emission, source, func_cb, data);
117 for (i = 0; i < 6; i++)
120 edje_object_signal_callback_add(wd->digit[i], emission, source,
126 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
128 Widget_Data *wd = elm_widget_data_get(obj);
130 for (i = 0; i < 6; i++)
132 edje_object_signal_callback_del_full(wd->digit[i], emission, source,
135 edje_object_signal_callback_del_full(wd->clk, emission, source, func_cb,
140 _timediff_set(Widget_Data *wd)
142 struct timeval timev;
145 gettimeofday(&timev, NULL);
146 tt = (time_t)(timev.tv_sec);
149 wd->timediff = (((wd->hrs - tm->tm_hour) * 60 +
150 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;
172 wd->hrs = tm->tm_hour;
173 wd->min = tm->tm_min;
174 wd->sec = tm->tm_sec;
178 return ECORE_CALLBACK_CANCEL;
182 _signal_clock_val_up(void *data)
184 Widget_Data *wd = elm_widget_data_get(data);
185 if (!wd) goto clock_val_up_exit_on_error;
186 if (!wd->edit) goto clock_val_up_cancel;
187 if (!wd->sel_obj) goto clock_val_up_cancel;
188 if (wd->sel_obj == wd->digit[0])
190 wd->hrs = wd->hrs + 10;
191 if (wd->hrs >= 24) wd->hrs -= 24;
193 if (wd->sel_obj == wd->digit[1])
195 wd->hrs = wd->hrs + 1;
196 if (wd->hrs >= 24) wd->hrs -= 24;
198 if (wd->sel_obj == wd->digit[2])
200 wd->min = wd->min + 10;
201 if (wd->min >= 60) wd->min -= 60;
203 if (wd->sel_obj == wd->digit[3])
205 wd->min = wd->min + 1;
206 if (wd->min >= 60) wd->min -= 60;
208 if (wd->sel_obj == wd->digit[4])
210 wd->sec = wd->sec + 10;
211 if (wd->sec >= 60) wd->sec -= 60;
213 if (wd->sel_obj == wd->digit[5])
215 wd->sec = wd->sec + 1;
216 if (wd->sec >= 60) wd->sec -= 60;
218 if (wd->sel_obj == wd->ampm)
220 wd->hrs = wd->hrs + 12;
221 if (wd->hrs > 23) wd->hrs -= 24;
223 wd->interval = wd->interval / 1.05;
224 ecore_timer_interval_set(wd->spin, wd->interval);
226 evas_object_smart_callback_call(data, "changed", NULL);
227 return ECORE_CALLBACK_RENEW;
230 clock_val_up_exit_on_error:
231 return ECORE_CALLBACK_CANCEL;
235 _signal_clock_val_down(void *data)
237 Widget_Data *wd = elm_widget_data_get(data);
238 if (!wd) goto clock_val_down_exit_on_error;
239 if (!wd->edit) goto clock_val_down_cancel;
240 if (!wd->sel_obj) goto clock_val_down_cancel;
241 if (wd->sel_obj == wd->digit[0])
243 wd->hrs = wd->hrs - 10;
244 if (wd->hrs < 0) wd->hrs += 24;
246 if (wd->sel_obj == wd->digit[1])
248 wd->hrs = wd->hrs - 1;
249 if (wd->hrs < 0) wd->hrs += 24;
251 if (wd->sel_obj == wd->digit[2])
253 wd->min = wd->min - 10;
254 if (wd->min < 0) wd->min += 60;
256 if (wd->sel_obj == wd->digit[3])
258 wd->min = wd->min - 1;
259 if (wd->min < 0) wd->min += 60;
261 if (wd->sel_obj == wd->digit[4])
263 wd->sec = wd->sec - 10;
264 if (wd->sec < 0) wd->sec += 60;
266 if (wd->sel_obj == wd->digit[5])
268 wd->sec = wd->sec - 1;
269 if (wd->sec < 0) wd->sec += 60;
271 if (wd->sel_obj == wd->ampm)
273 wd->hrs = wd->hrs - 12;
274 if (wd->hrs < 0) wd->hrs += 24;
276 wd->interval = wd->interval / 1.05;
277 ecore_timer_interval_set(wd->spin, wd->interval);
279 evas_object_smart_callback_call(data, "changed", NULL);
280 return ECORE_CALLBACK_RENEW;
281 clock_val_down_cancel:
283 clock_val_down_exit_on_error:
284 return ECORE_CALLBACK_CANCEL;
288 _signal_clock_val_up_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
290 Widget_Data *wd = elm_widget_data_get(data);
292 wd->interval = wd->first_interval;
294 if (wd->spin) ecore_timer_del(wd->spin);
295 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_up, data);
296 _signal_clock_val_up(data);
300 _signal_clock_val_down_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
302 Widget_Data *wd = elm_widget_data_get(data);
304 wd->interval = wd->first_interval;
306 if (wd->spin) ecore_timer_del(wd->spin);
307 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_down, data);
308 _signal_clock_val_down(data);
312 _signal_clock_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
314 Widget_Data *wd = elm_widget_data_get(data);
316 if (wd->spin) ecore_timer_del(wd->spin);
322 _time_update(Evas_Object *obj)
324 Widget_Data *wd = elm_widget_data_get(obj);
325 Edje_Message_Int msg;
327 const char *style = elm_widget_style_get(obj);
329 if ((wd->cur.seconds != wd->seconds) || (wd->cur.am_pm != wd->am_pm) ||
330 (wd->cur.edit != wd->edit) || (wd->cur.digedit != wd->digedit))
335 for (i = 0; i < 6; i++)
339 evas_object_del(wd->digit[i]);
345 evas_object_del(wd->ampm);
349 if ((wd->seconds) && (wd->am_pm))
350 _elm_theme_object_set(obj, wd->clk, "clock", "base-all", style);
351 else if (wd->seconds)
352 _elm_theme_object_set(obj, wd->clk, "clock", "base-seconds", style);
354 _elm_theme_object_set(obj, wd->clk, "clock", "base-am_pm", style);
356 _elm_theme_object_set(obj, wd->clk, "clock", "base", style);
357 edje_object_scale_set(wd->clk, elm_widget_scale_get(obj) *
360 for (i = 0; i < 6; i++)
364 if ((!wd->seconds) && (i >= 4)) break;
365 wd->digit[i] = edje_object_add(evas_object_evas_get(wd->clk));
366 _elm_theme_object_set(obj, wd->digit[i], "clock", "flipdigit", style);
367 edje_object_scale_set(wd->digit[i], elm_widget_scale_get(obj) *
369 if ((wd->edit) && (wd->digedit & (1 << i)))
370 edje_object_signal_emit(wd->digit[i], "elm,state,edit,on", "elm");
371 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,start",
372 "", _signal_clock_val_up_start, obj);
373 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,stop",
374 "", _signal_clock_val_change_stop, obj);
375 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,start",
376 "", _signal_clock_val_down_start, obj);
377 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,stop",
378 "", _signal_clock_val_change_stop, obj);
380 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
381 edje_object_size_min_restricted_calc(wd->digit[i], &mw, &mh, mw, mh);
382 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
383 edje_extern_object_min_size_set(wd->digit[i], mw, mh);
384 snprintf(buf, sizeof(buf), "d%i", i);
385 edje_object_part_swallow(wd->clk , buf, wd->digit[i]);
386 evas_object_show(wd->digit[i]);
390 wd->ampm = edje_object_add(evas_object_evas_get(wd->clk));
391 _elm_theme_object_set(obj, wd->ampm, "clock", "flipampm", style);
392 edje_object_scale_set(wd->ampm, elm_widget_scale_get(obj) *
395 edje_object_signal_emit(wd->ampm, "elm,state,edit,on", "elm");
396 edje_object_signal_callback_add(wd->ampm, "elm,action,up,start",
397 "", _signal_clock_val_up_start, obj);
398 edje_object_signal_callback_add(wd->ampm, "elm,action,up,stop",
399 "", _signal_clock_val_change_stop, obj);
400 edje_object_signal_callback_add(wd->ampm, "elm,action,down,start",
401 "", _signal_clock_val_down_start, obj);
402 edje_object_signal_callback_add(wd->ampm, "elm,action,down,stop",
403 "", _signal_clock_val_change_stop, obj);
405 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
406 edje_object_size_min_restricted_calc(wd->ampm, &mw, &mh, mw, mh);
407 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
408 edje_extern_object_min_size_set(wd->ampm, mw, mh);
409 edje_object_part_swallow(wd->clk , "ampm", wd->ampm);
410 evas_object_show(wd->ampm);
413 edje_object_size_min_calc(wd->clk, &mw, &mh);
414 evas_object_size_hint_min_set(obj, mw, mh);
420 wd->cur.seconds = wd->seconds;
421 wd->cur.am_pm = wd->am_pm;
422 wd->cur.edit = wd->edit;
423 wd->cur.digedit = wd->digedit;
425 if (wd->hrs != wd->cur.hrs)
428 int d1, d2, dc1, dc2;
435 if (hrs > 12) hrs -= 12;
438 else if (!hrs) hrs = 12;
442 dc1 = wd->cur.hrs / 10;
443 dc2 = wd->cur.hrs % 10;
447 edje_object_message_send(wd->digit[0], EDJE_MESSAGE_INT, 1, &msg);
452 edje_object_message_send(wd->digit[1], EDJE_MESSAGE_INT, 1, &msg);
456 if (wd->min != wd->cur.min)
458 int d1, d2, dc1, dc2;
462 dc1 = wd->cur.min / 10;
463 dc2 = wd->cur.min % 10;
467 edje_object_message_send(wd->digit[2], EDJE_MESSAGE_INT, 1, &msg);
472 edje_object_message_send(wd->digit[3], EDJE_MESSAGE_INT, 1, &msg);
474 wd->cur.min = wd->min;
478 if (wd->sec != wd->cur.sec)
480 int d1, d2, dc1, dc2;
484 dc1 = wd->cur.sec / 10;
485 dc2 = wd->cur.sec % 10;
489 edje_object_message_send(wd->digit[4], EDJE_MESSAGE_INT, 1, &msg);
494 edje_object_message_send(wd->digit[5], EDJE_MESSAGE_INT, 1, &msg);
496 wd->cur.sec = wd->sec;
504 if (wd->hrs >= 12) ampm = 1;
505 if (ampm != wd->cur.ampm)
507 if (wd->cur.ampm != ampm)
510 edje_object_message_send(wd->ampm, EDJE_MESSAGE_INT, 1, &msg);
520 * Add a new clock to the parent
522 * @param parent The parent object
524 * This function inserts a clock widget on a given canvas to show a
530 elm_clock_add(Evas_Object *parent)
536 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
538 ELM_SET_WIDTYPE(widtype, "clock");
539 elm_widget_type_set(obj, "clock");
540 elm_widget_sub_object_add(parent, obj);
541 elm_widget_data_set(obj, wd);
542 elm_widget_del_hook_set(obj, _del_hook);
543 elm_widget_theme_hook_set(obj, _theme_hook);
544 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
545 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
546 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
547 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
548 elm_widget_can_focus_set(obj, EINA_TRUE);
550 wd->clk = edje_object_add(e);
551 elm_widget_resize_object_set(obj, wd->clk);
554 wd->cur.seconds = EINA_TRUE;
555 wd->cur.am_pm = EINA_TRUE;
556 wd->cur.edit = EINA_TRUE;
557 wd->cur.digedit = ELM_CLOCK_NONE;
558 wd->first_interval = 0.85;
570 * @param obj The clock object
571 * @param hrs The hours to set
572 * @param min The minutes to set
573 * @param sec The secondes to set
575 * This function updates the time that is showed by the clock widget
580 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
582 ELM_CHECK_WIDTYPE(obj, widtype);
583 Widget_Data *wd = elm_widget_data_get(obj);
595 * @param obj The clock object
596 * @param hrs Pointer to the variable to get the hour of this clock
598 * @param min Pointer to the variable to get the minute of this clock
600 * @param sec Pointer to the variable to get the second of this clock
603 * This function gets the time set of the clock widget and returns it
604 * on the variables passed as the arguments to function
609 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
611 ELM_CHECK_WIDTYPE(obj, widtype);
612 Widget_Data *wd = elm_widget_data_get(obj);
614 if (hrs) *hrs = wd->hrs;
615 if (min) *min = wd->min;
616 if (sec) *sec = wd->sec;
620 * Set if the clock settings can be edited
622 * @param obj The clock object
623 * @param edit Bool option for edited (1 = yes, 0 = no)
625 * This function sets if the clock settings can be edited or not.
626 * By default or if digit_edit option was previously set to ELM_CLOCK_NONE,
627 * all digits are editable. To choose what digits to make editable
628 * use elm_clock_digit_edit_set().
633 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
635 ELM_CHECK_WIDTYPE(obj, widtype);
636 Widget_Data *wd = elm_widget_data_get(obj);
641 if ((edit) && (wd->digedit == ELM_CLOCK_NONE))
642 elm_clock_digit_edit_set(obj, ELM_CLOCK_ALL);
648 * Get if the clock settings can be edited
650 * @param obj The clock object
651 * @return Bool option for edited (1 = yes, 0 = no)
653 * This function gets if the clock settings can be edited or not.
658 elm_clock_edit_get(const Evas_Object *obj)
660 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
661 Widget_Data *wd = elm_widget_data_get(obj);
662 if (!wd) return EINA_FALSE;
667 * Set what digits of the clock are editable
669 * @param obj The clock object
670 * @param digedit Bit mask indicating the digits to edit
672 * If the digedit param is ELM_CLOCK_NONE, editing will be disabled.
677 elm_clock_digit_edit_set(Evas_Object *obj, Elm_Clock_Digedit digedit)
679 ELM_CHECK_WIDTYPE(obj, widtype);
680 Widget_Data *wd = elm_widget_data_get(obj);
682 wd->digedit = digedit;
683 if (digedit == ELM_CLOCK_NONE)
684 elm_clock_edit_set(obj, EINA_FALSE);
690 * Get what digits of the clock are editable
692 * @param obj The clock object
693 * @return Bit mask indicating the digits.
697 EAPI Elm_Clock_Digedit
698 elm_clock_digit_edit_get(const Evas_Object *obj)
700 ELM_CHECK_WIDTYPE(obj, widtype) 0;
701 Widget_Data *wd = elm_widget_data_get(obj);
707 * Set if the clock shows hours in military or am/pm mode
709 * @param obj The clock object
710 * @param am_pm Bool option for the hours mode
711 * (1 = am/pm, 0 = military)
713 * This function sets the clock to show hours in military or am/pm
714 * mode. Some countries like Brazil the military mode (00-24h-format)
715 * is used in opposition to the USA where the am/pm mode is more
721 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
723 ELM_CHECK_WIDTYPE(obj, widtype);
724 Widget_Data *wd = elm_widget_data_get(obj);
731 * Get if the clock shows hours in military or am/pm mode
733 * @param obj The clock object
734 * @return Bool option for the hours mode
735 * (1 = am/pm, 0 = military)
737 * This function gets if the clock show hours in military or am/pm
738 * mode. Some countries like Brazil the military mode (00-24h-format)
739 * is used in opposition to the USA where the am/pm mode is more
745 elm_clock_show_am_pm_get(const Evas_Object *obj)
747 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
748 Widget_Data *wd = elm_widget_data_get(obj);
749 if (!wd) return EINA_FALSE;
754 * Set if the clock shows hour with the seconds
756 * @param obj The clock object
757 * @param seconds Bool option for the show seconds
758 * (1 = show seconds, 0 = not show seconds)
760 * This function sets the clock to show or not to show the elapsed
766 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
768 ELM_CHECK_WIDTYPE(obj, widtype);
769 Widget_Data *wd = elm_widget_data_get(obj);
771 wd->seconds = seconds;
776 * Get if the clock shows hour with the seconds
778 * @param obj The clock object
779 * @return Bool option for the show seconds
780 * (1 = show seconds, 0 = not show seconds)
782 * This function gets if the clock show or not show the elapsed
788 elm_clock_show_seconds_get(const Evas_Object *obj)
790 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
791 Widget_Data *wd = elm_widget_data_get(obj);
792 if (!wd) return EINA_FALSE;
797 * Set the interval for the clock
799 * @param obj The clock object
800 * @param interval The interval value in seconds
802 * The interval value is decreased while the user increments or decrements
803 * the clock value. The next interval value is the previous interval / 1.05,
804 * so it speed up a bit. Default value is 0.85 seconds.
809 elm_clock_interval_set(Evas_Object *obj, double interval)
811 ELM_CHECK_WIDTYPE(obj, widtype);
812 Widget_Data *wd = elm_widget_data_get(obj);
814 wd->first_interval = interval;
818 * Get the interval of the clock
820 * @param obj The clock object
821 * @return The value of the first interval in seconds
823 * The interval value is decreased while the user increments or decrements
824 * the clock value. The next interval value is the previous interval / 1.05,
825 * so it speed up a bit. Default value is 0.85 seconds.
830 elm_clock_interval_get(const Evas_Object *obj)
832 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
833 Widget_Data *wd = elm_widget_data_get(obj);
835 return wd->first_interval;