b16f72ad12a41a9d7d364e2e146b5c3d7471260b
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Events / LongPressGestureDetector.cs
1 /*
2  * Copyright(c) 2019 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     /// This class emits a signals when a long press gesture occurs that meets the requirements set by the application.<br />
26     /// For any valid long press, two signals will be emitted:<br />
27     /// - First identifying the beginning (state = Started) i.e. when fingers held down for the required time.<br />
28     /// - Second identifying the ending (state = Finished) i.e. when fingers are released.<br />
29     /// </summary>
30     /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class LongPressGestureDetector : GestureDetector
33     {
34         /// <summary>
35         /// Constructor.
36         /// </summary>
37         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
38         [EditorBrowsable(EditorBrowsableState.Never)]
39         public LongPressGestureDetector() : this(Interop.LongPressGestureDetector.New(), true)
40         {
41             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
42         }
43
44         /// <summary>
45         /// Creates an initialized LongPressGestureDetector with the number of touches required.<br />
46         /// A long press gesture will be emitted from this detector if the number of fingers touching the screen is equal to the touches required.<br />
47         /// </summary>
48         /// <param name="touchesRequired">The number of touches required.</param>
49         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public LongPressGestureDetector(uint touchesRequired) : this(Interop.LongPressGestureDetector.New(touchesRequired), true)
52         {
53             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
54         }
55
56         /// <summary>
57         /// Creates an initialized LongPressGestureDetector with the minimum and maximum number of touches required.<br />
58         /// A long press gesture will be emitted from this detector if the number of fingers touching the screen falls between the minimum and maximum touches set.<br />
59         /// </summary>
60         /// <param name="minTouches">The minimum number of touches required.</param>
61         /// <param name="maxTouches">The maximum number of touches required.</param>
62         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         public LongPressGestureDetector(uint minTouches, uint maxTouches) : this(Interop.LongPressGestureDetector.New(minTouches, maxTouches), true)
65         {
66             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
67         }
68
69         /// <summary>
70         /// The copy constructor.
71         /// </summary>
72         /// <param name="handle">A reference to the copied handle</param>
73         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         public LongPressGestureDetector(LongPressGestureDetector handle) : this(Interop.LongPressGestureDetector.NewLongPressGestureDetector(LongPressGestureDetector.getCPtr(handle)), true)
76         {
77             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
78         }
79
80         internal LongPressGestureDetector(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
81         {
82         }
83
84         private DaliEventHandler<object, DetectedEventArgs> detectedEventHandler;
85         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
86         private delegate void DetectedCallbackType(IntPtr actor, IntPtr longPressGesture);
87         private DetectedCallbackType detectedCallback;
88
89         /// <summary>
90         /// This signal is emitted when the specified long press is detected on the attached view.
91         /// </summary>
92         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
93         [EditorBrowsable(EditorBrowsableState.Never)]
94         public event DaliEventHandler<object, DetectedEventArgs> Detected
95         {
96             add
97             {
98                 if (detectedEventHandler == null)
99                 {
100                     detectedCallback = OnLongPressGestureDetected;
101                     DetectedSignal().Connect(detectedCallback);
102                 }
103
104                 detectedEventHandler += value;
105             }
106
107             remove
108             {
109                 detectedEventHandler -= value;
110
111                 if (detectedEventHandler == null && DetectedSignal().Empty() == false)
112                 {
113                     DetectedSignal().Disconnect(detectedCallback);
114                 }
115             }
116         }
117
118         /// <summary>
119         /// Sets the number of touches required.<br />
120         /// The number of touches corresponds to the number of fingers a user has on the screen. The default is 1.<br />
121         /// </summary>
122         /// <param name="touches">Touches required</param>
123         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
124         [EditorBrowsable(EditorBrowsableState.Never)]
125         public void SetTouchesRequired(uint touches)
126         {
127             Interop.LongPressGestureDetector.SetTouchesRequired(SwigCPtr, touches);
128             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
129         }
130
131         /// <summary>
132         /// Sets the minimum and maximum touches required.
133         /// </summary>
134         /// <param name="minTouches">Minimum touches required.</param>
135         /// <param name="maxTouches">Maximum touches required.</param>
136         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         public void SetTouchesRequired(uint minTouches, uint maxTouches)
139         {
140             Interop.LongPressGestureDetector.SetTouchesRequired(SwigCPtr, minTouches, maxTouches);
141             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142         }
143
144         /// <summary>
145         /// Retrieves the minimum number of touches required.
146         /// </summary>
147         /// <returns>The minimum number of touches required.</returns>
148         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
149         [EditorBrowsable(EditorBrowsableState.Never)]
150         public uint GetMinimumTouchesRequired()
151         {
152             uint ret = Interop.LongPressGestureDetector.GetMinimumTouchesRequired(SwigCPtr);
153             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
154             return ret;
155         }
156
157         /// <summary>
158         /// Retrieves the maximum number of touches required.
159         /// </summary>
160         /// <returns>The maximum number of touches required.</returns>
161         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
162         [EditorBrowsable(EditorBrowsableState.Never)]
163         public uint GetMaximumTouchesRequired()
164         {
165             uint ret = Interop.LongPressGestureDetector.GetMaximumTouchesRequired(SwigCPtr);
166             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167             return ret;
168         }
169
170         internal static LongPressGestureDetector GetLongPressGestureDetectorFromPtr(global::System.IntPtr cPtr)
171         {
172             LongPressGestureDetector ret = new LongPressGestureDetector(cPtr, false);
173             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174             return ret;
175         }
176
177         internal new static LongPressGestureDetector DownCast(BaseHandle handle)
178         {
179             LongPressGestureDetector ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as LongPressGestureDetector;
180             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
181             return ret;
182         }
183
184         internal LongPressGestureDetector Assign(LongPressGestureDetector rhs)
185         {
186             LongPressGestureDetector ret = new LongPressGestureDetector(Interop.LongPressGestureDetector.Assign(SwigCPtr, LongPressGestureDetector.getCPtr(rhs)), false);
187             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188             return ret;
189         }
190
191         internal LongPressGestureDetectedSignal DetectedSignal()
192         {
193             LongPressGestureDetectedSignal ret = new LongPressGestureDetectedSignal(Interop.LongPressGestureDetector.DetectedSignal(SwigCPtr), false);
194             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
195             return ret;
196         }
197
198         /// <summary>
199         /// override it to clean-up your own resources.
200         /// </summary>
201         /// <param name="type"></param>
202         [EditorBrowsable(EditorBrowsableState.Never)]
203         protected override void Dispose(DisposeTypes type)
204         {
205             if (disposed)
206             {
207                 return;
208             }
209
210             if (type == DisposeTypes.Explicit)
211             {
212                 //Called by User
213                 //Release your own managed resources here.
214                 //You should release all of your own disposable objects here.
215             }
216
217             //Release your own unmanaged resources here.
218             //You should not access any managed member here except static instance.
219             //because the execution order of Finalizes is non-deterministic.
220
221             if (HasBody())
222             {
223                 if (detectedCallback != null)
224                 {
225                     using LongPressGestureDetectedSignal signal = new LongPressGestureDetectedSignal(Interop.LongPressGestureDetector.DetectedSignal(GetBaseHandleCPtrHandleRef), false);
226                     signal?.Disconnect(detectedCallback);
227                     detectedCallback = null;
228                 }
229             }
230             base.Dispose(type);
231         }
232
233         /// This will not be public opened.
234         [EditorBrowsable(EditorBrowsableState.Never)]
235         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
236         {
237             Interop.LongPressGestureDetector.DeleteLongPressGestureDetector(swigCPtr);
238         }
239
240         private void OnLongPressGestureDetected(IntPtr actor, IntPtr longPressGesture)
241         {
242             DetectedEventArgs e = new DetectedEventArgs();
243
244             // Populate all members of "e" (LongPressGestureEventArgs) with real data.
245             e.View = Registry.GetManagedBaseHandleFromNativePtr(actor) as View;
246             if (null == e.View)
247             {
248                 e.View = Registry.GetManagedBaseHandleFromRefObject(actor) as View;
249             }
250
251             // If DispatchGestureEvents is false, no gesture events are dispatched.
252             if (e.View != null && e.View.DispatchGestureEvents == false)
253             {
254                 return;
255             }
256
257             e.LongPressGesture = Tizen.NUI.LongPressGesture.GetLongPressGestureFromPtr(longPressGesture);
258
259             if (detectedEventHandler != null)
260             {
261                 //Here we send all data to user event handlers.
262                 detectedEventHandler(this, e);
263             }
264         }
265
266         /// <summary>
267         /// Event arguments that passed via the LongPressGestureEvent signal.
268         /// </summary>
269         /// <since_tizen> 5 </since_tizen>
270         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
271         [EditorBrowsable(EditorBrowsableState.Never)]
272         public class DetectedEventArgs : EventArgs
273         {
274             private View view;
275             private LongPressGesture longPressGesture;
276             private bool handled = true;
277
278             /// <summary>
279             /// View the attached view.
280             /// </summary>
281             /// <since_tizen> 5 </since_tizen>
282             /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
283             [EditorBrowsable(EditorBrowsableState.Never)]
284             public View View
285             {
286                 get
287                 {
288                     return view;
289                 }
290                 set
291                 {
292                     view = value;
293                 }
294             }
295
296             /// <summary>
297             /// The LongPressGesture.
298             /// </summary>
299             /// <since_tizen> 5 </since_tizen>
300             /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
301             [EditorBrowsable(EditorBrowsableState.Never)]
302             public LongPressGesture LongPressGesture
303             {
304                 get
305                 {
306                     return longPressGesture;
307                 }
308                 set
309                 {
310                     longPressGesture = value;
311                 }
312             }
313
314             /// <summary>
315             /// 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.
316             /// </summary>
317             [EditorBrowsable(EditorBrowsableState.Never)]
318             public bool Handled
319             {
320                 get => handled;
321                 set
322                 {
323                     handled = value;
324                     Interop.Actor.SetNeedGesturePropagation(View.getCPtr(view), !value);
325                     if (NDalicPINVOKE.SWIGPendingException.Pending)
326                         throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327                 }
328             }
329         }
330     }
331 }