[NUI] Fix Parent crash error (#97)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Widget.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *Z:\Desktop\shared\tizenfx\src\Tizen.NUI\src\public\BaseComponents\CustomView.cs
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
18 using System;
19
20 namespace Tizen.NUI
21 {
22     /// <summary>
23     /// Widget provides some common functionality required by all custom widget.
24     /// </summary>
25     /// <since_tizen> 4 </since_tizen>
26     public class Widget : BaseHandle
27     {
28         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
29         internal WidgetImpl widgetImpl;
30
31         internal Widget(WidgetImpl widgetImpl, bool swigCMemOwn) : this(NDalicManualPINVOKE.Widget_New__SWIG_1(WidgetImpl.getCPtr(widgetImpl)), swigCMemOwn)
32         {
33             this.widgetImpl = widgetImpl;
34             widgetImpl.WidgetInstanceCreated += OnWidgetInstanceCreated;
35             widgetImpl.WidgetInstanceDestroyed += OnWidgetInstanceDestroyed;
36             widgetImpl.WidgetInstancePaused += OnWidgetInstancePaused;
37             widgetImpl.WidgetInstanceResumed += OnWidgetInstanceResumed;
38             widgetImpl.WidgetInstanceResized += OnWidgetInstanceResized;
39             widgetImpl.WidgetInstanceUpdated += OnWidgetInstanceUpdated;
40
41             WidgetApplication.Instance?.AddWidgetInstance(this);
42         }
43
44         internal Widget(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicManualPINVOKE.Widget_SWIGUpcast(cPtr), cMemoryOwn)
45         {
46             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
47         }
48
49         private void OnWidgetInstanceCreated(object sender, WidgetImpl.WIdgetInstanceOnCreateArgs e)
50         {
51             OnCreate(e.ContentInfo, e.Window);
52         }
53
54         private void OnWidgetInstanceDestroyed(object sender, WidgetImpl.WIdgetInstanceOnDestroyArgs e)
55         {
56             OnTerminate(e.ContentInfo, e.TerminateType);
57         }
58
59         private void OnWidgetInstancePaused(object sender, EventArgs e)
60         {
61             OnPause();
62         }
63
64         private void OnWidgetInstanceResumed(object sender, EventArgs e)
65         {
66             OnResume();
67         }
68
69         private void OnWidgetInstanceResized(object sender, WidgetImpl.WidgetInstanceOnResizeArgs e)
70         {
71             OnResize(e.Window);
72         }
73
74         private void OnWidgetInstanceUpdated(object sender, WidgetImpl.WidgetInstanceOnUpdateArgs e)
75         {
76             OnUpdate(e.ContentInfo, e.Force);
77         }
78
79         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Widget obj)
80         {
81             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
82         }
83
84         /// <summary>
85         /// Dispose.
86         /// </summary>
87         /// <param name="type">The dispose type</param>
88         /// <since_tizen> 4 </since_tizen>
89         protected override void Dispose(DisposeTypes type)
90         {
91             if(disposed)
92             {
93                 return;
94             }
95
96             if(type == DisposeTypes.Explicit)
97             {
98                 //Called by User
99                 //Release your own managed resources here.
100                 //You should release all of your own disposable objects here.
101             }
102
103             if (swigCPtr.Handle != global::System.IntPtr.Zero)
104             {
105                 if (swigCMemOwn)
106                 {
107                     swigCMemOwn = false;
108                     NDalicManualPINVOKE.delete_Widget(swigCPtr);
109                 }
110                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
111             }
112
113             base.Dispose(type);
114         }
115         /// <summary>
116         /// Enumeration for termination type of widget
117         /// </summary>
118         /// <since_tizen> 4 </since_tizen>
119         public enum TerminationType
120         {
121             /// <summary>
122             /// User deleted this widget from the viewer
123             /// </summary>
124             /// <since_tizen> 4 </since_tizen>
125             Permanent,
126             /// <summary>
127             /// Widget is deleted because of other reasons (e.g. widget process is terminated temporarily by system)
128             /// </summary>
129             /// <since_tizen> 4 </since_tizen>
130             Temporary
131         }
132
133         /// <summary>
134         /// Creates a Widget handle.
135         /// </summary>
136         /// <since_tizen> 4 </since_tizen>
137         public Widget () : this (new WidgetImpl(), true) {
138                         if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
139                 }
140
141         internal Widget Assign(Widget widget)
142         {
143             Widget ret = new Widget(NDalicManualPINVOKE.Widget_Assign(swigCPtr, Widget.getCPtr(widget)), false);
144             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
145             return ret;
146         }
147
148         /// <summary>
149         /// The user should override this function to determine when they create widget.
150         /// </summary>
151         /// <param name="contentInfo">Information from WidgetView for creating. It contains previous status of widget which is sent by SetContentInfo before.</param>
152         /// <param name="window">Window for widget</param>
153         /// <since_tizen> 4 </since_tizen>
154         protected virtual void OnCreate(string contentInfo, Window window)
155         {
156         }
157
158         /// <summary>
159         /// The user should override this function to determine when they terminate widget.
160         /// </summary>
161         /// <param name="contentInfo">Data from WidgetView for deleting</param>
162         /// <param name="type">Termination type. When user delete widget view, termination type is PERMANENT.</param>
163         /// <since_tizen> 4 </since_tizen>
164         protected virtual void OnTerminate(string contentInfo, Widget.TerminationType type)
165         {
166         }
167
168         /// <summary>
169         /// The user should override this function to determine when they pause widget.
170         /// </summary>
171         /// <since_tizen> 4 </since_tizen>
172         protected virtual void OnPause()
173         {
174         }
175
176         /// <summary>
177         /// The user should override this function to determine when they resume widget.
178         /// </summary>
179         /// <since_tizen> 4 </since_tizen>
180         protected virtual void OnResume()
181         {
182         }
183
184         /// <summary>
185         /// The user should override this function to determine when they resize widget.
186         /// </summary>
187         /// <param name="window">Window for widget</param>
188         /// <since_tizen> 4 </since_tizen>
189         protected virtual void OnResize(Window window)
190         {
191         }
192
193         /// <summary>
194         /// The user should override this function to determine when they update widget.
195         /// </summary>
196         /// <param name="contentInfo">Data from WidgetView for updating</param>
197         /// <param name="force">Although the widget is paused, if it is true, the widget can be updated</param>
198         /// <since_tizen> 4 </since_tizen>
199         protected virtual void OnUpdate(string contentInfo, int force)
200         {
201         }
202
203         /// <summary>
204         /// Set content info to WidgetView.
205         /// </summary>
206         /// <param name="contentInfo">Content info is kind of context information which contains current status of widget.</param>
207         /// <since_tizen> 4 </since_tizen>
208         public void SetContentInfo(string contentInfo)
209         {
210             widgetImpl.SetContentInfo(contentInfo);
211         }
212
213         internal System.IntPtr GetIntPtr()
214         {
215             return swigCPtr.Handle;
216         }
217     }
218 }