ccf94aa0080e9b7373677f8272ec19e275d43c7e
[framework/osp/web.git] / src / controls / FWebCtrl_GestureState.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FWebCtrl_GestureState.cpp
20  * @brief               The file contains the definition of _Web class.
21  *
22  * The file contains the definition of _Web class.
23  */
24 #include <Ecore.h>
25 #include <EWebKit2.h>
26 #include <FBaseSysLog.h>
27 #include <FGrpPoint.h>
28 #include <FUi_CoordinateSystemUtils.h>
29 #include <FUi_Control.h>
30 #include <FUi_TouchManager.h>
31 #include <FUi_UiTouchEvent.h>
32 #include "FWebCtrl_GestureState.h"
33 #include "FWebCtrl_Web.h"
34 #include "FWebCtrl_WebSettingImpl.h"
35
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Ui;
40
41
42 namespace Tizen { namespace Web { namespace Controls
43 {
44
45
46 static const int  FLICK_SCROLL_WEIGHT = 3000;
47 static const double PINCH_ZOOM_FINGER_FACTOR = 1.0;
48 static const double PINCH_ZOOM_DISTANCE_TOLERANCE = 40.0;
49
50
51 void
52 SetGestureEvent(Ewk_Event_Gesture& gestureEvent, Ewk_Gesture_Type gestureType, const Point& absPoint, const Point& velocity, double scale, int count)
53 {
54         gestureEvent.type = gestureType;
55         gestureEvent.position.x = absPoint.x;
56         gestureEvent.position.y = absPoint.y;
57         gestureEvent.velocity.x = velocity.x;
58         gestureEvent.velocity.y = velocity.y;
59         gestureEvent.scale = scale;
60         gestureEvent.count = count;
61         gestureEvent.timestamp = ecore_time_get() * 1000;
62 }
63
64
65 _TapGestureState::_TapGestureState(_Web* pWeb)
66         : __pWebCore(pWeb)
67         , __longPressed(false)
68         , __doubleTapped(false)
69 {
70 }
71
72
73 _TapGestureState::~_TapGestureState(void)
74 {
75 }
76
77
78 bool
79 _TapGestureState::OnLongPressGestureDetected(_TouchLongPressGestureDetector& gesture)
80 {
81         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
82         SysAssertf(pSmartData, "Failed to get webkit smart data.");
83
84         Ewk_Event_Gesture gestureEvent;
85
86         SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, Point(0, 0), Point(0, 0), 0.0, 0);
87         pSmartData->api->gesture_move(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
88
89         __longPressed = true;
90
91         return true;
92 }
93
94
95 bool
96 _TapGestureState::OnTapGestureDetected(_TouchTapGestureDetector& gesture)
97 {
98         __doubleTapped = true;
99
100         return true;
101 }
102
103
104 bool
105 _TapGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
106 {
107         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
108         SysAssertf(pSmartData, "Failed to get webkit smart data.");
109
110         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
111
112         Ewk_Event_Gesture gestureEvent;
113
114         SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, 1);
115         pSmartData->api->gesture_start(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
116
117         __longPressed = false;
118         __doubleTapped = false;
119
120         return true;
121 }
122
123
124 bool
125 _TapGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
126 {
127         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
128         SysAssertf(pSmartData, "Failed to get webkit smart data.");
129
130         Ewk_Event_Gesture gestureEvent;
131
132         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
133
134         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
135         pSmartData->api->gesture_start(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
136
137         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_PANNING);
138
139         return true;
140 }
141
142
143 bool
144 _TapGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
145 {
146         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
147         SysAssertf(pSmartData, "Failed to get webkit smart data.");
148
149         Ewk_Event_Gesture gestureEvent;
150         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
151
152         if (__longPressed)
153         {
154                 Ewk_Hit_Test* pEwkHitTest = ewk_view_hit_test_new(__pWebCore->GetWebNativeNode(), absPoint.x, absPoint.y, EWK_HIT_TEST_MODE_ALL);
155                 SysTryReturn(NID_WEB_CTRL, pEwkHitTest, true, E_SYSTEM, "Failed to get hit test.");
156
157                 String tagName(ewk_hit_test_tag_name_get(pEwkHitTest));
158
159                 if (tagName.Equals(L"INPUT", false) || tagName.Equals(L"TEXTAREA", false))
160                 {
161                         Eina_Rectangle leftHandle;
162                         Eina_Rectangle rightHandle;
163
164                         ewk_view_text_selection_range_get(__pWebCore->GetWebNativeNode(), &leftHandle, &rightHandle);
165                         if ((rightHandle.x == 0) && (rightHandle.y == 0))
166                         {
167                                 SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, absPoint, Point(0, 0), 0.0, 1);
168                                 pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
169                         }
170                 }
171         }
172         else if (__doubleTapped)
173         {
174                 SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, 2);
175                 pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
176         }
177         else
178         {
179                 SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, 1);
180                 pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
181         }
182
183         return true;
184 }
185
186
187 bool
188 _TapGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
189 {
190         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
191         SysAssertf(pSmartData, "Failed to get webkit smart data.");
192
193         Ewk_Event_Gesture gestureEvent;
194
195         SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, Point(0, 0), Point(0, 0), 0.0, 0);
196         pSmartData->api->gesture_move(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
197
198         return true;
199 }
200
201
202 _PanningGestureState::_PanningGestureState(_Web* pWeb)
203         : __pWebCore(pWeb)
204 {
205 }
206
207
208 _PanningGestureState::~_PanningGestureState(void)
209 {
210 }
211
212
213 bool
214 _PanningGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
215 {
216         return true;
217 }
218
219
220 bool
221 _PanningGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
222 {
223         if (_WebSettingImpl::GetInstance(__pWebCore->GetSetting())->IsScrollEnabled())
224         {
225                 const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
226                 SysAssertf(pSmartData, "Failed to get webkit smart data.");
227
228                 Ewk_Event_Gesture gestureEvent;
229
230                 Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
231
232                 SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
233                 pSmartData->api->gesture_move(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
234         }
235
236         return true;
237 }
238
239
240 bool
241 _PanningGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
242 {
243         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
244         SysAssertf(pSmartData, "Failed to get webkit smart data.");
245
246         Ewk_Event_Gesture gestureEvent;
247
248         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
249
250         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
251         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
252
253         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
254
255         return true;
256 }
257
258
259 bool
260 _PanningGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
261 {
262         return OnTouchReleased(source, touchInfo);
263 }
264
265
266 _FlickGestureState::_FlickGestureState(_Web* pWeb)
267         : __pWebCore(pWeb)
268         , __velocity(0, 0)
269 {
270 }
271
272
273 _FlickGestureState::~_FlickGestureState(void)
274 {
275 }
276
277
278 bool
279 _FlickGestureState::OnFlickGestureDetected(_TouchFlickGestureDetector& gesture)
280 {
281         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_FLICK);
282
283         int duration = gesture.GetDuration();
284         gesture.GetDistance(__velocity.x, __velocity.y);
285
286         __velocity.x = (__velocity.x * FLICK_SCROLL_WEIGHT) / duration;
287         __velocity.y = (__velocity.y * FLICK_SCROLL_WEIGHT) / duration;
288
289         return true;
290 }
291
292
293 bool
294 _FlickGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
295 {
296         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
297         SysAssertf(pSmartData, "Failed to get webkit smart data.");
298
299         Ewk_Event_Gesture gestureEvent;
300
301         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
302
303         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
304         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
305
306         SetGestureEvent(gestureEvent, EWK_GESTURE_FLICK, Point(0, 0), Point(0, 0), 0.0, 0);
307         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
308
309         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
310
311         return true;
312 }
313
314
315 bool
316 _FlickGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
317 {
318         return true;
319 }
320
321
322 bool
323 _FlickGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
324 {
325         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
326         SysAssertf(pSmartData, "Failed to get webkit smart data.");
327
328         Ewk_Event_Gesture gestureEvent;
329
330         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
331
332         SetGestureEvent(gestureEvent, EWK_GESTURE_FLICK, absPoint, __velocity, 0, 0);
333         pSmartData->api->gesture_start(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
334
335         return true;
336 }
337
338
339 bool
340 _FlickGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
341 {
342         return OnTouchPressed(source, touchInfo);
343 }
344
345
346 _PinchGestureState::_PinchGestureState(_Web* pWeb)
347         : __pWebCore(pWeb)
348         , __standardScale(0.0)
349 {
350 }
351
352
353 _PinchGestureState::~_PinchGestureState(void)
354 {
355 }
356
357
358 bool
359 _PinchGestureState::OnPinchGestureStarted(_TouchPinchGestureDetector& gesture)
360 {
361         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_PINCH);
362
363         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
364         SysAssertf(pSmartData, "Failed to get webkit smart data.");
365
366         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(gesture.GetCenterPointF())));
367         __standardScale = static_cast< double >(gesture.GetScaleF());
368
369         Ewk_Event_Gesture gestureEvent;
370
371         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, absPoint, Point(0, 0), 1.0, 0);
372         pSmartData->api->gesture_start(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
373
374         return true;
375 }
376
377
378 bool
379 _PinchGestureState::OnPinchGestureChanged(_TouchPinchGestureDetector& gesture)
380 {
381         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
382         SysAssertf(pSmartData, "Failed to get webkit smart data.");
383
384         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(gesture.GetCenterPointF())));
385         double relScale = static_cast< double >(gesture.GetScaleF()) /__standardScale;
386
387         Ewk_Event_Gesture gestureEvent;
388
389         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, absPoint, Point(0, 0), relScale, 0);
390         pSmartData->api->gesture_move(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
391
392         return true;
393 }
394
395
396 bool
397 _PinchGestureState::OnPinchGestureFinished(_TouchPinchGestureDetector& gesture)
398 {
399         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
400         SysAssertf(pSmartData, "Failed to get webkit smart data.");
401
402         Ewk_Event_Gesture gestureEvent;
403
404         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, Point(0, 0), Point(0, 0), 0.0, 0);
405         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
406
407         return true;
408 }
409
410
411 bool
412 _PinchGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
413 {
414         return true;
415 }
416
417
418 bool
419 _PinchGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
420 {
421         return true;
422 }
423
424
425 bool
426 _PinchGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
427 {
428         _TouchManager* pTouchManager = _TouchManager::GetInstance();
429         SysAssertf(pTouchManager, "Failed to get touch manager.");
430
431         if(pTouchManager->GetPointCount() == 0)
432         {
433                 __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
434         }
435
436         return true;
437 }
438
439
440 bool
441 _PinchGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
442 {
443         const Ewk_View_Smart_Data* pSmartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get( __pWebCore->GetWebNativeNode()));
444         SysAssertf(pSmartData, "Failed to get webkit smart data.");
445
446         Ewk_Event_Gesture gestureEvent;
447
448         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, Point(0, 0), Point(0, 0), 0.0, 0);
449         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
450
451         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
452
453         return true;
454 }
455
456
457 }}}