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