[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Naviframe.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     /// The NaviframeEventArgs is an event arguments class for naviframe.
24     /// Inherits EventArgs.
25     /// </summary>
26     /// <since_tizen> preview </since_tizen>
27     public class NaviframeEventArgs : EventArgs
28     {
29         /// <summary>
30         /// Sets or gets the content object. The name of the content part is "elm.swallow.content".
31         /// </summary>
32         /// <since_tizen> preview </since_tizen>
33         public EvasObject Content { get; set; }
34     }
35     /// <summary>
36     /// The Naviframe is a widget to stand for the navigation frame. It's a views manager for applications.
37     /// Inherits Widget.
38     /// </summary>
39     /// <since_tizen> preview </since_tizen>
40     public class Naviframe : Widget
41     {
42         SmartEvent _transitionFinished;
43         readonly List<NaviItem> _itemStack = new List<NaviItem>();
44
45         /// <summary>
46         /// Creates and initializes a new instance of the Naviframe class.
47         /// </summary>
48         /// <param name="parent">The parent is a given container which will be attached by Naviframe as a child. It's <see cref="EvasObject"/> type.</param>
49         /// <since_tizen> preview </since_tizen>
50         public Naviframe(EvasObject parent) : base(parent)
51         {
52             _transitionFinished = new SmartEvent(this, this.RealHandle, "transition,finished");
53             _transitionFinished.On += (s, e) => AnimationFinished?.Invoke(this, EventArgs.Empty);
54         }
55
56         /// <summary>
57         /// Popped will be triggered when the NaviItem is removed.
58         /// </summary>
59         /// <remarks>
60         /// It is always called when the NaviItem is removed.
61         /// (even if removed by NaviItem.Delete())
62         /// This event will be invoked in progress of the Pop/Delete operation.
63         /// After calling the Popped event, the Pop/Delete method will be returned.
64         /// </remarks>
65         /// <since_tizen> preview </since_tizen>
66         public event EventHandler<NaviframeEventArgs> Popped;
67
68         /// <summary>
69         /// AnimationFinished will be triggered when the animation is finished.
70         /// </summary>
71         /// <since_tizen> preview </since_tizen>
72         public event EventHandler AnimationFinished;
73
74         /// <summary>
75         /// Gets the list of the navi item.
76         /// </summary>
77         /// <since_tizen> preview </since_tizen>
78         public IReadOnlyList<NaviItem> NavigationStack
79         {
80             get { return _itemStack; }
81         }
82
83         /// <summary>
84         /// Sets or gets the preserve content objects when items are popped.
85         /// </summary>
86         /// <since_tizen> preview </since_tizen>
87         public bool PreserveContentOnPop
88         {
89             get
90             {
91                 return Interop.Elementary.elm_naviframe_content_preserve_on_pop_get(RealHandle);
92             }
93             set
94             {
95                 Interop.Elementary.elm_naviframe_content_preserve_on_pop_set(RealHandle, value);
96             }
97         }
98
99         /// <summary>
100         /// Sets or gets whether the default back button is enabled.
101         /// </summary>
102         /// <since_tizen> preview </since_tizen>
103         public bool DefaultBackButtonEnabled
104         {
105             get
106             {
107                 return Interop.Elementary.elm_naviframe_prev_btn_auto_pushed_get(RealHandle);
108             }
109             set
110             {
111                 Interop.Elementary.elm_naviframe_prev_btn_auto_pushed_set(RealHandle, value);
112             }
113         }
114
115         /// <summary>
116         /// Pushes a new item to the top of the naviframe stack and shows it.
117         /// The title and style are null.
118         /// </summary>
119         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
120         /// <returns>The created item, or null upon failure.</returns>
121         /// <since_tizen> preview </since_tizen>
122         public NaviItem Push(EvasObject content)
123         {
124             return Push(content, null);
125         }
126
127         /// <summary>
128         /// Pushes a new item to the top of the naviframe stack and shows it.
129         /// The style is null.
130         /// </summary>
131         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
132         /// <param name="title">The current item title. Null would be default.</param>
133         /// <returns></returns>
134         /// <since_tizen> preview </since_tizen>
135         public NaviItem Push(EvasObject content, string title)
136         {
137             return Push(content, title, null);
138         }
139
140         /// <summary>
141         /// Pushes a new item to the top of the naviframe stack and shows it.
142         /// </summary>
143         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
144         /// <param name="title">The current item title. Null would be default.</param>
145         /// <param name="style">The current item style name. Null would be default.</param>
146         /// <returns>The created item, or null upon failure.</returns>
147         /// <since_tizen> preview </since_tizen>
148         public NaviItem Push(EvasObject content, string title, string style)
149         {
150             IntPtr item = Interop.Elementary.elm_naviframe_item_push(RealHandle, title, IntPtr.Zero, IntPtr.Zero, content.Handle, style);
151             NaviItem naviItem = NaviItem.FromNativeHandle(item, content, this);
152             _itemStack.Add(naviItem);
153             naviItem.Popped += ItemPoppedHandler;
154             return naviItem;
155         }
156
157         /// <summary>
158         /// Inserts a new item into the naviframe before the item.
159         /// The title is "" and the style is null.
160         /// </summary>
161         /// <param name="before">The item for which a new item is inserted before.</param>
162         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
163         /// <returns>The created item, or null upon failure.</returns>
164         /// <since_tizen> preview </since_tizen>
165         public NaviItem InsertBefore(NaviItem before, EvasObject content)
166         {
167             return InsertBefore(before, content, "");
168         }
169
170         /// <summary>
171         /// Inserts a new item into the naviframe before the item.
172         /// The style is null.
173         /// </summary>
174         /// <param name="before">The item for which a new item is inserted before.</param>
175         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
176         /// <param name="title">The current item title. Null would be default.</param>
177         /// <returns>The created item, or null upon failure.</returns>
178         /// <since_tizen> preview </since_tizen>
179         public NaviItem InsertBefore(NaviItem before, EvasObject content, string title)
180         {
181             return InsertBefore(before, content, title, null);
182         }
183
184         /// <summary>
185         /// Inserts a new item into the naviframe before the item.
186         /// </summary>
187         /// <param name="before">The item for which a new item is inserted before.</param>
188         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
189         /// <param name="title">The current item title. Null would be default.</param>
190         /// <param name="style">The current item style name. Null would be default.</param>
191         /// <returns>The created item, or null upon failure.</returns>
192         /// <since_tizen> preview </since_tizen>
193         public NaviItem InsertBefore(NaviItem before, EvasObject content, string title, string style)
194         {
195             IntPtr item = Interop.Elementary.elm_naviframe_item_insert_before(RealHandle, before, title, IntPtr.Zero, IntPtr.Zero, content, null);
196             NaviItem naviItem = NaviItem.FromNativeHandle(item, content, this);
197             int idx = _itemStack.IndexOf(before);
198             _itemStack.Insert(idx, naviItem);
199             naviItem.Popped += ItemPoppedHandler;
200             return naviItem;
201         }
202
203         /// <summary>
204         /// Inserts a new item into the naviframe after the item.
205         /// The title is "" and the style is null.
206         /// </summary>
207         /// <param name="after">The item for which a new item is inserted after.</param>
208         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
209         /// <returns>The created item, or null upon failure.</returns>
210         /// <since_tizen> preview </since_tizen>
211         public NaviItem InsertAfter(NaviItem after, EvasObject content)
212         {
213             return InsertAfter(after, content, "");
214         }
215
216         /// <summary>
217         /// Inserts a new item into the naviframe after the item.
218         /// The style is null.
219         /// </summary>
220         /// <param name="after">The item which a new item is inserted after.</param>
221         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
222         /// <param name="title">The current item title. Null would be default.</param>
223         /// <returns>The created item, or null upon failure.</returns>
224         /// <since_tizen> preview </since_tizen>
225         public NaviItem InsertAfter(NaviItem after, EvasObject content, string title)
226         {
227             return InsertAfter(after, content, title, null);
228         }
229
230         /// <summary>
231         /// Inserts a new item into the naviframe after the item.
232         /// </summary>
233         /// <param name="after">The item for which a new item is inserted after.</param>
234         /// <param name="content">The main content object. The name of the content part is "elm.swallow.content".</param>
235         /// <param name="title">The current item title. Null would be default.</param>
236         /// <param name="style">The current item style name. Null would be default.</param>
237         /// <returns>The created item, or null upon failure.</returns>
238         /// <since_tizen> preview </since_tizen>
239         public NaviItem InsertAfter(NaviItem after, EvasObject content, string title, string style)
240         {
241             IntPtr item = Interop.Elementary.elm_naviframe_item_insert_after(RealHandle, after, title, IntPtr.Zero, IntPtr.Zero, content, null);
242             NaviItem naviItem = NaviItem.FromNativeHandle(item, content, this);
243             int idx = _itemStack.IndexOf(after);
244             _itemStack.Insert(idx + 1, naviItem);
245             naviItem.Popped += ItemPoppedHandler;
246             return naviItem;
247         }
248
249         /// <summary>
250         /// Pops an item that is on top of the stack.
251         /// </summary>
252         /// <since_tizen> preview </since_tizen>
253         public void Pop()
254         {
255             Interop.Elementary.elm_naviframe_item_pop(RealHandle);
256         }
257
258         /// <summary>
259         /// Creates a widget handle.
260         /// </summary>
261         /// <param name="parent">Parent EvasObject.</param>
262         /// <returns>Handle IntPtr.</returns>
263         /// <since_tizen> preview </since_tizen>
264         protected override IntPtr CreateHandle(EvasObject parent)
265         {
266             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
267             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
268
269             RealHandle = Interop.Elementary.elm_naviframe_add(handle);
270             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
271
272             return handle;
273         }
274
275         void ItemPoppedHandler(object sender, EventArgs e)
276         {
277             NaviItem item = sender as NaviItem;
278             if (item == null)
279                 return;
280             _itemStack.Remove(item);
281             Popped?.Invoke(this, new NaviframeEventArgs { Content = item.Content });
282         }
283     }
284 }