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