elementary/index - [E-devel] [Patch] elm_index patch
[framework/uifw/elementary.git] / src / lib / elm_index.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "els_box.h"
4
5 typedef struct _Widget_Data Widget_Data;
6
7 struct _Widget_Data
8 {
9    Evas_Object *base;
10    Evas_Object *event[2];
11    Evas_Object *bx[2]; // 2 - for now all that's supported
12    Eina_List *items; // 1 list. yes N levels, but only 2 for now and # of items will be small
13    int level;
14    Evas_Coord dx, dy;
15    Ecore_Timer *delay;
16    Eina_Bool level_active[2];
17    Eina_Bool horizontal : 1;
18    Eina_Bool active : 1;
19    Eina_Bool down : 1;
20 };
21
22 struct _Elm_Index_Item
23 {
24    ELM_WIDGET_ITEM;
25    const char *letter;
26    int level;
27    Eina_Bool selected : 1;
28 };
29
30 static const char *widtype = NULL;
31 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
32 static void _theme_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _index_box_auto_fill(Evas_Object *obj, Evas_Object *box, int level);
35 static void _index_box_clear(Evas_Object *obj, Evas_Object *box, int level);
36 static void _item_free(Elm_Index_Item *it);
37
38 static const char SIG_CHANGED[] = "changed";
39 static const char SIG_DELAY_CHANGED[] = "delay,changed";
40 static const char SIG_SELECTED[] = "selected";
41 static const char SIG_LEVEL_UP[] = "level,up";
42 static const char SIG_LEVEL_DOWN[] = "level,down";
43
44 static const Evas_Smart_Cb_Description _signals[] = {
45    {SIG_CHANGED, ""},
46    {SIG_DELAY_CHANGED, ""},
47    {SIG_SELECTED, ""},
48    {SIG_LEVEL_UP, ""},
49    {SIG_LEVEL_DOWN, ""},
50    {NULL, NULL}
51 };
52
53 static void
54 _del_pre_hook(Evas_Object *obj)
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57    if (!wd) return;
58    _index_box_clear(obj, wd->bx[wd->level], wd->level);
59    _index_box_clear(obj, wd->bx[0], 0);
60    while (wd->items) _item_free(wd->items->data);
61    if (wd->delay) ecore_timer_del(wd->delay);
62 }
63
64 static void
65 _del_hook(Evas_Object *obj)
66 {
67    Widget_Data *wd = elm_widget_data_get(obj);
68    free(wd);
69 }
70
71 static void
72 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
73 {
74    Widget_Data *wd = data;
75    if (!wd) return;
76    _els_box_layout(o, priv, wd->horizontal, 1, 0);
77 }
78
79 static void
80 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
81 {
82    Widget_Data *wd = elm_widget_data_get(obj);
83    if (!wd) return;
84    edje_object_signal_emit(wd->base, emission, source);
85 }
86
87 static void
88 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
89 {
90    Widget_Data *wd = elm_widget_data_get(obj);
91    if (!wd) return;
92    edje_object_signal_callback_add(wd->base, emission, source, func_cb, data);
93 }
94
95 static void
96 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
97 {
98    Widget_Data *wd = elm_widget_data_get(obj);
99    edje_object_signal_callback_del_full(wd->base, emission, source, func_cb,
100                                         data);
101 }
102
103 static void
104 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
105 {
106    Widget_Data *wd = elm_widget_data_get(obj);
107    if (!wd) return;
108    if (!wd->horizontal)
109      edje_object_mirrored_set(wd->base, rtl);
110 }
111
112 static void
113 _theme_hook(Evas_Object *obj)
114 {
115    Evas_Coord minw = 0, minh = 0;
116    Widget_Data *wd = elm_widget_data_get(obj);
117    if (!wd) return;
118    _elm_widget_mirrored_reload(obj);
119
120    _index_box_clear(obj, wd->bx[0], 0);
121    _index_box_clear(obj, wd->bx[1], 1);
122    if (wd->horizontal)
123      _elm_theme_object_set(obj, wd->base, "index", "base/horizontal", elm_widget_style_get(obj));
124    else
125      {
126         _elm_theme_object_set(obj, wd->base, "index", "base/vertical", elm_widget_style_get(obj));
127         _mirrored_set(obj, elm_widget_mirrored_get(obj));
128      }
129    edje_object_part_swallow(wd->base, "elm.swallow.event.0", wd->event[0]);
130    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
131    evas_object_size_hint_min_set(wd->event[0], minw, minh);
132    edje_object_part_swallow(wd->base, "elm.swallow.index.0", wd->bx[0]);
133    if (edje_object_part_exists(wd->base, "elm.swallow.index.1"))
134      {
135         if (!wd->bx[1])
136           {
137              wd->bx[1] = evas_object_box_add(evas_object_evas_get(wd->base));
138              evas_object_box_layout_set(wd->bx[1], _layout, wd, NULL);
139              elm_widget_sub_object_add(obj, wd->bx[1]);
140           }
141         edje_object_part_swallow(wd->base, "elm.swallow.index.1", wd->bx[1]);
142         evas_object_show(wd->bx[1]);
143      }
144    else if (wd->bx[1])
145      {
146         evas_object_del(wd->bx[1]);
147         wd->bx[1] = NULL;
148      }
149    if (edje_object_part_exists(wd->base, "elm.swallow.event.1"))
150      {
151         if (!wd->event[1])
152           {
153              wd->event[1] = evas_object_rectangle_add(evas_object_evas_get(wd->base));
154              evas_object_color_set(wd->event[1], 0, 0, 0, 0);
155              elm_widget_sub_object_add(obj, wd->event[1]);
156           }
157         edje_object_part_swallow(wd->base, "elm.swallow.event.1", wd->event[1]);
158         evas_object_size_hint_min_set(wd->event[1], minw, minh);
159      }
160    else if (wd->event[1])
161      {
162         evas_object_del(wd->event[1]);
163         wd->event[1] = NULL;
164      }
165    edje_object_message_signal_process(wd->base);
166    edje_object_scale_set(wd->base, elm_widget_scale_get(obj) * _elm_config->scale);
167    _sizing_eval(obj);
168    _index_box_auto_fill(obj, wd->bx[0], 0);
169    if (wd->active)
170      if (wd->level == 1)
171        _index_box_auto_fill(obj, wd->bx[1], 1);
172 }
173
174 static void
175 _sizing_eval(Evas_Object *obj)
176 {
177    Widget_Data *wd = elm_widget_data_get(obj);
178    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
179    if (!wd) return;
180    edje_object_size_min_calc(wd->base, &minw, &minh);
181    evas_object_size_hint_min_set(obj, minw, minh);
182    evas_object_size_hint_max_set(obj, maxw, maxh);
183 }
184
185 static Elm_Index_Item *
186 _item_new(Evas_Object *obj, const char *letter, const void *item)
187 {
188    Widget_Data *wd = elm_widget_data_get(obj);
189    Elm_Index_Item *it;
190    if (!wd) return NULL;
191    it = elm_widget_item_new(obj, Elm_Index_Item);
192    if (!it) return NULL;
193    it->letter = eina_stringshare_add(letter);
194    it->base.data = item;
195    it->level = wd->level;
196    return it;
197 }
198
199 static Elm_Index_Item *
200 _item_find(Evas_Object *obj, const void *item)
201 {
202    Widget_Data *wd = elm_widget_data_get(obj);
203    Eina_List *l;
204    Elm_Index_Item *it;
205    if (!wd) return NULL;
206    EINA_LIST_FOREACH(wd->items, l, it)
207       if (it->base.data == item) return it;
208    return NULL;
209 }
210
211 static void
212 _item_free(Elm_Index_Item *it)
213 {
214    Widget_Data *wd = elm_widget_data_get(WIDGET(it));
215    if (!wd) return;
216    wd->items = eina_list_remove(wd->items, it);
217    elm_widget_item_pre_notify_del(it);
218    eina_stringshare_del(it->letter);
219    elm_widget_item_del(it);
220 }
221
222 // FIXME: always have index filled
223 static void
224 _index_box_auto_fill(Evas_Object *obj, Evas_Object *box, int level)
225 {
226    Widget_Data *wd = elm_widget_data_get(obj);
227    Eina_Bool rtl;
228    Eina_List *l;
229    Elm_Index_Item *it;
230    Evas_Coord mw, mh, w, h;
231    int i = 0;
232    if (!wd) return;
233    if (wd->level_active[level]) return;
234    rtl = elm_widget_mirrored_get(obj);
235    evas_object_geometry_get(box, NULL, NULL, &w, &h);
236    EINA_LIST_FOREACH(wd->items, l, it)
237      {
238         Evas_Object *o;
239         const char *stacking;
240
241         if (it->level != level) continue;
242         o = edje_object_add(evas_object_evas_get(obj));
243         VIEW(it) = o;
244         edje_object_mirrored_set(VIEW(it), rtl);
245         if (i & 0x1)
246           _elm_theme_object_set(obj, o, "index", "item_odd/vertical", elm_widget_style_get(obj));
247         else
248           _elm_theme_object_set(obj, o, "index", "item/vertical", elm_widget_style_get(obj));
249         edje_object_part_text_set(o, "elm.text", it->letter);
250         edje_object_size_min_restricted_calc(o, &mw, &mh, 0, 0);
251         evas_object_size_hint_min_set(o, mw, mh);
252         evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
253         evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
254         elm_widget_sub_object_add(obj, o);
255         evas_object_box_append(box, o);
256         stacking = edje_object_data_get(o, "stacking");
257         if (stacking)
258           {
259              if (!strcmp(stacking, "below")) evas_object_lower(o);
260              else if (!strcmp(stacking, "above")) evas_object_raise(o);
261           }
262         evas_object_show(o);
263         i++;
264         evas_object_smart_calculate(box); // force a calc so we know the size
265         evas_object_size_hint_min_get(box, &mw, &mh);
266         if (mh > h)
267           {
268              _index_box_clear(obj, box, level);
269              if (i > 0)
270                {
271                   // FIXME: only i objects fit! try again. overflows right now
272                }
273           }
274      }
275    evas_object_smart_calculate(box);
276    wd->level_active[level] = 1;
277 }
278
279 static void
280 _index_box_clear(Evas_Object *obj, Evas_Object *box __UNUSED__, int level)
281 {
282    Widget_Data *wd = elm_widget_data_get(obj);
283    Eina_List *l;
284    Elm_Index_Item *it;
285    if (!wd) return;
286    if (!wd->level_active[level]) return;
287    EINA_LIST_FOREACH(wd->items, l, it)
288      {
289         if (!VIEW(it)) continue;
290         if (it->level != level) continue;
291         evas_object_del(VIEW(it));
292         VIEW(it) = NULL;
293      }
294    wd->level_active[level] = 0;
295 }
296
297 static Eina_Bool
298 _delay_change(void *data)
299 {
300    Widget_Data *wd = elm_widget_data_get(data);
301    void *d;
302    if (!wd) return ECORE_CALLBACK_CANCEL;
303    wd->delay = NULL;
304    d = (void *)elm_index_item_selected_get(data, wd->level);
305    if (d) evas_object_smart_callback_call(data, SIG_DELAY_CHANGED, d);
306    return ECORE_CALLBACK_CANCEL;
307 }
308
309 static void
310 _sel_eval(Evas_Object *obj, Evas_Coord evx, Evas_Coord evy)
311 {
312    Widget_Data *wd = elm_widget_data_get(obj);
313    Elm_Index_Item *it, *it_closest, *it_last;
314    Eina_List *l;
315    Evas_Coord x, y, w, h, bx, by, bw, bh, xx, yy;
316    double cdv = 0.5;
317    Evas_Coord dist;
318    char *label = NULL, *last = NULL;
319    int i;
320    if (!wd) return;
321    for (i = 0; i <= wd->level; i++)
322      {
323         it_last = NULL;
324         it_closest  = NULL;
325         dist = 0x7fffffff;
326         evas_object_geometry_get(wd->bx[i], &bx, &by, &bw, &bh);
327         EINA_LIST_FOREACH(wd->items, l, it)
328           {
329              if (!((it->level == i) && (VIEW(it)))) continue;
330              if ((VIEW(it)) && (it->level != wd->level))
331                {
332                   if (it->selected)
333                     {
334                        it_closest = it;
335                        break;
336                     }
337                   continue;
338                }
339              if (it->selected)
340                {
341                   it_last = it;
342                   it->selected = 0;
343                }
344              evas_object_geometry_get(VIEW(it), &x, &y, &w, &h);
345              xx = x + (w / 2);
346              yy = y + (h / 2);
347              x = evx - xx;
348              y = evy - yy;
349              x = (x * x) + (y * y);
350              if ((x < dist) || (!it_closest))
351                {
352                   if (wd->horizontal)
353                     cdv = (double)(xx - bx) / (double)bw;
354                   else
355                     cdv = (double)(yy - by) / (double)bh;
356                   it_closest = it;
357                   dist = x;
358                }
359           }
360         if ((!i) && (!wd->level))
361           edje_object_part_drag_value_set(wd->base, "elm.dragable.index.1",
362                                           cdv, cdv);
363         if (it_closest) it_closest->selected = 1;
364         if (it_closest != it_last)
365           {
366              if (it_last)
367                {
368                   const char *stacking, *selectraise;
369
370                   it = it_last;
371                   edje_object_signal_emit(VIEW(it), "elm,state,inactive", "elm");
372                   stacking = edje_object_data_get(VIEW(it), "stacking");
373                   selectraise = edje_object_data_get(VIEW(it), "selectraise");
374                   if ((selectraise) && (!strcmp(selectraise, "on")))
375                     {
376                        if ((stacking) && (!strcmp(stacking, "below")))
377                          evas_object_lower(VIEW(it));
378                     }
379                }
380              if (it_closest)
381                {
382                   const char *selectraise;
383
384                   it = it_closest;
385                   edje_object_signal_emit(VIEW(it), "elm,state,active", "elm");
386                   selectraise = edje_object_data_get(VIEW(it), "selectraise");
387                   if ((selectraise) && (!strcmp(selectraise, "on")))
388                     evas_object_raise(VIEW(it));
389                   evas_object_smart_callback_call((void *)obj, SIG_CHANGED, (void *)it->base.data);
390                   if (wd->delay) ecore_timer_del(wd->delay);
391                   wd->delay = ecore_timer_add(0.2, _delay_change, obj);
392                }
393           }
394         if (it_closest)
395           {
396              it = it_closest;
397              if (!last)
398                last = strdup(it->letter);
399              else
400                {
401                   if (!label) label = strdup(last);
402                   else
403                     {
404                        /* FIXME: realloc return NULL if the request fails */
405                        label = realloc(label, strlen(label) + strlen(last) + 1);
406                        strcat(label, last);
407                     }
408                   free(last);
409                   last = strdup(it->letter);
410                }
411           }
412      }
413    if (!label) label = strdup("");
414    if (!last) last = strdup("");
415    edje_object_part_text_set(wd->base, "elm.text.body", label);
416    edje_object_part_text_set(wd->base, "elm.text", last);
417    free(label);
418    free(last);
419 }
420
421 static void
422 _wheel(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
423 {
424    Widget_Data *wd = elm_widget_data_get(data);
425    //   Evas_Event_Mouse_Wheel *ev = event_info;
426    //   Evas_Object *obj = o;
427    if (!wd) return;
428 }
429
430 static void
431 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
432 {
433    Widget_Data *wd = elm_widget_data_get(data);
434    Evas_Event_Mouse_Down *ev = event_info;
435    Evas_Coord x, y, w;
436    if (!wd) return;
437    if (ev->button != 1) return;
438    wd->down = 1;
439    evas_object_geometry_get(wd->base, &x, &y, &w, NULL);
440    wd->dx = ev->canvas.x - x;
441    wd->dy = ev->canvas.y - y;
442    elm_index_active_set(data, 1);
443    _sel_eval(data, ev->canvas.x, ev->canvas.y);
444    edje_object_part_drag_value_set(wd->base, "elm.dragable.pointer",
445                                    (!edje_object_mirrored_get(wd->base)) ? wd->dx : (wd->dx - w), wd->dy);
446    if (wd->items)
447       edje_object_signal_emit(wd->base, "elm,indicator,state,active", "elm");
448 }
449
450 static void
451 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
452 {
453    Widget_Data *wd = elm_widget_data_get(data);
454    Evas_Event_Mouse_Up *ev = event_info;
455    void *d;
456    if (!wd) return;
457    if (ev->button != 1) return;
458    wd->down = 0;
459    d = (void *)elm_index_item_selected_get(data, wd->level);
460    if (d) evas_object_smart_callback_call(data, SIG_SELECTED, d);
461    elm_index_active_set(data, 0);
462    edje_object_signal_emit(wd->base, "elm,state,level,0", "elm");
463    edje_object_signal_emit(wd->base, "elm,indicator,state,inactive", "elm");
464 }
465
466 static void
467 _mouse_move(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
468 {
469    Widget_Data *wd = elm_widget_data_get(data);
470    Evas_Event_Mouse_Move *ev = event_info;
471    Evas_Coord minw = 0, minh = 0, x, y, dx, adx, w;
472    char buf[1024];
473    if (!wd) return;
474    if (!wd->down) return;
475    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
476    evas_object_geometry_get(wd->base, &x, &y, &w, NULL);
477    x = ev->cur.canvas.x - x;
478    y = ev->cur.canvas.y - y;
479    dx = x - wd->dx;
480    adx = dx;
481    if (adx < 0) adx = -dx;
482    edje_object_part_drag_value_set(wd->base, "elm.dragable.pointer"
483                                    , (!edje_object_mirrored_get(wd->base)) ? x : (x - w), y);
484    if (!wd->horizontal)
485      {
486         if (adx > minw)
487           {
488              if (!wd->level)
489                {
490                   wd->level = 1;
491                   snprintf(buf, sizeof(buf), "elm,state,level,%i", wd->level);
492                   edje_object_signal_emit(wd->base, buf, "elm");
493                   evas_object_smart_callback_call(data, SIG_LEVEL_UP, NULL);
494                }
495           }
496         else
497           {
498              if (wd->level == 1)
499                {
500                   wd->level = 0;
501                   snprintf(buf, sizeof(buf), "elm,state,level,%i", wd->level);
502                   edje_object_signal_emit(wd->base, buf, "elm");
503                   evas_object_smart_callback_call(data, SIG_LEVEL_DOWN, NULL);
504                }
505           }
506      }
507    _sel_eval(data, ev->cur.canvas.x, ev->cur.canvas.y);
508 }
509
510 EAPI Evas_Object *
511 elm_index_add(Evas_Object *parent)
512 {
513    Evas_Object *obj;
514    Evas_Object *o;
515    Evas *e;
516    Widget_Data *wd;
517    Evas_Coord minw, minh;
518
519    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
520
521    ELM_SET_WIDTYPE(widtype, "index");
522    elm_widget_type_set(obj, "index");
523    elm_widget_sub_object_add(parent, obj);
524    elm_widget_data_set(obj, wd);
525    elm_widget_del_hook_set(obj, _del_hook);
526    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
527    elm_widget_theme_hook_set(obj, _theme_hook);
528    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
529    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
530    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
531    elm_widget_can_focus_set(obj, EINA_FALSE);
532
533    wd->horizontal = EINA_FALSE;
534
535    wd->base = edje_object_add(e);
536    _elm_theme_object_set(obj, wd->base, "index", "base/vertical", "default");
537    elm_widget_resize_object_set(obj, wd->base);
538
539    o = evas_object_rectangle_add(e);
540    wd->event[0] = o;
541    evas_object_color_set(o, 0, 0, 0, 0);
542    minw = minh = 0;
543    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
544    evas_object_size_hint_min_set(o, minw, minh);
545    edje_object_part_swallow(wd->base, "elm.swallow.event.0", o);
546    elm_widget_sub_object_add(obj, o);
547    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_WHEEL, _wheel, obj);
548    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, obj);
549    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, _mouse_up, obj);
550    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move, obj);
551    evas_object_show(o);
552    if (edje_object_part_exists(wd->base, "elm.swallow.event.1"))
553      {
554         o = evas_object_rectangle_add(e);
555         wd->event[1] = o;
556         evas_object_color_set(o, 0, 0, 0, 0);
557         evas_object_size_hint_min_set(o, minw, minh);
558         edje_object_part_swallow(wd->base, "elm.swallow.event.1", o);
559         elm_widget_sub_object_add(obj, o);
560      }
561
562    wd->bx[0] = evas_object_box_add(e);
563    evas_object_box_layout_set(wd->bx[0], _layout, wd, NULL);
564    elm_widget_sub_object_add(obj, wd->bx[0]);
565    edje_object_part_swallow(wd->base, "elm.swallow.index.0", wd->bx[0]);
566    evas_object_show(wd->bx[0]);
567
568    if (edje_object_part_exists(wd->base, "elm.swallow.index.1"))
569      {
570         wd->bx[1] = evas_object_box_add(e);
571         evas_object_box_layout_set(wd->bx[1], _layout, wd, NULL);
572         elm_widget_sub_object_add(obj, wd->bx[1]);
573         edje_object_part_swallow(wd->base, "elm.swallow.index.1", wd->bx[1]);
574         evas_object_show(wd->bx[1]);
575      }
576
577    evas_object_smart_callbacks_descriptions_set(obj, _signals);
578
579    _mirrored_set(obj, elm_widget_mirrored_get(obj));
580    _sizing_eval(obj);
581    return obj;
582 }
583
584 EAPI void
585 elm_index_active_set(Evas_Object *obj, Eina_Bool active)
586 {
587    ELM_CHECK_WIDTYPE(obj, widtype);
588    Widget_Data *wd = elm_widget_data_get(obj);
589    if (!wd) return;
590    if (wd->active == active) return;
591    wd->active = active;
592    wd->level = 0;
593    if (wd->active)
594      {
595         _index_box_clear(obj, wd->bx[1], 1);
596         _index_box_auto_fill(obj, wd->bx[0], 0);
597         edje_object_signal_emit(wd->base, "elm,state,active", "elm");
598      }
599    else
600      edje_object_signal_emit(wd->base, "elm,state,inactive", "elm");
601 }
602
603 EAPI Eina_Bool
604 elm_index_active_get(const Evas_Object *obj)
605 {
606    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
607    Widget_Data *wd = elm_widget_data_get(obj);
608    if (!wd) return EINA_FALSE;
609    return wd->active;
610 }
611
612 EAPI void
613 elm_index_item_level_set(Evas_Object *obj, int level)
614 {
615    ELM_CHECK_WIDTYPE(obj, widtype);
616    Widget_Data *wd = elm_widget_data_get(obj);
617    if (!wd) return;
618    if (wd->level == level) return;
619    wd->level = level;
620 }
621
622 EAPI int
623 elm_index_item_level_get(const Evas_Object *obj)
624 {
625    ELM_CHECK_WIDTYPE(obj, widtype) 0;
626    Widget_Data *wd = elm_widget_data_get(obj);
627    if (!wd) return 0;
628    return wd->level;
629 }
630
631 EAPI void *
632 elm_index_item_selected_get(const Evas_Object *obj, int level)
633 {
634    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
635    Widget_Data *wd = elm_widget_data_get(obj);
636    Eina_List *l;
637    Elm_Index_Item *it;
638    if (!wd) return NULL;
639    EINA_LIST_FOREACH(wd->items, l, it)
640       if ((it->selected) && (it->level == level))
641         return elm_widget_item_data_get(it);
642    return NULL;
643 }
644
645 EAPI void
646 elm_index_item_append(Evas_Object *obj, const char *letter, const void *item)
647 {
648    ELM_CHECK_WIDTYPE(obj, widtype);
649    Widget_Data *wd = elm_widget_data_get(obj);
650    Elm_Index_Item *it;
651    if (!wd) return;
652    it = _item_new(obj, letter, item);
653    if (!it) return;
654    wd->items = eina_list_append(wd->items, it);
655    _index_box_clear(obj, wd->bx[wd->level], wd->level);
656 }
657
658 EAPI void
659 elm_index_item_prepend(Evas_Object *obj, const char *letter, const void *item)
660 {
661    ELM_CHECK_WIDTYPE(obj, widtype);
662    Widget_Data *wd = elm_widget_data_get(obj);
663    Elm_Index_Item *it;
664
665    if (!wd) return;
666    it = _item_new(obj, letter, item);
667    if (!it) return;
668    wd->items = eina_list_prepend(wd->items, it);
669    _index_box_clear(obj, wd->bx[wd->level], wd->level);
670 }
671
672 EAPI void
673 elm_index_item_append_relative(Evas_Object *obj, const char *letter, const void *item, const void *relative)
674 {
675    ELM_CHECK_WIDTYPE(obj, widtype);
676    Widget_Data *wd = elm_widget_data_get(obj);
677    Elm_Index_Item *it, *it_rel;
678    if (!wd) return;
679    if (!relative)
680      {
681         elm_index_item_append(obj, letter, item);
682         return;
683      }
684    it_rel = _item_find(obj, relative);
685    if (!it_rel)
686      {
687         elm_index_item_append(obj, letter, item);
688         return;
689      }
690    it = _item_new(obj, letter, item);
691    if (!it) return;
692    wd->items = eina_list_append_relative(wd->items, it, it_rel);
693    _index_box_clear(obj, wd->bx[wd->level], wd->level);
694 }
695
696 EAPI void
697 elm_index_item_prepend_relative(Evas_Object *obj, const char *letter, const void *item, const void *relative)
698 {
699    ELM_CHECK_WIDTYPE(obj, widtype);
700    Widget_Data *wd = elm_widget_data_get(obj);
701    Elm_Index_Item *it, *it_rel;
702    if (!wd) return;
703    if (!relative)
704      {
705         elm_index_item_prepend(obj, letter, item);
706         return;
707      }
708    it_rel = _item_find(obj, relative);
709    if (!it_rel)
710      {
711         elm_index_item_append(obj, letter, item);
712         return;
713      }
714    it = _item_new(obj, letter, item);
715    if (!it) return;
716    wd->items = eina_list_prepend_relative(wd->items, it, it_rel);
717    _index_box_clear(obj, wd->bx[wd->level], wd->level);
718 }
719
720 EAPI void
721 elm_index_item_sorted_insert(Evas_Object *obj, const char *letter, const void *item, Eina_Compare_Cb cmp_func, Eina_Compare_Cb cmp_data_func)
722 {
723    ELM_CHECK_WIDTYPE(obj, widtype);
724    Widget_Data *wd = elm_widget_data_get(obj);
725    Eina_List *lnear;
726    Elm_Index_Item *it;
727    int cmp;
728
729    if (!wd) return;
730    if (!(wd->items))
731      {
732         elm_index_item_append(obj, letter, item);
733         return;
734      }
735
736    it = _item_new(obj, letter, item);
737    if (!it) return;
738
739    lnear = eina_list_search_sorted_near_list(wd->items, cmp_func, it, &cmp);
740    if (cmp < 0)
741      wd->items =  eina_list_append_relative_list(wd->items, it, lnear);
742    else if (cmp > 0)
743      wd->items = eina_list_prepend_relative_list(wd->items, it, lnear);
744    else
745      {
746         /* If cmp_data_func is not provided, append a duplicated item */
747         if (!cmp_data_func)
748           wd->items =  eina_list_append_relative_list(wd->items, it, lnear);
749         else
750           {
751              Elm_Index_Item *p_it = eina_list_data_get(lnear);
752              if (cmp_data_func(p_it->base.data, it->base.data) >= 0)
753                p_it->base.data = it->base.data;
754              _item_free(it);
755           }
756      }
757
758    _index_box_clear(obj, wd->bx[wd->level], wd->level);
759 }
760
761 EAPI void
762 elm_index_item_del(Evas_Object *obj, const void *item)
763 {
764    ELM_CHECK_WIDTYPE(obj, widtype);
765    Widget_Data *wd = elm_widget_data_get(obj);
766    Elm_Index_Item *it;
767    if (!wd) return;
768    it = _item_find(obj, item);
769    if (!it) return;
770    _item_free(it);
771    _index_box_clear(obj, wd->bx[wd->level], wd->level);
772 }
773
774 EAPI Elm_Index_Item *
775 elm_index_item_find(Evas_Object *obj, const void *item)
776 {
777    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
778    Widget_Data *wd = elm_widget_data_get(obj);
779    if (!wd) return NULL;
780    return _item_find(obj, item);
781 }
782
783 EAPI void
784 elm_index_item_clear(Evas_Object *obj)
785 {
786    ELM_CHECK_WIDTYPE(obj, widtype);
787    Widget_Data *wd = elm_widget_data_get(obj);
788    Elm_Index_Item *it;
789    Eina_List *l, *clear = NULL;
790    if (!wd) return;
791    _index_box_clear(obj, wd->bx[wd->level], wd->level);
792    EINA_LIST_FOREACH(wd->items, l, it)
793      {
794         if (it->level != wd->level) continue;
795         clear = eina_list_append(clear, it);
796      }
797    EINA_LIST_FREE(clear, it) _item_free(it);
798 }
799
800 EAPI void
801 elm_index_item_go(Evas_Object *obj, int level __UNUSED__)
802 {
803    ELM_CHECK_WIDTYPE(obj, widtype);
804    Widget_Data *wd = elm_widget_data_get(obj);
805    if (!wd) return;
806    _index_box_auto_fill(obj, wd->bx[0], 0);
807    if (wd->level == 1) _index_box_auto_fill(obj, wd->bx[1], 1);
808 }
809
810 EAPI void *
811 elm_index_item_data_get(const Elm_Index_Item *it)
812 {
813    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
814    return elm_widget_item_data_get(it);
815 }
816
817 EAPI void
818 elm_index_item_data_set(Elm_Index_Item *it, const void *data)
819 {
820    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it);
821    elm_widget_item_data_set(it, data);
822 }
823
824 EAPI void
825 elm_index_item_del_cb_set(Elm_Index_Item *it, Evas_Smart_Cb func)
826 {
827    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it);
828    elm_widget_item_del_cb_set(it, func);
829 }
830
831 EAPI const char *
832 elm_index_item_letter_get(const Elm_Index_Item *it)
833 {
834    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
835    return it->letter;
836 }
837