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);
329 /* TODO: if direction setting via API is not coming in, replace
330 these calls by flip_{next,prev} */
331 _flipselector_walk(wd);
332 if (is_up) _flip_up(wd);
334 _flipselector_unwalk(wd);
336 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
341 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
343 Widget_Data *wd = elm_widget_data_get(obj);
346 /* FIXME: no treatment of this signal so far */
347 if (elm_widget_focus_get(obj))
349 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
350 evas_object_focus_set(wd->base, EINA_TRUE);
354 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
355 evas_object_focus_set(wd->base, EINA_FALSE);
360 _sizing_eval(Evas_Object *obj)
363 const char *tmp = NULL;
364 Evas_Coord minw = -1, minh = -1, w, h;
366 wd = elm_widget_data_get(obj);
369 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
373 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
374 tmp = edje_object_part_text_get(wd->base, "top");
375 edje_object_part_text_set(wd->base, "top", label);
378 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
379 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
380 evas_object_size_hint_min_get(obj, &w, &h);
382 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
384 if (w > minw) minw = w;
385 if (h > minh) minh = h;
387 evas_object_size_hint_min_set(obj, minw, minh);
391 _update_view(Evas_Object *obj)
395 Elm_Flipselector_Item *item;
397 wd = elm_widget_data_get(obj);
401 item = DATA_GET(wd->current);
402 if (item) label = item->label;
404 edje_object_part_text_set(wd->base, "top", label ? label : "");
405 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
406 edje_object_message_signal_process(wd->base);
410 _changed(Widget_Data *wd)
412 Elm_Flipselector_Item *item;
414 item = DATA_GET(wd->current);
418 item->func((void *)item->base.data, WIDGET(item), item);
420 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
424 _send_msg(Widget_Data *wd, int flipside, char *label)
426 Edje_Message_String msg;
429 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
430 edje_object_message_signal_process(wd->base);
436 _flip_up(Widget_Data *wd)
438 Elm_Flipselector_Item *item;
440 if (!wd->current) return;
442 if (wd->current == wd->items)
444 wd->current = eina_list_last(wd->items);
445 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
448 wd->current = eina_list_prev(wd->current);
450 item = DATA_GET(wd->current);
453 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
457 _signal_val_up(void *data)
459 Widget_Data *wd = elm_widget_data_get(data);
461 if (!wd) goto val_up_exit_on_error;
463 _flipselector_walk(wd);
465 if (wd->interval > FLIP_MIN_INTERVAL)
466 wd->interval = wd->interval / 1.05;
468 ecore_timer_interval_set(wd->spin, wd->interval);
472 _flipselector_unwalk(wd);
474 return ECORE_CALLBACK_RENEW;
476 val_up_exit_on_error:
477 return ECORE_CALLBACK_CANCEL;
481 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
483 Widget_Data *wd = elm_widget_data_get(data);
486 wd->interval = wd->first_interval;
488 if (wd->spin) ecore_timer_del(wd->spin);
489 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
491 _signal_val_up(data);
495 _flip_down(Widget_Data *wd)
497 Elm_Flipselector_Item *item;
499 if (!wd->current) return;
501 wd->current = eina_list_next(wd->current);
504 wd->current = wd->items;
505 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
508 item = DATA_GET(wd->current);
511 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
515 _signal_val_down(void *data)
517 Widget_Data *wd = elm_widget_data_get(data);
519 if (!wd) goto val_down_exit_on_error;
521 _flipselector_walk(wd);
523 if (wd->interval > FLIP_MIN_INTERVAL)
524 wd->interval = wd->interval / 1.05;
525 ecore_timer_interval_set(wd->spin, wd->interval);
529 _flipselector_unwalk(wd);
531 return ECORE_CALLBACK_RENEW;
533 val_down_exit_on_error:
534 return ECORE_CALLBACK_CANCEL;
538 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
540 Widget_Data *wd = elm_widget_data_get(data);
543 wd->interval = wd->first_interval;
545 if (wd->spin) ecore_timer_del(wd->spin);
546 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
548 _signal_val_down(data);
552 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
554 Widget_Data *wd = elm_widget_data_get(data);
557 if (wd->spin) ecore_timer_del(wd->spin);
562 _callbacks_set(Evas_Object *obj)
564 Widget_Data *wd = elm_widget_data_get(obj);
566 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
567 "", _signal_val_up_start, obj);
568 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
569 "", _signal_val_change_stop, obj);
570 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
571 "", _signal_val_down_start, obj);
572 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
573 "", _signal_val_change_stop, obj);
577 _item_del_pre_hook(Elm_Object_Item *it)
580 Elm_Flipselector_Item *item, *item2;
583 item = (Elm_Flipselector_Item *)it;
584 wd = elm_widget_data_get(WIDGET(item));
585 if (!wd) return EINA_FALSE;
589 item->deleted = EINA_TRUE;
593 _flipselector_walk(wd);
595 EINA_LIST_FOREACH(wd->items, l, item2)
599 wd->items = eina_list_remove_list(wd->items, l);
600 if (wd->current == l)
602 wd->current = l->prev;
603 if (!wd->current) wd->current = l->next;
606 item2 = wd->current->data;
607 _send_msg(wd, MSG_FLIP_DOWN, (char *)item2->label);
610 _send_msg(wd, MSG_FLIP_DOWN, "");
615 eina_stringshare_del(item->label);
617 _flipselector_unwalk(wd);
623 elm_flipselector_add(Evas_Object *parent)
629 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
631 ELM_SET_WIDTYPE(widtype, "flipselector");
632 elm_widget_type_set(obj, "flipselector");
633 elm_widget_sub_object_add(parent, obj);
634 elm_widget_data_set(obj, wd);
637 elm_widget_del_hook_set(obj, _del_hook);
638 elm_widget_theme_hook_set(obj, _theme_hook);
639 elm_widget_disable_hook_set(obj, _disable_hook);
641 elm_widget_can_focus_set(obj, EINA_TRUE);
642 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
643 elm_widget_event_hook_set(obj, _event_hook);
645 wd->base = edje_object_add(e);
646 elm_widget_resize_object_set(obj, wd->base);
650 wd->first_interval = FLIP_FIRST_INTERVAL;
654 evas_object_smart_callbacks_descriptions_set(obj, _signals);
659 elm_flipselector_flip_next(Evas_Object *obj)
661 ELM_CHECK_WIDTYPE(obj, widtype);
663 Widget_Data *wd = elm_widget_data_get(obj);
666 if (wd->spin) ecore_timer_del(wd->spin);
669 _flipselector_walk(wd);
671 _flipselector_unwalk(wd);
675 elm_flipselector_flip_prev(Evas_Object *obj)
677 ELM_CHECK_WIDTYPE(obj, widtype);
679 Widget_Data *wd = elm_widget_data_get(obj);
682 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 EAPI const Eina_List *
761 elm_flipselector_items_get(const Evas_Object *obj)
763 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
765 Widget_Data *wd = elm_widget_data_get(obj);
766 if (!wd) return NULL;
770 EAPI Elm_Object_Item *
771 elm_flipselector_first_item_get(const Evas_Object *obj)
773 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
775 Elm_Flipselector_Item *it;
779 wd = elm_widget_data_get(obj);
780 if (!wd || !wd->items) return NULL;
782 EINA_LIST_FOREACH(wd->items, l, it)
784 if (it->deleted) continue;
785 return (Elm_Object_Item *)it;
790 EAPI Elm_Object_Item *
791 elm_flipselector_last_item_get(const Evas_Object *obj)
793 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
795 Elm_Flipselector_Item *it;
799 wd = elm_widget_data_get(obj);
800 if (!wd || !wd->items) return NULL;
802 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
804 if (it->deleted) continue;
805 return (Elm_Object_Item *)it;
810 EAPI Elm_Object_Item *
811 elm_flipselector_selected_item_get(const Evas_Object *obj)
813 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
815 Widget_Data *wd = elm_widget_data_get(obj);
816 if (!wd) return NULL;
817 return DATA_GET(wd->current);
821 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
823 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
825 Elm_Flipselector_Item *item, *_item, *cur;
826 int flipside = MSG_FLIP_UP;
830 item = (Elm_Flipselector_Item *)it;
831 wd = elm_widget_data_get(WIDGET(item));
834 cur = DATA_GET(wd->current);
835 if ((selected) && (cur == item)) return;
837 _flipselector_walk(wd);
839 if ((!selected) && (cur == item))
841 EINA_LIST_FOREACH(wd->items, l, _item)
846 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
850 _flipselector_unwalk(wd);
854 EINA_LIST_FOREACH(wd->items, l, _item)
856 if (_item == cur) flipside = MSG_FLIP_DOWN;
861 _send_msg(wd, flipside, (char *)item->label);
866 _flipselector_unwalk(wd);
870 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
872 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
874 Elm_Flipselector_Item *item;
876 item = (Elm_Flipselector_Item *)it;
877 wd = elm_widget_data_get(WIDGET(item));
878 if (!wd) return EINA_FALSE;
879 return (eina_list_data_get(wd->current) == item);
882 EAPI Elm_Object_Item *
883 elm_flipselector_item_prev_get(const Elm_Object_Item *it)
885 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
889 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *)it;
891 wd = elm_widget_data_get(WIDGET(item));
892 if ((!wd) || (!wd->items)) return NULL;
894 l = eina_list_data_find_list(wd->items, it);
895 if (l && l->prev) return DATA_GET(l->prev);
900 EAPI Elm_Object_Item *
901 elm_flipselector_item_next_get(const Elm_Object_Item *it)
903 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
907 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *)it;
909 wd = elm_widget_data_get(WIDGET(item));
910 if ((!wd) || (!wd->items)) return NULL;
912 l = eina_list_data_find_list(wd->items, it);
913 if (l && l->next) return DATA_GET(l->next);
919 elm_flipselector_first_interval_set(Evas_Object *obj, double interval)
921 ELM_CHECK_WIDTYPE(obj, widtype);
923 Widget_Data *wd = elm_widget_data_get(obj);
925 wd->first_interval = interval;
929 elm_flipselector_first_interval_get(const Evas_Object *obj)
931 ELM_CHECK_WIDTYPE(obj, widtype) 0;
933 Widget_Data *wd = elm_widget_data_get(obj);
935 return wd->first_interval;