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: fix default theme image borders for looong strings as item
12 /* TODO: set text elipsis on labels if one enforces mininum size on
13 * the overall widget less the required for displaying it. */
14 /* TODO: find a way to, in the default theme, to detect we are
15 * bootstrapping (receiving the 1st message) and populate the downmost
16 * TEXT parts with the same text as the upmost, where appropriate. */
18 #define FLIP_FIRST_INTERVAL (0.85)
19 #define FLIP_MIN_INTERVAL (0.1)
20 #define MSG_FLIP_DOWN (1)
21 #define MSG_FLIP_UP (2)
22 #define MAX_LEN_DEFAULT (50)
24 #define DATA_GET eina_list_data_get
26 struct _Elm_Flipselector_Item
35 typedef struct _Widget_Data Widget_Data;
36 typedef struct _Elm_Flipselector_Item Elm_Flipselector_Item;
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);
59 static Eina_Bool _item_del_pre_hook(Elm_Object_Item *it);
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,
78 Elm_Flipselector_Item *item;
82 if (part && strcmp(part ,"default")) return;
84 item = (Elm_Flipselector_Item *) it;
85 wd = elm_widget_data_get(WIDGET(item));
86 if ((!wd) || (!wd->items)) return;
88 l = eina_list_data_find_list(wd->items, item);
91 eina_stringshare_del(item->label);
92 item->label = eina_stringshare_add_length(label, wd->max_len);
94 if (strlen(label) > strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
99 _update_view(WIDGET(item));
100 _sizing_eval(wd->self);
105 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
107 if (part && strcmp(part ,"default")) return NULL;
109 return ((Elm_Flipselector_Item *) it)->label;
113 _item_signal_emit_hook(Elm_Object_Item *it,
114 const char *emission,
117 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
118 edje_object_signal_emit(VIEW(item), emission, source);
121 static Elm_Flipselector_Item *
122 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
125 Elm_Flipselector_Item *it;
126 Widget_Data *wd = elm_widget_data_get(obj);
128 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
129 if (!it) return NULL;
131 elm_widget_item_del_pre_hook_set(it, _item_del_pre_hook);
132 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
133 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
134 elm_widget_item_signal_emit_hook_set(it, _item_signal_emit_hook);
137 if (len > wd->max_len)
140 it->label = eina_stringshare_add_length(label, len);
142 it->base.data = data;
144 /* TODO: no view here, but if one desires general contents in the
150 _item_free(Elm_Flipselector_Item *it)
152 eina_stringshare_del(it->label);
153 elm_widget_item_free(it);
157 _del_hook(Evas_Object *obj)
159 Elm_Flipselector_Item *item;
161 Widget_Data *wd = elm_widget_data_get(obj);
164 if (wd->walking) ERR("flipselector deleted while walking.\n");
166 EINA_LIST_FREE(wd->items, item)
169 if (wd->spin) ecore_timer_del(wd->spin);
174 _disable_hook(Evas_Object *obj)
176 Widget_Data *wd = elm_widget_data_get(obj);
179 if (elm_widget_disabled_get(obj))
180 edje_object_signal_emit(wd->base, "elm,state,disabled", "elm");
182 edje_object_signal_emit(wd->base, "elm,state,enabled", "elm");
186 _theme_hook(Evas_Object *obj)
191 wd = elm_widget_data_get(obj);
194 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
195 elm_widget_style_get(obj));
196 edje_object_scale_set(wd->base,
197 elm_widget_scale_get(obj) * _elm_config->scale);
199 max_len = edje_object_data_get(wd->base, "max_len");
201 wd->max_len = MAX_LEN_DEFAULT;
204 wd->max_len = atoi(max_len);
206 wd->max_len = MAX_LEN_DEFAULT;
214 _sentinel_eval(Widget_Data *wd)
216 Elm_Flipselector_Item *it;
225 wd->sentinel = wd->items;
227 EINA_LIST_FOREACH(wd->items, l, it)
229 if (strlen(elm_object_item_text_get((Elm_Object_Item *) it)) >
230 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
235 /* TODO: create a flag to avoid looping here all times */
237 _flipselector_process_deletions(Widget_Data *wd)
239 Elm_Flipselector_Item *it;
241 Eina_Bool skip = EINA_TRUE;
242 Eina_Bool sentinel_eval = EINA_FALSE;
244 wd->walking++; /* avoid nested deletions */
246 EINA_LIST_FOREACH(wd->items, l, it)
248 if (!it->deleted) continue;
250 if (wd->current == l)
252 if (wd->current == wd->sentinel)
253 sentinel_eval = EINA_TRUE;
254 wd->current = eina_list_prev(wd->current);
256 wd->items = eina_list_remove(wd->items, it);
259 wd->current = wd->items;
264 if (eina_list_count(wd->items) <= 1)
265 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
267 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
271 _update_view(wd->self);
280 _flipselector_walk(Widget_Data *wd)
284 ERR("walking was negative. fixed!\n");
291 _flipselector_unwalk(Widget_Data *wd)
296 ERR("walking became negative. fixed!\n");
299 if (wd->walking) return;
301 _flipselector_process_deletions(wd);
305 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
307 Evas_Event_Key_Down *ev;
309 Eina_Bool is_up = EINA_TRUE;
311 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
313 wd = elm_widget_data_get(obj);
314 if (!wd) return EINA_FALSE;
317 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
319 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
321 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
323 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
326 if (wd->spin) ecore_timer_del(wd->spin);
328 /* TODO: if direction setting via API is not coming in, replace
329 these calls by flip_{next,prev} */
330 _flipselector_walk(wd);
331 if (is_up) _flip_up(wd);
333 _flipselector_unwalk(wd);
335 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
340 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
342 Widget_Data *wd = elm_widget_data_get(obj);
345 /* FIXME: no treatment of this signal so far */
346 if (elm_widget_focus_get(obj))
348 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
349 evas_object_focus_set(wd->base, EINA_TRUE);
353 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
354 evas_object_focus_set(wd->base, EINA_FALSE);
359 _sizing_eval(Evas_Object *obj)
362 const char *tmp = NULL;
363 Evas_Coord minw = -1, minh = -1, w, h;
365 wd = elm_widget_data_get(obj);
368 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
372 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
373 tmp = edje_object_part_text_get(wd->base, "top");
374 edje_object_part_text_set(wd->base, "top", label);
377 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
378 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
379 evas_object_size_hint_min_get(obj, &w, &h);
381 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
383 if (w > minw) minw = w;
384 if (h > minh) minh = h;
386 evas_object_size_hint_min_set(obj, minw, minh);
390 _update_view(Evas_Object *obj)
394 Elm_Flipselector_Item *item;
396 wd = elm_widget_data_get(obj);
400 item = DATA_GET(wd->current);
401 if (item) label = item->label;
403 edje_object_part_text_set(wd->base, "top", label ? label : "");
404 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
405 edje_object_message_signal_process(wd->base);
409 _changed(Widget_Data *wd)
411 Elm_Flipselector_Item *item;
413 item = DATA_GET(wd->current);
417 item->func((void *)item->base.data, WIDGET(item), item);
419 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
423 _send_msg(Widget_Data *wd, int flipside, char *label)
425 Edje_Message_String msg;
428 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
429 edje_object_message_signal_process(wd->base);
435 _flip_up(Widget_Data *wd)
437 Elm_Flipselector_Item *item;
439 if (!wd->current) return;
441 if (wd->current == wd->items)
443 wd->current = eina_list_last(wd->items);
444 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
447 wd->current = eina_list_prev(wd->current);
449 item = DATA_GET(wd->current);
452 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
456 _signal_val_up(void *data)
458 Widget_Data *wd = elm_widget_data_get(data);
460 if (!wd) goto val_up_exit_on_error;
462 _flipselector_walk(wd);
464 if (wd->interval > FLIP_MIN_INTERVAL)
465 wd->interval = wd->interval / 1.05;
467 ecore_timer_interval_set(wd->spin, wd->interval);
471 _flipselector_unwalk(wd);
473 return ECORE_CALLBACK_RENEW;
475 val_up_exit_on_error:
476 return ECORE_CALLBACK_CANCEL;
480 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
482 Widget_Data *wd = elm_widget_data_get(data);
485 wd->interval = wd->first_interval;
487 if (wd->spin) ecore_timer_del(wd->spin);
488 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
490 _signal_val_up(data);
494 _flip_down(Widget_Data *wd)
496 Elm_Flipselector_Item *item;
498 if (!wd->current) return;
500 wd->current = eina_list_next(wd->current);
503 wd->current = wd->items;
504 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
507 item = DATA_GET(wd->current);
510 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
514 _signal_val_down(void *data)
516 Widget_Data *wd = elm_widget_data_get(data);
518 if (!wd) goto val_down_exit_on_error;
520 _flipselector_walk(wd);
522 if (wd->interval > FLIP_MIN_INTERVAL)
523 wd->interval = wd->interval / 1.05;
524 ecore_timer_interval_set(wd->spin, wd->interval);
528 _flipselector_unwalk(wd);
530 return ECORE_CALLBACK_RENEW;
532 val_down_exit_on_error:
533 return ECORE_CALLBACK_CANCEL;
537 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
539 Widget_Data *wd = elm_widget_data_get(data);
542 wd->interval = wd->first_interval;
544 if (wd->spin) ecore_timer_del(wd->spin);
545 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
547 _signal_val_down(data);
551 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
553 Widget_Data *wd = elm_widget_data_get(data);
556 if (wd->spin) ecore_timer_del(wd->spin);
561 _callbacks_set(Evas_Object *obj)
563 Widget_Data *wd = elm_widget_data_get(obj);
565 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
566 "", _signal_val_up_start, obj);
567 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
568 "", _signal_val_change_stop, obj);
569 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
570 "", _signal_val_down_start, obj);
571 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
572 "", _signal_val_change_stop, obj);
576 _item_del_pre_hook(Elm_Object_Item *it)
579 Elm_Flipselector_Item *item, *item2;
582 item = (Elm_Flipselector_Item *) it;
583 wd = elm_widget_data_get(WIDGET(item));
584 if (!wd) return EINA_FALSE;
588 item->deleted = EINA_TRUE;
592 _flipselector_walk(wd);
594 EINA_LIST_FOREACH(wd->items, l, item2)
598 wd->items = eina_list_remove_list(wd->items, l);
599 if (wd->current == l)
601 wd->current = l->prev;
602 if (!wd->current) wd->current = l->next;
605 item2 = wd->current->data;
606 _send_msg(wd, MSG_FLIP_DOWN, (char *)item2->label);
609 _send_msg(wd, MSG_FLIP_DOWN, "");
614 eina_stringshare_del(item->label);
616 _flipselector_unwalk(wd);
622 elm_flipselector_add(Evas_Object *parent)
628 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
630 ELM_SET_WIDTYPE(widtype, "flipselector");
631 elm_widget_type_set(obj, "flipselector");
632 elm_widget_sub_object_add(parent, obj);
633 elm_widget_data_set(obj, wd);
636 elm_widget_del_hook_set(obj, _del_hook);
637 elm_widget_theme_hook_set(obj, _theme_hook);
638 elm_widget_disable_hook_set(obj, _disable_hook);
640 elm_widget_can_focus_set(obj, EINA_TRUE);
641 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
642 elm_widget_event_hook_set(obj, _event_hook);
644 wd->base = edje_object_add(e);
645 elm_widget_resize_object_set(obj, wd->base);
649 wd->first_interval = FLIP_FIRST_INTERVAL;
653 evas_object_smart_callbacks_descriptions_set(obj, _signals);
658 elm_flipselector_flip_next(Evas_Object *obj)
660 ELM_CHECK_WIDTYPE(obj, widtype);
662 Widget_Data *wd = elm_widget_data_get(obj);
665 if (wd->spin) ecore_timer_del(wd->spin);
667 _flipselector_walk(wd);
669 _flipselector_unwalk(wd);
673 elm_flipselector_flip_prev(Evas_Object *obj)
675 ELM_CHECK_WIDTYPE(obj, widtype);
677 Widget_Data *wd = elm_widget_data_get(obj);
680 if (wd->spin) ecore_timer_del(wd->spin);
682 _flipselector_walk(wd);
684 _flipselector_unwalk(wd);
687 EAPI Elm_Object_Item *
688 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
690 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
692 Elm_Flipselector_Item *item;
695 wd = elm_widget_data_get(obj);
696 if (!wd) return NULL;
698 item = _item_new(obj, label, func, data);
699 if (!item) return NULL;
701 wd->items = eina_list_append(wd->items, item);
704 wd->current = wd->items;
709 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
710 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
712 wd->sentinel = eina_list_last(wd->items);
716 if (eina_list_count(wd->items) >= 2)
717 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
719 return (Elm_Object_Item *) item;
722 EAPI Elm_Object_Item *
723 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
725 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
727 Elm_Flipselector_Item *item;
730 wd = elm_widget_data_get(obj);
731 if (!wd) return NULL;
733 item = _item_new(obj, label, func, data);
734 if (!item) return NULL;
736 wd->items = eina_list_prepend(wd->items, item);
739 wd->current = wd->items;
744 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
745 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
747 wd->sentinel = wd->items;
751 if (eina_list_count(wd->items) >= 2)
752 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
754 return (Elm_Object_Item *) item;
757 EAPI const Eina_List *
758 elm_flipselector_items_get(const Evas_Object *obj)
760 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
762 Widget_Data *wd = elm_widget_data_get(obj);
763 if (!wd) return NULL;
767 EAPI Elm_Object_Item *
768 elm_flipselector_first_item_get(const Evas_Object *obj)
770 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
772 Elm_Flipselector_Item *it;
776 wd = elm_widget_data_get(obj);
777 if (!wd || !wd->items) return NULL;
779 EINA_LIST_FOREACH(wd->items, l, it)
781 if (it->deleted) continue;
782 return (Elm_Object_Item *) it;
787 EAPI Elm_Object_Item *
788 elm_flipselector_last_item_get(const Evas_Object *obj)
790 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
792 Elm_Flipselector_Item *it;
796 wd = elm_widget_data_get(obj);
797 if (!wd || !wd->items) return NULL;
799 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
801 if (it->deleted) continue;
802 return (Elm_Object_Item *) it;
807 EAPI Elm_Object_Item *
808 elm_flipselector_selected_item_get(const Evas_Object *obj)
810 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
812 Widget_Data *wd = elm_widget_data_get(obj);
813 if (!wd) return NULL;
814 return DATA_GET(wd->current);
818 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
820 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
822 Elm_Flipselector_Item *item, *_item, *cur;
823 int flipside = MSG_FLIP_UP;
827 item = (Elm_Flipselector_Item *) it;
828 wd = elm_widget_data_get(WIDGET(item));
831 cur = DATA_GET(wd->current);
832 if ((selected) && (cur == item)) return;
834 _flipselector_walk(wd);
836 if ((!selected) && (cur == item))
838 EINA_LIST_FOREACH(wd->items, l, _item)
843 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
847 _flipselector_unwalk(wd);
851 EINA_LIST_FOREACH(wd->items, l, _item)
853 if (_item == cur) flipside = MSG_FLIP_DOWN;
858 _send_msg(wd, flipside, (char *)item->label);
863 _flipselector_unwalk(wd);
867 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
869 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
871 Elm_Flipselector_Item *item;
873 item = (Elm_Flipselector_Item *) it;
874 wd = elm_widget_data_get(WIDGET(item));
875 if (!wd) return EINA_FALSE;
876 return (eina_list_data_get(wd->current) == item);
880 elm_flipselector_item_del(Elm_Object_Item *it)
882 elm_object_item_del(it);
886 elm_flipselector_item_label_get(const Elm_Object_Item *it)
888 return _item_text_get_hook(it, NULL);
892 elm_flipselector_item_label_set(Elm_Object_Item *it, const char *label)
894 _item_text_set_hook(it, NULL, label);
897 EAPI Elm_Object_Item *
898 elm_flipselector_item_prev_get(const Elm_Object_Item *it)
900 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
904 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
906 wd = elm_widget_data_get(WIDGET(item));
907 if ((!wd) || (!wd->items)) return NULL;
909 l = eina_list_data_find_list(wd->items, it);
910 if (l && l->prev) return DATA_GET(l->prev);
915 EAPI Elm_Object_Item *
916 elm_flipselector_item_next_get(const Elm_Object_Item *it)
918 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
922 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
924 wd = elm_widget_data_get(WIDGET(item));
925 if ((!wd) || (!wd->items)) return NULL;
927 l = eina_list_data_find_list(wd->items, it);
928 if (l && l->next) return DATA_GET(l->next);
934 elm_flipselector_interval_set(Evas_Object *obj, double interval)
936 ELM_CHECK_WIDTYPE(obj, widtype);
938 Widget_Data *wd = elm_widget_data_get(obj);
940 wd->first_interval = interval;
944 elm_flipselector_interval_get(const Evas_Object *obj)
946 ELM_CHECK_WIDTYPE(obj, widtype) 0;
948 Widget_Data *wd = elm_widget_data_get(obj);
950 return wd->first_interval;