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