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;
37 typedef struct _Elm_Flipselector_Item Elm_Flipselector_Item;
45 Eina_List *sentinel; /* item containing the largest label string */
49 double interval, first_interval;
52 static const char *widtype = NULL;
53 static void _del_hook(Evas_Object *obj);
54 static void _theme_hook(Evas_Object *obj);
55 static void _sizing_eval(Evas_Object *obj);
56 static void _update_view(Evas_Object *obj);
57 static void _callbacks_set(Evas_Object *obj);
58 static void _flip_up(Widget_Data *wd);
59 static void _flip_down(Widget_Data *wd);
61 static const char SIG_SELECTED[] = "selected";
62 static const char SIG_UNDERFLOWED[] = "underflowed";
63 static const char SIG_OVERFLOWED[] = "overflowed";
64 static const Evas_Smart_Cb_Description _signals[] = {
66 {SIG_UNDERFLOWED, ""},
72 _item_text_set_hook(Elm_Object_Item *it,
76 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
80 Elm_Flipselector_Item *item;
84 if (part && strcmp(part ,"default")) return;
86 item = (Elm_Flipselector_Item *) it;
87 wd = elm_widget_data_get(WIDGET(item));
88 if ((!wd) || (!wd->items)) return;
90 l = eina_list_data_find_list(wd->items, item);
93 eina_stringshare_del(item->label);
94 item->label = eina_stringshare_add_length(label, wd->max_len);
96 if (strlen(label) > strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
101 _update_view(WIDGET(item));
102 _sizing_eval(wd->self);
107 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
109 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
111 Elm_Flipselector_Item *item, *_item;
115 if (part && strcmp(part ,"default")) return NULL;
117 item = (Elm_Flipselector_Item *) it;
118 wd = elm_widget_data_get(WIDGET(item));
119 if ((!wd) || (!wd->items)) return NULL;
121 EINA_LIST_FOREACH(wd->items, l, _item)
122 if (_item == item) return item->label;
127 _item_signal_emit_hook(Elm_Object_Item *it,
128 const char *emission,
131 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
132 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
133 edje_object_signal_emit(VIEW(item), emission, source);
136 static Elm_Flipselector_Item *
137 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
140 Elm_Flipselector_Item *it;
141 Widget_Data *wd = elm_widget_data_get(obj);
143 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
144 if (!it) return NULL;
146 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
147 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
148 elm_widget_item_signal_emit_hook_set(it, _item_signal_emit_hook);
151 if (len > wd->max_len)
154 it->label = eina_stringshare_add_length(label, len);
156 it->base.data = data;
158 /* TODO: no view here, but if one desires general contents in the
164 _item_free(Elm_Flipselector_Item *it)
166 eina_stringshare_del(it->label);
167 elm_widget_item_del(it);
171 _del_hook(Evas_Object *obj)
173 Elm_Flipselector_Item *item;
175 Widget_Data *wd = elm_widget_data_get(obj);
178 if (wd->walking) ERR("flipselector deleted while walking.\n");
180 EINA_LIST_FREE(wd->items, item)
183 if (wd->spin) ecore_timer_del(wd->spin);
188 _theme_hook(Evas_Object *obj)
193 wd = elm_widget_data_get(obj);
196 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
197 elm_widget_style_get(obj));
198 edje_object_scale_set(wd->base,
199 elm_widget_scale_get(obj) * _elm_config->scale);
201 max_len = edje_object_data_get(wd->base, "max_len");
203 wd->max_len = MAX_LEN_DEFAULT;
206 wd->max_len = atoi(max_len);
208 wd->max_len = MAX_LEN_DEFAULT;
216 _sentinel_eval(Widget_Data *wd)
218 Elm_Flipselector_Item *it;
227 wd->sentinel = wd->items;
229 EINA_LIST_FOREACH(wd->items, l, it)
231 if (strlen(elm_object_item_text_get((Elm_Object_Item *) it)) >
232 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
237 /* TODO: create a flag to avoid looping here all times */
239 _flipselector_process_deletions(Widget_Data *wd)
241 Elm_Flipselector_Item *it;
243 Eina_Bool skip = EINA_TRUE;
244 Eina_Bool sentinel_eval = EINA_FALSE;
246 wd->walking++; /* avoid nested deletions */
248 EINA_LIST_FOREACH(wd->items, l, it)
250 if (!it->deleted) continue;
252 if (wd->current == l)
254 if (wd->current == wd->sentinel)
255 sentinel_eval = EINA_TRUE;
256 wd->current = eina_list_prev(wd->current);
258 wd->items = eina_list_remove(wd->items, it);
261 wd->current = wd->items;
266 if (eina_list_count(wd->items) <= 1)
267 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
269 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
273 _update_view(wd->self);
282 _flipselector_walk(Widget_Data *wd)
286 ERR("walking was negative. fixed!\n");
293 _flipselector_unwalk(Widget_Data *wd)
298 ERR("walking became negative. fixed!\n");
301 if (wd->walking) return;
303 _flipselector_process_deletions(wd);
307 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
309 Evas_Event_Key_Down *ev;
311 Eina_Bool is_up = EINA_TRUE;
313 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
315 wd = elm_widget_data_get(obj);
316 if (!wd) return EINA_FALSE;
319 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
321 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
323 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
325 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
328 if (wd->spin) ecore_timer_del(wd->spin);
330 /* TODO: if direction setting via API is not coming in, replace
331 these calls by flip_{next,prev} */
332 _flipselector_walk(wd);
333 if (is_up) _flip_up(wd);
335 _flipselector_unwalk(wd);
337 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
342 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
344 Widget_Data *wd = elm_widget_data_get(obj);
347 /* FIXME: no treatment of this signal so far */
348 if (elm_widget_focus_get(obj))
350 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
351 evas_object_focus_set(wd->base, EINA_TRUE);
355 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
356 evas_object_focus_set(wd->base, EINA_FALSE);
361 _sizing_eval(Evas_Object *obj)
364 const char *tmp = NULL;
365 Evas_Coord minw = -1, minh = -1, w, h;
367 wd = elm_widget_data_get(obj);
370 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
374 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
375 tmp = edje_object_part_text_get(wd->base, "top");
376 edje_object_part_text_set(wd->base, "top", label);
379 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
380 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
381 evas_object_size_hint_min_get(obj, &w, &h);
383 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
385 if (w > minw) minw = w;
386 if (h > minh) minh = h;
388 evas_object_size_hint_min_set(obj, minw, minh);
392 _update_view(Evas_Object *obj)
396 Elm_Flipselector_Item *item;
398 wd = elm_widget_data_get(obj);
402 item = DATA_GET(wd->current);
403 if (item) label = item->label;
405 edje_object_part_text_set(wd->base, "top", label ? label : "");
406 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
407 edje_object_message_signal_process(wd->base);
411 _changed(Widget_Data *wd)
413 Elm_Flipselector_Item *item;
415 item = DATA_GET(wd->current);
419 item->func((void *)item->base.data, WIDGET(item), item);
421 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
425 _send_msg(Widget_Data *wd, int flipside, char *label)
427 Edje_Message_String msg;
430 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
431 edje_object_message_signal_process(wd->base);
437 _flip_up(Widget_Data *wd)
439 Elm_Flipselector_Item *item;
441 if (!wd->current) return;
443 if (wd->current == wd->items)
445 wd->current = eina_list_last(wd->items);
446 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
449 wd->current = eina_list_prev(wd->current);
451 item = DATA_GET(wd->current);
454 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
458 _signal_val_up(void *data)
460 Widget_Data *wd = elm_widget_data_get(data);
462 if (!wd) goto val_up_exit_on_error;
464 _flipselector_walk(wd);
466 if (wd->interval > FLIP_MIN_INTERVAL)
467 wd->interval = wd->interval / 1.05;
469 ecore_timer_interval_set(wd->spin, wd->interval);
473 _flipselector_unwalk(wd);
475 return ECORE_CALLBACK_RENEW;
477 val_up_exit_on_error:
478 return ECORE_CALLBACK_CANCEL;
482 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
484 Widget_Data *wd = elm_widget_data_get(data);
487 wd->interval = wd->first_interval;
489 if (wd->spin) ecore_timer_del(wd->spin);
490 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
492 _signal_val_up(data);
496 _flip_down(Widget_Data *wd)
498 Elm_Flipselector_Item *item;
500 if (!wd->current) return;
502 wd->current = eina_list_next(wd->current);
505 wd->current = wd->items;
506 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
509 item = DATA_GET(wd->current);
512 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
516 _signal_val_down(void *data)
518 Widget_Data *wd = elm_widget_data_get(data);
520 if (!wd) goto val_down_exit_on_error;
522 _flipselector_walk(wd);
524 if (wd->interval > FLIP_MIN_INTERVAL)
525 wd->interval = wd->interval / 1.05;
526 ecore_timer_interval_set(wd->spin, wd->interval);
530 _flipselector_unwalk(wd);
532 return ECORE_CALLBACK_RENEW;
534 val_down_exit_on_error:
535 return ECORE_CALLBACK_CANCEL;
539 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
541 Widget_Data *wd = elm_widget_data_get(data);
544 wd->interval = wd->first_interval;
546 if (wd->spin) ecore_timer_del(wd->spin);
547 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
549 _signal_val_down(data);
553 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
555 Widget_Data *wd = elm_widget_data_get(data);
558 if (wd->spin) ecore_timer_del(wd->spin);
563 _callbacks_set(Evas_Object *obj)
565 Widget_Data *wd = elm_widget_data_get(obj);
567 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
568 "", _signal_val_up_start, obj);
569 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
570 "", _signal_val_change_stop, obj);
571 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
572 "", _signal_val_down_start, obj);
573 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
574 "", _signal_val_change_stop, obj);
578 elm_flipselector_add(Evas_Object *parent)
584 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
586 ELM_SET_WIDTYPE(widtype, "flipselector");
587 elm_widget_type_set(obj, "flipselector");
588 elm_widget_sub_object_add(parent, obj);
589 elm_widget_data_set(obj, wd);
592 elm_widget_del_hook_set(obj, _del_hook);
593 elm_widget_theme_hook_set(obj, _theme_hook);
594 /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
596 elm_widget_can_focus_set(obj, EINA_TRUE);
597 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
598 elm_widget_event_hook_set(obj, _event_hook);
600 wd->base = edje_object_add(e);
601 elm_widget_resize_object_set(obj, wd->base);
605 wd->first_interval = FLIP_FIRST_INTERVAL;
609 evas_object_smart_callbacks_descriptions_set(obj, _signals);
614 elm_flipselector_flip_next(Evas_Object *obj)
616 ELM_CHECK_WIDTYPE(obj, widtype);
618 Widget_Data *wd = elm_widget_data_get(obj);
621 if (wd->spin) ecore_timer_del(wd->spin);
623 _flipselector_walk(wd);
625 _flipselector_unwalk(wd);
629 elm_flipselector_flip_prev(Evas_Object *obj)
631 ELM_CHECK_WIDTYPE(obj, widtype);
633 Widget_Data *wd = elm_widget_data_get(obj);
636 if (wd->spin) ecore_timer_del(wd->spin);
638 _flipselector_walk(wd);
640 _flipselector_unwalk(wd);
643 EAPI Elm_Object_Item *
644 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
646 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
648 Elm_Flipselector_Item *item;
651 wd = elm_widget_data_get(obj);
652 if (!wd) return NULL;
654 item = _item_new(obj, label, func, data);
655 if (!item) return NULL;
657 wd->items = eina_list_append(wd->items, item);
660 wd->current = wd->items;
665 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
666 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
668 wd->sentinel = eina_list_last(wd->items);
672 if (eina_list_count(wd->items) >= 2)
673 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
675 return (Elm_Object_Item *) item;
678 EAPI Elm_Object_Item *
679 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
681 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
683 Elm_Flipselector_Item *item;
686 wd = elm_widget_data_get(obj);
687 if (!wd) return NULL;
689 item = _item_new(obj, label, func, data);
690 if (!item) return NULL;
692 wd->items = eina_list_prepend(wd->items, item);
695 wd->current = wd->items;
700 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
701 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
703 wd->sentinel = wd->items;
707 if (eina_list_count(wd->items) >= 2)
708 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
710 return (Elm_Object_Item *) item;
713 /* TODO: account for deleted items? */
714 EAPI const Eina_List *
715 elm_flipselector_items_get(const Evas_Object *obj)
717 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
719 Widget_Data *wd = elm_widget_data_get(obj);
720 if (!wd) return NULL;
724 EAPI Elm_Object_Item *
725 elm_flipselector_first_item_get(const Evas_Object *obj)
727 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
729 Elm_Flipselector_Item *it;
733 wd = elm_widget_data_get(obj);
734 if (!wd || !wd->items) return NULL;
736 EINA_LIST_FOREACH(wd->items, l, it)
738 if (it->deleted) continue;
739 return (Elm_Object_Item *) it;
744 EAPI Elm_Object_Item *
745 elm_flipselector_last_item_get(const Evas_Object *obj)
747 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
749 Elm_Flipselector_Item *it;
753 wd = elm_widget_data_get(obj);
754 if (!wd || !wd->items) return NULL;
756 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
758 if (it->deleted) continue;
759 return (Elm_Object_Item *) it;
764 EAPI Elm_Object_Item *
765 elm_flipselector_selected_item_get(const Evas_Object *obj)
767 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
769 Widget_Data *wd = elm_widget_data_get(obj);
770 if (!wd || !wd->current) return NULL;
771 return DATA_GET(wd->current);
775 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
777 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
779 Elm_Flipselector_Item *item, *_item, *cur;
780 int flipside = MSG_FLIP_UP;
784 item = (Elm_Flipselector_Item *) it;
785 wd = elm_widget_data_get(WIDGET(item));
788 cur = DATA_GET(wd->current);
789 if ((selected) && (cur == item)) return;
791 _flipselector_walk(wd);
793 if ((!selected) && (cur == item))
795 EINA_LIST_FOREACH(wd->items, l, _item)
800 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
804 _flipselector_unwalk(wd);
808 EINA_LIST_FOREACH(wd->items, l, _item)
810 if (_item == cur) flipside = MSG_FLIP_DOWN;
815 _send_msg(wd, flipside, (char *)item->label);
820 _flipselector_unwalk(wd);
824 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
826 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
828 Elm_Flipselector_Item *item;
830 item = (Elm_Flipselector_Item *) it;
831 wd = elm_widget_data_get(WIDGET(item));
832 if (!wd) return EINA_FALSE;
833 return (eina_list_data_get(wd->current) == item);
837 elm_flipselector_item_del(Elm_Object_Item *it)
839 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
841 Elm_Flipselector_Item *item, *item2;
844 item = (Elm_Flipselector_Item *) it;
845 wd = elm_widget_data_get(WIDGET(item));
850 item->deleted = EINA_TRUE;
854 _flipselector_walk(wd);
856 wd->items = eina_list_remove(wd->items, item);
859 _flipselector_unwalk(wd);
863 elm_flipselector_item_label_get(const Elm_Object_Item *it)
865 return _item_text_get_hook(it, NULL);
869 elm_flipselector_item_label_set(Elm_Object_Item *it, const char *label)
871 _item_text_set_hook(it, NULL, label);
874 EAPI Elm_Object_Item *
875 elm_flipselector_item_prev_get(Elm_Object_Item *it)
877 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
879 Elm_Flipselector_Item *item, *_item;
883 item = (Elm_Flipselector_Item *) it;
884 wd = elm_widget_data_get(WIDGET(item));
885 if ((!wd) || (!wd->items)) return NULL;
887 EINA_LIST_FOREACH(wd->items, l, _item)
890 l = eina_list_prev(l);
897 EAPI Elm_Object_Item *
898 elm_flipselector_item_next_get(Elm_Object_Item *it)
900 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
902 Elm_Flipselector_Item *item, *_item;
906 item = (Elm_Flipselector_Item *) it;
907 wd = elm_widget_data_get(WIDGET(item));
908 if ((!wd) || (!wd->items)) return NULL;
910 EINA_LIST_FOREACH(wd->items, l, _item)
913 l = eina_list_next(l);
921 elm_flipselector_interval_set(Evas_Object *obj, double interval)
923 ELM_CHECK_WIDTYPE(obj, widtype);
925 Widget_Data *wd = elm_widget_data_get(obj);
927 wd->first_interval = interval;
931 elm_flipselector_interval_get(const Evas_Object *obj)
933 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
935 Widget_Data *wd = elm_widget_data_get(obj);
937 return wd->first_interval;