Release 4.0.0-preview1-00051
[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     /// <summary>
23     /// Enumeration of ContextPopup direction type.
24     /// </summary>
25     public enum ContextPopupDirection
26     {
27         /// <summary>
28         /// ContextPopup show appear below clicked area
29         /// /// </summary>
30         Down,
31         /// <summary>
32         /// ContextPopup show appear to the right of the clicked area
33         /// </summary>
34         Right,
35         /// <summary>
36         /// ContextPopup show appear to the left of the clicked area
37         /// </summary>
38         Left,
39         /// <summary>
40         /// ContextPopup show appear above the clicked area
41         /// </summary>
42         Up,
43         /// <summary>
44         /// ContextPopup does not determine it's direction yet
45         /// </summary>
46         Unknown
47     }
48
49     /// <summary>
50     /// It inherits <see cref="Layout"/>.
51     /// The ContextPopup is a widget that when it shown, pops up a list of items.
52     /// </summary>
53     public class ContextPopup : Layout
54     {
55         HashSet<ContextPopupItem> _children = new HashSet<ContextPopupItem>();
56         SmartEvent _dismissed;
57         Interop.Evas.SmartCallback _onSelected;
58
59         /// <summary>
60         /// Creates and initializes a new instance of the ContextPopup class.
61         /// </summary>
62         /// <param name="parent">The parent is a given container which will be attached by ContextPopup
63         /// as a child.It's <see cref="EvasObject"/> type.</param>
64         public ContextPopup(EvasObject parent) : base(parent)
65         {
66             _dismissed = new SmartEvent(this, this.RealHandle, "dismissed");
67             _dismissed.On += (sender, e) =>
68             {
69                 Dismissed?.Invoke(this, EventArgs.Empty);
70             };
71             _onSelected = (data, obj, info) =>
72             {
73                 ContextPopupItem item = ItemObject.GetItemById((int)data) as ContextPopupItem;
74                 item?.SendSelected();
75             };
76         }
77
78         /// <summary>
79         /// Dismissed is raised when the ContextPopup item is dismissed.
80         /// </summary>
81         /// <remarks>
82         /// Outside of ContextPopup was clicked or it's parent area is changed or the language is changed. and then ContextPopup is dismissed.
83         /// </remarks>
84         public event EventHandler Dismissed;
85
86         /// <summary>
87         /// Gets the current direction of a ContextPopup.
88         /// </summary>
89         /// <remarks>
90         /// Once the ContextPopup showed up, the direction would be determined.
91         /// </remarks>
92         public ContextPopupDirection Direction
93         {
94             get
95             {
96                 return (ContextPopupDirection)Interop.Elementary.elm_ctxpopup_direction_get(RealHandle);
97             }
98         }
99
100         /// <summary>
101         /// Gets or sets the value of current ContextPopup object's orientation.
102         /// True for horizontal mode, False for vertical mode (or errors)
103         /// </summary>
104         public bool IsHorizontal
105         {
106             get
107             {
108                 return Interop.Elementary.elm_ctxpopup_horizontal_get(RealHandle);
109             }
110             set
111             {
112                 Interop.Elementary.elm_ctxpopup_horizontal_set(RealHandle, value);
113             }
114         }
115
116         /// <summary>
117         /// Gets or sets whether ContextPopup hide automatically
118         /// or not when parent of ContextPopup is resized.
119         /// </summary>
120         /// <remarks>
121         /// Default value of AutoHide is False.
122         /// </remarks>
123         public bool AutoHide
124         {
125             get
126             {
127                 return !Interop.Elementary.elm_ctxpopup_auto_hide_disabled_get(RealHandle);
128             }
129             set
130             {
131                 Interop.Elementary.elm_ctxpopup_auto_hide_disabled_set(RealHandle, !value);
132             }
133         }
134
135         /// <summary>
136         /// Clears all items in the given ContextPopup object.
137         /// </summary>
138         public void Clear()
139         {
140             Interop.Elementary.elm_ctxpopup_clear(Handle);
141         }
142
143         /// <summary>
144         /// Sets the direction priority of a ContextPopup.
145         /// </summary>
146         /// <param name="first">1st priority of direction </param>
147         /// <param name="second">2nd priority of direction </param>
148         /// <param name="third">3th priority of direction </param>
149         /// <param name="fourth">4th priority of direction</param>
150         public void SetDirectionPriorty(ContextPopupDirection first, ContextPopupDirection second, ContextPopupDirection third, ContextPopupDirection fourth)
151         {
152             Interop.Elementary.elm_ctxpopup_direction_priority_set(RealHandle, (int)first, (int)second, (int)third, (int)fourth);
153         }
154
155         /// <summary>
156         /// Gets the direction priority of a ContextPopup.
157         /// </summary>
158         /// <param name="first">1st priority of direction to be returned</param>
159         /// <param name="second">2nd priority of direction to be returned</param>
160         /// <param name="third">2nd priority of direction to be returned </param>
161         /// <param name="fourth">4th priority of direction to be returned</param>
162         public void GetDirectionPriority(out ContextPopupDirection first, out ContextPopupDirection second, out ContextPopupDirection third, out ContextPopupDirection fourth)
163         {
164             int firstOut, secondOut, thirdOut, fourthOut;
165             Interop.Elementary.elm_ctxpopup_direction_priority_get(Handle, out firstOut, out secondOut, out thirdOut, out fourthOut);
166             first = (ContextPopupDirection)firstOut;
167             second = (ContextPopupDirection)secondOut;
168             third = (ContextPopupDirection)thirdOut;
169             fourth = (ContextPopupDirection)fourthOut;
170         }
171
172         /// <summary>
173         /// Adds a new item to a ContextPopup object with label.
174         /// </summary>
175         /// <param name="label">The Label of the new item</param>
176         /// <returns>
177         /// A ContextPopupItem added or NULL, on errors
178         /// </returns>
179         public ContextPopupItem Append(string label)
180         {
181             return Append(label, null);
182         }
183
184         /// <summary>
185         /// Adds a new item to a ContextPopup object with label and icon.
186         /// </summary>
187         /// <param name="label">The Label of the new item</param>
188         /// <param name="icon">Icon to be set on new item</param>
189         /// <returns>A ContextPopupItem added or NULL, on errors</returns>
190         public ContextPopupItem Append(string label, EvasObject icon)
191         {
192             ContextPopupItem item = new ContextPopupItem(label, icon);
193             item.Handle = Interop.Elementary.elm_ctxpopup_item_append(RealHandle, label, icon, _onSelected, (IntPtr)item.Id);
194             AddInternal(item);
195             return item;
196         }
197
198         /// <summary>
199         /// Dismiss a ContextPopup object. The ContextPopup will be hidden and the "clicked" signal will be emitted.
200         /// </summary>
201         public void Dismiss()
202         {
203             Interop.Elementary.elm_ctxpopup_dismiss(RealHandle);
204         }
205
206         /// <summary>
207         /// Gets the possibility that the direction would be available
208         /// </summary>
209         /// <param name="direction">A direction user wants to check</param>
210         /// <returns>
211         /// Get false if you cannot put it in the direction. Gets true if it's possible.
212         /// </returns>
213         public bool IsAvailableDirection(ContextPopupDirection direction)
214         {
215             return Interop.Elementary.elm_ctxpopup_direction_available_get(RealHandle, (int)direction);
216         }
217
218         /// <summary>
219         /// Gets Alpha of a default Color Class.
220         /// </summary>
221         public override int Opacity
222         {
223             get
224             {
225                 return Color.Default.A;
226             }
227
228             set
229             {
230                 Console.WriteLine("ContextPopup instance doesn't support to set Opacity.");
231             }
232         }
233
234         protected override IntPtr CreateHandle(EvasObject parent)
235         {
236             return Interop.Elementary.elm_ctxpopup_add(parent.Handle);
237         }
238
239         void AddInternal(ContextPopupItem item)
240         {
241             _children.Add(item);
242             item.Deleted += Item_Deleted;
243         }
244
245         void Item_Deleted(object sender, EventArgs e)
246         {
247             _children.Remove((ContextPopupItem)sender);
248         }
249     }
250 }