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