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,
76 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
80 Elm_Flipselector_Item *item;
84 if (part && strcmp(part ,"default")) return;
86 item = (Elm_Flipselector_Item *) it;
87 wd = elm_widget_data_get(WIDGET(item));
88 if ((!wd) || (!wd->items)) return;
90 l = eina_list_data_find_list(wd->items, item);
93 eina_stringshare_del(item->label);
94 item->label = eina_stringshare_add_length(label, wd->max_len);
96 if (strlen(label) > strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
101 _update_view(WIDGET(item));
102 _sizing_eval(wd->self);
107 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
109 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
111 if (part && strcmp(part ,"default")) return NULL;
113 return ((Elm_Flipselector_Item *) it)->label;
117 _item_signal_emit_hook(Elm_Object_Item *it,
118 const char *emission,
121 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
122 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
123 edje_object_signal_emit(VIEW(item), emission, source);
126 static Elm_Flipselector_Item *
127 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
130 Elm_Flipselector_Item *it;
131 Widget_Data *wd = elm_widget_data_get(obj);
133 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
134 if (!it) return NULL;
136 elm_widget_item_del_pre_hook_set(it, _item_del_pre_hook);
137 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
138 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
139 elm_widget_item_signal_emit_hook_set(it, _item_signal_emit_hook);
142 if (len > wd->max_len)
145 it->label = eina_stringshare_add_length(label, len);
147 it->base.data = data;
149 /* TODO: no view here, but if one desires general contents in the
155 _item_free(Elm_Flipselector_Item *it)
157 eina_stringshare_del(it->label);
158 elm_widget_item_free(it);
162 _del_hook(Evas_Object *obj)
164 Elm_Flipselector_Item *item;
166 Widget_Data *wd = elm_widget_data_get(obj);
169 if (wd->walking) ERR("flipselector deleted while walking.\n");
171 EINA_LIST_FREE(wd->items, item)
174 if (wd->spin) ecore_timer_del(wd->spin);
179 _disable_hook(Evas_Object *obj)
181 Widget_Data *wd = elm_widget_data_get(obj);
184 if (elm_widget_disabled_get(obj))
185 edje_object_signal_emit(wd->base, "elm,state,disabled", "elm");
187 edje_object_signal_emit(wd->base, "elm,state,enabled", "elm");
191 _theme_hook(Evas_Object *obj)
196 wd = elm_widget_data_get(obj);
199 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
200 elm_widget_style_get(obj));
201 edje_object_scale_set(wd->base,
202 elm_widget_scale_get(obj) * _elm_config->scale);
204 max_len = edje_object_data_get(wd->base, "max_len");
206 wd->max_len = MAX_LEN_DEFAULT;
209 wd->max_len = atoi(max_len);
211 wd->max_len = MAX_LEN_DEFAULT;
219 _sentinel_eval(Widget_Data *wd)
221 Elm_Flipselector_Item *it;
230 wd->sentinel = wd->items;
232 EINA_LIST_FOREACH(wd->items, l, it)
234 if (strlen(elm_object_item_text_get((Elm_Object_Item *) it)) >
235 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
240 /* TODO: create a flag to avoid looping here all times */
242 _flipselector_process_deletions(Widget_Data *wd)
244 Elm_Flipselector_Item *it;
246 Eina_Bool skip = EINA_TRUE;
247 Eina_Bool sentinel_eval = EINA_FALSE;
249 wd->walking++; /* avoid nested deletions */
251 EINA_LIST_FOREACH(wd->items, l, it)
253 if (!it->deleted) continue;
255 if (wd->current == l)
257 if (wd->current == wd->sentinel)
258 sentinel_eval = EINA_TRUE;
259 wd->current = eina_list_prev(wd->current);
261 wd->items = eina_list_remove(wd->items, it);
264 wd->current = wd->items;
269 if (eina_list_count(wd->items) <= 1)
270 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
272 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
276 _update_view(wd->self);
285 _flipselector_walk(Widget_Data *wd)
289 ERR("walking was negative. fixed!\n");
296 _flipselector_unwalk(Widget_Data *wd)
301 ERR("walking became negative. fixed!\n");
304 if (wd->walking) return;
306 _flipselector_process_deletions(wd);
310 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
312 Evas_Event_Key_Down *ev;
314 Eina_Bool is_up = EINA_TRUE;
316 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
318 wd = elm_widget_data_get(obj);
319 if (!wd) return EINA_FALSE;
322 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
324 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
326 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
328 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
331 if (wd->spin) ecore_timer_del(wd->spin);
333 /* TODO: if direction setting via API is not coming in, replace
334 these calls by flip_{next,prev} */
335 _flipselector_walk(wd);
336 if (is_up) _flip_up(wd);
338 _flipselector_unwalk(wd);
340 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
345 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
347 Widget_Data *wd = elm_widget_data_get(obj);
350 /* FIXME: no treatment of this signal so far */
351 if (elm_widget_focus_get(obj))
353 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
354 evas_object_focus_set(wd->base, EINA_TRUE);
358 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
359 evas_object_focus_set(wd->base, EINA_FALSE);
364 _sizing_eval(Evas_Object *obj)
367 const char *tmp = NULL;
368 Evas_Coord minw = -1, minh = -1, w, h;
370 wd = elm_widget_data_get(obj);
373 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
377 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
378 tmp = edje_object_part_text_get(wd->base, "top");
379 edje_object_part_text_set(wd->base, "top", label);
382 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
383 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
384 evas_object_size_hint_min_get(obj, &w, &h);
386 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
388 if (w > minw) minw = w;
389 if (h > minh) minh = h;
391 evas_object_size_hint_min_set(obj, minw, minh);
395 _update_view(Evas_Object *obj)
399 Elm_Flipselector_Item *item;
401 wd = elm_widget_data_get(obj);
405 item = DATA_GET(wd->current);
406 if (item) label = item->label;
408 edje_object_part_text_set(wd->base, "top", label ? label : "");
409 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
410 edje_object_message_signal_process(wd->base);
414 _changed(Widget_Data *wd)
416 Elm_Flipselector_Item *item;
418 item = DATA_GET(wd->current);
422 item->func((void *)item->base.data, WIDGET(item), item);
424 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
428 _send_msg(Widget_Data *wd, int flipside, char *label)
430 Edje_Message_String msg;
433 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
434 edje_object_message_signal_process(wd->base);
440 _flip_up(Widget_Data *wd)
442 Elm_Flipselector_Item *item;
444 if (!wd->current) return;
446 if (wd->current == wd->items)
448 wd->current = eina_list_last(wd->items);
449 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
452 wd->current = eina_list_prev(wd->current);
454 item = DATA_GET(wd->current);
457 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
461 _signal_val_up(void *data)
463 Widget_Data *wd = elm_widget_data_get(data);
465 if (!wd) goto val_up_exit_on_error;
467 _flipselector_walk(wd);
469 if (wd->interval > FLIP_MIN_INTERVAL)
470 wd->interval = wd->interval / 1.05;
472 ecore_timer_interval_set(wd->spin, wd->interval);
476 _flipselector_unwalk(wd);
478 return ECORE_CALLBACK_RENEW;
480 val_up_exit_on_error:
481 return ECORE_CALLBACK_CANCEL;
485 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
487 Widget_Data *wd = elm_widget_data_get(data);
490 wd->interval = wd->first_interval;
492 if (wd->spin) ecore_timer_del(wd->spin);
493 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
495 _signal_val_up(data);
499 _flip_down(Widget_Data *wd)
501 Elm_Flipselector_Item *item;
503 if (!wd->current) return;
505 wd->current = eina_list_next(wd->current);
508 wd->current = wd->items;
509 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
512 item = DATA_GET(wd->current);
515 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
519 _signal_val_down(void *data)
521 Widget_Data *wd = elm_widget_data_get(data);
523 if (!wd) goto val_down_exit_on_error;
525 _flipselector_walk(wd);
527 if (wd->interval > FLIP_MIN_INTERVAL)
528 wd->interval = wd->interval / 1.05;
529 ecore_timer_interval_set(wd->spin, wd->interval);
533 _flipselector_unwalk(wd);
535 return ECORE_CALLBACK_RENEW;
537 val_down_exit_on_error:
538 return ECORE_CALLBACK_CANCEL;
542 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
544 Widget_Data *wd = elm_widget_data_get(data);
547 wd->interval = wd->first_interval;
549 if (wd->spin) ecore_timer_del(wd->spin);
550 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
552 _signal_val_down(data);
556 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
558 Widget_Data *wd = elm_widget_data_get(data);
561 if (wd->spin) ecore_timer_del(wd->spin);
566 _callbacks_set(Evas_Object *obj)
568 Widget_Data *wd = elm_widget_data_get(obj);
570 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
571 "", _signal_val_up_start, obj);
572 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
573 "", _signal_val_change_stop, obj);
574 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
575 "", _signal_val_down_start, obj);
576 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
577 "", _signal_val_change_stop, obj);
581 _item_del_pre_hook(Elm_Object_Item *it)
583 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
585 Elm_Flipselector_Item *item, *item2;
588 item = (Elm_Flipselector_Item *) it;
589 wd = elm_widget_data_get(WIDGET(item));
590 if (!wd) return EINA_FALSE;
594 item->deleted = EINA_TRUE;
598 _flipselector_walk(wd);
600 EINA_LIST_FOREACH(wd->items, l, item2)
604 wd->items = eina_list_remove_list(wd->items, l);
605 if (wd->current == l)
607 wd->current = l->prev;
608 if (!wd->current) wd->current = l->next;
611 item2 = wd->current->data;
612 _send_msg(wd, MSG_FLIP_DOWN, (char *)item2->label);
615 _send_msg(wd, MSG_FLIP_DOWN, "");
620 eina_stringshare_del(item->label);
622 _flipselector_unwalk(wd);
628 elm_flipselector_add(Evas_Object *parent)
634 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
636 ELM_SET_WIDTYPE(widtype, "flipselector");
637 elm_widget_type_set(obj, "flipselector");
638 elm_widget_sub_object_add(parent, obj);
639 elm_widget_data_set(obj, wd);
642 elm_widget_del_hook_set(obj, _del_hook);
643 elm_widget_theme_hook_set(obj, _theme_hook);
644 elm_widget_disable_hook_set(obj, _disable_hook);
646 elm_widget_can_focus_set(obj, EINA_TRUE);
647 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
648 elm_widget_event_hook_set(obj, _event_hook);
650 wd->base = edje_object_add(e);
651 elm_widget_resize_object_set(obj, wd->base);
655 wd->first_interval = FLIP_FIRST_INTERVAL;
659 evas_object_smart_callbacks_descriptions_set(obj, _signals);
664 elm_flipselector_flip_next(Evas_Object *obj)
666 ELM_CHECK_WIDTYPE(obj, widtype);
668 Widget_Data *wd = elm_widget_data_get(obj);
671 if (wd->spin) ecore_timer_del(wd->spin);
673 _flipselector_walk(wd);
675 _flipselector_unwalk(wd);
679 elm_flipselector_flip_prev(Evas_Object *obj)
681 ELM_CHECK_WIDTYPE(obj, widtype);
683 Widget_Data *wd = elm_widget_data_get(obj);
686 if (wd->spin) ecore_timer_del(wd->spin);
688 _flipselector_walk(wd);
690 _flipselector_unwalk(wd);
693 EAPI Elm_Object_Item *
694 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
696 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
698 Elm_Flipselector_Item *item;
701 wd = elm_widget_data_get(obj);
702 if (!wd) return NULL;
704 item = _item_new(obj, label, func, data);
705 if (!item) return NULL;
707 wd->items = eina_list_append(wd->items, item);
710 wd->current = wd->items;
715 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
716 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
718 wd->sentinel = eina_list_last(wd->items);
722 if (eina_list_count(wd->items) >= 2)
723 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
725 return (Elm_Object_Item *) item;
728 EAPI Elm_Object_Item *
729 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
731 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
733 Elm_Flipselector_Item *item;
736 wd = elm_widget_data_get(obj);
737 if (!wd) return NULL;
739 item = _item_new(obj, label, func, data);
740 if (!item) return NULL;
742 wd->items = eina_list_prepend(wd->items, item);
745 wd->current = wd->items;
750 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
751 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
753 wd->sentinel = wd->items;
757 if (eina_list_count(wd->items) >= 2)
758 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
760 return (Elm_Object_Item *) item;
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) 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(const Elm_Object_Item *it)
906 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
910 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
912 wd = elm_widget_data_get(WIDGET(item));
913 if ((!wd) || (!wd->items)) return NULL;
915 l = eina_list_data_find_list(wd->items, it);
916 if (l && l->prev) return DATA_GET(l->prev);
921 EAPI Elm_Object_Item *
922 elm_flipselector_item_next_get(const Elm_Object_Item *it)
924 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
928 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
930 wd = elm_widget_data_get(WIDGET(item));
931 if ((!wd) || (!wd->items)) return NULL;
933 l = eina_list_data_find_list(wd->items, it);
934 if (l && l->next) return DATA_GET(l->next);
940 elm_flipselector_interval_set(Evas_Object *obj, double interval)
942 ELM_CHECK_WIDTYPE(obj, widtype);
944 Widget_Data *wd = elm_widget_data_get(obj);
946 wd->first_interval = interval;
950 elm_flipselector_interval_get(const Evas_Object *obj)
952 ELM_CHECK_WIDTYPE(obj, widtype) 0;
954 Widget_Data *wd = elm_widget_data_get(obj);
956 return wd->first_interval;