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