Revert "[NUI] Fix ConvertIdToView (#877)" (#889)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / ContentPage.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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.ComponentModel;
19 using Tizen.NUI.Binding;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Xaml;
22 using System.Collections.Generic;
23 using System.IO;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// The ContentPage class.
29     /// </summary>
30     [ContentProperty("Content")]
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class ContentPage : TemplatedPage
33     {
34         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public View Root {get; internal set;}
37
38         /// <summary>
39         /// The contents of ContentPage can be added into it.
40         /// </summary>
41         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentPage), null, propertyChanged: (bindable, oldValue, newValue) =>
44         {
45             var self = (ContentPage)bindable;
46             if (newValue != null)
47             {
48                 self.Root.Add((View)newValue);
49             }
50         });
51
52         /// <summary>
53         /// The contents of ContentPage can be added into it.
54         /// </summary>
55         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
56         [EditorBrowsable(EditorBrowsableState.Never)]
57         public View Content
58         {
59             get { return (View)GetValue(ContentProperty); }
60             set { SetValue(ContentProperty, value); }
61         }
62
63         /// <summary>
64         /// Method that is called when the binding content changes.
65         /// </summary>
66         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         protected override void OnBindingContextChanged()
69         {
70             base.OnBindingContextChanged();
71
72             View content = Content;
73             ControlTemplate controlTemplate = ControlTemplate;
74             if (content != null && controlTemplate != null)
75             {
76                 SetInheritedBindingContext(content, BindingContext);
77             }
78         }
79
80         internal override void OnControlTemplateChanged(ControlTemplate oldValue, ControlTemplate newValue)
81         {
82             if (oldValue == null)
83                 return;
84
85             base.OnControlTemplateChanged(oldValue, newValue);
86             View content = Content;
87             ControlTemplate controlTemplate = ControlTemplate;
88             if (content != null && controlTemplate != null)
89             {
90                 SetInheritedBindingContext(content, BindingContext);
91             }
92         }
93
94         /// <summary>
95         /// The constructor.
96         /// </summary>
97         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
98         [EditorBrowsable(EditorBrowsableState.Never)]
99         public ContentPage(Window win)
100         {
101             IsCreateByXaml = true;
102
103             Root = new View();
104             Root.WidthResizePolicy = ResizePolicyType.FillToParent;
105             Root.HeightResizePolicy = ResizePolicyType.FillToParent;
106
107             win.Add(Root);
108         }
109
110         /// <summary>
111         /// The Resources property.
112         /// </summary>
113         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public ResourceDictionary XamlResources
116         {
117             get
118             {
119                 return Application.Current.XamlResources;
120             }
121
122             set
123             {
124                 Application.Current.XamlResources = value;
125             }
126         }
127
128         /// <summary>
129         /// To make the ContentPage instance be disposed.
130         /// </summary>
131         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
132         [EditorBrowsable(EditorBrowsableState.Never)]
133         protected override void Dispose(DisposeTypes type)
134         {
135             if (disposed)
136             {
137                 return;
138             }
139
140             if (type == DisposeTypes.Explicit)
141             {
142                 //Called by User
143                 //Release your own managed resources here.
144                 //You should release all of your own disposable objects here.
145             }
146
147             //Release your own unmanaged resources here.
148             //You should not access any managed member here except static instance.
149             //because the execution order of Finalizes is non-deterministic.
150             if(Root != null) 
151             {
152                 Window.Instance.Remove(Root);
153                 Root.Dispose();
154                 Root = null;
155             }
156             base.Dispose(type);
157         }
158
159         /// <summary>
160         /// Check whether the content is empty.
161         /// </summary>
162         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
163         [EditorBrowsable(EditorBrowsableState.Never)]
164         public bool IsEmpty
165         {
166             get
167             {
168                 return ( Root.ChildCount == 0 ) ? true : false;
169             }
170         }
171
172         /// <summary>
173         /// Clear all contents from this ContentPage.
174         /// </summary>
175         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
176         [EditorBrowsable(EditorBrowsableState.Never)]
177         public void ClearContent()
178         {
179             if ( Root != null )
180             {
181                 //Remove it from the window
182                 Window.Instance.Remove(Root);
183                 Root.Dispose();
184                 Root = null;
185
186                 //Readd to window
187                 Root = new View();
188                 Root.WidthResizePolicy = ResizePolicyType.FillToParent;
189                 Root.HeightResizePolicy = ResizePolicyType.FillToParent;
190                 Window.Instance.Add(Root);
191
192                 ClearHandler();
193             }
194         }
195
196         private EventHandler _clearEventHandler;
197
198         /// <summary>
199         /// Clear event.
200         /// </summary>
201         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
202         [EditorBrowsable(EditorBrowsableState.Never)]
203         public event EventHandler ClearEvent
204         {
205             add
206             {
207                 _clearEventHandler += value;
208             }
209             remove
210             {
211                 _clearEventHandler -= value;
212             }
213         }
214
215         private void ClearHandler()
216         {
217             if (_clearEventHandler != null)
218             {
219                 _clearEventHandler(this, null);
220             }
221         }
222
223         /// <summary>
224         /// Users can set focus logic codes here.
225         /// </summary>
226         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
227         [EditorBrowsable(EditorBrowsableState.Never)]
228         public virtual void SetFocus() { }
229
230         private Dictionary<string, Transition> transDictionary = new Dictionary<string, Transition>();
231
232         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
233         [EditorBrowsable(EditorBrowsableState.Never)]
234         public Transition GetTransition(string transitionName)
235         {
236             Transition trans = null;
237             transDictionary.TryGetValue(transitionName, out trans);
238             return trans;
239         }
240
241         private void LoadTransitions()
242         {
243             foreach (string str in transitionNames)
244             {
245                 string resourceName = str + ".xaml";
246                 Transition trans = null;
247
248                 string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
249
250                 string likelyResourcePath = resource + "animation/" + resourceName;
251
252                 if (File.Exists(likelyResourcePath))
253                 {
254                     trans = Extensions.LoadObject<Transition>(likelyResourcePath);
255                 }
256                 if (trans)
257                 {
258                     transDictionary.Add(trans.Name, trans);
259                 }
260             }
261         }
262
263         private string[] transitionNames;
264
265         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
266         [EditorBrowsable(EditorBrowsableState.Never)]
267         public string[] TransitionNames
268         {
269             get
270             {
271                 return transitionNames;
272             }
273             set
274             {
275                 transitionNames = value;
276                 LoadTransitions();
277             }
278         }
279     }
280 }