1 #include <Elementary.h>
4 /* TODO: ideally, the default theme would use map{} blocks on the TEXT
5 parts to implement their fading in/out propertly (as in the clock
7 /* TODO: if one ever wants to extend it to receiving generic widgets
8 as items, be my guest. in this case, remember to implement the
9 items tooltip infra. */
10 /* TODO: implement disabled mode -- disable_hook() and stuff. */
11 /* TODO: fix default theme image borders for looong strings as item
13 /* TODO: set text elipsis on labels if one enforces mininum size on
14 * the overall widget less the required for displaying it. */
15 /* TODO: find a way to, in the default theme, to detect we are
16 * bootstrapping (receiving the 1st message) and populate the downmost
17 * TEXT parts with the same text as the upmost, where appropriate. */
19 #define FLIP_FIRST_INTERVAL (0.85)
20 #define FLIP_MIN_INTERVAL (0.1)
21 #define MSG_FLIP_DOWN (1)
22 #define MSG_FLIP_UP (2)
23 #define MAX_LEN_DEFAULT (50)
25 #define DATA_GET eina_list_data_get
27 struct _Elm_Flipselector_Item
36 typedef struct _Widget_Data Widget_Data;
44 Eina_List *sentinel; /* item containing the largest label string */
48 double interval, first_interval;
51 static const char *widtype = NULL;
52 static void _del_hook(Evas_Object *obj);
53 static void _theme_hook(Evas_Object *obj);
54 static void _sizing_eval(Evas_Object *obj);
55 static void _update_view(Evas_Object *obj);
56 static void _callbacks_set(Evas_Object *obj);
57 static void _flip_up(Widget_Data *wd);
58 static void _flip_down(Widget_Data *wd);
60 static const char SIG_SELECTED[] = "selected";
61 static const char SIG_UNDERFLOWED[] = "underflowed";
62 static const char SIG_OVERFLOWED[] = "overflowed";
63 static const Evas_Smart_Cb_Description _signals[] = {
65 {SIG_UNDERFLOWED, ""},
70 #define ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(it, ...) \
71 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, __VA_ARGS__); \
74 ERR(""#it" has been DELETED.\n"); \
78 static Elm_Flipselector_Item *
79 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
82 Elm_Flipselector_Item *it;
83 Widget_Data *wd = elm_widget_data_get(obj);
85 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
90 if (len > wd->max_len)
93 it->label = eina_stringshare_add_length(label, len);
97 /* TODO: no view here, but if one desires general contents in the
103 _item_free(Elm_Flipselector_Item *it)
105 eina_stringshare_del(it->label);
106 elm_widget_item_del(it);
110 _del_hook(Evas_Object *obj)
112 Elm_Flipselector_Item *item;
114 Widget_Data *wd = elm_widget_data_get(obj);
119 ERR("flipselector deleted while walking.\n");
121 EINA_LIST_FREE(wd->items, item)
124 if (wd->spin) ecore_timer_del(wd->spin);
129 _theme_hook(Evas_Object *obj)
134 wd = elm_widget_data_get(obj);
138 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
139 elm_widget_style_get(obj));
140 edje_object_scale_set(wd->base,
141 elm_widget_scale_get(obj) * _elm_config->scale);
143 max_len = edje_object_data_get(wd->base, "max_len");
145 wd->max_len = MAX_LEN_DEFAULT;
148 wd->max_len = atoi(max_len);
150 wd->max_len = MAX_LEN_DEFAULT;
158 _sentinel_eval(Widget_Data *wd)
160 Elm_Flipselector_Item *it;
169 wd->sentinel = wd->items;
171 EINA_LIST_FOREACH(wd->items, l, it)
173 if (strlen(elm_flipselector_item_label_get(it)) >
174 strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel))))
179 /* TODO: create a flag to avoid looping here all times */
181 _flipselector_process_deletions(Widget_Data *wd)
183 Elm_Flipselector_Item *it;
185 Eina_Bool skip = EINA_TRUE;
186 Eina_Bool sentinel_eval = EINA_FALSE;
188 wd->walking++; /* avoid nested deletions */
190 EINA_LIST_FOREACH(wd->items, l, it)
195 if (wd->current == l)
197 if (wd->current == wd->sentinel)
198 sentinel_eval = EINA_TRUE;
200 wd->current = eina_list_prev(wd->current);
202 wd->items = eina_list_remove(wd->items, it);
205 wd->current = wd->items;
210 if (eina_list_count(wd->items) <= 1)
211 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
213 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
217 _update_view(wd->self);
226 _flipselector_walk(Widget_Data *wd)
230 ERR("walking was negative. fixed!\n");
237 _flipselector_unwalk(Widget_Data *wd)
242 ERR("walking became negative. fixed!\n");
249 _flipselector_process_deletions(wd);
253 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
255 Evas_Event_Key_Down *ev;
257 Eina_Bool is_up = EINA_TRUE;
259 if (type != EVAS_CALLBACK_KEY_DOWN)
262 wd = elm_widget_data_get(obj);
267 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
270 if (elm_widget_disabled_get(obj))
273 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
275 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
278 if (wd->spin) ecore_timer_del(wd->spin);
280 /* TODO: if direction setting via API is not coming in, replace
281 these calls by flip_{next,prev} */
282 _flipselector_walk(wd);
287 _flipselector_unwalk(wd);
289 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
294 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
296 Widget_Data *wd = elm_widget_data_get(obj);
300 /* FIXME: no treatment of this signal so far */
301 if (elm_widget_focus_get(obj))
303 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
304 evas_object_focus_set(wd->base, EINA_TRUE);
308 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
309 evas_object_focus_set(wd->base, EINA_FALSE);
314 _sizing_eval(Evas_Object *obj)
317 const char *tmp = NULL;
318 Evas_Coord minw = -1, minh = -1, w, h;
320 wd = elm_widget_data_get(obj);
324 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
328 const char *label = \
329 elm_flipselector_item_label_get(DATA_GET(wd->sentinel));
331 tmp = edje_object_part_text_get(wd->base, "top");
332 edje_object_part_text_set(wd->base, "top", label);
335 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
336 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
337 evas_object_size_hint_min_get(obj, &w, &h);
340 edje_object_part_text_set(wd->base, "top", tmp);
342 if (w > minw) minw = w;
343 if (h > minh) minh = h;
345 evas_object_size_hint_min_set(obj, minw, minh);
349 _update_view(Evas_Object *obj)
353 Elm_Flipselector_Item *item;
355 wd = elm_widget_data_get(obj);
360 item = DATA_GET(wd->current);
364 edje_object_part_text_set(wd->base, "top", label ? label : "");
365 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
367 edje_object_message_signal_process(wd->base);
371 _changed(Widget_Data *wd)
373 Elm_Flipselector_Item *item;
375 item = DATA_GET(wd->current);
380 item->func((void *)item->base.data, WIDGET(item), item);
382 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
386 _send_msg(Widget_Data *wd, int flipside, char *label)
388 Edje_Message_String msg;
391 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
392 edje_object_message_signal_process(wd->base);
398 _flip_up(Widget_Data *wd)
400 Elm_Flipselector_Item *item;
405 if (wd->current == wd->items)
407 wd->current = eina_list_last(wd->items);
408 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
411 wd->current = eina_list_prev(wd->current);
413 item = DATA_GET(wd->current);
417 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
421 _signal_val_up(void *data)
423 Widget_Data *wd = elm_widget_data_get(data);
426 goto val_up_exit_on_error;
428 _flipselector_walk(wd);
430 if (wd->interval > FLIP_MIN_INTERVAL)
431 wd->interval = wd->interval / 1.05;
433 ecore_timer_interval_set(wd->spin, wd->interval);
437 _flipselector_unwalk(wd);
439 return ECORE_CALLBACK_RENEW;
441 val_up_exit_on_error:
442 return ECORE_CALLBACK_CANCEL;
446 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
448 Widget_Data *wd = elm_widget_data_get(data);
452 wd->interval = wd->first_interval;
455 ecore_timer_del(wd->spin);
456 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
458 _signal_val_up(data);
462 _flip_down(Widget_Data *wd)
464 Elm_Flipselector_Item *item;
469 wd->current = eina_list_next(wd->current);
472 wd->current = wd->items;
473 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
476 item = DATA_GET(wd->current);
480 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
484 _signal_val_down(void *data)
486 Widget_Data *wd = elm_widget_data_get(data);
489 goto val_down_exit_on_error;
491 _flipselector_walk(wd);
493 if (wd->interval > FLIP_MIN_INTERVAL)
494 wd->interval = wd->interval / 1.05;
495 ecore_timer_interval_set(wd->spin, wd->interval);
499 _flipselector_unwalk(wd);
501 return ECORE_CALLBACK_RENEW;
503 val_down_exit_on_error:
504 return ECORE_CALLBACK_CANCEL;
508 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
510 Widget_Data *wd = elm_widget_data_get(data);
514 wd->interval = wd->first_interval;
517 ecore_timer_del(wd->spin);
518 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
520 _signal_val_down(data);
524 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
526 Widget_Data *wd = elm_widget_data_get(data);
531 ecore_timer_del(wd->spin);
536 _callbacks_set(Evas_Object *obj)
538 Widget_Data *wd = elm_widget_data_get(obj);
540 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
541 "", _signal_val_up_start, obj);
542 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
543 "", _signal_val_change_stop, obj);
544 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
545 "", _signal_val_down_start, obj);
546 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
547 "", _signal_val_change_stop, obj);
551 elm_flipselector_add(Evas_Object *parent)
557 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
559 ELM_SET_WIDTYPE(widtype, "flipselector");
560 elm_widget_type_set(obj, "flipselector");
561 elm_widget_sub_object_add(parent, obj);
562 elm_widget_data_set(obj, wd);
565 elm_widget_del_hook_set(obj, _del_hook);
566 elm_widget_theme_hook_set(obj, _theme_hook);
567 /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
569 elm_widget_can_focus_set(obj, EINA_TRUE);
570 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
571 elm_widget_event_hook_set(obj, _event_hook);
573 wd->base = edje_object_add(e);
574 elm_widget_resize_object_set(obj, wd->base);
578 wd->first_interval = FLIP_FIRST_INTERVAL;
582 evas_object_smart_callbacks_descriptions_set(obj, _signals);
587 elm_flipselector_flip_next(Evas_Object *obj)
589 ELM_CHECK_WIDTYPE(obj, widtype);
591 Widget_Data *wd = elm_widget_data_get(obj);
595 if (wd->spin) ecore_timer_del(wd->spin);
597 _flipselector_walk(wd);
599 _flipselector_unwalk(wd);
603 elm_flipselector_flip_prev(Evas_Object *obj)
605 ELM_CHECK_WIDTYPE(obj, widtype);
607 Widget_Data *wd = elm_widget_data_get(obj);
611 if (wd->spin) ecore_timer_del(wd->spin);
613 _flipselector_walk(wd);
615 _flipselector_unwalk(wd);
618 EAPI Elm_Flipselector_Item *
619 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
621 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
623 Elm_Flipselector_Item *item;
626 wd = elm_widget_data_get(obj);
630 item = _item_new(obj, label, func, data);
634 wd->items = eina_list_append(wd->items, item);
636 wd->current = wd->items;
641 (strlen(elm_flipselector_item_label_get(item)) >
642 strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel)))))
644 wd->sentinel = eina_list_last(wd->items);
648 if (eina_list_count(wd->items) >= 2)
649 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
654 EAPI Elm_Flipselector_Item *
655 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
657 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
659 Elm_Flipselector_Item *item;
662 wd = elm_widget_data_get(obj);
666 item = _item_new(obj, label, func, data);
670 wd->items = eina_list_prepend(wd->items, item);
672 wd->current = wd->items;
677 (strlen(elm_flipselector_item_label_get(item)) >
678 strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel)))))
680 wd->sentinel = wd->items;
684 if (eina_list_count(wd->items) >= 2)
685 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
690 /* TODO: account for deleted items? */
691 EAPI const Eina_List *
692 elm_flipselector_items_get(const Evas_Object *obj)
694 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
696 Widget_Data *wd = elm_widget_data_get(obj);
703 EAPI Elm_Flipselector_Item *
704 elm_flipselector_first_item_get(const Evas_Object *obj)
706 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
708 Elm_Flipselector_Item *it;
712 wd = elm_widget_data_get(obj);
713 if (!wd || !wd->items)
716 EINA_LIST_FOREACH(wd->items, l, it)
727 EAPI Elm_Flipselector_Item *
728 elm_flipselector_last_item_get(const Evas_Object *obj)
730 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
732 Elm_Flipselector_Item *it;
736 wd = elm_widget_data_get(obj);
737 if (!wd || !wd->items)
740 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
751 EAPI Elm_Flipselector_Item *
752 elm_flipselector_selected_item_get(const Evas_Object *obj)
754 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
756 Widget_Data *wd = elm_widget_data_get(obj);
757 if (!wd || !wd->current)
760 return DATA_GET(wd->current);
764 elm_flipselector_item_selected_set(Elm_Flipselector_Item *item, Eina_Bool selected)
766 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
768 Elm_Flipselector_Item *_item, *cur;
769 int flipside = MSG_FLIP_UP;
773 wd = elm_widget_data_get(WIDGET(item));
777 cur = DATA_GET(wd->current);
778 if ((selected) && (cur == item))
781 _flipselector_walk(wd);
783 if ((!selected) && (cur == item))
785 EINA_LIST_FOREACH(wd->items, l, _item)
790 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
794 _flipselector_unwalk(wd);
798 EINA_LIST_FOREACH(wd->items, l, _item)
801 flipside = MSG_FLIP_DOWN;
806 _send_msg(wd, flipside, (char *)item->label);
811 _flipselector_unwalk(wd);
815 elm_flipselector_item_selected_get(const Elm_Flipselector_Item *item)
817 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, EINA_FALSE);
820 wd = elm_widget_data_get(WIDGET(item));
821 if (!wd) return EINA_FALSE;
822 return (eina_list_data_get(wd->current) == item);
826 elm_flipselector_item_del(Elm_Flipselector_Item *item)
828 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
832 wd = elm_widget_data_get(WIDGET(item));
838 item->deleted = EINA_TRUE;
842 _flipselector_walk(wd);
844 wd->items = eina_list_remove(wd->items, item);
848 _flipselector_unwalk(wd);
852 elm_flipselector_item_label_get(const Elm_Flipselector_Item *item)
854 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
856 Elm_Flipselector_Item *_item;
860 wd = elm_widget_data_get(WIDGET(item));
861 if ((!wd) || (!wd->items))
864 EINA_LIST_FOREACH(wd->items, l, _item)
872 elm_flipselector_item_label_set(Elm_Flipselector_Item *item, const char *label)
874 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
879 if ((!item) || (!label))
882 wd = elm_widget_data_get(WIDGET(item));
883 if ((!wd) || (!wd->items))
886 l = eina_list_data_find_list(wd->items, item);
890 eina_stringshare_del(item->label);
891 item->label = eina_stringshare_add_length(label, wd->max_len);
894 strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel))))
897 if (wd->current == l)
899 _update_view(WIDGET(item));
900 _sizing_eval(wd->self);
906 EAPI Elm_Flipselector_Item *
907 elm_flipselector_item_prev_get(Elm_Flipselector_Item *item)
909 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
911 Elm_Flipselector_Item *_item;
915 wd = elm_widget_data_get(WIDGET(item));
916 if ((!wd) || (!wd->items))
919 EINA_LIST_FOREACH(wd->items, l, _item)
922 l = eina_list_prev(l);
931 EAPI Elm_Flipselector_Item *
932 elm_flipselector_item_next_get(Elm_Flipselector_Item *item)
934 ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
936 Elm_Flipselector_Item *_item;
940 wd = elm_widget_data_get(WIDGET(item));
941 if ((!wd) || (!wd->items))
944 EINA_LIST_FOREACH(wd->items, l, _item)
947 l = eina_list_next(l);
957 elm_flipselector_interval_set(Evas_Object *obj, double interval)
959 ELM_CHECK_WIDTYPE(obj, widtype);
961 Widget_Data *wd = elm_widget_data_get(obj);
965 wd->first_interval = interval;
969 elm_flipselector_interval_get(const Evas_Object *obj)
971 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
973 Widget_Data *wd = elm_widget_data_get(obj);
977 return wd->first_interval;