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