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);
60 static Eina_Bool _item_del_pre_hook(Elm_Object_Item *it);
62 static const char SIG_SELECTED[] = "selected";
63 static const char SIG_UNDERFLOWED[] = "underflowed";
64 static const char SIG_OVERFLOWED[] = "overflowed";
65 static const Evas_Smart_Cb_Description _signals[] = {
67 {SIG_UNDERFLOWED, ""},
73 _item_text_set_hook(Elm_Object_Item *it,
77 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
81 Elm_Flipselector_Item *item;
85 if (part && strcmp(part ,"default")) return;
87 item = (Elm_Flipselector_Item *) it;
88 wd = elm_widget_data_get(WIDGET(item));
89 if ((!wd) || (!wd->items)) return;
91 l = eina_list_data_find_list(wd->items, item);
94 eina_stringshare_del(item->label);
95 item->label = eina_stringshare_add_length(label, wd->max_len);
97 if (strlen(label) > strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
100 if (wd->current == l)
102 _update_view(WIDGET(item));
103 _sizing_eval(wd->self);
108 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
110 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
112 Elm_Flipselector_Item *item, *_item;
116 if (part && strcmp(part ,"default")) return NULL;
118 item = (Elm_Flipselector_Item *) it;
119 wd = elm_widget_data_get(WIDGET(item));
120 if ((!wd) || (!wd->items)) return NULL;
122 EINA_LIST_FOREACH(wd->items, l, _item)
123 if (_item == item) return item->label;
128 _item_signal_emit_hook(Elm_Object_Item *it,
129 const char *emission,
132 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
133 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
134 edje_object_signal_emit(VIEW(item), emission, source);
137 static Elm_Flipselector_Item *
138 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
141 Elm_Flipselector_Item *it;
142 Widget_Data *wd = elm_widget_data_get(obj);
144 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
145 if (!it) return NULL;
147 elm_widget_item_del_pre_hook_set(it, _item_del_pre_hook);
148 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
149 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
150 elm_widget_item_signal_emit_hook_set(it, _item_signal_emit_hook);
153 if (len > wd->max_len)
156 it->label = eina_stringshare_add_length(label, len);
158 it->base.data = data;
160 /* TODO: no view here, but if one desires general contents in the
166 _item_free(Elm_Flipselector_Item *it)
168 eina_stringshare_del(it->label);
169 elm_widget_item_free(it);
173 _del_hook(Evas_Object *obj)
175 Elm_Flipselector_Item *item;
177 Widget_Data *wd = elm_widget_data_get(obj);
180 if (wd->walking) ERR("flipselector deleted while walking.\n");
182 EINA_LIST_FREE(wd->items, item)
185 if (wd->spin) ecore_timer_del(wd->spin);
190 _theme_hook(Evas_Object *obj)
195 wd = elm_widget_data_get(obj);
198 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
199 elm_widget_style_get(obj));
200 edje_object_scale_set(wd->base,
201 elm_widget_scale_get(obj) * _elm_config->scale);
203 max_len = edje_object_data_get(wd->base, "max_len");
205 wd->max_len = MAX_LEN_DEFAULT;
208 wd->max_len = atoi(max_len);
210 wd->max_len = MAX_LEN_DEFAULT;
218 _sentinel_eval(Widget_Data *wd)
220 Elm_Flipselector_Item *it;
229 wd->sentinel = wd->items;
231 EINA_LIST_FOREACH(wd->items, l, it)
233 if (strlen(elm_object_item_text_get((Elm_Object_Item *) it)) >
234 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
239 /* TODO: create a flag to avoid looping here all times */
241 _flipselector_process_deletions(Widget_Data *wd)
243 Elm_Flipselector_Item *it;
245 Eina_Bool skip = EINA_TRUE;
246 Eina_Bool sentinel_eval = EINA_FALSE;
248 wd->walking++; /* avoid nested deletions */
250 EINA_LIST_FOREACH(wd->items, l, it)
252 if (!it->deleted) continue;
254 if (wd->current == l)
256 if (wd->current == wd->sentinel)
257 sentinel_eval = EINA_TRUE;
258 wd->current = eina_list_prev(wd->current);
260 wd->items = eina_list_remove(wd->items, it);
263 wd->current = wd->items;
268 if (eina_list_count(wd->items) <= 1)
269 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
271 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
275 _update_view(wd->self);
284 _flipselector_walk(Widget_Data *wd)
288 ERR("walking was negative. fixed!\n");
295 _flipselector_unwalk(Widget_Data *wd)
300 ERR("walking became negative. fixed!\n");
303 if (wd->walking) return;
305 _flipselector_process_deletions(wd);
309 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
311 Evas_Event_Key_Down *ev;
313 Eina_Bool is_up = EINA_TRUE;
315 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
317 wd = elm_widget_data_get(obj);
318 if (!wd) return EINA_FALSE;
321 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
323 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
325 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
327 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
330 if (wd->spin) ecore_timer_del(wd->spin);
332 /* TODO: if direction setting via API is not coming in, replace
333 these calls by flip_{next,prev} */
334 _flipselector_walk(wd);
335 if (is_up) _flip_up(wd);
337 _flipselector_unwalk(wd);
339 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
344 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
346 Widget_Data *wd = elm_widget_data_get(obj);
349 /* FIXME: no treatment of this signal so far */
350 if (elm_widget_focus_get(obj))
352 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
353 evas_object_focus_set(wd->base, EINA_TRUE);
357 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
358 evas_object_focus_set(wd->base, EINA_FALSE);
363 _sizing_eval(Evas_Object *obj)
366 const char *tmp = NULL;
367 Evas_Coord minw = -1, minh = -1, w, h;
369 wd = elm_widget_data_get(obj);
372 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
376 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
377 tmp = edje_object_part_text_get(wd->base, "top");
378 edje_object_part_text_set(wd->base, "top", label);
381 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
382 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
383 evas_object_size_hint_min_get(obj, &w, &h);
385 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
387 if (w > minw) minw = w;
388 if (h > minh) minh = h;
390 evas_object_size_hint_min_set(obj, minw, minh);
394 _update_view(Evas_Object *obj)
398 Elm_Flipselector_Item *item;
400 wd = elm_widget_data_get(obj);
404 item = DATA_GET(wd->current);
405 if (item) label = item->label;
407 edje_object_part_text_set(wd->base, "top", label ? label : "");
408 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
409 edje_object_message_signal_process(wd->base);
413 _changed(Widget_Data *wd)
415 Elm_Flipselector_Item *item;
417 item = DATA_GET(wd->current);
421 item->func((void *)item->base.data, WIDGET(item), item);
423 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
427 _send_msg(Widget_Data *wd, int flipside, char *label)
429 Edje_Message_String msg;
432 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
433 edje_object_message_signal_process(wd->base);
439 _flip_up(Widget_Data *wd)
441 Elm_Flipselector_Item *item;
443 if (!wd->current) return;
445 if (wd->current == wd->items)
447 wd->current = eina_list_last(wd->items);
448 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
451 wd->current = eina_list_prev(wd->current);
453 item = DATA_GET(wd->current);
456 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
460 _signal_val_up(void *data)
462 Widget_Data *wd = elm_widget_data_get(data);
464 if (!wd) goto val_up_exit_on_error;
466 _flipselector_walk(wd);
468 if (wd->interval > FLIP_MIN_INTERVAL)
469 wd->interval = wd->interval / 1.05;
471 ecore_timer_interval_set(wd->spin, wd->interval);
475 _flipselector_unwalk(wd);
477 return ECORE_CALLBACK_RENEW;
479 val_up_exit_on_error:
480 return ECORE_CALLBACK_CANCEL;
484 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
486 Widget_Data *wd = elm_widget_data_get(data);
489 wd->interval = wd->first_interval;
491 if (wd->spin) ecore_timer_del(wd->spin);
492 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
494 _signal_val_up(data);
498 _flip_down(Widget_Data *wd)
500 Elm_Flipselector_Item *item;
502 if (!wd->current) return;
504 wd->current = eina_list_next(wd->current);
507 wd->current = wd->items;
508 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
511 item = DATA_GET(wd->current);
514 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
518 _signal_val_down(void *data)
520 Widget_Data *wd = elm_widget_data_get(data);
522 if (!wd) goto val_down_exit_on_error;
524 _flipselector_walk(wd);
526 if (wd->interval > FLIP_MIN_INTERVAL)
527 wd->interval = wd->interval / 1.05;
528 ecore_timer_interval_set(wd->spin, wd->interval);
532 _flipselector_unwalk(wd);
534 return ECORE_CALLBACK_RENEW;
536 val_down_exit_on_error:
537 return ECORE_CALLBACK_CANCEL;
541 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
543 Widget_Data *wd = elm_widget_data_get(data);
546 wd->interval = wd->first_interval;
548 if (wd->spin) ecore_timer_del(wd->spin);
549 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
551 _signal_val_down(data);
555 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
557 Widget_Data *wd = elm_widget_data_get(data);
560 if (wd->spin) ecore_timer_del(wd->spin);
565 _callbacks_set(Evas_Object *obj)
567 Widget_Data *wd = elm_widget_data_get(obj);
569 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
570 "", _signal_val_up_start, obj);
571 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
572 "", _signal_val_change_stop, obj);
573 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
574 "", _signal_val_down_start, obj);
575 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
576 "", _signal_val_change_stop, obj);
580 _item_del_pre_hook(Elm_Object_Item *it)
582 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
584 Elm_Flipselector_Item *item, *item2;
587 item = (Elm_Flipselector_Item *) it;
588 wd = elm_widget_data_get(WIDGET(item));
589 if (!wd) return EINA_FALSE;
593 item->deleted = EINA_TRUE;
597 _flipselector_walk(wd);
599 EINA_LIST_FOREACH(wd->items, l, item2)
603 wd->items = eina_list_remove_list(wd->items, l);
604 if (wd->current == l)
606 wd->current = l->prev;
607 if (!wd->current) wd->current = l->next;
610 item2 = wd->current->data;
611 _send_msg(wd, MSG_FLIP_DOWN, (char *)item2->label);
614 _send_msg(wd, MSG_FLIP_DOWN, "");
619 eina_stringshare_del(item->label);
621 _flipselector_unwalk(wd);
627 elm_flipselector_add(Evas_Object *parent)
633 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
635 ELM_SET_WIDTYPE(widtype, "flipselector");
636 elm_widget_type_set(obj, "flipselector");
637 elm_widget_sub_object_add(parent, obj);
638 elm_widget_data_set(obj, wd);
641 elm_widget_del_hook_set(obj, _del_hook);
642 elm_widget_theme_hook_set(obj, _theme_hook);
643 /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
645 elm_widget_can_focus_set(obj, EINA_TRUE);
646 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
647 elm_widget_event_hook_set(obj, _event_hook);
649 wd->base = edje_object_add(e);
650 elm_widget_resize_object_set(obj, wd->base);
654 wd->first_interval = FLIP_FIRST_INTERVAL;
658 evas_object_smart_callbacks_descriptions_set(obj, _signals);
663 elm_flipselector_flip_next(Evas_Object *obj)
665 ELM_CHECK_WIDTYPE(obj, widtype);
667 Widget_Data *wd = elm_widget_data_get(obj);
670 if (wd->spin) ecore_timer_del(wd->spin);
672 _flipselector_walk(wd);
674 _flipselector_unwalk(wd);
678 elm_flipselector_flip_prev(Evas_Object *obj)
680 ELM_CHECK_WIDTYPE(obj, widtype);
682 Widget_Data *wd = elm_widget_data_get(obj);
685 if (wd->spin) ecore_timer_del(wd->spin);
687 _flipselector_walk(wd);
689 _flipselector_unwalk(wd);
692 EAPI Elm_Object_Item *
693 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
695 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
697 Elm_Flipselector_Item *item;
700 wd = elm_widget_data_get(obj);
701 if (!wd) return NULL;
703 item = _item_new(obj, label, func, data);
704 if (!item) return NULL;
706 wd->items = eina_list_append(wd->items, item);
709 wd->current = wd->items;
714 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
715 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
717 wd->sentinel = eina_list_last(wd->items);
721 if (eina_list_count(wd->items) >= 2)
722 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
724 return (Elm_Object_Item *) item;
727 EAPI Elm_Object_Item *
728 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
730 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
732 Elm_Flipselector_Item *item;
735 wd = elm_widget_data_get(obj);
736 if (!wd) return NULL;
738 item = _item_new(obj, label, func, data);
739 if (!item) return NULL;
741 wd->items = eina_list_prepend(wd->items, item);
744 wd->current = wd->items;
749 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
750 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
752 wd->sentinel = wd->items;
756 if (eina_list_count(wd->items) >= 2)
757 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
759 return (Elm_Object_Item *) item;
762 /* TODO: account for deleted items? */
763 EAPI const Eina_List *
764 elm_flipselector_items_get(const Evas_Object *obj)
766 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
768 Widget_Data *wd = elm_widget_data_get(obj);
769 if (!wd) return NULL;
773 EAPI Elm_Object_Item *
774 elm_flipselector_first_item_get(const Evas_Object *obj)
776 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
778 Elm_Flipselector_Item *it;
782 wd = elm_widget_data_get(obj);
783 if (!wd || !wd->items) return NULL;
785 EINA_LIST_FOREACH(wd->items, l, it)
787 if (it->deleted) continue;
788 return (Elm_Object_Item *) it;
793 EAPI Elm_Object_Item *
794 elm_flipselector_last_item_get(const Evas_Object *obj)
796 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
798 Elm_Flipselector_Item *it;
802 wd = elm_widget_data_get(obj);
803 if (!wd || !wd->items) return NULL;
805 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
807 if (it->deleted) continue;
808 return (Elm_Object_Item *) it;
813 EAPI Elm_Object_Item *
814 elm_flipselector_selected_item_get(const Evas_Object *obj)
816 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
818 Widget_Data *wd = elm_widget_data_get(obj);
819 if (!wd || !wd->current) return NULL;
820 return DATA_GET(wd->current);
824 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
826 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
828 Elm_Flipselector_Item *item, *_item, *cur;
829 int flipside = MSG_FLIP_UP;
833 item = (Elm_Flipselector_Item *) it;
834 wd = elm_widget_data_get(WIDGET(item));
837 cur = DATA_GET(wd->current);
838 if ((selected) && (cur == item)) return;
840 _flipselector_walk(wd);
842 if ((!selected) && (cur == item))
844 EINA_LIST_FOREACH(wd->items, l, _item)
849 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
853 _flipselector_unwalk(wd);
857 EINA_LIST_FOREACH(wd->items, l, _item)
859 if (_item == cur) flipside = MSG_FLIP_DOWN;
864 _send_msg(wd, flipside, (char *)item->label);
869 _flipselector_unwalk(wd);
873 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
875 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
877 Elm_Flipselector_Item *item;
879 item = (Elm_Flipselector_Item *) it;
880 wd = elm_widget_data_get(WIDGET(item));
881 if (!wd) return EINA_FALSE;
882 return (eina_list_data_get(wd->current) == item);
886 elm_flipselector_item_del(Elm_Object_Item *it)
888 elm_object_item_del(it);
892 elm_flipselector_item_label_get(const Elm_Object_Item *it)
894 return _item_text_get_hook(it, NULL);
898 elm_flipselector_item_label_set(Elm_Object_Item *it, const char *label)
900 _item_text_set_hook(it, NULL, label);
903 EAPI Elm_Object_Item *
904 elm_flipselector_item_prev_get(Elm_Object_Item *it)
906 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
908 Elm_Flipselector_Item *item, *_item;
912 item = (Elm_Flipselector_Item *) it;
913 wd = elm_widget_data_get(WIDGET(item));
914 if ((!wd) || (!wd->items)) return NULL;
916 EINA_LIST_FOREACH(wd->items, l, _item)
919 l = eina_list_prev(l);
926 EAPI Elm_Object_Item *
927 elm_flipselector_item_next_get(Elm_Object_Item *it)
929 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
931 Elm_Flipselector_Item *item, *_item;
935 item = (Elm_Flipselector_Item *) it;
936 wd = elm_widget_data_get(WIDGET(item));
937 if ((!wd) || (!wd->items)) return NULL;
939 EINA_LIST_FOREACH(wd->items, l, _item)
942 l = eina_list_next(l);
950 elm_flipselector_interval_set(Evas_Object *obj, double interval)
952 ELM_CHECK_WIDTYPE(obj, widtype);
954 Widget_Data *wd = elm_widget_data_get(obj);
956 wd->first_interval = interval;
960 elm_flipselector_interval_get(const Evas_Object *obj)
962 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
964 Widget_Data *wd = elm_widget_data_get(obj);
966 return wd->first_interval;