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