Add ScriptUI to support XAML file (#320)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseHandle.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 using System;
18 using System.ComponentModel;
19 using System.Runtime.CompilerServices;
20 using Tizen.NUI.Binding;
21 using Tizen.NUI.Binding.Internals;
22
23 namespace Tizen.NUI
24 {
25
26     /// <summary>
27     /// BaseHandle is a handle to an internal Dali resource.
28     /// </summary>
29     /// <since_tizen> 3 </since_tizen>
30     public class BaseHandle : Element, global::System.IDisposable
31     {
32         /// <summary>
33         /// Event which is occurred when a property is set
34         /// </summary>
35         /// <since_tizen> 5 </since_tizen>
36         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         public event PropertyChangedEventHandler PropertySet;
39
40         internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
41         {
42             PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
43         }
44
45         internal static readonly BindablePropertyKey NavigationPropertyKey = BindableProperty.CreateReadOnly("Navigation", typeof(INavigation), typeof(/*VisualElement*/BaseHandle), default(INavigation));
46         /// <summary>
47         /// Backing store for the Navigation property.
48         /// </summary>
49         internal static readonly BindableProperty NavigationProperty = NavigationPropertyKey.BindableProperty;
50
51         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
52         /// <summary>
53         /// swigCMemOwn
54         /// </summary>
55         /// <since_tizen> 3 </since_tizen>
56         protected bool swigCMemOwn;
57         private bool _registerMe;
58
59         internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
60         {
61             //to catch derived classes dali native exceptions
62             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
63
64             _registerMe = swigCMemOwn = cMemoryOwn;
65             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
66
67             // using copy constructor to create another native handle so Registry.Unregister works fine.
68             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
69
70             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
71
72             if (_registerMe)
73             {
74
75                 // Register this instance of BaseHandle in the registry.
76                 Registry.Register(this);
77             }
78         }
79
80         internal BaseHandle(global::System.IntPtr cPtr)
81         {
82             _registerMe = swigCMemOwn = true;
83
84             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
85
86             // using copy constructor to create another native handle so Registry.Unregister works fine.
87             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
88             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
89
90             if (_registerMe)
91             {
92                 // Register this instance of BaseHandle in the registry.
93                 Registry.Register(this);
94             }
95         }
96
97         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
98         {
99             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
100         }
101
102         //A Flag to check who called Dispose(). (By User or DisposeQueue)
103         private bool isDisposeQueued = false;
104
105         /// <summary>
106         /// A Flat to check if it is already disposed.
107         /// </summary>
108         /// <since_tizen> 3 </since_tizen>
109         protected bool disposed = false;
110
111         /// <summary>
112         /// Dispose.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         ~BaseHandle()
116         {
117             if (!isDisposeQueued)
118             {
119                 isDisposeQueued = true;
120                 DisposeQueue.Instance.Add(this);
121             }
122         }
123
124         /// <summary>
125         /// Dispose.
126         /// </summary>
127         /// <since_tizen> 3 </since_tizen>
128         public void Dispose()
129         {
130             //Throw excpetion if Dispose() is called in separate thread.
131             if (!Window.IsInstalled())
132             {
133                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
134             }
135
136             if (isDisposeQueued)
137             {
138                 Dispose(DisposeTypes.Implicit);
139             }
140             else
141             {
142                 Dispose(DisposeTypes.Explicit);
143                 System.GC.SuppressFinalize(this);
144             }
145         }
146
147         /// <summary>
148         /// Dispose.
149         /// </summary>
150         /// <since_tizen> 3 </since_tizen>
151         protected virtual void Dispose(DisposeTypes type)
152         {
153             if (disposed)
154             {
155                 return;
156             }
157
158             if (type == DisposeTypes.Explicit)
159             {
160                 //Called by User
161                 //Release your own managed resources here.
162                 //You should release all of your own disposable objects here.
163
164             }
165
166             //Release your own unmanaged resources here.
167             //You should not access any managed member here except static instance.
168             //because the execution order of Finalizes is non-deterministic.
169
170             //Unreference this instance from Registry.
171             if (_registerMe)
172             {
173                 Registry.Unregister(this);
174             }
175
176             if (swigCPtr.Handle != global::System.IntPtr.Zero)
177             {
178                 swigCMemOwn = false;
179                 NDalicPINVOKE.delete_BaseHandle(swigCPtr);
180                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
181             }
182
183             disposed = true;
184         }
185
186         /// <summary>
187         /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
188         /// </summary>
189         /// <since_tizen> 3 </since_tizen>
190         public static bool operator true(BaseHandle handle)
191         {
192             // if the C# object is null, return false
193             if (BaseHandle.ReferenceEquals(handle, null))
194             {
195                 return false;
196             }
197             // returns true if the handle has a body, false otherwise
198             return handle.HasBody();
199         }
200
201         /// <summary>
202         /// Returns the bool false  to indicate that an operand is false and returns true otherwise.
203         /// </summary>
204         /// <since_tizen> 3 </since_tizen>
205         public static bool operator false(BaseHandle handle)
206         {
207             // if the C# object is null, return true
208             if (BaseHandle.ReferenceEquals(handle, null))
209             {
210                 return true;
211             }
212             return !handle.HasBody();
213         }
214
215         /// <summary>
216         /// Explicit conversion from Handle to bool.
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         public static explicit operator bool(BaseHandle handle)
220         {
221             // if the C# object is null, return false
222             if (BaseHandle.ReferenceEquals(handle, null))
223             {
224                 return false;
225             }
226             // returns true if the handle has a body, false otherwise
227             return handle.HasBody();
228         }
229
230         /// <summary>
231         /// Equality operator
232         /// </summary>
233         /// <since_tizen> 3 </since_tizen>
234         public static bool operator ==(BaseHandle x, BaseHandle y)
235         {
236             // if the C# objects are the same return true
237             if (BaseHandle.ReferenceEquals(x, y))
238             {
239                 return true;
240             }
241             if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
242             {
243                 // drop into native code to see if both handles point to the same body
244                 return x.IsEqual(y);
245             }
246
247             if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
248             {
249                 if (y.HasBody()) return false;
250                 else return true;
251             }
252             if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
253             {
254                 if (x.HasBody()) return false;
255                 else return true;
256             }
257
258             return false;
259         }
260
261         /// <summary>
262         /// Inequality operator. Returns Null if either operand is Null
263         /// </summary>
264         /// <since_tizen> 3 </since_tizen>
265         public static bool operator !=(BaseHandle x, BaseHandle y)
266         {
267             return !(x == y);
268         }
269
270         /// <summary>
271         /// Logical AND operator.<br />
272         /// It's possible when doing a  operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
276         {
277             if (x == y)
278             {
279                 return x;
280             }
281             return null;
282         }
283
284         /// <summary>
285         /// Logical OR operator for ||.<br />
286         /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
287         /// </summary>
288         /// <since_tizen> 3 </since_tizen>
289         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
290         {
291             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
292             {
293                 if (x.HasBody())
294                 {
295                     return x;
296                 }
297                 if (y.HasBody())
298                 {
299                     return y;
300                 }
301                 return null;
302             }
303             return null;
304         }
305
306         /// <summary>
307         /// Logical ! operator
308         /// </summary>
309         /// <since_tizen> 3 </since_tizen>
310         public static bool operator !(BaseHandle x)
311         {
312             // if the C# object is null, return true
313             if (BaseHandle.ReferenceEquals(x, null))
314             {
315                 return true;
316             }
317             if (x.HasBody())
318             {
319                 return false;
320             }
321             return true;
322         }
323
324         /// <summary>
325         /// Equals
326         /// </summary>
327         /// <param name="o">The object should be compared.</param>
328         /// <returns>True if equal.</returns>
329         /// <since_tizen> 5 </since_tizen>
330         public override bool Equals(object o)
331         {
332             return base.Equals(o);
333         }
334
335         /// <summary>
336         /// Gets the the hash code of this baseHandle.
337         /// </summary>
338         /// <returns>The Hash Code.</returns>
339         /// <since_tizen> 5 </since_tizen>
340         public override int GetHashCode()
341         {
342             return base.GetHashCode();
343         }
344
345         /// <summary>
346         /// Create an instance of BaseHandle.
347         /// </summary>
348         /// <since_tizen> 3 </since_tizen>
349         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
350         {
351             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
352         }
353
354         /// <summary>
355         /// Create an instance of BaseHandle.
356         /// </summary>
357         /// <param name="handle">The BaseHandle instance.</param>
358         /// <since_tizen> 3 </since_tizen>
359         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
360         {
361             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
362         }
363
364
365         /// <summary>
366         /// Performs an action on this object with the given action name and attributes.
367         /// </summary>
368         /// <param name="actionName">The command for the action.</param>
369         /// <param name="attributes">The list of attributes for the action.</param>
370         /// <returns>The action is performed by the object or not.</returns>
371         /// <since_tizen> 3 </since_tizen>
372         public bool DoAction(string actionName, PropertyMap attributes)
373         {
374             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
375             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
376             return ret;
377         }
378
379         /// <summary>
380         /// Returns the type name for the Handle.<br />
381         /// Will return an empty string if the typename does not exist. This will happen for types that
382         /// have not registered with type-registry.
383         /// </summary>
384         /// <returns>The type name. Empty string if the typename does not exist.</returns>
385         /// <since_tizen> 3 </since_tizen>
386         public string GetTypeName()
387         {
388             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
389             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
390             return ret;
391         }
392
393         /// <summary>
394         /// Returns the type info for the Handle.<br />
395         /// </summary>
396         /// <param name="info">The type information.</param>
397         /// <returns>True If get the type info.</returns>
398         /// <since_tizen> 3 </since_tizen>
399         public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
400         {
401             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, Tizen.NUI.TypeInfo.getCPtr(info));
402             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403             return ret;
404         }
405
406         /// <summary>
407         /// Resets the handle.
408         /// </summary>
409         /// <since_tizen> 3 </since_tizen>
410         public void Reset()
411         {
412             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
413             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
414         }
415
416         /// <summary>
417         /// To check the BaseHandle instance is equal or not.
418         /// </summary>
419         /// <param name="rhs">The baseHandle instance.</param>
420         /// <returns>True If equal.</returns>
421         /// <since_tizen> 3 </since_tizen>
422         public bool EqualTo(BaseHandle rhs)
423         {
424             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426             return ret;
427         }
428
429         /// <summary>
430         /// To check the BaseHandle instance is equal or not.
431         /// </summary>
432         /// <param name="rhs">The baseHandle instance.</param>
433         /// <returns>True If not equal.</returns>
434         /// <since_tizen> 3 </since_tizen>
435         public bool NotEqualTo(BaseHandle rhs)
436         {
437             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
438             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
439             return ret;
440         }
441
442         internal RefObject GetObjectPtr()
443         {
444             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
445             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
446             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447             return ret;
448         }
449
450         /// <summary>
451         /// To check the BaseHandle instance has body or not.
452         /// </summary>
453         /// <returns>True If the baseHandle instance has body.</returns>
454         /// <since_tizen> 3 </since_tizen>
455         public bool HasBody()
456         {
457             if (disposed == true)
458             {
459                 return false;
460             }
461
462             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
463             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
464             return ret;
465         }
466
467         /// <summary>
468         /// To check the BaseHandle instance is equal or not.
469         /// </summary>
470         /// <param name="rhs">The baseHandle instance.</param>
471         /// <returns>True If equal.</returns>
472         /// <since_tizen> 3 </since_tizen>
473         public bool IsEqual(BaseHandle rhs)
474         {
475             if (disposed == true)
476             {
477                 return false;
478             }
479
480             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
481             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482             return ret;
483         }
484
485         internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
486         {
487             get
488             {
489                 return swigCPtr;
490             }
491         }
492
493         /// <summary>
494         /// For internal use.
495         /// </summary>
496         [EditorBrowsable(EditorBrowsableState.Never)]
497         internal NavigationProxy NavigationProxy
498         {
499             get { return Navigation as NavigationProxy; }
500         }
501
502         /// <summary>
503         /// Gets the navigation.
504         /// </summary>
505         internal INavigation Navigation
506         {
507             get { return (INavigation)GetValue(NavigationProperty); }
508             set { SetValue(NavigationPropertyKey, value); }
509         }
510
511         /// <summary>
512         /// Contains event arguments for the FocusChangeRequested event.
513         /// </summary>
514         public class FocusRequestArgs : EventArgs
515         {
516
517             /// <summary>
518             /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
519             /// </summary>
520             public bool Focus { get; set; }
521
522             /// <summary>
523             /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
524             /// </summary>
525             public bool Result { get; set; }
526         }
527     }
528
529 }