make all widgets use a standard setup macro. cuts code down and
[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    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
572    
573    ELM_SET_WIDTYPE(widtype, "flipselector");
574    elm_widget_type_set(obj, "flipselector");
575    elm_widget_sub_object_add(parent, obj);
576    elm_widget_data_set(obj, wd);
577
578    elm_widget_del_hook_set(obj, _del_hook);
579    elm_widget_theme_hook_set(obj, _theme_hook);
580    /* TODO: elm_widget_disable_hook_set(obj, _disable_hook); */
581
582    elm_widget_can_focus_set(obj, EINA_TRUE);
583    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
584    elm_widget_event_hook_set(obj, _event_hook);
585
586    wd->base = edje_object_add(e);
587    elm_widget_resize_object_set(obj, wd->base);
588
589    _callbacks_set(obj);
590
591    wd->first_interval = FLIP_FIRST_INTERVAL;
592
593    _theme_hook(obj);
594
595    evas_object_smart_callbacks_descriptions_set(obj, _signals);
596    return obj;
597 }
598
599 /**
600  * Select next item of a flipselector.
601  *
602  * @param obj The flipselector object
603  *
604  * @ingroup Flipselector
605  */
606 EAPI void
607 elm_flipselector_flip_next(Evas_Object *obj)
608 {
609    ELM_CHECK_WIDTYPE(obj, widtype);
610
611    Widget_Data *wd = elm_widget_data_get(obj);
612    if (!wd)
613      return;
614
615    if (wd->spin) ecore_timer_del(wd->spin);
616
617    _flipselector_walk(wd);
618    _flip_down(wd);
619    _flipselector_unwalk(wd);
620 }
621
622 /**
623  * Select previous item of a flipselector.
624  *
625  * @param obj The flipselector object
626  *
627  * @ingroup Flipselector
628  */
629 EAPI void
630 elm_flipselector_flip_prev(Evas_Object *obj)
631 {
632    ELM_CHECK_WIDTYPE(obj, widtype);
633
634    Widget_Data *wd = elm_widget_data_get(obj);
635    if (!wd)
636      return;
637
638    if (wd->spin) ecore_timer_del(wd->spin);
639
640    _flipselector_walk(wd);
641    _flip_up(wd);
642    _flipselector_unwalk(wd);
643 }
644
645 /**
646  * Append item to a flipselector.
647  *
648  * @param obj The flipselector object
649  * @param label The label of new item
650  * @param func Convenience function called when item selected
651  * @param data Data passed to @p func above
652  * @return A handle to the item added or NULL, on errors
653  *
654  * @note The maximum length of the label is going to be determined by
655  * the widget's theme. Strings larger than that value are going to be
656  * truncated.
657  *
658  * @ingroup Flipselector
659  */
660 EAPI Elm_Flipselector_Item *
661 elm_flipselector_item_append(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
662 {
663    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
664
665    Elm_Flipselector_Item *item;
666    Widget_Data *wd;
667
668    wd = elm_widget_data_get(obj);
669    if (!wd)
670      return NULL;
671
672    item = _item_new(obj, label, func, data);
673    if (!item)
674      return NULL;
675
676    wd->items = eina_list_append(wd->items, item);
677    if (!wd->current) {
678       wd->current = wd->items;
679       _update_view(obj);
680    }
681
682    if (!wd->sentinel ||
683        (strlen(elm_flipselector_item_label_get(item)) >
684         strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel)))))
685      {
686         wd->sentinel = eina_list_last(wd->items);
687         _sizing_eval(obj);
688      }
689
690    if (eina_list_count(wd->items) >= 2)
691      edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
692
693    return item;
694 }
695
696 /**
697  * Prepend item to a flipselector.
698  *
699  * @param obj The flipselector object
700  * @param label The label of new item
701  * @param func Convenience function called when item selected
702  * @param data Data passed to @p func above
703  * @return A handle to the item added or NULL, on errors
704  *
705  * @note The maximum length of the label is going to be determined by
706  * the widget's theme. Strings larger than that value are going to be
707  * truncated.
708  *
709  * @ingroup Flipselector
710  */
711 EAPI Elm_Flipselector_Item *
712 elm_flipselector_item_prepend(Evas_Object *obj, const char *label, void (*func)(void *data, Evas_Object *obj, void *event_info), void *data)
713 {
714    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
715
716    Elm_Flipselector_Item *item;
717    Widget_Data *wd;
718
719    wd = elm_widget_data_get(obj);
720    if (!wd)
721      return NULL;
722
723    item = _item_new(obj, label, func, data);
724    if (!item)
725      return NULL;
726
727    wd->items = eina_list_prepend(wd->items, item);
728    if (!wd->current) {
729       wd->current = wd->items;
730       _update_view(obj);
731    }
732
733    if (!wd->sentinel ||
734        (strlen(elm_flipselector_item_label_get(item)) >
735         strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel)))))
736      {
737         wd->sentinel = wd->items;
738         _sizing_eval(obj);
739      }
740
741    if (eina_list_count(wd->items) >= 2)
742      edje_object_signal_emit(wd->base, "elm,state,button,visible", "elm");
743
744    return item;
745 }
746
747 /* TODO: account for deleted items?  */
748 /**
749  * Get a list of items in the flipselector.
750  *
751  * @param obj The flipselector object
752  * @return The list of items, or NULL on errors.
753  *
754  * @ingroup Flipselector
755  */
756 EAPI const Eina_List *
757 elm_flipselector_items_get(const Evas_Object *obj)
758 {
759    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
760
761    Widget_Data *wd = elm_widget_data_get(obj);
762    if (!wd)
763      return NULL;
764
765    return wd->items;
766 }
767
768 /**
769  * Get the first item in the flipselector
770  *
771  * @param obj The flipselector object
772  * @return The first item, or NULL if none
773  *
774  * @ingroup Flipselector
775  */
776 EAPI Elm_Flipselector_Item *
777 elm_flipselector_first_item_get(const Evas_Object *obj)
778 {
779    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
780
781    Elm_Flipselector_Item *it;
782    Widget_Data *wd;
783    Eina_List *l;
784
785    wd = elm_widget_data_get(obj);
786    if (!wd || !wd->items)
787      return NULL;
788
789    EINA_LIST_FOREACH(wd->items, l, it)
790      {
791         if (it->deleted)
792           continue;
793
794         return it;
795      }
796
797    return NULL;
798 }
799
800 /**
801  * Get the last item in the flipselector
802  *
803  * @param obj The flipselector object
804  * @return The last item, or NULL if none
805  *
806  * @ingroup Flipselector
807  */
808 EAPI Elm_Flipselector_Item *
809 elm_flipselector_last_item_get(const Evas_Object *obj)
810 {
811    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
812
813    Elm_Flipselector_Item *it;
814    Widget_Data *wd;
815    Eina_List *l;
816
817    wd = elm_widget_data_get(obj);
818    if (!wd || !wd->items)
819      return NULL;
820
821    EINA_LIST_REVERSE_FOREACH(wd->items, l, it)
822      {
823         if (it->deleted)
824           continue;
825
826         return it;
827      }
828
829    return NULL;
830 }
831
832 /**
833  * Get the selected item in a flipselector.
834  *
835  * @param obj The flipselector object
836  * @return The selected item, or NULL if none
837  *
838  * @ingroup Flipselector
839  */
840 EAPI Elm_Flipselector_Item *
841 elm_flipselector_selected_item_get(const Evas_Object *obj)
842 {
843    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
844
845    Widget_Data *wd = elm_widget_data_get(obj);
846    if (!wd || !wd->current)
847      return NULL;
848
849    return DATA_GET(wd->current);
850 }
851
852 /**
853  * Set the selected state of an item
854  *
855  * This sets the selected state (EINA_TRUE selected, EINA_FALSE not selected)
856  * of the given item @p item.
857  * If a new item is selected the previosly selected will be unselected.
858  * If the item @p item is unselected, the first item will be selected.
859  *
860  * @param item The item
861  * @param selected The selected state
862  *
863  * @ingroup Flipselector
864  */
865 EAPI void
866 elm_flipselector_item_selected_set(Elm_Flipselector_Item *item, Eina_Bool selected)
867 {
868    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
869
870    Elm_Flipselector_Item *_item, *cur;
871    int flipside = MSG_FLIP_UP;
872    Widget_Data *wd;
873    Eina_List *l;
874
875    wd = elm_widget_data_get(item->base.widget);
876    if (!wd)
877      return;
878
879    cur = DATA_GET(wd->current);
880    if ((selected) && (cur == item))
881      return;
882
883    _flipselector_walk(wd);
884
885    if ((!selected) && (cur == item))
886      {
887         EINA_LIST_FOREACH(wd->items, l, _item)
888           {
889              if (!_item->deleted)
890                {
891                   wd->current = l;
892                   _send_msg(wd, MSG_FLIP_UP, (char *)_item->label);
893                   break;
894                }
895           }
896         _flipselector_unwalk(wd);
897         return;
898      }
899
900    EINA_LIST_FOREACH(wd->items, l, _item)
901      {
902         if (_item == cur)
903           flipside = MSG_FLIP_DOWN;
904
905         if (_item == item)
906           {
907              wd->current = l;
908              _send_msg(wd, flipside, (char *)item->label);
909              break;
910           }
911      }
912
913    _flipselector_unwalk(wd);
914 }
915
916 /*
917  * Get the selected state of @p item.
918  *
919  * @param item The flipselector item
920  * @return If true, the item is selected
921  *
922  * @ingroup Flipselector
923  */
924 EAPI Eina_Bool
925 elm_flipselector_item_selected_get(const Elm_Flipselector_Item *item)
926 {
927    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, EINA_FALSE);
928    Widget_Data *wd;
929
930    wd = elm_widget_data_get(item->base.widget);
931    if (!wd) return EINA_FALSE;
932    return (eina_list_data_get(wd->current) == item);
933 }
934
935 /**
936  * Delete a given item from a flipselector.
937  *
938  * @param item The item
939  *
940  * @ingroup Flipselector
941  */
942 EAPI void
943 elm_flipselector_item_del(Elm_Flipselector_Item *item)
944 {
945    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
946
947    Widget_Data *wd;
948
949    wd = elm_widget_data_get(item->base.widget);
950    if (!wd)
951      return;
952
953    if (wd->walking > 0)
954      {
955         item->deleted = EINA_TRUE;
956         return;
957      }
958
959    _flipselector_walk(wd);
960
961    wd->items = eina_list_remove(wd->items, item);
962    _item_free(item);
963    _sentinel_eval(wd);
964
965    _flipselector_unwalk(wd);
966 }
967
968 /**
969  * Get the label of a given flipselector item.
970  *
971  * @param item The item
972  * @return The label of a given item, or NULL if none
973  *
974  * @ingroup Flipselector
975  */
976 EAPI const char *
977 elm_flipselector_item_label_get(const Elm_Flipselector_Item *item)
978 {
979    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
980
981    Elm_Flipselector_Item *_item;
982    Widget_Data *wd;
983    Eina_List *l;
984
985    wd = elm_widget_data_get(item->base.widget);
986    if ((!wd) || (!wd->items))
987      return NULL;
988
989    EINA_LIST_FOREACH(wd->items, l, _item)
990      if (_item == item)
991        return item->label;
992
993    return NULL;
994 }
995
996 /**
997  * Set the label of a given flipselector item.
998  *
999  * @param item The item
1000  * @param label The text label string in UTF-8
1001  *
1002  * @ingroup Flipselector
1003  */
1004 EAPI void
1005 elm_flipselector_item_label_set(Elm_Flipselector_Item *item, const char *label)
1006 {
1007    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item);
1008
1009    Widget_Data *wd;
1010    Eina_List *l;
1011
1012    if ((!item) || (!label))
1013      return;
1014
1015    wd = elm_widget_data_get(item->base.widget);
1016    if ((!wd) || (!wd->items))
1017      return;
1018
1019    l = eina_list_data_find_list(wd->items, item);
1020    if (!l)
1021      return;
1022
1023    eina_stringshare_del(item->label);
1024    item->label = eina_stringshare_add_length(label, wd->max_len);
1025
1026    if (strlen(label) >
1027        strlen(elm_flipselector_item_label_get(DATA_GET(wd->sentinel))))
1028      wd->sentinel = l;
1029
1030    if (wd->current == l)
1031      {
1032         _update_view(item->base.widget);
1033         _sizing_eval(wd->self);
1034      }
1035
1036    return;
1037 }
1038
1039 /**
1040  * Gets the item before @p item in a flipselector.
1041  *
1042  * @param item The item
1043  * @return The item before the item @p item
1044  *
1045  * @ingroup Flipselector
1046  */
1047 EAPI Elm_Flipselector_Item *
1048 elm_flipselector_item_prev_get(Elm_Flipselector_Item *item)
1049 {
1050    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
1051
1052    Elm_Flipselector_Item *_item;
1053    Widget_Data *wd;
1054    Eina_List *l;
1055
1056    wd = elm_widget_data_get(item->base.widget);
1057    if ((!wd) || (!wd->items))
1058      return NULL;
1059
1060    EINA_LIST_FOREACH(wd->items, l, _item)
1061      if (_item == item)
1062        {
1063           l = eina_list_prev(l);
1064           if (!l)
1065             return NULL;
1066           return DATA_GET(l);
1067        }
1068
1069    return NULL;
1070 }
1071
1072 /**
1073  * Gets the item after @p item in a flipselector.
1074  *
1075  * @param item The item
1076  * @return The item after the item @p item
1077  *
1078  * @ingroup Flipselector
1079  */
1080 EAPI Elm_Flipselector_Item *
1081 elm_flipselector_item_next_get(Elm_Flipselector_Item *item)
1082 {
1083    ELM_FLIPSELECTOR_ITEM_CHECK_DELETED_RETURN(item, NULL);
1084
1085    Elm_Flipselector_Item *_item;
1086    Widget_Data *wd;
1087    Eina_List *l;
1088
1089    wd = elm_widget_data_get(item->base.widget);
1090    if ((!wd) || (!wd->items))
1091      return NULL;
1092
1093    EINA_LIST_FOREACH(wd->items, l, _item)
1094      if (_item == item)
1095        {
1096           l = eina_list_next(l);
1097           if (!l)
1098             return NULL;
1099           return DATA_GET(l);
1100        }
1101
1102    return NULL;
1103 }
1104
1105 /**
1106  * Set the flipping interval for the flipselector.
1107  *
1108  * @param obj The flipselector object
1109  * @param interval The interval value in seconds
1110  *
1111  * The interval value is decreased while the user flips the widget up
1112  * or down repeatedly. The next interval value is the previous
1113  * interval / 1.05, so it speeds up a bit. Default value is 0.85
1114  * seconds.
1115  *
1116  * @ingroup Flipselector
1117  */
1118 EAPI void
1119 elm_flipselector_interval_set(Evas_Object *obj, double interval)
1120 {
1121    ELM_CHECK_WIDTYPE(obj, widtype);
1122
1123    Widget_Data *wd = elm_widget_data_get(obj);
1124    if (!wd)
1125      return;
1126
1127    wd->first_interval = interval;
1128 }
1129
1130 /**
1131  * Get the flipping interval of the flipselector.
1132  *
1133  * @param obj The flipselector object
1134  * @return The value of the first interval in seconds
1135  *
1136  * The interval value is decreased while the user flips the widget up
1137  * or down repeatedly. The next interval value is the previous
1138  * interval / 1.05, so it speeds up a bit. Default value is 0.85
1139  * seconds.
1140  *
1141  * @ingroup Flipselector
1142  */
1143 EAPI double
1144 elm_flipselector_interval_get(const Evas_Object *obj)
1145 {
1146    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
1147
1148    Widget_Data *wd = elm_widget_data_get(obj);
1149
1150    if (!wd)
1151      return 0.0;
1152    return wd->first_interval;
1153 }