Merge "[Password]: New design based changes, a new style removed password mode contro...
[framework/uifw/elementary.git] / src / lib / elc_ctxpopup.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Ctxpopup
6  *
7  * A ctxpopup is a widget that, when shown, pops up a list of items.
8  * It automatically chooses an area inside its parent object's view
9  * (set via elm_ctxpopup_add() and elm_ctxpopup_hover_parent_set()) to
10  * optimally fit into it. In the default theme, it will also point an
11  * arrow to the cursor position at the time one shows it. Ctxpopup
12  * items have a label and/or an icon. It is intended for a small
13  * number of items (hence the use of list, not genlist).
14  *
15  * Signals that you can add callbacks for are:
16  *
17  * "dismissed" - the ctxpopup was dismissed
18  */
19
20 typedef struct _Widget_Data Widget_Data;
21
22 struct _Elm_Ctxpopup_Item
23 {
24    Elm_Widget_Item base;
25    const char *label;
26    Evas_Object *icon;
27    Evas_Smart_Cb func;
28    Eina_Bool disabled:1;
29 };
30
31 struct _Widget_Data
32 {
33    Evas_Object *parent;
34    Evas_Object *base;
35    Evas_Object *content;
36    Evas_Object *box;
37    Evas_Object *arrow;
38    Evas_Object *scr;
39    Evas_Object *bg;
40    Evas_Object *hover_parent;
41    Eina_List *items;
42    Elm_Ctxpopup_Direction dir;
43    Elm_Ctxpopup_Direction dir_priority[4];
44    Evas_Coord max_sc_w, max_sc_h;
45    Eina_Bool horizontal:1;
46    Eina_Bool visible:1;
47    Eina_Bool finished:1;
48 };
49
50 static const char *widtype = NULL;
51
52 static void _del_hook(Evas_Object *obj);
53 static void _del_pre_hook(Evas_Object *obj);
54 static void _theme_hook(Evas_Object *obj);
55 static void _sizing_eval(Evas_Object *obj);
56 static void _hover_parent_resize(void *data, Evas *e __UNUSED__,
57                                  Evas_Object *obj __UNUSED__,
58                                  void *event_info __UNUSED__);
59 static void _hover_parent_move(void *data, Evas *e __UNUSED__,
60                                Evas_Object *obj __UNUSED__,
61                                void *event_info __UNUSED__);
62 static void _hover_parent_del(void *data, Evas *e __UNUSED__,
63                               Evas_Object *obj __UNUSED__,
64                               void *event_info __UNUSED__);
65 static void _hover_parent_callbacks_del(Evas_Object *obj);
66 static void _bg_clicked_cb(void *data, Evas_Object *obj __UNUSED__,
67                            const char *emission __UNUSED__,
68                            const char *source __UNUSED__);
69 static void _parent_resize(void *data, Evas *e, Evas_Object *obj,
70                            void *event_info __UNUSED__);
71 static void _ctxpopup_show(void *data __UNUSED__, Evas *e __UNUSED__,
72                            Evas_Object *obj, void *event_info __UNUSED__);
73 static void _ctxpopup_hide(void *data __UNUSED__, Evas *e __UNUSED__,
74                            Evas_Object *obj, void *event_info __UNUSED__);
75 static void _ctxpopup_move(void *data __UNUSED__, Evas *e __UNUSED__,
76                            Evas_Object *obj, void *event_info __UNUSED__);
77 static void _scroller_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
78                              void *event_info __UNUSED__);
79 static void _ctxpopup_changed_size_hints(void *data __UNUSED__,
80                                          Evas *e __UNUSED__, Evas_Object *obj,
81                                          void *event_info __UNUSED__);
82 static void _item_new(Elm_Ctxpopup_Item *item, char *group_name);
83 static void _list_new(Evas_Object *obj);
84 static void _item_sizing_eval(Elm_Ctxpopup_Item *item);
85 static void _item_select_cb(void *data, Evas_Object *obj __UNUSED__,
86                             const char *emission __UNUSED__,
87                             const char *source __UNUSED__);
88 static Elm_Ctxpopup_Direction _calc_base_geometry(Evas_Object *obj,
89                                                   Evas_Coord_Rectangle *rect);
90 static void _update_arrow(Evas_Object *obj, Elm_Ctxpopup_Direction dir);
91 static void _shift_base_by_arrow(Evas_Object *arrow,
92                                  Elm_Ctxpopup_Direction dir,
93                                  Evas_Coord_Rectangle *rect);
94 static void _adjust_pos_x(Evas_Coord_Point *pos, Evas_Coord_Point *base_size,
95                           Evas_Coord_Rectangle *hover_area);
96 static void _adjust_pos_y(Evas_Coord_Point *pos, Evas_Coord_Point *base_size,
97                           Evas_Coord_Rectangle *hover_area);
98 static void _scroller_size_reset(Widget_Data *wd);
99 static void _hide(Evas_Object *obj);
100 static void _content_del(void *data, Evas *e, Evas_Object *obj __UNUSED__,
101                          void *event_info __UNUSED__);
102 static void _freeze_on(void *data __UNUSED__, Evas_Object *obj,
103                        void *event_info __UNUSED__);
104 static void _freeze_off(void *data __UNUSED__, Evas_Object *obj,
105                         void *event_info __UNUSED__);
106 static void _hold_on(void *data __UNUSED__, Evas_Object *obj,
107                      void *event_info __UNUSED__);
108 static void _hold_off(void *data __UNUSED__, Evas_Object *obj,
109                       void *event_info __UNUSED__);
110 static void _item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon);
111 static void _item_label_set(Elm_Ctxpopup_Item *item, const char *label);
112 static void _remove_items(Widget_Data * wd);
113
114 static const char SIG_DISMISSED[] = "dismissed";
115
116 static const Evas_Smart_Cb_Description _signals[] = {
117        {SIG_DISMISSED, ""},
118        {NULL, NULL}
119 };
120
121 #define ELM_CTXPOPUP_ITEM_CHECK_RETURN(it, ...)                        \
122   ELM_WIDGET_ITEM_CHECK_OR_RETURN((Elm_Widget_Item *)it, __VA_ARGS__); \
123   ELM_CHECK_WIDTYPE(item->base.widget, widtype) __VA_ARGS__;
124
125 static void
126 _freeze_on(void *data __UNUSED__, Evas_Object *obj,
127            void *event_info __UNUSED__)
128 {
129    Widget_Data *wd = elm_widget_data_get(obj);
130
131    if (!wd) return;
132    if (!wd->scr) return;
133
134    elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_FALSE);
135 }
136
137 static void
138 _freeze_off(void *data __UNUSED__, Evas_Object *obj,
139             void *event_info __UNUSED__)
140 {
141    Widget_Data *wd = elm_widget_data_get(obj);
142
143    if (!wd)
144       return;
145    if (!wd->scr) return;
146
147    if (wd->horizontal)
148       elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
149    else
150       elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
151 }
152
153 static void
154 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
155 {
156    Widget_Data *wd = elm_widget_data_get(obj);
157
158    if (!wd) return;
159    if (wd->scr) return;
160
161    elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_FALSE);
162 }
163
164 static void
165 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
166 {
167    Widget_Data *wd = elm_widget_data_get(obj);
168
169    if (!wd) return;
170    if (wd->scr) return;
171
172    if (wd->horizontal)
173       elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
174    else
175       elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
176 }
177
178 static void
179 _scroller_size_reset(Widget_Data *wd)
180 {
181    wd->finished = EINA_FALSE;
182    wd->max_sc_h = -1;
183    wd->max_sc_w = -1;
184 }
185
186 static void
187 _hover_parent_callbacks_del(Evas_Object *obj)
188 {
189    Widget_Data *wd = elm_widget_data_get(obj);
190
191    if ((!wd) || (!wd->hover_parent))
192       return;
193
194    evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
195                                        _hover_parent_del, obj);
196    evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_MOVE,
197                                        _hover_parent_move, obj);
198    evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_RESIZE,
199                                        _hover_parent_resize, obj);
200 }
201
202 static void
203 _hover_parent_resize(void *data, Evas *e __UNUSED__,
204                      Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
205 {
206    Widget_Data *wd = elm_widget_data_get(data);
207
208    if (!wd) return;
209
210    if (wd->visible)
211      {
212         _scroller_size_reset(wd);
213         _sizing_eval(data);
214      }
215 }
216
217 static void
218 _hover_parent_move(void *data, Evas *e __UNUSED__,
219                    Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
220 {
221    Widget_Data *wd = elm_widget_data_get(data);
222
223    if (!wd) return;
224
225    if (wd->visible)
226      {
227         _scroller_size_reset(wd);
228         _sizing_eval(obj);
229      }
230 }
231
232 static void
233 _hover_parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
234                   void *event_info __UNUSED__)
235 {
236    Widget_Data *wd = elm_widget_data_get(data);
237
238    if (!wd) return;
239
240    wd->hover_parent = NULL;
241 }
242
243 static void
244 _item_sizing_eval(Elm_Ctxpopup_Item *item)
245 {
246    Evas_Coord min_w = -1, min_h = -1, max_w = -1, max_h = -1;
247
248    if (!item) return;
249
250    edje_object_size_min_restricted_calc(item->base.view, &min_w, &min_h, min_w,
251                                         min_h);
252    evas_object_size_hint_min_set(item->base.view, min_w, min_h);
253    evas_object_size_hint_max_set(item->base.view, max_w, max_h);
254 }
255
256 static void
257 _adjust_pos_x(Evas_Coord_Point *pos, Evas_Coord_Point *base_size,
258               Evas_Coord_Rectangle *hover_area)
259 {
260    pos->x -= (base_size->x / 2);
261
262    if (pos->x < hover_area->x)
263       pos->x = hover_area->x;
264    else if ((pos->x + base_size->x) > (hover_area->x + hover_area->w))
265       pos->x = (hover_area->x + hover_area->w) - base_size->x;
266
267    if (base_size->x > hover_area->w)
268       base_size->x -= (base_size->x - hover_area->w);
269
270    if (pos->x < hover_area->x)
271       pos->x = hover_area->x;
272 }
273
274 static void
275 _adjust_pos_y(Evas_Coord_Point *pos, Evas_Coord_Point *base_size,
276               Evas_Coord_Rectangle *hover_area)
277 {
278    pos->y -= (base_size->y / 2);
279
280    if (pos->y < hover_area->y)
281       pos->y = hover_area->y;
282    else if ((pos->y + base_size->y) > (hover_area->y + hover_area->h))
283       pos->y = hover_area->y + hover_area->h - base_size->y;
284
285    if (base_size->y > hover_area->h)
286       base_size->y -= (base_size->y - hover_area->h);
287
288    if (pos->y < hover_area->y)
289       pos->y = hover_area->y;
290 }
291
292 static void
293 _ctxpopup_changed_size_hints(void *data __UNUSED__, Evas *e __UNUSED__,
294                              Evas_Object *obj, void *event_info __UNUSED__)
295 {
296    Widget_Data *wd;
297
298    wd = elm_widget_data_get(obj);
299    if (!wd) return;
300
301    if (wd->visible)
302       _sizing_eval(obj);
303 }
304
305 static Elm_Ctxpopup_Direction
306 _calc_base_geometry(Evas_Object *obj, Evas_Coord_Rectangle *rect)
307 {
308    Widget_Data *wd;
309    Evas_Coord_Point pos = {0, 0};
310    Evas_Coord_Point base_size;
311    Evas_Coord_Point max_size;
312    Evas_Coord_Point min_size;
313    Evas_Coord_Rectangle hover_area;
314    Evas_Coord_Rectangle parent_size;
315    Evas_Coord_Point arrow_size;
316    Elm_Ctxpopup_Direction arrow = ELM_CTXPOPUP_DIRECTION_DOWN;
317    Evas_Coord_Point temp;
318    int idx;
319
320    wd = elm_widget_data_get(obj);
321
322    if ((!wd) || (!rect))
323       return ELM_CTXPOPUP_DIRECTION_DOWN;
324
325    edje_object_part_geometry_get(wd->arrow, "ctxpopup_arrow", NULL, NULL,
326                                  &arrow_size.x, &arrow_size.y);
327    evas_object_resize(wd->arrow, arrow_size.x, arrow_size.y);
328
329    //Initialize Area Rectangle.
330    if (wd->hover_parent)
331       evas_object_geometry_get(wd->hover_parent, &hover_area.x, &hover_area.y,
332                                &hover_area.w, &hover_area.h);
333    else
334      {
335         evas_object_geometry_get(wd->parent, &parent_size.x, &parent_size.y,
336                                  &parent_size.w, &parent_size.h);
337         hover_area.x = parent_size.x;
338         hover_area.y = parent_size.y;
339         hover_area.w = parent_size.w;
340         hover_area.h = parent_size.h;
341      }
342
343    evas_object_geometry_get(obj, &pos.x, &pos.y, NULL, NULL);
344
345    //recalc the edje
346    edje_object_size_min_calc(wd->base, &base_size.x, &base_size.y);
347    evas_object_smart_calculate(wd->base);
348
349    //Limit to Max Size
350    evas_object_size_hint_max_get(obj, &max_size.x, &max_size.y);
351
352    if ((max_size.y > 0) && (base_size.y > max_size.y))
353       base_size.y = max_size.y;
354
355    if ((max_size.x > 0) && (base_size.x > max_size.x))
356       base_size.x = max_size.x;
357
358    //Limit to Min Size
359    evas_object_size_hint_min_get(obj, &min_size.x, &min_size.y);
360
361    if ((min_size.y > 0) && (base_size.y < min_size.y))
362       base_size.y = min_size.y;
363
364    if ((min_size.x > 0) && (base_size.x < min_size.x))
365       base_size.x = min_size.x;
366
367    //Check the Which direction is available.
368    //If find a avaialble direction, it adjusts position and size.
369    for (idx = 0; idx < 4; idx++)
370      {
371         switch (wd->dir_priority[idx])
372           {
373            case ELM_CTXPOPUP_DIRECTION_UP:
374               temp.y = (pos.y - base_size.y);
375               if ((temp.y - arrow_size.y) < hover_area.y)
376                  continue;
377               _adjust_pos_x(&pos, &base_size, &hover_area);
378               pos.y -= base_size.y;
379               arrow = ELM_CTXPOPUP_DIRECTION_DOWN;
380               break;
381            case ELM_CTXPOPUP_DIRECTION_LEFT:
382               temp.x = (pos.x - base_size.x);
383               if ((temp.x - arrow_size.x) < hover_area.x)
384                  continue;
385               _adjust_pos_y(&pos, &base_size, &hover_area);
386               pos.x -= base_size.x;
387               arrow = ELM_CTXPOPUP_DIRECTION_RIGHT;
388               break;
389            case ELM_CTXPOPUP_DIRECTION_RIGHT:
390               temp.x = (pos.x + base_size.x);
391               if ((temp.x + arrow_size.x) >
392                   (hover_area.x + hover_area.w))
393                  continue;
394               _adjust_pos_y(&pos, &base_size, &hover_area);
395               arrow = ELM_CTXPOPUP_DIRECTION_LEFT;
396               break;
397            case ELM_CTXPOPUP_DIRECTION_DOWN:
398               temp.y = (pos.y + base_size.y);
399               if ((temp.y + arrow_size.y) >
400                   (hover_area.y + hover_area.h))
401                  continue;
402               _adjust_pos_x(&pos, &base_size, &hover_area);
403               arrow = ELM_CTXPOPUP_DIRECTION_UP;
404               break;
405            default:
406               break;
407           }
408         break;
409      }
410
411    //In this case, all directions are invalid because of lack of space.
412    if (idx == 4)
413      {
414         //TODO 1: Find the largest space direction.
415         Evas_Coord length[2];
416
417         length[0] = pos.y - hover_area.y;
418         length[1] = (hover_area.y + hover_area.h) - pos.y;
419
420         if (length[0] > length[1])
421            idx = ELM_CTXPOPUP_DIRECTION_UP;
422         else
423            idx = ELM_CTXPOPUP_DIRECTION_DOWN;
424
425         //TODO 2: determine x , y
426         switch (idx)
427           {
428            case ELM_CTXPOPUP_DIRECTION_UP:
429               _adjust_pos_x(&pos, &base_size, &hover_area);
430               pos.y -= base_size.y;
431               arrow = ELM_CTXPOPUP_DIRECTION_DOWN;
432               if (pos.y < hover_area.y + arrow_size.y)
433                 {
434                    base_size.y -= ((hover_area.y + arrow_size.y) - pos.y);
435                    pos.y = hover_area.y + arrow_size.y;
436                 }
437               break;
438            case ELM_CTXPOPUP_DIRECTION_LEFT:
439               _adjust_pos_y(&pos, &base_size, &hover_area);
440               pos.x -= base_size.x;
441               arrow = ELM_CTXPOPUP_DIRECTION_RIGHT;
442               if (pos.x < hover_area.x + arrow_size.x)
443                 {
444                    base_size.x -= ((hover_area.x + arrow_size.x) - pos.x);
445                    pos.x = hover_area.x + arrow_size.x;
446                 }
447               break;
448            case ELM_CTXPOPUP_DIRECTION_RIGHT:
449               _adjust_pos_y(&pos, &base_size, &hover_area);
450               arrow = ELM_CTXPOPUP_DIRECTION_LEFT;
451               if (pos.x + arrow_size.x + base_size.x >
452                   hover_area.x + hover_area.w)
453                  base_size.x -=
454                     ((pos.x + arrow_size.x + base_size.x) -
455                      (hover_area.x + hover_area.w));
456               break;
457            case ELM_CTXPOPUP_DIRECTION_DOWN:
458               _adjust_pos_x(&pos, &base_size, &hover_area);
459               arrow = ELM_CTXPOPUP_DIRECTION_UP;
460               if (pos.y + arrow_size.y + base_size.y >
461                   hover_area.y + hover_area.h)
462                  base_size.y -=
463                     ((pos.y + arrow_size.y + base_size.y) -
464                      (hover_area.y + hover_area.h));
465               break;
466            default:
467               break;
468           }
469      }
470
471    //Final position and size.
472    rect->x = pos.x;
473    rect->y = pos.y;
474    rect->w = base_size.x;
475    rect->h = base_size.y;
476
477    return arrow;
478 }
479
480 static void
481 _update_arrow(Evas_Object *obj, Elm_Ctxpopup_Direction dir)
482 {
483    Evas_Coord x, y;
484    Evas_Coord_Rectangle arrow_size;
485    Evas_Coord_Rectangle base_size;
486    Widget_Data *wd;
487
488    wd = elm_widget_data_get(obj);
489    if (!wd) return;
490
491    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
492    evas_object_geometry_get(wd->arrow, NULL, NULL, &arrow_size.w,
493                             &arrow_size.h);
494    evas_object_geometry_get(wd->base, &base_size.x, &base_size.y,
495                             &base_size.w, &base_size.h);
496
497    switch (dir)
498      {
499       case ELM_CTXPOPUP_DIRECTION_LEFT:
500          edje_object_signal_emit(wd->arrow, "elm,state,left", "elm");
501
502          // if user does not use dragable part
503          arrow_size.y = (y - (arrow_size.h * 0.5));
504          arrow_size.x = x;
505
506          edje_object_part_swallow(wd->base, "elm.swallow.arrow_left", wd->arrow);
507          if (base_size.h > 0)
508            {
509               if (y < (arrow_size.h * 0.5) + base_size.y) y = 0;
510               else if (y > base_size.y + base_size.h - (arrow_size.h * 0.5)) y = base_size.h - arrow_size.h;
511               else y = y - base_size.y - (arrow_size.h * 0.5);
512               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_left", 1,
513                                               (double) (y) / (double) (base_size.h - arrow_size.h));
514           }
515          break;
516       case ELM_CTXPOPUP_DIRECTION_RIGHT:
517          edje_object_signal_emit(wd->arrow, "elm,state,right", "elm");
518
519          // if user does not use dragable part
520          arrow_size.y = (y - (arrow_size.h * 0.5));
521          arrow_size.x = (x - arrow_size.w);
522
523          edje_object_part_swallow(wd->base, "elm.swallow.arrow_right", wd->arrow);
524          if (base_size.h > 0)
525             {
526               if (y < (arrow_size.h * 0.5) + base_size.y) y = 0;
527               else if (y > base_size.y + base_size.h - (arrow_size.h * 0.5)) y = base_size.h - arrow_size.h;
528               else y = y - base_size.y - (arrow_size.h * 0.5);
529               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_right", 0,
530                                               (double) (y) / (double) (base_size.h - arrow_size.h));
531             }
532          break;
533       case ELM_CTXPOPUP_DIRECTION_UP:
534          edje_object_signal_emit(wd->arrow, "elm,state,top", "elm");
535
536          // if user does not use dragable part
537          arrow_size.x = (x - (arrow_size.w * 0.5));
538          arrow_size.y = y;
539
540          edje_object_part_swallow(wd->base, "elm.swallow.arrow_up", wd->arrow);
541          if (base_size.w > 0)
542            {
543               if (x < (arrow_size.w * 0.5) + base_size.x) x = 0;
544               else if (x > base_size.x + base_size.w - (arrow_size.w * 0.5)) x = base_size.w - arrow_size.w;
545               else x = x - base_size.x - (arrow_size.w * 0.5);
546               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_up",
547                                               (double) (x) / (double) (base_size.w - arrow_size.w), 1);
548            }
549          break;
550       case ELM_CTXPOPUP_DIRECTION_DOWN:
551          edje_object_signal_emit(wd->arrow, "elm,state,bottom", "elm");
552
553          // if user does not use dragable part
554          arrow_size.x = (x - (arrow_size.w * 0.5));
555          arrow_size.y = (y - arrow_size.h);
556
557          edje_object_part_swallow(wd->base, "elm.swallow.arrow_down", wd->arrow);
558          if (base_size.w > 0)
559            {
560               if (x < (arrow_size.w * 0.5) + base_size.x) x = 0;
561               else if (x > base_size.x + base_size.w - (arrow_size.w * 0.5)) x = base_size.w - arrow_size.w;
562               else x = x - base_size.x - (arrow_size.w * 0.5);
563               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_down",
564                                               (double) (x) / (double) (base_size.w - arrow_size.w), 0);
565            }
566          break;
567       default:
568          break;
569      }
570
571    // if user does not use dragable part
572    evas_object_move(wd->arrow, arrow_size.x, arrow_size.y);
573 }
574
575 static void
576 _sizing_eval(Evas_Object *obj)
577 {
578    Widget_Data *wd;
579    Eina_List *elist;
580    Elm_Ctxpopup_Item *item;
581    Evas_Coord_Rectangle rect = { 0, 0, 1, 1 };
582    Evas_Coord_Point box_size = { 0, 0 };
583    Evas_Coord_Point _box_size = { 0, 0 };
584
585    wd = elm_widget_data_get(obj);
586    if ((!wd) || (!wd->parent)) return;
587
588    //Box, Scroller
589    EINA_LIST_FOREACH(wd->items, elist, item)
590      {
591         _item_sizing_eval(item);
592         evas_object_size_hint_min_get(item->base.view, &_box_size.x, &_box_size.y);
593         if (!wd->horizontal)
594           {
595              if (_box_size.x > box_size.x)
596                 box_size.x = _box_size.x;
597              if (_box_size.y != -1)
598                 box_size.y += _box_size.y;
599           }
600         else
601           {
602              if (_box_size.x != -1)
603                 box_size.x += _box_size.x;
604              if (_box_size.y > box_size.y)
605                 box_size.y = _box_size.y;
606           }
607      }
608
609    if (!wd->content)
610      {
611         evas_object_size_hint_min_set(wd->box, box_size.x, box_size.y);
612         evas_object_size_hint_min_set(wd->scr, box_size.x, box_size.y);
613      }
614
615    //Base
616    wd->dir = _calc_base_geometry(obj, &rect);
617    _update_arrow(obj, wd->dir);
618    _shift_base_by_arrow(wd->arrow, wd->dir, &rect);
619
620    //resize scroller according to final size. 
621    if (!wd->content)
622       evas_object_smart_calculate(wd->scr);
623
624    evas_object_move(wd->base, rect.x, rect.y);
625    evas_object_resize(wd->base, rect.w, rect.h);
626 }
627
628 static void
629 _shift_base_by_arrow(Evas_Object *arrow, Elm_Ctxpopup_Direction dir,
630                      Evas_Coord_Rectangle *rect)
631 {
632    Evas_Coord arrow_w, arrow_h;
633
634    evas_object_geometry_get(arrow, NULL, NULL, &arrow_w, &arrow_h);
635
636    switch (dir)
637      {
638       case ELM_CTXPOPUP_DIRECTION_LEFT:
639          rect->x += arrow_w;
640          break;
641       case ELM_CTXPOPUP_DIRECTION_RIGHT:
642          rect->x -= arrow_w;
643          break;
644       case ELM_CTXPOPUP_DIRECTION_UP:
645          rect->y += arrow_h;
646          break;
647       case ELM_CTXPOPUP_DIRECTION_DOWN:
648          rect->y -= arrow_h;
649          break;
650       default:
651          break;
652      }
653 }
654
655 static void
656 _del_pre_hook(Evas_Object *obj)
657 {
658    Widget_Data *wd;
659
660    wd = elm_widget_data_get(obj);
661    if (!wd) return;
662
663    evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_RESIZE,
664                                        _parent_resize, obj);
665
666    _hover_parent_callbacks_del(obj);
667 }
668
669 static void
670 _del_hook(Evas_Object *obj)
671 {
672    Widget_Data *wd;
673
674    wd = elm_widget_data_get(obj);
675    if (!wd) return;
676
677    elm_ctxpopup_clear(obj);
678    evas_object_del(wd->arrow);
679    evas_object_del(wd->base);
680    free(wd);
681 }
682
683 static void
684 _theme_hook(Evas_Object *obj)
685 {
686    Widget_Data *wd;
687    Eina_List *elist;
688    Elm_Ctxpopup_Item *item;
689
690    wd = elm_widget_data_get(obj);
691    if (!wd) return;
692
693    //Items
694    EINA_LIST_FOREACH(wd->items, elist, item)
695      {
696         if (item->label && item->icon)
697            _elm_theme_object_set(obj, item->base.view, "ctxpopup",
698                                  "icon_text_style_item",
699                                  elm_widget_style_get(obj));
700         else if (item->label)
701            _elm_theme_object_set(obj, item->base.view, "ctxpopup", "text_style_item",
702                                  elm_widget_style_get(obj));
703         else if (item->icon)
704            _elm_theme_object_set(obj, item->base.view, "ctxpopup", "icon_style_item",
705                                  elm_widget_style_get(obj));
706         if (item->label)
707            edje_object_part_text_set(item->base.view, "elm.text", item->label);
708
709         if (item->disabled)
710            edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
711
712         edje_object_message_signal_process(item->base.view);
713      }
714
715    _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg",
716                          elm_widget_style_get(obj));
717    _elm_theme_object_set(obj, wd->base, "ctxpopup", "base",
718                          elm_widget_style_get(obj));
719    _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow",
720                          elm_widget_style_get(obj));
721
722    if (wd->scr)
723      {
724         if (!strncmp(elm_object_style_get(obj), "default",
725                      strlen("default")))
726            elm_object_style_set(wd->scr, "ctxpopup");
727         else
728            elm_object_style_set(wd->scr, elm_object_style_get(obj));
729      }
730
731    if (wd->visible)
732      {
733         _scroller_size_reset(wd);
734         _sizing_eval(obj);
735      }
736 }
737
738 static void
739 _bg_clicked_cb(void *data, Evas_Object *obj __UNUSED__,
740                const char *emission __UNUSED__, const char *source __UNUSED__)
741 {
742    evas_object_hide(data);
743 }
744
745 static void
746 _parent_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
747                void *event_info __UNUSED__)
748 {
749    Evas_Coord w, h;
750    Widget_Data *wd;
751
752    wd = elm_widget_data_get(data);
753    if (!wd) return;
754
755    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
756    evas_object_resize(wd->bg, w, h);
757
758    if (!wd->visible) return;
759
760    _hide(data);
761 }
762
763 static void
764 _ctxpopup_show(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
765                void *event_info __UNUSED__)
766 {
767    Widget_Data *wd;
768    Eina_List *elist;
769    Elm_Ctxpopup_Item *item;
770    int idx = 0;
771
772    wd = elm_widget_data_get(obj);
773    if (!wd) return;
774
775    if ((!wd->items) && (!wd->content)) return;
776
777    wd->visible = EINA_TRUE;
778
779    evas_object_show(wd->bg);
780    evas_object_show(wd->base);
781    evas_object_show(wd->arrow);
782
783    edje_object_signal_emit(wd->bg, "elm,state,show", "elm");
784
785    EINA_LIST_FOREACH(wd->items, elist, item)
786      {
787        if (idx++ == 0)
788          edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
789        else
790          {
791            if(!wd->horizontal)
792              edje_object_signal_emit(item->base.view, "elm,state,vertical", "elm");
793            else
794              edje_object_signal_emit(item->base.view, "elm,state,horizontal", "elm");
795          }
796            }
797
798    _sizing_eval(obj);
799 }
800
801 static void
802 _hide(Evas_Object *obj)
803 {
804    Widget_Data *wd = elm_widget_data_get(obj);
805
806    if (!wd) return;
807
808    evas_object_hide(wd->bg);
809    evas_object_hide(wd->arrow);
810    evas_object_hide(wd->base);
811
812    _scroller_size_reset(wd);
813
814    evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
815    wd->visible = EINA_FALSE;
816 }
817
818 static void
819 _ctxpopup_hide(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
820                void *event_info __UNUSED__)
821 {
822    Widget_Data *wd;
823
824    wd = elm_widget_data_get(obj);
825    if ((!wd) || (!wd->visible))
826       return;
827
828    _hide(obj);
829 }
830
831 static void
832 _scroller_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
833                  void *event_info __UNUSED__)
834 {
835    Widget_Data *wd;
836    Evas_Coord w, h;
837
838    wd = elm_widget_data_get(data);
839    if (!wd) return;
840    if (!wd->visible) return;
841    if (wd->finished) return;
842
843    evas_object_geometry_get(obj, 0, 0, &w, &h);
844
845    if (w != 0 && h != 0)
846      {
847         if ((w <= wd->max_sc_w) && (h <= wd->max_sc_h))
848           {
849              _sizing_eval(data);
850              wd->finished = EINA_TRUE;
851              return;
852           }
853      }
854
855    if (wd->max_sc_w < w)
856       wd->max_sc_w = w;
857    if (wd->max_sc_h < h)
858       wd->max_sc_h = h;
859
860    _sizing_eval(data);
861 }
862
863 static void
864 _ctxpopup_move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
865                void *event_info __UNUSED__)
866 {
867    Widget_Data *wd;
868
869    wd = elm_widget_data_get(obj);
870
871    if (!wd) return;
872
873    if (wd->visible)
874       evas_object_show(wd->arrow);
875
876    _scroller_size_reset(wd);
877    _sizing_eval(obj);
878 }
879
880 static void
881 _item_select_cb(void *data, Evas_Object *obj __UNUSED__,
882                 const char *emission __UNUSED__, const char *source __UNUSED__)
883 {
884    Elm_Ctxpopup_Item *item = data;
885
886    if (!item) return;
887    if (item->disabled) return;
888
889    if (item->func)
890       item->func((void*) item->base.data, item->base.widget, data);
891 }
892
893 static void
894 _item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
895 {
896    if (item->icon)
897      {
898         elm_widget_sub_object_del(item->base.view, item->icon);
899         evas_object_del(item->icon);
900      }
901
902    item->icon = icon;
903    if (!icon) return;
904
905    edje_object_part_swallow(item->base.view, "elm.swallow.icon", item->icon);
906    edje_object_message_signal_process(item->base.view);
907 }
908
909 static void
910 _item_label_set(Elm_Ctxpopup_Item *item, const char *label)
911 {
912    if (!eina_stringshare_replace(&item->label, label))
913       return;
914
915    edje_object_part_text_set(item->base.view, "elm.text", label);
916    edje_object_message_signal_process(item->base.view);
917 }
918
919 static void
920 _item_new(Elm_Ctxpopup_Item *item, char *group_name)
921 {
922    Widget_Data *wd;
923
924    wd = elm_widget_data_get(item->base.widget);
925    if (!wd) return;
926
927    item->base.view = edje_object_add(evas_object_evas_get(wd->base));
928    _elm_theme_object_set(item->base.widget, item->base.view, "ctxpopup", group_name,
929                          elm_widget_style_get(item->base.widget));
930    edje_object_signal_callback_add(item->base.view, "elm,action,click", "",
931                                    _item_select_cb, item);
932    evas_object_size_hint_align_set(item->base.view, EVAS_HINT_FILL, EVAS_HINT_FILL);
933    evas_object_show(item->base.view);
934 }
935
936 static void
937 _content_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
938              void *event_info __UNUSED__)
939 {
940    elm_ctxpopup_content_unset(data);
941 }
942
943 static void
944 _list_del(Widget_Data *wd)
945 {
946    if (!wd->scr) return;
947
948    edje_object_part_unswallow(wd->base, wd->scr);
949    evas_object_del(wd->scr);
950    wd->scr = NULL;
951    wd->box = NULL;
952 }
953
954 static void
955 _list_new(Evas_Object *obj)
956 {
957    Widget_Data *wd;
958    wd = elm_widget_data_get(obj);
959    if (!wd) return;
960
961    //scroller
962    wd->scr = elm_scroller_add(obj);
963    elm_object_style_set(wd->scr, "ctxpopup");
964    evas_object_size_hint_align_set(wd->scr, EVAS_HINT_FILL, EVAS_HINT_FILL);
965    evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE,
966                                   _scroller_resize, obj);
967    edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scr);
968
969    //box
970    wd->box = elm_box_add(obj);
971    evas_object_size_hint_weight_set(wd->box, EVAS_HINT_EXPAND,
972                                     EVAS_HINT_EXPAND);
973
974    elm_scroller_content_set(wd->scr, wd->box);
975    elm_ctxpopup_horizontal_set(obj, wd->horizontal);
976 }
977
978 static void
979 _remove_items(Widget_Data *wd)
980 {
981    Eina_List *elist;
982    Elm_Ctxpopup_Item *item;
983
984    if (!wd->items) return;
985
986    EINA_LIST_FOREACH(wd->items, elist, item)
987      {
988         if (item->label)
989            eina_stringshare_del(item->label);
990         if (item->icon)
991            evas_object_del(item->icon);
992         wd->items = eina_list_remove(wd->items, item);
993         free(item);
994      }
995
996    wd->items = NULL;
997 }
998
999 /**
1000  * Add a new Ctxpopup object to the parent.
1001  *
1002  * @param parent Parent object
1003  * @return New object or @c NULL, if it cannot be created
1004  *
1005  * @ingroup Ctxpopup
1006  */
1007 EAPI Evas_Object *
1008 elm_ctxpopup_add(Evas_Object *parent)
1009 {
1010    Evas_Object *obj;
1011    Evas *e;
1012    Widget_Data *wd;
1013    Evas_Coord x, y, w, h;
1014
1015    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1016
1017    ELM_SET_WIDTYPE(widtype, "ctxpopup");
1018    elm_widget_type_set(obj, "ctxpopup");
1019    elm_widget_sub_object_add(parent, obj);
1020    elm_widget_data_set(obj, wd);
1021    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1022    elm_widget_del_hook_set(obj, _del_hook);
1023    elm_widget_theme_hook_set(obj, _theme_hook);
1024
1025    wd->parent = parent;
1026
1027    //Background
1028    wd->bg = edje_object_add(e);
1029    elm_widget_sub_object_add(obj, wd->bg);
1030    _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg", "default");
1031    evas_object_geometry_get(parent, &x, &y, &w, &h);
1032    evas_object_move(wd->bg, x, y);
1033    evas_object_resize(wd->bg, w, h);
1034    edje_object_signal_callback_add(wd->bg, "elm,action,click", "",
1035                                    _bg_clicked_cb, obj);
1036
1037    //Base
1038    wd->base = edje_object_add(e);
1039    elm_widget_sub_object_add(obj, wd->base);
1040    _elm_theme_object_set(obj, wd->base, "ctxpopup", "base", "default");
1041
1042    //Arrow
1043    wd->arrow = edje_object_add(e);
1044    elm_widget_sub_object_add(obj, wd->arrow);
1045    _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow", "default");
1046
1047    wd->dir_priority[0] = ELM_CTXPOPUP_DIRECTION_UP;
1048    wd->dir_priority[1] = ELM_CTXPOPUP_DIRECTION_LEFT;
1049    wd->dir_priority[2] = ELM_CTXPOPUP_DIRECTION_RIGHT;
1050    wd->dir_priority[3] = ELM_CTXPOPUP_DIRECTION_DOWN;
1051
1052    evas_object_event_callback_add(parent, EVAS_CALLBACK_RESIZE, _parent_resize,
1053                                   obj);
1054    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _ctxpopup_show,
1055                                   NULL);
1056    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _ctxpopup_hide,
1057                                   NULL);
1058    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _ctxpopup_move,
1059                                   NULL);
1060    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1061                                   _ctxpopup_changed_size_hints, NULL);
1062    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
1063    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
1064    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
1065    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
1066
1067    evas_object_smart_callbacks_descriptions_set(obj, _signals);
1068
1069    return obj;
1070 }
1071
1072 /**
1073  * Get the icon object for the given ctxpopup item.
1074  *
1075  * @param item Ctxpopup item
1076  * @return icon object or @c NULL, if the item does not have icon or an error occurred
1077  *
1078  * @ingroup Ctxpopup
1079  */
1080 EAPI Evas_Object *
1081 elm_ctxpopup_item_icon_get(const Elm_Ctxpopup_Item *item)
1082 {
1083    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
1084    return item->icon;
1085 }
1086
1087 /**
1088  * Sets the side icon associated with the ctxpopup item
1089  *
1090  * Once the icon object is set, a previously set one will be deleted.
1091  * You probably don't want, then, to have the <b>same</b> icon object
1092  * set for more than one item of the list (when replacing one of its
1093  * instances).
1094  *
1095  * @param item Ctxpopup item
1096  * @param icon Icon object to be set
1097  *
1098  * @ingroup Ctxpopup
1099  */
1100 EAPI void
1101 elm_ctxpopup_item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
1102 {
1103    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1104
1105    Widget_Data *wd;
1106
1107    wd = elm_widget_data_get(item->base.widget);
1108    if (!wd) return;
1109
1110    _item_icon_set(item, icon);
1111
1112    if (wd->visible)
1113      {
1114         _scroller_size_reset(wd);
1115         _sizing_eval(item->base.widget);
1116      }
1117 }
1118
1119 /**
1120  * Get the label object for the given ctxpopup item.
1121  *
1122  * @param item Ctxpopup item
1123  * @return label object or @c NULL, if the item does not have label or an error occured
1124  *
1125  * @ingroup Ctxpopup
1126  *
1127  */
1128 EAPI const char *
1129 elm_ctxpopup_item_label_get(const Elm_Ctxpopup_Item *item)
1130 {
1131    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
1132    return item->label;
1133 }
1134
1135 /**
1136  * (Re)set the label on the given ctxpopup item.
1137  *
1138  * @param item Ctxpopup item
1139  * @param label String to set as label
1140  *
1141  * @ingroup Ctxpopup
1142  */
1143 EAPI void
1144 elm_ctxpopup_item_label_set(Elm_Ctxpopup_Item *item, const char *label)
1145 {
1146    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1147
1148    Widget_Data *wd;
1149
1150    wd = elm_widget_data_get(item->base.widget);
1151    if (!wd) return;
1152
1153    _item_label_set(item, label);
1154
1155    if (wd->visible)
1156      {
1157         _scroller_size_reset(wd);
1158         _sizing_eval(item->base.widget);
1159      }
1160 }
1161
1162 /**
1163  * Set the Ctxpopup's parent
1164  * Set the parent object (it would much probably be the
1165  * window that the ctxpopup is in).
1166  *
1167  * @param obj The ctxpopup object
1168  * @param area The parent to use
1169  *
1170  * @note elm_ctxpopup_add() will automatically call this function
1171  * with its @c parent argument.
1172  *
1173  * @ingroup Ctxpopup
1174  */
1175 EAPI void
1176 elm_ctxpopup_hover_parent_set(Evas_Object *obj, Evas_Object *hover_parent)
1177 {
1178    ELM_CHECK_WIDTYPE(obj, widtype);
1179
1180    Widget_Data *wd;
1181
1182    wd = elm_widget_data_get(obj);
1183    if (!wd) return;
1184
1185    _hover_parent_callbacks_del(obj);
1186
1187    if (hover_parent)
1188      {
1189         evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_DEL,
1190                                        _hover_parent_del, obj);
1191         evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_MOVE,
1192                                        _hover_parent_move, obj);
1193         evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_RESIZE,
1194                                        _hover_parent_resize, obj);
1195      }
1196
1197    wd->hover_parent = hover_parent;
1198 }
1199
1200 /**
1201  * Get the Ctxpopup's parent
1202  *
1203  * @param obj The ctxpopup object
1204  *
1205  * @see elm_ctxpopup_hover_parent_set() for more information
1206  *
1207  * @ingroup Ctxpopup
1208  */
1209 EAPI Evas_Object *
1210 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
1211 {
1212    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1213
1214    Widget_Data *wd;
1215
1216    wd = elm_widget_data_get(obj);
1217    if (!wd) return NULL;
1218
1219    return wd->hover_parent;
1220 }
1221
1222 /**
1223  * Clear all items in the given ctxpopup object.
1224  *
1225  * @param obj Ctxpopup object
1226  *
1227  * @ingroup Ctxpopup
1228  */
1229 EAPI void
1230 elm_ctxpopup_clear(Evas_Object * obj)
1231 {
1232    ELM_CHECK_WIDTYPE(obj, widtype);
1233
1234    Widget_Data *wd = elm_widget_data_get(obj);
1235    if (!wd) return;
1236
1237    _remove_items(wd);
1238    _list_del(wd);
1239 }
1240
1241 /**
1242  * Change the ctxpopup's orientation to horizontal or vertical.
1243  *
1244  * @param obj Ctxpopup object
1245  * @param horizontal @c EINA_TRUE for horizontal mode, @c EINA_FALSE for vertical
1246  *
1247  * @ingroup Ctxpopup
1248  */
1249 EAPI void
1250 elm_ctxpopup_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
1251 {
1252    ELM_CHECK_WIDTYPE(obj, widtype);
1253
1254    Widget_Data *wd;
1255    Eina_List *elist;
1256          Elm_Ctxpopup_Item *item;
1257          int idx = 0;
1258
1259    wd = elm_widget_data_get(obj);
1260    if (!wd) return;
1261
1262    wd->horizontal = !!horizontal;
1263
1264    if ((!wd->scr) && (!wd->box))
1265       return;
1266
1267    if (!horizontal)
1268      {
1269         elm_box_horizontal_set(wd->box, EINA_FALSE);
1270         elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
1271
1272         EINA_LIST_FOREACH(wd->items, elist, item)
1273           {
1274              if (idx++ == 0)
1275                 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
1276              else
1277                 edje_object_signal_emit(item->base.view, "elm,state,vertical", "elm");
1278           }
1279      }
1280    else
1281      {
1282         elm_box_horizontal_set(wd->box, EINA_TRUE);
1283         elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
1284
1285         EINA_LIST_FOREACH(wd->items, elist, item)
1286           {
1287              if (idx++ == 0)
1288                 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
1289              else
1290                 edje_object_signal_emit(item->base.view, "elm,state,horizontal", "elm");
1291           }
1292      }
1293
1294    if (wd->visible)
1295       _sizing_eval(obj);
1296 }
1297
1298 /**
1299  * Get the value of current ctxpopup object's orientation.
1300  *
1301  * @param obj Ctxpopup object
1302  * @return @c EINA_TRUE for horizontal mode, @c EINA_FALSE for vertical mode (or errors)
1303  *
1304  * @ingroup Ctxpopup
1305  */
1306 EAPI Eina_Bool
1307 elm_ctxpopup_horizontal_get(const Evas_Object *obj)
1308 {
1309    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1310
1311    Widget_Data *wd;
1312
1313    wd = elm_widget_data_get(obj);
1314    if (!wd) return EINA_FALSE;
1315
1316    return wd->horizontal;
1317 }
1318
1319 /**
1320  * Add a new item to a ctxpopup object.
1321  *
1322  * Both a item list and a content could not be set at the same time!
1323  * once you set add a item, the previous content will be removed.
1324  *
1325  * @param obj Ctxpopup object
1326  * @param icon Icon to be set on new item
1327  * @param label The Label of the new item
1328  * @param func Convenience function called when item selected
1329  * @param data Data passed to @p func above
1330  * @return A handle to the item added or @c NULL, on errors
1331  *
1332  * @ingroup Ctxpopup
1333  */
1334 EAPI Elm_Ctxpopup_Item *
1335 elm_ctxpopup_item_append(Evas_Object *obj, const char *label,
1336                          Evas_Object *icon, Evas_Smart_Cb func,
1337                          const void *data)
1338 {
1339    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1340
1341    Widget_Data *wd;
1342    Elm_Ctxpopup_Item *item;
1343
1344    wd = elm_widget_data_get(obj);
1345    if (!wd) return NULL;
1346
1347    item = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
1348    if (!item) return NULL;
1349
1350    //The first item is appended.
1351    if (wd->content)
1352       evas_object_del(elm_ctxpopup_content_unset(obj));
1353
1354    if (!wd->items)
1355       _list_new(obj);
1356
1357    item->func = func;
1358    item->base.data = data;
1359
1360    if (icon && label)
1361       _item_new(item, "icon_text_style_item");
1362    else if (label)
1363       _item_new(item, "text_style_item");
1364    else
1365       _item_new(item, "icon_style_item");
1366
1367    _item_icon_set(item, icon);
1368    _item_label_set(item, label);
1369    elm_box_pack_end(wd->box, item->base.view);
1370    wd->items = eina_list_append(wd->items, item);
1371
1372    if (wd->visible)
1373      {
1374         _scroller_size_reset(wd);
1375         _sizing_eval(obj);
1376      }
1377
1378    return item;
1379 }
1380
1381 /**
1382  * Delete the given item in a ctxpopup object.
1383  *
1384  * @param item Ctxpopup item to be deleted
1385  *
1386  * @ingroup Ctxpopup
1387  */
1388 EAPI void
1389 elm_ctxpopup_item_del(Elm_Ctxpopup_Item *item)
1390 {
1391    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1392
1393    Widget_Data *wd;
1394
1395    wd = elm_widget_data_get(item->base.widget);
1396    if (!wd) return;
1397
1398    if (item->icon)
1399       evas_object_del(item->icon);
1400    if (item->base.view)
1401       evas_object_del(item->base.view);
1402
1403    eina_stringshare_del(item->label);
1404
1405    wd->items = eina_list_remove(wd->items, item);
1406
1407    if (eina_list_count(wd->items) < 1)
1408       wd->items = NULL;
1409
1410    if (wd->visible)
1411       _sizing_eval(item->base.widget);
1412
1413    free(item);
1414 }
1415
1416 /**
1417  * Set the ctxpopup item's state as disabled or enabled.
1418  *
1419  * @param item Ctxpopup item to be enabled/disabled
1420  * @param disabled @c EINA_TRUE to disable it, @c EINA_FALSE to enable it
1421  *
1422  * @ingroup Ctxpopup
1423  */
1424 EAPI void
1425 elm_ctxpopup_item_disabled_set(Elm_Ctxpopup_Item *item, Eina_Bool disabled)
1426 {
1427    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1428
1429    Widget_Data *wd;
1430
1431    wd = elm_widget_data_get(item->base.widget);
1432    if (!wd) return;
1433
1434    if (disabled == item->disabled)
1435       return;
1436
1437    if (disabled)
1438       edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
1439    else
1440       edje_object_signal_emit(item->base.view, "elm,state,enabled", "elm");
1441
1442    item->disabled = !!disabled;
1443 }
1444
1445 /**
1446  * Get the ctxpopup item's disabled/enabled state.
1447  *
1448  * @param item Ctxpopup item to be enabled/disabled
1449  * @return disabled @c EINA_TRUE, if disabled, @c EINA_FALSE otherwise
1450  *
1451  * @ingroup Ctxpopup
1452  */
1453 EAPI Eina_Bool
1454 elm_ctxpopup_item_disabled_get(const Elm_Ctxpopup_Item *item)
1455 {
1456    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, EINA_FALSE);
1457
1458    return item->disabled;
1459 }
1460
1461 /**
1462  * Once the content object is set, a previously set one will be deleted.
1463  * If you want to keep that old content object, use the
1464  * elm_ctxpopup_content_unset() function
1465  *
1466  * Both a item list and a content could not be set at the same time!
1467  * once you set a content, the previous list items will be removed.
1468  *
1469  * @param obj Ctxpopup object
1470  * @param content Content to be swallowed
1471  *
1472  * @ingroup Ctxpopup
1473  */
1474 EAPI void
1475 elm_ctxpopup_content_set(Evas_Object *obj, Evas_Object *content)
1476 {
1477    ELM_CHECK_WIDTYPE(obj, widtype);
1478
1479    Widget_Data *wd;
1480
1481    wd = elm_widget_data_get(obj);
1482    if ((!wd) || (!content))
1483       return;
1484
1485    if (wd->items)
1486       elm_ctxpopup_clear(obj);
1487
1488    if (wd->content)
1489       evas_object_del(wd->content);
1490
1491    evas_object_event_callback_add(content, EVAS_CALLBACK_DEL, _content_del,
1492                                   obj);
1493
1494    elm_widget_sub_object_add(obj, content);
1495    edje_object_part_swallow(wd->base, "elm.swallow.content", content);
1496    edje_object_message_signal_process(wd->base);
1497
1498    wd->content = content;
1499
1500    if (wd->visible)
1501       _sizing_eval(obj);
1502 }
1503
1504 /**
1505  * Unset the ctxpopup content
1506  *
1507  * Unparent and return the content object which was set for this widget
1508  *
1509  * @param obj Ctxpopup object
1510  * @return The content that was being used
1511  *
1512  * @ingroup Ctxpopup
1513  */
1514 EAPI Evas_Object *
1515 elm_ctxpopup_content_unset(Evas_Object *obj)
1516 {
1517    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1518
1519    Widget_Data *wd;
1520    Evas_Object *content;
1521
1522    wd = elm_widget_data_get(obj);
1523    if (!wd) return NULL;
1524
1525    content = wd->content;
1526    if (!content) return NULL;
1527
1528    edje_object_part_unswallow(wd->base, content);
1529    elm_widget_sub_object_del(obj, content);
1530    evas_object_event_callback_del(content, EVAS_CALLBACK_DEL, _content_del);
1531    edje_object_signal_emit(wd->base, "elm,state,content,disable", "elm");
1532
1533    wd->content = NULL;
1534
1535    return content;
1536 }
1537
1538 /**
1539  * Set the direction priority of a ctxpopup.
1540  * This functions gives a chance to user to set the priority of ctxpopup showing direction.
1541  *
1542  * @param obj Ctxpopup object
1543  * @param first 1st priority of direction
1544  * @param second 2nd priority of direction
1545  * @param third 3th priority of direction
1546  * @param fourth 4th priority of direction
1547  *
1548  * @ingroup Ctxpopup
1549  */
1550 EAPI void
1551 elm_ctxpopup_direction_priority_set(Evas_Object *obj,
1552                                     Elm_Ctxpopup_Direction first,
1553                                     Elm_Ctxpopup_Direction second,
1554                                     Elm_Ctxpopup_Direction third,
1555                                     Elm_Ctxpopup_Direction fourth)
1556 {
1557    ELM_CHECK_WIDTYPE(obj, widtype);
1558    Widget_Data *wd;
1559
1560    wd = elm_widget_data_get(obj);
1561    if (!wd) return;
1562
1563    wd->dir_priority[0] = first;
1564    wd->dir_priority[1] = second;
1565    wd->dir_priority[2] = third;
1566    wd->dir_priority[3] = fourth;
1567
1568    if (wd->visible)
1569       _sizing_eval(obj);
1570 }
1571
1572 /**
1573  * Get the direction priority of a ctxpopup.
1574  *
1575  * @param obj Ctxpopup object
1576  * @param first 1st priority of direction to be returned
1577  * @param second 2nd priority of direction to be returned
1578  * @param third 3th priority of direction to be returned
1579  * @param fourth 4th priority of direction to be returned
1580  *
1581  * @see elm_ctxpopup_direction_priority_set for more information.
1582  *
1583  * @ingroup Ctxpopup
1584  */
1585 EAPI void
1586 elm_ctxpopup_direction_priority_get(Evas_Object *obj,
1587                                     Elm_Ctxpopup_Direction *first,
1588                                     Elm_Ctxpopup_Direction *second,
1589                                     Elm_Ctxpopup_Direction *third,
1590                                     Elm_Ctxpopup_Direction *fourth)
1591 {
1592    ELM_CHECK_WIDTYPE(obj, widtype);
1593    Widget_Data *wd;
1594
1595    wd = elm_widget_data_get(obj);
1596    if (!wd) return;
1597
1598    if (first)
1599       *first = wd->dir_priority[0];
1600    if (second)
1601       *second = wd->dir_priority[1];
1602    if (third)
1603       *third = wd->dir_priority[2];
1604    if (fourth)
1605       *fourth = wd->dir_priority[3];
1606 }