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