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