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