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