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