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