Merge "Add FieldUpdate API in GenListItem" into tizen
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / ContextPopup.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 namespace ElmSharp
21 {
22     public enum ContextPopupDirection
23     {
24         Down,
25         Right,
26         Left,
27         Up,
28         Unknown
29     }
30
31     public class ContextPopup : Layout
32     {
33         HashSet<ContextPopupItem> _children = new HashSet<ContextPopupItem>();
34         SmartEvent _dismissed;
35         Interop.Evas.SmartCallback _onSelected;
36
37         public ContextPopup(EvasObject parent) : base(parent)
38         {
39             _dismissed = new SmartEvent(this, "dismissed");
40             _dismissed.On += (sender, e) =>
41             {
42                 Dismissed?.Invoke(this, EventArgs.Empty);
43             };
44             _onSelected = (data, obj, info) =>
45             {
46                 ContextPopupItem item = ItemObject.GetItemById((int)data) as ContextPopupItem;
47                 item?.SendSelected();
48             };
49         }
50
51         public event EventHandler Dismissed;
52
53         public ContextPopupDirection Direction
54         {
55             get
56             {
57                 return (ContextPopupDirection)Interop.Elementary.elm_ctxpopup_direction_get(Handle);
58             }
59         }
60
61         public bool IsHorizontal
62         {
63             get
64             {
65                 return Interop.Elementary.elm_ctxpopup_horizontal_get(Handle);
66             }
67             set
68             {
69                 Interop.Elementary.elm_ctxpopup_horizontal_set(Handle, value);
70             }
71         }
72
73         public bool AutoHide
74         {
75             get
76             {
77                 return !Interop.Elementary.elm_ctxpopup_auto_hide_disabled_get(Handle);
78             }
79             set
80             {
81                 Interop.Elementary.elm_ctxpopup_auto_hide_disabled_set(Handle, !value);
82             }
83         }
84
85         public void SetDirectionPriorty(ContextPopupDirection first, ContextPopupDirection second, ContextPopupDirection third, ContextPopupDirection fourth)
86         {
87             Interop.Elementary.elm_ctxpopup_direction_priority_set(Handle, (int)first, (int)second, (int)third, (int)fourth);
88         }
89
90         public ContextPopupItem Append(string label)
91         {
92             return Append(label, null);
93         }
94
95         public ContextPopupItem Append(string label, EvasObject icon)
96         {
97             ContextPopupItem item = new ContextPopupItem(label, icon);
98             item.Handle = Interop.Elementary.elm_ctxpopup_item_append(Handle, label, icon, _onSelected, (IntPtr)item.Id);
99             AddInternal(item);
100             return item;
101         }
102
103         public void Dismiss()
104         {
105             Interop.Elementary.elm_ctxpopup_dismiss(Handle);
106         }
107
108         public bool IsAvailableDirection(ContextPopupDirection direction)
109         {
110             return Interop.Elementary.elm_ctxpopup_direction_available_get(Handle, (int)direction);
111         }
112
113         protected override IntPtr CreateHandle(EvasObject parent)
114         {
115             return Interop.Elementary.elm_ctxpopup_add(parent.Handle);
116         }
117
118         void AddInternal(ContextPopupItem item)
119         {
120             _children.Add(item);
121             item.Deleted += Item_Deleted;
122         }
123
124         void Item_Deleted(object sender, EventArgs e)
125         {
126             _children.Remove((ContextPopupItem)sender);
127         }
128     }
129 }