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