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