Merge "[stackicon] remove trailing whitespaces"
[framework/uifw/elementary.git] / src / lib / elm_index.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4 #include <Elementary.h>
5 #include "elm_priv.h"
6 #include "els_box.h"
7
8 #define MIN_GRP_SIZE 2 //for symmetry it is 2, otherwise it can be 1 and zero have no meaning.
9 #define MIN_PIXEL_VALUE 1 //Min pixel value is highly dependent on touch sensitivity support.
10 #define MIN_OBJ_HEIGHT 24 //should be taken from .edc file.
11 /*
12  *  use for find view toplevel
13  */
14 #define SET_VIEW_LEVEL(wd, view_level)\
15    view_level = wd->level;\
16    while ((!wd->tot_items_count[view_level]) && view_level)\
17      {\
18         view_level--; \
19      }
20
21 typedef struct _Widget_Data Widget_Data;
22
23 typedef struct _PlacementPart PlacementPart;
24
25 struct _Widget_Data
26 {
27    Evas_Object *base;
28    Evas_Object *event[2];
29    Evas_Object *bx[2]; // 2 - for now all that's supported
30    Eina_List *items; // 1 list. yes N levels, but only 2 for now and # of items will be small
31    char *popup_str[2];
32    int level;
33    int max_supp_items_count;
34    int tot_items_count[2];
35    int min_obj_height, max_grp_size;
36    int min_1st_level_obj_height;
37    int items_count;
38    Evas_Coord dx, dy;
39    Evas_Coord pwidth, pheight;
40    Ecore_Timer *delay;
41    const char *special_char;
42    Eina_Bool level_active[2];
43    Eina_Bool horizontal : 1;
44    Eina_Bool active : 1;
45    Eina_Bool down : 1;
46    Eina_Bool hide_button : 1;
47    double scale_factor;
48 };
49
50 struct _Elm_Index_Item
51 {
52    ELM_WIDGET_ITEM;
53    const char *letter, *vis_letter;
54    int level, size;
55    Eina_Bool selected : 1;
56 };
57
58 struct _PlacementPart
59 {
60    int start;
61    int count;
62 };
63
64 static const char *widtype = NULL;
65
66 static void _del_hook(Evas_Object *obj);
67 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
68 static void _theme_hook(Evas_Object *obj);
69 static void _sizing_eval(Evas_Object *obj);
70 static void _index_box_auto_fill(Evas_Object *obj, Evas_Object *box, int level);
71 static void _index_box_clear(Evas_Object *obj, Evas_Object *box, int level);
72 static void _item_free(Elm_Index_Item *it);
73 static void _index_process(Evas_Object *obj);
74
75 static const char SIG_CHANGED[] = "changed";
76 static const char SIG_DELAY_CHANGED[] = "delay,changed";
77 static const char SIG_SELECTED[] = "selected";
78 static const char SIG_LEVEL_UP[] = "level,up";
79 static const char SIG_LEVEL_DOWN[] = "level,down";
80
81 static const Evas_Smart_Cb_Description _signals[] = {
82    {SIG_CHANGED, ""},
83    {SIG_DELAY_CHANGED, ""},
84    {SIG_SELECTED, ""},
85    {SIG_LEVEL_UP, ""},
86    {SIG_LEVEL_DOWN, ""},
87    {NULL, NULL}
88 };
89 /* Free a block allocated by `malloc', `realloc' or `calloc' one by one*/
90 static void
91 _del_pre_hook(Evas_Object *obj)
92 {
93    Widget_Data *wd = elm_widget_data_get(obj);
94    if (!wd) return;
95    _index_box_clear(obj, wd->bx[wd->level], wd->level);
96    _index_box_clear(obj, wd->bx[0], 0);
97    while (wd->items) _item_free(wd->items->data);
98    if (wd->delay) ecore_timer_del(wd->delay);
99
100    if(wd->popup_str[0]) free(wd->popup_str[0]);
101    if(wd->popup_str[1]) free(wd->popup_str[1]);
102 }
103
104 static void
105 _del_hook(Evas_Object *obj)
106 {
107    Widget_Data *wd = elm_widget_data_get(obj);
108    free(wd);
109 }
110
111 static void
112 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
113 {
114    Widget_Data *wd = data;
115    if (!wd) return;
116    _els_box_layout(o, priv, wd->horizontal, 0, 0);
117 }
118
119 static void
120 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
121 {
122    Widget_Data *wd = elm_widget_data_get(obj);
123    if (!wd) return;
124    edje_object_signal_emit(wd->base, emission, source);
125 }
126
127 static void
128 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
129 {
130    Widget_Data *wd = elm_widget_data_get(obj);
131    if (!wd) return;
132    edje_object_signal_callback_add(wd->base, emission, source, func_cb, data);
133 }
134
135 static void
136 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
137 {
138    Widget_Data *wd = elm_widget_data_get(obj);
139    edje_object_signal_callback_del_full(wd->base, emission, source, func_cb,
140                                         data);
141 }
142
143 static void
144 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
145 {
146    Widget_Data *wd = elm_widget_data_get(obj);
147    if (!wd) return;
148    if (!wd->horizontal)
149      edje_object_mirrored_set(wd->base, rtl);
150 }
151
152 static void
153 _theme_hook(Evas_Object *obj)
154 {
155    Evas_Coord minw = 0, minh = 0;
156    Widget_Data *wd = elm_widget_data_get(obj);
157    if (!wd) return;
158    _elm_widget_mirrored_reload(obj);
159
160    _index_box_clear(obj, wd->bx[0], 0);
161    _index_box_clear(obj, wd->bx[1], 1);
162    if (wd->horizontal)
163      _elm_theme_object_set(obj, wd->base, "index", "base/horizontal", elm_widget_style_get(obj));
164    else
165      {
166         _elm_theme_object_set(obj, wd->base, "index", "base/vertical", elm_widget_style_get(obj));
167         _mirrored_set(obj, elm_widget_mirrored_get(obj));
168      }
169    edje_object_part_swallow(wd->base, "elm.swallow.event.0", wd->event[0]);
170    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
171    evas_object_size_hint_min_set(wd->event[0], minw, minh);
172
173    edje_object_part_swallow(wd->base, "elm.swallow.index.0", wd->bx[0]);
174    if (edje_object_part_exists(wd->base, "elm.swallow.index.1"))
175      {
176         if (!wd->bx[1])
177           {
178              wd->bx[1] = evas_object_box_add(evas_object_evas_get(wd->base));
179              evas_object_box_layout_set(wd->bx[1], _layout, wd, NULL);
180              elm_widget_sub_object_add(obj, wd->bx[1]);
181           }
182         edje_object_part_swallow(wd->base, "elm.swallow.index.1", wd->bx[1]);
183         evas_object_show(wd->bx[1]);
184      }
185    else if (wd->bx[1])
186      {
187         evas_object_del(wd->bx[1]);
188         wd->bx[1] = NULL;
189      }
190    if (edje_object_part_exists(wd->base, "elm.swallow.event.1"))
191      {
192         if (!wd->event[1])
193           {
194              wd->event[1] = evas_object_rectangle_add(evas_object_evas_get(wd->base));
195              evas_object_color_set(wd->event[1], 0, 0, 0, 0);
196              elm_widget_sub_object_add(obj, wd->event[1]);
197           }
198         edje_object_part_swallow(wd->base, "elm.swallow.event.1", wd->event[1]);
199         evas_object_size_hint_min_set(wd->event[1], minw, minh);
200      }
201    else if (wd->event[1])
202      {
203         evas_object_del(wd->event[1]);
204         wd->event[1] = NULL;
205      }
206    edje_object_message_signal_process(wd->base);
207    edje_object_scale_set(wd->base, elm_widget_scale_get(obj) * _elm_config->scale);
208    _sizing_eval(obj);
209    _index_box_auto_fill(obj, wd->bx[0], 0);
210    if (wd->active)
211      if (wd->level == 1)
212        _index_box_auto_fill(obj, wd->bx[1], 1);
213 }
214
215 static void
216 _sizing_eval(Evas_Object *obj)
217 {
218    Widget_Data *wd = elm_widget_data_get(obj);
219    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
220    if (!wd) return;
221    edje_object_size_min_calc(wd->base, &minw, &minh);
222    evas_object_size_hint_min_set(obj, minw, minh);
223    evas_object_size_hint_max_set(obj, maxw, maxh);
224 }
225
226 static Elm_Index_Item *
227 _item_new(Evas_Object *obj, const char *letter, const void *item)
228 {
229    Widget_Data *wd = elm_widget_data_get(obj);
230    Elm_Index_Item *it;
231    if (!wd) return NULL;
232    it = elm_widget_item_new(obj, Elm_Index_Item);
233    if (!it) return NULL;
234    it->base.data = item;
235    it->level = wd->level;
236    if(wd->level == 0)
237      it->size =  wd->min_obj_height;
238    else
239      it->size =  wd->min_1st_level_obj_height;
240    if(letter)
241      {
242         it->letter = eina_stringshare_add(letter);
243         it->vis_letter = eina_stringshare_add(letter);
244      }
245    else
246      {
247         _item_free(it);
248         return NULL;
249      }
250    return it;
251 }
252
253 static Elm_Index_Item *
254 _item_find(Evas_Object *obj, const void *item)
255 {
256    Widget_Data *wd = elm_widget_data_get(obj);
257    Eina_List *l;
258    Elm_Index_Item *it;
259    if (!wd) return NULL;
260    EINA_LIST_FOREACH(wd->items, l, it)
261       if (it->base.data == item) return it;
262    return NULL;
263 }
264
265 static void
266 _item_free(Elm_Index_Item *it)
267 {
268    Widget_Data *wd = elm_widget_data_get(WIDGET(it));
269    if (!wd) return;
270
271    wd->items = eina_list_remove(wd->items, it);
272    elm_widget_item_pre_notify_del(it);
273    eina_stringshare_del(it->letter);
274    eina_stringshare_del(it->vis_letter);
275    elm_widget_item_del(it);
276 }
277
278 // FIXME: always have index filled
279 static void
280 _index_box_auto_fill(Evas_Object *obj, Evas_Object *box, int level)
281 {
282    Widget_Data *wd = elm_widget_data_get(obj);
283    Eina_Bool rtl;
284    Eina_List *l;
285    Elm_Index_Item *it;
286    Evas_Coord mw, mh, w, h;
287    int i = 0;
288    if (!wd) return;
289    if (wd->level_active[level]) return;
290    rtl = elm_widget_mirrored_get(obj);
291    evas_object_geometry_get(box, NULL, NULL, &w, &h);
292    EINA_LIST_FOREACH(wd->items, l, it)
293      {
294         Evas_Object *o;
295         const char *stacking;
296
297         if (it->level != level) continue;
298         if(i > wd->max_supp_items_count) break;
299
300         o = edje_object_add(evas_object_evas_get(obj));
301         VIEW(it) = o;
302         edje_object_mirrored_set(VIEW(it), rtl);
303         if (i & 0x1)
304           _elm_theme_object_set(obj, o, "index", "item_odd/vertical", elm_widget_style_get(obj));
305         else
306           _elm_theme_object_set(obj, o, "index", "item/vertical", elm_widget_style_get(obj));
307         edje_object_part_text_set(o, "elm.text", it->letter);
308         edje_object_size_min_restricted_calc(o, &mw, &mh, 0, 0);
309         evas_object_size_hint_min_set(o, mw, mh);
310         evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
311         evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
312         edje_object_part_text_set(o, "elm.text", it->vis_letter);
313         evas_object_size_hint_min_set(o, mw, it->size);
314         evas_object_size_hint_max_set(o, mw, it->size);
315         evas_object_resize(o, mw, it->size);
316
317         elm_widget_sub_object_add(obj, o);
318         evas_object_box_append(box, o);
319         stacking = edje_object_data_get(o, "stacking");
320         if (stacking)
321           {
322              if (!strcmp(stacking, "below")) evas_object_lower(o);
323              else if (!strcmp(stacking, "above")) evas_object_raise(o);
324           }
325         evas_object_show(o);
326         i++;
327         if(level == 1)
328           wd->tot_items_count[1] = i;
329         evas_object_smart_calculate(box); // force a calc so we know the size
330         evas_object_size_hint_min_get(box, &mw, &mh);
331         if (mh > h)
332           {
333              _index_box_clear(obj, box, level);
334              if (i > 0)
335                {
336                   // FIXME: only i objects fit! try again. overflows right now
337                }
338           }
339      }
340    evas_object_smart_calculate(box);
341    wd->level_active[level] = 1;
342 }
343
344 static void
345 _index_box_clear(Evas_Object *obj, Evas_Object *box __UNUSED__, int level)
346 {
347    Widget_Data *wd = elm_widget_data_get(obj);
348    Eina_List *l;
349    Elm_Index_Item *it;
350    if (!wd) return;
351    if (!wd->level_active[level]) return;
352    EINA_LIST_FOREACH(wd->items, l, it)
353      {
354         if (!VIEW(it)) continue;
355         if (it->level != level) continue;
356         evas_object_del(VIEW(it));
357         VIEW(it) = NULL;
358      }
359    wd->level_active[level] = 0;
360 }
361
362 static Eina_Bool
363 _delay_change(void *data)
364 {
365    Widget_Data *wd = elm_widget_data_get(data);
366    void *d;
367    int view_level;
368    if (!wd) return ECORE_CALLBACK_CANCEL;
369    wd->delay = NULL;
370    SET_VIEW_LEVEL(wd, view_level);
371    d = (void *)elm_index_item_selected_get(data, view_level);
372    if (d) evas_object_smart_callback_call(data, "delay,changed", d);
373    return ECORE_CALLBACK_CANCEL;
374 }
375
376 static void
377 _sel_eval(Evas_Object *obj, Evas_Coord evx, Evas_Coord evy)
378 {
379    Widget_Data *wd = elm_widget_data_get(obj);
380    Elm_Index_Item *it, *it_closest, *it_last;
381    Eina_List *l;
382    Evas_Coord x, y, w, h, bx, by, bw, bh, xx, yy;
383    double cdv = 0.5;
384    double cdvv = 0.0;
385    double dmax = 0.0;
386    double dmin = 0.0;
387    Evas_Coord dist;
388    Eina_Bool change = EINA_FALSE;
389    char *label = NULL, *last = NULL;
390    int i;
391    int view_level;
392    if (!wd) return;
393
394    SET_VIEW_LEVEL(wd, view_level);
395    for (i = 0; i <= view_level; i++)
396      {
397         it_last = NULL;
398         it_closest  = NULL;
399         dist = 0x7fffffff;
400         evas_object_geometry_get(wd->bx[i], &bx, &by, &bw, &bh);
401         dmin = (double)(wd->min_1st_level_obj_height*wd->tot_items_count[1])/(2*(double)bh);
402         dmax = 1.0-dmin-0.08;
403         EINA_LIST_FOREACH(wd->items, l, it)
404           {
405              if (!((it->level == i) && (VIEW(it)))) continue;
406              if (it->selected)
407                {
408                   it_last = it;
409                   it->selected = 0;
410                }
411              evas_object_geometry_get(VIEW(it), &x, &y, &w, &h);
412              xx = x + (w / 2);
413              yy = y + (h / 2);
414              x = evx - xx;
415              y = evy - yy;
416              x = (x * x) + (y * y);
417              if ((x < dist) || (!it_closest))
418                {
419                   if (wd->horizontal)
420                     cdv = (double)(xx - bx) / (double)bw;
421                   else
422                     cdv = (double)(yy - by) / (double)bh;
423                   it_closest = it;
424                   dist = x;
425                }
426           }
427           if ((i == 0) && (view_level == 0))
428             {
429                if(cdv > dmax || cdv < dmin)
430                  {
431                     if(cdv > dmax)
432                       {
433                          cdvv = dmax;
434                       }
435                     else
436                       {
437                          cdvv = dmin;
438                       }
439                     edje_object_part_drag_value_set(wd->base, "elm.dragable.index.1", cdv, cdvv);
440                  }
441                else
442                  {
443                     edje_object_part_drag_value_set(wd->base, "elm.dragable.index.1", cdv, cdv);
444                  }
445             }
446         if (it_closest) it_closest->selected = 1;
447         if (it_closest != it_last)
448           {
449              change = 1;
450              if (it_last)
451                {
452                   const char *stacking, *selectraise;
453
454                   it = it_last;
455                   if(view_level == it->level)
456                   edje_object_signal_emit(VIEW(it), "elm,state,inactive", "elm");
457                   stacking = edje_object_data_get(VIEW(it), "stacking");
458                   selectraise = edje_object_data_get(VIEW(it), "selectraise");
459                   if ((selectraise) && (!strcmp(selectraise, "on")))
460                     {
461                        if ((stacking) && (!strcmp(stacking, "below")))
462                          evas_object_lower(VIEW(it));
463                     }
464                }
465              if (it_closest)
466                {
467                   const char *selectraise;
468
469                   it = it_closest;
470                   if(view_level == it->level)
471                   edje_object_signal_emit(VIEW(it), "elm,state,active", "elm");
472                   selectraise = edje_object_data_get(VIEW(it), "selectraise");
473                   if ((selectraise) && (!strcmp(selectraise, "on")))
474                     evas_object_raise(VIEW(it));
475                   evas_object_smart_callback_call((void *)obj, SIG_CHANGED, (void *)it->base.data);
476                   if (wd->delay) ecore_timer_del(wd->delay);
477                   wd->delay = ecore_timer_add(0.2, _delay_change, obj);
478                }
479           }
480         if (it_closest)
481           {
482              it = it_closest;
483              if (!last)
484                last = strdup(it->letter);
485              else
486                {
487                   if (!label) label = strdup(last);
488                   else
489                     {
490                        /* FIXME: realloc return NULL if the request fails */
491                        label = realloc(label, strlen(label) + strlen(last) + 1);
492                        strcat(label, last);
493                     }
494                   free(last);
495                   last = strdup(it->letter);
496                }
497           }
498      }
499    if (!label) label = strdup("");
500    if (!last) last = strdup("");
501    if(!wd->hide_button)
502      {
503         char *popup_text;
504
505         if(view_level == 0)
506           {
507              if(wd->tot_items_count[0])
508                {
509                   if (wd->popup_str[1]) wd->popup_str[1][0] = '\0';
510                   wd->popup_str[0] = (char *)realloc(wd->popup_str[0], (sizeof(char) * strlen(last) + 1));
511
512                   strcpy(wd->popup_str[0], last);
513                   edje_object_signal_emit(wd->base, "hide_2nd_level", "");
514                }
515              else
516                {
517                   edje_object_signal_emit(wd->base, "button.image.hidden", "");
518                   if(label)free(label);
519                   if(last) free(last);
520                   return;
521                }
522           }
523         if (view_level == 1 && wd->level_active[1])
524           {
525              if(wd->tot_items_count[1])
526                {
527                   wd->popup_str[1] = (char *)realloc(wd->popup_str[1], (sizeof(char) * strlen(last) + 1));
528
529                   strcpy(wd->popup_str[1], last);
530                   edje_object_signal_emit(wd->base, "hide_first_level", "");
531                }
532              else
533                {
534                   edje_object_signal_emit(wd->base, "button.image.hidden", "");
535                   if(label)free(label);
536                   if(last) free(last);
537                   return;
538                }
539           }
540         popup_text = (char *)malloc(sizeof(char) * (strlen(wd->popup_str[0]) + strlen(wd->popup_str[1]) + 1));
541         sprintf(popup_text, "%s%s", wd->popup_str[0], wd->popup_str[1]);
542         edje_object_part_text_set(wd->base, "elm.text", popup_text);
543
544         free(popup_text);
545      }
546
547    if(label)
548      free(label);
549    if(last)
550      free(last);
551 }
552
553 static void
554 _wheel(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
555 {
556    Widget_Data *wd = elm_widget_data_get(data);
557    //   Evas_Event_Mouse_Wheel *ev = event_info;
558    //   Evas_Object *obj = o;
559    if (!wd) return;
560 }
561
562 static void
563 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
564 {
565    Widget_Data *wd = elm_widget_data_get(data);
566    Evas_Event_Mouse_Down *ev = event_info;
567    Evas_Coord x, y, w;
568    if (!wd) return;
569    if (ev->button != 1) return;
570    wd->down = 1;
571    evas_object_geometry_get(wd->base, &x, &y, &w, NULL);
572    wd->dx = ev->canvas.x - x;
573    wd->dy = ev->canvas.y - y;
574    elm_index_active_set(data, 1);
575    _sel_eval(data, ev->canvas.x, ev->canvas.y);
576    edje_object_part_drag_value_set(wd->base, "elm.dragable.pointer",
577                                    (!edje_object_mirrored_get(wd->base)) ? wd->dx : (wd->dx - w), wd->dy);
578 }
579
580 static void
581 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
582 {
583    Widget_Data *wd = elm_widget_data_get(data);
584    Evas_Event_Mouse_Up *ev = event_info;
585    void *d;
586    Elm_Index_Item *it;
587    Eina_List *l;
588    int view_level;
589
590    if (!wd) return;
591    if (ev->button != 1) return;
592    if (wd->level == 1 && wd->delay) ecore_timer_del(wd->delay);
593    wd->delay = NULL;
594    wd->down = 0;
595    SET_VIEW_LEVEL(wd, view_level);
596    d = (void *)elm_index_item_selected_get(data, view_level);
597    EINA_LIST_FOREACH(wd->items, l, it)
598      {
599         edje_object_signal_emit(it->base.view, "elm,state,inactive", "elm");
600      }
601    if (d) evas_object_smart_callback_call(data, SIG_SELECTED, d);
602    elm_index_active_set(data, 0);
603    edje_object_signal_emit(wd->base, "elm,state,level,0", "elm");
604 }
605
606 static void
607 _mouse_move(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
608 {
609    Widget_Data *wd = elm_widget_data_get(data);
610    Evas_Event_Mouse_Move *ev = event_info;
611    Evas_Coord minw = 0, minh = 0, x, y, dx, adx, w;
612    void *d;
613    char buf[1024];
614    if (!wd) return;
615    if (!wd->down) return;
616    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
617    evas_object_geometry_get(wd->base, &x, &y, &w, NULL);
618    x = ev->cur.canvas.x - x;
619    y = ev->cur.canvas.y - y;
620    dx = x - wd->dx;
621    adx = dx;
622    if (adx < 0) adx = -dx;
623    edje_object_part_drag_value_set(wd->base, "elm.dragable.pointer"
624                                    , (!edje_object_mirrored_get(wd->base)) ? x : (x - w), y);
625    if (!wd->horizontal)
626      {
627         if (adx > minw)
628           {
629              if (!wd->level)
630                {
631                   wd->level = 1;
632                   snprintf(buf, sizeof(buf), "elm,state,level,%i", wd->level);
633                   edje_object_signal_emit(wd->base, buf, "elm");
634                   evas_object_smart_callback_call(data, SIG_LEVEL_UP, NULL);
635                }
636           }
637         else
638           {
639              if (wd->level == 1)
640                {
641                   wd->level = 0;
642                   snprintf(buf, sizeof(buf), "elm,state,level,%i", wd->level);
643                   edje_object_signal_emit(wd->base, buf, "elm");
644                   d = (void *)elm_index_item_selected_get(data, wd->level);
645                   evas_object_smart_callback_call(data, "changed", d);
646                   if (wd->delay) ecore_timer_del(wd->delay);
647                   wd->delay = ecore_timer_add(0.2, _delay_change, data);
648                   evas_object_smart_callback_call(data, SIG_LEVEL_DOWN, NULL);
649                }
650           }
651      }
652    _sel_eval(data, ev->cur.canvas.x, ev->cur.canvas.y);
653 }
654
655 static void
656 _index_box_refill_job(void *data)
657 {
658    Widget_Data *wd = elm_widget_data_get((Evas_Object *)data);
659    if (!wd) return;
660
661    const char *string;
662    Evas_Coord pw, ph;
663
664    evas_object_geometry_get(wd->base, NULL, NULL, &pw, &ph);
665    wd->scale_factor = elm_scale_get();
666    if ( wd->scale_factor == 0.0 ) {
667      wd->scale_factor = 1.0;
668    }
669    string = edje_object_data_get(wd->base, "min_obj_height");
670    if(string)
671      wd->min_obj_height = (int) (atoi(string))*wd->scale_factor;
672    else
673      wd->min_obj_height = MIN_OBJ_HEIGHT*wd->scale_factor;
674    if(!wd->min_obj_height) return;
675
676    wd->max_grp_size = wd->min_obj_height - 2*MIN_GRP_SIZE;
677    wd->items_count = ph/wd->min_obj_height;
678    wd->max_supp_items_count = wd->max_grp_size*(int)((wd->items_count-1)*0.5)+wd->items_count;
679
680    if(pw != wd->pwidth && ph != wd->pheight)
681      {
682         if(wd->down == 1)
683           {
684              wd->active = 0;
685              elm_index_active_set(data, 1);
686           }
687         _index_box_clear((Evas_Object *)data, wd->bx[0], 0);
688         evas_object_smart_calculate( wd->bx[0]);
689         elm_index_item_go((Evas_Object *)data, wd->level);
690         wd->pwidth = pw;
691         wd->pheight = ph;
692      }
693 }
694
695 static void _index_object_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
696 {
697    Widget_Data *wd;
698    if(!data) return;
699    wd = elm_widget_data_get((Evas_Object *)data);
700    if(!wd) return;
701    ecore_job_add(_index_box_refill_job, (Evas_Object *)data);
702 }
703
704 EAPI Evas_Object *
705 elm_index_add(Evas_Object *parent)
706 {
707    Evas_Object *obj;
708    Evas_Object *o;
709    Evas *e;
710    Widget_Data *wd;
711    Evas_Coord minw, minh;
712    const char *string;
713
714    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
715
716    ELM_SET_WIDTYPE(widtype, "index");
717    elm_widget_type_set(obj, "index");
718    elm_widget_sub_object_add(parent, obj);
719    elm_widget_data_set(obj, wd);
720    elm_widget_del_hook_set(obj, _del_hook);
721    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
722    elm_widget_theme_hook_set(obj, _theme_hook);
723    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
724    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
725    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
726    elm_widget_can_focus_set(obj, EINA_FALSE);
727
728    wd->horizontal = EINA_FALSE;
729    wd->min_obj_height = 0;
730    wd->max_grp_size = 0;
731    wd->items_count = 0;
732    wd->max_supp_items_count = 0;
733    wd->tot_items_count[0] = 0;
734    wd->tot_items_count[1] = 0;
735    wd->hide_button = 0;
736    wd->special_char = edje_object_data_get(wd->base, "special_char");
737    if(wd->special_char == NULL)  wd->special_char = eina_stringshare_add("*");
738
739    wd->base = edje_object_add(e);
740    _elm_theme_object_set(obj, wd->base, "index", "base/vertical", "default");
741    elm_widget_resize_object_set(obj, wd->base);
742
743    o = evas_object_rectangle_add(e);
744    wd->event[0] = o;
745    evas_object_color_set(o, 0, 0, 0, 0);
746    minw = minh = 0;
747    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
748    evas_object_size_hint_min_set(o, minw, minh);
749    edje_object_part_swallow(wd->base, "elm.swallow.event.0", o);
750    elm_widget_sub_object_add(obj, o);
751    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _index_object_resize, obj);
752    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_WHEEL, _wheel, obj);
753    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, obj);
754    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, _mouse_up, obj);
755    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move, obj);
756    evas_object_show(o);
757    if (edje_object_part_exists(wd->base, "elm.swallow.event.1"))
758      {
759         o = evas_object_rectangle_add(e);
760         wd->event[1] = o;
761         evas_object_color_set(o, 0, 0, 0, 0);
762         evas_object_size_hint_min_set(o, minw, minh);
763         edje_object_part_swallow(wd->base, "elm.swallow.event.1", o);
764         elm_widget_sub_object_add(obj, o);
765      }
766
767    wd->bx[0] = evas_object_box_add(e);
768    evas_object_box_layout_set(wd->bx[0], _layout, wd, NULL);
769    elm_widget_sub_object_add(obj, wd->bx[0]);
770    edje_object_part_swallow(wd->base, "elm.swallow.index.0", wd->bx[0]);
771    evas_object_show(wd->bx[0]);
772
773    if (edje_object_part_exists(wd->base, "elm.swallow.index.1"))
774      {
775         wd->bx[1] = evas_object_box_add(e);
776         evas_object_box_layout_set(wd->bx[1], _layout, wd, NULL);
777         elm_widget_sub_object_add(obj, wd->bx[1]);
778         edje_object_part_swallow(wd->base, "elm.swallow.index.1", wd->bx[1]);
779         evas_object_show(wd->bx[1]);
780      }
781
782    evas_object_smart_callbacks_descriptions_set(obj, _signals);
783    wd->scale_factor = elm_scale_get();
784    if ( wd->scale_factor == 0.0 )
785                 wd->scale_factor = 1.0;
786    string = edje_object_data_get(wd->base, "min_1st_level_obj_height");
787    if(string)
788      wd->min_1st_level_obj_height = (int) (atoi(string))*wd->scale_factor;
789    else
790      wd->min_1st_level_obj_height = MIN_OBJ_HEIGHT*wd->scale_factor;
791    wd->popup_str[0] = calloc(1, sizeof(char) * 1);
792    wd->popup_str[1] = calloc(1, sizeof(char) * 1);
793
794    _mirrored_set(obj, elm_widget_mirrored_get(obj));
795    _sizing_eval(obj);
796    return obj;
797 }
798
799 static int
800 _group_count(Evas_Object *obj, int extraIndex, int adj_pos, int vis_pos)
801 {
802    Widget_Data *wd = elm_widget_data_get(obj);
803    if (!wd) return 0;
804    int group_count = MIN_GRP_SIZE;
805    while(group_count <= wd->max_grp_size)
806      {
807         if(extraIndex <= wd->max_grp_size*adj_pos)
808           {
809              if(group_count*adj_pos>=extraIndex) return group_count;
810           }
811         else
812           return wd->max_grp_size;
813
814         group_count += MIN_GRP_SIZE;
815      }
816    return group_count;
817 }
818
819 static void
820 _index_process(Evas_Object *obj)
821 {
822    int extraIndex;
823    int j,i, group_count;
824    Eina_List *l;
825    Elm_Index_Item *it;
826    int count;
827    int n;
828
829    Widget_Data *wd = elm_widget_data_get(obj);
830    if (!wd) return;
831
832    if (wd->items_count == 0) return;
833
834    const int adj_pos = (wd->items_count-1)*0.5;
835    if(wd->tot_items_count[wd->level] <= wd->max_supp_items_count)
836       n = wd->tot_items_count[wd->level];
837    else
838       n = wd->max_supp_items_count;
839    group_count = MIN_GRP_SIZE;
840
841    int *indx = (int*)calloc(n, sizeof(int));
842    if (!indx) return;
843
844    const int minh = wd->min_obj_height;
845    EINA_LIST_FOREACH(wd->items, l, it)
846      {
847         it->vis_letter = eina_stringshare_add(it->letter);
848         it->size =  minh;
849      }
850    int remainder;
851    int numberofparts;
852    int N = wd->items_count;
853
854    for (i=0;i<n;i++)
855      {
856         indx[i] = minh;
857      }
858    extraIndex=n-N;
859    if (extraIndex < 0)
860      {
861         free(indx);
862         indx = NULL;
863         return;
864      }
865
866    group_count = _group_count(obj, extraIndex, adj_pos, N);
867    if (group_count <= 0)
868      {
869         if (indx)
870           free(indx);
871         indx = NULL;
872         return;
873      }
874
875    PlacementPart place[adj_pos];
876    remainder = extraIndex%group_count;
877    numberofparts=(extraIndex/group_count)+(remainder == 0? 0: 1);
878
879    for (i=0;i<numberofparts; i++)
880      {
881         place[i].count=group_count+1;
882         count = (int)(((float)(i+1)/(float)(numberofparts+1))*N);
883         place[i].start= count +i*group_count-1;
884      }
885    if (remainder)
886      place[numberofparts-1].count=remainder+1;
887
888    for (i=0;i<numberofparts;i++)
889      {
890         for (j=0;j<place[i].count; j++)
891           {
892              indx[((place[i].start)+j)]= MIN_PIXEL_VALUE;
893           }
894         indx[(place[i].start+(place[i].count)/2)] = minh-place[i].count+1;
895      }
896    count = 0;
897    EINA_LIST_FOREACH(wd->items, l, it)
898      {
899         int size = indx[count];
900         count++;
901         if (size == minh)
902           {
903              it->vis_letter = eina_stringshare_add(it->letter);
904              continue;
905           }
906         else if (size == 1)
907           {
908              eina_stringshare_del(it->vis_letter);
909              it->vis_letter = eina_stringshare_add("");
910           }
911         else
912           {
913              eina_stringshare_del(it->vis_letter);
914              it->vis_letter = eina_stringshare_add(wd->special_char);
915           }
916         it->size = size*wd->scale_factor;
917      }
918    if (indx)
919      {
920         free(indx);
921         indx = NULL;
922      }
923 }
924
925 EAPI void
926 elm_index_active_set(Evas_Object *obj, Eina_Bool active)
927 {
928    ELM_CHECK_WIDTYPE(obj, widtype);
929    Widget_Data *wd = elm_widget_data_get(obj);
930    if (!wd) return;
931    if (wd->active == active) return;
932    wd->active = active;
933    wd->level = 0;
934    if (wd->active)
935      {
936         _index_box_clear(obj, wd->bx[1], 1);
937         _index_process(obj);
938         _index_box_auto_fill(obj, wd->bx[0], 0);
939         if(wd->tot_items_count[wd->level])
940           edje_object_signal_emit(wd->base, "elm,state,active", "elm");
941         else
942           edje_object_signal_emit(wd->base, "button.image.hidden", "");
943      }
944    else
945      edje_object_signal_emit(wd->base, "elm,state,inactive", "elm");
946 }
947
948 EAPI Eina_Bool
949 elm_index_active_get(const Evas_Object *obj)
950 {
951    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
952    Widget_Data *wd = elm_widget_data_get(obj);
953    if (!wd) return EINA_FALSE;
954    return wd->active;
955 }
956
957 EAPI void
958 elm_index_item_level_set(Evas_Object *obj, int level)
959 {
960    ELM_CHECK_WIDTYPE(obj, widtype);
961    Widget_Data *wd = elm_widget_data_get(obj);
962    if (!wd) return;
963    if (wd->level == level) return;
964    wd->level = level;
965 }
966
967 EAPI int
968 elm_index_item_level_get(const Evas_Object *obj)
969 {
970    ELM_CHECK_WIDTYPE(obj, widtype) 0;
971    Widget_Data *wd = elm_widget_data_get(obj);
972    if (!wd) return 0;
973    return wd->level;
974 }
975
976 EAPI void *
977 elm_index_item_selected_get(const Evas_Object *obj, int level)
978 {
979    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
980    Widget_Data *wd = elm_widget_data_get(obj);
981    Eina_List *l;
982    Elm_Index_Item *it;
983    if (!wd) return NULL;
984    EINA_LIST_FOREACH(wd->items, l, it)
985       if ((it->selected) && (it->level == level))
986         return elm_widget_item_data_get(it);
987    return NULL;
988 }
989
990 EAPI void
991 elm_index_item_append(Evas_Object *obj, const char *letter, const void *item)
992 {
993    ELM_CHECK_WIDTYPE(obj, widtype);
994    Widget_Data *wd = elm_widget_data_get(obj);
995    Elm_Index_Item *it;
996    if (!wd) return;
997    it = _item_new(obj, letter, item);
998    if (!it) return;
999    wd->items = eina_list_append(wd->items, it);
1000    wd->tot_items_count[wd->level]++;
1001    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1002 }
1003
1004 EAPI void
1005 elm_index_item_prepend(Evas_Object *obj, const char *letter, const void *item)
1006 {
1007    ELM_CHECK_WIDTYPE(obj, widtype);
1008    Widget_Data *wd = elm_widget_data_get(obj);
1009    Elm_Index_Item *it;
1010
1011    if (!wd) return;
1012    it = _item_new(obj, letter, item);
1013    if (!it) return;
1014    wd->items = eina_list_prepend(wd->items, it);
1015    wd->tot_items_count[wd->level]++;
1016    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1017 }
1018
1019 EAPI void
1020 elm_index_item_append_relative(Evas_Object *obj, const char *letter, const void *item, const void *relative)
1021 {
1022    ELM_CHECK_WIDTYPE(obj, widtype);
1023    Widget_Data *wd = elm_widget_data_get(obj);
1024    Elm_Index_Item *it, *it_rel;
1025    if (!wd) return;
1026    if (!relative)
1027      {
1028         elm_index_item_append(obj, letter, item);
1029         wd->tot_items_count[wd->level]++;
1030         return;
1031      }
1032    it = _item_new(obj, letter, item);
1033    if (!it) return;
1034
1035    it_rel = _item_find(obj, relative);
1036    if (!it_rel)
1037      {
1038         elm_index_item_append(obj, letter, item);
1039         wd->tot_items_count[wd->level]++;
1040         _item_free(it);
1041         return;
1042      }
1043
1044    wd->items = eina_list_append_relative(wd->items, it, it_rel);
1045    wd->tot_items_count[wd->level]++;
1046    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1047 }
1048
1049 EAPI void
1050 elm_index_item_prepend_relative(Evas_Object *obj, const char *letter, const void *item, const void *relative)
1051 {
1052    ELM_CHECK_WIDTYPE(obj, widtype);
1053    Widget_Data *wd = elm_widget_data_get(obj);
1054    Elm_Index_Item *it, *it_rel;
1055    if (!wd) return;
1056    if (!relative)
1057      {
1058         elm_index_item_prepend(obj, letter, item);
1059         wd->tot_items_count[wd->level]++;
1060         return;
1061      }
1062    it = _item_new(obj, letter, item);
1063    if (!it) return;
1064
1065    it_rel = _item_find(obj, relative);
1066    if (!it_rel)
1067      {
1068         elm_index_item_append(obj, letter, item);
1069         wd->tot_items_count[wd->level]++;
1070         _item_free(it);
1071         return;
1072      }
1073
1074    wd->items = eina_list_prepend_relative(wd->items, it, it_rel);
1075    wd->tot_items_count[wd->level]++;
1076    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1077 }
1078
1079 EAPI void
1080 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)
1081 {
1082    ELM_CHECK_WIDTYPE(obj, widtype);
1083    Widget_Data *wd = elm_widget_data_get(obj);
1084    Eina_List *lnear;
1085    Elm_Index_Item *it;
1086    int cmp;
1087
1088    if (!wd) return;
1089    if (!(wd->items))
1090      {
1091         elm_index_item_append(obj, letter, item);
1092         return;
1093      }
1094
1095    it = _item_new(obj, letter, item);
1096    if (!it) return;
1097
1098    lnear = eina_list_search_sorted_near_list(wd->items, cmp_func, it, &cmp);
1099    if (cmp < 0)
1100      wd->items =  eina_list_append_relative_list(wd->items, it, lnear);
1101    else if (cmp > 0)
1102      wd->items = eina_list_prepend_relative_list(wd->items, it, lnear);
1103    else
1104      {
1105         /* If cmp_data_func is not provided, append a duplicated item */
1106         if (!cmp_data_func)
1107           wd->items =  eina_list_append_relative_list(wd->items, it, lnear);
1108         else
1109           {
1110              Elm_Index_Item *p_it = eina_list_data_get(lnear);
1111              if (cmp_data_func(p_it->base.data, it->base.data) >= 0)
1112                p_it->base.data = it->base.data;
1113              _item_free(it);
1114           }
1115      }
1116
1117    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1118 }
1119
1120 EAPI void
1121 elm_index_item_del(Evas_Object *obj, const void *item)
1122 {
1123    ELM_CHECK_WIDTYPE(obj, widtype);
1124    Widget_Data *wd = elm_widget_data_get(obj);
1125    Elm_Index_Item *it;
1126    if (!wd) return;
1127    it = _item_find(obj, item);
1128    if (!it) return;
1129    _item_free(it);
1130    wd->tot_items_count[wd->level]--;
1131    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1132 }
1133
1134 EAPI Elm_Index_Item *
1135 elm_index_item_find(Evas_Object *obj, const void *item)
1136 {
1137    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1138    Widget_Data *wd = elm_widget_data_get(obj);
1139    if (!wd) return NULL;
1140    return _item_find(obj, item);
1141 }
1142
1143 EAPI void
1144 elm_index_item_clear(Evas_Object *obj)
1145 {
1146    ELM_CHECK_WIDTYPE(obj, widtype);
1147    Widget_Data *wd = elm_widget_data_get(obj);
1148    Elm_Index_Item *it;
1149    Eina_List *l, *clear = NULL;
1150    if (!wd) return;
1151    _index_box_clear(obj, wd->bx[wd->level], wd->level);
1152    EINA_LIST_FOREACH(wd->items, l, it)
1153      {
1154         if (it->level != wd->level) continue;
1155         clear = eina_list_append(clear, it);
1156      }
1157    EINA_LIST_FREE(clear, it)
1158      {
1159         _item_free(it);
1160         wd->tot_items_count[wd->level]--;
1161      }
1162 }
1163
1164 EAPI void
1165 elm_index_item_go(Evas_Object *obj, int level)
1166 {
1167    ELM_CHECK_WIDTYPE(obj, widtype);
1168    Widget_Data *wd = elm_widget_data_get(obj);
1169    if (!wd) return;
1170    if(level == 0)
1171      _index_process(obj);
1172    _index_box_auto_fill(obj, wd->bx[0], 0);
1173    if (wd->level == 1) _index_box_auto_fill(obj, wd->bx[1], 1);
1174 }
1175
1176 EAPI void *
1177 elm_index_item_data_get(const Elm_Index_Item *it)
1178 {
1179    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
1180    return elm_widget_item_data_get(it);
1181 }
1182
1183 EAPI void
1184 elm_index_item_data_set(Elm_Index_Item *it, const void *data)
1185 {
1186    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it);
1187    elm_widget_item_data_set(it, data);
1188 }
1189
1190 EAPI void
1191 elm_index_button_image_invisible_set(Evas_Object *obj, Eina_Bool invisible)
1192 {
1193    ELM_CHECK_WIDTYPE(obj, widtype);
1194    Widget_Data *wd = elm_widget_data_get(obj);
1195    wd->hide_button = invisible;
1196
1197    edje_object_signal_emit(wd->base, "elm,state,button,image,hide", "elm");
1198    return;
1199 }
1200
1201 EAPI void
1202 elm_index_item_del_cb_set(Elm_Index_Item *it, Evas_Smart_Cb func)
1203 {
1204    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it);
1205    elm_widget_item_del_cb_set(it, func);
1206 }
1207
1208 EAPI const char *
1209 elm_index_item_letter_get(const Elm_Index_Item *it)
1210 {
1211    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
1212    return it->letter;
1213 }
1214