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,
73 const char *part __UNUSED__,
76 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
80 Elm_Flipselector_Item *item;
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 __UNUSED__)
107 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
109 Elm_Flipselector_Item *item, *_item;
113 item = (Elm_Flipselector_Item *) it;
114 wd = elm_widget_data_get(WIDGET(item));
115 if ((!wd) || (!wd->items)) return NULL;
117 EINA_LIST_FOREACH(wd->items, l, _item)
118 if (_item == item) return item->label;
123 _item_signal_emit_hook(Elm_Object_Item *it,
124 const char *emission,
127 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
128 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
129 edje_object_signal_emit(VIEW(item), emission, source);
132 static Elm_Flipselector_Item *
133 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
136 Elm_Flipselector_Item *it;
137 Widget_Data *wd = elm_widget_data_get(obj);
139 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
140 if (!it) return NULL;
142 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
143 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
144 elm_widget_item_signal_emit_hook_set(it, _item_signal_emit_hook);
147 if (len > wd->max_len)
150 it->label = eina_stringshare_add_length(label, len);
152 it->base.data = data;
154 /* TODO: no view here, but if one desires general contents in the
160 _item_free(Elm_Flipselector_Item *it)
162 eina_stringshare_del(it->label);
163 elm_widget_item_del(it);
167 _del_hook(Evas_Object *obj)
169 Elm_Flipselector_Item *item;
171 Widget_Data *wd = elm_widget_data_get(obj);
174 if (wd->walking) ERR("flipselector deleted while walking.\n");
176 EINA_LIST_FREE(wd->items, item)
179 if (wd->spin) ecore_timer_del(wd->spin);
184 _theme_hook(Evas_Object *obj)
189 wd = elm_widget_data_get(obj);
192 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
193 elm_widget_style_get(obj));
194 edje_object_scale_set(wd->base,
195 elm_widget_scale_get(obj) * _elm_config->scale);
197 max_len = edje_object_data_get(wd->base, "max_len");
199 wd->max_len = MAX_LEN_DEFAULT;
202 wd->max_len = atoi(max_len);
204 wd->max_len = MAX_LEN_DEFAULT;
212 _sentinel_eval(Widget_Data *wd)
214 Elm_Flipselector_Item *it;
223 wd->sentinel = wd->items;
225 EINA_LIST_FOREACH(wd->items, l, it)
227 if (strlen(elm_object_item_text_get((Elm_Object_Item *) it)) >
228 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
233 /* TODO: create a flag to avoid looping here all times */
235 _flipselector_process_deletions(Widget_Data *wd)
237 Elm_Flipselector_Item *it;
239 Eina_Bool skip = EINA_TRUE;
240 Eina_Bool sentinel_eval = EINA_FALSE;
242 wd->walking++; /* avoid nested deletions */
244 EINA_LIST_FOREACH(wd->items, l, it)
246 if (!it->deleted) continue;
248 if (wd->current == l)
250 if (wd->current == wd->sentinel)
251 sentinel_eval = EINA_TRUE;
252 wd->current = eina_list_prev(wd->current);
254 wd->items = eina_list_remove(wd->items, it);
257 wd->current = wd->items;
262 if (eina_list_count(wd->items) <= 1)
263 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
265 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
269 _update_view(wd->self);
278 _flipselector_walk(Widget_Data *wd)
282 ERR("walking was negative. fixed!\n");
289 _flipselector_unwalk(Widget_Data *wd)
294 ERR("walking became negative. fixed!\n");
297 if (wd->walking) return;
299 _flipselector_process_deletions(wd);
303 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
305 Evas_Event_Key_Down *ev;
307 Eina_Bool is_up = EINA_TRUE;
309 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
311 wd = elm_widget_data_get(obj);
312 if (!wd) return EINA_FALSE;
315 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
317 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
319 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
321 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
324 if (wd->spin) ecore_timer_del(wd->spin);
326 /* TODO: if direction setting via API is not coming in, replace
327 these calls by flip_{next,prev} */
328 _flipselector_walk(wd);
329 if (is_up) _flip_up(wd);
331 _flipselector_unwalk(wd);
333 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
338 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
340 Widget_Data *wd = elm_widget_data_get(obj);
343 /* FIXME: no treatment of this signal so far */
344 if (elm_widget_focus_get(obj))
346 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
347 evas_object_focus_set(wd->base, EINA_TRUE);
351 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
352 evas_object_focus_set(wd->base, EINA_FALSE);
357 _sizing_eval(Evas_Object *obj)
360 const char *tmp = NULL;
361 Evas_Coord minw = -1, minh = -1, w, h;
363 wd = elm_widget_data_get(obj);
366 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
370 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
371 tmp = edje_object_part_text_get(wd->base, "top");
372 edje_object_part_text_set(wd->base, "top", label);
375 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
376 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
377 evas_object_size_hint_min_get(obj, &w, &h);
379 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
381 if (w > minw) minw = w;
382 if (h > minh) minh = h;
384 evas_object_size_hint_min_set(obj, minw, minh);
388 _update_view(Evas_Object *obj)
392 Elm_Flipselector_Item *item;
394 wd = elm_widget_data_get(obj);
398 item = DATA_GET(wd->current);
399 if (item) label = item->label;
401 edje_object_part_text_set(wd->base, "top", label ? label : "");
402 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
403 edje_object_message_signal_process(wd->base);
407 _changed(Widget_Data *wd)
409 Elm_Flipselector_Item *item;
411 item = DATA_GET(wd->current);
415 item->func((void *)item->base.data, WIDGET(item), item);
417 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
421 _send_msg(Widget_Data *wd, int flipside, char *label)
423 Edje_Message_String msg;
426 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
427 edje_object_message_signal_process(wd->base);
433 _flip_up(Widget_Data *wd)
435 Elm_Flipselector_Item *item;
437 if (!wd->current) return;
439 if (wd->current == wd->items)
441 wd->current = eina_list_last(wd->items);
442 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
445 wd->current = eina_list_prev(wd->current);
447 item = DATA_GET(wd->current);
450 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
454 _signal_val_up(void *data)
456 Widget_Data *wd = elm_widget_data_get(data);
458 if (!wd) goto val_up_exit_on_error;
460 _flipselector_walk(wd);
462 if (wd->interval > FLIP_MIN_INTERVAL)
463 wd->interval = wd->interval / 1.05;
465 ecore_timer_interval_set(wd->spin, wd->interval);
469 _flipselector_unwalk(wd);
471 return ECORE_CALLBACK_RENEW;
473 val_up_exit_on_error:
474 return ECORE_CALLBACK_CANCEL;
478 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
480 Widget_Data *wd = elm_widget_data_get(data);
483 wd->interval = wd->first_interval;
485 if (wd->spin) ecore_timer_del(wd->spin);
486 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
488 _signal_val_up(data);
492 _flip_down(Widget_Data *wd)
494 Elm_Flipselector_Item *item;
496 if (!wd->current) return;
498 wd->current = eina_list_next(wd->current);
501 wd->current = wd->items;
502 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
505 item = DATA_GET(wd->current);
508 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
512 _signal_val_down(void *data)
514 Widget_Data *wd = elm_widget_data_get(data);
516 if (!wd) goto val_down_exit_on_error;
518 _flipselector_walk(wd);
520 if (wd->interval > FLIP_MIN_INTERVAL)
521 wd->interval = wd->interval / 1.05;
522 ecore_timer_interval_set(wd->spin, wd->interval);
526 _flipselector_unwalk(wd);
528 return ECORE_CALLBACK_RENEW;
530 val_down_exit_on_error:
531 return ECORE_CALLBACK_CANCEL;
535 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
537 Widget_Data *wd = elm_widget_data_get(data);
540 wd->interval = wd->first_interval;
542 if (wd->spin) ecore_timer_del(wd->spin);
543 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
545 _signal_val_down(data);
549 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
551 Widget_Data *wd = elm_widget_data_get(data);
554 if (wd->spin) ecore_timer_del(wd->spin);
559 _callbacks_set(Evas_Object *obj)
561 Widget_Data *wd = elm_widget_data_get(obj);
563 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
564 "", _signal_val_up_start, obj);
565 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
566 "", _signal_val_change_stop, obj);
567 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
568 "", _signal_val_down_start, obj);
569 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
570 "", _signal_val_change_stop, obj);
574 elm_flipselector_add(Evas_Object *parent)
580 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
582 ELM_SET_WIDTYPE(widtype, "flipselector");
583 elm_widget_type_set(obj, "flipselector");
584 elm_widget_sub_object_add(parent, obj);
585 elm_widget_data_set(obj, wd);
588 elm_widget_del_hook_set(obj, _del_hook);
589 elm_widget_theme_hook_set(obj, _theme_hook);
590 /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
592 elm_widget_can_focus_set(obj, EINA_TRUE);
593 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
594 elm_widget_event_hook_set(obj, _event_hook);
596 wd->base = edje_object_add(e);
597 elm_widget_resize_object_set(obj, wd->base);
601 wd->first_interval = FLIP_FIRST_INTERVAL;
605 evas_object_smart_callbacks_descriptions_set(obj, _signals);
610 elm_flipselector_flip_next(Evas_Object *obj)
612 ELM_CHECK_WIDTYPE(obj, widtype);
614 Widget_Data *wd = elm_widget_data_get(obj);
617 if (wd->spin) ecore_timer_del(wd->spin);
619 _flipselector_walk(wd);
621 _flipselector_unwalk(wd);
625 elm_flipselector_flip_prev(Evas_Object *obj)
627 ELM_CHECK_WIDTYPE(obj, widtype);
629 Widget_Data *wd = elm_widget_data_get(obj);
632 if (wd->spin) ecore_timer_del(wd->spin);
634 _flipselector_walk(wd);
636 _flipselector_unwalk(wd);
639 EAPI Elm_Object_Item *
640 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
642 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
644 Elm_Flipselector_Item *item;
647 wd = elm_widget_data_get(obj);
648 if (!wd) return NULL;
650 item = _item_new(obj, label, func, data);
651 if (!item) return NULL;
653 wd->items = eina_list_append(wd->items, item);
656 wd->current = wd->items;
661 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
662 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
664 wd->sentinel = eina_list_last(wd->items);
668 if (eina_list_count(wd->items) >= 2)
669 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
671 return (Elm_Object_Item *) item;
674 EAPI Elm_Object_Item *
675 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
677 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
679 Elm_Flipselector_Item *item;
682 wd = elm_widget_data_get(obj);
683 if (!wd) return NULL;
685 item = _item_new(obj, label, func, data);
686 if (!item) return NULL;
688 wd->items = eina_list_prepend(wd->items, item);
691 wd->current = wd->items;
696 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
697 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
699 wd->sentinel = wd->items;
703 if (eina_list_count(wd->items) >= 2)
704 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
706 return (Elm_Object_Item *) item;
709 /* TODO: account for deleted items? */
710 EAPI const Eina_List *
711 elm_flipselector_items_get(const Evas_Object *obj)
713 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
715 Widget_Data *wd = elm_widget_data_get(obj);
716 if (!wd) return NULL;
720 EAPI Elm_Object_Item *
721 elm_flipselector_first_item_get(const Evas_Object *obj)
723 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
725 Elm_Flipselector_Item *it;
729 wd = elm_widget_data_get(obj);
730 if (!wd || !wd->items) return NULL;
732 EINA_LIST_FOREACH(wd->items, l, it)
734 if (it->deleted) continue;
735 return (Elm_Object_Item *) it;
740 EAPI Elm_Object_Item *
741 elm_flipselector_last_item_get(const Evas_Object *obj)
743 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
745 Elm_Flipselector_Item *it;
749 wd = elm_widget_data_get(obj);
750 if (!wd || !wd->items) return NULL;
752 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
754 if (it->deleted) continue;
755 return (Elm_Object_Item *) it;
760 EAPI Elm_Object_Item *
761 elm_flipselector_selected_item_get(const Evas_Object *obj)
763 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
765 Widget_Data *wd = elm_widget_data_get(obj);
766 if (!wd || !wd->current) return NULL;
767 return DATA_GET(wd->current);
771 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
773 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
775 Elm_Flipselector_Item *item, *_item, *cur;
776 int flipside = MSG_FLIP_UP;
780 item = (Elm_Flipselector_Item *) it;
781 wd = elm_widget_data_get(WIDGET(item));
784 cur = DATA_GET(wd->current);
785 if ((selected) && (cur == item)) return;
787 _flipselector_walk(wd);
789 if ((!selected) && (cur == item))
791 EINA_LIST_FOREACH(wd->items, l, _item)
796 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
800 _flipselector_unwalk(wd);
804 EINA_LIST_FOREACH(wd->items, l, _item)
806 if (_item == cur) flipside = MSG_FLIP_DOWN;
811 _send_msg(wd, flipside, (char *)item->label);
816 _flipselector_unwalk(wd);
820 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
822 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
824 Elm_Flipselector_Item *item;
826 item = (Elm_Flipselector_Item *) it;
827 wd = elm_widget_data_get(WIDGET(item));
828 if (!wd) return EINA_FALSE;
829 return (eina_list_data_get(wd->current) == item);
833 elm_flipselector_item_del(Elm_Object_Item *it)
835 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
837 Elm_Flipselector_Item *item, *item2;
840 item = (Elm_Flipselector_Item *) it;
841 wd = elm_widget_data_get(WIDGET(item));
846 item->deleted = EINA_TRUE;
850 _flipselector_walk(wd);
852 wd->items = eina_list_remove(wd->items, item);
855 _flipselector_unwalk(wd);
859 elm_flipselector_item_label_get(const Elm_Object_Item *it)
861 return _item_text_get_hook(it, NULL);
865 elm_flipselector_item_label_set(Elm_Object_Item *it, const char *label)
867 _item_text_set_hook(it, NULL, label);
870 EAPI Elm_Object_Item *
871 elm_flipselector_item_prev_get(Elm_Object_Item *it)
873 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
875 Elm_Flipselector_Item *item, *_item;
879 item = (Elm_Flipselector_Item *) it;
880 wd = elm_widget_data_get(WIDGET(item));
881 if ((!wd) || (!wd->items)) return NULL;
883 EINA_LIST_FOREACH(wd->items, l, _item)
886 l = eina_list_prev(l);
893 EAPI Elm_Object_Item *
894 elm_flipselector_item_next_get(Elm_Object_Item *it)
896 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
898 Elm_Flipselector_Item *item, *_item;
902 item = (Elm_Flipselector_Item *) it;
903 wd = elm_widget_data_get(WIDGET(item));
904 if ((!wd) || (!wd->items)) return NULL;
906 EINA_LIST_FOREACH(wd->items, l, _item)
909 l = eina_list_next(l);
917 elm_flipselector_interval_set(Evas_Object *obj, double interval)
919 ELM_CHECK_WIDTYPE(obj, widtype);
921 Widget_Data *wd = elm_widget_data_get(obj);
923 wd->first_interval = interval;
927 elm_flipselector_interval_get(const Evas_Object *obj)
929 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
931 Widget_Data *wd = elm_widget_data_get(obj);
933 return wd->first_interval;