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