elementary: Fixed elc_ctxpopup arrow for 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, "elm,state,show,left", "elm");
413         break;
414
415       case ELM_CTXPOPUP_DIRECTION_RIGHT:
416         elm_layout_signal_emit(obj, "elm,state,show,right", "elm");
417         break;
418
419       case ELM_CTXPOPUP_DIRECTION_DOWN:
420         elm_layout_signal_emit(obj, "elm,state,show,down", "elm");
421         break;
422
423       default:
424         break;
425      }
426
427    edje_object_signal_emit(sd->bg, "elm,state,show", "elm");
428    elm_layout_signal_emit(obj, "elm,state,show", "elm");
429 }
430
431 static void
432 _hide_signals_emit(Evas_Object *obj,
433                    Elm_Ctxpopup_Direction dir)
434 {
435    ELM_CTXPOPUP_DATA_GET(obj, sd);
436
437    if (!sd->visible) return;
438
439    switch (dir)
440      {
441       case ELM_CTXPOPUP_DIRECTION_UP:
442         elm_layout_signal_emit(obj, "elm,state,hide,up", "elm");
443         break;
444
445       case ELM_CTXPOPUP_DIRECTION_LEFT:
446         elm_layout_signal_emit(obj, "elm,state,hide,left", "elm");
447         break;
448
449       case ELM_CTXPOPUP_DIRECTION_RIGHT:
450         elm_layout_signal_emit(obj, "elm,state,hide,right", "elm");
451         break;
452
453       case ELM_CTXPOPUP_DIRECTION_DOWN:
454         elm_layout_signal_emit(obj, "elm,state,hide,down", "elm");
455         break;
456
457       default:
458         break;
459      }
460
461    edje_object_signal_emit(sd->bg, "elm,state,hide", "elm");
462 }
463
464 static void
465 _base_shift_by_arrow(Evas_Object *arrow,
466                      Elm_Ctxpopup_Direction dir,
467                      Evas_Coord_Rectangle *rect)
468 {
469    Evas_Coord arrow_w, arrow_h;
470
471    evas_object_geometry_get(arrow, NULL, NULL, &arrow_w, &arrow_h);
472    switch (dir)
473      {
474       case ELM_CTXPOPUP_DIRECTION_RIGHT:
475         rect->x += arrow_w;
476         break;
477
478       case ELM_CTXPOPUP_DIRECTION_LEFT:
479         rect->x -= arrow_w;
480         break;
481
482       case ELM_CTXPOPUP_DIRECTION_DOWN:
483         rect->y += arrow_h;
484         break;
485
486       case ELM_CTXPOPUP_DIRECTION_UP:
487         rect->y -= arrow_h;
488         break;
489
490       default:
491         break;
492      }
493 }
494
495 static Eina_Bool
496 _elm_ctxpopup_smart_sub_object_add(Evas_Object *obj,
497                                    Evas_Object *sobj)
498 {
499    Elm_Widget_Smart_Class *parent_parent;
500
501    parent_parent = (Elm_Widget_Smart_Class *)((Evas_Smart_Class *)
502                                               _elm_ctxpopup_parent_sc)->parent;
503
504    /* skipping layout's code, which registers size hint changing
505     * callback on sub objects. a hack to make ctxpopup live, as it is,
506     * on the new classing schema. this widget needs a total
507     * rewrite. */
508    if (!parent_parent->sub_object_add(obj, sobj))
509      return EINA_FALSE;
510
511    return EINA_TRUE;
512 }
513
514 static void
515 _elm_ctxpopup_smart_sizing_eval(Evas_Object *obj)
516 {
517    Evas_Coord_Rectangle rect = { 0, 0, 1, 1 };
518    Evas_Coord_Point list_size = { 0, 0 };
519
520    ELM_CTXPOPUP_DATA_GET(obj, sd);
521
522    if (!sd->arrow) return;  /* simple way to flag "under deletion" */
523
524    //Base
525    sd->dir = _base_geometry_calc(obj, &rect);
526
527    _arrow_update(obj, sd->dir, rect);
528
529    _base_shift_by_arrow(sd->arrow, sd->dir, &rect);
530
531    if ((sd->list) && (sd->list_visible))
532      {
533         evas_object_geometry_get(sd->list, 0, 0, &list_size.x, &list_size.y);
534         if ((list_size.x >= rect.w) || (list_size.y >= rect.h))
535           {
536              elm_list_mode_set(sd->list, ELM_LIST_COMPRESS);
537              evas_object_size_hint_min_set(sd->box, rect.w, rect.h);
538              evas_object_size_hint_min_set(obj, rect.w, rect.h);
539           }
540      }
541
542    evas_object_move(ELM_WIDGET_DATA(sd)->resize_obj, rect.x, rect.y);
543    evas_object_resize(ELM_WIDGET_DATA(sd)->resize_obj, rect.w, rect.h);
544
545    _show_signals_emit(obj, sd->dir);
546 }
547
548 static void
549 _on_parent_del(void *data,
550                Evas *e __UNUSED__,
551                Evas_Object *obj __UNUSED__,
552                void *event_info __UNUSED__)
553 {
554    evas_object_del(data);
555 }
556
557 static void
558 _on_parent_move(void *data,
559                 Evas *e __UNUSED__,
560                 Evas_Object *obj __UNUSED__,
561                 void *event_info __UNUSED__)
562 {
563    ELM_CTXPOPUP_DATA_GET(data, sd);
564
565    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
566
567    if (sd->visible) elm_layout_sizing_eval(data);
568 }
569
570 static void
571 _on_parent_resize(void *data,
572                   Evas *e __UNUSED__,
573                   Evas_Object *obj __UNUSED__,
574                   void *event_info __UNUSED__)
575 {
576    ELM_CTXPOPUP_DATA_GET(data, sd);
577
578    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
579
580    evas_object_hide(data);
581 }
582
583 static void
584 _parent_detach(Evas_Object *obj)
585 {
586    ELM_CTXPOPUP_DATA_GET(obj, sd);
587
588    if (!sd->parent) return;
589
590    evas_object_event_callback_del_full
591      (sd->parent, EVAS_CALLBACK_DEL, _on_parent_del, obj);
592    evas_object_event_callback_del_full
593      (sd->parent, EVAS_CALLBACK_MOVE, _on_parent_move, obj);
594    evas_object_event_callback_del_full
595      (sd->parent, EVAS_CALLBACK_RESIZE, _on_parent_resize, obj);
596 }
597
598 static void
599 _on_content_resized(void *data,
600                     Evas *e __UNUSED__,
601                     Evas_Object *obj __UNUSED__,
602                     void *event_info __UNUSED__)
603 {
604    ELM_CTXPOPUP_DATA_GET(data, sd);
605
606    elm_box_recalculate(sd->box);
607    elm_layout_sizing_eval(data);
608 }
609
610 //FIXME: lost the content size when theme hook is called.
611 static Eina_Bool
612 _elm_ctxpopup_smart_theme(Evas_Object *obj)
613 {
614    ELM_CTXPOPUP_DATA_GET(obj, sd);
615
616    if (!ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->theme(obj))
617      return EINA_FALSE;
618
619    elm_widget_theme_object_set
620      (obj, sd->bg, "ctxpopup", "bg", elm_widget_style_get(obj));
621    elm_widget_theme_object_set
622      (obj, sd->arrow, "ctxpopup", "arrow", elm_widget_style_get(obj));
623
624    if (sd->list)
625      {
626         if (!strncmp(elm_object_style_get(obj), "default", strlen("default")))
627           elm_object_style_set(sd->list, "ctxpopup");
628         else
629           elm_object_style_set(sd->list, elm_object_style_get(obj));
630      }
631
632    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
633
634    if (sd->visible) elm_layout_sizing_eval(obj);
635
636    return EINA_TRUE;
637 }
638
639 /* kind of a big and tricky override here: an internal box will hold
640  * the actual content. content aliases won't be of much help here */
641 static Eina_Bool
642 _elm_ctxpopup_smart_content_set(Evas_Object *obj,
643                                 const char *part,
644                                 Evas_Object *content)
645 {
646    Evas_Coord min_w = -1, min_h = -1;
647
648    ELM_CTXPOPUP_DATA_GET(obj, sd);
649
650    if ((part) && (strcmp(part, "default")))
651      return ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_set
652               (obj, part, content);
653
654    if (!content) return EINA_FALSE;
655
656    if (content == sd->content) return EINA_TRUE;
657
658    if (sd->content) evas_object_del(sd->content);
659    if (sd->content == sd->list) sd->list = NULL;
660
661    evas_object_size_hint_weight_set
662      (content, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
663    evas_object_size_hint_fill_set
664      (content, EVAS_HINT_FILL, EVAS_HINT_FILL);
665
666    /* since it's going to be a box content, not a layout's... */
667    evas_object_show(content);
668
669    evas_object_size_hint_min_get(content, &min_w, &min_h);
670    evas_object_size_hint_min_set(sd->box, min_w, min_h);
671    elm_box_pack_end(sd->box, content);
672
673    sd->content = content;
674    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
675
676    if (sd->visible) elm_layout_sizing_eval(obj);
677
678    return EINA_TRUE;
679 }
680
681 static Evas_Object *
682 _elm_ctxpopup_smart_content_get(const Evas_Object *obj,
683                                 const char *part)
684 {
685    ELM_CTXPOPUP_DATA_GET(obj, sd);
686
687    if ((part) && (strcmp(part, "default")))
688      return ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_get
689               (obj, part);
690
691    return sd->content;
692 }
693
694 static Evas_Object *
695 _elm_ctxpopup_smart_content_unset(Evas_Object *obj,
696                                   const char *part)
697 {
698    Evas_Object *content;
699
700    ELM_CTXPOPUP_DATA_GET(obj, sd);
701
702    if ((part) && (strcmp(part, "default")))
703      return ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_unset
704               (obj, part);
705
706    content = sd->content;
707    if (!content) return NULL;
708
709    sd->content = NULL;
710    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
711
712    if (sd->visible) elm_layout_sizing_eval(obj);
713
714    return content;
715 }
716
717 static void
718 _item_text_set_hook(Elm_Object_Item *it,
719                     const char *part,
720                     const char *label)
721 {
722    Elm_Ctxpopup_Item *ctxpopup_it;
723
724    if ((part) && (strcmp(part, "default"))) return;
725
726    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
727
728    ELM_CTXPOPUP_DATA_GET(WIDGET(ctxpopup_it), sd);
729
730    elm_object_item_part_text_set(ctxpopup_it->list_item, "default", label);
731    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
732
733    if (sd->visible) elm_layout_sizing_eval(WIDGET(ctxpopup_it));
734 }
735
736 static const char *
737 _item_text_get_hook(const Elm_Object_Item *it,
738                     const char *part)
739 {
740    Elm_Ctxpopup_Item *ctxpopup_it;
741
742    if (part && strcmp(part, "default")) return NULL;
743
744    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
745    return elm_object_item_part_text_get(ctxpopup_it->list_item, "default");
746 }
747
748 static void
749 _item_content_set_hook(Elm_Object_Item *it,
750                        const char *part,
751                        Evas_Object *content)
752 {
753    Elm_Ctxpopup_Item *ctxpopup_it;
754
755    if ((part) && (strcmp(part, "icon"))
756        && (strcmp(part, "start"))
757        && (strcmp(part, "end"))) return;
758
759    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
760
761    ELM_CTXPOPUP_DATA_GET(WIDGET(ctxpopup_it), sd);
762
763    if ((part) && (!strcmp(part, "end")))
764      elm_object_item_part_content_set(ctxpopup_it->list_item, "end", content);
765    else
766      elm_object_item_part_content_set
767        (ctxpopup_it->list_item, "start", content);
768
769    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
770
771    if (sd->visible) elm_layout_sizing_eval(WIDGET(ctxpopup_it));
772 }
773
774 static Evas_Object *
775 _item_content_get_hook(const Elm_Object_Item *it,
776                        const char *part)
777 {
778    Elm_Ctxpopup_Item *ctxpopup_it;
779
780    if (part && strcmp(part, "icon") && strcmp(part, "start")
781        && strcmp(part, "end")) return NULL;
782
783    ctxpopup_it = (Elm_Ctxpopup_Item *)it;
784
785    if (part && !strcmp(part, "end"))
786      return elm_object_item_part_content_get(ctxpopup_it->list_item, "end");
787    else
788      return elm_object_item_part_content_get(ctxpopup_it->list_item, "start");
789 }
790
791 static void
792 _item_disable_hook(Elm_Object_Item *it)
793 {
794    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *)it;
795
796    elm_object_item_disabled_set
797      (ctxpopup_it->list_item, elm_widget_item_disabled_get(ctxpopup_it));
798 }
799
800 static void
801 _item_signal_emit_hook(Elm_Object_Item *it,
802                        const char *emission,
803                        const char *source)
804 {
805    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *)it;
806
807    elm_object_item_signal_emit(ctxpopup_it->list_item, emission, source);
808 }
809
810 static void
811 _bg_clicked_cb(void *data,
812                Evas_Object *obj __UNUSED__,
813                const char *emission __UNUSED__,
814                const char *source __UNUSED__)
815 {
816    ELM_CTXPOPUP_DATA_GET(data, sd);
817
818    _hide_signals_emit(data, sd->dir);
819 }
820
821 static void
822 _on_show(void *data __UNUSED__,
823          Evas *e __UNUSED__,
824          Evas_Object *obj,
825          void *event_info __UNUSED__)
826 {
827    ELM_CTXPOPUP_DATA_GET(obj, sd);
828
829    if (sd->list)
830      {
831         elm_list_go(sd->list);
832         sd->visible = EINA_TRUE;
833         elm_object_focus_set(obj, EINA_TRUE);
834         return;
835      }
836
837    if (!sd->content) return;
838
839    sd->visible = EINA_TRUE;
840
841    evas_object_show(sd->bg);
842    evas_object_show(sd->arrow);
843
844    edje_object_signal_emit(sd->bg, "elm,state,show", "elm");
845    elm_layout_signal_emit(obj, "elm,state,show", "elm");
846
847    elm_layout_sizing_eval(obj);
848
849    elm_object_focus_set(obj, EINA_TRUE);
850 }
851
852 static void
853 _on_hide(void *data __UNUSED__,
854          Evas *e __UNUSED__,
855          Evas_Object *obj,
856          void *event_info __UNUSED__)
857 {
858    ELM_CTXPOPUP_DATA_GET(obj, sd);
859
860    if (!sd->visible) return;
861
862    evas_object_hide(sd->bg);
863    evas_object_hide(sd->arrow);
864
865    sd->visible = EINA_FALSE;
866    sd->list_visible = EINA_FALSE;
867 }
868
869 static void
870 _on_move(void *data __UNUSED__,
871          Evas *e __UNUSED__,
872          Evas_Object *obj,
873          void *event_info __UNUSED__)
874 {
875    ELM_CTXPOPUP_DATA_GET(obj, sd);
876
877    if (sd->visible) evas_object_show(sd->arrow);
878
879    elm_layout_sizing_eval(obj);
880 }
881
882 static void
883 _hide_finished_cb(void *data,
884                   Evas_Object *obj __UNUSED__,
885                   const char *emission __UNUSED__,
886                   const char *source __UNUSED__)
887 {
888    evas_object_hide(data);
889    evas_object_smart_callback_call(data, SIG_DISMISSED, NULL);
890 }
891
892 static void
893 _list_resize_cb(void *data,
894                 Evas *e __UNUSED__,
895                 Evas_Object *obj __UNUSED__,
896                 void *event_info __UNUSED__)
897 {
898    ELM_CTXPOPUP_DATA_GET(data, sd);
899
900    if (!sd->visible) return;
901    if (sd->list_visible) return;
902
903    sd->list_visible = EINA_TRUE;
904
905    evas_object_show(sd->bg);
906    evas_object_show(sd->arrow);
907
908    elm_layout_sizing_eval(data);
909 }
910
911 static void
912 _ctxpopup_restack_cb(void *data __UNUSED__,
913                      Evas *e __UNUSED__,
914                      Evas_Object *obj,
915                      void *event_info __UNUSED__)
916 {
917    ELM_CTXPOPUP_DATA_GET(obj, sd);
918
919    evas_object_layer_set(sd->bg, evas_object_layer_get(obj));
920 }
921
922 static void
923 _list_del(Elm_Ctxpopup_Smart_Data *sd)
924 {
925    if (!sd->list) return;
926
927    evas_object_del(sd->list);
928    sd->list = NULL;
929 }
930
931 static Eina_Bool
932 _item_del_pre_hook(Elm_Object_Item *it)
933 {
934    Evas_Object *list;
935    Elm_Ctxpopup_Item *ctxpopup_it = (Elm_Ctxpopup_Item *)it;
936
937    ELM_CTXPOPUP_DATA_GET(WIDGET(ctxpopup_it), sd);
938
939    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
940
941    list = elm_object_item_widget_get(ctxpopup_it->list_item);
942
943    if (eina_list_count(elm_list_items_get(list)) < 2)
944      {
945         elm_object_item_del(ctxpopup_it->list_item);
946         evas_object_hide(WIDGET(ctxpopup_it));
947
948         return EINA_TRUE;
949      }
950
951    elm_object_item_del(ctxpopup_it->list_item);
952    if (sd->list_visible) elm_layout_sizing_eval(WIDGET(ctxpopup_it));
953
954    return EINA_TRUE;
955 }
956
957 static Eina_Bool
958 _elm_ctxpopup_smart_disable(Evas_Object *obj)
959 {
960    ELM_CTXPOPUP_DATA_GET(obj, sd);
961
962    if (!ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->disable(obj))
963      return EINA_FALSE;
964
965    elm_object_disabled_set(sd->list, elm_widget_disabled_get(obj));
966
967    return EINA_TRUE;
968 }
969
970 static void
971 _elm_ctxpopup_smart_add(Evas_Object *obj)
972 {
973    EVAS_SMART_DATA_ALLOC(obj, Elm_Ctxpopup_Smart_Data);
974
975    ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->base.add(obj);
976
977    elm_layout_theme_set(obj, "ctxpopup", "base", elm_widget_style_get(obj));
978    elm_layout_signal_callback_add
979      (obj, "elm,action,hide,finished", "", _hide_finished_cb, obj);
980
981    //Background
982    priv->bg = edje_object_add(evas_object_evas_get(obj));
983    elm_widget_theme_object_set(obj, priv->bg, "ctxpopup", "bg", "default");
984    edje_object_signal_callback_add
985      (priv->bg, "elm,action,click", "", _bg_clicked_cb, obj);
986
987    evas_object_stack_below(priv->bg, obj);
988
989    //Arrow
990    priv->arrow = edje_object_add(evas_object_evas_get(obj));
991    elm_widget_theme_object_set
992      (obj, priv->arrow, "ctxpopup", "arrow", "default");
993
994    priv->dir_priority[0] = ELM_CTXPOPUP_DIRECTION_UP;
995    priv->dir_priority[1] = ELM_CTXPOPUP_DIRECTION_LEFT;
996    priv->dir_priority[2] = ELM_CTXPOPUP_DIRECTION_RIGHT;
997    priv->dir_priority[3] = ELM_CTXPOPUP_DIRECTION_DOWN;
998    priv->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
999
1000    evas_object_event_callback_add
1001      (obj, EVAS_CALLBACK_RESTACK, _ctxpopup_restack_cb, obj);
1002
1003    priv->box = elm_box_add(obj);
1004    evas_object_size_hint_weight_set
1005      (priv->box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1006
1007    evas_object_event_callback_add
1008      (priv->box, EVAS_CALLBACK_RESIZE, _on_content_resized, obj);
1009
1010    /* box will be our content placeholder, thus the parent's version call */
1011    ELM_CONTAINER_CLASS(_elm_ctxpopup_parent_sc)->content_set
1012      (obj, "elm.swallow.content", priv->box);
1013
1014    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _on_show, NULL);
1015    evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _on_hide, NULL);
1016    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _on_move, NULL);
1017
1018    elm_widget_can_focus_set(obj, EINA_TRUE);
1019 }
1020
1021 static void
1022 _elm_ctxpopup_smart_del(Evas_Object *obj)
1023 {
1024    ELM_CTXPOPUP_DATA_GET(obj, sd);
1025
1026    evas_object_event_callback_del_full
1027      (sd->box, EVAS_CALLBACK_RESIZE, _on_content_resized, obj);
1028    _parent_detach(obj);
1029
1030    elm_ctxpopup_clear(obj);
1031    evas_object_del(sd->arrow);
1032    sd->arrow = NULL; /* stops _sizing_eval() from going on on deletion */
1033
1034    evas_object_del(sd->bg);
1035    sd->bg = NULL;
1036
1037    ELM_WIDGET_CLASS(_elm_ctxpopup_parent_sc)->base.del(obj);
1038 }
1039
1040 static void
1041 _elm_ctxpopup_smart_parent_set(Evas_Object *obj,
1042                                Evas_Object *parent)
1043 {
1044    //default parent is to be hover parent
1045    elm_ctxpopup_hover_parent_set(obj, parent);
1046 }
1047
1048 static void
1049 _elm_ctxpopup_smart_set_user(Elm_Ctxpopup_Smart_Class *sc)
1050 {
1051    ELM_WIDGET_CLASS(sc)->base.add = _elm_ctxpopup_smart_add;
1052    ELM_WIDGET_CLASS(sc)->base.del = _elm_ctxpopup_smart_del;
1053
1054    ELM_WIDGET_CLASS(sc)->parent_set = _elm_ctxpopup_smart_parent_set;
1055    ELM_WIDGET_CLASS(sc)->disable = _elm_ctxpopup_smart_disable;
1056    ELM_WIDGET_CLASS(sc)->event = _elm_ctxpopup_smart_event;
1057    ELM_WIDGET_CLASS(sc)->theme = _elm_ctxpopup_smart_theme;
1058    ELM_WIDGET_CLASS(sc)->sub_object_add = _elm_ctxpopup_smart_sub_object_add;
1059    ELM_WIDGET_CLASS(sc)->focus_next = _elm_ctxpopup_smart_focus_next;
1060    ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
1061
1062    ELM_CONTAINER_CLASS(sc)->content_get = _elm_ctxpopup_smart_content_get;
1063    ELM_CONTAINER_CLASS(sc)->content_set = _elm_ctxpopup_smart_content_set;
1064    ELM_CONTAINER_CLASS(sc)->content_unset = _elm_ctxpopup_smart_content_unset;
1065
1066    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_ctxpopup_smart_sizing_eval;
1067 }
1068
1069 EAPI const Elm_Ctxpopup_Smart_Class *
1070 elm_ctxpopup_smart_class_get(void)
1071 {
1072    static Elm_Ctxpopup_Smart_Class _sc =
1073      ELM_CTXPOPUP_SMART_CLASS_INIT_NAME_VERSION(ELM_CTXPOPUP_SMART_NAME);
1074    static const Elm_Ctxpopup_Smart_Class *class = NULL;
1075    Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
1076
1077    if (class)
1078      return class;
1079
1080    _elm_ctxpopup_smart_set(&_sc);
1081    esc->callbacks = _smart_callbacks;
1082    class = &_sc;
1083
1084    return class;
1085 }
1086
1087 EAPI Evas_Object *
1088 elm_ctxpopup_add(Evas_Object *parent)
1089 {
1090    Evas_Object *obj;
1091
1092    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
1093
1094    obj = elm_widget_add(_elm_ctxpopup_smart_class_new(), parent);
1095    if (!obj) return NULL;
1096
1097    if (!elm_widget_sub_object_add(parent, obj))
1098      ERR("could not add %p as sub object of %p", obj, parent);
1099
1100    return obj;
1101 }
1102
1103 EAPI void
1104 elm_ctxpopup_hover_parent_set(Evas_Object *obj,
1105                               Evas_Object *parent)
1106 {
1107    Evas_Coord x, y, w, h;
1108
1109    ELM_CTXPOPUP_CHECK(obj);
1110    ELM_CTXPOPUP_DATA_GET(obj, sd);
1111
1112    if (!parent) return;
1113
1114    _parent_detach(obj);
1115
1116    evas_object_event_callback_add
1117      (parent, EVAS_CALLBACK_DEL, _on_parent_del, obj);
1118    evas_object_event_callback_add
1119      (parent, EVAS_CALLBACK_MOVE, _on_parent_move, obj);
1120    evas_object_event_callback_add
1121      (parent, EVAS_CALLBACK_RESIZE, _on_parent_resize, obj);
1122
1123    sd->parent = parent;
1124
1125    //Update Background
1126    evas_object_geometry_get(parent, &x, &y, &w, &h);
1127    evas_object_move(sd->bg, x, y);
1128    evas_object_resize(sd->bg, w, h);
1129
1130    if (sd->visible) elm_layout_sizing_eval(obj);
1131 }
1132
1133 EAPI Evas_Object *
1134 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
1135 {
1136    ELM_CTXPOPUP_CHECK(obj) NULL;
1137    ELM_CTXPOPUP_DATA_GET(obj, sd);
1138
1139    return sd->parent;
1140 }
1141
1142 EAPI void
1143 elm_ctxpopup_clear(Evas_Object *obj)
1144 {
1145    ELM_CTXPOPUP_CHECK(obj);
1146    ELM_CTXPOPUP_DATA_GET(obj, sd);
1147
1148    _list_del(sd);
1149    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1150 }
1151
1152 EAPI void
1153 elm_ctxpopup_horizontal_set(Evas_Object *obj,
1154                             Eina_Bool horizontal)
1155 {
1156    ELM_CTXPOPUP_CHECK(obj);
1157    ELM_CTXPOPUP_DATA_GET(obj, sd);
1158
1159    sd->horizontal = !!horizontal;
1160
1161    if (!sd->list) return;
1162
1163    elm_list_horizontal_set(sd->list, sd->horizontal);
1164
1165    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1166
1167    if (sd->visible) elm_layout_sizing_eval(obj);
1168 }
1169
1170 EAPI Eina_Bool
1171 elm_ctxpopup_horizontal_get(const Evas_Object *obj)
1172 {
1173    ELM_CTXPOPUP_CHECK(obj) EINA_FALSE;
1174    ELM_CTXPOPUP_DATA_GET(obj, sd);
1175
1176    return sd->horizontal;
1177 }
1178
1179 EAPI Elm_Object_Item *
1180 elm_ctxpopup_item_append(Evas_Object *obj,
1181                          const char *label,
1182                          Evas_Object *icon,
1183                          Evas_Smart_Cb func,
1184                          const void *data)
1185 {
1186    Elm_Ctxpopup_Item *item;
1187
1188    ELM_CTXPOPUP_CHECK(obj) NULL;
1189    ELM_CTXPOPUP_DATA_GET(obj, sd);
1190
1191    item = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
1192    if (!item) return NULL;
1193
1194    elm_widget_item_del_pre_hook_set(item, _item_del_pre_hook);
1195    elm_widget_item_disable_hook_set(item, _item_disable_hook);
1196    elm_widget_item_text_set_hook_set(item, _item_text_set_hook);
1197    elm_widget_item_text_get_hook_set(item, _item_text_get_hook);
1198    elm_widget_item_content_set_hook_set(item, _item_content_set_hook);
1199    elm_widget_item_content_get_hook_set(item, _item_content_get_hook);
1200    elm_widget_item_signal_emit_hook_set(item, _item_signal_emit_hook);
1201
1202    if (!sd->list)
1203      {
1204         //The first item is appended.
1205         sd->list = elm_list_add(obj);
1206         elm_list_mode_set(sd->list, ELM_LIST_EXPAND);
1207         elm_list_horizontal_set(sd->list, sd->horizontal);
1208         evas_object_event_callback_add
1209           (sd->list, EVAS_CALLBACK_RESIZE, _list_resize_cb, obj);
1210         elm_layout_content_set(obj, "default", sd->list);
1211      }
1212
1213    item->list_item =
1214      elm_list_item_append(sd->list, label, icon, NULL, func, data);
1215
1216    sd->dir = ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1217
1218    if (sd->visible) elm_layout_sizing_eval(obj);
1219
1220    return (Elm_Object_Item *)item;
1221 }
1222
1223 EAPI void
1224 elm_ctxpopup_direction_priority_set(Evas_Object *obj,
1225                                     Elm_Ctxpopup_Direction first,
1226                                     Elm_Ctxpopup_Direction second,
1227                                     Elm_Ctxpopup_Direction third,
1228                                     Elm_Ctxpopup_Direction fourth)
1229 {
1230    ELM_CTXPOPUP_CHECK(obj);
1231    ELM_CTXPOPUP_DATA_GET(obj, sd);
1232
1233    sd->dir_priority[0] = first;
1234    sd->dir_priority[1] = second;
1235    sd->dir_priority[2] = third;
1236    sd->dir_priority[3] = fourth;
1237
1238    if (sd->visible) elm_layout_sizing_eval(obj);
1239 }
1240
1241 EAPI void
1242 elm_ctxpopup_direction_priority_get(Evas_Object *obj,
1243                                     Elm_Ctxpopup_Direction *first,
1244                                     Elm_Ctxpopup_Direction *second,
1245                                     Elm_Ctxpopup_Direction *third,
1246                                     Elm_Ctxpopup_Direction *fourth)
1247 {
1248    ELM_CTXPOPUP_CHECK(obj);
1249    ELM_CTXPOPUP_DATA_GET(obj, sd);
1250
1251    if (first) *first = sd->dir_priority[0];
1252    if (second) *second = sd->dir_priority[1];
1253    if (third) *third = sd->dir_priority[2];
1254    if (fourth) *fourth = sd->dir_priority[3];
1255 }
1256
1257 EAPI Elm_Ctxpopup_Direction
1258 elm_ctxpopup_direction_get(const Evas_Object *obj)
1259 {
1260    ELM_CTXPOPUP_CHECK(obj) ELM_CTXPOPUP_DIRECTION_UNKNOWN;
1261    ELM_CTXPOPUP_DATA_GET(obj, sd);
1262
1263    return sd->dir;
1264 }
1265
1266 EAPI void
1267 elm_ctxpopup_dismiss(Evas_Object *obj)
1268 {
1269    ELM_CTXPOPUP_CHECK(obj);
1270    ELM_CTXPOPUP_DATA_GET(obj, sd);
1271
1272    _hide_signals_emit(obj, sd->dir);
1273 }