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