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