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