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