[NUI] Add DispatchGestureEvents and DispatchParentGestureEvents (#4609)
[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         /// This will not be public opened.
199         [EditorBrowsable(EditorBrowsableState.Never)]
200         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
201         {
202             if (detectedEventHandler == null && DetectedSignal().Empty() == false)
203             {
204                 DetectedSignal().Disconnect(detectedCallback);
205             }
206             Interop.LongPressGestureDetector.DeleteLongPressGestureDetector(swigCPtr);
207         }
208
209         private void OnLongPressGestureDetected(IntPtr actor, IntPtr longPressGesture)
210         {
211             DetectedEventArgs e = new DetectedEventArgs();
212
213             // Populate all members of "e" (LongPressGestureEventArgs) with real data.
214             e.View = Registry.GetManagedBaseHandleFromNativePtr(actor) as View;
215             if (null == e.View)
216             {
217                 e.View = Registry.GetManagedBaseHandleFromRefObject(actor) as View;
218             }
219
220             // If DispatchGestureEvents is false, no gesture events are dispatched.
221             if (e.View != null && e.View.DispatchGestureEvents == false)
222             {
223                 return;
224             }
225
226             e.LongPressGesture = Tizen.NUI.LongPressGesture.GetLongPressGestureFromPtr(longPressGesture);
227
228             if (detectedEventHandler != null)
229             {
230                 //Here we send all data to user event handlers.
231                 detectedEventHandler(this, e);
232             }
233         }
234
235         /// <summary>
236         /// Event arguments that passed via the LongPressGestureEvent signal.
237         /// </summary>
238         /// <since_tizen> 5 </since_tizen>
239         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
240         [EditorBrowsable(EditorBrowsableState.Never)]
241         public class DetectedEventArgs : EventArgs
242         {
243             private View view;
244             private LongPressGesture longPressGesture;
245             private bool handled = true;
246
247             /// <summary>
248             /// View the attached view.
249             /// </summary>
250             /// <since_tizen> 5 </since_tizen>
251             /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
252             [EditorBrowsable(EditorBrowsableState.Never)]
253             public View View
254             {
255                 get
256                 {
257                     return view;
258                 }
259                 set
260                 {
261                     view = value;
262                 }
263             }
264
265             /// <summary>
266             /// The LongPressGesture.
267             /// </summary>
268             /// <since_tizen> 5 </since_tizen>
269             /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
270             [EditorBrowsable(EditorBrowsableState.Never)]
271             public LongPressGesture LongPressGesture
272             {
273                 get
274                 {
275                     return longPressGesture;
276                 }
277                 set
278                 {
279                     longPressGesture = value;
280                 }
281             }
282
283             /// <summary>
284             /// 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.
285             /// </summary>
286             [EditorBrowsable(EditorBrowsableState.Never)]
287             public bool Handled
288             {
289                 get => handled;
290                 set
291                 {
292                     handled = value;
293                     Interop.Actor.SetNeedGesturePropagation(View.getCPtr(view), !value);
294                     if (NDalicPINVOKE.SWIGPendingException.Pending)
295                         throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296                 }
297             }
298         }
299     }
300 }