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 void _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);
584 Elm_Flipselector_Item *item, *item2;
587 item = (Elm_Flipselector_Item *) it;
588 wd = elm_widget_data_get(WIDGET(item));
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);
625 elm_flipselector_add(Evas_Object *parent)
631 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
633 ELM_SET_WIDTYPE(widtype, "flipselector");
634 elm_widget_type_set(obj, "flipselector");
635 elm_widget_sub_object_add(parent, obj);
636 elm_widget_data_set(obj, wd);
639 elm_widget_del_hook_set(obj, _del_hook);
640 elm_widget_theme_hook_set(obj, _theme_hook);
641 /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
643 elm_widget_can_focus_set(obj, EINA_TRUE);
644 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
645 elm_widget_event_hook_set(obj, _event_hook);
647 wd->base = edje_object_add(e);
648 elm_widget_resize_object_set(obj, wd->base);
652 wd->first_interval = FLIP_FIRST_INTERVAL;
656 evas_object_smart_callbacks_descriptions_set(obj, _signals);
661 elm_flipselector_flip_next(Evas_Object *obj)
663 ELM_CHECK_WIDTYPE(obj, widtype);
665 Widget_Data *wd = elm_widget_data_get(obj);
668 if (wd->spin) ecore_timer_del(wd->spin);
670 _flipselector_walk(wd);
672 _flipselector_unwalk(wd);
676 elm_flipselector_flip_prev(Evas_Object *obj)
678 ELM_CHECK_WIDTYPE(obj, widtype);
680 Widget_Data *wd = elm_widget_data_get(obj);
683 if (wd->spin) ecore_timer_del(wd->spin);
685 _flipselector_walk(wd);
687 _flipselector_unwalk(wd);
690 EAPI Elm_Object_Item *
691 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
693 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
695 Elm_Flipselector_Item *item;
698 wd = elm_widget_data_get(obj);
699 if (!wd) return NULL;
701 item = _item_new(obj, label, func, data);
702 if (!item) return NULL;
704 wd->items = eina_list_append(wd->items, item);
707 wd->current = wd->items;
712 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
713 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
715 wd->sentinel = eina_list_last(wd->items);
719 if (eina_list_count(wd->items) >= 2)
720 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
722 return (Elm_Object_Item *) item;
725 EAPI Elm_Object_Item *
726 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
728 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
730 Elm_Flipselector_Item *item;
733 wd = elm_widget_data_get(obj);
734 if (!wd) return NULL;
736 item = _item_new(obj, label, func, data);
737 if (!item) return NULL;
739 wd->items = eina_list_prepend(wd->items, item);
742 wd->current = wd->items;
747 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
748 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
750 wd->sentinel = wd->items;
754 if (eina_list_count(wd->items) >= 2)
755 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
757 return (Elm_Object_Item *) item;
760 /* TODO: account for deleted items? */
761 EAPI const Eina_List *
762 elm_flipselector_items_get(const Evas_Object *obj)
764 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
766 Widget_Data *wd = elm_widget_data_get(obj);
767 if (!wd) return NULL;
771 EAPI Elm_Object_Item *
772 elm_flipselector_first_item_get(const Evas_Object *obj)
774 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
776 Elm_Flipselector_Item *it;
780 wd = elm_widget_data_get(obj);
781 if (!wd || !wd->items) return NULL;
783 EINA_LIST_FOREACH(wd->items, l, it)
785 if (it->deleted) continue;
786 return (Elm_Object_Item *) it;
791 EAPI Elm_Object_Item *
792 elm_flipselector_last_item_get(const Evas_Object *obj)
794 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
796 Elm_Flipselector_Item *it;
800 wd = elm_widget_data_get(obj);
801 if (!wd || !wd->items) return NULL;
803 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
805 if (it->deleted) continue;
806 return (Elm_Object_Item *) it;
811 EAPI Elm_Object_Item *
812 elm_flipselector_selected_item_get(const Evas_Object *obj)
814 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
816 Widget_Data *wd = elm_widget_data_get(obj);
817 if (!wd || !wd->current) return NULL;
818 return DATA_GET(wd->current);
822 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
824 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
826 Elm_Flipselector_Item *item, *_item, *cur;
827 int flipside = MSG_FLIP_UP;
831 item = (Elm_Flipselector_Item *) it;
832 wd = elm_widget_data_get(WIDGET(item));
835 cur = DATA_GET(wd->current);
836 if ((selected) && (cur == item)) return;
838 _flipselector_walk(wd);
840 if ((!selected) && (cur == item))
842 EINA_LIST_FOREACH(wd->items, l, _item)
847 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
851 _flipselector_unwalk(wd);
855 EINA_LIST_FOREACH(wd->items, l, _item)
857 if (_item == cur) flipside = MSG_FLIP_DOWN;
862 _send_msg(wd, flipside, (char *)item->label);
867 _flipselector_unwalk(wd);
871 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
873 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
875 Elm_Flipselector_Item *item;
877 item = (Elm_Flipselector_Item *) it;
878 wd = elm_widget_data_get(WIDGET(item));
879 if (!wd) return EINA_FALSE;
880 return (eina_list_data_get(wd->current) == item);
884 elm_flipselector_item_del(Elm_Object_Item *it)
886 elm_object_item_del(it);
890 elm_flipselector_item_label_get(const Elm_Object_Item *it)
892 return _item_text_get_hook(it, NULL);
896 elm_flipselector_item_label_set(Elm_Object_Item *it, const char *label)
898 _item_text_set_hook(it, NULL, label);
901 EAPI Elm_Object_Item *
902 elm_flipselector_item_prev_get(Elm_Object_Item *it)
904 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
906 Elm_Flipselector_Item *item, *_item;
910 item = (Elm_Flipselector_Item *) it;
911 wd = elm_widget_data_get(WIDGET(item));
912 if ((!wd) || (!wd->items)) return NULL;
914 EINA_LIST_FOREACH(wd->items, l, _item)
917 l = eina_list_prev(l);
924 EAPI Elm_Object_Item *
925 elm_flipselector_item_next_get(Elm_Object_Item *it)
927 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
929 Elm_Flipselector_Item *item, *_item;
933 item = (Elm_Flipselector_Item *) it;
934 wd = elm_widget_data_get(WIDGET(item));
935 if ((!wd) || (!wd->items)) return NULL;
937 EINA_LIST_FOREACH(wd->items, l, _item)
940 l = eina_list_next(l);
948 elm_flipselector_interval_set(Evas_Object *obj, double interval)
950 ELM_CHECK_WIDTYPE(obj, widtype);
952 Widget_Data *wd = elm_widget_data_get(obj);
954 wd->first_interval = interval;
958 elm_flipselector_interval_get(const Evas_Object *obj)
960 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
962 Widget_Data *wd = elm_widget_data_get(obj);
964 return wd->first_interval;