elm genlist/list/gengrid: Changed edge smart callback name. 'edge' callback is not...
[framework/uifw/elementary.git] / src / lib / elm_gengrid.c
1 #include <Elementary.h>
2 #include <Elementary_Cursor.h>
3 #include "elm_priv.h"
4 #include "els_scroller.h"
5
6 /* --
7  * TODO:
8  * Handle non-homogeneous objects too.
9  */
10
11  typedef struct _Widget_Data Widget_Data;
12  typedef struct _Pan         Pan;
13
14 #define PRELOAD 1
15 #define REORDER_EFFECT_TIME 0.5
16
17  struct _Elm_Gengrid_Item
18 {
19    Elm_Widget_Item               base;
20    EINA_INLIST;
21    Evas_Object                  *spacer;
22    const Elm_Gengrid_Item_Class *gic;
23    Elm_Gengrid_Item             *parent_group_item;
24    Ecore_Timer                  *long_timer;
25    Ecore_Animator               *item_moving_effect_timer;
26    Widget_Data                  *wd;
27    Eina_List                    *labels, *icons, *states, *icon_objs;
28    struct
29      {
30         Evas_Smart_Cb func;
31         const void   *data;
32      } func;
33
34    Evas_Coord   x, y, gx, gy, dx, dy, ox, oy, tx, ty, rx, ry;
35    unsigned int moving_effect_start_time;
36    int          relcount;
37    int          walking;
38    int          prev_group;
39
40    struct
41      {
42         const void                 *data;
43         Elm_Tooltip_Item_Content_Cb content_cb;
44         Evas_Smart_Cb               del_cb;
45         const char                 *style;
46         Eina_Bool                   free_size : 1;
47      } tooltip;
48
49    const char *mouse_cursor;
50
51    Eina_Bool   is_group : 1;
52    Eina_Bool   want_unrealize : 1;
53    Eina_Bool   group_realized : 1;
54    Eina_Bool   realized : 1;
55    Eina_Bool   dragging : 1;
56    Eina_Bool   down : 1;
57    Eina_Bool   delete_me : 1;
58    Eina_Bool   display_only : 1;
59    Eina_Bool   disabled : 1;
60    Eina_Bool   selected : 1;
61    Eina_Bool   highlighted : 1;
62    Eina_Bool   moving : 1;
63 };
64
65 struct _Widget_Data
66 {
67    Eina_Inlist_Sorted_State *state;
68    Evas_Object      *self, *scr;
69    Evas_Object      *pan_smart;
70    Pan              *pan;
71    Eina_Inlist      *items;
72    Eina_List        *group_items;
73    Ecore_Job        *calc_job;
74    Eina_List        *selected;
75    Elm_Gengrid_Item *last_selected_item, *reorder_item;
76    double            align_x, align_y;
77
78    Evas_Coord        pan_x, pan_y, old_pan_x, old_pan_y;
79    Evas_Coord        item_width, item_height; /* Each item size */
80    Evas_Coord        group_item_width, group_item_height; /* Each group item size */
81    Evas_Coord        minw, minh; /* Total obj size */
82    Evas_Coord        reorder_item_x, reorder_item_y;
83    unsigned int      nmax;
84    long              count;
85    long              items_lost;
86    int               walking;
87
88    Eina_Bool         horizontal : 1;
89    Eina_Bool         on_hold : 1;
90    Eina_Bool         longpressed : 1;
91    Eina_Bool         multi : 1;
92    Eina_Bool         no_select : 1;
93    Eina_Bool         wasselected : 1;
94    Eina_Bool         always_select : 1;
95    Eina_Bool         clear_me : 1;
96    Eina_Bool         h_bounce : 1;
97    Eina_Bool         v_bounce : 1;
98    Eina_Bool         reorder_mode : 1;
99    Eina_Bool         reorder_item_changed : 1;
100    Eina_Bool         move_effect_enabled : 1;
101 };
102
103 #define ELM_GENGRID_ITEM_FROM_INLIST(item) \
104    ((item) ? EINA_INLIST_CONTAINER_GET(item, Elm_Gengrid_Item) : NULL)
105
106 struct _Pan
107 {
108    Evas_Object_Smart_Clipped_Data __clipped_data;
109    Widget_Data                   *wd;
110 };
111
112 static const char *widtype = NULL;
113 static void      _item_highlight(Elm_Gengrid_Item *item);
114 static void      _item_unrealize(Elm_Gengrid_Item *item);
115 static void      _item_select(Elm_Gengrid_Item *item);
116 static void      _item_unselect(Elm_Gengrid_Item *item);
117 static void      _calc_job(void *data);
118 static void      _on_focus_hook(void        *data,
119                                 Evas_Object *obj);
120 static Eina_Bool _item_multi_select_up(Widget_Data *wd);
121 static Eina_Bool _item_multi_select_down(Widget_Data *wd);
122 static Eina_Bool _item_multi_select_left(Widget_Data *wd);
123 static Eina_Bool _item_multi_select_right(Widget_Data *wd);
124 static Eina_Bool _item_single_select_up(Widget_Data *wd);
125 static Eina_Bool _item_single_select_down(Widget_Data *wd);
126 static Eina_Bool _item_single_select_left(Widget_Data *wd);
127 static Eina_Bool _item_single_select_right(Widget_Data *wd);
128 static Eina_Bool _event_hook(Evas_Object       *obj,
129                              Evas_Object       *src,
130                              Evas_Callback_Type type,
131                              void              *event_info);
132 static Eina_Bool _deselect_all_items(Widget_Data *wd);
133
134 static Evas_Smart_Class _pan_sc = EVAS_SMART_CLASS_INIT_VERSION;
135 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
136
137 static const char SIG_ACTIVATED[] = "activated";
138 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
139 static const char SIG_LONGPRESSED[] = "longpressed";
140 static const char SIG_SELECTED[] = "selected";
141 static const char SIG_UNSELECTED[] = "unselected";
142 static const char SIG_REALIZED[] = "realized";
143 static const char SIG_UNREALIZED[] = "unrealized";
144 static const char SIG_CHANGED[] = "changed";
145 static const char SIG_DRAG_START_UP[] = "drag,start,up";
146 static const char SIG_DRAG_START_DOWN[] = "drag,start,down";
147 static const char SIG_DRAG_START_LEFT[] = "drag,start,left";
148 static const char SIG_DRAG_START_RIGHT[] = "drag,start,right";
149 static const char SIG_DRAG_STOP[] = "drag,stop";
150 static const char SIG_DRAG[] = "drag";
151 static const char SIG_SCROLL[] = "scroll";
152 static const char SIG_SCROLL_ANIM_START[] = "scroll,anim,start";
153 static const char SIG_SCROLL_ANIM_STOP[] = "scroll,anim,stop";
154 static const char SIG_SCROLL_DRAG_START[] = "scroll,drag,start";
155 static const char SIG_SCROLL_DRAG_STOP[] = "scroll,drag,stop";
156 static const char SIG_SCROLL_EDGE_TOP[] = "scroll,edge,top"; // TODO : remove this
157 static const char SIG_SCROLL_EDGE_BOTTOM[] = "scroll,edge,bottom"; // TODO : remove this
158 static const char SIG_SCROLL_EDGE_LEFT[] = "scroll,edge,left"; // TODO : remove this
159 static const char SIG_SCROLL_EDGE_RIGHT[] = "scroll,edge,right"; // TODO : remove this
160 static const char SIG_EDGE_TOP[] = "edge,top";
161 static const char SIG_EDGE_BOTTOM[] = "edge,bottom";
162 static const char SIG_EDGE_LEFT[] = "edge,left";
163 static const char SIG_EDGE_RIGHT[] = "edge,right";
164 static const char SIG_MOVED[] = "moved";
165
166 static const Evas_Smart_Cb_Description _signals[] = {
167    {SIG_ACTIVATED, ""},
168    {SIG_CLICKED_DOUBLE, ""},
169    {SIG_LONGPRESSED, ""},
170    {SIG_SELECTED, ""},
171    {SIG_UNSELECTED, ""},
172    {SIG_REALIZED, ""},
173    {SIG_UNREALIZED, ""},
174    {SIG_CHANGED, ""},
175    {SIG_DRAG_START_UP, ""},
176    {SIG_DRAG_START_DOWN, ""},
177    {SIG_DRAG_START_LEFT, ""},
178    {SIG_DRAG_START_RIGHT, ""},
179    {SIG_DRAG_STOP, ""},
180    {SIG_DRAG, ""},
181    {SIG_SCROLL, ""},
182    {SIG_SCROLL_ANIM_START, ""},
183    {SIG_SCROLL_ANIM_STOP, ""},
184    {SIG_SCROLL_DRAG_START, ""},
185    {SIG_SCROLL_DRAG_STOP, ""},
186    {SIG_SCROLL_EDGE_TOP, ""},
187    {SIG_SCROLL_EDGE_BOTTOM, ""},
188    {SIG_SCROLL_EDGE_LEFT, ""},
189    {SIG_SCROLL_EDGE_RIGHT, ""},
190    {SIG_EDGE_TOP, ""},
191    {SIG_EDGE_BOTTOM, ""},
192    {SIG_EDGE_LEFT, ""},
193    {SIG_EDGE_RIGHT, ""},
194    {SIG_MOVED, ""},
195    {NULL, NULL}
196 };
197
198 static Eina_Compare_Cb _elm_gengrid_item_compare_cb;
199 static Eina_Compare_Cb _elm_gengrid_item_compare_data_cb;
200
201 static Eina_Bool
202 _event_hook(Evas_Object       *obj,
203             Evas_Object *src   __UNUSED__,
204             Evas_Callback_Type type,
205             void              *event_info)
206 {
207    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
208    Evas_Event_Key_Down *ev = event_info;
209    Widget_Data *wd = elm_widget_data_get(obj);
210    if (!wd) return EINA_FALSE;
211    if (!wd->items) return EINA_FALSE;
212    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
213    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
214
215    Elm_Gengrid_Item *item = NULL;
216    Evas_Coord x = 0;
217    Evas_Coord y = 0;
218    Evas_Coord step_x = 0;
219    Evas_Coord step_y = 0;
220    Evas_Coord v_w = 0;
221    Evas_Coord v_h = 0;
222    Evas_Coord page_x = 0;
223    Evas_Coord page_y = 0;
224
225    elm_smart_scroller_child_pos_get(wd->scr, &x, &y);
226    elm_smart_scroller_step_size_get(wd->scr, &step_x, &step_y);
227    elm_smart_scroller_page_size_get(wd->scr, &page_x, &page_y);
228    elm_smart_scroller_child_viewport_size_get(wd->scr, &v_w, &v_h);
229
230    if ((!strcmp(ev->keyname, "Left")) || (!strcmp(ev->keyname, "KP_Left")))
231      {
232         if ((wd->horizontal) &&
233             (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
234               (_item_multi_select_up(wd)))
235              || (_item_single_select_up(wd))))
236           {
237              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
238              return EINA_TRUE;
239           }
240         else if ((!wd->horizontal) &&
241                  (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
242                    (_item_multi_select_left(wd)))
243                   || (_item_single_select_left(wd))))
244           {
245              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
246              return EINA_TRUE;
247           }
248         else
249           x -= step_x;
250      }
251    else if ((!strcmp(ev->keyname, "Right")) || (!strcmp(ev->keyname, "KP_Right")))
252      {
253         if ((wd->horizontal) &&
254             (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
255               (_item_multi_select_down(wd)))
256              || (_item_single_select_down(wd))))
257           {
258              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
259              return EINA_TRUE;
260           }
261         else if ((!wd->horizontal) &&
262                  (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
263                    (_item_multi_select_right(wd)))
264                   || (_item_single_select_right(wd))))
265           {
266              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
267              return EINA_TRUE;
268           }
269         else
270           x += step_x;
271      }
272    else if ((!strcmp(ev->keyname, "Up")) || (!strcmp(ev->keyname, "KP_Up")))
273      {
274         if ((wd->horizontal) &&
275             (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
276               (_item_multi_select_left(wd)))
277              || (_item_single_select_left(wd))))
278           {
279              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
280              return EINA_TRUE;
281           }
282         else if ((!wd->horizontal) &&
283                  (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
284                    (_item_multi_select_up(wd)))
285                   || (_item_single_select_up(wd))))
286           {
287              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
288              return EINA_TRUE;
289           }
290         else
291           y -= step_y;
292      }
293    else if ((!strcmp(ev->keyname, "Down")) || (!strcmp(ev->keyname, "KP_Down")))
294      {
295         if ((wd->horizontal) &&
296             (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
297               (_item_multi_select_right(wd)))
298              || (_item_single_select_right(wd))))
299           {
300              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
301              return EINA_TRUE;
302           }
303         else if ((!wd->horizontal) &&
304                  (((evas_key_modifier_is_set(ev->modifiers, "Shift")) &&
305                    (_item_multi_select_down(wd)))
306                   || (_item_single_select_down(wd))))
307           {
308              ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
309              return EINA_TRUE;
310           }
311         else
312           y += step_y;
313      }
314    else if ((!strcmp(ev->keyname, "Home")) || (!strcmp(ev->keyname, "KP_Home")))
315      {
316         item = elm_gengrid_first_item_get(obj);
317         elm_gengrid_item_bring_in(item);
318         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
319         return EINA_TRUE;
320      }
321    else if ((!strcmp(ev->keyname, "End")) || (!strcmp(ev->keyname, "KP_End")))
322      {
323         item = elm_gengrid_last_item_get(obj);
324         elm_gengrid_item_bring_in(item);
325         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
326         return EINA_TRUE;
327      }
328    else if ((!strcmp(ev->keyname, "Prior")) ||
329             (!strcmp(ev->keyname, "KP_Prior")))
330      {
331         if (wd->horizontal)
332           {
333              if (page_x < 0)
334                x -= -(page_x * v_w) / 100;
335              else
336                x -= page_x;
337           }
338         else
339           {
340              if (page_y < 0)
341                y -= -(page_y * v_h) / 100;
342              else
343                y -= page_y;
344           }
345      }
346    else if ((!strcmp(ev->keyname, "Next")) || (!strcmp(ev->keyname, "KP_Next")))
347      {
348         if (wd->horizontal)
349           {
350              if (page_x < 0)
351                x += -(page_x * v_w) / 100;
352              else
353                x += page_x;
354           }
355         else
356           {
357              if (page_y < 0)
358                y += -(page_y * v_h) / 100;
359              else
360                y += page_y;
361           }
362      }
363    else if (!strcmp(ev->keyname, "Escape"))
364      {
365         if (!_deselect_all_items(wd)) return EINA_FALSE;
366         ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
367         return EINA_TRUE;
368      }
369    else if ((!strcmp(ev->keyname, "Return")) ||
370             (!strcmp(ev->keyname, "KP_Enter")) ||
371             (!strcmp(ev->keyname, "space")))
372      {
373         item = elm_gengrid_selected_item_get(obj);
374         evas_object_smart_callback_call(item->wd->self, SIG_ACTIVATED, item);
375      }
376    else return EINA_FALSE;
377
378    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
379    elm_smart_scroller_child_pos_set(wd->scr, x, y);
380    return EINA_TRUE;
381 }
382
383 static Eina_Bool
384 _deselect_all_items(Widget_Data *wd)
385 {
386    if (!wd->selected) return EINA_FALSE;
387    while(wd->selected)
388      elm_gengrid_item_selected_set(wd->selected->data, EINA_FALSE);
389
390    return EINA_TRUE;
391 }
392
393 static Eina_Bool
394 _item_multi_select_left(Widget_Data *wd)
395 {
396    if (!wd->selected) return EINA_FALSE;
397
398    Elm_Gengrid_Item *prev = elm_gengrid_item_prev_get(wd->last_selected_item);
399    if (!prev) return EINA_TRUE;
400    if (elm_gengrid_item_selected_get(prev))
401      {
402         elm_gengrid_item_selected_set(wd->last_selected_item, EINA_FALSE);
403         wd->last_selected_item = prev;
404         elm_gengrid_item_show(wd->last_selected_item);
405      }
406    else
407      {
408         elm_gengrid_item_selected_set(prev, EINA_TRUE);
409         elm_gengrid_item_show(prev);
410      }
411
412    return EINA_TRUE;
413 }
414
415 static Eina_Bool
416 _item_multi_select_right(Widget_Data *wd)
417 {
418    if (!wd->selected) return EINA_FALSE;
419
420    Elm_Gengrid_Item *next = elm_gengrid_item_next_get(wd->last_selected_item);
421    if (!next) return EINA_TRUE;
422    if (elm_gengrid_item_selected_get(next))
423      {
424         elm_gengrid_item_selected_set(wd->last_selected_item, EINA_FALSE);
425         wd->last_selected_item = next;
426         elm_gengrid_item_show(wd->last_selected_item);
427      }
428    else
429      {
430         elm_gengrid_item_selected_set(next, EINA_TRUE);
431         elm_gengrid_item_show(next);
432      }
433
434    return EINA_TRUE;
435 }
436
437 static Eina_Bool
438 _item_multi_select_up(Widget_Data *wd)
439 {
440    unsigned int i;
441    Eina_Bool r = EINA_TRUE;
442
443    if (!wd->selected) return EINA_FALSE;
444
445    for (i = 0; (r) && (i < wd->nmax); i++)
446      r &= _item_multi_select_left(wd);
447
448    return r;
449 }
450
451 static Eina_Bool
452 _item_multi_select_down(Widget_Data *wd)
453 {
454    unsigned int i;
455    Eina_Bool r = EINA_TRUE;
456
457    if (!wd->selected) return EINA_FALSE;
458
459    for (i = 0; (r) && (i < wd->nmax); i++)
460      r &= _item_multi_select_right(wd);
461
462    return r;
463 }
464
465 static Eina_Bool
466 _item_single_select_up(Widget_Data *wd)
467 {
468    unsigned int i;
469
470    Elm_Gengrid_Item *prev;
471
472    if (!wd->selected)
473      {
474         prev = ELM_GENGRID_ITEM_FROM_INLIST(wd->items->last);
475         while ((prev) && (prev->delete_me))
476           prev = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(prev)->prev);
477         elm_gengrid_item_selected_set(prev, EINA_TRUE);
478         elm_gengrid_item_show(prev);
479         return EINA_TRUE;
480      }
481    else prev = elm_gengrid_item_prev_get(wd->last_selected_item);
482
483    if (!prev) return EINA_FALSE;
484
485    for (i = 1; i < wd->nmax; i++)
486      {
487         Elm_Gengrid_Item *tmp = elm_gengrid_item_prev_get(prev);
488         if (!tmp) return EINA_FALSE;
489         prev = tmp;
490      }
491
492    _deselect_all_items(wd);
493
494    elm_gengrid_item_selected_set(prev, EINA_TRUE);
495    elm_gengrid_item_show(prev);
496    return EINA_TRUE;
497 }
498
499 static Eina_Bool
500 _item_single_select_down(Widget_Data *wd)
501 {
502    unsigned int i;
503
504    Elm_Gengrid_Item *next;
505
506    if (!wd->selected)
507      {
508         next = ELM_GENGRID_ITEM_FROM_INLIST(wd->items);
509         while ((next) && (next->delete_me))
510           next = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(next)->next);
511         elm_gengrid_item_selected_set(next, EINA_TRUE);
512         elm_gengrid_item_show(next);
513         return EINA_TRUE;
514      }
515    else next = elm_gengrid_item_next_get(wd->last_selected_item);
516
517    if (!next) return EINA_FALSE;
518
519    for (i = 1; i < wd->nmax; i++)
520      {
521         Elm_Gengrid_Item *tmp = elm_gengrid_item_next_get(next);
522         if (!tmp) return EINA_FALSE;
523         next = tmp;
524      }
525
526    _deselect_all_items(wd);
527
528    elm_gengrid_item_selected_set(next, EINA_TRUE);
529    elm_gengrid_item_show(next);
530    return EINA_TRUE;
531 }
532
533 static Eina_Bool
534 _item_single_select_left(Widget_Data *wd)
535 {
536    Elm_Gengrid_Item *prev;
537    if (!wd->selected)
538      {
539         prev = ELM_GENGRID_ITEM_FROM_INLIST(wd->items->last);
540         while ((prev) && (prev->delete_me))
541           prev = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(prev)->prev);
542      }
543    else prev = elm_gengrid_item_prev_get(wd->last_selected_item);
544
545    if (!prev) return EINA_FALSE;
546
547    _deselect_all_items(wd);
548
549    elm_gengrid_item_selected_set(prev, EINA_TRUE);
550    elm_gengrid_item_show(prev);
551    return EINA_TRUE;
552 }
553
554 static Eina_Bool
555 _item_single_select_right(Widget_Data *wd)
556 {
557    Elm_Gengrid_Item *next;
558    if (!wd->selected)
559      {
560         next = ELM_GENGRID_ITEM_FROM_INLIST(wd->items);
561         while ((next) && (next->delete_me))
562           next = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(next)->next);
563      }
564    else next = elm_gengrid_item_next_get(wd->last_selected_item);
565
566    if (!next) return EINA_FALSE;
567
568    _deselect_all_items(wd);
569
570    elm_gengrid_item_selected_set(next, EINA_TRUE);
571    elm_gengrid_item_show(next);
572    return EINA_TRUE;
573 }
574
575 static void
576 _on_focus_hook(void *data   __UNUSED__,
577                Evas_Object *obj)
578 {
579    Widget_Data *wd = elm_widget_data_get(obj);
580    if (!wd) return;
581    if (elm_widget_focus_get(obj))
582      {
583         edje_object_signal_emit(wd->self, "elm,action,focus", "elm");
584         evas_object_focus_set(wd->self, EINA_TRUE);
585         if ((wd->selected) && (!wd->last_selected_item))
586           wd->last_selected_item = eina_list_data_get(wd->selected);
587      }
588    else
589      {
590         edje_object_signal_emit(wd->self, "elm,action,unfocus", "elm");
591         evas_object_focus_set(wd->self, EINA_FALSE);
592      }
593 }
594
595 static void
596 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
597 {
598    Widget_Data *wd = elm_widget_data_get(obj);
599    Elm_Gengrid_Item *item;
600    if (!wd) return;
601    elm_smart_scroller_mirrored_set(wd->scr, rtl);
602    if (!wd->items) return;
603    item = ELM_GENGRID_ITEM_FROM_INLIST(wd->items);
604
605    while (item)
606      {
607         edje_object_mirrored_set(item->base.view, rtl);
608         elm_gengrid_item_update(item);
609         item = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(item)->next);
610      }
611 }
612
613 static void
614 _theme_hook(Evas_Object *obj)
615 {
616    Widget_Data *wd = elm_widget_data_get(obj);
617    if (!wd) return;
618    _elm_widget_mirrored_reload(obj);
619    _mirrored_set(obj, elm_widget_mirrored_get(obj));
620    elm_smart_scroller_object_theme_set(obj, wd->scr, "gengrid", "base",
621                                        elm_widget_style_get(obj));
622 }
623
624 static void
625 _del_pre_hook(Evas_Object *obj)
626 {
627    Widget_Data *wd = elm_widget_data_get(obj);
628    if (!wd) return;
629    elm_gengrid_clear(obj);
630    evas_object_del(wd->pan_smart);
631    wd->pan_smart = NULL;
632 }
633
634 static void
635 _del_hook(Evas_Object *obj)
636 {
637    Widget_Data *wd = elm_widget_data_get(obj);
638    free(wd);
639 }
640
641 static void
642 _signal_emit_hook(Evas_Object *obj,
643                   const char  *emission,
644                   const char  *source)
645 {
646    Widget_Data *wd = elm_widget_data_get(obj);
647    if (!wd) return;
648    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
649                            emission, source);
650 }
651
652 static void
653 _signal_callback_add_hook(Evas_Object *obj,
654                           const char  *emission,
655                           const char  *source,
656                           Edje_Signal_Cb func_cb,
657                           void *data)
658 {
659    Widget_Data *wd = elm_widget_data_get(obj);
660    if (!wd) return;
661    edje_object_signal_callback_add(elm_smart_scroller_edje_object_get(wd->scr),
662                                    emission, source, func_cb, data);
663 }
664
665 static void
666 _signal_callback_del_hook(Evas_Object *obj,
667                           const char  *emission,
668                           const char  *source,
669                           Edje_Signal_Cb func_cb,
670                           void *data)
671 {
672    Widget_Data *wd = elm_widget_data_get(obj);
673    if (!wd) return;
674    edje_object_signal_callback_del_full(elm_smart_scroller_edje_object_get(wd->scr),
675                                         emission, source, func_cb, data);
676 }
677
678 static void
679 _mouse_move(void        *data,
680             Evas *evas   __UNUSED__,
681             Evas_Object *obj,
682             void        *event_info)
683 {
684    Elm_Gengrid_Item *item = data;
685    Evas_Event_Mouse_Move *ev = event_info;
686    Evas_Coord minw = 0, minh = 0, x, y, dx, dy, adx, ady;
687    Evas_Coord ox, oy, ow, oh, it_scrl_x, it_scrl_y;
688
689    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
690      {
691         if (!item->wd->on_hold)
692           {
693              item->wd->on_hold = EINA_TRUE;
694              if (!item->wd->wasselected)
695                _item_unselect(item);
696           }
697      }
698    if ((item->dragging) && (item->down))
699      {
700         if (item->long_timer)
701           {
702              ecore_timer_del(item->long_timer);
703              item->long_timer = NULL;
704           }
705         evas_object_smart_callback_call(item->wd->self, SIG_DRAG, item);
706         return;
707      }
708    if ((!item->down) || (item->wd->longpressed))
709      {
710         if (item->long_timer)
711           {
712              ecore_timer_del(item->long_timer);
713              item->long_timer = NULL;
714           }
715         if ((item->wd->reorder_mode) && (item->wd->reorder_item))
716           {
717              evas_object_geometry_get(item->wd->pan_smart, &ox, &oy, &ow, &oh);
718
719              it_scrl_x = ev->cur.canvas.x - item->wd->reorder_item->dx;
720              it_scrl_y = ev->cur.canvas.y - item->wd->reorder_item->dy;
721
722              if (it_scrl_x < ox) item->wd->reorder_item_x = ox;
723              else if (it_scrl_x + item->wd->item_width > ox + ow)
724                item->wd->reorder_item_x = ox + ow - item->wd->item_width;
725              else item->wd->reorder_item_x = it_scrl_x;
726
727              if (it_scrl_y < oy) item->wd->reorder_item_y = oy;
728              else if (it_scrl_y + item->wd->item_height > oy + oh)
729                item->wd->reorder_item_y = oy + oh - item->wd->item_height;
730              else item->wd->reorder_item_y = it_scrl_y;
731
732              if (item->wd->calc_job) ecore_job_del(item->wd->calc_job);
733              item->wd->calc_job = ecore_job_add(_calc_job, item->wd);
734           }
735         return;
736      }
737    if (!item->display_only)
738      elm_coords_finger_size_adjust(1, &minw, 1, &minh);
739    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
740    x = ev->cur.canvas.x - x;
741    y = ev->cur.canvas.y - y;
742    dx = x - item->dx;
743    adx = dx;
744    if (adx < 0) adx = -dx;
745    dy = y - item->dy;
746    ady = dy;
747    if (ady < 0) ady = -dy;
748    minw /= 2;
749    minh /= 2;
750    if ((adx > minw) || (ady > minh))
751      {
752         const char *left_drag, *right_drag;
753         if (!elm_widget_mirrored_get(item->wd->self))
754           {
755              left_drag = SIG_DRAG_START_LEFT;
756              right_drag = SIG_DRAG_START_RIGHT;
757           }
758         else
759           {
760              left_drag = SIG_DRAG_START_RIGHT;
761              right_drag = SIG_DRAG_START_LEFT;
762           }
763
764         item->dragging = 1;
765         if (item->long_timer)
766           {
767              ecore_timer_del(item->long_timer);
768              item->long_timer = NULL;
769           }
770         if (!item->wd->wasselected)
771           _item_unselect(item);
772         if (dy < 0)
773           {
774              if (ady > adx)
775                evas_object_smart_callback_call(item->wd->self, SIG_DRAG_START_UP,
776                                                item);
777              else
778                {
779                   if (dx < 0)
780                     evas_object_smart_callback_call(item->wd->self,
781                                                     left_drag, item);
782                }
783           }
784         else
785           {
786              if (ady > adx)
787                evas_object_smart_callback_call(item->wd->self,
788                                                SIG_DRAG_START_DOWN, item);
789              else
790                {
791                   if (dx < 0)
792                     evas_object_smart_callback_call(item->wd->self,
793                                                     left_drag, item);
794                   else
795                     evas_object_smart_callback_call(item->wd->self,
796                                                     right_drag, item);
797                }
798           }
799      }
800 }
801
802 static Eina_Bool
803 _long_press(void *data)
804 {
805    Elm_Gengrid_Item *item = data;
806
807    item->long_timer = NULL;
808    if ((item->disabled) || (item->dragging)) return ECORE_CALLBACK_CANCEL;
809    item->wd->longpressed = EINA_TRUE;
810    evas_object_smart_callback_call(item->wd->self, SIG_LONGPRESSED, item);
811    if (item->wd->reorder_mode)
812      {
813         item->wd->reorder_item = item;
814         evas_object_raise(item->base.view);
815         elm_smart_scroller_hold_set(item->wd->scr, EINA_TRUE);
816         elm_smart_scroller_bounce_allow_set(item->wd->scr, EINA_FALSE, EINA_FALSE);
817         edje_object_signal_emit(item->base.view, "elm,state,reorder,enabled", "elm");
818      }
819    return ECORE_CALLBACK_CANCEL;
820 }
821
822 static void
823 _mouse_down(void        *data,
824             Evas *evas   __UNUSED__,
825             Evas_Object *obj,
826             void        *event_info)
827 {
828    Elm_Gengrid_Item *item = data;
829    Evas_Event_Mouse_Down *ev = event_info;
830    Evas_Coord x, y;
831
832    if (ev->button != 1) return;
833    item->down = 1;
834    item->dragging = 0;
835    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
836    item->dx = ev->canvas.x - x;
837    item->dy = ev->canvas.y - y;
838    item->wd->longpressed = EINA_FALSE;
839    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) item->wd->on_hold = EINA_TRUE;
840    else item->wd->on_hold = EINA_FALSE;
841    item->wd->wasselected = item->selected;
842    _item_highlight(item);
843    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
844      {
845         evas_object_smart_callback_call(item->wd->self, SIG_CLICKED_DOUBLE, item);
846         evas_object_smart_callback_call(item->wd->self, SIG_ACTIVATED, item);
847      }
848    if (item->long_timer) ecore_timer_del(item->long_timer);
849    if (item->realized)
850      item->long_timer = ecore_timer_add(_elm_config->longpress_timeout,
851                                         _long_press, item);
852    else
853      item->long_timer = NULL;
854 }
855
856 static void
857 _mouse_up(void            *data,
858           Evas *evas       __UNUSED__,
859           Evas_Object *obj __UNUSED__,
860           void            *event_info)
861 {
862    Elm_Gengrid_Item *item = data;
863    Evas_Event_Mouse_Up *ev = event_info;
864    Eina_Bool dragged = EINA_FALSE;
865
866    if (ev->button != 1) return;
867    item->down = EINA_FALSE;
868    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) item->wd->on_hold = EINA_TRUE;
869    else item->wd->on_hold = EINA_FALSE;
870    if (item->long_timer)
871      {
872         ecore_timer_del(item->long_timer);
873         item->long_timer = NULL;
874      }
875    if (item->dragging)
876      {
877         item->dragging = EINA_FALSE;
878         evas_object_smart_callback_call(item->wd->self, SIG_DRAG_STOP, item);
879         dragged = EINA_TRUE;
880      }
881    if (item->wd->on_hold)
882      {
883         item->wd->longpressed = EINA_FALSE;
884         item->wd->on_hold = EINA_FALSE;
885         return;
886      }
887    if ((item->wd->reorder_mode) && (item->wd->reorder_item))
888      {
889         evas_object_smart_callback_call(item->wd->self, SIG_MOVED, item->wd->reorder_item);
890         item->wd->reorder_item = NULL;
891         item->wd->move_effect_enabled = EINA_FALSE;
892         if (item->wd->calc_job) ecore_job_del(item->wd->calc_job);
893           item->wd->calc_job = ecore_job_add(_calc_job, item->wd);
894
895         elm_smart_scroller_hold_set(item->wd->scr, EINA_FALSE);
896         elm_smart_scroller_bounce_allow_set(item->wd->scr, item->wd->h_bounce, item->wd->v_bounce);
897         edje_object_signal_emit(item->base.view, "elm,state,reorder,disabled", "elm");
898      }
899    if (item->wd->longpressed)
900      {
901         item->wd->longpressed = EINA_FALSE;
902         if (!item->wd->wasselected) _item_unselect(item);
903         item->wd->wasselected = EINA_FALSE;
904         return;
905      }
906    if (dragged)
907      {
908         if (item->want_unrealize) _item_unrealize(item);
909      }
910    if ((item->disabled) || (dragged)) return;
911    if (item->wd->multi)
912      {
913         if (!item->selected)
914           {
915              _item_highlight(item);
916              _item_select(item);
917           }
918         else _item_unselect(item);
919      }
920    else
921      {
922         if (!item->selected)
923           {
924              while (item->wd->selected)
925                _item_unselect(item->wd->selected->data);
926           }
927         else
928           {
929              const Eina_List *l, *l_next;
930              Elm_Gengrid_Item *item2;
931
932              EINA_LIST_FOREACH_SAFE(item->wd->selected, l, l_next, item2)
933                 if (item2 != item) _item_unselect(item2);
934           }
935         _item_highlight(item);
936         _item_select(item);
937      }
938 }
939
940 static void
941 _item_highlight(Elm_Gengrid_Item *item)
942 {
943    if ((item->wd->no_select) || (item->delete_me) || (item->highlighted)) return;
944    edje_object_signal_emit(item->base.view, "elm,state,selected", "elm");
945    item->highlighted = EINA_TRUE;
946 }
947
948 static void
949 _item_realize(Elm_Gengrid_Item *item)
950 {
951    char buf[1024];
952    char style[1024];
953
954    if ((item->realized) || (item->delete_me)) return;
955    item->base.view = edje_object_add(evas_object_evas_get(item->wd->self));
956    edje_object_scale_set(item->base.view, elm_widget_scale_get(item->wd->self) *
957                          _elm_config->scale);
958    edje_object_mirrored_set(item->base.view, elm_widget_mirrored_get(item->base.widget));
959    evas_object_smart_member_add(item->base.view, item->wd->pan_smart);
960    elm_widget_sub_object_add(item->wd->self, item->base.view);
961    snprintf(style, sizeof(style), "item/%s",
962             item->gic->item_style ? item->gic->item_style : "default");
963    _elm_theme_object_set(item->wd->self, item->base.view, "gengrid", style,
964                          elm_widget_style_get(item->wd->self));
965    item->spacer =
966       evas_object_rectangle_add(evas_object_evas_get(item->wd->self));
967    evas_object_color_set(item->spacer, 0, 0, 0, 0);
968    elm_widget_sub_object_add(item->wd->self, item->spacer);
969    evas_object_size_hint_min_set(item->spacer, 2 * _elm_config->scale, 1);
970    edje_object_part_swallow(item->base.view, "elm.swallow.pad", item->spacer);
971
972    if (item->gic->func.label_get)
973      {
974         const Eina_List *l;
975         const char *key;
976
977         item->labels =
978            elm_widget_stringlist_get(edje_object_data_get(item->base.view,
979                                                           "labels"));
980         EINA_LIST_FOREACH(item->labels, l, key)
981           {
982              char *s = item->gic->func.label_get
983                 ((void *)item->base.data, item->wd->self, l->data);
984              if (s)
985                {
986                   edje_object_part_text_set(item->base.view, l->data, s);
987                   free(s);
988                }
989           }
990      }
991
992    if (item->gic->func.icon_get)
993      {
994         const Eina_List *l;
995         const char *key;
996
997         item->icons =
998            elm_widget_stringlist_get(edje_object_data_get(item->base.view,
999                                                           "icons"));
1000         EINA_LIST_FOREACH(item->icons, l, key)
1001           {
1002              Evas_Object *ic = item->gic->func.icon_get
1003                 ((void *)item->base.data, item->wd->self, l->data);
1004              if (ic)
1005                {
1006                   item->icon_objs = eina_list_append(item->icon_objs, ic);
1007                   edje_object_part_swallow(item->base.view, key, ic);
1008                   evas_object_show(ic);
1009                   elm_widget_sub_object_add(item->wd->self, ic);
1010                }
1011           }
1012      }
1013
1014    if (item->gic->func.state_get)
1015      {
1016         const Eina_List *l;
1017         const char *key;
1018
1019         item->states =
1020            elm_widget_stringlist_get(edje_object_data_get(item->base.view,
1021                                                           "states"));
1022         EINA_LIST_FOREACH(item->states, l, key)
1023           {
1024              Eina_Bool on = item->gic->func.state_get
1025                 ((void *)item->base.data, item->wd->self, l->data);
1026              if (on)
1027                {
1028                   snprintf(buf, sizeof(buf), "elm,state,%s,active", key);
1029                   edje_object_signal_emit(item->base.view, buf, "elm");
1030                }
1031           }
1032      }
1033
1034    if (item->is_group)
1035      {
1036         if ((!item->wd->group_item_width) && (!item->wd->group_item_height))
1037           {
1038              edje_object_size_min_restricted_calc(item->base.view,
1039                                                   &item->wd->group_item_width,
1040                                                   &item->wd->group_item_height,
1041                                                   item->wd->group_item_width,
1042                                                   item->wd->group_item_height);
1043           }
1044      }
1045    else
1046      {
1047         if ((!item->wd->item_width) && (!item->wd->item_height))
1048           {
1049              edje_object_size_min_restricted_calc(item->base.view,
1050                                                   &item->wd->item_width,
1051                                                   &item->wd->item_height,
1052                                                   item->wd->item_width,
1053                                                   item->wd->item_height);
1054              elm_coords_finger_size_adjust(1, &item->wd->item_width,
1055                                            1, &item->wd->item_height);
1056           }
1057
1058         evas_object_event_callback_add(item->base.view, EVAS_CALLBACK_MOUSE_DOWN,
1059                                        _mouse_down, item);
1060         evas_object_event_callback_add(item->base.view, EVAS_CALLBACK_MOUSE_UP,
1061                                        _mouse_up, item);
1062         evas_object_event_callback_add(item->base.view, EVAS_CALLBACK_MOUSE_MOVE,
1063                                        _mouse_move, item);
1064
1065         if (item->selected)
1066           edje_object_signal_emit(item->base.view, "elm,state,selected", "elm");
1067         if (item->disabled)
1068           edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
1069      }
1070    evas_object_show(item->base.view);
1071
1072    if (item->tooltip.content_cb)
1073      {
1074         elm_widget_item_tooltip_content_cb_set(item,
1075                                                item->tooltip.content_cb,
1076                                                item->tooltip.data, NULL);
1077         elm_widget_item_tooltip_style_set(item, item->tooltip.style);
1078         elm_widget_item_tooltip_size_restrict_disable(item, item->tooltip.free_size);
1079      }
1080
1081    if (item->mouse_cursor)
1082      elm_widget_item_cursor_set(item, item->mouse_cursor);
1083
1084    item->realized = EINA_TRUE;
1085    item->want_unrealize = EINA_FALSE;
1086 }
1087
1088 static void
1089 _item_unrealize(Elm_Gengrid_Item *item)
1090 {
1091    Evas_Object *icon;
1092
1093    if (!item->realized) return;
1094    if (item->long_timer)
1095      {
1096         ecore_timer_del(item->long_timer);
1097         item->long_timer = NULL;
1098      }
1099    evas_object_del(item->base.view);
1100    item->base.view = NULL;
1101    evas_object_del(item->spacer);
1102    item->spacer = NULL;
1103    elm_widget_stringlist_free(item->labels);
1104    item->labels = NULL;
1105    elm_widget_stringlist_free(item->icons);
1106    item->icons = NULL;
1107    elm_widget_stringlist_free(item->states);
1108    item->states = NULL;
1109
1110    EINA_LIST_FREE(item->icon_objs, icon)
1111       evas_object_del(icon);
1112
1113    item->realized = EINA_FALSE;
1114    item->want_unrealize = EINA_FALSE;
1115 }
1116
1117 static Eina_Bool
1118 _reorder_item_moving_effect_timer_cb(void *data)
1119 {
1120    Elm_Gengrid_Item *item = data;
1121    double time, t;
1122    Evas_Coord dx, dy;
1123
1124    time = REORDER_EFFECT_TIME;
1125    t = ((0.0 > (t = ecore_loop_time_get()-item->moving_effect_start_time)) ? 0.0 : t);
1126    dx = ((item->tx - item->ox) / 10) * _elm_config->scale;
1127    dy = ((item->ty - item->oy) / 10) * _elm_config->scale;
1128
1129    if (t <= time)
1130      {
1131         item->rx += (1 * sin((t / time) * (M_PI / 2)) * dx);
1132         item->ry += (1 * sin((t / time) * (M_PI / 2)) * dy);
1133      }
1134    else
1135      {
1136         item->rx += dx;
1137         item->ry += dy;
1138      }
1139
1140    if ((((dx > 0) && (item->rx >= item->tx)) || ((dx <= 0) && (item->rx <= item->tx))) &&
1141        (((dy > 0) && (item->ry >= item->ty)) || ((dy <= 0) && (item->ry <= item->ty))))
1142      {
1143         evas_object_move(item->base.view, item->tx, item->ty);
1144         if (item->is_group)
1145           {
1146              Evas_Coord vw, vh;
1147              evas_object_geometry_get(item->wd->pan_smart, NULL, NULL, &vw, &vh);
1148              if (item->wd->horizontal)
1149                evas_object_resize(item->base.view, item->wd->group_item_width, vh);
1150              else
1151                evas_object_resize(item->base.view, vw, item->wd->group_item_height);
1152           }
1153         else
1154           evas_object_resize(item->base.view, item->wd->item_width, item->wd->item_height);
1155         item->moving = EINA_FALSE;
1156         item->item_moving_effect_timer = NULL;
1157         return ECORE_CALLBACK_CANCEL;
1158      }
1159
1160    evas_object_move(item->base.view, item->rx, item->ry);
1161    if (item->is_group)
1162      {
1163         Evas_Coord vw, vh;
1164         evas_object_geometry_get(item->wd->pan_smart, NULL, NULL, &vw, &vh);
1165         if (item->wd->horizontal)
1166           evas_object_resize(item->base.view, item->wd->group_item_width, vh);
1167         else
1168           evas_object_resize(item->base.view, vw, item->wd->group_item_height);
1169      }
1170    else
1171      evas_object_resize(item->base.view, item->wd->item_width, item->wd->item_height);
1172
1173    return ECORE_CALLBACK_RENEW;
1174 }
1175
1176 static void
1177 _group_item_place(Pan *sd)
1178 {
1179    Evas_Coord iw, ih, vw, vh;
1180    Eina_List *l;
1181    Eina_Bool was_realized;
1182    Elm_Gengrid_Item *it;
1183    evas_object_geometry_get(sd->wd->pan_smart, NULL, NULL, &vw, &vh);
1184    if (sd->wd->horizontal)
1185      {
1186         iw = sd->wd->group_item_width;
1187         ih = vh;
1188      }
1189    else
1190      {
1191         iw = vw;
1192         ih = sd->wd->group_item_height;
1193      }
1194    EINA_LIST_FOREACH(sd->wd->group_items, l, it)
1195      {
1196         was_realized = it->realized;
1197         if (it->group_realized)
1198           {
1199              _item_realize(it);
1200              if (!was_realized)
1201                evas_object_smart_callback_call(it->wd->self, SIG_REALIZED, it);
1202              evas_object_move(it->base.view, it->gx, it->gy);
1203              evas_object_resize(it->base.view, iw, ih);
1204              evas_object_raise(it->base.view);
1205           }
1206         else
1207           {
1208              _item_unrealize(it);
1209              if (was_realized)
1210                evas_object_smart_callback_call(it->wd->self, SIG_UNREALIZED, it);
1211           }
1212      }
1213 }
1214
1215
1216 static void
1217 _item_place(Elm_Gengrid_Item *item,
1218             Evas_Coord        cx,
1219             Evas_Coord        cy)
1220 {
1221    Evas_Coord x, y, ox, oy, cvx, cvy, cvw, cvh, iw, ih, ww;
1222    Evas_Coord tch, tcw, alignw = 0, alignh = 0, vw, vh;
1223    Eina_Bool reorder_item_move_forward = EINA_FALSE;
1224    long items_count;
1225    item->x = cx;
1226    item->y = cy;
1227    evas_object_geometry_get(item->wd->pan_smart, &ox, &oy, &vw, &vh);
1228
1229    /* Preload rows/columns at each side of the Gengrid */
1230    cvx = ox - PRELOAD * item->wd->item_width;
1231    cvy = oy - PRELOAD * item->wd->item_height;
1232    cvw = vw + 2 * PRELOAD * item->wd->item_width;
1233    cvh = vh + 2 * PRELOAD * item->wd->item_height;
1234
1235    alignh = 0;
1236    alignw = 0;
1237
1238    items_count = item->wd->count - eina_list_count(item->wd->group_items) + item->wd->items_lost;
1239    if (item->wd->horizontal)
1240      {
1241         int columns, items_visible = 0, items_row;
1242
1243         if (item->wd->item_height > 0)
1244           items_visible = vh / item->wd->item_height;
1245         if (items_visible < 1)
1246           items_visible = 1;
1247
1248         columns = items_count / items_visible;
1249         if (items_count % items_visible)
1250           columns++;
1251
1252         tcw = (item->wd->item_width * columns) + (item->wd->group_item_width * eina_list_count(item->wd->group_items));
1253         alignw = (vw - tcw) * item->wd->align_x;
1254
1255         items_row = items_visible;
1256         if (items_row > item->wd->count)
1257           items_row = item->wd->count;
1258         tch = items_row * item->wd->item_height;
1259         alignh = (vh - tch) * item->wd->align_y;
1260      }
1261    else
1262      {
1263         int rows, items_visible = 0, items_col;
1264
1265         if (item->wd->item_width > 0)
1266           items_visible = vw / item->wd->item_width;
1267         if (items_visible < 1)
1268           items_visible = 1;
1269
1270         rows = items_count / items_visible;
1271         if (items_count % items_visible)
1272           rows++;
1273
1274         tch = (item->wd->item_height * rows) + (item->wd->group_item_height * eina_list_count(item->wd->group_items));
1275         alignh = (vh - tch) * item->wd->align_y;
1276
1277         items_col = items_visible;
1278         if (items_col > item->wd->count)
1279           items_col = item->wd->count;
1280         tcw = items_col * item->wd->item_width;
1281         alignw = (vw - tcw) * item->wd->align_x;
1282      }
1283
1284    if (item->is_group)
1285      {
1286         if (item->wd->horizontal)
1287           {
1288              x = (((cx - item->prev_group) * item->wd->item_width) + (item->prev_group * item->wd->group_item_width)) - item->wd->pan_x + ox + alignw;
1289              y = 0;
1290              iw = item->wd->group_item_width;
1291              ih = vh;
1292           }
1293         else
1294           {
1295              x = 0;
1296              y = (((cy - item->prev_group) * item->wd->item_height) + (item->prev_group * item->wd->group_item_height)) - item->wd->pan_y + oy + alignh;
1297              iw = vw;
1298              ih = item->wd->group_item_height;
1299           }
1300         item->gx = x;
1301         item->gy = y;
1302      }
1303    else
1304      {
1305         if (item->wd->horizontal)
1306           {
1307              x = (((cx - item->prev_group) * item->wd->item_width) + (item->prev_group * item->wd->group_item_width)) - item->wd->pan_x + ox + alignw;
1308              y = (cy * item->wd->item_height) - item->wd->pan_y + oy + alignh;
1309           }
1310         else
1311           {
1312              x = (cx * item->wd->item_width) - item->wd->pan_x + ox + alignw;
1313              y = (((cy - item->prev_group) * item->wd->item_height) + (item->prev_group * item->wd->group_item_height)) - item->wd->pan_y + oy + alignh;
1314           }
1315         if (elm_widget_mirrored_get(item->wd->self))
1316           {  /* Switch items side and componsate for pan_x when in RTL mode */
1317              evas_object_geometry_get(item->wd->self, NULL, NULL, &ww, NULL);
1318              x = ww - x - item->wd->item_width - item->wd->pan_x - item->wd->pan_x;
1319           }
1320         iw = item->wd->item_width;
1321         ih = item->wd->item_height;
1322      }
1323
1324    Eina_Bool was_realized = item->realized;
1325    if (ELM_RECTS_INTERSECT(x, y, iw, ih, cvx, cvy, cvw, cvh))
1326      {
1327         _item_realize(item);
1328         if (!was_realized)
1329           evas_object_smart_callback_call(item->wd->self, SIG_REALIZED, item);
1330         if (item->parent_group_item)
1331           {
1332              if (item->wd->horizontal)
1333                {
1334                   if (item->parent_group_item->gx < 0)
1335                     {
1336                        item->parent_group_item->gx = x + item->wd->item_width - item->wd->group_item_width;
1337                        if (item->parent_group_item->gx > 0)
1338                          item->parent_group_item->gx = 0;
1339                     }
1340                }
1341              else
1342                {
1343                   if (item->parent_group_item->gy < 0)
1344                     {
1345                        item->parent_group_item->gy = y + item->wd->item_height - item->wd->group_item_height;
1346                        if (item->parent_group_item->gy > 0)
1347                          item->parent_group_item->gy = 0;
1348                     }
1349                   item->parent_group_item->group_realized = EINA_TRUE;
1350                }
1351           }
1352         if (item->wd->reorder_mode)
1353           {
1354              if (item->wd->reorder_item)
1355                {
1356                   if (item->moving) return;
1357
1358                   if (!item->wd->move_effect_enabled)
1359                     {
1360                        item->ox = x;
1361                        item->oy = y;
1362                     }
1363                   if (item->wd->reorder_item == item)
1364                     {
1365                        evas_object_move(item->base.view,
1366                                         item->wd->reorder_item_x, item->wd->reorder_item_y);
1367                        evas_object_resize(item->base.view, iw, ih);
1368                        return;
1369                     }
1370                   else
1371                     {
1372                        if (item->wd->move_effect_enabled)
1373                          {
1374                             if ((item->ox != x) || (item->oy != y))
1375                               {
1376                                  if (((item->wd->old_pan_x == item->wd->pan_x) && (item->wd->old_pan_y == item->wd->pan_y)) ||
1377                                      ((item->wd->old_pan_x != item->wd->pan_x) && !(item->ox - item->wd->pan_x + item->wd->old_pan_x == x)) ||
1378                                      ((item->wd->old_pan_y != item->wd->pan_y) && !(item->oy - item->wd->pan_y + item->wd->old_pan_y == y)))
1379                                    {
1380                                       item->tx = x;
1381                                       item->ty = y;
1382                                       item->rx = item->ox;
1383                                       item->ry = item->oy;
1384                                       item->moving = EINA_TRUE;
1385                                       item->moving_effect_start_time = ecore_loop_time_get();
1386                                       item->item_moving_effect_timer = ecore_animator_add(_reorder_item_moving_effect_timer_cb, item);
1387                                       return;
1388                                    }
1389                               }
1390                          }
1391
1392                        /* need fix here */
1393                        Evas_Coord nx, ny, nw, nh;
1394                        if (item->is_group)
1395                          {
1396                             if (item->wd->horizontal)
1397                               {
1398                                  nx = x + (item->wd->group_item_width / 2);
1399                                  ny = y;
1400                                  nw = 1;
1401                                  nh = vh;
1402                               }
1403                             else
1404                               {
1405                                  nx = x;
1406                                  ny = y + (item->wd->group_item_height / 2);
1407                                  nw = vw;
1408                                  nh = 1;
1409                               }
1410                          }
1411                        else
1412                          {
1413                             nx = x + (item->wd->item_width / 2);
1414                             ny = y + (item->wd->item_height / 2);
1415                             nw = 1;
1416                             nh = 1;
1417                          }
1418
1419                        if ( ELM_RECTS_INTERSECT(item->wd->reorder_item_x, item->wd->reorder_item_y,
1420                                                 item->wd->item_width, item->wd->item_height,
1421                                                 nx, ny, nw, nh))
1422                          {
1423                             if (item->wd->horizontal)
1424                               {
1425                                  if ((item->wd->nmax * item->wd->reorder_item->x + item->wd->reorder_item->y) >
1426                                      (item->wd->nmax * item->x + item->y))
1427                                    reorder_item_move_forward = EINA_TRUE;
1428                               }
1429                             else
1430                               {
1431                                  if ((item->wd->nmax * item->wd->reorder_item->y + item->wd->reorder_item->x) >
1432                                      (item->wd->nmax * item->y + item->x))
1433                                    reorder_item_move_forward = EINA_TRUE;
1434                               }
1435
1436                             item->wd->items = eina_inlist_remove(item->wd->items,
1437                                                                  EINA_INLIST_GET(item->wd->reorder_item));
1438                             if (reorder_item_move_forward)
1439                               item->wd->items = eina_inlist_prepend_relative(item->wd->items,
1440                                                                              EINA_INLIST_GET(item->wd->reorder_item),
1441                                                                              EINA_INLIST_GET(item));
1442                             else
1443                               item->wd->items = eina_inlist_append_relative(item->wd->items,
1444                                                                             EINA_INLIST_GET(item->wd->reorder_item),
1445                                                                             EINA_INLIST_GET(item));
1446
1447                             item->wd->reorder_item_changed = EINA_TRUE;
1448                             item->wd->move_effect_enabled = EINA_TRUE;
1449                             if (item->wd->calc_job) ecore_job_del(item->wd->calc_job);
1450                               item->wd->calc_job = ecore_job_add(_calc_job, item->wd);
1451
1452                             return;
1453                          }
1454                     }
1455                }
1456              else if (item->item_moving_effect_timer)
1457                {
1458                   ecore_animator_del(item->item_moving_effect_timer);
1459                   item->item_moving_effect_timer = NULL;
1460                   item->moving = EINA_FALSE;
1461                }
1462           }
1463         if (!item->is_group)
1464           {
1465              evas_object_move(item->base.view, x, y);
1466              evas_object_resize(item->base.view, iw, ih);
1467           }
1468         else
1469           item->group_realized = EINA_TRUE;
1470      }
1471    else
1472      {
1473         if (!item->is_group)
1474           {
1475              _item_unrealize(item);
1476              if (was_realized)
1477                evas_object_smart_callback_call(item->wd->self, SIG_UNREALIZED, item);
1478           }
1479         else
1480           item->group_realized = EINA_FALSE;
1481      }
1482 }
1483
1484 static const char *
1485 _item_label_hook(Elm_Gengrid_Item *it, const char *part)
1486 {
1487    if (!it->gic->func.label_get) return NULL;
1488    return edje_object_part_text_get(it->base.view, part);
1489 }
1490
1491 static Elm_Gengrid_Item *
1492 _item_create(Widget_Data                  *wd,
1493              const Elm_Gengrid_Item_Class *gic,
1494              const void                   *data,
1495              Evas_Smart_Cb                 func,
1496              const void                   *func_data)
1497 {
1498    Elm_Gengrid_Item *item;
1499
1500    item = elm_widget_item_new(wd->self, Elm_Gengrid_Item);
1501    if (!item) return NULL;
1502    wd->count++;
1503    item->wd = wd;
1504    item->gic = gic;
1505    item->base.data = data;
1506    item->func.func = func;
1507    item->func.data = func_data;
1508    item->mouse_cursor = NULL;
1509    item->is_group = !strcmp(item->gic->item_style, "group_index");
1510    elm_widget_item_text_get_hook_set(item, _item_label_hook);
1511
1512    return item;
1513 }
1514
1515 static void
1516 _item_del(Elm_Gengrid_Item *item)
1517 {
1518    elm_widget_item_pre_notify_del(item);
1519    if (item->selected)
1520      item->wd->selected = eina_list_remove(item->wd->selected, item);
1521    if (item->realized) _item_unrealize(item);
1522    if ((!item->delete_me) && (item->gic->func.del))
1523      item->gic->func.del((void *)item->base.data, item->wd->self);
1524    item->delete_me = EINA_TRUE;
1525    item->wd->items = eina_inlist_remove(item->wd->items, EINA_INLIST_GET(item));
1526    if (item->long_timer) ecore_timer_del(item->long_timer);
1527    if (item->tooltip.del_cb)
1528      item->tooltip.del_cb((void *)item->tooltip.data, item->base.widget, item);
1529    item->wd->walking -= item->walking;
1530    item->wd->count--;
1531    if (item->is_group)
1532      item->wd->group_items = eina_list_remove(item->wd->group_items, item);
1533    if (item->wd->calc_job) ecore_job_del(item->wd->calc_job);
1534    item->wd->calc_job = ecore_job_add(_calc_job, item->wd);
1535    elm_widget_item_del(item);
1536 }
1537
1538 static void
1539 _item_select(Elm_Gengrid_Item *item)
1540 {
1541    if ((item->wd->no_select) || (item->delete_me)) return;
1542    if (item->selected)
1543      {
1544         if (item->wd->always_select) goto call;
1545         return;
1546      }
1547    item->selected = EINA_TRUE;
1548    item->wd->selected = eina_list_append(item->wd->selected, item);
1549 call:
1550    item->walking++;
1551    item->wd->walking++;
1552    if (item->func.func)
1553      item->func.func((void *)item->func.data, item->wd->self, item);
1554    if (!item->delete_me)
1555      evas_object_smart_callback_call(item->wd->self, SIG_SELECTED, item);
1556    item->walking--;
1557    item->wd->walking--;
1558    item->wd->last_selected_item = item;
1559    if ((item->wd->clear_me) && (!item->wd->walking))
1560      elm_gengrid_clear(item->base.widget);
1561    else
1562      {
1563         if ((!item->walking) && (item->delete_me))
1564           if (!item->relcount) _item_del(item);
1565      }
1566 }
1567
1568 static void
1569 _item_unselect(Elm_Gengrid_Item *item)
1570 {
1571    if ((item->delete_me) || (!item->highlighted)) return;
1572    edje_object_signal_emit(item->base.view, "elm,state,unselected", "elm");
1573    item->highlighted = EINA_FALSE;
1574    if (item->selected)
1575      {
1576         item->selected = EINA_FALSE;
1577         item->wd->selected = eina_list_remove(item->wd->selected, item);
1578         evas_object_smart_callback_call(item->wd->self, SIG_UNSELECTED, item);
1579      }
1580 }
1581
1582 static void
1583 _calc_job(void *data)
1584 {
1585    Widget_Data *wd = data;
1586    Evas_Coord minw = 0, minh = 0, nmax = 0, cvw, cvh;
1587    Elm_Gengrid_Item *item, *group_item = NULL;
1588    int count_group = 0;
1589    long count = 0;
1590    wd->items_lost = 0;
1591
1592    evas_object_geometry_get(wd->pan_smart, NULL, NULL, &cvw, &cvh);
1593    if ((cvw != 0) || (cvh != 0))
1594      {
1595         if ((wd->horizontal) && (wd->item_height > 0))
1596           nmax = cvh / wd->item_height;
1597         else if (wd->item_width > 0)
1598           nmax = cvw / wd->item_width;
1599
1600         if (nmax < 1)
1601           nmax = 1;
1602
1603         EINA_INLIST_FOREACH(wd->items, item)
1604           {
1605              if (item->prev_group != count_group)
1606                item->prev_group = count_group;
1607              if (item->is_group)
1608                {
1609                   count = count % nmax;
1610                   if (count)
1611                     wd->items_lost += nmax - count;
1612                   //printf("%d items and I lost %d\n", count, wd->items_lost);
1613                   count_group++;
1614                   if (count) count = 0;
1615                   group_item = item;
1616                }
1617              else
1618                {
1619                   if (item->parent_group_item != group_item)
1620                     item->parent_group_item = group_item;
1621                   count++;
1622                }
1623           }
1624         count = wd->count + wd->items_lost - count_group;
1625         if (wd->horizontal)
1626           {
1627              minw = (ceil(count / (float)nmax) * wd->item_width) + (count_group * wd->group_item_width);
1628              minh = nmax * wd->item_height;
1629           }
1630         else
1631           {
1632              minw = nmax * wd->item_width;
1633              minh = (ceil(count / (float)nmax) * wd->item_height) + (count_group * wd->group_item_height);
1634           }
1635
1636         if ((minw != wd->minw) || (minh != wd->minh))
1637           {
1638              wd->minh = minh;
1639              wd->minw = minw;
1640              evas_object_smart_callback_call(wd->pan_smart, "changed", NULL);
1641           }
1642
1643         wd->nmax = nmax;
1644         evas_object_smart_changed(wd->pan_smart);
1645      }
1646    wd->calc_job = NULL;
1647 }
1648
1649 static void
1650 _pan_add(Evas_Object *obj)
1651 {
1652    Pan *sd;
1653    Evas_Object_Smart_Clipped_Data *cd;
1654
1655    _pan_sc.add(obj);
1656    cd = evas_object_smart_data_get(obj);
1657    sd = ELM_NEW(Pan);
1658    if (!sd) return;
1659    sd->__clipped_data = *cd;
1660    free(cd);
1661    evas_object_smart_data_set(obj, sd);
1662 }
1663
1664 static void
1665 _pan_del(Evas_Object *obj)
1666 {
1667    Pan *sd = evas_object_smart_data_get(obj);
1668
1669    if (!sd) return;
1670    _pan_sc.del(obj);
1671 }
1672
1673 static void
1674 _pan_set(Evas_Object *obj,
1675          Evas_Coord   x,
1676          Evas_Coord   y)
1677 {
1678    Pan *sd = evas_object_smart_data_get(obj);
1679    if ((x == sd->wd->pan_x) && (y == sd->wd->pan_y)) return;
1680    sd->wd->pan_x = x;
1681    sd->wd->pan_y = y;
1682    evas_object_smart_changed(obj);
1683 }
1684
1685 static void
1686 _pan_get(Evas_Object *obj,
1687          Evas_Coord  *x,
1688          Evas_Coord  *y)
1689 {
1690    Pan *sd = evas_object_smart_data_get(obj);
1691    if (x) *x = sd->wd->pan_x;
1692    if (y) *y = sd->wd->pan_y;
1693 }
1694
1695 static void
1696 _pan_child_size_get(Evas_Object *obj,
1697                     Evas_Coord  *w,
1698                     Evas_Coord  *h)
1699 {
1700    Pan *sd = evas_object_smart_data_get(obj);
1701    if (w) *w = sd->wd->minw;
1702    if (h) *h = sd->wd->minh;
1703 }
1704
1705 static void
1706 _pan_max_get(Evas_Object *obj,
1707              Evas_Coord  *x,
1708              Evas_Coord  *y)
1709 {
1710    Pan *sd = evas_object_smart_data_get(obj);
1711    Evas_Coord ow, oh;
1712
1713    if (!sd) return;
1714    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1715    if (x)
1716      *x = (ow < sd->wd->minw) ? sd->wd->minw - ow : 0;
1717    if (y)
1718      *y = (oh < sd->wd->minh) ? sd->wd->minh - oh : 0;
1719 }
1720
1721 static void
1722 _pan_min_get(Evas_Object *obj,
1723              Evas_Coord  *x,
1724              Evas_Coord  *y)
1725 {
1726    Pan *sd = evas_object_smart_data_get(obj);
1727    Evas_Coord mx, my;
1728
1729    if (!sd) return;
1730    _pan_max_get(obj, &mx, &my);
1731    if (x)
1732      *x = -mx * sd->wd->align_x;
1733    if (y)
1734      *y = -my * sd->wd->align_y;
1735 }
1736
1737 static void
1738 _pan_resize(Evas_Object *obj,
1739             Evas_Coord   w,
1740             Evas_Coord   h)
1741 {
1742    Pan *sd = evas_object_smart_data_get(obj);
1743    Evas_Coord ow, oh;
1744
1745    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
1746    if ((ow == w) && (oh == h)) return;
1747    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
1748    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
1749 }
1750
1751 static void
1752 _pan_calculate(Evas_Object *obj)
1753 {
1754    Pan *sd = evas_object_smart_data_get(obj);
1755    Evas_Coord cx = 0, cy = 0;
1756    Elm_Gengrid_Item *item;
1757
1758    if (!sd) return;
1759    if (!sd->wd->nmax) return;
1760
1761    sd->wd->reorder_item_changed = EINA_FALSE;
1762
1763    EINA_INLIST_FOREACH(sd->wd->items, item)
1764      {
1765         if (item->is_group)
1766           {
1767              if (sd->wd->horizontal)
1768                {
1769                   if (cy)
1770                     {
1771                        cx++;
1772                        cy = 0;
1773                     }
1774                }
1775              else
1776                {
1777                   if (cx)
1778                     {
1779                        cx = 0;
1780                        cy++;
1781                     }
1782                }
1783           }
1784         _item_place(item, cx, cy);
1785         if (sd->wd->reorder_item_changed) return;
1786         if (item->is_group)
1787           {
1788              if (sd->wd->horizontal)
1789                {
1790                   cx++;
1791                   cy = 0;
1792                }
1793              else
1794                {
1795                   cx = 0;
1796                   cy++;
1797                }
1798           }
1799         else
1800           {
1801              if (sd->wd->horizontal)
1802                {
1803                   cy = (cy + 1) % sd->wd->nmax;
1804                   if (!cy) cx++;
1805                }
1806              else
1807                {
1808                   cx = (cx + 1) % sd->wd->nmax;
1809                   if (!cx) cy++;
1810                }
1811           }
1812      }
1813    _group_item_place(sd);
1814
1815
1816    if ((sd->wd->reorder_mode) && (sd->wd->reorder_item))
1817      {
1818         if (!sd->wd->reorder_item_changed)
1819           {
1820              sd->wd->old_pan_x = sd->wd->pan_x;
1821              sd->wd->old_pan_y = sd->wd->pan_y;
1822           }
1823         sd->wd->move_effect_enabled = EINA_FALSE;
1824      }
1825    evas_object_smart_callback_call(sd->wd->self, SIG_CHANGED, NULL);
1826 }
1827
1828 static void
1829 _pan_move(Evas_Object *obj,
1830           Evas_Coord x __UNUSED__,
1831           Evas_Coord y __UNUSED__)
1832 {
1833    Pan *sd = evas_object_smart_data_get(obj);
1834    if (!sd) return;
1835    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
1836    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
1837 }
1838
1839 static void
1840 _hold_on(void *data       __UNUSED__,
1841          Evas_Object     *obj,
1842          void *event_info __UNUSED__)
1843 {
1844    Widget_Data *wd = elm_widget_data_get(obj);
1845    if (!wd) return;
1846    elm_smart_scroller_hold_set(wd->scr, 1);
1847 }
1848
1849 static void
1850 _hold_off(void *data       __UNUSED__,
1851           Evas_Object     *obj,
1852           void *event_info __UNUSED__)
1853 {
1854    Widget_Data *wd = elm_widget_data_get(obj);
1855    if (!wd) return;
1856    elm_smart_scroller_hold_set(wd->scr, 0);
1857 }
1858
1859 static void
1860 _freeze_on(void *data       __UNUSED__,
1861            Evas_Object     *obj,
1862            void *event_info __UNUSED__)
1863 {
1864    Widget_Data *wd = elm_widget_data_get(obj);
1865    if (!wd) return;
1866    elm_smart_scroller_freeze_set(wd->scr, 1);
1867 }
1868
1869 static void
1870 _freeze_off(void *data       __UNUSED__,
1871             Evas_Object     *obj,
1872             void *event_info __UNUSED__)
1873 {
1874    Widget_Data *wd = elm_widget_data_get(obj);
1875    if (!wd) return;
1876    elm_smart_scroller_freeze_set(wd->scr, 0);
1877 }
1878
1879 static void
1880 _scr_anim_start(void        *data,
1881                 Evas_Object *obj __UNUSED__,
1882                 void        *event_info __UNUSED__)
1883 {
1884    evas_object_smart_callback_call(data, SIG_SCROLL_ANIM_START, NULL);
1885 }
1886
1887 static void
1888 _scr_anim_stop(void        *data,
1889                 Evas_Object *obj __UNUSED__,
1890                 void        *event_info __UNUSED__)
1891 {
1892    evas_object_smart_callback_call(data, SIG_SCROLL_ANIM_STOP, NULL);
1893 }
1894
1895 static void
1896 _scr_drag_start(void            *data,
1897                 Evas_Object *obj __UNUSED__,
1898                 void *event_info __UNUSED__)
1899 {
1900    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_START, NULL);
1901 }
1902
1903 static void
1904 _scr_drag_stop(void            *data,
1905                Evas_Object *obj __UNUSED__,
1906                void *event_info __UNUSED__)
1907 {
1908    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_STOP, NULL);
1909 }
1910
1911 static void
1912 _edge_left(void        *data,
1913            Evas_Object *scr __UNUSED__,
1914            void        *event_info __UNUSED__)
1915 {
1916    evas_object_smart_callback_call(data, SIG_SCROLL_EDGE_LEFT, NULL);
1917    evas_object_smart_callback_call(data, SIG_EDGE_LEFT, NULL);
1918 }
1919
1920 static void
1921 _edge_right(void        *data,
1922             Evas_Object *scr __UNUSED__,
1923             void        *event_info __UNUSED__)
1924 {
1925    evas_object_smart_callback_call(data, SIG_SCROLL_EDGE_RIGHT, NULL);
1926    evas_object_smart_callback_call(data, SIG_EDGE_RIGHT, NULL);
1927 }
1928
1929 static void
1930 _edge_top(void        *data,
1931           Evas_Object *scr __UNUSED__,
1932           void        *event_info __UNUSED__)
1933 {
1934    evas_object_smart_callback_call(data, SIG_SCROLL_EDGE_TOP, NULL);
1935    evas_object_smart_callback_call(data, SIG_EDGE_TOP, NULL);
1936 }
1937
1938 static void
1939 _edge_bottom(void        *data,
1940              Evas_Object *scr __UNUSED__,
1941              void        *event_info __UNUSED__)
1942 {
1943    evas_object_smart_callback_call(data, SIG_SCROLL_EDGE_BOTTOM, NULL);
1944    evas_object_smart_callback_call(data, SIG_EDGE_BOTTOM, NULL);
1945 }
1946
1947 static void
1948 _scr_scroll(void            *data,
1949             Evas_Object *obj __UNUSED__,
1950             void *event_info __UNUSED__)
1951 {
1952    evas_object_smart_callback_call(data, SIG_SCROLL, NULL);
1953 }
1954
1955 static int
1956 _elm_gengrid_item_compare_data(const void *data, const void *data1)
1957 {
1958    const Elm_Gengrid_Item *item = data;
1959    const Elm_Gengrid_Item *item1 = data1;
1960
1961    return _elm_gengrid_item_compare_data_cb(item->base.data, item1->base.data);
1962 }
1963
1964 static int
1965 _elm_gengrid_item_compare(const void *data, const void *data1)
1966 {
1967    Elm_Gengrid_Item *item, *item1;
1968    item = ELM_GENGRID_ITEM_FROM_INLIST(data);
1969    item1 = ELM_GENGRID_ITEM_FROM_INLIST(data1);
1970    return _elm_gengrid_item_compare_cb(item, item1);
1971 }
1972
1973 EAPI Evas_Object *
1974 elm_gengrid_add(Evas_Object *parent)
1975 {
1976    Evas_Object *obj;
1977    Evas *e;
1978    Widget_Data *wd;
1979    static Evas_Smart *smart = NULL;
1980    Eina_Bool bounce = _elm_config->thumbscroll_bounce_enable;
1981
1982    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1983
1984    ELM_SET_WIDTYPE(widtype, "gengrid");
1985    elm_widget_type_set(obj, "gengrid");
1986    elm_widget_sub_object_add(parent, obj);
1987    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1988    elm_widget_data_set(obj, wd);
1989    elm_widget_del_hook_set(obj, _del_hook);
1990    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1991    elm_widget_theme_hook_set(obj, _theme_hook);
1992    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
1993    elm_widget_can_focus_set(obj, EINA_TRUE);
1994    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
1995    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
1996    elm_widget_event_hook_set(obj, _event_hook);
1997
1998    wd->scr = elm_smart_scroller_add(e);
1999    elm_smart_scroller_widget_set(wd->scr, obj);
2000    elm_smart_scroller_object_theme_set(obj, wd->scr, "gengrid", "base",
2001                                        "default");
2002    elm_widget_resize_object_set(obj, wd->scr);
2003
2004    evas_object_smart_callback_add(wd->scr, "animate,start", _scr_anim_start, obj);
2005    evas_object_smart_callback_add(wd->scr, "animate,stop", _scr_anim_stop, obj);
2006    evas_object_smart_callback_add(wd->scr, "drag,start", _scr_drag_start, obj);
2007    evas_object_smart_callback_add(wd->scr, "drag,stop", _scr_drag_stop, obj);
2008    evas_object_smart_callback_add(wd->scr, "edge,left", _edge_left, obj);
2009    evas_object_smart_callback_add(wd->scr, "edge,right", _edge_right, obj);
2010    evas_object_smart_callback_add(wd->scr, "edge,top", _edge_top, obj);
2011    evas_object_smart_callback_add(wd->scr, "edge,bottom", _edge_bottom,
2012                                   obj);
2013    evas_object_smart_callback_add(wd->scr, "scroll", _scr_scroll, obj);
2014
2015    elm_smart_scroller_bounce_allow_set(wd->scr, bounce, bounce);
2016
2017    wd->self = obj;
2018    wd->align_x = 0.5;
2019    wd->align_y = 0.5;
2020    wd->h_bounce = bounce;
2021    wd->v_bounce = bounce;
2022    wd->no_select = EINA_FALSE;
2023
2024    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
2025    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
2026    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
2027    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
2028
2029    evas_object_smart_callbacks_descriptions_set(obj, _signals);
2030
2031    if (!smart)
2032      {
2033         static Evas_Smart_Class sc;
2034
2035         evas_object_smart_clipped_smart_set(&_pan_sc);
2036         sc = _pan_sc;
2037         sc.name = "elm_gengrid_pan";
2038         sc.version = EVAS_SMART_CLASS_VERSION;
2039         sc.add = _pan_add;
2040         sc.del = _pan_del;
2041         sc.resize = _pan_resize;
2042         sc.move = _pan_move;
2043         sc.calculate = _pan_calculate;
2044         smart = evas_smart_class_new(&sc);
2045      }
2046    if (smart)
2047      {
2048         wd->pan_smart = evas_object_smart_add(e, smart);
2049         wd->pan = evas_object_smart_data_get(wd->pan_smart);
2050         wd->pan->wd = wd;
2051      }
2052
2053    elm_smart_scroller_extern_pan_set(wd->scr, wd->pan_smart,
2054                                      _pan_set, _pan_get, _pan_max_get,
2055                                      _pan_min_get, _pan_child_size_get);
2056
2057    _mirrored_set(obj, elm_widget_mirrored_get(obj));
2058    return obj;
2059 }
2060
2061 EAPI void
2062 elm_gengrid_item_size_set(Evas_Object *obj,
2063                           Evas_Coord   w,
2064                           Evas_Coord   h)
2065 {
2066    ELM_CHECK_WIDTYPE(obj, widtype);
2067    Widget_Data *wd = elm_widget_data_get(obj);
2068    if (!wd) return;
2069    if ((wd->item_width == w) && (wd->item_height == h)) return;
2070    wd->item_width = w;
2071    wd->item_height = h;
2072    if (wd->calc_job) ecore_job_del(wd->calc_job);
2073    wd->calc_job = ecore_job_add(_calc_job, wd);
2074 }
2075
2076 EAPI void
2077 elm_gengrid_item_size_get(const Evas_Object *obj,
2078                           Evas_Coord        *w,
2079                           Evas_Coord        *h)
2080 {
2081    ELM_CHECK_WIDTYPE(obj, widtype);
2082    Widget_Data *wd = elm_widget_data_get(obj);
2083    if (!wd) return;
2084    if (w) *w = wd->item_width;
2085    if (h) *h = wd->item_height;
2086 }
2087
2088 EAPI void
2089 elm_gengrid_group_item_size_set(Evas_Object *obj,
2090                           Evas_Coord   w,
2091                           Evas_Coord   h)
2092 {
2093    ELM_CHECK_WIDTYPE(obj, widtype);
2094    Widget_Data *wd = elm_widget_data_get(obj);
2095    if (!wd) return;
2096    if ((wd->group_item_width == w) && (wd->group_item_height == h)) return;
2097    wd->group_item_width = w;
2098    wd->group_item_height = h;
2099    if (wd->calc_job) ecore_job_del(wd->calc_job);
2100    wd->calc_job = ecore_job_add(_calc_job, wd);
2101 }
2102
2103 EAPI void
2104 elm_gengrid_group_item_size_get(const Evas_Object *obj,
2105                           Evas_Coord        *w,
2106                           Evas_Coord        *h)
2107 {
2108    ELM_CHECK_WIDTYPE(obj, widtype);
2109    Widget_Data *wd = elm_widget_data_get(obj);
2110    if (!wd) return;
2111    if (w) *w = wd->group_item_width;
2112    if (h) *h = wd->group_item_height;
2113 }
2114
2115 EAPI void
2116 elm_gengrid_align_set(Evas_Object *obj,
2117                       double       align_x,
2118                       double       align_y)
2119 {
2120    ELM_CHECK_WIDTYPE(obj, widtype);
2121
2122    Widget_Data *wd = elm_widget_data_get(obj);
2123    double old_h = wd->align_x, old_y = wd->align_y;
2124
2125    if (align_x > 1.0)
2126      align_x = 1.0;
2127    else if (align_x < 0.0)
2128      align_x = 0.0;
2129    wd->align_x = align_x;
2130
2131    if (align_y > 1.0)
2132      align_y = 1.0;
2133    else if (align_y < 0.0)
2134      align_y = 0.0;
2135    wd->align_y = align_y;
2136
2137    if ((old_h != wd->align_x) || (old_y != wd->align_y))
2138      evas_object_smart_calculate(wd->pan_smart);
2139 }
2140
2141 EAPI void
2142 elm_gengrid_align_get(const Evas_Object *obj,
2143                       double            *align_x,
2144                       double            *align_y)
2145 {
2146    ELM_CHECK_WIDTYPE(obj, widtype);
2147    Widget_Data *wd = elm_widget_data_get(obj);
2148    if (align_x) *align_x = wd->align_x;
2149    if (align_y) *align_y = wd->align_y;
2150 }
2151
2152 EAPI Elm_Gengrid_Item *
2153 elm_gengrid_item_append(Evas_Object                  *obj,
2154                         const Elm_Gengrid_Item_Class *gic,
2155                         const void                   *data,
2156                         Evas_Smart_Cb                 func,
2157                         const void                   *func_data)
2158 {
2159    Elm_Gengrid_Item *item;
2160    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2161    Widget_Data *wd = elm_widget_data_get(obj);
2162    if (!wd) return NULL;
2163
2164    item = _item_create(wd, gic, data, func, func_data);
2165    if (!item) return NULL;
2166    wd->items = eina_inlist_append(wd->items, EINA_INLIST_GET(item));
2167
2168    if (item->is_group)
2169      wd->group_items = eina_list_prepend(wd->group_items, item);
2170
2171    if (wd->calc_job) ecore_job_del(wd->calc_job);
2172    wd->calc_job = ecore_job_add(_calc_job, wd);
2173
2174    return item;
2175 }
2176
2177 EAPI Elm_Gengrid_Item *
2178 elm_gengrid_item_prepend(Evas_Object                  *obj,
2179                          const Elm_Gengrid_Item_Class *gic,
2180                          const void                   *data,
2181                          Evas_Smart_Cb                 func,
2182                          const void                   *func_data)
2183 {
2184    Elm_Gengrid_Item *item;
2185    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2186    Widget_Data *wd = elm_widget_data_get(obj);
2187    if (!wd) return NULL;
2188
2189    item = _item_create(wd, gic, data, func, func_data);
2190    if (!item) return NULL;
2191    wd->items = eina_inlist_prepend(wd->items, EINA_INLIST_GET(item));
2192    if (item->is_group)
2193      wd->group_items = eina_list_append(wd->group_items, item);
2194
2195    if (wd->calc_job) ecore_job_del(wd->calc_job);
2196    wd->calc_job = ecore_job_add(_calc_job, wd);
2197
2198    return item;
2199 }
2200
2201 EAPI Elm_Gengrid_Item *
2202 elm_gengrid_item_insert_before(Evas_Object                  *obj,
2203                                const Elm_Gengrid_Item_Class *gic,
2204                                const void                   *data,
2205                                Elm_Gengrid_Item             *relative,
2206                                Evas_Smart_Cb                 func,
2207                                const void                   *func_data)
2208 {
2209    Elm_Gengrid_Item *item;
2210    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2211    EINA_SAFETY_ON_NULL_RETURN_VAL(relative, NULL);
2212    Widget_Data *wd = elm_widget_data_get(obj);
2213    if (!wd) return NULL;
2214
2215    item = _item_create(wd, gic, data, func, func_data);
2216    if (!item) return NULL;
2217    wd->items = eina_inlist_prepend_relative
2218       (wd->items, EINA_INLIST_GET(item), EINA_INLIST_GET(relative));
2219    if (item->is_group)
2220      wd->group_items = eina_list_append_relative(wd->group_items, item, relative->parent_group_item);
2221
2222    if (wd->calc_job) ecore_job_del(wd->calc_job);
2223    wd->calc_job = ecore_job_add(_calc_job, wd);
2224
2225    return item;
2226 }
2227
2228 EAPI Elm_Gengrid_Item *
2229 elm_gengrid_item_insert_after(Evas_Object                  *obj,
2230                               const Elm_Gengrid_Item_Class *gic,
2231                               const void                   *data,
2232                               Elm_Gengrid_Item             *relative,
2233                               Evas_Smart_Cb                 func,
2234                               const void                   *func_data)
2235 {
2236    Elm_Gengrid_Item *item;
2237    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2238    EINA_SAFETY_ON_NULL_RETURN_VAL(relative, NULL);
2239    Widget_Data *wd = elm_widget_data_get(obj);
2240    if (!wd) return NULL;
2241
2242    item = _item_create(wd, gic, data, func, func_data);
2243    if (!item) return NULL;
2244    wd->items = eina_inlist_append_relative
2245       (wd->items, EINA_INLIST_GET(item), EINA_INLIST_GET(relative));
2246    if (item->is_group)
2247      wd->group_items = eina_list_prepend_relative(wd->group_items, item, relative->parent_group_item);
2248
2249    if (wd->calc_job) ecore_job_del(wd->calc_job);
2250    wd->calc_job = ecore_job_add(_calc_job, wd);
2251
2252    return item;
2253 }
2254
2255 EAPI Elm_Gengrid_Item *
2256 elm_gengrid_item_direct_sorted_insert(Evas_Object                  *obj,
2257                                       const Elm_Gengrid_Item_Class *gic,
2258                                       const void                   *data,
2259                                       Eina_Compare_Cb               comp,
2260                                       Evas_Smart_Cb                 func,
2261                                       const void                   *func_data)
2262 {
2263    Elm_Gengrid_Item *item;
2264    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2265    Widget_Data *wd = elm_widget_data_get(obj);
2266    if (!wd) return NULL;
2267
2268    item = _item_create(wd, gic, data, func, func_data);
2269    if (!item) return NULL;
2270
2271    if (!wd->state)
2272      wd->state = eina_inlist_sorted_state_new();
2273
2274    _elm_gengrid_item_compare_cb = comp;
2275    wd->items = eina_inlist_sorted_state_insert(wd->items, EINA_INLIST_GET(item),
2276                                          _elm_gengrid_item_compare, wd->state);
2277    if (wd->calc_job) ecore_job_del(wd->calc_job);
2278    wd->calc_job = ecore_job_add(_calc_job, wd);
2279
2280    return item;
2281 }
2282
2283 EAPI Elm_Gengrid_Item *
2284 elm_gengrid_item_sorted_insert(Evas_Object                  *obj,
2285                                const Elm_Gengrid_Item_Class *gic,
2286                                const void                   *data,
2287                                Eina_Compare_Cb               comp,
2288                                Evas_Smart_Cb                 func,
2289                                const void                   *func_data)
2290 {
2291    _elm_gengrid_item_compare_data_cb = comp;
2292
2293    return elm_gengrid_item_direct_sorted_insert(obj, gic, data, _elm_gengrid_item_compare_data, func, func_data);
2294 }
2295
2296 EAPI void
2297 elm_gengrid_item_del(Elm_Gengrid_Item *item)
2298 {
2299    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2300    if ((item->relcount > 0) || (item->walking > 0))
2301      {
2302         item->delete_me = EINA_TRUE;
2303         elm_widget_item_pre_notify_del(item);
2304         if (item->selected)
2305           item->wd->selected = eina_list_remove(item->wd->selected, item);
2306         if (item->gic->func.del)
2307           item->gic->func.del((void *)item->base.data, item->wd->self);
2308         return;
2309      }
2310
2311    _item_del(item);
2312 }
2313
2314 EAPI void
2315 elm_gengrid_horizontal_set(Evas_Object *obj,
2316                            Eina_Bool    setting)
2317 {
2318    ELM_CHECK_WIDTYPE(obj, widtype);
2319    Widget_Data *wd = elm_widget_data_get(obj);
2320    if (!wd) return;
2321    if (setting == wd->horizontal) return;
2322    wd->horizontal = setting;
2323
2324    /* Update the items to conform to the new layout */
2325    if (wd->calc_job) ecore_job_del(wd->calc_job);
2326    wd->calc_job = ecore_job_add(_calc_job, wd);
2327 }
2328
2329 EAPI Eina_Bool
2330 elm_gengrid_horizontal_get(const Evas_Object *obj)
2331 {
2332    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2333    Widget_Data *wd = elm_widget_data_get(obj);
2334    if (!wd) return EINA_FALSE;
2335    return wd->horizontal;
2336 }
2337
2338 EAPI void
2339 elm_gengrid_clear(Evas_Object *obj)
2340 {
2341    ELM_CHECK_WIDTYPE(obj, widtype);
2342    Widget_Data *wd = elm_widget_data_get(obj);
2343    if (!wd) return;
2344
2345    if (wd->state)
2346      {
2347         eina_inlist_sorted_state_free(wd->state);
2348         wd->state = NULL;
2349      }
2350    if (wd->calc_job)
2351      {
2352         ecore_job_del(wd->calc_job);
2353         wd->calc_job = NULL;
2354      }
2355
2356    if (wd->walking > 0)
2357      {
2358         Elm_Gengrid_Item *item;
2359         wd->clear_me = 1;
2360         EINA_INLIST_FOREACH(wd->items, item)
2361            item->delete_me = 1;
2362         return;
2363      }
2364    wd->clear_me = 0;
2365    while (wd->items)
2366      {
2367         Elm_Gengrid_Item *item = ELM_GENGRID_ITEM_FROM_INLIST(wd->items);
2368
2369         wd->items = eina_inlist_remove(wd->items, wd->items);
2370         elm_widget_item_pre_notify_del(item);
2371         if (item->realized) _item_unrealize(item);
2372         if (item->gic->func.del)
2373           item->gic->func.del((void *)item->base.data, wd->self);
2374         if (item->long_timer) ecore_timer_del(item->long_timer);
2375         elm_widget_item_del(item);
2376      }
2377
2378    if (wd->selected)
2379      {
2380         eina_list_free(wd->selected);
2381         wd->selected = NULL;
2382      }
2383
2384    wd->pan_x = 0;
2385    wd->pan_y = 0;
2386    wd->minw = 0;
2387    wd->minh = 0;
2388    wd->count = 0;
2389    evas_object_size_hint_min_set(wd->pan_smart, wd->minw, wd->minh);
2390    evas_object_smart_callback_call(wd->pan_smart, "changed", NULL);
2391 }
2392
2393 EAPI const Evas_Object *
2394 elm_gengrid_item_object_get(const Elm_Gengrid_Item *item)
2395 {
2396    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2397    return item->base.view;
2398 }
2399
2400 EAPI void
2401 elm_gengrid_item_update(Elm_Gengrid_Item *item)
2402 {
2403    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2404    if (!item->realized) return;
2405    if (item->want_unrealize) return;
2406    _item_unrealize(item);
2407    _item_realize(item);
2408    _item_place(item, item->x, item->y);
2409 }
2410
2411 EAPI void *
2412 elm_gengrid_item_data_get(const Elm_Gengrid_Item *item)
2413 {
2414    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2415    return elm_widget_item_data_get(item);
2416 }
2417
2418 EAPI void
2419 elm_gengrid_item_data_set(Elm_Gengrid_Item *item,
2420                           const void       *data)
2421 {
2422    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2423    elm_widget_item_data_set(item, data);
2424 }
2425
2426 EAPI const Elm_Gengrid_Item_Class *
2427 elm_gengrid_item_item_class_get(const Elm_Gengrid_Item *item)
2428 {
2429    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2430    if (item->delete_me) return NULL;
2431    return item->gic;
2432 }
2433
2434 EAPI void
2435 elm_gengrid_item_item_class_set(Elm_Gengrid_Item *item,
2436                                 const Elm_Gengrid_Item_Class *gic)
2437 {
2438    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2439    EINA_SAFETY_ON_NULL_RETURN(gic);
2440    if (item->delete_me) return;
2441    item->gic = gic;
2442    elm_gengrid_item_update(item);
2443 }
2444
2445 EAPI void
2446 elm_gengrid_item_pos_get(const Elm_Gengrid_Item *item,
2447                          unsigned int           *x,
2448                          unsigned int           *y)
2449 {
2450    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2451    if (x) *x = item->x;
2452    if (y) *y = item->y;
2453 }
2454
2455 EAPI void
2456 elm_gengrid_multi_select_set(Evas_Object *obj,
2457                              Eina_Bool    multi)
2458 {
2459    ELM_CHECK_WIDTYPE(obj, widtype);
2460    Widget_Data *wd = elm_widget_data_get(obj);
2461    if (!wd) return;
2462    wd->multi = multi;
2463 }
2464
2465 EAPI Eina_Bool
2466 elm_gengrid_multi_select_get(const Evas_Object *obj)
2467 {
2468    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2469    Widget_Data *wd = elm_widget_data_get(obj);
2470    if (!wd) return EINA_FALSE;
2471    return wd->multi;
2472 }
2473
2474 EAPI Elm_Gengrid_Item *
2475 elm_gengrid_selected_item_get(const Evas_Object *obj)
2476 {
2477    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2478    Widget_Data *wd = elm_widget_data_get(obj);
2479    if (!wd) return NULL;
2480    if (wd->selected) return wd->selected->data;
2481    return NULL;
2482 }
2483
2484 EAPI const Eina_List *
2485 elm_gengrid_selected_items_get(const Evas_Object *obj)
2486 {
2487    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2488    Widget_Data *wd = elm_widget_data_get(obj);
2489    if (!wd) return NULL;
2490    return wd->selected;
2491 }
2492
2493 EAPI void
2494 elm_gengrid_item_selected_set(Elm_Gengrid_Item *item,
2495                               Eina_Bool         selected)
2496 {
2497    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2498    Widget_Data *wd = item->wd;
2499    if (!wd) return;
2500    if (item->delete_me) return;
2501    selected = !!selected;
2502    if (item->selected == selected) return;
2503
2504    if (selected)
2505      {
2506         if (!wd->multi)
2507           {
2508              while (wd->selected)
2509                _item_unselect(wd->selected->data);
2510           }
2511         _item_highlight(item);
2512         _item_select(item);
2513      }
2514    else
2515      _item_unselect(item);
2516 }
2517
2518 EAPI Eina_Bool
2519 elm_gengrid_item_selected_get(const Elm_Gengrid_Item *item)
2520 {
2521    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
2522    return item->selected;
2523 }
2524
2525 EAPI void
2526 elm_gengrid_item_disabled_set(Elm_Gengrid_Item *item,
2527                               Eina_Bool         disabled)
2528 {
2529    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2530    if (item->disabled == disabled) return;
2531    if (item->delete_me) return;
2532    item->disabled = !!disabled;
2533    if (item->realized)
2534      {
2535         if (item->disabled)
2536           edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
2537         else
2538           edje_object_signal_emit(item->base.view, "elm,state,enabled", "elm");
2539      }
2540 }
2541
2542 EAPI Eina_Bool
2543 elm_gengrid_item_disabled_get(const Elm_Gengrid_Item *item)
2544 {
2545    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
2546    if (item->delete_me) return EINA_FALSE;
2547    return item->disabled;
2548 }
2549
2550 static Evas_Object *
2551 _elm_gengrid_item_label_create(void        *data,
2552                                Evas_Object *obj __UNUSED__,
2553                                Evas_Object *tooltip,
2554                                void *item   __UNUSED__)
2555 {
2556    Evas_Object *label = elm_label_add(tooltip);
2557    if (!label)
2558      return NULL;
2559    elm_object_style_set(label, "tooltip");
2560    elm_object_text_set(label, data);
2561    return label;
2562 }
2563
2564 static void
2565 _elm_gengrid_item_label_del_cb(void            *data,
2566                                Evas_Object *obj __UNUSED__,
2567                                void *event_info __UNUSED__)
2568 {
2569    eina_stringshare_del(data);
2570 }
2571
2572 EAPI void
2573 elm_gengrid_item_tooltip_text_set(Elm_Gengrid_Item *item,
2574                                   const char       *text)
2575 {
2576    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2577    text = eina_stringshare_add(text);
2578    elm_gengrid_item_tooltip_content_cb_set(item, _elm_gengrid_item_label_create,
2579                                            text,
2580                                            _elm_gengrid_item_label_del_cb);
2581 }
2582
2583 EAPI void
2584 elm_gengrid_item_tooltip_content_cb_set(Elm_Gengrid_Item           *item,
2585                                         Elm_Tooltip_Item_Content_Cb func,
2586                                         const void                 *data,
2587                                         Evas_Smart_Cb               del_cb)
2588 {
2589    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_GOTO(item, error);
2590
2591    if ((item->tooltip.content_cb == func) && (item->tooltip.data == data))
2592      return;
2593
2594    if (item->tooltip.del_cb)
2595      item->tooltip.del_cb((void *)item->tooltip.data,
2596                           item->base.widget, item);
2597    item->tooltip.content_cb = func;
2598    item->tooltip.data = data;
2599    item->tooltip.del_cb = del_cb;
2600    if (item->base.view)
2601      {
2602         elm_widget_item_tooltip_content_cb_set(item,
2603                                                item->tooltip.content_cb,
2604                                                item->tooltip.data, NULL);
2605         elm_widget_item_tooltip_style_set(item, item->tooltip.style);
2606         elm_widget_item_tooltip_size_restrict_disable(item, item->tooltip.free_size);
2607      }
2608
2609    return;
2610
2611 error:
2612    if (del_cb) del_cb((void *)data, NULL, NULL);
2613 }
2614
2615 EAPI void
2616 elm_gengrid_item_tooltip_unset(Elm_Gengrid_Item *item)
2617 {
2618    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2619    if ((item->base.view) && (item->tooltip.content_cb))
2620      elm_widget_item_tooltip_unset(item);
2621
2622    if (item->tooltip.del_cb)
2623      item->tooltip.del_cb((void *)item->tooltip.data, item->base.widget, item);
2624    item->tooltip.del_cb = NULL;
2625    item->tooltip.content_cb = NULL;
2626    item->tooltip.data = NULL;
2627    item->tooltip.free_size = EINA_FALSE;
2628    if (item->tooltip.style)
2629      elm_gengrid_item_tooltip_style_set(item, NULL);
2630 }
2631
2632 EAPI void
2633 elm_gengrid_item_tooltip_style_set(Elm_Gengrid_Item *item,
2634                                    const char       *style)
2635 {
2636    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2637    eina_stringshare_replace(&item->tooltip.style, style);
2638    if (item->base.view) elm_widget_item_tooltip_style_set(item, style);
2639 }
2640
2641 EAPI const char *
2642 elm_gengrid_item_tooltip_style_get(const Elm_Gengrid_Item *item)
2643 {
2644    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2645    return item->tooltip.style;
2646 }
2647
2648 EAPI Eina_Bool
2649 elm_gengrid_item_tooltip_size_restrict_disable(Elm_Gengrid_Item *item, Eina_Bool disable)
2650 {
2651    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
2652    item->tooltip.free_size = disable;
2653    if (item->base.view) return elm_widget_item_tooltip_size_restrict_disable(item, disable);
2654    return EINA_TRUE;
2655 }
2656
2657 EAPI Eina_Bool
2658 elm_gengrid_item_tooltip_size_restrict_disabled_get(const Elm_Gengrid_Item *item)
2659 {
2660    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
2661    return item->tooltip.free_size;
2662 }
2663
2664 EAPI void
2665 elm_gengrid_item_cursor_set(Elm_Gengrid_Item *item,
2666                             const char       *cursor)
2667 {
2668    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2669    eina_stringshare_replace(&item->mouse_cursor, cursor);
2670    if (item->base.view) elm_widget_item_cursor_set(item, cursor);
2671 }
2672
2673 EAPI const char *
2674 elm_gengrid_item_cursor_get(const Elm_Gengrid_Item *item)
2675 {
2676    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2677    return elm_widget_item_cursor_get(item);
2678 }
2679
2680 EAPI void
2681 elm_gengrid_item_cursor_unset(Elm_Gengrid_Item *item)
2682 {
2683    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2684    if (!item->mouse_cursor)
2685      return;
2686
2687    if (item->base.view)
2688      elm_widget_item_cursor_unset(item);
2689
2690    eina_stringshare_del(item->mouse_cursor);
2691    item->mouse_cursor = NULL;
2692 }
2693
2694 EAPI void
2695 elm_gengrid_item_cursor_style_set(Elm_Gengrid_Item *item,
2696                                   const char       *style)
2697 {
2698    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2699    elm_widget_item_cursor_style_set(item, style);
2700 }
2701
2702 EAPI const char *
2703 elm_gengrid_item_cursor_style_get(const Elm_Gengrid_Item *item)
2704 {
2705    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2706    return elm_widget_item_cursor_style_get(item);
2707 }
2708
2709 EAPI void
2710 elm_gengrid_item_cursor_engine_only_set(Elm_Gengrid_Item *item,
2711                                         Eina_Bool         engine_only)
2712 {
2713    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2714    elm_widget_item_cursor_engine_only_set(item, engine_only);
2715 }
2716
2717 EAPI Eina_Bool
2718 elm_gengrid_item_cursor_engine_only_get(const Elm_Gengrid_Item *item)
2719 {
2720    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
2721    return elm_widget_item_cursor_engine_only_get(item);
2722 }
2723
2724 EAPI void
2725 elm_gengrid_reorder_mode_set(Evas_Object *obj,
2726                              Eina_Bool    reorder_mode)
2727 {
2728    ELM_CHECK_WIDTYPE(obj, widtype);
2729    Widget_Data *wd = elm_widget_data_get(obj);
2730    if (!wd) return;
2731    wd->reorder_mode = reorder_mode;
2732 }
2733
2734 EAPI Eina_Bool
2735 elm_gengrid_reorder_mode_get(const Evas_Object *obj)
2736 {
2737    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2738    Widget_Data *wd = elm_widget_data_get(obj);
2739    if (!wd) return EINA_FALSE;
2740    return wd->reorder_mode;
2741 }
2742
2743 EAPI void
2744 elm_gengrid_always_select_mode_set(Evas_Object *obj,
2745                                    Eina_Bool    always_select)
2746 {
2747    ELM_CHECK_WIDTYPE(obj, widtype);
2748    Widget_Data *wd = elm_widget_data_get(obj);
2749    if (!wd) return;
2750    wd->always_select = always_select;
2751 }
2752
2753 EAPI Eina_Bool
2754 elm_gengrid_always_select_mode_get(const Evas_Object *obj)
2755 {
2756    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2757    Widget_Data *wd = elm_widget_data_get(obj);
2758    if (!wd) return EINA_FALSE;
2759    return wd->always_select;
2760 }
2761
2762 EAPI void
2763 elm_gengrid_no_select_mode_set(Evas_Object *obj,
2764                                Eina_Bool    no_select)
2765 {
2766    ELM_CHECK_WIDTYPE(obj, widtype);
2767    Widget_Data *wd = elm_widget_data_get(obj);
2768    if (!wd) return;
2769    wd->no_select = no_select;
2770 }
2771
2772 EAPI Eina_Bool
2773 elm_gengrid_no_select_mode_get(const Evas_Object *obj)
2774 {
2775    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
2776    Widget_Data *wd = elm_widget_data_get(obj);
2777    if (!wd) return EINA_FALSE;
2778    return wd->no_select;
2779 }
2780
2781 EAPI void
2782 elm_gengrid_bounce_set(Evas_Object *obj,
2783                        Eina_Bool    h_bounce,
2784                        Eina_Bool    v_bounce)
2785 {
2786    ELM_CHECK_WIDTYPE(obj, widtype);
2787    Widget_Data *wd = elm_widget_data_get(obj);
2788    if (!wd) return;
2789    elm_smart_scroller_bounce_allow_set(wd->scr, h_bounce, v_bounce);
2790    wd->h_bounce = h_bounce;
2791    wd->v_bounce = v_bounce;
2792 }
2793
2794 EAPI void
2795 elm_gengrid_bounce_get(const Evas_Object *obj,
2796                        Eina_Bool         *h_bounce,
2797                        Eina_Bool         *v_bounce)
2798 {
2799    ELM_CHECK_WIDTYPE(obj, widtype);
2800    Widget_Data *wd = elm_widget_data_get(obj);
2801    if (!wd) return;
2802    if (h_bounce) *h_bounce = wd->h_bounce;
2803    if (v_bounce) *v_bounce = wd->v_bounce;
2804 }
2805
2806 EAPI void
2807 elm_gengrid_page_relative_set(Evas_Object *obj,
2808                               double       h_pagerel,
2809                               double       v_pagerel)
2810 {
2811    Evas_Coord pagesize_h;
2812    Evas_Coord pagesize_v;
2813
2814    ELM_CHECK_WIDTYPE(obj, widtype);
2815    Widget_Data *wd = elm_widget_data_get(obj);
2816    if (!wd) return;
2817
2818    elm_smart_scroller_paging_get(wd->scr, NULL, NULL, &pagesize_h, &pagesize_v);
2819    elm_smart_scroller_paging_set(wd->scr, h_pagerel, v_pagerel, pagesize_h,
2820                                  pagesize_v);
2821 }
2822
2823 EAPI void
2824 elm_gengrid_page_relative_get(const Evas_Object *obj, double *h_pagerel, double *v_pagerel)
2825 {
2826    ELM_CHECK_WIDTYPE(obj, widtype);
2827    Widget_Data *wd = elm_widget_data_get(obj);
2828    if (!wd) return;
2829
2830    elm_smart_scroller_paging_get(wd->scr, h_pagerel, v_pagerel, NULL, NULL);
2831 }
2832
2833 EAPI void
2834 elm_gengrid_page_size_set(Evas_Object *obj,
2835                           Evas_Coord   h_pagesize,
2836                           Evas_Coord   v_pagesize)
2837 {
2838    double pagerel_h;
2839    double pagerel_v;
2840
2841    ELM_CHECK_WIDTYPE(obj, widtype);
2842    Widget_Data *wd = elm_widget_data_get(obj);
2843    if (!wd) return;
2844    elm_smart_scroller_paging_get(wd->scr, &pagerel_h, &pagerel_v, NULL, NULL);
2845    elm_smart_scroller_paging_set(wd->scr, pagerel_h, pagerel_v, h_pagesize,
2846                                  v_pagesize);
2847 }
2848
2849 EAPI void
2850 elm_gengrid_current_page_get(const Evas_Object *obj, int *h_pagenumber, int *v_pagenumber)
2851 {
2852    ELM_CHECK_WIDTYPE(obj, widtype);
2853    Widget_Data *wd = elm_widget_data_get(obj);
2854    if (!wd) return;
2855    if (wd->scr)
2856      elm_smart_scroller_current_page_get(wd->scr, h_pagenumber, v_pagenumber);
2857 }
2858
2859 EAPI void
2860 elm_gengrid_last_page_get(const Evas_Object *obj, int *h_pagenumber, int *v_pagenumber)
2861 {
2862    ELM_CHECK_WIDTYPE(obj, widtype);
2863    Widget_Data *wd = elm_widget_data_get(obj);
2864    if (!wd) return;
2865    if (wd->scr)
2866      elm_smart_scroller_last_page_get(wd->scr, h_pagenumber, v_pagenumber);
2867 }
2868
2869 EAPI void
2870 elm_gengrid_page_show(const Evas_Object *obj, int h_pagenumber, int v_pagenumber)
2871 {
2872    ELM_CHECK_WIDTYPE(obj, widtype);
2873    Widget_Data *wd = elm_widget_data_get(obj);
2874    if (!wd) return;
2875    if (wd->scr)
2876      elm_smart_scroller_page_show(wd->scr, h_pagenumber, v_pagenumber);
2877 }
2878
2879 EAPI void
2880 elm_gengrid_page_bring_in(const Evas_Object *obj, int h_pagenumber, int v_pagenumber)
2881 {
2882    ELM_CHECK_WIDTYPE(obj, widtype);
2883    Widget_Data *wd = elm_widget_data_get(obj);
2884    if (!wd) return;
2885    if (wd->scr)
2886      elm_smart_scroller_page_bring_in(wd->scr, h_pagenumber, v_pagenumber);
2887 }
2888
2889 EAPI Elm_Gengrid_Item *
2890 elm_gengrid_first_item_get(const Evas_Object *obj)
2891 {
2892    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2893    Widget_Data *wd = elm_widget_data_get(obj);
2894    if (!wd) return NULL;
2895    if (!wd->items) return NULL;
2896    Elm_Gengrid_Item *item = ELM_GENGRID_ITEM_FROM_INLIST(wd->items);
2897    while ((item) && (item->delete_me))
2898      item = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(item)->next);
2899    return item;
2900 }
2901
2902 EAPI Elm_Gengrid_Item *
2903 elm_gengrid_last_item_get(const Evas_Object *obj)
2904 {
2905    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
2906    Widget_Data *wd = elm_widget_data_get(obj);
2907    if (!wd) return NULL;
2908    if (!wd->items) return NULL;
2909    Elm_Gengrid_Item *item = ELM_GENGRID_ITEM_FROM_INLIST(wd->items->last);
2910    while ((item) && (item->delete_me))
2911      item = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(item)->prev);
2912    return item;
2913 }
2914
2915 EAPI Elm_Gengrid_Item *
2916 elm_gengrid_item_next_get(const Elm_Gengrid_Item *item)
2917 {
2918    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2919    while (item)
2920      {
2921         item = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(item)->next);
2922         if ((item) && (!item->delete_me)) break;
2923      }
2924    return (Elm_Gengrid_Item *)item;
2925 }
2926
2927 EAPI Elm_Gengrid_Item *
2928 elm_gengrid_item_prev_get(const Elm_Gengrid_Item *item)
2929 {
2930    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2931    while (item)
2932      {
2933         item = ELM_GENGRID_ITEM_FROM_INLIST(EINA_INLIST_GET(item)->prev);
2934         if ((item) && (!item->delete_me)) break;
2935      }
2936    return (Elm_Gengrid_Item *)item;
2937 }
2938
2939 EAPI Evas_Object *
2940 elm_gengrid_item_gengrid_get(const Elm_Gengrid_Item *item)
2941 {
2942    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
2943    return item->base.widget;
2944 }
2945
2946 EAPI void
2947 elm_gengrid_item_show(Elm_Gengrid_Item *item)
2948 {
2949    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2950    Widget_Data *wd = elm_widget_data_get(item->wd->self);
2951    Evas_Coord minx = 0, miny = 0;
2952
2953    if (!wd) return;
2954    if ((!item) || (item->delete_me)) return;
2955    _pan_min_get(wd->pan_smart, &minx, &miny);
2956
2957    if (wd->horizontal)
2958      elm_smart_scroller_region_bring_in(item->wd->scr,
2959                                         ((item->x - item->prev_group) * wd->item_width) + (item->prev_group * item->wd->group_item_width) + minx,
2960                                         item->y * wd->item_height + miny,
2961                                         item->wd->item_width,
2962                                         item->wd->item_height);
2963    else
2964      elm_smart_scroller_region_bring_in(item->wd->scr,
2965                                         item->x * wd->item_width + minx,
2966                                         ((item->y - item->prev_group)* wd->item_height) + (item->prev_group * item->wd->group_item_height) + miny,
2967                                         item->wd->item_width,
2968                                         item->wd->item_height);
2969 }
2970
2971 EAPI void
2972 elm_gengrid_item_bring_in(Elm_Gengrid_Item *item)
2973 {
2974    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
2975    if (item->delete_me) return;
2976
2977    Evas_Coord minx = 0, miny = 0;
2978    Widget_Data *wd = elm_widget_data_get(item->wd->self);
2979    if (!wd) return;
2980    _pan_min_get(wd->pan_smart, &minx, &miny);
2981
2982    if (wd->horizontal)
2983      elm_smart_scroller_region_bring_in(item->wd->scr,
2984                                         ((item->x - item->prev_group) * wd->item_width) + (item->prev_group * item->wd->group_item_width) + minx,
2985                                         item->y * wd->item_height + miny,
2986                                         item->wd->item_width,
2987                                         item->wd->item_height);
2988    else
2989      elm_smart_scroller_region_bring_in(item->wd->scr,
2990                                         item->x * wd->item_width + minx,
2991                                         ((item->y - item->prev_group)* wd->item_height) + (item->prev_group * item->wd->group_item_height) + miny,
2992                                         item->wd->item_width,
2993                                         item->wd->item_height);
2994 }