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