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