================================================================
[framework/uifw/elementary.git] / src / lib / elm_flipselector.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @addtogroup Flipselector Flipselector
6  *
7  * A flip selector is a widget to show a set of label items, one at a
8  * time, with an animation when one changes the current selection
9  * (like the flip of calendar sheets, in the default theme).
10  */
11
12 /* TODO: ideally, the default theme would use map{} blocks on the TEXT
13    parts to implement their fading in/out propertly (as in the clock
14    widget) */
15 /* TODO: if one ever wants to extend it to receiving generic widgets
16    as items, be my guest. in this case, remember to implement the
17    items tooltip infra. */
18 /* TODO: implement disabled mode -- disable_hook() and stuff. */
19 /* TODO: fix default theme image borders for looong strings as item
20    labels. */
21 /* TODO: set text elipsis on labels if one enforces mininum size on
22  * the overall widget less the required for displaying it. */
23 /* TODO: find a way to, in the default theme, to detect we are
24  * bootstrapping (receiving the 1st message) and populate the downmost
25  * TEXT parts with the same text as the upmost, where appropriate. */
26
27 #define FLIP_FIRST_INTERVAL (0.85)
28 #define FLIP_MIN_INTERVAL (0.1)
29 #define MSG_FLIP_DOWN (1)
30 #define MSG_FLIP_UP (2)
31 #define MAX_LEN_DEFAULT (50)
32
33 #define DATA_GET eina_list_data_get
34
35 struct _Elm_Flipselector_Item {
36    Elm_Widget_Item base;
37    const char *label;
38    Evas_Smart_Cb func;
39    void *data;
40    int deleted : 1;
41 };
42
43 typedef struct _Widget_Data Widget_Data;
44
45 struct _Widget_Data {
46    Evas_Object *self;
47    Evas_Object *base;
48    Eina_List *items;
49    Eina_List *current;
50    Eina_List *sentinel; /* item containing the largest label string */
51    int walking;
52    unsigned int max_len;
53    Ecore_Timer *spin;
54    double interval, first_interval;
55 };
56
57 static const char *widtype = NULL;
58 static void _del_hook(Evas_Object *obj);
59 static void _theme_hook(Evas_Object *obj);
60 static void _sizing_eval(Evas_Object *obj);
61 static void _update_view(Evas_Object *obj);
62 static void _callbacks_set(Evas_Object *obj);
63 static void _flip_up(Widget_Data *wd);
64 static void _flip_down(Widget_Data *wd);
65
66 static const char SIG_SELECTED[] = "selected";
67 static const char SIG_UNDERFLOWED[] = "underflowed";
68 static const char SIG_OVERFLOWED[] = "overflowed";
69 static const Evas_Smart_Cb_Description _signals[] = {
70   {SIG_SELECTED, ""},
71   {SIG_UNDERFLOWED, ""},
72   {SIG_OVERFLOWED, ""},
73   {NULL, NULL}
74 };
75
76 #define ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(it, ...)               \
77    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, __VA_ARGS__);            \
78   if (it->deleted)                                                      \
79     {                                                                   \
80        ERR(""#it" has been DELETED.\n");                                \
81        return __VA_ARGS__;                                              \
82     }                                                                   \
83
84 static Elm_Flipselector_Item *
85 _item_new(Evas_Object *obj, const char *label, Evas_Smart_Cb func, const void *data)
86 {
87    unsigned int len;
88    Elm_Flipselector_Item *it;
89    Widget_Data *wd = elm_widget_data_get(obj);
90
91    it = elm_widget_item_new(obj, Elm_Flipselector_Item);
92    if (!it)
93      return NULL;
94
95    len = strlen(label);
96    if (len > wd->max_len)
97      len = wd->max_len;
98
99    it->label = eina_stringshare_add_length(label, len);
100    it->func = func;
101    it->base.data = data;
102
103    /* TODO: no view here, but if one desires general contents in the
104     * future... */
105    return it;
106 }
107
108 static inline void
109 _item_free(Elm_Flipselector_Item *it)
110 {
111    eina_stringshare_del(it->label);
112    elm_widget_item_del(it);
113 }
114
115 static void
116 _del_hook(Evas_Object *obj)
117 {
118    Elm_Flipselector_Item *item;
119
120    Widget_Data *wd = elm_widget_data_get(obj);
121    if (!wd)
122      return;
123
124    if (wd->walking)
125      ERR("flipselector deleted while walking.\n");
126
127    EINA_LIST_FREE(wd->items, item)
128      _item_free(item);
129
130    if (wd->spin) ecore_timer_del(wd->spin);
131    free(wd);
132 }
133
134 static void
135 _theme_hook(Evas_Object *obj)
136 {
137    Widget_Data *wd;
138    const char *max_len;
139
140    wd = elm_widget_data_get(obj);
141    if (!wd)
142      return;
143
144    _elm_theme_object_set(obj, wd->base, "flipselector", "base",
145                          elm_widget_style_get(obj));
146    edje_object_scale_set(wd->base,
147                          elm_widget_scale_get(obj) * _elm_config->scale);
148
149    max_len = edje_object_data_get(wd->base, "max_len");
150    if (!max_len)
151      wd->max_len = MAX_LEN_DEFAULT;
152    else
153      {
154         wd->max_len = atoi(max_len);
155         if (!wd->max_len)
156           wd->max_len = MAX_LEN_DEFAULT;
157      }
158
159    _update_view(obj);
160    _sizing_eval(obj);
161 }
162
163 static void
164 _sentinel_eval(Widget_Data *wd)
165 {
166    Elm_Flipselector_Item *it;
167    Eina_List *l;
168
169    if (!wd->items)
170      {
171         wd->sentinel = NULL;
172         return;
173      }
174
175    wd->sentinel = wd->items;
176
177    EINA_LIST_FOREACH(wd->items, l, it)
178      {
179         if (strlen(elm_flipselector_item_label_get(it)) >
180             strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel))))
181           wd->sentinel = l;
182      }
183 }
184
185 /* TODO: create a flag to avoid looping here all times */
186 static void
187 _flipselector_process_deletions(Widget_Data *wd)
188 {
189    Elm_Flipselector_Item *it;
190    Eina_List *l;
191    Eina_Bool skip = EINA_TRUE;
192    Eina_Bool sentinel_eval = EINA_FALSE;
193
194    wd->walking++; /* avoid nested deletions */
195
196    EINA_LIST_FOREACH(wd->items, l, it)
197      {
198         if (!it->deleted)
199           continue;
200
201         if (wd->current == l)
202           {
203              if (wd->current == wd->sentinel)
204                sentinel_eval = EINA_TRUE;
205
206              wd->current = eina_list_prev(wd->current);
207           }
208         wd->items = eina_list_remove(wd->items, it);
209
210         if (!wd->current)
211           wd->current = wd->items;
212
213         _item_free(it);
214         skip = EINA_FALSE;
215
216         if (eina_list_count(wd->items) <= 1)
217           edje_object_signal_emit(wd->base, "elm,state,button,hidden", "elm");
218         else
219           edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
220      }
221
222    if (!skip)
223      _update_view(wd->self);
224
225    if (sentinel_eval)
226      _sentinel_eval(wd);
227
228    wd->walking--;
229 }
230
231 static inline void
232 _flipselector_walk(Widget_Data *wd)
233 {
234    if (wd->walking < 0)
235      {
236         ERR("walking was negative. fixed!\n");
237         wd->walking = 0;
238      }
239    wd->walking++;
240 }
241
242 static inline void
243 _flipselector_unwalk(Widget_Data *wd)
244 {
245    wd->walking--;
246    if (wd->walking < 0)
247      {
248         ERR("walking became negative. fixed!\n");
249         wd->walking = 0;
250      }
251
252    if (wd->walking)
253      return;
254
255    _flipselector_process_deletions(wd);
256 }
257
258 static Eina_Bool
259 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
260 {
261    Evas_Event_Key_Down *ev;
262    Widget_Data *wd;
263    Eina_Bool is_up = EINA_TRUE;
264
265    if (type != EVAS_CALLBACK_KEY_DOWN)
266      return EINA_FALSE;
267
268    wd = elm_widget_data_get(obj);
269    if (!wd)
270      return EINA_FALSE;
271
272    ev = event_info;
273    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
274      return EINA_FALSE;
275
276    if (elm_widget_disabled_get(obj))
277      return EINA_FALSE;
278
279    if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
280      is_up = EINA_FALSE;
281    else if ((strcmp(ev->keyname, "Up")) && (strcmp(ev->keyname, "KP_Up")))
282      return EINA_FALSE;
283
284    if (wd->spin) ecore_timer_del(wd->spin);
285
286    /* TODO: if direction setting via API is not coming in, replace
287       these calls by flip_{next,prev} */
288    _flipselector_walk(wd);
289    if (is_up)
290      _flip_up(wd);
291    else
292      _flip_down(wd);
293    _flipselector_unwalk(wd);
294
295    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
296    return EINA_TRUE;
297 }
298
299 static void
300 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
301 {
302    Widget_Data *wd = elm_widget_data_get(obj);
303    if (!wd)
304      return;
305
306    /* FIXME: no treatment of this signal so far */
307    if (elm_widget_focus_get(obj))
308      {
309         edje_object_signal_emit(wd->base, "elm,action,focus", "elm");
310         evas_object_focus_set(wd->base, EINA_TRUE);
311      }
312    else
313      {
314         edje_object_signal_emit(wd->base, "elm,action,unfocus", "elm");
315         evas_object_focus_set(wd->base, EINA_FALSE);
316      }
317 }
318
319 static void
320 _sizing_eval(Evas_Object *obj)
321 {
322    Widget_Data *wd;
323    const char *tmp = NULL;
324    Evas_Coord minw = -1, minh = -1, w, h;
325
326    wd = elm_widget_data_get(obj);
327    if (!wd)
328      return;
329
330    elm_coords_finger_size_adjust(1, &minw, 2, &minh);
331
332    if (wd->sentinel)
333      {
334         const char *label = \
335           elm_flipselector_item_label_get(DATA_GET(wd->sentinel));
336
337         tmp = edje_object_part_text_get(wd->base, "top");
338         edje_object_part_text_set(wd->base, "top", label);
339      }
340
341    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
342    elm_coords_finger_size_adjust(1, &minw, 2, &minh);
343    evas_object_size_hint_min_get(obj, &w, &h);
344
345    if (wd->sentinel)
346      edje_object_part_text_set(wd->base, "top", tmp);
347
348    if (w > minw) minw = w;
349    if (h > minh) minh = h;
350
351    evas_object_size_hint_min_set(obj, minw, minh);
352 }
353
354 static void
355 _update_view(Evas_Object *obj)
356 {
357    Widget_Data *wd;
358    const char *label;
359    Elm_Flipselector_Item *item;
360
361    wd = elm_widget_data_get(obj);
362    if (!wd)
363      return;
364
365    label = NULL;
366    item = DATA_GET(wd->current);
367    if (item)
368      label = item->label;
369
370    edje_object_part_text_set(wd->base, "top", label ? label : "");
371    edje_object_part_text_set(wd->base, "bottom", label ? label : "");
372
373    edje_object_message_signal_process(wd->base);
374 }
375
376 static void
377 _changed(Widget_Data *wd)
378 {
379    Elm_Flipselector_Item *item;
380
381    item = DATA_GET(wd->current);
382    if (!item)
383      return;
384
385    if (item->func)
386      item->func((void *)item->base.data, item->base.widget, item);
387    if (!item->deleted)
388      evas_object_smart_callback_call(wd->self, SIG_SELECTED, item);
389 }
390
391 static void
392 _send_msg(Widget_Data *wd, int flipside, char *label)
393 {
394    Edje_Message_String msg;
395
396    msg.str = label;
397    edje_object_message_send(wd->base, EDJE_MESSAGE_STRING, flipside, &msg);
398    edje_object_message_signal_process(wd->base);
399
400    _changed(wd);
401 }
402
403 static void
404 _flip_up(Widget_Data *wd)
405 {
406    Elm_Flipselector_Item *item;
407
408    if (!wd->current)
409      return;
410
411    if (wd->current == wd->items)
412      {
413         wd->current = eina_list_last(wd->items);
414         evas_object_smart_callback_call(wd->self, SIG_UNDERFLOWED, NULL);
415      }
416    else
417      wd->current = eina_list_prev(wd->current);
418
419    item = DATA_GET(wd->current);
420    if (!item)
421      return;
422
423    _send_msg(wd, MSG_FLIP_UP, (char *)item->label);
424 }
425
426 static Eina_Bool
427 _signal_val_up(void *data)
428 {
429    Widget_Data *wd = elm_widget_data_get(data);
430
431    if (!wd)
432      goto val_up_exit_on_error;
433
434    _flipselector_walk(wd);
435
436    if (wd->interval > FLIP_MIN_INTERVAL)
437      wd->interval = wd->interval / 1.05;
438
439    ecore_timer_interval_set(wd->spin, wd->interval);
440
441    _flip_up(wd);
442
443    _flipselector_unwalk(wd);
444
445    return ECORE_CALLBACK_RENEW;
446
447  val_up_exit_on_error:
448    return ECORE_CALLBACK_CANCEL;
449 }
450
451 static void
452 _signal_val_up_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
453 {
454    Widget_Data *wd = elm_widget_data_get(data);
455    if (!wd)
456      return;
457
458    wd->interval = wd->first_interval;
459
460    if (wd->spin)
461      ecore_timer_del(wd->spin);
462    wd->spin = ecore_timer_add(wd->interval, _signal_val_up, data);
463
464    _signal_val_up(data);
465 }
466
467 static void
468 _flip_down(Widget_Data *wd)
469 {
470    Elm_Flipselector_Item *item;
471
472    if (!wd->current)
473      return;
474
475    wd->current = eina_list_next(wd->current);
476    if (!wd->current)
477      {
478         wd->current = wd->items;
479         evas_object_smart_callback_call(wd->self, SIG_OVERFLOWED, NULL);
480      }
481
482    item = DATA_GET(wd->current);
483    if (!item)
484      return;
485
486    _send_msg(wd, MSG_FLIP_DOWN, (char *)item->label);
487 }
488
489 static Eina_Bool
490 _signal_val_down(void *data)
491 {
492    Widget_Data *wd = elm_widget_data_get(data);
493
494    if (!wd)
495      goto val_down_exit_on_error;
496
497    _flipselector_walk(wd);
498
499    if (wd->interval > FLIP_MIN_INTERVAL)
500      wd->interval = wd->interval / 1.05;
501    ecore_timer_interval_set(wd->spin, wd->interval);
502
503    _flip_down(wd);
504
505    _flipselector_unwalk(wd);
506
507    return ECORE_CALLBACK_RENEW;
508
509  val_down_exit_on_error:
510    return ECORE_CALLBACK_CANCEL;
511 }
512
513 static void
514 _signal_val_down_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
515 {
516    Widget_Data *wd = elm_widget_data_get(data);
517    if (!wd)
518      return;
519
520    wd->interval = wd->first_interval;
521
522    if (wd->spin)
523      ecore_timer_del(wd->spin);
524    wd->spin = ecore_timer_add(wd->interval, _signal_val_down, data);
525
526    _signal_val_down(data);
527 }
528
529 static void
530 _signal_val_change_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
531 {
532    Widget_Data *wd = elm_widget_data_get(data);
533    if (!wd)
534      return;
535
536    if (wd->spin)
537      ecore_timer_del(wd->spin);
538    wd->spin = NULL;
539 }
540
541 static void
542 _callbacks_set(Evas_Object *obj)
543 {
544    Widget_Data *wd = elm_widget_data_get(obj);
545
546    edje_object_signal_callback_add(wd->base, "elm,action,up,start",
547                                    "", _signal_val_up_start, obj);
548    edje_object_signal_callback_add(wd->base, "elm,action,up,stop",
549                                    "", _signal_val_change_stop, obj);
550    edje_object_signal_callback_add(wd->base, "elm,action,down,start",
551                                    "", _signal_val_down_start, obj);
552    edje_object_signal_callback_add(wd->base, "elm,action,down,stop",
553                                    "", _signal_val_change_stop, obj);
554 }
555
556 /**
557  * Add a new flipselector to the parent.
558  *
559  * @param parent The parent object
560  * @return The new object or NULL, if it cannot be created
561  *
562  * @ingroup Flipselector
563  */
564 EAPI Evas_Object *
565 elm_flipselector_add(Evas_Object *parent)
566 {
567    Evas_Object *obj;
568    Evas *e;
569    Widget_Data *wd;
570
571    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
572
573    wd = ELM_NEW(Widget_Data);
574    e = evas_object_evas_get(parent);
575    if (!e) return NULL;
576    obj = elm_widget_add(e);
577    wd->self = obj;
578    ELM_SET_WIDTYPE(widtype, "flipselector");
579    elm_widget_type_set(obj, "flipselector");
580    elm_widget_sub_object_add(parent, obj);
581    elm_widget_data_set(obj, wd);
582
583    elm_widget_del_hook_set(obj, _del_hook);
584    elm_widget_theme_hook_set(obj, _theme_hook);
585    /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
586
587    elm_widget_can_focus_set(obj, EINA_TRUE);
588    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
589    elm_widget_event_hook_set(obj, _event_hook);
590
591    wd->base = edje_object_add(e);
592    elm_widget_resize_object_set(obj, wd->base);
593
594    _callbacks_set(obj);
595
596    wd->first_interval = FLIP_FIRST_INTERVAL;
597
598    _theme_hook(obj);
599
600    evas_object_smart_callbacks_descriptions_set(obj, _signals);
601    return obj;
602 }
603
604 /**
605  * Select next item of a flipselector.
606  *
607  * @param obj The flipselector object
608  *
609  * @ingroup Flipselector
610  */
611 EAPI void
612 elm_flipselector_flip_next(Evas_Object *obj)
613 {
614    ELM_CHECK_WIDTYPE(obj, widtype);
615
616    Widget_Data *wd = elm_widget_data_get(obj);
617    if (!wd)
618      return;
619
620    if (wd->spin) ecore_timer_del(wd->spin);
621
622    _flipselector_walk(wd);
623    _flip_down(wd);
624    _flipselector_unwalk(wd);
625 }
626
627 /**
628  * Select previous item of a flipselector.
629  *
630  * @param obj The flipselector object
631  *
632  * @ingroup Flipselector
633  */
634 EAPI void
635 elm_flipselector_flip_prev(Evas_Object *obj)
636 {
637    ELM_CHECK_WIDTYPE(obj, widtype);
638
639    Widget_Data *wd = elm_widget_data_get(obj);
640    if (!wd)
641      return;
642
643    if (wd->spin) ecore_timer_del(wd->spin);
644
645    _flipselector_walk(wd);
646    _flip_up(wd);
647    _flipselector_unwalk(wd);
648 }
649
650 /**
651  * Append item to a flipselector.
652  *
653  * @param obj The flipselector object
654  * @param label The label of new item
655  * @param func Convenience function called when item selected
656  * @param data Data passed to @p func above
657  * @return A handle to the item added or NULL, on errors
658  *
659  * @note The maximum length of the label is going to be determined by
660  * the widget's theme. Strings larger than that value are going to be
661  * truncated.
662  *
663  * @ingroup Flipselector
664  */
665 EAPI Elm_Flipselector_Item *
666 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
667 {
668    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
669
670    Elm_Flipselector_Item *item;
671    Widget_Data *wd;
672
673    wd = elm_widget_data_get(obj);
674    if (!wd)
675      return NULL;
676
677    item = _item_new(obj, label, func, data);
678    if (!item)
679      return NULL;
680
681    wd->items = eina_list_append(wd->items, item);
682    if (!wd->current) {
683       wd->current = wd->items;
684       _update_view(obj);
685    }
686
687    if (!wd->sentinel ||
688        (strlen(elm_flipselector_item_label_get(item)) >
689         strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel)))))
690      {
691         wd->sentinel = eina_list_last(wd->items);
692         _sizing_eval(obj);
693      }
694
695    if (eina_list_count(wd->items) >= 2)
696      edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
697
698    return item;
699 }
700
701 /**
702  * Prepend item to a flipselector.
703  *
704  * @param obj The flipselector object
705  * @param label The label of new item
706  * @param func Convenience function called when item selected
707  * @param data Data passed to @p func above
708  * @return A handle to the item added or NULL, on errors
709  *
710  * @note The maximum length of the label is going to be determined by
711  * the widget's theme. Strings larger than that value are going to be
712  * truncated.
713  *
714  * @ingroup Flipselector
715  */
716 EAPI Elm_Flipselector_Item *
717 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
718 {
719    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
720
721    Elm_Flipselector_Item *item;
722    Widget_Data *wd;
723
724    wd = elm_widget_data_get(obj);
725    if (!wd)
726      return NULL;
727
728    item = _item_new(obj, label, func, data);
729    if (!item)
730      return NULL;
731
732    wd->items = eina_list_prepend(wd->items, item);
733    if (!wd->current) {
734       wd->current = wd->items;
735       _update_view(obj);
736    }
737
738    if (!wd->sentinel ||
739        (strlen(elm_flipselector_item_label_get(item)) >
740         strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel)))))
741      {
742         wd->sentinel = wd->items;
743         _sizing_eval(obj);
744      }
745
746    if (eina_list_count(wd->items) >= 2)
747      edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
748
749    return item;
750 }
751
752 /* TODO: account for deleted items?  */
753 /**
754  * Get a list of items in the flipselector.
755  *
756  * @param obj The flipselector object
757  * @return The list of items, or NULL on errors.
758  *
759  * @ingroup Flipselector
760  */
761 EAPI const Eina_List *
762 elm_flipselector_items_get(const Evas_Object *obj)
763 {
764    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
765
766    Widget_Data *wd = elm_widget_data_get(obj);
767    if (!wd)
768      return NULL;
769
770    return wd->items;
771 }
772
773 /**
774  * Get the first item in the flipselector
775  *
776  * @param obj The flipselector object
777  * @return The first item, or NULL if none
778  *
779  * @ingroup Flipselector
780  */
781 EAPI Elm_Flipselector_Item *
782 elm_flipselector_first_item_get(const Evas_Object *obj)
783 {
784    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
785
786    Elm_Flipselector_Item *it;
787    Widget_Data *wd;
788    Eina_List *l;
789
790    wd = elm_widget_data_get(obj);
791    if (!wd || !wd->items)
792      return NULL;
793
794    EINA_LIST_FOREACH(wd->items, l, it)
795      {
796         if (it->deleted)
797           continue;
798
799         return it;
800      }
801
802    return NULL;
803 }
804
805 /**
806  * Get the last item in the flipselector
807  *
808  * @param obj The flipselector object
809  * @return The last item, or NULL if none
810  *
811  * @ingroup Flipselector
812  */
813 EAPI Elm_Flipselector_Item *
814 elm_flipselector_last_item_get(const Evas_Object *obj)
815 {
816    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
817
818    Elm_Flipselector_Item *it;
819    Widget_Data *wd;
820    Eina_List *l;
821
822    wd = elm_widget_data_get(obj);
823    if (!wd || !wd->items)
824      return NULL;
825
826    EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
827      {
828         if (it->deleted)
829           continue;
830
831         return it;
832      }
833
834    return NULL;
835 }
836
837 /**
838  * Get the selected item in a flipselector.
839  *
840  * @param obj The flipselector object
841  * @return The selected item, or NULL if none
842  *
843  * @ingroup Flipselector
844  */
845 EAPI Elm_Flipselector_Item *
846 elm_flipselector_selected_item_get(const Evas_Object *obj)
847 {
848    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
849
850    Widget_Data *wd = elm_widget_data_get(obj);
851    if (!wd || !wd->current)
852      return NULL;
853
854    return DATA_GET(wd->current);
855 }
856
857 /**
858  * Set the selected state of an item
859  *
860  * This sets the selected state (EINA_TRUE selected, EINA_FALSE not selected)
861  * of the given item @p item.
862  * If a new item is selected the previosly selected will be unselected.
863  * If the item @p item is unselected, the first item will be selected.
864  *
865  * @param item The item
866  * @param selected The selected state
867  *
868  * @ingroup Flipselector
869  */
870 EAPI void
871 elm_flipselector_item_selected_set(Elm_Flipselector_Item *item, Eina_Bool selected)
872 {
873    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
874
875    Elm_Flipselector_Item *_item, *cur;
876    int flipside = MSG_FLIP_UP;
877    Widget_Data *wd;
878    Eina_List *l;
879
880    wd = elm_widget_data_get(item->base.widget);
881    if (!wd)
882      return;
883
884    cur = DATA_GET(wd->current);
885    if ((selected) && (cur == item))
886      return;
887
888    _flipselector_walk(wd);
889
890    if ((!selected) && (cur == item))
891      {
892         EINA_LIST_FOREACH(wd->items, l, _item)
893           {
894              if (!_item->deleted)
895                {
896                   wd->current = l;
897                   _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
898                   break;
899                }
900           }
901         _flipselector_unwalk(wd);
902         return;
903      }
904
905    EINA_LIST_FOREACH(wd->items, l, _item)
906      {
907         if (_item == cur)
908           flipside = MSG_FLIP_DOWN;
909
910         if (_item == item)
911           {
912              wd->current = l;
913              _send_msg(wd, flipside, (char *)item->label);
914              break;
915           }
916      }
917
918    _flipselector_unwalk(wd);
919 }
920
921 /*
922  * Get the selected state of @p item.
923  *
924  * @param item The flipselector item
925  * @return If true, the item is selected
926  *
927  * @ingroup Flipselector
928  */
929 EAPI Eina_Bool
930 elm_flipselector_item_selected_get(const Elm_Flipselector_Item *item)
931 {
932    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, EINA_FALSE);
933    Widget_Data *wd;
934
935    wd = elm_widget_data_get(item->base.widget);
936    if (!wd) return EINA_FALSE;
937    return (eina_list_data_get(wd->current) == item);
938 }
939
940 /**
941  * Delete a given item from a flipselector.
942  *
943  * @param item The item
944  *
945  * @ingroup Flipselector
946  */
947 EAPI void
948 elm_flipselector_item_del(Elm_Flipselector_Item *item)
949 {
950    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
951
952    Widget_Data *wd;
953
954    wd = elm_widget_data_get(item->base.widget);
955    if (!wd)
956      return;
957
958    if (wd->walking > 0)
959      {
960         item->deleted = EINA_TRUE;
961         return;
962      }
963
964    _flipselector_walk(wd);
965
966    wd->items = eina_list_remove(wd->items, item);
967    _item_free(item);
968    _sentinel_eval(wd);
969
970    _flipselector_unwalk(wd);
971 }
972
973 /**
974  * Get the label of a given flipselector item.
975  *
976  * @param item The item
977  * @return The label of a given item, or NULL if none
978  *
979  * @ingroup Flipselector
980  */
981 EAPI const char *
982 elm_flipselector_item_label_get(const Elm_Flipselector_Item *item)
983 {
984    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
985
986    Elm_Flipselector_Item *_item;
987    Widget_Data *wd;
988    Eina_List *l;
989
990    wd = elm_widget_data_get(item->base.widget);
991    if ((!wd) || (!wd->items))
992      return NULL;
993
994    EINA_LIST_FOREACH(wd->items, l, _item)
995      if (_item == item)
996        return item->label;
997
998    return NULL;
999 }
1000
1001 /**
1002  * Set the label of a given flipselector item.
1003  *
1004  * @param item The item
1005  * @param label The text label string in UTF-8
1006  *
1007  * @ingroup Flipselector
1008  */
1009 EAPI void
1010 elm_flipselector_item_label_set(Elm_Flipselector_Item *item, const char *label)
1011 {
1012    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
1013
1014    Widget_Data *wd;
1015    Eina_List *l;
1016
1017    if ((!item) || (!label))
1018      return;
1019
1020    wd = elm_widget_data_get(item->base.widget);
1021    if ((!wd) || (!wd->items))
1022      return;
1023
1024    l = eina_list_data_find_list(wd->items, item);
1025    if (!l)
1026      return;
1027
1028    eina_stringshare_del(item->label);
1029    item->label = eina_stringshare_add_length(label, wd->max_len);
1030
1031    if (strlen(label) >
1032        strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel))))
1033      wd->sentinel = l;
1034
1035    if (wd->current == l)
1036      {
1037         _update_view(item->base.widget);
1038         _sizing_eval(wd->self);
1039      }
1040
1041    return;
1042 }
1043
1044 /**
1045  * Gets the item before @p item in a flipselector.
1046  *
1047  * @param item The item
1048  * @return The item before the item @p item
1049  *
1050  * @ingroup Flipselector
1051  */
1052 EAPI Elm_Flipselector_Item *
1053 elm_flipselector_item_prev_get(Elm_Flipselector_Item *item)
1054 {
1055    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
1056
1057    Elm_Flipselector_Item *_item;
1058    Widget_Data *wd;
1059    Eina_List *l;
1060
1061    wd = elm_widget_data_get(item->base.widget);
1062    if ((!wd) || (!wd->items))
1063      return NULL;
1064
1065    EINA_LIST_FOREACH(wd->items, l, _item)
1066      if (_item == item)
1067        {
1068           l = eina_list_prev(l);
1069           if (!l)
1070             return NULL;
1071           return DATA_GET(l);
1072        }
1073
1074    return NULL;
1075 }
1076
1077 /**
1078  * Gets the item after @p item in a flipselector.
1079  *
1080  * @param item The item
1081  * @return The item after the item @p item
1082  *
1083  * @ingroup Flipselector
1084  */
1085 EAPI Elm_Flipselector_Item *
1086 elm_flipselector_item_next_get(Elm_Flipselector_Item *item)
1087 {
1088    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
1089
1090    Elm_Flipselector_Item *_item;
1091    Widget_Data *wd;
1092    Eina_List *l;
1093
1094    wd = elm_widget_data_get(item->base.widget);
1095    if ((!wd) || (!wd->items))
1096      return NULL;
1097
1098    EINA_LIST_FOREACH(wd->items, l, _item)
1099      if (_item == item)
1100        {
1101           l = eina_list_next(l);
1102           if (!l)
1103             return NULL;
1104           return DATA_GET(l);
1105        }
1106
1107    return NULL;
1108 }
1109
1110 /**
1111  * Set the flipping interval for the flipselector.
1112  *
1113  * @param obj The flipselector object
1114  * @param interval The interval value in seconds
1115  *
1116  * The interval value is decreased while the user flips the widget up
1117  * or down repeatedly. The next interval value is the previous
1118  * interval / 1.05, so it speeds up a bit. Default value is 0.85
1119  * seconds.
1120  *
1121  * @ingroup Flipselector
1122  */
1123 EAPI void
1124 elm_flipselector_interval_set(Evas_Object *obj, double interval)
1125 {
1126    ELM_CHECK_WIDTYPE(obj, widtype);
1127
1128    Widget_Data *wd = elm_widget_data_get(obj);
1129    if (!wd)
1130      return;
1131
1132    wd->first_interval = interval;
1133 }
1134
1135 /**
1136  * Get the flipping interval of the flipselector.
1137  *
1138  * @param obj The flipselector object
1139  * @return The value of the first interval in seconds
1140  *
1141  * The interval value is decreased while the user flips the widget up
1142  * or down repeatedly. The next interval value is the previous
1143  * interval / 1.05, so it speeds up a bit. Default value is 0.85
1144  * seconds.
1145  *
1146  * @ingroup Flipselector
1147  */
1148 EAPI double
1149 elm_flipselector_interval_get(const Evas_Object *obj)
1150 {
1151    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
1152
1153    Widget_Data *wd = elm_widget_data_get(obj);
1154
1155    if (!wd)
1156      return 0.0;
1157    return wd->first_interval;
1158 }