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