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