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