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 Eina_Bool _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 if (part && strcmp(part ,"default")) return NULL;
114 return ((Elm_Flipselector_Item *) it)->label;
118 _item_signal_emit_hook(Elm_Object_Item *it,
119 const char *emission,
122 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
123 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
124 edje_object_signal_emit(VIEW(item), emission, source);
127 static Elm_Flipselector_Item *
128 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
131 Elm_Flipselector_Item *it;
132 Widget_Data *wd = elm_widget_data_get(obj);
134 it = elm_widget_item_new(obj, Elm_Flipselector_Item);
135 if (!it) return NULL;
137 elm_widget_item_del_pre_hook_set(it, _item_del_pre_hook);
138 elm_widget_item_text_set_hook_set(it, _item_text_set_hook);
139 elm_widget_item_text_get_hook_set(it, _item_text_get_hook);
140 elm_widget_item_signal_emit_hook_set(it, _item_signal_emit_hook);
143 if (len > wd->max_len)
146 it->label = eina_stringshare_add_length(label, len);
148 it->base.data = data;
150 /* TODO: no view here, but if one desires general contents in the
156 _item_free(Elm_Flipselector_Item *it)
158 eina_stringshare_del(it->label);
159 elm_widget_item_free(it);
163 _del_hook(Evas_Object *obj)
165 Elm_Flipselector_Item *item;
167 Widget_Data *wd = elm_widget_data_get(obj);
170 if (wd->walking) ERR("flipselector deleted while walking.\n");
172 EINA_LIST_FREE(wd->items, item)
175 if (wd->spin) ecore_timer_del(wd->spin);
180 _theme_hook(Evas_Object *obj)
185 wd = elm_widget_data_get(obj);
188 _elm_theme_object_set(obj, wd->base, "flipselector", "base",
189 elm_widget_style_get(obj));
190 edje_object_scale_set(wd->base,
191 elm_widget_scale_get(obj) * _elm_config->scale);
193 max_len = edje_object_data_get(wd->base, "max_len");
195 wd->max_len = MAX_LEN_DEFAULT;
198 wd->max_len = atoi(max_len);
200 wd->max_len = MAX_LEN_DEFAULT;
208 _sentinel_eval(Widget_Data *wd)
210 Elm_Flipselector_Item *it;
219 wd->sentinel = wd->items;
221 EINA_LIST_FOREACH(wd->items, l, it)
223 if (strlen(elm_object_item_text_get((Elm_Object_Item *) it)) >
224 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel))))
229 /* TODO: create a flag to avoid looping here all times */
231 _flipselector_process_deletions(Widget_Data *wd)
233 Elm_Flipselector_Item *it;
235 Eina_Bool skip = EINA_TRUE;
236 Eina_Bool sentinel_eval = EINA_FALSE;
238 wd->walking++; /* avoid nested deletions */
240 EINA_LIST_FOREACH(wd->items, l, it)
242 if (!it->deleted) continue;
244 if (wd->current == l)
246 if (wd->current == wd->sentinel)
247 sentinel_eval = EINA_TRUE;
248 wd->current = eina_list_prev(wd->current);
250 wd->items = eina_list_remove(wd->items, it);
253 wd->current = wd->items;
258 if (eina_list_count(wd->items) <= 1)
259 edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
261 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
265 _update_view(wd->self);
274 _flipselector_walk(Widget_Data *wd)
278 ERR("walking was negative. fixed!\n");
285 _flipselector_unwalk(Widget_Data *wd)
290 ERR("walking became negative. fixed!\n");
293 if (wd->walking) return;
295 _flipselector_process_deletions(wd);
299 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
301 Evas_Event_Key_Down *ev;
303 Eina_Bool is_up = EINA_TRUE;
305 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
307 wd = elm_widget_data_get(obj);
308 if (!wd) return EINA_FALSE;
311 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
313 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
315 if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
317 else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
320 if (wd->spin) ecore_timer_del(wd->spin);
322 /* TODO: if direction setting via API is not coming in, replace
323 these calls by flip_{next,prev} */
324 _flipselector_walk(wd);
325 if (is_up) _flip_up(wd);
327 _flipselector_unwalk(wd);
329 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
334 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
336 Widget_Data *wd = elm_widget_data_get(obj);
339 /* FIXME: no treatment of this signal so far */
340 if (elm_widget_focus_get(obj))
342 edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
343 evas_object_focus_set(wd->base, EINA_TRUE);
347 edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
348 evas_object_focus_set(wd->base, EINA_FALSE);
353 _sizing_eval(Evas_Object *obj)
356 const char *tmp = NULL;
357 Evas_Coord minw = -1, minh = -1, w, h;
359 wd = elm_widget_data_get(obj);
362 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
366 const char *label = elm_object_item_text_get(DATA_GET(wd->sentinel));
367 tmp = edje_object_part_text_get(wd->base, "top");
368 edje_object_part_text_set(wd->base, "top", label);
371 edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
372 elm_coords_finger_size_adjust(1, &minw, 2, &minh);
373 evas_object_size_hint_min_get(obj, &w, &h);
375 if (wd->sentinel) edje_object_part_text_set(wd->base, "top", tmp);
377 if (w > minw) minw = w;
378 if (h > minh) minh = h;
380 evas_object_size_hint_min_set(obj, minw, minh);
384 _update_view(Evas_Object *obj)
388 Elm_Flipselector_Item *item;
390 wd = elm_widget_data_get(obj);
394 item = DATA_GET(wd->current);
395 if (item) label = item->label;
397 edje_object_part_text_set(wd->base, "top", label ? label : "");
398 edje_object_part_text_set(wd->base, "bottom", label ? label : "");
399 edje_object_message_signal_process(wd->base);
403 _changed(Widget_Data *wd)
405 Elm_Flipselector_Item *item;
407 item = DATA_GET(wd->current);
411 item->func((void *)item->base.data, WIDGET(item), item);
413 evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
417 _send_msg(Widget_Data *wd, int flipside, char *label)
419 Edje_Message_String msg;
422 edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
423 edje_object_message_signal_process(wd->base);
429 _flip_up(Widget_Data *wd)
431 Elm_Flipselector_Item *item;
433 if (!wd->current) return;
435 if (wd->current == wd->items)
437 wd->current = eina_list_last(wd->items);
438 evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
441 wd->current = eina_list_prev(wd->current);
443 item = DATA_GET(wd->current);
446 _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
450 _signal_val_up(void *data)
452 Widget_Data *wd = elm_widget_data_get(data);
454 if (!wd) goto val_up_exit_on_error;
456 _flipselector_walk(wd);
458 if (wd->interval > FLIP_MIN_INTERVAL)
459 wd->interval = wd->interval / 1.05;
461 ecore_timer_interval_set(wd->spin, wd->interval);
465 _flipselector_unwalk(wd);
467 return ECORE_CALLBACK_RENEW;
469 val_up_exit_on_error:
470 return ECORE_CALLBACK_CANCEL;
474 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
476 Widget_Data *wd = elm_widget_data_get(data);
479 wd->interval = wd->first_interval;
481 if (wd->spin) ecore_timer_del(wd->spin);
482 wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
484 _signal_val_up(data);
488 _flip_down(Widget_Data *wd)
490 Elm_Flipselector_Item *item;
492 if (!wd->current) return;
494 wd->current = eina_list_next(wd->current);
497 wd->current = wd->items;
498 evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
501 item = DATA_GET(wd->current);
504 _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
508 _signal_val_down(void *data)
510 Widget_Data *wd = elm_widget_data_get(data);
512 if (!wd) goto val_down_exit_on_error;
514 _flipselector_walk(wd);
516 if (wd->interval > FLIP_MIN_INTERVAL)
517 wd->interval = wd->interval / 1.05;
518 ecore_timer_interval_set(wd->spin, wd->interval);
522 _flipselector_unwalk(wd);
524 return ECORE_CALLBACK_RENEW;
526 val_down_exit_on_error:
527 return ECORE_CALLBACK_CANCEL;
531 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
533 Widget_Data *wd = elm_widget_data_get(data);
536 wd->interval = wd->first_interval;
538 if (wd->spin) ecore_timer_del(wd->spin);
539 wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
541 _signal_val_down(data);
545 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
547 Widget_Data *wd = elm_widget_data_get(data);
550 if (wd->spin) ecore_timer_del(wd->spin);
555 _callbacks_set(Evas_Object *obj)
557 Widget_Data *wd = elm_widget_data_get(obj);
559 edje_object_signal_callback_add(wd->base, "elm,action,up,start",
560 "", _signal_val_up_start, obj);
561 edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
562 "", _signal_val_change_stop, obj);
563 edje_object_signal_callback_add(wd->base, "elm,action,down,start",
564 "", _signal_val_down_start, obj);
565 edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
566 "", _signal_val_change_stop, obj);
570 _item_del_pre_hook(Elm_Object_Item *it)
572 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
574 Elm_Flipselector_Item *item, *item2;
577 item = (Elm_Flipselector_Item *) it;
578 wd = elm_widget_data_get(WIDGET(item));
579 if (!wd) return EINA_FALSE;
583 item->deleted = EINA_TRUE;
587 _flipselector_walk(wd);
589 EINA_LIST_FOREACH(wd->items, l, item2)
593 wd->items = eina_list_remove_list(wd->items, l);
594 if (wd->current == l)
596 wd->current = l->prev;
597 if (!wd->current) wd->current = l->next;
600 item2 = wd->current->data;
601 _send_msg(wd, MSG_FLIP_DOWN, (char *)item2->label);
604 _send_msg(wd, MSG_FLIP_DOWN, "");
609 eina_stringshare_del(item->label);
611 _flipselector_unwalk(wd);
617 elm_flipselector_add(Evas_Object *parent)
623 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
625 ELM_SET_WIDTYPE(widtype, "flipselector");
626 elm_widget_type_set(obj, "flipselector");
627 elm_widget_sub_object_add(parent, obj);
628 elm_widget_data_set(obj, wd);
631 elm_widget_del_hook_set(obj, _del_hook);
632 elm_widget_theme_hook_set(obj, _theme_hook);
634 elm_widget_can_focus_set(obj, EINA_TRUE);
635 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
636 elm_widget_event_hook_set(obj, _event_hook);
638 wd->base = edje_object_add(e);
639 elm_widget_resize_object_set(obj, wd->base);
643 wd->first_interval = FLIP_FIRST_INTERVAL;
647 evas_object_smart_callbacks_descriptions_set(obj, _signals);
652 elm_flipselector_flip_next(Evas_Object *obj)
654 ELM_CHECK_WIDTYPE(obj, widtype);
656 Widget_Data *wd = elm_widget_data_get(obj);
659 if (wd->spin) ecore_timer_del(wd->spin);
661 _flipselector_walk(wd);
663 _flipselector_unwalk(wd);
667 elm_flipselector_flip_prev(Evas_Object *obj)
669 ELM_CHECK_WIDTYPE(obj, widtype);
671 Widget_Data *wd = elm_widget_data_get(obj);
674 if (wd->spin) ecore_timer_del(wd->spin);
676 _flipselector_walk(wd);
678 _flipselector_unwalk(wd);
681 EAPI Elm_Object_Item *
682 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
684 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
686 Elm_Flipselector_Item *item;
689 wd = elm_widget_data_get(obj);
690 if (!wd) return NULL;
692 item = _item_new(obj, label, func, data);
693 if (!item) return NULL;
695 wd->items = eina_list_append(wd->items, item);
698 wd->current = wd->items;
703 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
704 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
706 wd->sentinel = eina_list_last(wd->items);
710 if (eina_list_count(wd->items) >= 2)
711 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
713 return (Elm_Object_Item *) item;
716 EAPI Elm_Object_Item *
717 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
719 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
721 Elm_Flipselector_Item *item;
724 wd = elm_widget_data_get(obj);
725 if (!wd) return NULL;
727 item = _item_new(obj, label, func, data);
728 if (!item) return NULL;
730 wd->items = eina_list_prepend(wd->items, item);
733 wd->current = wd->items;
738 (strlen(elm_object_item_text_get((Elm_Object_Item *) item)) >
739 strlen(elm_object_item_text_get(DATA_GET(wd->sentinel)))))
741 wd->sentinel = wd->items;
745 if (eina_list_count(wd->items) >= 2)
746 edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
748 return (Elm_Object_Item *) item;
751 EAPI const Eina_List *
752 elm_flipselector_items_get(const Evas_Object *obj)
754 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
756 Widget_Data *wd = elm_widget_data_get(obj);
757 if (!wd) return NULL;
761 EAPI Elm_Object_Item *
762 elm_flipselector_first_item_get(const Evas_Object *obj)
764 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
766 Elm_Flipselector_Item *it;
770 wd = elm_widget_data_get(obj);
771 if (!wd || !wd->items) return NULL;
773 EINA_LIST_FOREACH(wd->items, l, it)
775 if (it->deleted) continue;
776 return (Elm_Object_Item *) it;
781 EAPI Elm_Object_Item *
782 elm_flipselector_last_item_get(const Evas_Object *obj)
784 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
786 Elm_Flipselector_Item *it;
790 wd = elm_widget_data_get(obj);
791 if (!wd || !wd->items) return NULL;
793 EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
795 if (it->deleted) continue;
796 return (Elm_Object_Item *) it;
801 EAPI Elm_Object_Item *
802 elm_flipselector_selected_item_get(const Evas_Object *obj)
804 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
806 Widget_Data *wd = elm_widget_data_get(obj);
807 if (!wd) return NULL;
808 return DATA_GET(wd->current);
812 elm_flipselector_item_selected_set(Elm_Object_Item *it, Eina_Bool selected)
814 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
816 Elm_Flipselector_Item *item, *_item, *cur;
817 int flipside = MSG_FLIP_UP;
821 item = (Elm_Flipselector_Item *) it;
822 wd = elm_widget_data_get(WIDGET(item));
825 cur = DATA_GET(wd->current);
826 if ((selected) && (cur == item)) return;
828 _flipselector_walk(wd);
830 if ((!selected) && (cur == item))
832 EINA_LIST_FOREACH(wd->items, l, _item)
837 _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
841 _flipselector_unwalk(wd);
845 EINA_LIST_FOREACH(wd->items, l, _item)
847 if (_item == cur) flipside = MSG_FLIP_DOWN;
852 _send_msg(wd, flipside, (char *)item->label);
857 _flipselector_unwalk(wd);
861 elm_flipselector_item_selected_get(const Elm_Object_Item *it)
863 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
865 Elm_Flipselector_Item *item;
867 item = (Elm_Flipselector_Item *) it;
868 wd = elm_widget_data_get(WIDGET(item));
869 if (!wd) return EINA_FALSE;
870 return (eina_list_data_get(wd->current) == item);
874 elm_flipselector_item_del(Elm_Object_Item *it)
876 elm_object_item_del(it);
880 elm_flipselector_item_label_get(const Elm_Object_Item *it)
882 return _item_text_get_hook(it, NULL);
886 elm_flipselector_item_label_set(Elm_Object_Item *it, const char *label)
888 _item_text_set_hook(it, NULL, label);
891 EAPI Elm_Object_Item *
892 elm_flipselector_item_prev_get(const Elm_Object_Item *it)
894 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
898 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
900 wd = elm_widget_data_get(WIDGET(item));
901 if ((!wd) || (!wd->items)) return NULL;
903 l = eina_list_data_find_list(wd->items, it);
904 if (l && l->prev) return DATA_GET(l->prev);
909 EAPI Elm_Object_Item *
910 elm_flipselector_item_next_get(const Elm_Object_Item *it)
912 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
916 Elm_Flipselector_Item *item = (Elm_Flipselector_Item *) it;
918 wd = elm_widget_data_get(WIDGET(item));
919 if ((!wd) || (!wd->items)) return NULL;
921 l = eina_list_data_find_list(wd->items, it);
922 if (l && l->next) return DATA_GET(l->next);
928 elm_flipselector_interval_set(Evas_Object *obj, double interval)
930 ELM_CHECK_WIDTYPE(obj, widtype);
932 Widget_Data *wd = elm_widget_data_get(obj);
934 wd->first_interval = interval;
938 elm_flipselector_interval_get(const Evas_Object *obj)
940 ELM_CHECK_WIDTYPE(obj, widtype) 0;
942 Widget_Data *wd = elm_widget_data_get(obj);
944 return wd->first_interval;