elementary/ctxpopup - correct position and size of ctxpopup
[platform/upstream/elementary.git] / src / lib / elc_ctxpopup.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_widget_ctxpopup.h"
4
5 EAPI const char ELM_CTXPOPUP_SMART_NAME[] = "elm_ctxpopup";
6
7 static const char SIG_DISMISSED[] = "dismissed";
8 static const Evas_Smart_Cb_Description _smart_callbacks[] = {
9    {SIG_DISMISSED, ""},
10    {NULL, NULL}
11 };
12
13 EVAS_SMART_SUBCLASS_NEW
14   (ELM_CTXPOPUP_SMART_NAME, _elm_ctxpopup, Elm_Ctxpopup_Smart_Class,
15    Elm_Layout_Smart_Class, elm_layout_smart_class_get, _smart_callbacks);
16 static Eina_Bool
17 _elm_ctxpopup_smart_focus_next(const Evas_Object *obj,
18                                Elm_Focus_Direction dir,
19                                Evas_Object **next)
20 {
21    ELM_CTXPOPUP_DATA_GET(obj, sd);
22
23    if (!sd)
24      return EINA_FALSE;
25
26    if (!elm_widget_focus_next_get(sd->box, dir, next))
27      {
28         elm_widget_focused_object_clear(sd->box);
29         elm_widget_focus_next_get(sd->box, dir, next);
30      }
31
32    return EINA_TRUE;
33 }
34
35 static Eina_Bool
36 _elm_ctxpopup_smart_event(Evas_Object *obj,
37                           Evas_Object *src __UNUSED__,
38                           Evas_Callback_Type type,
39                           void *event_info)
40 {
41    Evas_Event_Key_Down *ev = event_info;
42
43    ELM_CTXPOPUP_DATA_GET(obj, sd);
44
45    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
46    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
47    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
48
49    if (!strcmp(ev->keyname, "Tab"))
50      {
51         if (evas_key_modifier_is_set(ev->modifiers, "Shift"))
52           elm_widget_focus_cycle(sd->box, ELM_FOCUS_PREVIOUS);
53         else
54           elm_widget_focus_cycle(sd->box, ELM_FOCUS_NEXT);
55         return EINA_TRUE;
56      }
57
58    if (strcmp(ev->keyname, "Escape")) return EINA_FALSE;
59
60    evas_object_hide(obj);
61    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
62    return EINA_TRUE;
63 }
64
65 static void
66 _x_pos_adjust(Evas_Coord_Point *pos,
67               Evas_Coord_Point *base_size,
68               Evas_Coord_Rectangle *hover_area)
69 {
70    pos->x -= (base_size->x / 2);
71
72    if (pos->x < hover_area->x)
73      pos->x = hover_area->x;
74    else if ((pos->x + base_size->x) > (hover_area->x + hover_area->w))
75      pos->x = (hover_area->x + hover_area->w) - base_size->x;
76
77    if (base_size->x > hover_area->w)
78      base_size->x -= (base_size->x - hover_area->w);
79
80    if (pos->x < hover_area->x)
81      pos->x = hover_area->x;
82 }
83
84 static void
85 _y_pos_adjust(Evas_Coord_Point *pos,
86               Evas_Coord_Point *base_size,
87               Evas_Coord_Rectangle *hover_area)
88 {
89    pos->y -= (base_size->y / 2);
90
91    if (pos->y < hover_area->y)
92      pos->y = hover_area->y;
93    else if ((pos->y + base_size->y) > (hover_area->y + hover_area->h))
94      pos->y = hover_area->y + hover_area->h - base_size->y;
95
96    if (base_size->y > hover_area->h)
97      base_size->y -= (base_size->y - hover_area->h);
98
99    if (pos->y < hover_area->y)
100      pos->y = hover_area->y;
101 }
102
103 static Elm_Ctxpopup_Direction
104 _base_geometry_calc(Evas_Object *obj,
105                     Evas_Coord_Rectangle *rect)
106 {
107    Elm_Ctxpopup_Direction dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
108    Evas_Coord_Rectangle hover_area;
109    Evas_Coord_Point pos = {0, 0};
110    Evas_Coord_Point arrow_size;
111    Evas_Coord_Point base_size;
112    Evas_Coord_Point max_size;
113    Evas_Coord_Point min_size;
114    Evas_Coord_Point temp;
115    int idx;
116
117    ELM_CTXPOPUP_DATA_GET(obj, sd);
118
119    if (!rect) return ELM_CTXPOPUP_DIRECTION_DOWN;
120
121    edje_object_part_geometry_get
122      (sd->arrow, "ctxpopup_arrow", NULL, NULL, &arrow_size.x, &arrow_size.y);
123    evas_object_resize(sd->arrow, arrow_size.x, arrow_size.y);
124
125    //Initialize Area Rectangle.
126    evas_object_geometry_get
127      (sd->parent, &hover_area.x, &hover_area.y, &hover_area.w,
128      &hover_area.h);
129
130    evas_object_geometry_get(obj, &pos.x, &pos.y, NULL, NULL);
131
132    //recalc the edje
133    edje_object_size_min_calc
134      (ELM_WIDGET_DATA(sd)->resize_obj, &base_size.x, &base_size.y);
135    evas_object_smart_calculate(ELM_WIDGET_DATA(sd)->resize_obj);
136
137    //Limit to Max Size
138    evas_object_size_hint_max_get(obj, &max_size.x, &max_size.y);
139
140    if ((max_size.y > 0) && (base_size.y > max_size.y))
141      base_size.y = max_size.y;
142
143    if ((max_size.x > 0) && (base_size.x > max_size.x))
144      base_size.x = max_size.x;
145
146    //Limit to Min Size
147    evas_object_size_hint_min_get(obj, &min_size.x, &min_size.y);
148
149    if ((min_size.y > 0) && (base_size.y < min_size.y))
150      base_size.y = min_size.y;
151
152    if ((min_size.x > 0) && (base_size.x < min_size.x))
153      base_size.x = min_size.x;
154
155    //Check the Which direction is available.
156    //If find a avaialble direction, it adjusts position and size.
157    for (idx = 0; idx < 4; idx++)
158      {
159         switch (sd->dir_priority[idx])
160           {
161            case ELM_CTXPOPUP_DIRECTION_UNKNOWN:
162            case ELM_CTXPOPUP_DIRECTION_UP:
163              temp.y = (pos.y - base_size.y);
164              if ((temp.y - arrow_size.y) < hover_area.y)
165                continue;
166
167              _x_pos_adjust(&pos, &base_size, &hover_area);
168              pos.y -= base_size.y;
169              dir = ELM_CTXPOPUP_DIRECTION_UP;
170              break;
171
172            case ELM_CTXPOPUP_DIRECTION_LEFT:
173              temp.x = (pos.x - base_size.x);
174              if ((temp.x - arrow_size.x) < hover_area.x)
175                continue;
176
177              _y_pos_adjust(&pos, &base_size, &hover_area);
178              pos.x -= base_size.x;
179              dir = ELM_CTXPOPUP_DIRECTION_LEFT;
180              break;
181
182            case ELM_CTXPOPUP_DIRECTION_RIGHT:
183              temp.x = (pos.x + base_size.x);
184              if ((temp.x + arrow_size.x) >
185                  (hover_area.x + hover_area.w))
186                continue;
187
188              _y_pos_adjust(&pos, &base_size, &hover_area);
189              dir = ELM_CTXPOPUP_DIRECTION_RIGHT;
190              break;
191
192            case ELM_CTXPOPUP_DIRECTION_DOWN:
193              temp.y = (pos.y + base_size.y);
194              if ((temp.y + arrow_size.y) >
195                  (hover_area.y + hover_area.h))
196                continue;
197
198              _x_pos_adjust(&pos, &base_size, &hover_area);
199              dir = ELM_CTXPOPUP_DIRECTION_DOWN;
200              break;
201
202            default:
203              continue;
204           }
205         break;
206      }
207
208    //In this case, all directions are invalid because of lack of space.
209    if (idx == 4)
210      {
211         Evas_Coord length[2];
212
213         if (!sd->horizontal)
214           {
215              length[0] = pos.y - hover_area.y;
216              length[1] = (hover_area.y + hover_area.h) - pos.y;
217
218              // ELM_CTXPOPUP_DIRECTION_UP
219              if (length[0] > length[1])
220                {
221                   _x_pos_adjust(&pos, &base_size, &hover_area);
222                   pos.y -= base_size.y;
223                   dir = ELM_CTXPOPUP_DIRECTION_UP;
224                   if (pos.y < (hover_area.y + arrow_size.y))
225                     {
226                        base_size.y -= ((hover_area.y + arrow_size.y) - pos.y);
227                        pos.y = hover_area.y + arrow_size.y;
228                     }
229                }
230              //ELM_CTXPOPUP_DIRECTION_DOWN
231              else
232                {
233                   _x_pos_adjust(&pos, &base_size, &hover_area);
234                   dir = ELM_CTXPOPUP_DIRECTION_DOWN;
235                   if ((pos.y + arrow_size.y + base_size.y) >
236                       (hover_area.y + hover_area.h))
237                     base_size.y -=
238                       ((pos.y + arrow_size.y + base_size.y) -
239                        (hover_area.y + hover_area.h));
240                }
241           }
242         else
243           {
244              length[0] = pos.x - hover_area.x;
245              length[1] = (hover_area.x + hover_area.w) - pos.x;
246
247              //ELM_CTXPOPUP_DIRECTION_LEFT
248              if (length[0] > length[1])
249                {
250                   _y_pos_adjust(&pos, &base_size, &hover_area);
251                   pos.x -= base_size.x;
252                   dir = ELM_CTXPOPUP_DIRECTION_LEFT;
253                   if (pos.x < (hover_area.x + arrow_size.x))
254                     {
255                        base_size.x -= ((hover_area.x + arrow_size.x) - pos.x);
256                        pos.x = hover_area.x + arrow_size.x;
257                     }
258                }
259              //ELM_CTXPOPUP_DIRECTION_RIGHT
260              else
261                {
262                   _y_pos_adjust(&pos, &base_size, &hover_area);
263                   dir = ELM_CTXPOPUP_DIRECTION_RIGHT;
264                   if (pos.x + (arrow_size.x + base_size.x) >
265                       hover_area.x + hover_area.w)
266                     base_size.x -=
267                       ((pos.x + arrow_size.x + base_size.x) -
268                        (hover_area.x + hover_area.w));
269                }
270           }
271      }
272
273    //Final position and size.
274    rect->x = pos.x;
275    rect->y = pos.y;
276    rect->w = base_size.x;
277    rect->h = base_size.y;
278
279    return dir;
280 }
281
282 static void
283 _arrow_update(Evas_Object *obj,
284               Elm_Ctxpopup_Direction dir,
285               Evas_Coord_Rectangle base_size)
286 {
287    Evas_Coord_Rectangle arrow_size;
288    Evas_Coord x, y;
289    double drag;
290
291    ELM_CTXPOPUP_DATA_GET(obj, sd);
292
293    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
294    evas_object_geometry_get
295      (sd->arrow, NULL, NULL, &arrow_size.w, &arrow_size.h);
296
297    /* arrow is not being kept as sub-object on purpose, here. the
298     * design of the widget does not help with the contrary */
299
300    switch (dir)
301      {
302       case ELM_CTXPOPUP_DIRECTION_RIGHT:
303         edje_object_signal_emit(sd->arrow, "elm,state,left", "elm");
304         edje_object_part_swallow
305           (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_left",
306           sd->arrow);
307         if (base_size.h > 0)
308           {
309              if (y < ((arrow_size.h * 0.5) + base_size.y))
310                y = 0;
311              else if (y > base_size.y + base_size.h - (arrow_size.h * 0.5))
312                y = base_size.h - arrow_size.h;
313              else
314                y = y - base_size.y - (arrow_size.h * 0.5);
315              drag = (double)(y) / (double)(base_size.h - arrow_size.h);
316              edje_object_part_drag_value_set
317                (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_left", 1,
318                drag);
319           }
320         break;
321
322       case ELM_CTXPOPUP_DIRECTION_LEFT:
323         edje_object_signal_emit(sd->arrow, "elm,state,right", "elm");
324         edje_object_part_swallow
325           (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_right",
326           sd->arrow);
327
328         if (base_size.h > 0)
329           {
330              if (y < ((arrow_size.h * 0.5) + base_size.y))
331                y = 0;
332              else if (y > (base_size.y + base_size.h - (arrow_size.h * 0.5)))
333                y = base_size.h - arrow_size.h;
334              else
335                y = y - base_size.y - (arrow_size.h * 0.5);
336              drag = (double)(y) / (double)(base_size.h - arrow_size.h);
337              edje_object_part_drag_value_set
338                (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_right", 0,
339                drag);
340           }
341         break;
342
343       case ELM_CTXPOPUP_DIRECTION_DOWN:
344         edje_object_signal_emit(sd->arrow, "elm,state,top", "elm");
345         edje_object_part_swallow
346           (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_up",
347           sd->arrow);
348
349         if (base_size.w > 0)
350           {
351              if (x < ((arrow_size.w * 0.5) + base_size.x))
352                x = 0;
353              else if (x > (base_size.x + base_size.w - (arrow_size.w * 0.5)))
354                x = base_size.w - arrow_size.w;
355              else
356                x = x - base_size.x - (arrow_size.w * 0.5);
357              drag = (double)(x) / (double)(base_size.w - arrow_size.w);
358              edje_object_part_drag_value_set
359                (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_up", drag,
360                1);
361           }
362         break;
363
364       case ELM_CTXPOPUP_DIRECTION_UP:
365         edje_object_signal_emit(sd->arrow, "elm,state,bottom", "elm");
366         edje_object_part_swallow
367           (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_down",
368           sd->arrow);
369
370         if (base_size.w > 0)
371           {
372              if (x < ((arrow_size.w * 0.5) + base_size.x))
373                x = 0;
374              else if (x > (base_size.x + base_size.w - (arrow_size.w * 0.5)))
375                x = base_size.w - arrow_size.w;
376              else x = x - base_size.x - (arrow_size.w * 0.5);
377              drag = (double)(x) / (double)(base_size.w - arrow_size.w);
378              edje_object_part_drag_value_set
379                (ELM_WIDGET_DATA(sd)->resize_obj, "elm.swallow.arrow_down",
380                drag, 0);
381           }
382         break;
383
384       default:
385         break;
386      }
387
388    //should be here for getting accurate geometry value
389    evas_object_smart_calculate(ELM_WIDGET_DATA(sd)->resize_obj);
390 }
391
392 static void
393 _show_signals_emit(Evas_Object *obj,
394                    Elm_Ctxpopup_Direction dir)
395 {
396    ELM_CTXPOPUP_DATA_GET(obj, sd);
397
398    if (!sd->visible) return;
399    if ((sd->list) && (!sd->list_visible)) return;
400
401    switch (dir)
402      {
403       case ELM_CTXPOPUP_DIRECTION_UP:
404         elm_layout_signal_emit(obj, "elm,state,show,up", "elm");
405         break;
406
407       case ELM_CTXPOPUP_DIRECTION_LEFT:
408         elm_layout_signal_emit(obj, "elm,state,show,left", "elm");
409         break;
410
411       case ELM_CTXPOPUP_DIRECTION_RIGHT:
412         elm_layout_signal_emit(obj, "elm,state,show,right", "elm");
413         break;
414
415       case ELM_CTXPOPUP_DIRECTION_DOWN:
416         elm_layout_signal_emit(obj, "elm,state,show,down", "elm");
417         break;
418
419       default:
420         break;
421      }
422
423    edje_object_signal_emit(sd->bg, "elm,state,show", "elm");
424    elm_layout_signal_emit(obj, "elm,state,show", "elm");
425 }
426
427 static void
428 _hide_signals_emit(Evas_Object *obj,
429                    Elm_Ctxpopup_Direction dir)
430 {
431    ELM_CTXPOPUP_DATA_GET(obj, sd);
432
433    if (!sd->visible) return;
434
435    switch (dir)
436      {
437       case ELM_CTXPOPUP_DIRECTION_UP:
438         elm_layout_signal_emit(obj, "elm,state,hide,up", "elm");
439         break;
440
441       case ELM_CTXPOPUP_DIRECTION_LEFT:
442         elm_layout_signal_emit(obj, "elm,state,hide,left", "elm");
443         break;
444
445       case ELM_CTXPOPUP_DIRECTION_RIGHT:
446         elm_layout_signal_emit(obj, "elm,state,hide,right", "elm");
447         break;
448
449       case ELM_CTXPOPUP_DIRECTION_DOWN:
450         elm_layout_signal_emit(obj, "elm,state,hide,down", "elm");
451         break;
452
453       default:
454         break;
455      }
456
457    edje_object_signal_emit(sd->bg, "elm,state,hide", "elm");
458 }
459
460 static void
461 _base_shift_by_arrow(Evas_Object *arrow,
462                      Elm_Ctxpopup_Direction dir,
463                      Evas_Coord_Rectangle *rect)
464 {
465    Evas_Coord arrow_w, arrow_h;
466
467    evas_object_geometry_get(arrow, NULL, NULL, &arrow_w, &arrow_h);
468    switch (dir)
469      {
470       case ELM_CTXPOPUP_DIRECTION_RIGHT:
471         rect->x += arrow_w;
472         break;
473
474       case ELM_CTXPOPUP_DIRECTION_LEFT:
475         rect->x -= arrow_w;
476         break;
477
478       case ELM_CTXPOPUP_DIRECTION_DOWN:
479         rect->y += arrow_h;
480         break;
481
482       case ELM_CTXPOPUP_DIRECTION_UP:
483         rect->y -= arrow_h;
484         break;
485
486       default:
487         break;
488      }
489 }
490
491 static Eina_Bool
492 _elm_ctxpopup_smart_sub_object_add(Evas_Object *obj,
493                                    Evas_Object *sobj)
494 {
495    Elm_Widget_Smart_Class *parent_parent;
496
497    parent_parent = (Elm_Widget_Smart_Class *)((Evas_Smart_Class *)
498                                               _elm_ctxpopup_parent_sc)->parent;
499
500    /* skipping layout's code, which registers size hint changing
501     * callback on sub objects. a hack to make ctxpopup live, as it is,
502     * on the new classing schema. this widget needs a total
503     * rewrite. */
504    if (!parent_parent->sub_object_add(obj, sobj))
505      return EINA_FALSE;
506
507    return EINA_TRUE;
508 }
509
510 static void
511 _elm_ctxpopup_smart_sizing_eval(Evas_Object *obj)
512 {
513    Evas_Coord_Rectangle rect = { 0, 0, 1, 1 };
514    Evas_Coord_Point list_size = { 0, 0 };
515
516    ELM_CTXPOPUP_DATA_GET(obj, sd);
517
518    if (!sd->arrow) return;  /* simple way to flag "under deletion" */
519
520    //Base
521    sd->dir = _base_geometry_calc(obj, &rect);
522
523    _arrow_update(obj, sd->dir, rect);
524
525    _base_shift_by_arrow(sd->arrow, sd->dir, &rect);
526
527    if ((sd->list) && (sd->list_visible))
528      {
529         evas_object_geometry_get(sd->list, 0, 0, &list_size.x, &list_size.y);
530         if ((list_size.x >= rect.w) || (list_size.y >= rect.h))
531           {
532              elm_list_mode_set(sd->list, ELM_LIST_COMPRESS);
533              evas_object_size_hint_min_set(sd->box, rect.w, rect.h);
534              evas_object_size_hint_min_set(obj, rect.w, rect.h);
535           }
536      }
537
538    evas_object_move(ELM_WIDGET_DATA(sd)->resize_obj, rect.x, rect.y);
539    evas_object_resize(ELM_WIDGET_DATA(sd)->resize_obj, rect.w, rect.h);
540
541    _show_signals_emit(obj, sd->dir);
542 }
543
544 static void
545 _on_parent_del(void *data,
546                Evas *e __UNUSED__,
547                Evas_Object *obj __UNUSED__,
548                void *event_info __UNUSED__)
549 {
550    evas_object_del(data);
551 }
552
553 static void
554 _on_parent_move(void *data,
555                 Evas *e __UNUSED__,
556                 Evas_Object *obj __UNUSED__,
557                 void *event_info __UNUSED__)
558 {
559    ELM_CTXPOPUP_DATA_GET(data, sd);
560
561    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
562
563    if (sd->visible) elm_layout_sizing_eval(data);
564 }
565
566 static void
567 _on_parent_resize(void *data,
568                   Evas *e __UNUSED__,
569                   Evas_Object *obj __UNUSED__,
570                   void *event_info __UNUSED__)
571 {
572    ELM_CTXPOPUP_DATA_GET(data, sd);
573
574    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
575
576    evas_object_hide(data);
577 }
578
579 static void
580 _parent_detach(Evas_Object *obj)
581 {
582    ELM_CTXPOPUP_DATA_GET(obj, sd);
583
584    if (!sd->parent) return;
585
586    evas_object_event_callback_del_full
587      (sd->parent, EVAS_CALLBACK_DEL, _on_parent_del, obj);
588    evas_object_event_callback_del_full
589      (sd->parent, EVAS_CALLBACK_MOVE, _on_parent_move, obj);
590    evas_object_event_callback_del_full
591      (sd->parent, EVAS_CALLBACK_RESIZE, _on_parent_resize, obj);
592 }
593
594 static void
595 _on_content_resized(void *data,
596                     Evas *e __UNUSED__,
597                     Evas_Object *obj __UNUSED__,
598                     void *event_info __UNUSED__)
599 {
600    ELM_CTXPOPUP_DATA_GET(data, sd);
601
602    elm_box_recalculate(sd->box);
603    elm_layout_sizing_eval(data);
604 }
605
606 //FIXME: lost the content size when theme hook is called.
607 static Eina_Bool
608 _elm_ctxpopup_smart_theme(Evas_Object *obj)
609 {
610    ELM_CTXPOPUP_DATA_GET(obj, sd);
611
612    if (!ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->theme(obj))
613      return EINA_FALSE;
614
615    elm_widget_theme_object_set
616      (obj, sd->bg, "ctxpopup", "bg", elm_widget_style_get(obj));
617    elm_widget_theme_object_set
618      (obj, sd->arrow, "ctxpopup", "arrow", elm_widget_style_get(obj));
619
620    if (sd->list)
621      {
622         if (!strncmp(elm_object_style_get(obj), "default", strlen("default")))
623           elm_object_style_set(sd->list, "ctxpopup");
624         else
625           elm_object_style_set(sd->list, elm_object_style_get(obj));
626      }
627
628    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
629
630    if (sd->visible) elm_layout_sizing_eval(obj);
631
632    return EINA_TRUE;
633 }
634
635 /* kind of a big and tricky override here: an internal box will hold
636  * the actual content. content aliases won't be of much help here */
637 static Eina_Bool
638 _elm_ctxpopup_smart_content_set(Evas_Object *obj,
639                                 const char *part,
640                                 Evas_Object *content)
641 {
642    Evas_Coord min_w = -1, min_h = -1;
643
644    ELM_CTXPOPUP_DATA_GET(obj, sd);
645
646    if ((part) && (strcmp(part, "default")))
647      return ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_set
648               (obj, part, content);
649
650    if (!content) return EINA_FALSE;
651
652    if (content == sd->content) return EINA_TRUE;
653
654    if (sd->content) evas_object_del(sd->content);
655    if (sd->content == sd->list) sd->list = NULL;
656
657    evas_object_size_hint_weight_set
658      (content, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
659    evas_object_size_hint_fill_set
660      (content, EVAS_HINT_FILL, EVAS_HINT_FILL);
661
662    /* since it's going to be a box content, not a layout's... */
663    evas_object_show(content);
664
665    evas_object_size_hint_min_get(content, &min_w, &min_h);
666    evas_object_size_hint_min_set(sd->box, min_w, min_h);
667    elm_box_pack_end(sd->box, content);
668
669    sd->content = content;
670    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
671
672    if (sd->visible) elm_layout_sizing_eval(obj);
673
674    return EINA_TRUE;
675 }
676
677 static Evas_Object *
678 _elm_ctxpopup_smart_content_get(const Evas_Object *obj,
679                                 const char *part)
680 {
681    ELM_CTXPOPUP_DATA_GET(obj, sd);
682
683    if ((part) && (strcmp(part, "default")))
684      return ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_get
685               (obj, part);
686
687    return sd->content;
688 }
689
690 static Evas_Object *
691 _elm_ctxpopup_smart_content_unset(Evas_Object *obj,
692                                   const char *part)
693 {
694    Evas_Object *content;
695
696    ELM_CTXPOPUP_DATA_GET(obj, sd);
697
698    if ((part) && (strcmp(part, "default")))
699      return ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_unset
700               (obj, part);
701
702    content = sd->content;
703    if (!content) return NULL;
704
705    sd->content = NULL;
706    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
707
708    if (sd->visible) elm_layout_sizing_eval(obj);
709
710    return content;
711 }
712
713 static void
714 _item_text_set_hook(Elm_Object_Item *it,
715                     const char *part,
716                     const char *label)
717 {
718    Elm_Ctxpopup_Item *ctxpopup_it;
719
720    if ((part) && (strcmp(part, "default"))) return;
721
722    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
723
724    ELM_CTXPOPUP_DATA_GET(WIDGET(ctxpopup_it), sd);
725
726    elm_object_item_part_text_set(ctxpopup_it->list_item, "default", label);
727    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
728
729    if (sd->visible) elm_layout_sizing_eval(WIDGET(ctxpopup_it));
730 }
731
732 static const char *
733 _item_text_get_hook(const Elm_Object_Item *it,
734                     const char *part)
735 {
736    Elm_Ctxpopup_Item *ctxpopup_it;
737
738    if (part && strcmp(part, "default")) return NULL;
739
740    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
741    return elm_object_item_part_text_get(ctxpopup_it->list_item, "default");
742 }
743
744 static void
745 _item_content_set_hook(Elm_Object_Item *it,
746                        const char *part,
747                        Evas_Object *content)
748 {
749    Elm_Ctxpopup_Item *ctxpopup_it;
750
751    if ((part) && (strcmp(part, "icon"))
752        && (strcmp(part, "start"))
753        && (strcmp(part, "end"))) return;
754
755    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
756
757    ELM_CTXPOPUP_DATA_GET(WIDGET(ctxpopup_it), sd);
758
759    if ((part) && (!strcmp(part, "end")))
760      elm_object_item_part_content_set(ctxpopup_it->list_item, "end", content);
761    else
762      elm_object_item_part_content_set
763        (ctxpopup_it->list_item, "start", content);
764
765    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
766
767    if (sd->visible) elm_layout_sizing_eval(WIDGET(ctxpopup_it));
768 }
769
770 static Evas_Object *
771 _item_content_get_hook(const Elm_Object_Item *it,
772                        const char *part)
773 {
774    Elm_Ctxpopup_Item *ctxpopup_it;
775
776    if (part && strcmp(part, "icon") && strcmp(part, "start")
777        && strcmp(part, "end")) return NULL;
778
779    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
780
781    if (part && !strcmp(part, "end"))
782      return elm_object_item_part_content_get(ctxpopup_it->list_item, "end");
783    else
784      return elm_object_item_part_content_get(ctxpopup_it->list_item, "start");
785 }
786
787 static void
788 _item_disable_hook(Elm_Object_Item *it)
789 {
790    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *)it;
791
792    elm_object_item_disabled_set
793      (ctxpopup_it->list_item, elm_widget_item_disabled_get(ctxpopup_it));
794 }
795
796 static void
797 _item_signal_emit_hook(Elm_Object_Item *it,
798                        const char *emission,
799                        const char *source)
800 {
801    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *)it;
802
803    elm_object_item_signal_emit(ctxpopup_it->list_item, emission, source);
804 }
805
806 static void
807 _bg_clicked_cb(void *data,
808                Evas_Object *obj __UNUSED__,
809                const char *emission __UNUSED__,
810                const char *source __UNUSED__)
811 {
812    ELM_CTXPOPUP_DATA_GET(data, sd);
813
814    _hide_signals_emit(data, sd->dir);
815 }
816
817 static void
818 _on_show(void *data __UNUSED__,
819          Evas *e __UNUSED__,
820          Evas_Object *obj,
821          void *event_info __UNUSED__)
822 {
823    ELM_CTXPOPUP_DATA_GET(obj, sd);
824
825    if (sd->list)
826      {
827         elm_list_go(sd->list);
828         sd->visible = EINA_TRUE;
829         elm_object_focus_set(obj, EINA_TRUE);
830         return;
831      }
832
833    if (!sd->content) return;
834
835    sd->visible = EINA_TRUE;
836
837    evas_object_show(sd->bg);
838    evas_object_show(sd->arrow);
839
840    edje_object_signal_emit(sd->bg, "elm,state,show", "elm");
841    elm_layout_signal_emit(obj, "elm,state,show", "elm");
842
843    elm_layout_sizing_eval(obj);
844
845    elm_object_focus_set(obj, EINA_TRUE);
846 }
847
848 static void
849 _on_hide(void *data __UNUSED__,
850          Evas *e __UNUSED__,
851          Evas_Object *obj,
852          void *event_info __UNUSED__)
853 {
854    ELM_CTXPOPUP_DATA_GET(obj, sd);
855
856    if (!sd->visible) return;
857
858    evas_object_hide(sd->bg);
859    evas_object_hide(sd->arrow);
860
861    sd->visible = EINA_FALSE;
862    sd->list_visible = EINA_FALSE;
863 }
864
865 static void
866 _on_move(void *data __UNUSED__,
867          Evas *e __UNUSED__,
868          Evas_Object *obj,
869          void *event_info __UNUSED__)
870 {
871    ELM_CTXPOPUP_DATA_GET(obj, sd);
872
873    if (sd->visible) evas_object_show(sd->arrow);
874
875    elm_layout_sizing_eval(obj);
876 }
877
878 static void
879 _hide_finished_cb(void *data,
880                   Evas_Object *obj __UNUSED__,
881                   const char *emission __UNUSED__,
882                   const char *source __UNUSED__)
883 {
884    evas_object_hide(data);
885    evas_object_smart_callback_call(data, SIG_DISMISSED, NULL);
886 }
887
888 static void
889 _list_resize_cb(void *data,
890                 Evas *e __UNUSED__,
891                 Evas_Object *obj __UNUSED__,
892                 void *event_info __UNUSED__)
893 {
894    ELM_CTXPOPUP_DATA_GET(data, sd);
895
896    if (!sd->visible) return;
897    if (sd->list_visible) return;
898
899    sd->list_visible = EINA_TRUE;
900
901    evas_object_show(sd->bg);
902    evas_object_show(sd->arrow);
903
904    elm_layout_sizing_eval(data);
905 }
906
907 static void
908 _ctxpopup_restack_cb(void *data __UNUSED__,
909                      Evas *e __UNUSED__,
910                      Evas_Object *obj,
911                      void *event_info __UNUSED__)
912 {
913    ELM_CTXPOPUP_DATA_GET(obj, sd);
914
915    evas_object_layer_set(sd->bg, evas_object_layer_get(obj));
916 }
917
918 static void
919 _list_del(Elm_Ctxpopup_Smart_Data *sd)
920 {
921    if (!sd->list) return;
922
923    evas_object_del(sd->list);
924    sd->list = NULL;
925 }
926
927 static Eina_Bool
928 _item_del_pre_hook(Elm_Object_Item *it)
929 {
930    Evas_Object *list;
931    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *)it;
932
933    ELM_CTXPOPUP_DATA_GET(WIDGET(ctxpopup_it), sd);
934
935    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
936
937    list = elm_object_item_widget_get(ctxpopup_it->list_item);
938
939    if (eina_list_count(elm_list_items_get(list)) < 2)
940      {
941         elm_object_item_del(ctxpopup_it->list_item);
942         evas_object_hide(WIDGET(ctxpopup_it));
943
944         return EINA_TRUE;
945      }
946
947    elm_object_item_del(ctxpopup_it->list_item);
948    if (sd->list_visible) elm_layout_sizing_eval(WIDGET(ctxpopup_it));
949
950    return EINA_TRUE;
951 }
952
953 static Eina_Bool
954 _elm_ctxpopup_smart_disable(Evas_Object *obj)
955 {
956    ELM_CTXPOPUP_DATA_GET(obj, sd);
957
958    if (!ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->disable(obj))
959      return EINA_FALSE;
960
961    elm_object_disabled_set(sd->list, elm_widget_disabled_get(obj));
962
963    return EINA_TRUE;
964 }
965
966 static void
967 _elm_ctxpopup_smart_add(Evas_Object *obj)
968 {
969    EVAS_SMART_DATA_ALLOC(obj, Elm_Ctxpopup_Smart_Data);
970
971    ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->base.add(obj);
972
973    elm_layout_theme_set(obj, "ctxpopup", "base", elm_widget_style_get(obj));
974    elm_layout_signal_callback_add
975      (obj, "elm,action,hide,finished", "", _hide_finished_cb, obj);
976
977    //Background
978    priv->bg = edje_object_add(evas_object_evas_get(obj));
979    elm_widget_theme_object_set(obj, priv->bg, "ctxpopup", "bg", "default");
980    edje_object_signal_callback_add
981      (priv->bg, "elm,action,click", "", _bg_clicked_cb, obj);
982
983    evas_object_stack_below(priv->bg, obj);
984
985    //Arrow
986    priv->arrow = edje_object_add(evas_object_evas_get(obj));
987    elm_widget_theme_object_set
988      (obj, priv->arrow, "ctxpopup", "arrow", "default");
989
990    priv->dir_priority[0] = ELM_CTXPOPUP_DIRECTION_UP;
991    priv->dir_priority[1] = ELM_CTXPOPUP_DIRECTION_LEFT;
992    priv->dir_priority[2] = ELM_CTXPOPUP_DIRECTION_RIGHT;
993    priv->dir_priority[3] = ELM_CTXPOPUP_DIRECTION_DOWN;
994    priv->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
995
996    evas_object_event_callback_add
997      (obj, EVAS_CALLBACK_RESTACK, _ctxpopup_restack_cb, obj);
998
999    priv->box = elm_box_add(obj);
1000    evas_object_size_hint_weight_set
1001      (priv->box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1002
1003    evas_object_event_callback_add
1004      (priv->box, EVAS_CALLBACK_RESIZE, _on_content_resized, obj);
1005
1006    /* box will be our content placeholder, thus the parent's version call */
1007    ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_set
1008      (obj, "elm.swallow.content", priv->box);
1009
1010    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _on_show, NULL);
1011    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _on_hide, NULL);
1012    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _on_move, NULL);
1013
1014    elm_widget_can_focus_set(obj, EINA_TRUE);
1015 }
1016
1017 static void
1018 _elm_ctxpopup_smart_del(Evas_Object *obj)
1019 {
1020    ELM_CTXPOPUP_DATA_GET(obj, sd);
1021
1022    evas_object_event_callback_del_full
1023      (sd->box, EVAS_CALLBACK_RESIZE, _on_content_resized, obj);
1024    _parent_detach(obj);
1025
1026    elm_ctxpopup_clear(obj);
1027    evas_object_del(sd->arrow);
1028    sd->arrow = NULL; /* stops _sizing_eval() from going on on deletion */
1029
1030    evas_object_del(sd->bg);
1031    sd->bg = NULL;
1032
1033    ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->base.del(obj);
1034 }
1035
1036 static void
1037 _elm_ctxpopup_smart_parent_set(Evas_Object *obj,
1038                                Evas_Object *parent)
1039 {
1040    //default parent is to be hover parent
1041    elm_ctxpopup_hover_parent_set(obj, parent);
1042 }
1043
1044 static void
1045 _elm_ctxpopup_smart_set_user(Elm_Ctxpopup_Smart_Class *sc)
1046 {
1047    ELM_WIDGET_CLASS(sc)->base.add = _elm_ctxpopup_smart_add;
1048    ELM_WIDGET_CLASS(sc)->base.del = _elm_ctxpopup_smart_del;
1049
1050    ELM_WIDGET_CLASS(sc)->parent_set = _elm_ctxpopup_smart_parent_set;
1051    ELM_WIDGET_CLASS(sc)->disable = _elm_ctxpopup_smart_disable;
1052    ELM_WIDGET_CLASS(sc)->event = _elm_ctxpopup_smart_event;
1053    ELM_WIDGET_CLASS(sc)->theme = _elm_ctxpopup_smart_theme;
1054    ELM_WIDGET_CLASS(sc)->sub_object_add = _elm_ctxpopup_smart_sub_object_add;
1055    ELM_WIDGET_CLASS(sc)->focus_next = _elm_ctxpopup_smart_focus_next;
1056    ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
1057
1058    ELM_CONTAINER_CLASS(sc)->content_get = _elm_ctxpopup_smart_content_get;
1059    ELM_CONTAINER_CLASS(sc)->content_set = _elm_ctxpopup_smart_content_set;
1060    ELM_CONTAINER_CLASS(sc)->content_unset = _elm_ctxpopup_smart_content_unset;
1061
1062    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_ctxpopup_smart_sizing_eval;
1063 }
1064
1065 EAPI const Elm_Ctxpopup_Smart_Class *
1066 elm_ctxpopup_smart_class_get(void)
1067 {
1068    static Elm_Ctxpopup_Smart_Class _sc =
1069      ELM_CTXPOPUP_SMART_CLASS_INIT_NAME_VERSION(ELM_CTXPOPUP_SMART_NAME);
1070    static const Elm_Ctxpopup_Smart_Class *class = NULL;
1071    Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
1072
1073    if (class)
1074      return class;
1075
1076    _elm_ctxpopup_smart_set(&_sc);
1077    esc->callbacks = _smart_callbacks;
1078    class = &_sc;
1079
1080    return class;
1081 }
1082
1083 EAPI Evas_Object *
1084 elm_ctxpopup_add(Evas_Object *parent)
1085 {
1086    Evas_Object *obj;
1087
1088    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
1089
1090    obj = elm_widget_add(_elm_ctxpopup_smart_class_new(), parent);
1091    if (!obj) return NULL;
1092
1093    if (!elm_widget_sub_object_add(parent, obj))
1094      ERR("could not add %p as sub object of %p", obj, parent);
1095
1096    return obj;
1097 }
1098
1099 EAPI void
1100 elm_ctxpopup_hover_parent_set(Evas_Object *obj,
1101                               Evas_Object *parent)
1102 {
1103    Evas_Coord x, y, w, h;
1104
1105    ELM_CTXPOPUP_CHECK(obj);
1106    ELM_CTXPOPUP_DATA_GET(obj, sd);
1107
1108    if (!parent) return;
1109
1110    _parent_detach(obj);
1111
1112    evas_object_event_callback_add
1113      (parent, EVAS_CALLBACK_DEL, _on_parent_del, obj);
1114    evas_object_event_callback_add
1115      (parent, EVAS_CALLBACK_MOVE, _on_parent_move, obj);
1116    evas_object_event_callback_add
1117      (parent, EVAS_CALLBACK_RESIZE, _on_parent_resize, obj);
1118
1119    sd->parent = parent;
1120
1121    //Update Background
1122    evas_object_geometry_get(parent, &x, &y, &w, &h);
1123    evas_object_move(sd->bg, x, y);
1124    evas_object_resize(sd->bg, w, h);
1125
1126    if (sd->visible) elm_layout_sizing_eval(obj);
1127 }
1128
1129 EAPI Evas_Object *
1130 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
1131 {
1132    ELM_CTXPOPUP_CHECK(obj) NULL;
1133    ELM_CTXPOPUP_DATA_GET(obj, sd);
1134
1135    return sd->parent;
1136 }
1137
1138 EAPI void
1139 elm_ctxpopup_clear(Evas_Object *obj)
1140 {
1141    ELM_CTXPOPUP_CHECK(obj);
1142    ELM_CTXPOPUP_DATA_GET(obj, sd);
1143
1144    _list_del(sd);
1145    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1146 }
1147
1148 EAPI void
1149 elm_ctxpopup_horizontal_set(Evas_Object *obj,
1150                             Eina_Bool horizontal)
1151 {
1152    ELM_CTXPOPUP_CHECK(obj);
1153    ELM_CTXPOPUP_DATA_GET(obj, sd);
1154
1155    sd->horizontal = !!horizontal;
1156
1157    if (!sd->list) return;
1158
1159    elm_list_horizontal_set(sd->list, sd->horizontal);
1160
1161    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1162
1163    if (sd->visible) elm_layout_sizing_eval(obj);
1164 }
1165
1166 EAPI Eina_Bool
1167 elm_ctxpopup_horizontal_get(const Evas_Object *obj)
1168 {
1169    ELM_CTXPOPUP_CHECK(obj) EINA_FALSE;
1170    ELM_CTXPOPUP_DATA_GET(obj, sd);
1171
1172    return sd->horizontal;
1173 }
1174
1175 EAPI Elm_Object_Item *
1176 elm_ctxpopup_item_append(Evas_Object *obj,
1177                          const char *label,
1178                          Evas_Object *icon,
1179                          Evas_Smart_Cb func,
1180                          const void *data)
1181 {
1182    Elm_Ctxpopup_Item *item;
1183
1184    ELM_CTXPOPUP_CHECK(obj) NULL;
1185    ELM_CTXPOPUP_DATA_GET(obj, sd);
1186
1187    item = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
1188    if (!item) return NULL;
1189
1190    elm_widget_item_del_pre_hook_set(item, _item_del_pre_hook);
1191    elm_widget_item_disable_hook_set(item, _item_disable_hook);
1192    elm_widget_item_text_set_hook_set(item, _item_text_set_hook);
1193    elm_widget_item_text_get_hook_set(item, _item_text_get_hook);
1194    elm_widget_item_content_set_hook_set(item, _item_content_set_hook);
1195    elm_widget_item_content_get_hook_set(item, _item_content_get_hook);
1196    elm_widget_item_signal_emit_hook_set(item, _item_signal_emit_hook);
1197
1198    if (!sd->list)
1199      {
1200         //The first item is appended.
1201         sd->list = elm_list_add(obj);
1202         elm_list_mode_set(sd->list, ELM_LIST_EXPAND);
1203         elm_list_horizontal_set(sd->list, sd->horizontal);
1204         evas_object_event_callback_add
1205           (sd->list, EVAS_CALLBACK_RESIZE, _list_resize_cb, obj);
1206         elm_layout_content_set(obj, "default", sd->list);
1207      }
1208
1209    item->list_item =
1210      elm_list_item_append(sd->list, label, icon, NULL, func, data);
1211
1212    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1213
1214    if (sd->visible) elm_layout_sizing_eval(obj);
1215
1216    return (Elm_Object_Item *)item;
1217 }
1218
1219 EAPI void
1220 elm_ctxpopup_direction_priority_set(Evas_Object *obj,
1221                                     Elm_Ctxpopup_Direction first,
1222                                     Elm_Ctxpopup_Direction second,
1223                                     Elm_Ctxpopup_Direction third,
1224                                     Elm_Ctxpopup_Direction fourth)
1225 {
1226    ELM_CTXPOPUP_CHECK(obj);
1227    ELM_CTXPOPUP_DATA_GET(obj, sd);
1228
1229    sd->dir_priority[0] = first;
1230    sd->dir_priority[1] = second;
1231    sd->dir_priority[2] = third;
1232    sd->dir_priority[3] = fourth;
1233
1234    if (sd->visible) elm_layout_sizing_eval(obj);
1235 }
1236
1237 EAPI void
1238 elm_ctxpopup_direction_priority_get(Evas_Object *obj,
1239                                     Elm_Ctxpopup_Direction *first,
1240                                     Elm_Ctxpopup_Direction *second,
1241                                     Elm_Ctxpopup_Direction *third,
1242                                     Elm_Ctxpopup_Direction *fourth)
1243 {
1244    ELM_CTXPOPUP_CHECK(obj);
1245    ELM_CTXPOPUP_DATA_GET(obj, sd);
1246
1247    if (first) *first = sd->dir_priority[0];
1248    if (second) *second = sd->dir_priority[1];
1249    if (third) *third = sd->dir_priority[2];
1250    if (fourth) *fourth = sd->dir_priority[3];
1251 }
1252
1253 EAPI Elm_Ctxpopup_Direction
1254 elm_ctxpopup_direction_get(const Evas_Object *obj)
1255 {
1256    ELM_CTXPOPUP_CHECK(obj) ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1257    ELM_CTXPOPUP_DATA_GET(obj, sd);
1258
1259    return sd->dir;
1260 }
1261
1262 EAPI void
1263 elm_ctxpopup_dismiss(Evas_Object *obj)
1264 {
1265    ELM_CTXPOPUP_CHECK(obj);
1266    ELM_CTXPOPUP_DATA_GET(obj, sd);
1267
1268    _hide_signals_emit(obj, sd->dir);
1269 }