1 #include <Elementary.h>
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).
15 * Signals that you can add callbacks for are:
17 * "dismissed" - the ctxpopup was dismissed
20 typedef struct _Widget_Data Widget_Data;
22 struct _Elm_Ctxpopup_Item
40 Evas_Object *hover_parent;
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;
50 static const char *widtype = NULL;
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);
115 static const char SIG_DISMISSED[] = "dismissed";
117 static const Evas_Smart_Cb_Description _signals[] = {
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__;
127 _freeze_on(void *data __UNUSED__, Evas_Object *obj,
128 void *event_info __UNUSED__)
130 Widget_Data *wd = elm_widget_data_get(obj);
132 if ((!wd) || (!wd->scr)) return;
134 elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_FALSE);
138 _freeze_off(void *data __UNUSED__, Evas_Object *obj,
139 void *event_info __UNUSED__)
141 Widget_Data *wd = elm_widget_data_get(obj);
143 if ((!wd) || (!wd->scr)) return;
146 elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
148 elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
152 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
154 Widget_Data *wd = elm_widget_data_get(obj);
156 if ((!wd) || (!wd->scr)) return;
158 elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_FALSE);
162 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
164 Widget_Data *wd = elm_widget_data_get(obj);
166 if ((!wd) || (!wd->scr)) return;
169 elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
171 elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
175 _scroller_size_reset(Widget_Data *wd)
177 wd->finished = EINA_FALSE;
183 _hover_parent_callbacks_del(Evas_Object *obj)
185 Widget_Data *wd = elm_widget_data_get(obj);
187 if ((!wd) || (!wd->hover_parent))
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);
199 _hover_parent_resize(void *data, Evas *e __UNUSED__,
200 Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
202 Widget_Data *wd = elm_widget_data_get(data);
208 _scroller_size_reset(wd);
214 _hover_parent_move(void *data, Evas *e __UNUSED__,
215 Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
217 Widget_Data *wd = elm_widget_data_get(data);
223 _scroller_size_reset(wd);
229 _hover_parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
230 void *event_info __UNUSED__)
232 Widget_Data *wd = elm_widget_data_get(data);
236 wd->hover_parent = NULL;
240 _item_sizing_eval(Elm_Ctxpopup_Item *item)
242 Evas_Coord min_w = -1, min_h = -1, max_w = -1, max_h = -1;
246 edje_object_size_min_restricted_calc(item->base.view, &min_w, &min_h, min_w,
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);
253 _adjust_pos_x(Evas_Coord_Point *pos, Evas_Coord_Point *base_size,
254 Evas_Coord_Rectangle *hover_area)
256 pos->x -= (base_size->x / 2);
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;
263 if (base_size->x > hover_area->w)
264 base_size->x -= (base_size->x - hover_area->w);
266 if (pos->x < hover_area->x)
267 pos->x = hover_area->x;
271 _adjust_pos_y(Evas_Coord_Point *pos, Evas_Coord_Point *base_size,
272 Evas_Coord_Rectangle *hover_area)
274 pos->y -= (base_size->y / 2);
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;
281 if (base_size->y > hover_area->h)
282 base_size->y -= (base_size->y - hover_area->h);
284 if (pos->y < hover_area->y)
285 pos->y = hover_area->y;
289 _ctxpopup_changed_size_hints(void *data __UNUSED__, Evas *e __UNUSED__,
290 Evas_Object *obj, void *event_info __UNUSED__)
294 wd = elm_widget_data_get(obj);
301 static Elm_Ctxpopup_Direction
302 _calc_base_geometry(Evas_Object *obj, Evas_Coord_Rectangle *rect)
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;
316 wd = elm_widget_data_get(obj);
318 if ((!wd) || (!rect))
319 return ELM_CTXPOPUP_DIRECTION_DOWN;
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);
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);
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;
339 evas_object_geometry_get(obj, &pos.x, &pos.y, NULL, NULL);
342 edje_object_size_min_calc(wd->base, &base_size.x, &base_size.y);
343 evas_object_smart_calculate(wd->base);
346 evas_object_size_hint_max_get(obj, &max_size.x, &max_size.y);
348 if ((max_size.y > 0) && (base_size.y > max_size.y))
349 base_size.y = max_size.y;
351 if ((max_size.x > 0) && (base_size.x > max_size.x))
352 base_size.x = max_size.x;
355 evas_object_size_hint_min_get(obj, &min_size.x, &min_size.y);
357 if ((min_size.y > 0) && (base_size.y < min_size.y))
358 base_size.y = min_size.y;
360 if ((min_size.x > 0) && (base_size.x < min_size.x))
361 base_size.x = min_size.x;
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++)
367 switch (wd->dir_priority[idx])
369 case ELM_CTXPOPUP_DIRECTION_UP:
370 temp.y = (pos.y - base_size.y);
371 if ((temp.y - arrow_size.y) < hover_area.y)
373 _adjust_pos_x(&pos, &base_size, &hover_area);
374 pos.y -= base_size.y;
375 arrow = ELM_CTXPOPUP_DIRECTION_DOWN;
377 case ELM_CTXPOPUP_DIRECTION_LEFT:
378 temp.x = (pos.x - base_size.x);
379 if ((temp.x - arrow_size.x) < hover_area.x)
381 _adjust_pos_y(&pos, &base_size, &hover_area);
382 pos.x -= base_size.x;
383 arrow = ELM_CTXPOPUP_DIRECTION_RIGHT;
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))
390 _adjust_pos_y(&pos, &base_size, &hover_area);
391 arrow = ELM_CTXPOPUP_DIRECTION_LEFT;
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))
398 _adjust_pos_x(&pos, &base_size, &hover_area);
399 arrow = ELM_CTXPOPUP_DIRECTION_UP;
407 //In this case, all directions are invalid because of lack of space.
410 //TODO 1: Find the largest space direction.
411 Evas_Coord length[2];
413 length[0] = pos.y - hover_area.y;
414 length[1] = (hover_area.y + hover_area.h) - pos.y;
416 if (length[0] > length[1])
417 idx = ELM_CTXPOPUP_DIRECTION_UP;
419 idx = ELM_CTXPOPUP_DIRECTION_DOWN;
421 //TODO 2: determine x , y
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)
430 base_size.y -= ((hover_area.y + arrow_size.y) - pos.y);
431 pos.y = hover_area.y + arrow_size.y;
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)
440 base_size.x -= ((hover_area.x + arrow_size.x) - pos.x);
441 pos.x = hover_area.x + arrow_size.x;
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)
450 ((pos.x + arrow_size.x + base_size.x) -
451 (hover_area.x + hover_area.w));
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)
459 ((pos.y + arrow_size.y + base_size.y) -
460 (hover_area.y + hover_area.h));
467 //Final position and size.
470 rect->w = base_size.x;
471 rect->h = base_size.y;
477 _update_arrow(Evas_Object *obj, Elm_Ctxpopup_Direction dir)
480 Evas_Coord_Rectangle arrow_size;
481 Evas_Coord_Rectangle base_size;
484 wd = elm_widget_data_get(obj);
487 evas_object_geometry_get(obj, &x, &y, NULL, NULL);
488 evas_object_geometry_get(wd->arrow, NULL, NULL, &arrow_size.w,
490 evas_object_geometry_get(wd->base, &base_size.x, &base_size.y,
491 &base_size.w, &base_size.h);
495 case ELM_CTXPOPUP_DIRECTION_LEFT:
496 edje_object_signal_emit(wd->arrow, "elm,state,left", "elm");
498 // if user does not use dragable part
499 arrow_size.y = (y - (arrow_size.h * 0.5));
502 edje_object_part_swallow(wd->base, "elm.swallow.arrow_left", wd->arrow);
505 if (y < ((arrow_size.h * 0.5) + base_size.y))
507 else if (y > base_size.y + base_size.h - (arrow_size.h * 0.5))
508 y = base_size.h - arrow_size.h;
510 y = y - base_size.y - (arrow_size.h * 0.5);
511 edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_left", 1,
512 (double) (y) / (double) (base_size.h - arrow_size.h));
515 case ELM_CTXPOPUP_DIRECTION_RIGHT:
516 edje_object_signal_emit(wd->arrow, "elm,state,right", "elm");
518 // if user does not use dragable part
519 arrow_size.y = (y - (arrow_size.h * 0.5));
520 arrow_size.x = (x - arrow_size.w);
522 edje_object_part_swallow(wd->base, "elm.swallow.arrow_right", wd->arrow);
525 if (y < (arrow_size.h * 0.5) + base_size.y)
527 else if (y > (base_size.y + base_size.h - (arrow_size.h * 0.5)))
528 y = base_size.h - arrow_size.h;
529 else y = y - base_size.y - (arrow_size.h * 0.5);
530 edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_right", 0,
531 (double) (y) / (double) (base_size.h - arrow_size.h));
534 case ELM_CTXPOPUP_DIRECTION_UP:
535 edje_object_signal_emit(wd->arrow, "elm,state,top", "elm");
537 // if user does not use dragable part
538 arrow_size.x = (x - (arrow_size.w * 0.5));
541 edje_object_part_swallow(wd->base, "elm.swallow.arrow_up", wd->arrow);
544 if (x < (arrow_size.w * 0.5) + base_size.x)
546 else if (x > (base_size.x + base_size.w - (arrow_size.w * 0.5)))
547 x = base_size.w - arrow_size.w;
549 x = x - base_size.x - (arrow_size.w * 0.5);
550 edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_up",
551 (double) (x) / (double) (base_size.w - arrow_size.w), 1);
554 case ELM_CTXPOPUP_DIRECTION_DOWN:
555 edje_object_signal_emit(wd->arrow, "elm,state,bottom", "elm");
557 // if user does not use dragable part
558 arrow_size.x = (x - (arrow_size.w * 0.5));
559 arrow_size.y = (y - arrow_size.h);
561 edje_object_part_swallow(wd->base, "elm.swallow.arrow_down", wd->arrow);
564 if (x < (arrow_size.w * 0.5) + base_size.x)
566 else if (x > (base_size.x + base_size.w - (arrow_size.w * 0.5)))
567 x = base_size.w - arrow_size.w;
568 else x = x - base_size.x - (arrow_size.w * 0.5);
569 edje_object_part_drag_value_set(wd->base, "elm.swallow.arrow_down",
570 (double) (x) / (double) (base_size.w - arrow_size.w), 0);
577 // if user does not use dragable part
578 evas_object_move(wd->arrow, arrow_size.x, arrow_size.y);
582 _sizing_eval(Evas_Object *obj)
586 Elm_Ctxpopup_Item *item;
587 Evas_Coord_Rectangle rect = { 0, 0, 1, 1 };
588 Evas_Coord_Point box_size = { 0, 0 };
589 Evas_Coord_Point _box_size = { 0, 0 };
591 wd = elm_widget_data_get(obj);
592 if ((!wd) || (!wd->parent)) return;
595 EINA_LIST_FOREACH(wd->items, elist, item)
597 _item_sizing_eval(item);
598 evas_object_size_hint_min_get(item->base.view, &_box_size.x, &_box_size.y);
601 if (_box_size.x > box_size.x)
602 box_size.x = _box_size.x;
603 if (_box_size.y != -1)
604 box_size.y += _box_size.y;
608 if (_box_size.x != -1)
609 box_size.x += _box_size.x;
610 if (_box_size.y > box_size.y)
611 box_size.y = _box_size.y;
617 evas_object_size_hint_min_set(wd->box, box_size.x, box_size.y);
618 evas_object_size_hint_min_set(wd->scr, box_size.x, box_size.y);
622 wd->dir = _calc_base_geometry(obj, &rect);
623 _update_arrow(obj, wd->dir);
624 _shift_base_by_arrow(wd->arrow, wd->dir, &rect);
626 //resize scroller according to final size.
628 evas_object_smart_calculate(wd->scr);
630 evas_object_move(wd->base, rect.x, rect.y);
631 evas_object_resize(wd->base, rect.w, rect.h);
635 _shift_base_by_arrow(Evas_Object *arrow, Elm_Ctxpopup_Direction dir,
636 Evas_Coord_Rectangle *rect)
638 Evas_Coord arrow_w, arrow_h;
640 evas_object_geometry_get(arrow, NULL, NULL, &arrow_w, &arrow_h);
644 case ELM_CTXPOPUP_DIRECTION_LEFT:
647 case ELM_CTXPOPUP_DIRECTION_RIGHT:
650 case ELM_CTXPOPUP_DIRECTION_UP:
653 case ELM_CTXPOPUP_DIRECTION_DOWN:
662 _del_pre_hook(Evas_Object *obj)
666 wd = elm_widget_data_get(obj);
669 evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_RESIZE,
670 _parent_resize, obj);
672 _hover_parent_callbacks_del(obj);
676 _del_hook(Evas_Object *obj)
680 wd = elm_widget_data_get(obj);
683 elm_ctxpopup_clear(obj);
684 evas_object_del(wd->arrow);
685 evas_object_del(wd->base);
690 _theme_hook(Evas_Object *obj)
694 Elm_Ctxpopup_Item *item;
696 wd = elm_widget_data_get(obj);
700 EINA_LIST_FOREACH(wd->items, elist, item)
702 if (item->label && item->icon)
703 _elm_theme_object_set(obj, item->base.view, "ctxpopup",
704 "icon_text_style_item",
705 elm_widget_style_get(obj));
706 else if (item->label)
707 _elm_theme_object_set(obj, item->base.view, "ctxpopup", "text_style_item",
708 elm_widget_style_get(obj));
710 _elm_theme_object_set(obj, item->base.view, "ctxpopup", "icon_style_item",
711 elm_widget_style_get(obj));
713 edje_object_part_text_set(item->base.view, "elm.text", item->label);
716 edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
718 edje_object_message_signal_process(item->base.view);
721 _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg",
722 elm_widget_style_get(obj));
723 _elm_theme_object_set(obj, wd->base, "ctxpopup", "base",
724 elm_widget_style_get(obj));
725 _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow",
726 elm_widget_style_get(obj));
730 if (!strncmp(elm_object_style_get(obj), "default",
732 elm_object_style_set(wd->scr, "ctxpopup");
734 elm_object_style_set(wd->scr, elm_object_style_get(obj));
739 _scroller_size_reset(wd);
745 _bg_clicked_cb(void *data, Evas_Object *obj __UNUSED__,
746 const char *emission __UNUSED__, const char *source __UNUSED__)
748 evas_object_hide(data);
752 _parent_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
753 void *event_info __UNUSED__)
758 wd = elm_widget_data_get(data);
761 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
762 evas_object_resize(wd->bg, w, h);
764 if (!wd->visible) return;
770 _ctxpopup_show(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
771 void *event_info __UNUSED__)
775 Elm_Ctxpopup_Item *item;
778 wd = elm_widget_data_get(obj);
781 if ((!wd->items) && (!wd->content)) return;
783 wd->visible = EINA_TRUE;
785 evas_object_show(wd->bg);
786 evas_object_show(wd->base);
787 evas_object_show(wd->arrow);
789 edje_object_signal_emit(wd->bg, "elm,state,show", "elm");
791 EINA_LIST_FOREACH(wd->items, elist, item)
794 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
798 edje_object_signal_emit(item->base.view, "elm,state,vertical", "elm");
800 edje_object_signal_emit(item->base.view, "elm,state,horizontal", "elm");
808 _hide(Evas_Object *obj)
810 Widget_Data *wd = elm_widget_data_get(obj);
814 evas_object_hide(wd->bg);
815 evas_object_hide(wd->arrow);
816 evas_object_hide(wd->base);
818 _scroller_size_reset(wd);
820 evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
821 wd->visible = EINA_FALSE;
825 _ctxpopup_hide(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
826 void *event_info __UNUSED__)
830 wd = elm_widget_data_get(obj);
831 if ((!wd) || (!wd->visible))
838 _scroller_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj,
839 void *event_info __UNUSED__)
844 wd = elm_widget_data_get(data);
846 if (!wd->visible) return;
847 if (wd->finished) return;
849 evas_object_geometry_get(obj, 0, 0, &w, &h);
851 if (w != 0 && h != 0)
853 if ((w <= wd->max_sc_w) && (h <= wd->max_sc_h))
856 wd->finished = EINA_TRUE;
861 if (wd->max_sc_w < w)
863 if (wd->max_sc_h < h)
870 _ctxpopup_move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
871 void *event_info __UNUSED__)
875 wd = elm_widget_data_get(obj);
880 evas_object_show(wd->arrow);
882 _scroller_size_reset(wd);
887 _item_select_cb(void *data, Evas_Object *obj __UNUSED__,
888 const char *emission __UNUSED__, const char *source __UNUSED__)
890 Elm_Ctxpopup_Item *item = data;
893 if (item->disabled) return;
896 item->func((void*) item->base.data, item->base.widget, data);
900 _item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
904 elm_widget_sub_object_del(item->base.view, item->icon);
905 evas_object_del(item->icon);
911 edje_object_part_swallow(item->base.view, "elm.swallow.icon", item->icon);
912 edje_object_message_signal_process(item->base.view);
916 _item_label_set(Elm_Ctxpopup_Item *item, const char *label)
918 if (!eina_stringshare_replace(&item->label, label))
921 edje_object_part_text_set(item->base.view, "elm.text", label);
922 edje_object_message_signal_process(item->base.view);
926 _item_new(Elm_Ctxpopup_Item *item, char *group_name)
930 wd = elm_widget_data_get(item->base.widget);
933 item->base.view = edje_object_add(evas_object_evas_get(wd->base));
934 _elm_theme_object_set(item->base.widget, item->base.view, "ctxpopup", group_name,
935 elm_widget_style_get(item->base.widget));
936 edje_object_signal_callback_add(item->base.view, "elm,action,click", "",
937 _item_select_cb, item);
938 evas_object_size_hint_align_set(item->base.view, EVAS_HINT_FILL, EVAS_HINT_FILL);
939 evas_object_show(item->base.view);
943 _content_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
944 void *event_info __UNUSED__)
946 elm_ctxpopup_content_unset(data);
950 _list_del(Widget_Data *wd)
952 if (!wd->scr) return;
954 edje_object_part_unswallow(wd->base, wd->scr);
955 evas_object_del(wd->scr);
961 _list_new(Evas_Object *obj)
964 wd = elm_widget_data_get(obj);
968 wd->scr = elm_scroller_add(obj);
969 elm_object_style_set(wd->scr, "ctxpopup");
970 evas_object_size_hint_align_set(wd->scr, EVAS_HINT_FILL, EVAS_HINT_FILL);
971 evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE,
972 _scroller_resize, obj);
973 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scr);
976 wd->box = elm_box_add(obj);
977 evas_object_size_hint_weight_set(wd->box, EVAS_HINT_EXPAND,
980 elm_scroller_content_set(wd->scr, wd->box);
981 elm_ctxpopup_horizontal_set(obj, wd->horizontal);
985 _remove_items(Widget_Data *wd)
988 Elm_Ctxpopup_Item *item;
990 if (!wd->items) return;
992 EINA_LIST_FOREACH(wd->items, elist, item)
995 eina_stringshare_del(item->label);
997 evas_object_del(item->icon);
998 wd->items = eina_list_remove(wd->items, item);
1006 * Add a new Ctxpopup object to the parent.
1008 * @param parent Parent object
1009 * @return New object or @c NULL, if it cannot be created
1014 elm_ctxpopup_add(Evas_Object *parent)
1019 Evas_Coord x, y, w, h;
1021 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1023 ELM_SET_WIDTYPE(widtype, "ctxpopup");
1024 elm_widget_type_set(obj, "ctxpopup");
1025 elm_widget_sub_object_add(parent, obj);
1026 elm_widget_data_set(obj, wd);
1027 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
1028 elm_widget_del_hook_set(obj, _del_hook);
1029 elm_widget_theme_hook_set(obj, _theme_hook);
1031 wd->parent = parent;
1034 wd->bg = edje_object_add(e);
1035 elm_widget_sub_object_add(obj, wd->bg);
1036 _elm_theme_object_set(obj, wd->bg, "ctxpopup", "bg", "default");
1037 evas_object_geometry_get(parent, &x, &y, &w, &h);
1038 evas_object_move(wd->bg, x, y);
1039 evas_object_resize(wd->bg, w, h);
1040 edje_object_signal_callback_add(wd->bg, "elm,action,click", "",
1041 _bg_clicked_cb, obj);
1044 wd->base = edje_object_add(e);
1045 elm_widget_sub_object_add(obj, wd->base);
1046 _elm_theme_object_set(obj, wd->base, "ctxpopup", "base", "default");
1049 wd->arrow = edje_object_add(e);
1050 elm_widget_sub_object_add(obj, wd->arrow);
1051 _elm_theme_object_set(obj, wd->arrow, "ctxpopup", "arrow", "default");
1053 wd->dir_priority[0] = ELM_CTXPOPUP_DIRECTION_UP;
1054 wd->dir_priority[1] = ELM_CTXPOPUP_DIRECTION_LEFT;
1055 wd->dir_priority[2] = ELM_CTXPOPUP_DIRECTION_RIGHT;
1056 wd->dir_priority[3] = ELM_CTXPOPUP_DIRECTION_DOWN;
1058 evas_object_event_callback_add(parent, EVAS_CALLBACK_RESIZE, _parent_resize,
1060 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _ctxpopup_show,
1062 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _ctxpopup_hide,
1064 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _ctxpopup_move,
1066 evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
1067 _ctxpopup_changed_size_hints, NULL);
1068 evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
1069 evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
1070 evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
1071 evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
1073 evas_object_smart_callbacks_descriptions_set(obj, _signals);
1079 * Get the icon object for the given ctxpopup item.
1081 * @param item Ctxpopup item
1082 * @return icon object or @c NULL, if the item does not have icon or an error occurred
1087 elm_ctxpopup_item_icon_get(const Elm_Ctxpopup_Item *item)
1089 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
1094 * Sets the side icon associated with the ctxpopup item
1096 * Once the icon object is set, a previously set one will be deleted.
1097 * You probably don't want, then, to have the <b>same</b> icon object
1098 * set for more than one item of the list (when replacing one of its
1101 * @param item Ctxpopup item
1102 * @param icon Icon object to be set
1107 elm_ctxpopup_item_icon_set(Elm_Ctxpopup_Item *item, Evas_Object *icon)
1109 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1113 wd = elm_widget_data_get(item->base.widget);
1116 _item_icon_set(item, icon);
1120 _scroller_size_reset(wd);
1121 _sizing_eval(item->base.widget);
1126 * Get the label object for the given ctxpopup item.
1128 * @param item Ctxpopup item
1129 * @return label object or @c NULL, if the item does not have label or an error occured
1135 elm_ctxpopup_item_label_get(const Elm_Ctxpopup_Item *item)
1137 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
1142 * (Re)set the label on the given ctxpopup item.
1144 * @param item Ctxpopup item
1145 * @param label String to set as label
1150 elm_ctxpopup_item_label_set(Elm_Ctxpopup_Item *item, const char *label)
1152 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1156 wd = elm_widget_data_get(item->base.widget);
1159 _item_label_set(item, label);
1163 _scroller_size_reset(wd);
1164 _sizing_eval(item->base.widget);
1169 * Set the Ctxpopup's parent
1170 * Set the parent object (it would much probably be the
1171 * window that the ctxpopup is in).
1173 * @param obj The ctxpopup object
1174 * @param area The parent to use
1176 * @note elm_ctxpopup_add() will automatically call this function
1177 * with its @c parent argument.
1182 elm_ctxpopup_hover_parent_set(Evas_Object *obj, Evas_Object *hover_parent)
1184 ELM_CHECK_WIDTYPE(obj, widtype);
1188 wd = elm_widget_data_get(obj);
1191 _hover_parent_callbacks_del(obj);
1195 evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_DEL,
1196 _hover_parent_del, obj);
1197 evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_MOVE,
1198 _hover_parent_move, obj);
1199 evas_object_event_callback_add(hover_parent, EVAS_CALLBACK_RESIZE,
1200 _hover_parent_resize, obj);
1203 wd->hover_parent = hover_parent;
1207 * Get the Ctxpopup's parent
1209 * @param obj The ctxpopup object
1211 * @see elm_ctxpopup_hover_parent_set() for more information
1216 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
1218 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1222 wd = elm_widget_data_get(obj);
1223 if (!wd) return NULL;
1225 return wd->hover_parent;
1229 * Clear all items in the given ctxpopup object.
1231 * @param obj Ctxpopup object
1236 elm_ctxpopup_clear(Evas_Object * obj)
1238 ELM_CHECK_WIDTYPE(obj, widtype);
1240 Widget_Data *wd = elm_widget_data_get(obj);
1248 * Change the ctxpopup's orientation to horizontal or vertical.
1250 * @param obj Ctxpopup object
1251 * @param horizontal @c EINA_TRUE for horizontal mode, @c EINA_FALSE for vertical
1256 elm_ctxpopup_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
1258 ELM_CHECK_WIDTYPE(obj, widtype);
1262 Elm_Ctxpopup_Item *item;
1265 wd = elm_widget_data_get(obj);
1268 wd->horizontal = !!horizontal;
1270 if ((!wd->scr) && (!wd->box))
1275 elm_box_horizontal_set(wd->box, EINA_FALSE);
1276 elm_scroller_bounce_set(wd->scr, EINA_FALSE, EINA_TRUE);
1278 EINA_LIST_FOREACH(wd->items, elist, item)
1281 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
1283 edje_object_signal_emit(item->base.view, "elm,state,vertical", "elm");
1288 elm_box_horizontal_set(wd->box, EINA_TRUE);
1289 elm_scroller_bounce_set(wd->scr, EINA_TRUE, EINA_FALSE);
1291 EINA_LIST_FOREACH(wd->items, elist, item)
1294 edje_object_signal_emit(item->base.view, "elm,state,default", "elm");
1296 edje_object_signal_emit(item->base.view, "elm,state,horizontal", "elm");
1305 * Get the value of current ctxpopup object's orientation.
1307 * @param obj Ctxpopup object
1308 * @return @c EINA_TRUE for horizontal mode, @c EINA_FALSE for vertical mode (or errors)
1313 elm_ctxpopup_horizontal_get(const Evas_Object *obj)
1315 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1319 wd = elm_widget_data_get(obj);
1320 if (!wd) return EINA_FALSE;
1322 return wd->horizontal;
1326 * Add a new item to a ctxpopup object.
1328 * Both a item list and a content could not be set at the same time!
1329 * once you set add a item, the previous content will be removed.
1331 * @param obj Ctxpopup object
1332 * @param icon Icon to be set on new item
1333 * @param label The Label of the new item
1334 * @param func Convenience function called when item selected
1335 * @param data Data passed to @p func above
1336 * @return A handle to the item added or @c NULL, on errors
1340 EAPI Elm_Ctxpopup_Item *
1341 elm_ctxpopup_item_append(Evas_Object *obj, const char *label,
1342 Evas_Object *icon, Evas_Smart_Cb func,
1345 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1348 Elm_Ctxpopup_Item *item;
1350 wd = elm_widget_data_get(obj);
1351 if (!wd) return NULL;
1353 item = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
1354 if (!item) return NULL;
1356 //The first item is appended.
1358 evas_object_del(elm_ctxpopup_content_unset(obj));
1364 item->base.data = data;
1367 _item_new(item, "icon_text_style_item");
1369 _item_new(item, "text_style_item");
1371 _item_new(item, "icon_style_item");
1373 _item_icon_set(item, icon);
1374 _item_label_set(item, label);
1375 elm_box_pack_end(wd->box, item->base.view);
1376 wd->items = eina_list_append(wd->items, item);
1380 _scroller_size_reset(wd);
1388 * Delete the given item in a ctxpopup object.
1390 * @param item Ctxpopup item to be deleted
1395 elm_ctxpopup_item_del(Elm_Ctxpopup_Item *item)
1397 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1401 wd = elm_widget_data_get(item->base.widget);
1405 evas_object_del(item->icon);
1406 if (item->base.view)
1407 evas_object_del(item->base.view);
1409 eina_stringshare_del(item->label);
1411 wd->items = eina_list_remove(wd->items, item);
1413 if (eina_list_count(wd->items) < 1)
1417 _sizing_eval(item->base.widget);
1423 * Set the ctxpopup item's state as disabled or enabled.
1425 * @param item Ctxpopup item to be enabled/disabled
1426 * @param disabled @c EINA_TRUE to disable it, @c EINA_FALSE to enable it
1431 elm_ctxpopup_item_disabled_set(Elm_Ctxpopup_Item *item, Eina_Bool disabled)
1433 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
1437 wd = elm_widget_data_get(item->base.widget);
1440 if (disabled == item->disabled)
1444 edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
1446 edje_object_signal_emit(item->base.view, "elm,state,enabled", "elm");
1448 item->disabled = !!disabled;
1452 * Get the ctxpopup item's disabled/enabled state.
1454 * @param item Ctxpopup item to be enabled/disabled
1455 * @return disabled @c EINA_TRUE, if disabled, @c EINA_FALSE otherwise
1460 elm_ctxpopup_item_disabled_get(const Elm_Ctxpopup_Item *item)
1462 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, EINA_FALSE);
1463 return item->disabled;
1467 * Once the content object is set, a previously set one will be deleted.
1468 * If you want to keep that old content object, use the
1469 * elm_ctxpopup_content_unset() function
1471 * Both a item list and a content could not be set at the same time!
1472 * once you set a content, the previous list items will be removed.
1474 * @param obj Ctxpopup object
1475 * @param content Content to be swallowed
1480 elm_ctxpopup_content_set(Evas_Object *obj, Evas_Object *content)
1482 ELM_CHECK_WIDTYPE(obj, widtype);
1486 wd = elm_widget_data_get(obj);
1487 if ((!wd) || (!content))
1491 elm_ctxpopup_clear(obj);
1494 evas_object_del(wd->content);
1496 evas_object_event_callback_add(content, EVAS_CALLBACK_DEL, _content_del,
1499 elm_widget_sub_object_add(obj, content);
1500 edje_object_part_swallow(wd->base, "elm.swallow.content", content);
1501 edje_object_message_signal_process(wd->base);
1503 wd->content = content;
1510 * Unset the ctxpopup content
1512 * Unparent and return the content object which was set for this widget
1514 * @param obj Ctxpopup object
1515 * @return The content that was being used
1520 elm_ctxpopup_content_unset(Evas_Object *obj)
1522 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1525 Evas_Object *content;
1527 wd = elm_widget_data_get(obj);
1528 if (!wd) return NULL;
1530 content = wd->content;
1531 if (!content) return NULL;
1533 edje_object_part_unswallow(wd->base, content);
1534 elm_widget_sub_object_del(obj, content);
1535 evas_object_event_callback_del(content, EVAS_CALLBACK_DEL, _content_del);
1536 edje_object_signal_emit(wd->base, "elm,state,content,disable", "elm");
1544 * Set the direction priority of a ctxpopup.
1545 * This functions gives a chance to user to set the priority of ctxpopup showing direction.
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
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)
1562 ELM_CHECK_WIDTYPE(obj, widtype);
1565 wd = elm_widget_data_get(obj);
1568 wd->dir_priority[0] = first;
1569 wd->dir_priority[1] = second;
1570 wd->dir_priority[2] = third;
1571 wd->dir_priority[3] = fourth;
1578 * Get the direction priority of a ctxpopup.
1580 * @param obj Ctxpopup object
1581 * @param first 1st priority of direction to be returned
1582 * @param second 2nd priority of direction to be returned
1583 * @param third 3th priority of direction to be returned
1584 * @param fourth 4th priority of direction to be returned
1586 * @see elm_ctxpopup_direction_priority_set for more information.
1591 elm_ctxpopup_direction_priority_get(Evas_Object *obj,
1592 Elm_Ctxpopup_Direction *first,
1593 Elm_Ctxpopup_Direction *second,
1594 Elm_Ctxpopup_Direction *third,
1595 Elm_Ctxpopup_Direction *fourth)
1597 ELM_CHECK_WIDTYPE(obj, widtype);
1600 wd = elm_widget_data_get(obj);
1603 if (first) *first = wd->dir_priority[0];
1604 if (second) *second = wd->dir_priority[1];
1605 if (third) *third = wd->dir_priority[2];
1606 if (fourth) *fourth = wd->dir_priority[3];