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