109d97e5da31ac382147273676c09e5a489ca1a3
[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     public class NaviframeEventArgs : EventArgs
23     {
24         public EvasObject Content { get; set; }
25     }
26     public class Naviframe : Widget
27     {
28         SmartEvent _transitionFinished;
29         readonly List<NaviItem> _itemStack = new List<NaviItem>();
30         public Naviframe(EvasObject parent) : base(parent)
31         {
32             _transitionFinished = new SmartEvent(this, this.RealHandle, "transition,finished");
33             _transitionFinished.On += (s, e) => AnimationFinished?.Invoke(this, EventArgs.Empty);
34         }
35
36         /// <summary>
37         /// 
38         /// </summary>
39         /// <remarks>
40         /// It is always called when NaviItem was removed.
41         /// (even if removed by NaviItem.Delete())
42         /// This event will be invoked in progress of Pop/Delete operation. 
43         /// After called Popped event, Pop/Delete method will be returned
44         /// </remarks>
45         public event EventHandler<NaviframeEventArgs> Popped;
46         public event EventHandler AnimationFinished;
47         public IReadOnlyList<NaviItem> NavigationStack
48         {
49             get { return _itemStack; }
50         }
51
52         public bool PreserveContentOnPop
53         {
54             get
55             {
56                 return Interop.Elementary.elm_naviframe_content_preserve_on_pop_get(RealHandle);
57             }
58             set
59             {
60                 Interop.Elementary.elm_naviframe_content_preserve_on_pop_set(RealHandle, value);
61             }
62         }
63
64         public bool DefaultBackButtonEnabled
65         {
66             get
67             {
68                 return Interop.Elementary.elm_naviframe_prev_btn_auto_pushed_get(RealHandle);
69             }
70             set
71             {
72                 Interop.Elementary.elm_naviframe_prev_btn_auto_pushed_set(RealHandle, value);
73             }
74         }
75
76         public NaviItem Push(EvasObject content)
77         {
78             return Push(content, null);
79         }
80
81         public NaviItem Push(EvasObject content, string title)
82         {
83             return Push(content, title, null);
84         }
85
86         public NaviItem Push(EvasObject content, string title, string style)
87         {
88             IntPtr item = Interop.Elementary.elm_naviframe_item_push(RealHandle, title, IntPtr.Zero, IntPtr.Zero, content.Handle, null);
89             NaviItem naviItem = NaviItem.FromNativeHandle(item, content);
90             _itemStack.Add(naviItem);
91             naviItem.Popped += ItemPoppedHandler;
92             return naviItem;
93         }
94
95         public NaviItem InsertBefore(NaviItem before, EvasObject content)
96         {
97             return InsertBefore(before, content, "");
98         }
99
100         public NaviItem InsertBefore(NaviItem before, EvasObject content, string title)
101         {
102             return InsertBefore(before, content, title, null);
103         }
104
105         public NaviItem InsertBefore(NaviItem before, EvasObject content, string title, string style)
106         {
107             IntPtr item = Interop.Elementary.elm_naviframe_item_insert_before(RealHandle, before, title, IntPtr.Zero, IntPtr.Zero, content, null);
108             NaviItem naviItem = NaviItem.FromNativeHandle(item, content);
109             int idx = _itemStack.IndexOf(before);
110             _itemStack.Insert(idx, naviItem);
111             naviItem.Popped += ItemPoppedHandler;
112             return naviItem;
113         }
114
115         public NaviItem InsertAfter(NaviItem after, EvasObject content)
116         {
117             return InsertAfter(after, content, "");
118         }
119
120         public NaviItem InsertAfter(NaviItem after, EvasObject content, string title)
121         {
122             return InsertAfter(after, content, title, null);
123         }
124
125         public NaviItem InsertAfter(NaviItem after, EvasObject content, string title, string style)
126         {
127             IntPtr item = Interop.Elementary.elm_naviframe_item_insert_after(RealHandle, after, title, IntPtr.Zero, IntPtr.Zero, content, null);
128             NaviItem naviItem = NaviItem.FromNativeHandle(item, content);
129             int idx = _itemStack.IndexOf(after);
130             _itemStack.Insert(idx + 1, naviItem);
131             naviItem.Popped += ItemPoppedHandler;
132             return naviItem;
133         }
134
135         public void Pop()
136         {
137             Interop.Elementary.elm_naviframe_item_pop(RealHandle);
138         }
139
140         protected override IntPtr CreateHandle(EvasObject parent)
141         {
142             IntPtr handle = Interop.Elementary.elm_layout_add(parent);
143             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
144
145             RealHandle = Interop.Elementary.elm_naviframe_add(handle);
146             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
147
148             return handle;
149         }
150
151         void ItemPoppedHandler(object sender, EventArgs e)
152         {
153             NaviItem item = sender as NaviItem;
154             if (item == null)
155                 return;
156             _itemStack.Remove(item);
157             Popped?.Invoke(this, new NaviframeEventArgs() { Content = item.Content });
158         }
159     }
160 }