[SLP Merge] Thu Jul 7 13:20:56 2011 +0900
[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         //TODO 1: Find the largest space direction.
411         Evas_Coord length[2];
412
413         length[0] = pos.y - hover_area.y;
414         length[1] = (hover_area.y + hover_area.h) - pos.y;
415
416         if (length[0] > length[1])
417            idx = ELM_CTXPOPUP_DIRECTION_UP;
418         else
419            idx = ELM_CTXPOPUP_DIRECTION_DOWN;
420
421         //TODO 2: determine x , y
422         switch (idx)
423           {
424            case ELM_CTXPOPUP_DIRECTION_UP:
425               _adjust_pos_x(&pos, &base_size, &hover_area);
426               pos.y -= base_size.y;
427               arrow = ELM_CTXPOPUP_DIRECTION_DOWN;
428               if (pos.y < hover_area.y + arrow_size.y)
429                 {
430                    base_size.y -= ((hover_area.y + arrow_size.y) - pos.y);
431                    pos.y = hover_area.y + arrow_size.y;
432                 }
433               break;
434            case ELM_CTXPOPUP_DIRECTION_LEFT:
435               _adjust_pos_y(&pos, &base_size, &hover_area);
436               pos.x -= base_size.x;
437               arrow = ELM_CTXPOPUP_DIRECTION_RIGHT;
438               if (pos.x < hover_area.x + arrow_size.x)
439                 {
440                    base_size.x -= ((hover_area.x + arrow_size.x) - pos.x);
441                    pos.x = hover_area.x + arrow_size.x;
442                 }
443               break;
444            case ELM_CTXPOPUP_DIRECTION_RIGHT:
445               _adjust_pos_y(&pos, &base_size, &hover_area);
446               arrow = ELM_CTXPOPUP_DIRECTION_LEFT;
447               if (pos.x + arrow_size.x + base_size.x >
448                   hover_area.x + hover_area.w)
449                  base_size.x -=
450                     ((pos.x + arrow_size.x + base_size.x) -
451                      (hover_area.x + hover_area.w));
452               break;
453            case ELM_CTXPOPUP_DIRECTION_DOWN:
454               _adjust_pos_x(&pos, &base_size, &hover_area);
455               arrow = ELM_CTXPOPUP_DIRECTION_UP;
456               if (pos.y + arrow_size.y + base_size.y >
457                   hover_area.y + hover_area.h)
458                  base_size.y -=
459                     ((pos.y + arrow_size.y + base_size.y) -
460                      (hover_area.y + hover_area.h));
461               break;
462            default:
463               break;
464           }
465      }
466
467    //Final position and size.
468    rect->x = pos.x;
469    rect->y = pos.y;
470    rect->w = base_size.x;
471    rect->h = base_size.y;
472
473    return arrow;
474 }
475
476 static void
477 _update_arrow(Evas_Object *obj, Elm_Ctxpopup_Direction dir)
478 {
479    Evas_Coord x, y;
480    Evas_Coord_Rectangle arrow_size;
481    Evas_Coord_Rectangle base_size;
482    Widget_Data *wd;
483
484    wd = elm_widget_data_get(obj);
485    if (!wd) return;
486
487    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
488    evas_object_geometry_get(wd->arrow, NULL, NULL, &arrow_size.w,
489                             &arrow_size.h);
490    evas_object_geometry_get(wd->base, &base_size.x, &base_size.y,
491                             &base_size.w, &base_size.h);
492
493    switch (dir)
494      {
495       case ELM_CTXPOPUP_DIRECTION_LEFT:
496          edje_object_signal_emit(wd->arrow, "elm,state,left", "elm");
497
498          // if user does not use dragable part
499          arrow_size.y = (y - (arrow_size.h * 0.5));
500          arrow_size.x = x;
501
502          edje_object_part_swallow(wd->base, "elm.swallow.arrow_left", wd->arrow);
503          if (base_size.h > 0)
504            {
505               if (y < (arrow_size.h * 0.5) + base_size.y) y = 0;
506               else if (y > base_size.y + base_size.h - (arrow_size.h * 0.5)) y = base_size.h - arrow_size.h;
507               else y = y - base_size.y - (arrow_size.h * 0.5);
508               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_left", 1,
509                                               (double) (y) / (double) (base_size.h - arrow_size.h));
510           }
511          break;
512       case ELM_CTXPOPUP_DIRECTION_RIGHT:
513          edje_object_signal_emit(wd->arrow, "elm,state,right", "elm");
514
515          // if user does not use dragable part
516          arrow_size.y = (y - (arrow_size.h * 0.5));
517          arrow_size.x = (x - arrow_size.w);
518
519          edje_object_part_swallow(wd->base, "elm.swallow.arrow_right", wd->arrow);
520          if (base_size.h > 0)
521             {
522               if (y < (arrow_size.h * 0.5) + base_size.y) y = 0;
523               else if (y > base_size.y + base_size.h - (arrow_size.h * 0.5)) y = base_size.h - arrow_size.h;
524               else y = y - base_size.y - (arrow_size.h * 0.5);
525               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_right", 0,
526                                               (double) (y) / (double) (base_size.h - arrow_size.h));
527             }
528          break;
529       case ELM_CTXPOPUP_DIRECTION_UP:
530          edje_object_signal_emit(wd->arrow, "elm,state,top", "elm");
531
532          // if user does not use dragable part
533          arrow_size.x = (x - (arrow_size.w * 0.5));
534          arrow_size.y = y;
535
536          edje_object_part_swallow(wd->base, "elm.swallow.arrow_up", wd->arrow);
537          if (base_size.w > 0)
538            {
539               if (x < (arrow_size.w * 0.5) + base_size.x) x = 0;
540               else if (x > base_size.x + base_size.w - (arrow_size.w * 0.5)) x = base_size.w - arrow_size.w;
541               else x = x - base_size.x - (arrow_size.w * 0.5);
542               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_up",
543                                               (double) (x) / (double) (base_size.w - arrow_size.w), 1);
544            }
545          break;
546       case ELM_CTXPOPUP_DIRECTION_DOWN:
547          edje_object_signal_emit(wd->arrow, "elm,state,bottom", "elm");
548
549          // if user does not use dragable part
550          arrow_size.x = (x - (arrow_size.w * 0.5));
551          arrow_size.y = (y - arrow_size.h);
552
553          edje_object_part_swallow(wd->base, "elm.swallow.arrow_down", wd->arrow);
554          if (base_size.w > 0)
555            {
556               if (x < (arrow_size.w * 0.5) + base_size.x) x = 0;
557               else if (x > base_size.x + base_size.w - (arrow_size.w * 0.5)) x = base_size.w - arrow_size.w;
558               else x = x - base_size.x - (arrow_size.w * 0.5);
559               edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_down",
560                                               (double) (x) / (double) (base_size.w - arrow_size.w), 0);
561            }
562          break;
563       default:
564          break;
565      }
566
567    // if user does not use dragable part
568    evas_object_move(wd->arrow, arrow_size.x, arrow_size.y);
569 }
570
571 static void
572 _sizing_eval(Evas_Object *obj)
573 {
574    Widget_Data *wd;
575    Eina_List *elist;
576    Elm_Ctxpopup_Item *item;
577    Evas_Coord_Rectangle rect = { 0, 0, 1, 1 };
578    Evas_Coord_Point box_size = { 0, 0 };
579    Evas_Coord_Point _box_size = { 0, 0 };
580
581    wd = elm_widget_data_get(obj);
582    if ((!wd) || (!wd->parent)) return;
583
584    //Box, Scroller
585    EINA_LIST_FOREACH(wd->items, elist, item)
586      {
587         _item_sizing_eval(item);
588         evas_object_size_hint_min_get(item->base.view, &_box_size.x, &_box_size.y);
589         if (!wd->horizontal)
590           {
591              if (_box_size.x > box_size.x)
592                 box_size.x = _box_size.x;
593              if (_box_size.y != -1)
594                 box_size.y += _box_size.y;
595           }
596         else
597           {
598              if (_box_size.x != -1)
599                 box_size.x += _box_size.x;
600              if (_box_size.y > box_size.y)
601                 box_size.y = _box_size.y;
602           }
603      }
604
605    if (!wd->content)
606      {
607         evas_object_size_hint_min_set(wd->box, box_size.x, box_size.y);
608         evas_object_size_hint_min_set(wd->scr, box_size.x, box_size.y);
609      }
610
611    //Base
612    wd->dir = _calc_base_geometry(obj, &rect);
613    _update_arrow(obj, wd->dir);
614    _shift_base_by_arrow(wd->arrow, wd->dir, &rect);
615
616    //resize scroller according to final size.
617    if (!wd->content)
618       evas_object_smart_calculate(wd->scr);
619
620    evas_object_move(wd->base, rect.x, rect.y);
621    evas_object_resize(wd->base, rect.w, rect.h);
622 }
623
624 static void
625 _shift_base_by_arrow(Evas_Object *arrow, Elm_Ctxpopup_Direction dir,
626                      Evas_Coord_Rectangle *rect)
627 {
628    Evas_Coord arrow_w, arrow_h;
629
630    evas_object_geometry_get(arrow, NULL, NULL, &arrow_w, &arrow_h);
631
632    switch (dir)
633      {
634       case ELM_CTXPOPUP_DIRECTION_LEFT:
635          rect->x += arrow_w;
636          break;
637       case ELM_CTXPOPUP_DIRECTION_RIGHT:
638          rect->x -= arrow_w;
639          break;
640       case ELM_CTXPOPUP_DIRECTION_UP:
641          rect->y += arrow_h;
642          break;
643       case ELM_CTXPOPUP_DIRECTION_DOWN:
644          rect->y -= arrow_h;
645          break;
646       default:
647          break;
648      }
649 }
650
651 static void
652 _del_pre_hook(Evas_Object *obj)
653 {
654    Widget_Data *wd;
655
656    wd = elm_widget_data_get(obj);
657    if (!wd) return;
658
659    evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_RESIZE,
660                                        _parent_resize, obj);
661
662    _hover_parent_callbacks_del(obj);
663 }
664
665 static void
666 _del_hook(Evas_Object *obj)
667 {
668    Widget_Data *wd;
669
670    wd = elm_widget_data_get(obj);
671    if (!wd) return;
672
673    elm_ctxpopup_clear(obj);
674    evas_object_del(wd->arrow);
675    evas_object_del(wd->base);
676    free(wd);
677 }
678
679 static void
680 _theme_hook(Evas_Object *obj)
681 {
682    Widget_Data *wd;
683    Eina_List *elist;
684    Elm_Ctxpopup_Item *item;
685
686    wd = elm_widget_data_get(obj);
687    if (!wd) return;
688
689    //Items
690    EINA_LIST_FOREACH(wd->items, elist, item)
691      {
692         if (item->label && item->icon)
693            _elm_theme_object_set(obj, item->base.view, "ctxpopup",
694                                  "icon_text_style_item",
695                                  elm_widget_style_get(obj));
696         else if (item->label)
697            _elm_theme_object_set(obj, item->base.view, "ctxpopup", "text_style_item",
698                                  elm_widget_style_get(obj));
699         else if (item->icon)
700            _elm_theme_object_set(obj, item->base.view, "ctxpopup", "icon_style_item",
701                                  elm_widget_style_get(obj));
702         if (item->label)
703            edje_object_part_text_set(item->base.view, "elm.text", item->label);
704
705         if (item->disabled)
706            edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
707
708         edje_object_message_signal_process(item->base.view);
709      }
710
711    _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg",
712                          elm_widget_style_get(obj));
713    _elm_theme_object_set(obj, wd->base, "ctxpopup", "base",
714                          elm_widget_style_get(obj));
715    _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow",
716                          elm_widget_style_get(obj));
717
718    if (wd->scr)
719      {
720         if (!strncmp(elm_object_style_get(obj), "default",
721                      strlen("default")))
722            elm_object_style_set(wd->scr, "ctxpopup");
723         else
724            elm_object_style_set(wd->scr, elm_object_style_get(obj));
725      }
726
727    if (wd->visible)
728      {
729         _scroller_size_reset(wd);
730         _sizing_eval(obj);
731      }
732 }
733
734 static void
735 _bg_clicked_cb(void *data, Evas_Object *obj __UNUSED__,
736                const char *emission __UNUSED__, const char *source __UNUSED__)
737 {
738    evas_object_hide(data);
739 }
740
741 static void
742 _parent_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
743                void *event_info __UNUSED__)
744 {
745    Evas_Coord w, h;
746    Widget_Data *wd;
747
748    wd = elm_widget_data_get(data);
749    if (!wd) return;
750
751    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
752    evas_object_resize(wd->bg, w, h);
753
754    if (!wd->visible) return;
755
756    _hide(data);
757 }
758
759 static void
760 _ctxpopup_show(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
761                void *event_info __UNUSED__)
762 {
763    Widget_Data *wd;
764    Eina_List *elist;
765    Elm_Ctxpopup_Item *item;
766    int idx = 0;
767
768    wd = elm_widget_data_get(obj);
769    if (!wd) return;
770
771    if ((!wd->items) && (!wd->content)) return;
772
773    wd->visible = EINA_TRUE;
774
775    evas_object_show(wd->bg);
776    evas_object_show(wd->base);
777    evas_object_show(wd->arrow);
778
779    edje_object_signal_emit(wd->bg, "elm,state,show", "elm");
780
781    EINA_LIST_FOREACH(wd->items, elist, item)
782      {
783        if (idx++ == 0)
784          edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
785        else
786          {
787            if(!wd->horizontal)
788              edje_object_signal_emit(item->base.view, "elm,state,vertical", "elm");
789            else
790              edje_object_signal_emit(item->base.view, "elm,state,horizontal", "elm");
791          }
792            }
793
794    _sizing_eval(obj);
795 }
796
797 static void
798 _hide(Evas_Object *obj)
799 {
800    Widget_Data *wd = elm_widget_data_get(obj);
801
802    if (!wd) return;
803
804    evas_object_hide(wd->bg);
805    evas_object_hide(wd->arrow);
806    evas_object_hide(wd->base);
807
808    _scroller_size_reset(wd);
809
810    evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
811    wd->visible = EINA_FALSE;
812 }
813
814 static void
815 _ctxpopup_hide(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
816                void *event_info __UNUSED__)
817 {
818    Widget_Data *wd;
819
820    wd = elm_widget_data_get(obj);
821    if ((!wd) || (!wd->visible))
822       return;
823
824    _hide(obj);
825 }
826
827 static void
828 _scroller_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
829                  void *event_info __UNUSED__)
830 {
831    Widget_Data *wd;
832    Evas_Coord w, h;
833
834    wd = elm_widget_data_get(data);
835    if (!wd) return;
836    if (!wd->visible) return;
837    if (wd->finished) return;
838
839    evas_object_geometry_get(obj, 0, 0, &w, &h);
840
841    if (w != 0 && h != 0)
842      {
843         if ((w <= wd->max_sc_w) && (h <= wd->max_sc_h))
844           {
845              _sizing_eval(data);
846              wd->finished = EINA_TRUE;
847              return;
848           }
849      }
850
851    if (wd->max_sc_w < w)
852       wd->max_sc_w = w;
853    if (wd->max_sc_h < h)
854       wd->max_sc_h = h;
855
856    _sizing_eval(data);
857 }
858
859 static void
860 _ctxpopup_move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
861                void *event_info __UNUSED__)
862 {
863    Widget_Data *wd;
864
865    wd = elm_widget_data_get(obj);
866
867    if (!wd) return;
868
869    if (wd->visible)
870       evas_object_show(wd->arrow);
871
872    _scroller_size_reset(wd);
873    _sizing_eval(obj);
874 }
875
876 static void
877 _item_select_cb(void *data, Evas_Object *obj __UNUSED__,
878                 const char *emission __UNUSED__, const char *source __UNUSED__)
879 {
880    Elm_Ctxpopup_Item *item = data;
881
882    if (!item) return;
883    if (item->disabled) return;
884
885    if (item->func)
886       item->func((void*) item->base.data, item->base.widget, data);
887 }
888
889 static void
890 _item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
891 {
892    if (item->icon)
893      {
894         elm_widget_sub_object_del(item->base.view, item->icon);
895         evas_object_del(item->icon);
896      }
897
898    item->icon = icon;
899    if (!icon) return;
900
901    edje_object_part_swallow(item->base.view, "elm.swallow.icon", item->icon);
902    edje_object_message_signal_process(item->base.view);
903 }
904
905 static void
906 _item_label_set(Elm_Ctxpopup_Item *item, const char *label)
907 {
908    if (!eina_stringshare_replace(&item->label, label))
909       return;
910
911    edje_object_part_text_set(item->base.view, "elm.text", label);
912    edje_object_message_signal_process(item->base.view);
913 }
914
915 static void
916 _item_new(Elm_Ctxpopup_Item *item, char *group_name)
917 {
918    Widget_Data *wd;
919
920    wd = elm_widget_data_get(item->base.widget);
921    if (!wd) return;
922
923    item->base.view = edje_object_add(evas_object_evas_get(wd->base));
924    _elm_theme_object_set(item->base.widget, item->base.view, "ctxpopup", group_name,
925                          elm_widget_style_get(item->base.widget));
926    edje_object_signal_callback_add(item->base.view, "elm,action,click", "",
927                                    _item_select_cb, item);
928    evas_object_size_hint_align_set(item->base.view, EVAS_HINT_FILL, EVAS_HINT_FILL);
929    evas_object_show(item->base.view);
930 }
931
932 static void
933 _content_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
934              void *event_info __UNUSED__)
935 {
936    elm_ctxpopup_content_unset(data);
937 }
938
939 static void
940 _list_del(Widget_Data *wd)
941 {
942    if (!wd->scr) return;
943
944    edje_object_part_unswallow(wd->base, wd->scr);
945    evas_object_del(wd->scr);
946    wd->scr = NULL;
947    wd->box = NULL;
948 }
949
950 static void
951 _list_new(Evas_Object *obj)
952 {
953    Widget_Data *wd;
954    wd = elm_widget_data_get(obj);
955    if (!wd) return;
956
957    //scroller
958    wd->scr = elm_scroller_add(obj);
959    elm_object_style_set(wd->scr, "ctxpopup");
960    evas_object_size_hint_align_set(wd->scr, EVAS_HINT_FILL, EVAS_HINT_FILL);
961    evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE,
962                                   _scroller_resize, obj);
963    edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scr);
964
965    //box
966    wd->box = elm_box_add(obj);
967    evas_object_size_hint_weight_set(wd->box, EVAS_HINT_EXPAND,
968                                     EVAS_HINT_EXPAND);
969
970    elm_scroller_content_set(wd->scr, wd->box);
971    elm_ctxpopup_horizontal_set(obj, wd->horizontal);
972 }
973
974 static void
975 _remove_items(Widget_Data *wd)
976 {
977    Eina_List *elist;
978    Elm_Ctxpopup_Item *item;
979
980    if (!wd->items) return;
981
982    EINA_LIST_FOREACH(wd->items, elist, item)
983      {
984         if (item->label)
985            eina_stringshare_del(item->label);
986         if (item->icon)
987            evas_object_del(item->icon);
988         wd->items = eina_list_remove(wd->items, item);
989         free(item);
990      }
991
992    wd->items = NULL;
993 }
994
995 /**
996  * Add a new Ctxpopup object to the parent.
997  *
998  * @param parent Parent object
999  * @return New object or @c NULL, if it cannot be created
1000  *
1001  * @ingroup Ctxpopup
1002  */
1003 EAPI Evas_Object *
1004 elm_ctxpopup_add(Evas_Object *parent)
1005 {
1006    Evas_Object *obj;
1007    Evas *e;
1008    Widget_Data *wd;
1009    Evas_Coord x, y, w, h;
1010
1011    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1012
1013    ELM_SET_WIDTYPE(widtype, "ctxpopup");
1014    elm_widget_type_set(obj, "ctxpopup");
1015    elm_widget_sub_object_add(parent, obj);
1016    elm_widget_data_set(obj, wd);
1017    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1018    elm_widget_del_hook_set(obj, _del_hook);
1019    elm_widget_theme_hook_set(obj, _theme_hook);
1020
1021    wd->parent = parent;
1022
1023    //Background
1024    wd->bg = edje_object_add(e);
1025    elm_widget_sub_object_add(obj, wd->bg);
1026    _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg", "default");
1027    evas_object_geometry_get(parent, &x, &y, &w, &h);
1028    evas_object_move(wd->bg, x, y);
1029    evas_object_resize(wd->bg, w, h);
1030    edje_object_signal_callback_add(wd->bg, "elm,action,click", "",
1031                                    _bg_clicked_cb, obj);
1032
1033    //Base
1034    wd->base = edje_object_add(e);
1035    elm_widget_sub_object_add(obj, wd->base);
1036    _elm_theme_object_set(obj, wd->base, "ctxpopup", "base", "default");
1037
1038    //Arrow
1039    wd->arrow = edje_object_add(e);
1040    elm_widget_sub_object_add(obj, wd->arrow);
1041    _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow", "default");
1042
1043    wd->dir_priority[0] = ELM_CTXPOPUP_DIRECTION_UP;
1044    wd->dir_priority[1] = ELM_CTXPOPUP_DIRECTION_LEFT;
1045    wd->dir_priority[2] = ELM_CTXPOPUP_DIRECTION_RIGHT;
1046    wd->dir_priority[3] = ELM_CTXPOPUP_DIRECTION_DOWN;
1047
1048    evas_object_event_callback_add(parent, EVAS_CALLBACK_RESIZE, _parent_resize,
1049                                   obj);
1050    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _ctxpopup_show,
1051                                   NULL);
1052    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _ctxpopup_hide,
1053                                   NULL);
1054    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _ctxpopup_move,
1055                                   NULL);
1056    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1057                                   _ctxpopup_changed_size_hints, NULL);
1058    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
1059    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
1060    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
1061    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
1062
1063    evas_object_smart_callbacks_descriptions_set(obj, _signals);
1064
1065    return obj;
1066 }
1067
1068 /**
1069  * Get the icon object for the given ctxpopup item.
1070  *
1071  * @param item Ctxpopup item
1072  * @return icon object or @c NULL, if the item does not have icon or an error occurred
1073  *
1074  * @ingroup Ctxpopup
1075  */
1076 EAPI Evas_Object *
1077 elm_ctxpopup_item_icon_get(const Elm_Ctxpopup_Item *item)
1078 {
1079    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
1080    return item->icon;
1081 }
1082
1083 /**
1084  * Sets the side icon associated with the ctxpopup item
1085  *
1086  * Once the icon object is set, a previously set one will be deleted.
1087  * You probably don't want, then, to have the <b>same</b> icon object
1088  * set for more than one item of the list (when replacing one of its
1089  * instances).
1090  *
1091  * @param item Ctxpopup item
1092  * @param icon Icon object to be set
1093  *
1094  * @ingroup Ctxpopup
1095  */
1096 EAPI void
1097 elm_ctxpopup_item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
1098 {
1099    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1100
1101    Widget_Data *wd;
1102
1103    wd = elm_widget_data_get(item->base.widget);
1104    if (!wd) return;
1105
1106    _item_icon_set(item, icon);
1107
1108    if (wd->visible)
1109      {
1110         _scroller_size_reset(wd);
1111         _sizing_eval(item->base.widget);
1112      }
1113 }
1114
1115 /**
1116  * Get the label object for the given ctxpopup item.
1117  *
1118  * @param item Ctxpopup item
1119  * @return label object or @c NULL, if the item does not have label or an error occured
1120  *
1121  * @ingroup Ctxpopup
1122  *
1123  */
1124 EAPI const char *
1125 elm_ctxpopup_item_label_get(const Elm_Ctxpopup_Item *item)
1126 {
1127    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
1128    return item->label;
1129 }
1130
1131 /**
1132  * (Re)set the label on the given ctxpopup item.
1133  *
1134  * @param item Ctxpopup item
1135  * @param label String to set as label
1136  *
1137  * @ingroup Ctxpopup
1138  */
1139 EAPI void
1140 elm_ctxpopup_item_label_set(Elm_Ctxpopup_Item *item, const char *label)
1141 {
1142    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1143
1144    Widget_Data *wd;
1145
1146    wd = elm_widget_data_get(item->base.widget);
1147    if (!wd) return;
1148
1149    _item_label_set(item, label);
1150
1151    if (wd->visible)
1152      {
1153         _scroller_size_reset(wd);
1154         _sizing_eval(item->base.widget);
1155      }
1156 }
1157
1158 /**
1159  * Set the Ctxpopup's parent
1160  * Set the parent object (it would much probably be the
1161  * window that the ctxpopup is in).
1162  *
1163  * @param obj The ctxpopup object
1164  * @param area The parent to use
1165  *
1166  * @note elm_ctxpopup_add() will automatically call this function
1167  * with its @c parent argument.
1168  *
1169  * @ingroup Ctxpopup
1170  */
1171 EAPI void
1172 elm_ctxpopup_hover_parent_set(Evas_Object *obj, Evas_Object *hover_parent)
1173 {
1174    ELM_CHECK_WIDTYPE(obj, widtype);
1175
1176    Widget_Data *wd;
1177
1178    wd = elm_widget_data_get(obj);
1179    if (!wd) return;
1180
1181    _hover_parent_callbacks_del(obj);
1182
1183    if (hover_parent)
1184      {
1185         evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_DEL,
1186                                        _hover_parent_del, obj);
1187         evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_MOVE,
1188                                        _hover_parent_move, obj);
1189         evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_RESIZE,
1190                                        _hover_parent_resize, obj);
1191      }
1192
1193    wd->hover_parent = hover_parent;
1194 }
1195
1196 /**
1197  * Get the Ctxpopup's parent
1198  *
1199  * @param obj The ctxpopup object
1200  *
1201  * @see elm_ctxpopup_hover_parent_set() for more information
1202  *
1203  * @ingroup Ctxpopup
1204  */
1205 EAPI Evas_Object *
1206 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
1207 {
1208    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1209
1210    Widget_Data *wd;
1211
1212    wd = elm_widget_data_get(obj);
1213    if (!wd) return NULL;
1214
1215    return wd->hover_parent;
1216 }
1217
1218 /**
1219  * Clear all items in the given ctxpopup object.
1220  *
1221  * @param obj Ctxpopup object
1222  *
1223  * @ingroup Ctxpopup
1224  */
1225 EAPI void
1226 elm_ctxpopup_clear(Evas_Object * obj)
1227 {
1228    ELM_CHECK_WIDTYPE(obj, widtype);
1229
1230    Widget_Data *wd = elm_widget_data_get(obj);
1231    if (!wd) return;
1232
1233    _remove_items(wd);
1234    _list_del(wd);
1235 }
1236
1237 /**
1238  * Change the ctxpopup's orientation to horizontal or vertical.
1239  *
1240  * @param obj Ctxpopup object
1241  * @param horizontal @c EINA_TRUE for horizontal mode, @c EINA_FALSE for vertical
1242  *
1243  * @ingroup Ctxpopup
1244  */
1245 EAPI void
1246 elm_ctxpopup_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
1247 {
1248    ELM_CHECK_WIDTYPE(obj, widtype);
1249
1250    Widget_Data *wd;
1251    Eina_List *elist;
1252    Elm_Ctxpopup_Item *item;
1253    int idx = 0;
1254
1255    wd = elm_widget_data_get(obj);
1256    if (!wd) return;
1257
1258    wd->horizontal = !!horizontal;
1259
1260    if ((!wd->scr) && (!wd->box))
1261       return;
1262
1263    if (!horizontal)
1264      {
1265         elm_box_horizontal_set(wd->box, EINA_FALSE);
1266         elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
1267
1268         EINA_LIST_FOREACH(wd->items, elist, item)
1269           {
1270              if (idx++ == 0)
1271                 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
1272              else
1273                 edje_object_signal_emit(item->base.view, "elm,state,vertical", "elm");
1274           }
1275      }
1276    else
1277      {
1278         elm_box_horizontal_set(wd->box, EINA_TRUE);
1279         elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
1280
1281         EINA_LIST_FOREACH(wd->items, elist, item)
1282           {
1283              if (idx++ == 0)
1284                 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
1285              else
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) return EINA_FALSE;
1311
1312    return wd->horizontal;
1313 }
1314
1315 /**
1316  * Add a new item to a ctxpopup object.
1317  *
1318  * Both a item list and a content could not be set at the same time!
1319  * once you set add a item, the previous content will be removed.
1320  *
1321  * @param obj Ctxpopup object
1322  * @param icon Icon to be set on new item
1323  * @param label The Label of the new item
1324  * @param func Convenience function called when item selected
1325  * @param data Data passed to @p func above
1326  * @return A handle to the item added or @c NULL, on errors
1327  *
1328  * @ingroup Ctxpopup
1329  */
1330 EAPI Elm_Ctxpopup_Item *
1331 elm_ctxpopup_item_append(Evas_Object *obj, const char *label,
1332                          Evas_Object *icon, Evas_Smart_Cb func,
1333                          const void *data)
1334 {
1335    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1336
1337    Widget_Data *wd;
1338    Elm_Ctxpopup_Item *item;
1339
1340    wd = elm_widget_data_get(obj);
1341    if (!wd) return NULL;
1342
1343    item = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
1344    if (!item) return NULL;
1345
1346    //The first item is appended.
1347    if (wd->content)
1348       evas_object_del(elm_ctxpopup_content_unset(obj));
1349
1350    if (!wd->items)
1351       _list_new(obj);
1352
1353    item->func = func;
1354    item->base.data = data;
1355
1356    if (icon && label)
1357       _item_new(item, "icon_text_style_item");
1358    else if (label)
1359       _item_new(item, "text_style_item");
1360    else
1361       _item_new(item, "icon_style_item");
1362
1363    _item_icon_set(item, icon);
1364    _item_label_set(item, label);
1365    elm_box_pack_end(wd->box, item->base.view);
1366    wd->items = eina_list_append(wd->items, item);
1367
1368    if (wd->visible)
1369      {
1370         _scroller_size_reset(wd);
1371         _sizing_eval(obj);
1372      }
1373
1374    return item;
1375 }
1376
1377 /**
1378  * Delete the given item in a ctxpopup object.
1379  *
1380  * @param item Ctxpopup item to be deleted
1381  *
1382  * @ingroup Ctxpopup
1383  */
1384 EAPI void
1385 elm_ctxpopup_item_del(Elm_Ctxpopup_Item *item)
1386 {
1387    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1388
1389    Widget_Data *wd;
1390
1391    wd = elm_widget_data_get(item->base.widget);
1392    if (!wd) return;
1393
1394    if (item->icon)
1395       evas_object_del(item->icon);
1396    if (item->base.view)
1397       evas_object_del(item->base.view);
1398
1399    eina_stringshare_del(item->label);
1400
1401    wd->items = eina_list_remove(wd->items, item);
1402
1403    if (eina_list_count(wd->items) < 1)
1404       wd->items = NULL;
1405
1406    if (wd->visible)
1407       _sizing_eval(item->base.widget);
1408
1409    free(item);
1410 }
1411
1412 /**
1413  * Set the ctxpopup item's state as disabled or enabled.
1414  *
1415  * @param item Ctxpopup item to be enabled/disabled
1416  * @param disabled @c EINA_TRUE to disable it, @c EINA_FALSE to enable it
1417  *
1418  * @ingroup Ctxpopup
1419  */
1420 EAPI void
1421 elm_ctxpopup_item_disabled_set(Elm_Ctxpopup_Item *item, Eina_Bool disabled)
1422 {
1423    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1424
1425    Widget_Data *wd;
1426
1427    wd = elm_widget_data_get(item->base.widget);
1428    if (!wd) return;
1429
1430    if (disabled == item->disabled)
1431       return;
1432
1433    if (disabled)
1434       edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
1435    else
1436       edje_object_signal_emit(item->base.view, "elm,state,enabled", "elm");
1437
1438    item->disabled = !!disabled;
1439 }
1440
1441 /**
1442  * Get the ctxpopup item's disabled/enabled state.
1443  *
1444  * @param item Ctxpopup item to be enabled/disabled
1445  * @return disabled @c EINA_TRUE, if disabled, @c EINA_FALSE otherwise
1446  *
1447  * @ingroup Ctxpopup
1448  */
1449 EAPI Eina_Bool
1450 elm_ctxpopup_item_disabled_get(const Elm_Ctxpopup_Item *item)
1451 {
1452    ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, EINA_FALSE);
1453    return item->disabled;
1454 }
1455
1456 /**
1457  * Once the content object is set, a previously set one will be deleted.
1458  * If you want to keep that old content object, use the
1459  * elm_ctxpopup_content_unset() function
1460  *
1461  * Both a item list and a content could not be set at the same time!
1462  * once you set a content, the previous list items will be removed.
1463  *
1464  * @param obj Ctxpopup object
1465  * @param content Content to be swallowed
1466  *
1467  * @ingroup Ctxpopup
1468  */
1469 EAPI void
1470 elm_ctxpopup_content_set(Evas_Object *obj, Evas_Object *content)
1471 {
1472    ELM_CHECK_WIDTYPE(obj, widtype);
1473
1474    Widget_Data *wd;
1475
1476    wd = elm_widget_data_get(obj);
1477    if ((!wd) || (!content))
1478       return;
1479
1480    if (wd->items)
1481       elm_ctxpopup_clear(obj);
1482
1483    if (wd->content)
1484       evas_object_del(wd->content);
1485
1486    evas_object_event_callback_add(content, EVAS_CALLBACK_DEL, _content_del,
1487                                   obj);
1488
1489    elm_widget_sub_object_add(obj, content);
1490    edje_object_part_swallow(wd->base, "elm.swallow.content", content);
1491    edje_object_message_signal_process(wd->base);
1492
1493    wd->content = content;
1494
1495    if (wd->visible)
1496       _sizing_eval(obj);
1497 }
1498
1499 /**
1500  * Unset the ctxpopup content
1501  *
1502  * Unparent and return the content object which was set for this widget
1503  *
1504  * @param obj Ctxpopup object
1505  * @return The content that was being used
1506  *
1507  * @ingroup Ctxpopup
1508  */
1509 EAPI Evas_Object *
1510 elm_ctxpopup_content_unset(Evas_Object *obj)
1511 {
1512    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1513
1514    Widget_Data *wd;
1515    Evas_Object *content;
1516
1517    wd = elm_widget_data_get(obj);
1518    if (!wd) return NULL;
1519
1520    content = wd->content;
1521    if (!content) return NULL;
1522
1523    edje_object_part_unswallow(wd->base, content);
1524    elm_widget_sub_object_del(obj, content);
1525    evas_object_event_callback_del(content, EVAS_CALLBACK_DEL, _content_del);
1526    edje_object_signal_emit(wd->base, "elm,state,content,disable", "elm");
1527
1528    wd->content = NULL;
1529
1530    return content;
1531 }
1532
1533 /**
1534  * Set the direction priority of a ctxpopup.
1535  * This functions gives a chance to user to set the priority of ctxpopup showing direction.
1536  *
1537  * @param obj Ctxpopup object
1538  * @param first 1st priority of direction
1539  * @param second 2nd priority of direction
1540  * @param third 3th priority of direction
1541  * @param fourth 4th priority of direction
1542  *
1543  * @ingroup Ctxpopup
1544  */
1545 EAPI void
1546 elm_ctxpopup_direction_priority_set(Evas_Object *obj,
1547                                     Elm_Ctxpopup_Direction first,
1548                                     Elm_Ctxpopup_Direction second,
1549                                     Elm_Ctxpopup_Direction third,
1550                                     Elm_Ctxpopup_Direction fourth)
1551 {
1552    ELM_CHECK_WIDTYPE(obj, widtype);
1553    Widget_Data *wd;
1554
1555    wd = elm_widget_data_get(obj);
1556    if (!wd) return;
1557
1558    wd->dir_priority[0] = first;
1559    wd->dir_priority[1] = second;
1560    wd->dir_priority[2] = third;
1561    wd->dir_priority[3] = fourth;
1562
1563    if (wd->visible)
1564       _sizing_eval(obj);
1565 }
1566
1567 /**
1568  * Get the direction priority of a ctxpopup.
1569  *
1570  * @param obj Ctxpopup object
1571  * @param first 1st priority of direction to be returned
1572  * @param second 2nd priority of direction to be returned
1573  * @param third 3th priority of direction to be returned
1574  * @param fourth 4th priority of direction to be returned
1575  *
1576  * @see elm_ctxpopup_direction_priority_set for more information.
1577  *
1578  * @ingroup Ctxpopup
1579  */
1580 EAPI void
1581 elm_ctxpopup_direction_priority_get(Evas_Object *obj,
1582                                     Elm_Ctxpopup_Direction *first,
1583                                     Elm_Ctxpopup_Direction *second,
1584                                     Elm_Ctxpopup_Direction *third,
1585                                     Elm_Ctxpopup_Direction *fourth)
1586 {
1587    ELM_CHECK_WIDTYPE(obj, widtype);
1588    Widget_Data *wd;
1589
1590    wd = elm_widget_data_get(obj);
1591    if (!wd) return;
1592
1593    if (first) *first = wd->dir_priority[0];
1594    if (second) *second = wd->dir_priority[1];
1595    if (third) *third = wd->dir_priority[2];
1596    if (fourth) *fourth = wd->dir_priority[3];
1597 }