Introduce ElmSharp project
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / ContextPopup.cs
1 using System;
2 using System.Collections.Generic;
3
4 namespace ElmSharp
5 {
6     public enum ContextPopupDirection
7     {
8         Down,
9         Right,
10         Left,
11         Up,
12         Unknown
13     }
14
15     public class ContextPopup : Layout
16     {
17         HashSet<ContextPopupItem> _children = new HashSet<ContextPopupItem>();
18         Interop.SmartEvent _dismissed;
19         Interop.Evas.SmartCallback _onSelected;
20
21         public ContextPopup(EvasObject parent) : base(parent)
22         {
23             _dismissed = new Interop.SmartEvent(this, Handle, "dismissed");
24             _dismissed.On += (sender, e) =>
25             {
26                 Dismissed?.Invoke(this, EventArgs.Empty);
27             };
28             _onSelected = (data, obj, info) =>
29             {
30                 ContextPopupItem item = ItemObject.GetItemById((int)data) as ContextPopupItem;
31                 item?.SendSelected();
32             };
33         }
34
35         public event EventHandler Dismissed;
36
37         public ContextPopupDirection Direction
38         {
39             get
40             {
41                 return (ContextPopupDirection)Interop.Elementary.elm_ctxpopup_direction_get(Handle);
42             }
43         }
44
45         public bool IsHorizontal
46         {
47             get
48             {
49                 return Interop.Elementary.elm_ctxpopup_horizontal_get(Handle);
50             }
51             set
52             {
53                 Interop.Elementary.elm_box_horizontal_set(Handle, value);
54             }
55         }
56
57         public bool AutoHide
58         {
59             get
60             {
61                 return !Interop.Elementary.elm_ctxpopup_auto_hide_disabled_get(Handle);
62             }
63             set
64             {
65                 Interop.Elementary.elm_ctxpopup_auto_hide_disabled_set(Handle, !value);
66             }
67         }
68
69         public void SetDirectionPriorty(ContextPopupDirection first, ContextPopupDirection second, ContextPopupDirection third, ContextPopupDirection fourth)
70         {
71             Interop.Elementary.elm_ctxpopup_direction_priority_set(Handle, (int)first, (int)second, (int)third, (int)fourth);
72         }
73
74         public ContextPopupItem Append(string label)
75         {
76             return Append(label, null);
77         }
78
79         public ContextPopupItem Append(string label, EvasObject icon)
80         {
81             ContextPopupItem item = new ContextPopupItem(label, icon);
82             item.Handle = Interop.Elementary.elm_ctxpopup_item_append(Handle, label, icon, _onSelected, (IntPtr)item.Id);
83             AddInternal(item);
84             return item;
85         }
86
87         public void Dismiss()
88         {
89             Interop.Elementary.elm_ctxpopup_dismiss(Handle);
90         }
91
92         public bool IsAvailableDirection(ContextPopupDirection direction)
93         {
94             return Interop.Elementary.elm_ctxpopup_direction_available_get(Handle, (int)direction);
95         }
96
97         internal override IntPtr CreateHandle(EvasObject parent)
98         {
99             return Interop.Elementary.elm_ctxpopup_add(parent.Handle);
100         }
101
102         void AddInternal(ContextPopupItem item)
103         {
104             _children.Add(item);
105             item.Deleted += Item_Deleted;
106         }
107
108         void Item_Deleted(object sender, EventArgs e)
109         {
110             _children.Remove((ContextPopupItem)sender);
111         }
112     }
113 }