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