Merge "[genlist] disable previous item focus"
[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, "elm,state,left", "elm");
521          edje_object_part_swallow(wd->base,
522                                   "elm.swallow.arrow_left",
523                                   wd->arrow);
524
525          // if user does not use dragable part
526          arrow_size.y = (y - (arrow_size.h * 0.5));
527          arrow_size.x = x;
528
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, "elm,state,right", "elm");
546          edje_object_part_swallow(wd->base,
547                                   "elm.swallow.arrow_right",
548                                   wd->arrow);
549
550          // if user does not use dragable part
551          arrow_size.y = (y - (arrow_size.h * 0.5));
552          arrow_size.x = (x - arrow_size.w);
553
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
561                 y = y - base_size.y - (arrow_size.h * 0.5);
562               drag = (double) (y) / (double) (base_size.h - arrow_size.h);
563               edje_object_part_drag_value_set(wd->base,
564                                               "elm.swallow.arrow_right",
565                                               0,
566                                               drag);
567            }
568          break;
569       case ELM_CTXPOPUP_DIRECTION_DOWN:
570          edje_object_signal_emit(wd->arrow, "elm,state,top", "elm");
571          edje_object_part_swallow(wd->base, "elm.swallow.arrow_up", wd->arrow);
572
573          // if user does not use dragable part
574          arrow_size.x = (x - (arrow_size.w * 0.5));
575          arrow_size.y = y;
576
577          if (base_size.w > 0)
578            {
579               if (x < ((arrow_size.w * 0.5) + base_size.x))
580                 x = 0;
581               else if (x > (base_size.x + base_size.w - (arrow_size.w * 0.5)))
582                 x = base_size.w - arrow_size.w;
583               else
584                 x = x - base_size.x - (arrow_size.w * 0.5);
585               drag = (double) (x) / (double) (base_size.w - arrow_size.w);
586               edje_object_part_drag_value_set(wd->base,
587                                               "elm.swallow.arrow_up",
588                                               drag,
589                                               1);
590            }
591          break;
592       case ELM_CTXPOPUP_DIRECTION_UP:
593          edje_object_signal_emit(wd->arrow, "elm,state,bottom", "elm");
594          edje_object_part_swallow(wd->base,
595                                   "elm.swallow.arrow_down",
596                                   wd->arrow);
597
598          // if user does not use dragable part
599          arrow_size.x = (x - (arrow_size.w * 0.5));
600          arrow_size.y = (y - arrow_size.h);
601
602          if (base_size.w > 0)
603            {
604               if (x < ((arrow_size.w * 0.5) + base_size.x))
605                 x = 0;
606               else if (x > (base_size.x + base_size.w - (arrow_size.w * 0.5)))
607                 x = base_size.w - arrow_size.w;
608               else x = x - base_size.x - (arrow_size.w * 0.5);
609               drag = (double) (x) / (double) (base_size.w - arrow_size.w);
610               edje_object_part_drag_value_set(wd->base,
611                                               "elm.swallow.arrow_down",
612                                               drag,
613                                               0);
614            }
615          break;
616       default:
617          break;
618      }
619
620    // if user does not use dragable part
621    evas_object_move(wd->arrow, arrow_size.x, arrow_size.y);
622 }
623
624 //TODO: compress item - different from opensource
625 static void
626 _compress_item(Evas_Object *obj)
627 {
628    Widget_Data *wd;
629    Eina_List *elist;
630    Elm_Ctxpopup_Item *item;
631
632    wd = elm_widget_data_get(obj);
633    if (!wd) return;
634
635    EINA_LIST_FOREACH(wd->items, elist, item)
636      {
637            edje_object_signal_emit(item->base.view, "elm,state,compress", "elm");
638      }
639 }
640
641 static void
642 _sizing_eval(Evas_Object *obj)
643 {
644    Widget_Data *wd;
645    Eina_List *elist;
646    Elm_Ctxpopup_Item *item;
647    Evas_Coord_Rectangle rect = { 0, 0, 1, 1 };
648    Evas_Coord_Point box_size = { 0, 0 };
649    Evas_Coord_Point _box_size = { 0, 0 };
650
651    wd = elm_widget_data_get(obj);
652    if (!wd) return;
653    if (!wd->parent) return;
654
655    //Box, Scroller
656    EINA_LIST_FOREACH(wd->items, elist, item)
657      {
658         _item_sizing_eval(item);
659         evas_object_size_hint_min_get(VIEW(item), &_box_size.x, &_box_size.y);
660         if (!wd->horizontal)
661           {
662              if (_box_size.x > box_size.x)
663                box_size.x = _box_size.x;
664              if (_box_size.y != -1)
665                box_size.y += _box_size.y;
666           }
667         else
668           {
669              if (_box_size.x != -1)
670                box_size.x += _box_size.x;
671              if (_box_size.y > box_size.y)
672                box_size.y = _box_size.y;
673           }
674      }
675
676    if (!wd->content)
677      {
678         evas_object_size_hint_min_set(wd->box, box_size.x, box_size.y);
679         evas_object_size_hint_min_set(wd->scr, box_size.x, box_size.y);
680      }
681
682    //Base
683    wd->dir = _calc_base_geometry(obj, &rect);
684
685    //TODO: compress item - different from opensource
686    if (!wd->horizontal && !wd->content)
687      _compress_item(obj);
688
689    _update_arrow(obj, wd->dir);
690    _shift_base_by_arrow(wd->arrow, wd->dir, &rect);
691
692    //resize scroller according to final size.
693    if (!wd->content)
694      evas_object_smart_calculate(wd->scr);
695
696    evas_object_move(wd->base, rect.x, rect.y);
697    evas_object_resize(wd->base, rect.w, rect.h);
698 }
699
700 static void
701 _shift_base_by_arrow(Evas_Object *arrow, Elm_Ctxpopup_Direction dir,
702                      Evas_Coord_Rectangle *rect)
703 {
704    Evas_Coord arrow_w, arrow_h;
705
706    evas_object_geometry_get(arrow, NULL, NULL, &arrow_w, &arrow_h);
707
708    switch (dir)
709      {
710       case ELM_CTXPOPUP_DIRECTION_RIGHT:
711          rect->x += arrow_w;
712          break;
713       case ELM_CTXPOPUP_DIRECTION_LEFT:
714          rect->x -= arrow_w;
715          break;
716       case ELM_CTXPOPUP_DIRECTION_DOWN:
717          rect->y += arrow_h;
718          break;
719       case ELM_CTXPOPUP_DIRECTION_UP:
720          rect->y -= arrow_h;
721          break;
722       default:
723          break;
724      }
725 }
726
727 static void
728 _del_pre_hook(Evas_Object *obj)
729 {
730    Widget_Data *wd;
731
732    wd = elm_widget_data_get(obj);
733    if (!wd) return;
734
735    evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_RESIZE,
736                                        _parent_resize, obj);
737
738    _hover_parent_callbacks_del(obj);
739 }
740
741 static void
742 _del_hook(Evas_Object *obj)
743 {
744    Widget_Data *wd;
745
746    wd = elm_widget_data_get(obj);
747    if (!wd) return;
748
749    elm_ctxpopup_clear(obj);
750    evas_object_del(wd->arrow);
751    evas_object_del(wd->base);
752    free(wd);
753 }
754
755 //FIXME: lost the content size when theme hook is called. 
756 static void
757 _theme_hook(Evas_Object *obj)
758 {
759    Widget_Data *wd;
760    Eina_List *elist;
761    Elm_Ctxpopup_Item *item;
762    int idx = 0;
763
764    wd = elm_widget_data_get(obj);
765    if (!wd) return;
766
767    //Items
768    EINA_LIST_FOREACH(wd->items, elist, item)
769      {
770         if (item->label && item->icon)
771           _elm_theme_object_set(obj, VIEW(item), "ctxpopup",
772                                 "icon_text_style_item",
773                                 elm_widget_style_get(obj));
774         else if (item->label)
775           _elm_theme_object_set(obj, VIEW(item), "ctxpopup", "text_style_item",
776                                 elm_widget_style_get(obj));
777         else if (item->icon)
778           _elm_theme_object_set(obj, VIEW(item), "ctxpopup", "icon_style_item",
779                                 elm_widget_style_get(obj));
780         if (item->label)
781           edje_object_part_text_set(VIEW(item), "elm.text", item->label);
782
783         if (item->disabled)
784           edje_object_signal_emit(VIEW(item), "elm,state,disabled", "elm");
785
786        /*
787         *  For separator, if the first item has visible separator,
788         *  then it should be aligned with edge of the base part.
789         *  In some cases, it gives improper display. Ex) rounded corner
790         *  So the first item separator should be invisible.
791         */
792        if ((idx++) == 0)
793          edje_object_signal_emit(VIEW(item), "elm,state,default", "elm");
794        else
795          {
796            if(!wd->horizontal)
797              edje_object_signal_emit(VIEW(item), "elm,state,vertical", "elm");
798            else
799              edje_object_signal_emit(VIEW(item), "elm,state,horizontal", "elm");
800          }
801
802         edje_object_message_signal_process(VIEW(item));
803      }
804
805    _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg",
806                          elm_widget_style_get(obj));
807    if (evas_object_visible_get(wd->bg))
808      edje_object_signal_emit(wd->bg, "elm,state,show", "elm");
809
810    _elm_theme_object_set(obj, wd->base, "ctxpopup", "base",
811                          elm_widget_style_get(obj));
812    _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow",
813                          elm_widget_style_get(obj));
814
815    if (wd->scr)
816      {
817         if (!strncmp(elm_object_style_get(obj), "default", strlen("default")))
818            elm_object_style_set(wd->scr, "ctxpopup");
819         else
820            elm_object_style_set(wd->scr, elm_object_style_get(obj));
821      }
822
823    if (wd->visible)
824      {
825         _scroller_size_reset(wd);
826         _sizing_eval(obj);
827      }
828 }
829
830 static void
831 _content_set_hook(Evas_Object *obj, const char *part,
832                   Evas_Object *content)
833 {
834    ELM_CHECK_WIDTYPE(obj, widtype);
835
836    Widget_Data *wd;
837
838    if (part && strcmp(part, "default")) return;
839
840    wd = elm_widget_data_get(obj);
841    if ((!wd) || (!content)) return;
842
843    if (wd->items) elm_ctxpopup_clear(obj);
844    if (wd->content) evas_object_del(wd->content);
845
846    evas_object_event_callback_add(content, EVAS_CALLBACK_DEL, _content_del,
847                                   obj);
848
849    elm_widget_sub_object_add(obj, content);
850    edje_object_part_swallow(wd->base, "elm.swallow.content", content);
851    edje_object_message_signal_process(wd->base);
852
853    wd->content = content;
854
855    if (wd->visible)
856      _sizing_eval(obj);
857 }
858
859 static Evas_Object *
860 _content_unset_hook(Evas_Object *obj, const char *part)
861 {
862    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
863
864    Widget_Data *wd;
865    Evas_Object *content;
866    if (part && strcmp(part, "default")) return NULL;
867    wd = elm_widget_data_get(obj);
868    if (!wd) return NULL;
869
870    content = wd->content;
871    if (!content) return NULL;
872
873    edje_object_part_unswallow(wd->base, content);
874    elm_widget_sub_object_del(obj, content);
875    evas_object_event_callback_del(content, EVAS_CALLBACK_DEL, _content_del);
876    edje_object_signal_emit(wd->base, "elm,state,content,disable", "elm");
877
878    wd->content = NULL;
879
880    return content;
881
882 }
883
884 static Evas_Object *
885 _content_get_hook(const Evas_Object *obj, const char *part)
886 {
887    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
888    Widget_Data *wd;
889    if (part && strcmp(part, "default")) return NULL;
890    wd = elm_widget_data_get(obj);
891    if (!wd) return NULL;
892    return wd->content;
893 }
894
895 static void
896 _bg_clicked_cb(void *data, Evas_Object *obj __UNUSED__,
897                const char *emission __UNUSED__, const char *source __UNUSED__)
898 {
899    evas_object_hide(data);
900 }
901
902 static void
903 _parent_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
904                void *event_info __UNUSED__)
905 {
906    Evas_Coord w, h;
907    Widget_Data *wd;
908
909    wd = elm_widget_data_get(data);
910    if (!wd) return;
911
912    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
913    evas_object_resize(wd->bg, w, h);
914
915    if (!wd->visible) return;
916
917    _hide(data);
918 }
919
920 static void
921 _ctxpopup_show(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
922                void *event_info __UNUSED__)
923 {
924    Widget_Data *wd;
925    Eina_List *elist;
926    Elm_Ctxpopup_Item *item;
927    int idx = 0;
928
929    wd = elm_widget_data_get(obj);
930    if (!wd) return;
931
932    if ((!wd->items) && (!wd->content)) return;
933
934    wd->visible = EINA_TRUE;
935
936    evas_object_show(wd->bg);
937    evas_object_show(wd->base);
938    evas_object_show(wd->arrow);
939
940    edje_object_signal_emit(wd->bg, "elm,state,show", "elm");
941
942    EINA_LIST_FOREACH(wd->items, elist, item)
943      {
944        if (idx++ == 0)
945          edje_object_signal_emit(VIEW(item), "elm,state,default", "elm");
946        else
947          {
948            if(!wd->horizontal)
949              edje_object_signal_emit(VIEW(item), "elm,state,vertical", "elm");
950            else
951              edje_object_signal_emit(VIEW(item), "elm,state,horizontal", "elm");
952          }
953      }
954
955    _sizing_eval(obj);
956 }
957
958 static void
959 _hide(Evas_Object *obj)
960 {
961    Widget_Data *wd = elm_widget_data_get(obj);
962
963    if (!wd) return;
964
965    evas_object_hide(wd->bg);
966    evas_object_hide(wd->arrow);
967    evas_object_hide(wd->base);
968
969    _scroller_size_reset(wd);
970
971    evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
972    wd->visible = EINA_FALSE;
973 }
974
975 static void
976 _ctxpopup_hide(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
977                void *event_info __UNUSED__)
978 {
979    Widget_Data *wd;
980
981    wd = elm_widget_data_get(obj);
982    if ((!wd) || (!wd->visible))
983       return;
984
985    _hide(obj);
986 }
987
988 static void
989 _scroller_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
990                  void *event_info __UNUSED__)
991 {
992    Widget_Data *wd;
993    Evas_Coord w, h;
994
995    wd = elm_widget_data_get(data);
996    if (!wd) return;
997    if (!wd->visible) return;
998    if (wd->finished) return;
999
1000    evas_object_geometry_get(obj, 0, 0, &w, &h);
1001
1002    if (w != 0 && h != 0)
1003      {
1004         if ((w <= wd->max_sc_w) && (h <= wd->max_sc_h))
1005           {
1006              _sizing_eval(data);
1007              wd->finished = EINA_TRUE;
1008              return;
1009           }
1010      }
1011
1012    if (wd->max_sc_w < w)
1013      wd->max_sc_w = w;
1014    if (wd->max_sc_h < h)
1015      wd->max_sc_h = h;
1016
1017    _sizing_eval(data);
1018 }
1019
1020 static void
1021 _ctxpopup_move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
1022                void *event_info __UNUSED__)
1023 {
1024    Widget_Data *wd;
1025
1026    wd = elm_widget_data_get(obj);
1027
1028    if (!wd) return;
1029
1030    if (wd->visible)
1031      evas_object_show(wd->arrow);
1032
1033    _scroller_size_reset(wd);
1034    _sizing_eval(obj);
1035 }
1036
1037 static void
1038 _item_select_cb(void *data, Evas_Object *obj __UNUSED__,
1039                 const char *emission __UNUSED__, const char *source __UNUSED__)
1040 {
1041    Elm_Ctxpopup_Item *item = data;
1042
1043    if (!item) return;
1044    if (item->disabled) return;
1045
1046    if (item->func)
1047      item->func((void*) item->base.data, WIDGET(item), data);
1048 }
1049
1050 static void
1051 _item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
1052 {
1053    if (item->icon)
1054      {
1055         elm_widget_sub_object_del(VIEW(item), item->icon);
1056         evas_object_del(item->icon);
1057      }
1058
1059    item->icon = icon;
1060    if (!icon) return;
1061
1062    edje_object_part_swallow(VIEW(item), "elm.swallow.icon", item->icon);
1063    edje_object_message_signal_process(VIEW(item));
1064 }
1065
1066 static void
1067 _item_label_set(Elm_Ctxpopup_Item *item, const char *label)
1068 {
1069    if (!eina_stringshare_replace(&item->label, label))
1070      return;
1071
1072    edje_object_part_text_set(VIEW(item), "elm.text", label);
1073    edje_object_message_signal_process(VIEW(item));
1074 }
1075
1076 static void
1077 _item_new(Elm_Ctxpopup_Item *item, char *group_name)
1078 {
1079    Widget_Data *wd;
1080
1081    wd = elm_widget_data_get(WIDGET(item));
1082    if (!wd) return;
1083
1084    VIEW(item) = edje_object_add(evas_object_evas_get(wd->base));
1085    _elm_theme_object_set(WIDGET(item), VIEW(item), "ctxpopup", group_name,
1086                          elm_widget_style_get(WIDGET(item)));
1087    edje_object_signal_callback_add(VIEW(item), "elm,action,click", "",
1088                                    _item_select_cb, item);
1089    evas_object_size_hint_align_set(VIEW(item), EVAS_HINT_FILL, EVAS_HINT_FILL);
1090    evas_object_show(VIEW(item));
1091 }
1092
1093 static void
1094 _content_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
1095              void *event_info __UNUSED__)
1096 {
1097    elm_object_content_unset(data);
1098 }
1099
1100 static void
1101 _list_del(Widget_Data *wd)
1102 {
1103    if (!wd->scr) return;
1104
1105    edje_object_part_unswallow(wd->base, wd->scr);
1106    evas_object_del(wd->scr);
1107    wd->scr = NULL;
1108    wd->box = NULL;
1109 }
1110
1111 static void
1112 _list_new(Evas_Object *obj)
1113 {
1114    Widget_Data *wd;
1115    wd = elm_widget_data_get(obj);
1116    if (!wd) return;
1117
1118    //scroller
1119    wd->scr = elm_scroller_add(obj);
1120    elm_object_style_set(wd->scr, "ctxpopup");
1121    evas_object_size_hint_align_set(wd->scr, EVAS_HINT_FILL, EVAS_HINT_FILL);
1122    evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE,
1123                                   _scroller_resize, obj);
1124    edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scr);
1125
1126    //box
1127    wd->box = elm_box_add(obj);
1128    evas_object_size_hint_weight_set(wd->box, EVAS_HINT_EXPAND,
1129                                     EVAS_HINT_EXPAND);
1130
1131    elm_object_content_set(wd->scr, wd->box);
1132    elm_ctxpopup_horizontal_set(obj, wd->horizontal);
1133 }
1134
1135 static void
1136 _remove_items(Widget_Data *wd)
1137 {
1138    Eina_List *elist;
1139    Elm_Ctxpopup_Item *item;
1140
1141    if (!wd->items) return;
1142
1143    EINA_LIST_FOREACH(wd->items, elist, item)
1144      {
1145         if (item->label)
1146           eina_stringshare_del(item->label);
1147         if (item->icon)
1148           evas_object_del(item->icon);
1149         wd->items = eina_list_remove(wd->items, item);
1150         free(item);
1151      }
1152
1153    wd->items = NULL;
1154 }
1155
1156 EAPI Evas_Object *
1157 elm_ctxpopup_add(Evas_Object *parent)
1158 {
1159    Evas_Object *obj;
1160    Evas *e;
1161    Widget_Data *wd;
1162    Evas_Coord x, y, w, h;
1163
1164    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1165
1166    ELM_SET_WIDTYPE(widtype, "ctxpopup");
1167    elm_widget_type_set(obj, "ctxpopup");
1168    elm_widget_sub_object_add(parent, obj);
1169    elm_widget_data_set(obj, wd);
1170    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1171    elm_widget_del_hook_set(obj, _del_hook);
1172    elm_widget_theme_hook_set(obj, _theme_hook);
1173    elm_widget_content_set_hook_set(obj, _content_set_hook);
1174    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
1175    elm_widget_content_get_hook_set(obj, _content_get_hook);
1176
1177    wd->parent = parent;
1178
1179    //Background
1180    wd->bg = edje_object_add(e);
1181    elm_widget_sub_object_add(obj, wd->bg);
1182    _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg", "default");
1183    evas_object_geometry_get(parent, &x, &y, &w, &h);
1184    evas_object_move(wd->bg, x, y);
1185    evas_object_resize(wd->bg, w, h);
1186    edje_object_signal_callback_add(wd->bg, "elm,action,click", "",
1187                                    _bg_clicked_cb, obj);
1188
1189    //Base
1190    wd->base = edje_object_add(e);
1191    elm_widget_sub_object_add(obj, wd->base);
1192    _elm_theme_object_set(obj, wd->base, "ctxpopup", "base", "default");
1193
1194    //Arrow
1195    wd->arrow = edje_object_add(e);
1196    elm_widget_sub_object_add(obj, wd->arrow);
1197    _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow", "default");
1198
1199    wd->dir_priority[0] = ELM_CTXPOPUP_DIRECTION_UP;
1200    wd->dir_priority[1] = ELM_CTXPOPUP_DIRECTION_LEFT;
1201    wd->dir_priority[2] = ELM_CTXPOPUP_DIRECTION_RIGHT;
1202    wd->dir_priority[3] = ELM_CTXPOPUP_DIRECTION_DOWN;
1203
1204    evas_object_event_callback_add(parent, EVAS_CALLBACK_RESIZE, _parent_resize,
1205                                   obj);
1206    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _ctxpopup_show,
1207                                   NULL);
1208    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _ctxpopup_hide,
1209                                   NULL);
1210    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _ctxpopup_move,
1211                                   NULL);
1212    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1213                                   _ctxpopup_changed_size_hints, NULL);
1214    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
1215    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
1216    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
1217    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
1218
1219    evas_object_smart_callbacks_descriptions_set(obj, _signals);
1220
1221    return obj;
1222 }
1223
1224 EAPI Evas_Object *
1225 elm_ctxpopup_item_icon_get(const Elm_Object_Item *it)
1226 {
1227    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1228    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1229    return ctxpopup_it->icon;
1230 }
1231
1232 EAPI void
1233 elm_ctxpopup_item_icon_set(Elm_Object_Item *it, Evas_Object *icon)
1234 {
1235    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1236
1237    Widget_Data *wd;
1238    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1239
1240    wd = elm_widget_data_get(WIDGET(ctxpopup_it));
1241    if (!wd) return;
1242
1243    _item_icon_set(ctxpopup_it, icon);
1244
1245    if (wd->visible)
1246      {
1247         _scroller_size_reset(wd);
1248         _sizing_eval(WIDGET(ctxpopup_it));
1249      }
1250 }
1251
1252 EAPI const char *
1253 elm_ctxpopup_item_label_get(const Elm_Object_Item *it)
1254 {
1255    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
1256    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1257    return ctxpopup_it->label;
1258 }
1259
1260 EAPI void
1261 elm_ctxpopup_item_label_set(Elm_Object_Item *it, const char *label)
1262 {
1263    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1264
1265    Widget_Data *wd;
1266    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1267
1268    wd = elm_widget_data_get(WIDGET(ctxpopup_it));
1269    if (!wd) return;
1270
1271    _item_label_set(ctxpopup_it, label);
1272
1273    if (wd->visible)
1274      {
1275         _scroller_size_reset(wd);
1276         _sizing_eval(WIDGET(ctxpopup_it));
1277      }
1278 }
1279
1280 EAPI void
1281 elm_ctxpopup_hover_parent_set(Evas_Object *obj, Evas_Object *hover_parent)
1282 {
1283    ELM_CHECK_WIDTYPE(obj, widtype);
1284
1285    Widget_Data *wd;
1286
1287    wd = elm_widget_data_get(obj);
1288    if (!wd) return;
1289
1290    _hover_parent_callbacks_del(obj);
1291
1292    if (hover_parent)
1293      {
1294         evas_object_event_callback_add(hover_parent,
1295                                        EVAS_CALLBACK_DEL,
1296                                        _hover_parent_del,
1297                                        obj);
1298         evas_object_event_callback_add(hover_parent,
1299                                        EVAS_CALLBACK_MOVE,
1300                                        _hover_parent_move,
1301                                        obj);
1302         evas_object_event_callback_add(hover_parent,
1303                                        EVAS_CALLBACK_RESIZE,
1304                                        _hover_parent_resize,
1305                                        obj);
1306      }
1307
1308    wd->hover_parent = hover_parent;
1309 }
1310
1311 EAPI Evas_Object *
1312 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
1313 {
1314    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1315
1316    Widget_Data *wd;
1317
1318    wd = elm_widget_data_get(obj);
1319    if (!wd) return NULL;
1320
1321    return wd->hover_parent;
1322 }
1323
1324 EAPI void
1325 elm_ctxpopup_clear(Evas_Object * obj)
1326 {
1327    ELM_CHECK_WIDTYPE(obj, widtype);
1328
1329    Widget_Data *wd = elm_widget_data_get(obj);
1330    if (!wd) return;
1331
1332    _remove_items(wd);
1333    _list_del(wd);
1334 }
1335
1336 EAPI void
1337 elm_ctxpopup_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
1338 {
1339    ELM_CHECK_WIDTYPE(obj, widtype);
1340
1341    Widget_Data *wd;
1342    Eina_List *elist;
1343    Elm_Ctxpopup_Item *item;
1344    int idx = 0;
1345
1346    wd = elm_widget_data_get(obj);
1347    if (!wd) return;
1348
1349    wd->horizontal = !!horizontal;
1350
1351    if ((!wd->scr) && (!wd->box))
1352       return;
1353
1354    if (!horizontal)
1355      {
1356         elm_box_horizontal_set(wd->box, EINA_FALSE);
1357         elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
1358
1359         EINA_LIST_FOREACH(wd->items, elist, item)
1360           {
1361              if (idx++ == 0)
1362                edje_object_signal_emit(VIEW(item), "elm,state,default", "elm");
1363              else
1364                edje_object_signal_emit(VIEW(item), "elm,state,vertical", "elm");
1365           }
1366      }
1367    else
1368      {
1369         elm_box_horizontal_set(wd->box, EINA_TRUE);
1370         elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
1371
1372         EINA_LIST_FOREACH(wd->items, elist, item)
1373           {
1374              if (idx++ == 0)
1375                edje_object_signal_emit(VIEW(item), "elm,state,default", "elm");
1376              else
1377                edje_object_signal_emit(VIEW(item), "elm,state,horizontal", "elm");
1378           }
1379      }
1380
1381    if (wd->visible)
1382       _sizing_eval(obj);
1383 }
1384
1385 EAPI Eina_Bool
1386 elm_ctxpopup_horizontal_get(const Evas_Object *obj)
1387 {
1388    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1389
1390    Widget_Data *wd;
1391
1392    wd = elm_widget_data_get(obj);
1393    if (!wd) return EINA_FALSE;
1394
1395    return wd->horizontal;
1396 }
1397
1398 EAPI Elm_Object_Item *
1399 elm_ctxpopup_item_append(Evas_Object *obj, const char *label,
1400                          Evas_Object *icon, Evas_Smart_Cb func,
1401                          const void *data)
1402 {
1403    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1404
1405    Widget_Data *wd;
1406    Elm_Ctxpopup_Item *item;
1407
1408    wd = elm_widget_data_get(obj);
1409    if (!wd) return NULL;
1410
1411    item = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
1412    if (!item) return NULL;
1413
1414    //The first item is appended.
1415    if (wd->content)
1416      evas_object_del(elm_object_content_unset(obj));
1417
1418    if (!wd->items)
1419      _list_new(obj);
1420
1421    item->func = func;
1422    item->base.data = data;
1423
1424    if (icon && label)
1425      _item_new(item, "icon_text_style_item");
1426    else if (label)
1427      _item_new(item, "text_style_item");
1428    else
1429      _item_new(item, "icon_style_item");
1430
1431    _item_icon_set(item, icon);
1432    _item_label_set(item, label);
1433    elm_box_pack_end(wd->box, VIEW(item));
1434    wd->items = eina_list_append(wd->items, item);
1435
1436    if (wd->visible)
1437      {
1438         _scroller_size_reset(wd);
1439         _sizing_eval(obj);
1440      }
1441
1442    return (Elm_Object_Item *) item;
1443 }
1444
1445 EAPI void
1446 elm_ctxpopup_item_del(Elm_Object_Item *it)
1447 {
1448    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1449
1450    Widget_Data *wd;
1451    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1452
1453    wd = elm_widget_data_get(WIDGET(ctxpopup_it));
1454    if (!wd) return;
1455
1456    if (ctxpopup_it->icon)
1457      evas_object_del(ctxpopup_it->icon);
1458    if (VIEW(ctxpopup_it))
1459      evas_object_del(VIEW(ctxpopup_it));
1460
1461    eina_stringshare_del(ctxpopup_it->label);
1462
1463    wd->items = eina_list_remove(wd->items, ctxpopup_it);
1464
1465    if (eina_list_count(wd->items) < 1)
1466      wd->items = NULL;
1467
1468    if (wd->visible)
1469      _sizing_eval(WIDGET(ctxpopup_it));
1470
1471    free(ctxpopup_it);
1472 }
1473
1474 EAPI void
1475 elm_ctxpopup_item_disabled_set(Elm_Object_Item *it, Eina_Bool disabled)
1476 {
1477    ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1478
1479    Widget_Data *wd;
1480    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1481
1482    wd = elm_widget_data_get(WIDGET(ctxpopup_it));
1483    if (!wd) return;
1484
1485    if (disabled == ctxpopup_it->disabled)
1486      return;
1487
1488    if (disabled)
1489      edje_object_signal_emit(VIEW(ctxpopup_it), "elm,state,disabled", "elm");
1490    else
1491      edje_object_signal_emit(VIEW(ctxpopup_it), "elm,state,enabled", "elm");
1492
1493    ctxpopup_it->disabled = !!disabled;
1494 }
1495
1496 EAPI Eina_Bool
1497 elm_ctxpopup_item_disabled_get(const Elm_Object_Item *it)
1498 {
1499    ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
1500    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *) it;
1501    return ctxpopup_it->disabled;
1502 }
1503
1504 EAPI void
1505 elm_ctxpopup_content_set(Evas_Object *obj, Evas_Object *content)
1506 {
1507    elm_object_content_set(obj, content);
1508 }
1509
1510 EAPI Evas_Object *
1511 elm_ctxpopup_content_unset(Evas_Object *obj)
1512 {
1513    return elm_object_content_unset(obj);
1514 }
1515
1516 EAPI void
1517 elm_ctxpopup_direction_priority_set(Evas_Object *obj,
1518                                     Elm_Ctxpopup_Direction first,
1519                                     Elm_Ctxpopup_Direction second,
1520                                     Elm_Ctxpopup_Direction third,
1521                                     Elm_Ctxpopup_Direction fourth)
1522 {
1523    ELM_CHECK_WIDTYPE(obj, widtype);
1524    Widget_Data *wd;
1525
1526    wd = elm_widget_data_get(obj);
1527    if (!wd) return;
1528
1529    wd->dir_priority[0] = first;
1530    wd->dir_priority[1] = second;
1531    wd->dir_priority[2] = third;
1532    wd->dir_priority[3] = fourth;
1533
1534    if (wd->visible)
1535      _sizing_eval(obj);
1536 }
1537
1538 EAPI void
1539 elm_ctxpopup_direction_priority_get(Evas_Object *obj,
1540                                     Elm_Ctxpopup_Direction *first,
1541                                     Elm_Ctxpopup_Direction *second,
1542                                     Elm_Ctxpopup_Direction *third,
1543                                     Elm_Ctxpopup_Direction *fourth)
1544 {
1545    ELM_CHECK_WIDTYPE(obj, widtype);
1546    Widget_Data *wd;
1547
1548    wd = elm_widget_data_get(obj);
1549    if (!wd) return;
1550
1551    if (first) *first = wd->dir_priority[0];
1552    if (second) *second = wd->dir_priority[1];
1553    if (third) *third = wd->dir_priority[2];
1554    if (fourth) *fourth = wd->dir_priority[3];
1555 }
1556
1557 EAPI Elm_Ctxpopup_Direction
1558 elm_ctxpopup_direction_get(const Evas_Object *obj)
1559 {
1560    ELM_CHECK_WIDTYPE(obj, widtype) ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1561    Widget_Data *wd;
1562
1563    wd = elm_widget_data_get(obj);
1564    if (!wd) return ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1565    return wd->dir;
1566 }