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