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