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;
19 Eina_Bool seconds : 1;
22 Elm_Clock_Digedit digedit;
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 int _ticker(void *data);
43 static int _signal_clock_val_up(void *data);
44 static int _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))
82 edje_object_signal_emit(wd->clk, "elm,action,focus", "elm");
84 edje_object_signal_emit(wd->clk, "elm,action,unfocus", "elm");
90 Widget_Data *wd = elm_widget_data_get(data);
96 gettimeofday(&timev, NULL);
97 t = ((double)(1000000 - timev.tv_usec)) / 1000000.0;
98 wd->ticker = ecore_timer_add(t, _ticker, data);
101 tt = (time_t)(timev.tv_sec);
106 wd->hrs = tm->tm_hour;
107 wd->min = tm->tm_min;
108 wd->sec = tm->tm_sec;
116 _signal_clock_val_up(void *data)
118 Widget_Data *wd = elm_widget_data_get(data);
120 if (!wd->edit) return;
121 if (!wd->sel_obj) return;
122 if (wd->sel_obj == wd->digit[0])
124 wd->hrs = wd->hrs + 10;
125 if (wd->hrs >= 24) wd->hrs -= 24;
127 if (wd->sel_obj == wd->digit[1])
129 wd->hrs = wd->hrs + 1;
130 if (wd->hrs >= 24) wd->hrs -= 24;
132 if (wd->sel_obj == wd->digit[2])
134 wd->min = wd->min + 10;
135 if (wd->min >= 60) wd->min -= 60;
137 if (wd->sel_obj == wd->digit[3])
139 wd->min = wd->min + 1;
140 if (wd->min >= 60) wd->min -= 60;
142 if (wd->sel_obj == wd->digit[4])
144 wd->sec = wd->sec + 10;
145 if (wd->sec >= 60) wd->sec -= 60;
147 if (wd->sel_obj == wd->digit[5])
149 wd->sec = wd->sec + 1;
150 if (wd->sec >= 60) wd->sec -= 60;
152 if (wd->sel_obj == wd->ampm)
154 wd->hrs = wd->hrs + 12;
155 if (wd->hrs > 23) wd->hrs -= 24;
157 wd->interval = wd->interval / 1.05;
158 ecore_timer_interval_set(wd->spin, wd->interval);
160 evas_object_smart_callback_call(data, "changed", NULL);
161 return ECORE_CALLBACK_RENEW;
165 _signal_clock_val_down(void *data)
167 Widget_Data *wd = elm_widget_data_get(data);
169 if (!wd->edit) return;
170 if (!wd->sel_obj) return;
171 if (wd->sel_obj == wd->digit[0])
173 wd->hrs = wd->hrs - 10;
174 if (wd->hrs < 0) wd->hrs += 24;
176 if (wd->sel_obj == wd->digit[1])
178 wd->hrs = wd->hrs - 1;
179 if (wd->hrs < 0) wd->hrs += 24;
181 if (wd->sel_obj == wd->digit[2])
183 wd->min = wd->min - 10;
184 if (wd->min < 0) wd->min += 60;
186 if (wd->sel_obj == wd->digit[3])
188 wd->min = wd->min - 1;
189 if (wd->min < 0) wd->min += 60;
191 if (wd->sel_obj == wd->digit[4])
193 wd->sec = wd->sec - 10;
194 if (wd->sec < 0) wd->sec += 60;
196 if (wd->sel_obj == wd->digit[5])
198 wd->sec = wd->sec - 1;
199 if (wd->sec < 0) wd->sec += 60;
201 if (wd->sel_obj == wd->ampm)
203 wd->hrs = wd->hrs - 12;
204 if (wd->hrs < 0) wd->hrs += 24;
206 wd->interval = wd->interval / 1.05;
207 ecore_timer_interval_set(wd->spin, wd->interval);
209 evas_object_smart_callback_call(data, "changed", NULL);
210 return ECORE_CALLBACK_RENEW;
214 _signal_clock_val_up_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
216 Widget_Data *wd = elm_widget_data_get(data);
220 if (wd->spin) ecore_timer_del(wd->spin);
221 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_up, data);
222 _signal_clock_val_up(data);
226 _signal_clock_val_down_start(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
228 Widget_Data *wd = elm_widget_data_get(data);
232 if (wd->spin) ecore_timer_del(wd->spin);
233 wd->spin = ecore_timer_add(wd->interval, _signal_clock_val_down, data);
234 _signal_clock_val_down(data);
238 _signal_clock_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
240 Widget_Data *wd = elm_widget_data_get(data);
242 if (wd->spin) ecore_timer_del(wd->spin);
248 _time_update(Evas_Object *obj)
250 Widget_Data *wd = elm_widget_data_get(obj);
251 Edje_Message_Int msg;
253 const char *style = elm_widget_style_get(obj);
255 if ((wd->cur.seconds != wd->seconds) || (wd->cur.am_pm != wd->am_pm) ||
256 (wd->cur.edit != wd->edit) || (wd->cur.digedit != wd->digedit))
261 for (i = 0; i < 6; i++)
265 evas_object_del(wd->digit[i]);
271 evas_object_del(wd->ampm);
275 if ((wd->seconds) && (wd->am_pm))
276 _elm_theme_object_set(obj, wd->clk, "clock", "base-all", style);
277 else if (wd->seconds)
278 _elm_theme_object_set(obj, wd->clk, "clock", "base-seconds", style);
280 _elm_theme_object_set(obj, wd->clk, "clock", "base-am_pm", style);
282 _elm_theme_object_set(obj, wd->clk, "clock", "base", style);
283 edje_object_scale_set(wd->clk, elm_widget_scale_get(obj) *
286 for (i = 0; i < 6; i++)
290 if ((!wd->seconds) && (i >= 4)) break;
291 wd->digit[i] = edje_object_add(evas_object_evas_get(wd->clk));
292 _elm_theme_object_set(obj, wd->digit[i], "clock", "flipdigit", style);
293 edje_object_scale_set(wd->digit[i], elm_widget_scale_get(obj) *
295 if (wd->edit && (wd->digedit & (1 << i)))
296 edje_object_signal_emit(wd->digit[i], "elm,state,edit,on", "elm");
297 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,start",
298 "", _signal_clock_val_up_start, obj);
299 edje_object_signal_callback_add(wd->digit[i], "elm,action,up,stop",
300 "", _signal_clock_val_change_stop, obj);
301 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,start",
302 "", _signal_clock_val_down_start, obj);
303 edje_object_signal_callback_add(wd->digit[i], "elm,action,down,stop",
304 "", _signal_clock_val_change_stop, obj);
306 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
307 edje_object_size_min_restricted_calc(wd->digit[i], &mw, &mh, mw, mh);
308 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
309 edje_extern_object_min_size_set(wd->digit[i], mw, mh);
310 snprintf(buf, sizeof(buf), "d%i", i);
311 edje_object_part_swallow(wd->clk , buf, wd->digit[i]);
312 evas_object_show(wd->digit[i]);
316 wd->ampm = edje_object_add(evas_object_evas_get(wd->clk));
317 _elm_theme_object_set(obj, wd->ampm, "clock", "flipampm", style);
318 edje_object_scale_set(wd->ampm, elm_widget_scale_get(obj) *
321 edje_object_signal_emit(wd->ampm, "elm,state,edit,on", "elm");
322 edje_object_signal_callback_add(wd->ampm, "elm,action,up,start",
323 "", _signal_clock_val_up_start, obj);
324 edje_object_signal_callback_add(wd->ampm, "elm,action,up,stop",
325 "", _signal_clock_val_change_stop, obj);
326 edje_object_signal_callback_add(wd->ampm, "elm,action,down,start",
327 "", _signal_clock_val_down_start, obj);
328 edje_object_signal_callback_add(wd->ampm, "elm,action,down,stop",
329 "", _signal_clock_val_change_stop, obj);
331 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
332 edje_object_size_min_restricted_calc(wd->ampm, &mw, &mh, mw, mh);
333 elm_coords_finger_size_adjust(1, &mw, 2, &mh);
334 edje_extern_object_min_size_set(wd->ampm, mw, mh);
335 edje_object_part_swallow(wd->clk , "ampm", wd->ampm);
336 evas_object_show(wd->ampm);
339 edje_object_size_min_calc(wd->clk, &mw, &mh);
340 evas_object_size_hint_min_set(obj, mw, mh);
346 wd->cur.seconds = wd->seconds;
347 wd->cur.am_pm = wd->am_pm;
348 wd->cur.edit = wd->edit;
349 wd->cur.digedit = wd->digedit;
351 if (wd->hrs != wd->cur.hrs)
354 int d1, d2, dc1, dc2;
361 if (hrs > 12) hrs -= 12;
364 else if (hrs == 0) hrs = 12;
368 dc1 = wd->cur.hrs / 10;
369 dc2 = wd->cur.hrs % 10;
373 edje_object_message_send(wd->digit[0], EDJE_MESSAGE_INT, 1, &msg);
378 edje_object_message_send(wd->digit[1], EDJE_MESSAGE_INT, 1, &msg);
382 if (wd->min != wd->cur.min)
384 int d1, d2, dc1, dc2;
388 dc1 = wd->cur.min / 10;
389 dc2 = wd->cur.min % 10;
393 edje_object_message_send(wd->digit[2], EDJE_MESSAGE_INT, 1, &msg);
398 edje_object_message_send(wd->digit[3], EDJE_MESSAGE_INT, 1, &msg);
400 wd->cur.min = wd->min;
404 if (wd->sec != wd->cur.sec)
406 int d1, d2, dc1, dc2;
410 dc1 = wd->cur.sec / 10;
411 dc2 = wd->cur.sec % 10;
415 edje_object_message_send(wd->digit[4], EDJE_MESSAGE_INT, 1, &msg);
420 edje_object_message_send(wd->digit[5], EDJE_MESSAGE_INT, 1, &msg);
422 wd->cur.sec = wd->sec;
430 if (wd->hrs >= 12) ampm = 1;
431 if (ampm != wd->cur.ampm)
433 if (wd->cur.ampm != ampm)
436 edje_object_message_send(wd->ampm, EDJE_MESSAGE_INT, 1, &msg);
446 * Add a new clock to the parent
448 * @param parent The parent object
450 * This function inserts a clock widget on a given canvas to show a
456 elm_clock_add(Evas_Object *parent)
462 wd = ELM_NEW(Widget_Data);
463 e = evas_object_evas_get(parent);
464 obj = elm_widget_add(e);
465 ELM_SET_WIDTYPE(widtype, "clock");
466 elm_widget_type_set(obj, "clock");
467 elm_widget_sub_object_add(parent, obj);
468 elm_widget_data_set(obj, wd);
469 elm_widget_del_hook_set(obj, _del_hook);
470 elm_widget_theme_hook_set(obj, _theme_hook);
471 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
473 wd->clk = edje_object_add(e);
474 elm_widget_resize_object_set(obj, wd->clk);
477 wd->cur.seconds = EINA_TRUE;
478 wd->cur.am_pm = EINA_TRUE;
479 wd->cur.edit = EINA_TRUE;
480 wd->cur.digedit = ELM_CLOCK_NONE;
491 * @param obj The clock object
492 * @param hrs The hours to set
493 * @param min The minutes to set
494 * @param sec The secondes to set
496 * This function updates the time that is showed by the clock widget
501 elm_clock_time_set(Evas_Object *obj, int hrs, int min, int sec)
503 ELM_CHECK_WIDTYPE(obj, widtype);
504 Widget_Data *wd = elm_widget_data_get(obj);
515 * @param obj The clock object
516 * @param hrs Pointer to the variable to get the hour of this clock
518 * @param min Pointer to the variable to get the minute of this clock
520 * @param sec Pointer to the variable to get the second of this clock
523 * This function gets the time set of the clock widget and returns it
524 * on the variables passed as the arguments to function
529 elm_clock_time_get(const Evas_Object *obj, int *hrs, int *min, int *sec)
531 ELM_CHECK_WIDTYPE(obj, widtype);
532 Widget_Data *wd = elm_widget_data_get(obj);
534 if (hrs) *hrs = wd->hrs;
535 if (min) *min = wd->min;
536 if (sec) *sec = wd->sec;
540 * Set if the clock settings can be edited
542 * @param obj The clock object
543 * @param edit Bool option for edited (1 = yes, 0 = no)
545 * This function sets if the clock settings can be edited or not.
546 * By default or if digit_edit option was previously set to ELM_CLOCK_NONE,
547 * all digits are editable. To choose what digits to make editable
548 * use elm_clock_digit_edit_set().
553 elm_clock_edit_set(Evas_Object *obj, Eina_Bool edit)
555 ELM_CHECK_WIDTYPE(obj, widtype);
556 Widget_Data *wd = elm_widget_data_get(obj);
559 if (edit && (wd->digedit == ELM_CLOCK_NONE))
560 elm_clock_digit_edit_set(obj, ELM_CLOCK_ALL);
566 * Get if the clock settings can be edited
568 * @param obj The clock object
569 * @return Bool option for edited (1 = yes, 0 = no)
571 * This function gets if the clock settings can be edited or not.
576 elm_clock_edit_get(const Evas_Object *obj)
578 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
579 Widget_Data *wd = elm_widget_data_get(obj);
580 if (!wd) return EINA_FALSE;
585 * Set what digits of the clock are editable
587 * @param obj The clock object
588 * @param digedit Bit mask indicating the digits to edit
590 * If the digedit param is ELM_CLOCK_NONE, editing will be disabled.
595 elm_clock_digit_edit_set(Evas_Object *obj, Elm_Clock_Digedit digedit)
597 ELM_CHECK_WIDTYPE(obj, widtype);
598 Widget_Data *wd = elm_widget_data_get(obj);
600 wd->digedit = digedit;
601 if (digedit == ELM_CLOCK_NONE)
602 elm_clock_edit_set(obj, EINA_FALSE);
608 * Get what digits of the clock are editable
610 * @param obj The clock object
611 * @return Bit mask indicating the digits.
615 EAPI Elm_Clock_Digedit
616 elm_clock_digit_edit_get(const Evas_Object *obj)
618 ELM_CHECK_WIDTYPE(obj, widtype) 0;
619 Widget_Data *wd = elm_widget_data_get(obj);
625 * Set if the clock shows hours in military or am/pm mode
627 * @param obj The clock object
628 * @param am_pm Bool option for the hours mode
629 * (1 = am/pm, 0 = military)
631 * This function sets the clock to show hours in military or am/pm
632 * mode. Some countries like Brazil the military mode (00-24h-format)
633 * is used in opposition to the USA where the am/pm mode is more
639 elm_clock_show_am_pm_set(Evas_Object *obj, Eina_Bool am_pm)
641 ELM_CHECK_WIDTYPE(obj, widtype);
642 Widget_Data *wd = elm_widget_data_get(obj);
649 * Get if the clock shows hours in military or am/pm mode
651 * @param obj The clock object
652 * @return Bool option for the hours mode
653 * (1 = am/pm, 0 = military)
655 * This function gets if the clock show hours in military or am/pm
656 * mode. Some countries like Brazil the military mode (00-24h-format)
657 * is used in opposition to the USA where the am/pm mode is more
663 elm_clock_show_am_pm_get(const Evas_Object *obj)
665 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
666 Widget_Data *wd = elm_widget_data_get(obj);
667 if (!wd) return EINA_FALSE;
672 * Set if the clock shows hour with the seconds
674 * @param obj The clock object
675 * @param seconds Bool option for the show seconds
676 * (1 = show seconds, 0 = not show seconds)
678 * This function sets the clock to show or not to show the elapsed
684 elm_clock_show_seconds_set(Evas_Object *obj, Eina_Bool seconds)
686 ELM_CHECK_WIDTYPE(obj, widtype);
687 Widget_Data *wd = elm_widget_data_get(obj);
689 wd->seconds = seconds;
694 * Get if the clock shows hour with the seconds
696 * @param obj The clock object
697 * @return Bool option for the show seconds
698 * (1 = show seconds, 0 = not show seconds)
700 * This function gets if the clock show or not show the elapsed
706 elm_clock_show_seconds_get(const Evas_Object *obj)
708 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
709 Widget_Data *wd = elm_widget_data_get(obj);
710 if (!wd) return EINA_FALSE;