1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Elm_Dayselector_Item Elm_Dayselector_Item;
8 #define ITEM_TYPE_WEEKDAY_DEFAULT "elm,type,weekday,default"
9 #define ITEM_TYPE_WEEKDAY_STYLE1 "elm,type,weekday,style1"
10 #define ITEM_TYPE_WEEKEND_DEFAULT "elm,type,weekend,default"
11 #define ITEM_TYPE_WEEKEND_STYLE1 "elm,type,weekend,style1"
12 #define ITEM_POS_LEFT "elm,pos,check,left"
13 #define ITEM_POS_RIGHT "elm,pos,check,right"
14 #define ITEM_POS_MIDDLE "elm,pos,check,middle"
20 Elm_Dayselector_Day week_start;
21 Elm_Dayselector_Day weekend_start;
22 unsigned int weekend_len;
25 struct _Elm_Dayselector_Item
28 Elm_Dayselector_Day day;
29 const char *day_style;
32 static const char *widtype = NULL;
33 static void _del_hook(Evas_Object *obj);
34 static void _sizing_eval(Evas_Object* obj);
35 static void _dayselector_resize(void *data, Evas *e __UNUSED__,
36 Evas_Object *obj __UNUSED__, void *event_info __UNUSED__);
37 static void _disable_hook(Evas_Object *obj);
38 static Eina_Bool _focus_next_hook(const Evas_Object *obj,
39 Elm_Focus_Direction dir __UNUSED__,
40 Evas_Object **next __UNUSED__);
41 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
42 static void _translate_hook(Evas_Object *obj);
43 static void _theme_hook(Evas_Object *obj);
44 static void _item_text_set_hook(Evas_Object *obj, const char *item,
46 static const char *_item_text_get_hook(const Evas_Object *obj,
48 static void _content_set_hook(Evas_Object *obj, const char *item,
49 Evas_Object *content);
50 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *item);
51 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *item);
52 static void _signal_emit_cb(void *data, Evas_Object *obj, const char *emission,
53 const char *source __UNUSED__);
54 static void _item_clicked_cb(void *data, Evas_Object *obj,
55 void *event_info __UNUSED__);
56 static Elm_Dayselector_Item * _item_find(const Evas_Object *obj,
57 Elm_Dayselector_Day day);
58 static void _items_style_set(Evas_Object *obj);
59 static void _update_items(Evas_Object *obj);
60 static void _create_items(Evas_Object *obj);
62 static const char SIG_CHANGED[] = "dayselector,changed";
63 static const char SIG_LANG_CHANGED[] = "language,changed";
64 static const Evas_Smart_Cb_Description _signals[] = {
66 {SIG_LANG_CHANGED, ""},
71 _del_hook(Evas_Object *obj)
73 Elm_Dayselector_Item *it;
74 Widget_Data *wd = elm_widget_data_get(obj);
77 EINA_LIST_FREE(wd->items, it)
79 wd->items = eina_list_remove(wd->items, it);
80 eina_stringshare_del(it->day_style);
81 elm_widget_item_free(it);
87 _sizing_eval(Evas_Object* obj)
89 Evas_Coord min_w = -1, min_h = -1;
90 Widget_Data *wd = elm_widget_data_get(obj);
93 elm_coords_finger_size_adjust(ELM_DAYSELECTOR_MAX, &min_w, 1, &min_h);
94 edje_object_size_min_restricted_calc(wd->base, &min_w, &min_h, min_w, min_h);
95 elm_coords_finger_size_adjust(ELM_DAYSELECTOR_MAX, &min_w, 1, &min_h);
96 evas_object_size_hint_min_set(obj, min_w, min_h);
100 _dayselector_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
101 void *event_info __UNUSED__)
107 _disable_hook(Evas_Object *obj)
109 Widget_Data* wd = elm_widget_data_get(obj);
112 if (elm_widget_disabled_get(obj))
113 edje_object_signal_emit(wd->base, "elm,state,disabled", "elm");
115 edje_object_signal_emit(wd->base, "elm,state,enabled", "elm");
119 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir __UNUSED__,
120 Evas_Object **next __UNUSED__)
122 Widget_Data *wd = elm_widget_data_get(obj);
123 if (!wd) return EINA_FALSE;
125 /* TODO: Focus switch support to Elm_widget_Item is not supported yet.*/
130 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
132 Widget_Data *wd = elm_widget_data_get(obj);
135 edje_object_mirrored_set(wd->base, rtl);
139 _translate_hook(Evas_Object *obj)
142 Elm_Dayselector_Item *it;
143 struct tm time_daysel;
146 Widget_Data *wd = elm_widget_data_get(obj);
150 localtime_r(&t, &time_daysel);
151 EINA_LIST_FOREACH(wd->items, l, it)
153 time_daysel.tm_wday = it->day;
154 strftime(buf, sizeof(buf), "%a", &time_daysel);
155 elm_object_text_set(VIEW(it), buf);
157 evas_object_smart_callback_call(obj, SIG_LANG_CHANGED, NULL);
161 _theme_hook(Evas_Object *obj)
164 Elm_Dayselector_Item *it;
167 Widget_Data* wd = elm_widget_data_get(obj);
170 _elm_widget_mirrored_reload(obj);
171 _mirrored_set(obj, elm_widget_mirrored_get(obj));
172 _elm_theme_object_set(obj, wd->base, "dayselector", "base",
173 elm_widget_style_get(obj));
175 EINA_LIST_FOREACH(wd->items, l, it)
177 snprintf(buf, sizeof(buf), "dayselector/%s", elm_object_style_get(obj));
178 elm_object_style_set(VIEW(it), buf);
179 loc = (ELM_DAYSELECTOR_MAX - wd->week_start + it->day) % ELM_DAYSELECTOR_MAX;
180 snprintf(buf, sizeof(buf), "day%d,visible", loc);
181 edje_object_signal_emit(wd->base, buf, "elm");
182 snprintf(buf, sizeof(buf), "day%d", loc);
183 edje_object_part_swallow(wd->base, buf, VIEW(it));
190 _item_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj,
191 void *event_info __UNUSED__)
193 ELM_CHECK_WIDTYPE(data, widtype);
195 Elm_Dayselector_Item *it;
197 unsigned int loc = 0;
198 Widget_Data *wd = elm_widget_data_get(data);
201 EINA_LIST_FOREACH(wd->items, l, it)
205 wd->items = eina_list_remove(wd->items, it);
206 eina_stringshare_del(it->day_style);
207 evas_object_smart_callback_del(VIEW(it), "changed", _item_clicked_cb);
208 evas_object_event_callback_del(VIEW(it), EVAS_CALLBACK_DEL, _item_del_cb);
209 edje_object_part_unswallow(wd->base, VIEW(it));
210 loc = (ELM_DAYSELECTOR_MAX - wd->week_start + it->day) % ELM_DAYSELECTOR_MAX;
211 snprintf(buf, sizeof(buf), "day%d,default", loc);
212 edje_object_signal_emit(wd->base, buf, "elm");
213 elm_widget_item_free(it);
221 _signal_emit_cb(void *data, Evas_Object *obj, const char *emission, const char *source __UNUSED__)
224 Elm_Dayselector_Item *it;
225 Widget_Data *wd = elm_widget_data_get(data);
228 EINA_LIST_FOREACH(wd->items, l, it)
232 eina_stringshare_replace(&it->day_style, emission);
239 _item_text_set_hook(Evas_Object *obj, const char *item, const char *label)
241 ELM_CHECK_WIDTYPE(obj, widtype);
243 Elm_Dayselector_Item *it;
245 Widget_Data *wd = elm_widget_data_get(obj);
246 if (item && !strcmp(item, "default")) return;
249 EINA_LIST_FOREACH(wd->items, l, it)
251 snprintf(buf, sizeof(buf), "day%d", it->day);
252 if (!strncmp(buf, item, sizeof(buf)))
254 elm_object_text_set(VIEW(it), label);
262 _item_text_get_hook(const Evas_Object *obj, const char *item)
264 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
266 Elm_Dayselector_Item *it;
268 Widget_Data *wd = elm_widget_data_get(obj);
269 if (item && !strcmp(item, "default")) return NULL;
270 if (!wd) return NULL;
272 EINA_LIST_FOREACH(wd->items, l, it)
274 snprintf(buf, sizeof(buf), "day%d", it->day);
275 if (!strncmp(buf, item, sizeof(buf)))
276 return elm_object_text_get(VIEW(it));
282 _content_set_hook(Evas_Object *obj, const char *item, Evas_Object *content)
284 ELM_CHECK_WIDTYPE(obj, widtype);
286 Elm_Dayselector_Item *it;
288 unsigned int idx, loc;
289 Widget_Data *wd = elm_widget_data_get(obj);
290 if (!wd || !content) return;
291 if (item && !strcmp(item, "default")) return;
292 if (strcmp(elm_object_widget_type_get(content), "check")) return;
294 EINA_LIST_FOREACH(wd->items, l, it)
296 snprintf(buf, sizeof(buf), "day%d", it->day);
297 if (!strncmp(buf, item, sizeof(buf)))
299 if (VIEW(it)) evas_object_del(VIEW(it));
303 for (idx = 0; idx < ELM_DAYSELECTOR_MAX; idx++)
305 snprintf(buf, sizeof(buf), "day%d", idx);
306 if (!strncmp(buf, item, sizeof(buf)))
308 it = elm_widget_item_new(obj, Elm_Dayselector_Item);
312 wd->items = eina_list_append(wd->items, it);
313 elm_widget_sub_object_add(obj, content);
314 loc = (ELM_DAYSELECTOR_MAX - wd->week_start + idx) % ELM_DAYSELECTOR_MAX;
315 snprintf(buf, sizeof(buf), "day%d", loc);
316 edje_object_part_swallow(wd->base, buf, VIEW(it));
317 snprintf(buf, sizeof(buf), "day%d,visible", loc);
318 edje_object_signal_emit(wd->base, buf, "elm");
319 evas_object_smart_callback_add(VIEW(it), "changed", _item_clicked_cb, obj);
320 evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_DEL, _item_del_cb, obj);
321 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKDAY_DEFAULT, "", _signal_emit_cb, obj);
322 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKDAY_STYLE1, "", _signal_emit_cb, obj);
323 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKEND_DEFAULT, "", _signal_emit_cb, obj);
324 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKEND_STYLE1, "", _signal_emit_cb, obj);
333 _content_get_hook(const Evas_Object *obj, const char *item)
335 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
337 Elm_Dayselector_Item *it;
339 Widget_Data *wd = elm_widget_data_get(obj);
340 if (!wd) return NULL;
341 if (item && !strcmp(item, "default")) return NULL;
343 EINA_LIST_FOREACH(wd->items, l, it)
345 snprintf(buf, sizeof(buf), "day%d", it->day);
346 if (!strncmp(buf, item, sizeof(buf))) return VIEW(it);
352 _content_unset_hook(Evas_Object *obj, const char *item)
354 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
356 Elm_Dayselector_Item *it;
357 Evas_Object *content;
360 Widget_Data *wd = elm_widget_data_get(obj);
361 if (!wd) return NULL;
362 if (item && !strcmp(item, "default")) return NULL;
364 EINA_LIST_FOREACH(wd->items, l, it)
366 snprintf(buf, sizeof(buf), "day%d", it->day);
367 if (!strncmp(buf, item, sizeof(buf)))
370 wd->items = eina_list_remove(wd->items, it);
371 elm_widget_sub_object_del(obj, VIEW(it));
372 evas_object_smart_callback_del(VIEW(it), "changed", _item_clicked_cb);
373 evas_object_event_callback_del(VIEW(it), EVAS_CALLBACK_DEL, _item_del_cb);
374 edje_object_part_unswallow(wd->base, VIEW(it));
375 loc = (ELM_DAYSELECTOR_MAX - wd->week_start + it->day) % ELM_DAYSELECTOR_MAX;
376 snprintf(buf, sizeof(buf), "day%d,default", loc);
377 edje_object_signal_emit(wd->base, buf, "elm");
386 _item_clicked_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
389 Elm_Dayselector_Item *it;
390 Widget_Data *wd = elm_widget_data_get(data);
393 EINA_LIST_FOREACH(wd->items, l, it)
395 if (obj != VIEW(it)) continue;
396 evas_object_smart_callback_call(data, SIG_CHANGED, (void *)it->day);
401 static Elm_Dayselector_Item *
402 _item_find(const Evas_Object *obj, Elm_Dayselector_Day day)
405 Elm_Dayselector_Item *it;
406 Widget_Data* wd = elm_widget_data_get(obj);
407 if (!wd) return NULL;
409 EINA_LIST_FOREACH(wd->items, l, it)
410 if (day == it->day) return it;
416 _items_style_set(Evas_Object *obj)
418 ELM_CHECK_WIDTYPE(obj, widtype);
420 Elm_Dayselector_Item *it;
421 unsigned int weekend_last;
422 Widget_Data* wd = elm_widget_data_get(obj);
425 weekend_last = wd->weekend_start + wd->weekend_len - 1;
426 if (weekend_last >= ELM_DAYSELECTOR_MAX)
427 weekend_last = weekend_last % ELM_DAYSELECTOR_MAX;
429 EINA_LIST_FOREACH(wd->items, l, it)
431 if (weekend_last >= wd->weekend_start)
433 if ((it->day >= wd->weekend_start) && (it->day <= weekend_last))
434 eina_stringshare_replace(&it->day_style,
435 ITEM_TYPE_WEEKEND_DEFAULT);
437 eina_stringshare_replace(&it->day_style,
438 ITEM_TYPE_WEEKDAY_DEFAULT);
442 if ((it->day >= wd->weekend_start) || (it->day <= weekend_last))
443 eina_stringshare_replace(&it->day_style,
444 ITEM_TYPE_WEEKEND_DEFAULT);
446 eina_stringshare_replace(&it->day_style,
447 ITEM_TYPE_WEEKDAY_DEFAULT);
453 _update_items(Evas_Object *obj)
456 Elm_Dayselector_Item *it;
457 unsigned int last_day;
459 Widget_Data* wd = elm_widget_data_get(obj);
462 last_day = wd->week_start + ELM_DAYSELECTOR_MAX - 1;
463 if (last_day >= ELM_DAYSELECTOR_MAX)
464 last_day = last_day % ELM_DAYSELECTOR_MAX;
466 rtl = elm_widget_mirrored_get(obj);
467 EINA_LIST_FOREACH(wd->items, l, it)
469 elm_object_signal_emit(VIEW(it), it->day_style, "");
470 if (it->day == wd->week_start)
472 if (rtl) elm_object_signal_emit(VIEW(it), ITEM_POS_RIGHT, "elm");
473 else elm_object_signal_emit(VIEW(it), ITEM_POS_LEFT, "elm");
475 else if (it->day == last_day)
477 if (rtl) elm_object_signal_emit(VIEW(it), ITEM_POS_LEFT, "elm");
478 else elm_object_signal_emit(VIEW(it), ITEM_POS_RIGHT, "elm");
481 elm_object_signal_emit(VIEW(it), ITEM_POS_MIDDLE, "elm");
486 _create_items(Evas_Object *obj)
488 Elm_Dayselector_Item *it;
489 struct tm time_daysel;
492 unsigned int idx, loc;
493 Widget_Data* wd = elm_widget_data_get(obj);
497 localtime_r(&t, &time_daysel);
499 for (idx = 0; idx < ELM_DAYSELECTOR_MAX; idx++)
501 it = elm_widget_item_new(obj, Elm_Dayselector_Item);
503 VIEW(it) = elm_check_add(wd->base);
504 elm_widget_sub_object_add(obj, VIEW(it));
505 wd->items = eina_list_append(wd->items, it);
506 elm_object_style_set(VIEW(it), "dayselector/default");
507 time_daysel.tm_wday = idx;
508 strftime(buf, sizeof(buf), "%a", &time_daysel);
509 elm_object_text_set(VIEW(it), buf);
511 loc = (ELM_DAYSELECTOR_MAX - wd->week_start + idx) % ELM_DAYSELECTOR_MAX;
512 snprintf(buf, sizeof(buf), "day%d", loc);
513 edje_object_part_swallow(wd->base, buf, VIEW(it));
514 snprintf(buf, sizeof(buf), "day%d,visible", idx);
515 edje_object_signal_emit(wd->base, buf, "elm");
516 evas_object_smart_callback_add(VIEW(it), "changed", _item_clicked_cb,
518 evas_object_event_callback_add(VIEW(it), EVAS_CALLBACK_DEL,
520 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKDAY_DEFAULT, "",
521 _signal_emit_cb, obj);
522 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKDAY_STYLE1, "",
523 _signal_emit_cb, obj);
524 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKEND_DEFAULT, "",
525 _signal_emit_cb, obj);
526 elm_object_signal_callback_add(VIEW(it), ITEM_TYPE_WEEKEND_STYLE1, "",
527 _signal_emit_cb, obj);
529 _items_style_set(obj);
534 elm_dayselector_add(Evas_Object *parent)
540 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
542 ELM_SET_WIDTYPE(widtype, "dayselector");
543 elm_widget_type_set(obj, widtype);
544 elm_widget_sub_object_add(parent, obj);
545 elm_widget_data_set(obj, wd);
546 elm_widget_del_hook_set(obj, _del_hook);
547 elm_widget_theme_hook_set(obj, _theme_hook);
548 elm_widget_disable_hook_set(obj, _disable_hook);
549 elm_widget_focus_next_hook_set(obj, _focus_next_hook);
550 elm_widget_translate_hook_set(obj, _translate_hook);
551 elm_widget_text_set_hook_set(obj, _item_text_set_hook);
552 elm_widget_text_get_hook_set(obj, _item_text_get_hook);
553 elm_widget_content_set_hook_set(obj, _content_set_hook);
554 elm_widget_content_get_hook_set(obj, _content_get_hook);
555 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
557 wd->base = edje_object_add(e);
558 _elm_theme_object_set(obj, wd->base, "dayselector", "base", "default");
559 elm_object_style_set(wd->base, "dayselector");
560 elm_widget_resize_object_set(obj, wd->base);
562 wd->week_start = _elm_config->week_start;
563 wd->weekend_start = _elm_config->weekend_start;
564 wd->weekend_len = _elm_config->weekend_len;
565 printf("%d %d %d\n", wd->week_start, wd->weekend_start, wd->weekend_len);
568 evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE,
569 _dayselector_resize, obj);
570 evas_object_smart_callbacks_descriptions_set(obj, _signals);
571 _mirrored_set(obj, elm_widget_mirrored_get(obj));
577 elm_dayselector_day_selected_set(Evas_Object *obj, Elm_Dayselector_Day day, Eina_Bool selected)
579 ELM_CHECK_WIDTYPE(obj, widtype);
580 Widget_Data* wd = elm_widget_data_get(obj);
583 elm_check_state_set(VIEW(_item_find(obj, day)), selected);
587 elm_dayselector_day_selected_get(const Evas_Object *obj, Elm_Dayselector_Day day)
589 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
590 Widget_Data* wd = elm_widget_data_get(obj);
591 if (!wd) return EINA_FALSE;
593 return elm_check_state_get(VIEW(_item_find(obj, day)));
597 elm_dayselector_week_start_set(Evas_Object *obj, Elm_Dayselector_Day day)
599 ELM_CHECK_WIDTYPE(obj, widtype);
601 Elm_Dayselector_Item *it;
604 Widget_Data* wd = elm_widget_data_get(obj);
607 wd->week_start = day;
608 EINA_LIST_FOREACH(wd->items, l, it)
610 loc = (ELM_DAYSELECTOR_MAX - wd->week_start + it->day) % ELM_DAYSELECTOR_MAX;
611 snprintf(buf, sizeof(buf), "day%d", loc);
612 edje_object_part_swallow(wd->base, buf, VIEW(it));
617 EAPI Elm_Dayselector_Day
618 elm_dayselector_week_start_get(const Evas_Object *obj)
620 ELM_CHECK_WIDTYPE(obj, widtype) ELM_DAYSELECTOR_MAX;
621 Widget_Data* wd = elm_widget_data_get(obj);
622 if (!wd) return ELM_DAYSELECTOR_MAX;
624 return wd->week_start;
628 elm_dayselector_weekend_start_set(Evas_Object *obj, Elm_Dayselector_Day day)
630 ELM_CHECK_WIDTYPE(obj, widtype);
631 Widget_Data* wd = elm_widget_data_get(obj);
634 wd->weekend_start = day;
635 _items_style_set(obj);
639 EAPI Elm_Dayselector_Day
640 elm_dayselector_weekend_start_get(const Evas_Object *obj)
642 ELM_CHECK_WIDTYPE(obj, widtype) ELM_DAYSELECTOR_MAX;
643 Widget_Data* wd = elm_widget_data_get(obj);
644 if (!wd) return ELM_DAYSELECTOR_MAX;
646 return wd->weekend_start;
650 elm_dayselector_weekend_length_set(Evas_Object *obj, unsigned int length)
652 ELM_CHECK_WIDTYPE(obj, widtype);
653 Widget_Data* wd = elm_widget_data_get(obj);
656 wd->weekend_len = length;
657 _items_style_set(obj);
662 elm_dayselector_weekend_length_get(const Evas_Object *obj)
664 ELM_CHECK_WIDTYPE(obj, widtype) 0;
665 Widget_Data* wd = elm_widget_data_get(obj);
668 return wd->weekend_len;