Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Popup.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 popup orientation type.
24     /// </summary>
25     public enum PopupOrientation
26     {
27         /// <summary>
28         /// Appears in the top of parent, default.
29         /// </summary>
30         Top,
31         /// <summary>
32         /// Appears in the center of parent.
33         /// </summary>
34         Center,
35         /// <summary>
36         /// Appears in the bottom of parent.
37         /// </summary>
38         Bottom,
39         /// <summary>
40         /// Appears in the left of parent.
41         /// </summary>
42         Left,
43         /// <summary>
44         /// Appears in the right of parent.
45         /// </summary>
46         Right,
47         /// <summary>
48         /// Appears in the top left of parent.
49         /// </summary>
50         TopLeft,
51         /// <summary>
52         /// Appears in the top right of parent.
53         /// </summary>
54         TopRight,
55         /// <summary>
56         /// Appears in the bottom left of parent.
57         /// </summary>
58         BottomLeft,
59         /// <summary>
60         /// Appears in the bottom right of parent.
61         /// </summary>
62         BottomRight
63     }
64
65     /// <summary>
66     /// The Popup is a widget that is an enhancement of Notify.
67     /// In addition to content area, there are two optional sections, namely title area and action area.
68     /// </summary>
69     public class Popup : Layout
70     {
71         HashSet<PopupItem> _children = new HashSet<PopupItem>();
72         SmartEvent _dismissed;
73         SmartEvent _blockClicked;
74         SmartEvent _timeout;
75         SmartEvent _showFinished;
76
77         /// <summary>
78         /// Creates and initializes a new instance of the Popup class.
79         /// </summary>
80         /// <param name="parent">The EvasObject to which the new Popup will be attached as a child.</param>
81         public Popup(EvasObject parent) : base(parent)
82         {
83             _dismissed = new SmartEvent(this, "dismissed");
84             _dismissed.On += (sender, e) =>
85             {
86                 Dismissed?.Invoke(this, EventArgs.Empty);
87             };
88
89             _blockClicked = new SmartEvent(this, "block,clicked");
90             _blockClicked.On += (sender, e) =>
91             {
92                 OutsideClicked?.Invoke(this, EventArgs.Empty);
93             };
94
95             _timeout = new SmartEvent(this, "timeout");
96             _timeout.On += (sender, e) =>
97             {
98                 TimedOut?.Invoke(this, EventArgs.Empty);
99             };
100
101             _showFinished = new SmartEvent(this, "show,finished");
102             _showFinished.On += (sender, e) =>
103             {
104                 ShowAnimationFinished?.Invoke(this, EventArgs.Empty);
105             };
106         }
107
108         /// <summary>
109         /// Dismissed will be triggered when Popup have been dismissed.
110         /// </summary>
111         public event EventHandler Dismissed;
112
113         /// <summary>
114         /// OutsideClicked will be triggered when users taps on the outside of Popup.
115         /// </summary>
116         public event EventHandler OutsideClicked;
117
118         /// <summary>
119         /// OutsideClicked will be triggered when Popup is closed as a result of timeout.
120         /// </summary>
121         public event EventHandler TimedOut;
122
123         /// <summary>
124         /// OutsideClicked will be triggered when the Popup transition is finished in showing.
125         /// </summary>
126         public event EventHandler ShowAnimationFinished;
127
128         /// <summary>
129         /// Sets or gets the position in which Popup will appear in its parent.
130         /// </summary>
131         public PopupOrientation Orientation
132         {
133             get
134             {
135                 return (PopupOrientation)Interop.Elementary.elm_popup_orient_get(Handle);
136             }
137             set
138             {
139                 Interop.Elementary.elm_popup_orient_set(Handle, (int)value);
140             }
141         }
142
143         /// <summary>
144         /// Sets or gets the wrapping type of content text packed in content area of Popup widget.
145         /// </summary>
146         public WrapType ContentTextWrapType
147         {
148             get
149             {
150                 return (WrapType)Interop.Elementary.elm_popup_content_text_wrap_type_get(Handle);
151             }
152             set
153             {
154                 Interop.Elementary.elm_popup_content_text_wrap_type_set(Handle, (int)value);
155             }
156         }
157
158         /// <summary>
159         /// Sets or gets the timeout value set to the Popup(in seconds).
160         /// </summary>
161         /// <remarks>
162         /// Since calling Show() on a popup restarts the timer controlling when it is hidden,
163         /// setting this before the popup is shown will in effect mean starting the timer when the popup is shown.
164         /// TimedOut is called afterwards which can be handled if needed.
165         /// Set a value <= 0.0 to disable a running timer.If the value > 0.0 and the popup is previously visible,
166         /// the timer will be started with this value, canceling any running timer.
167         /// </remarks>
168         public double Timeout
169         {
170             get
171             {
172                 return Interop.Elementary.elm_popup_timeout_get(Handle);
173             }
174             set
175             {
176                 Interop.Elementary.elm_popup_timeout_set(Handle, value);
177             }
178         }
179
180         /// <summary>
181         /// Sets or gets whether events should be passed to event blocked area by a click outside.
182         /// </summary>
183         /// <remarks>
184         /// The visible region of popup is surrounded by a translucent region called Blocked Event area.
185         /// </remarks>
186         public bool AllowEvents
187         {
188             get
189             {
190                 return Interop.Elementary.elm_popup_allow_events_get(Handle);
191             }
192             set
193             {
194                 Interop.Elementary.elm_popup_allow_events_set(Handle, value);
195             }
196         }
197
198         /// <summary>
199         /// Sets or gets the AlignmentX in which the popup will appear in its parent.
200         /// </summary>
201         public override double AlignmentX
202         {
203             get
204             {
205                 return Interop.Elementary.GetPopupAlignX(Handle);
206             }
207             set
208             {
209                 Interop.Elementary.SetPopupAlignX(Handle, value);
210             }
211         }
212
213         /// <summary>
214         /// Sets or gets the AlignmentY in which the popup will appear in its parent.
215         /// </summary>
216         public override double AlignmentY
217         {
218             get
219             {
220                 return Interop.Elementary.GetPopupAlignY(Handle);
221             }
222             set
223             {
224                 Interop.Elementary.SetPopupAlignY(Handle, value);
225             }
226         }
227
228         /// <summary>
229         /// Gets the Opacity value of the Popup.
230         /// </summary>
231         public override int Opacity
232         {
233             get
234             {
235                 return Color.Default.A;
236             }
237
238             set
239             {
240                 Console.WriteLine("Popup instance doesn't support to set Opacity.");
241             }
242         }
243
244         /// <summary>
245         /// Adds label to a Popup widget.
246         /// </summary>
247         /// <param name="label"></param>
248         /// <returns>The new PopupItem which contains label .</returns>
249         public PopupItem Append(string label)
250         {
251             return Append(label, null);
252         }
253
254         /// <summary>
255         /// Adds Label and icon to a Popup widget.
256         /// </summary>
257         /// <param name="label">The Label which will be added into a new PopupItem. </param>
258         /// <param name="icon">The icon which will be added into a new PopupItem. </param>
259         /// <returns>The new PopupItem which contains label and icon.</returns>
260         public PopupItem Append(string label, EvasObject icon)
261         {
262             PopupItem item = new PopupItem(label, icon);
263             item.Handle = Interop.Elementary.elm_popup_item_append(Handle, label, icon, null, (IntPtr)item.Id);
264             AddInternal(item);
265             return item;
266         }
267
268         /// <summary>
269         /// Uses this function to dismiss the popup in hide effect.
270         /// when the Popup is dismissed, the "dismissed" signal will be emitted.
271         /// </summary>
272         public void Dismiss()
273         {
274             Interop.Elementary.elm_popup_dismiss(Handle);
275         }
276
277         protected override IntPtr CreateHandle(EvasObject parent)
278         {
279             return Interop.Elementary.elm_popup_add(parent.Handle);
280         }
281         void AddInternal(PopupItem item)
282         {
283             _children.Add(item);
284             item.Deleted += Item_Deleted;
285         }
286         void Item_Deleted(object sender, EventArgs e)
287         {
288             _children.Remove((PopupItem)sender);
289         }
290     }
291 }