Do not register when we try to copy same handle
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Events / PinchGestureDetector.cs
1 /*
2  * Copyright(c) 2021 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.Runtime.InteropServices;
19 using Tizen.NUI.BaseComponents;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// It tries to detect when the user moves two touch points towards or away from each other.
26     /// </summary>
27     /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class PinchGestureDetector : GestureDetector
30     {
31         /// <summary>
32         /// Creates an initialized PinchGestureDetector.
33         /// </summary>
34         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public PinchGestureDetector() : this(Interop.PinchGesture.PinchGestureDetectorNew(), true)
37         {
38             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
39
40         }
41
42         /// <summary>
43         /// The copy constructor.
44         /// </summary>
45         /// <param name="handle">A reference to the copied handle</param>
46         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
47         [EditorBrowsable(EditorBrowsableState.Never)]
48         public PinchGestureDetector(PinchGestureDetector handle) : this(Interop.PinchGesture.NewPinchGestureDetector(PinchGestureDetector.getCPtr(handle)), true, false)
49         {
50             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
51         }
52
53         internal PinchGestureDetector(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, cMemoryOwn)
54         {
55         }
56
57         internal PinchGestureDetector(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
58         {
59         }
60
61         private DaliEventHandler<object, DetectedEventArgs> detectedEventHandler;
62         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
63         private delegate void DetectedCallbackType(IntPtr actor, IntPtr pinchGesture);
64         private DetectedCallbackType detectedCallback;
65
66         /// <summary>
67         /// This signal is emitted when the specified pinch is detected on the attached view.
68         /// </summary>
69         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         public event DaliEventHandler<object, DetectedEventArgs> Detected
72         {
73             add
74             {
75                 if (detectedEventHandler == null)
76                 {
77                     detectedCallback = OnPinchGestureDetected;
78                     DetectedSignal().Connect(detectedCallback);
79                 }
80
81                 detectedEventHandler += value;
82             }
83
84             remove
85             {
86                 detectedEventHandler -= value;
87
88                 if (detectedEventHandler == null && DetectedSignal().Empty() == false)
89                 {
90                     DetectedSignal().Disconnect(detectedCallback);
91                 }
92             }
93         }
94
95
96         internal static PinchGestureDetector GetPinchGestureDetectorFromPtr(global::System.IntPtr cPtr)
97         {
98             PinchGestureDetector ret = new PinchGestureDetector(cPtr, false);
99             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
100             return ret;
101         }
102
103         internal new static PinchGestureDetector DownCast(BaseHandle handle)
104         {
105             PinchGestureDetector ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as PinchGestureDetector;
106             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
107             return ret;
108         }
109
110         internal PinchGestureDetector Assign(PinchGestureDetector rhs)
111         {
112             PinchGestureDetector ret = new PinchGestureDetector(Interop.PinchGesture.PinchGestureDetectorAssign(SwigCPtr, PinchGestureDetector.getCPtr(rhs)), false);
113             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
114             return ret;
115         }
116
117         internal PinchGestureDetectedSignal DetectedSignal()
118         {
119             PinchGestureDetectedSignal ret = new PinchGestureDetectedSignal(Interop.PinchGesture.PinchGestureDetectorDetectedSignal(SwigCPtr), false);
120             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121             return ret;
122         }
123
124         /// <summary>
125         /// override it to clean-up your own resources.
126         /// </summary>
127         /// <param name="type"></param>
128         [EditorBrowsable(EditorBrowsableState.Never)]
129         protected override void Dispose(DisposeTypes type)
130         {
131             if (disposed)
132             {
133                 return;
134             }
135
136             if (type == DisposeTypes.Explicit)
137             {
138                 //Called by User
139                 //Release your own managed resources here.
140                 //You should release all of your own disposable objects here.
141             }
142
143             //Release your own unmanaged resources here.
144             //You should not access any managed member here except static instance.
145             //because the execution order of Finalizes is non-deterministic.
146
147             if (HasBody())
148             {
149                 if (detectedCallback != null)
150                 {
151                     using PinchGestureDetectedSignal signal = new PinchGestureDetectedSignal(Interop.PinchGesture.PinchGestureDetectorDetectedSignal(GetBaseHandleCPtrHandleRef), false);
152                     signal?.Disconnect(detectedCallback);
153                     detectedCallback = null;
154                 }
155             }
156             base.Dispose(type);
157         }
158
159         /// This will not be public opened.
160         [EditorBrowsable(EditorBrowsableState.Never)]
161         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
162         {
163             Interop.PinchGesture.DeletePinchGestureDetector(swigCPtr);
164         }
165
166         private void OnPinchGestureDetected(IntPtr actor, IntPtr pinchGesture)
167         {
168             if (detectedEventHandler != null)
169             {
170                 DetectedEventArgs e = new DetectedEventArgs();
171
172                 // Populate all members of "e" (DetectedEventArgs) with real data.
173                 e.View = Registry.GetManagedBaseHandleFromNativePtr(actor) as View;
174                 if (null == e.View)
175                 {
176                     e.View = Registry.GetManagedBaseHandleFromRefObject(actor) as View;
177                 }
178
179                 // If DispatchGestureEvents is false, no gesture events are dispatched.
180                 if (e.View != null && e.View.DispatchGestureEvents == false)
181                 {
182                     return;
183                 }
184
185                 e.PinchGesture = Tizen.NUI.PinchGesture.GetPinchGestureFromPtr(pinchGesture);
186                 //Here we send all data to user event handlers.
187                 detectedEventHandler(this, e);
188             }
189         }
190
191         /// <summary>
192         /// Event arguments that passed via the PinchGestureEvent signal.
193         /// </summary>
194         /// <since_tizen> 5 </since_tizen>
195         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
196         [EditorBrowsable(EditorBrowsableState.Never)]
197         public class DetectedEventArgs : EventArgs
198         {
199             private View view;
200             private PinchGesture pinchGesture;
201             private bool handled = true;
202
203             /// <summary>
204             /// The attached view.
205             /// </summary>
206             /// <since_tizen> 5 </since_tizen>
207             /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
208             [EditorBrowsable(EditorBrowsableState.Never)]
209             public View View
210             {
211                 get
212                 {
213                     return view;
214                 }
215                 set
216                 {
217                     view = value;
218                 }
219             }
220
221             /// <summary>
222             /// The PinchGesture.
223             /// </summary>
224             /// <since_tizen> 5 </since_tizen>
225             /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
226             [EditorBrowsable(EditorBrowsableState.Never)]
227             public PinchGesture PinchGesture
228             {
229                 get
230                 {
231                     return pinchGesture;
232                 }
233                 set
234                 {
235                     pinchGesture = value;
236                 }
237             }
238
239             /// <summary>
240             /// Gets or sets a value that indicates whether the event handler has completely handled the event or whether the system should continue its own processing.
241             /// </summary>
242             [EditorBrowsable(EditorBrowsableState.Never)]
243             public bool Handled
244             {
245                 get => handled;
246                 set
247                 {
248                     handled = value;
249                     Interop.Actor.SetNeedGesturePropagation(View.getCPtr(view), !value);
250                     if (NDalicPINVOKE.SWIGPendingException.Pending)
251                         throw NDalicPINVOKE.SWIGPendingException.Retrieve();
252                 }
253             }
254         }
255     }
256 }