Fix for webkit event issue
[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_WebImpl.h"
35 #include "FWebCtrl_WebSettingImpl.h"
36
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Web::Controls;
42
43
44 namespace Tizen { namespace Web { namespace Controls
45 {
46
47
48 static const int  FLICK_SCROLL_WEIGHT = 3000;
49 static const double PINCH_ZOOM_FINGER_FACTOR = 1.0;
50 static const double PINCH_ZOOM_DISTANCE_TOLERANCE = 40.0;
51
52
53 void
54 SetGestureEvent(Ewk_Event_Gesture& gestureEvent, Ewk_Gesture_Type gestureType, const Point& absPoint, const Point& velocity, double scale, int count)
55 {
56         gestureEvent.type = gestureType;
57         gestureEvent.position.x = absPoint.x;
58         gestureEvent.position.y = absPoint.y;
59         gestureEvent.velocity.x = velocity.x;
60         gestureEvent.velocity.y = velocity.y;
61         gestureEvent.scale = scale;
62         gestureEvent.count = count;
63         gestureEvent.timestamp = ecore_time_get() * 1000;
64 }
65
66
67 _TapGestureState::_TapGestureState(_Web* pWeb)
68         : __pWebCore(pWeb)
69         , __longPressed(false)
70         , __doubleTapped(false)
71         , __pressedPosition(0.0f, 0.0f)
72 {
73 }
74
75
76 _TapGestureState::~_TapGestureState(void)
77 {
78 }
79         
80
81 bool
82 _TapGestureState::OnLongPressGestureDetected(_TouchLongPressGestureDetector& gesture)
83 {
84         //To disable focus ring
85         Ewk_Event_Gesture gestureEvent;
86
87         SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, Point(0, 0), Point(0, 0), 0.0, 0);
88
89         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
90         SysAssertf(pSmartData, "Failed to get webkit smart data.");
91         pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
92
93         __longPressed = true;
94
95         return true;
96 }
97
98
99 bool
100 _TapGestureState::OnTapGestureDetected(_TouchTapGestureDetector& gesture)
101 {
102         __doubleTapped = true;
103
104         return true;
105 }
106
107
108 bool
109 _TapGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
110 {
111         Ewk_Event_Gesture gestureEvent;
112         __pressedPosition = touchInfo.GetCurrentPosition();
113         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(__pressedPosition)));
114
115         SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, 1);
116
117         const Ewk_View_Smart_Data* pSmartData = static_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
118         SysAssertf(pSmartData, "Failed to get webkit smart data.");
119         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
120
121         __longPressed = false;
122         __doubleTapped = false;
123
124         return true;
125 }
126
127
128 bool
129 _TapGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
130 {
131         Ewk_Event_Gesture gestureEvent;
132         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
133
134         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
135
136         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
137         SysAssertf(pSmartData, "Failed to get webkit smart data.");
138         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
139
140         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_PANNING);
141
142         if (!_WebSettingImpl::GetInstance(__pWebCore->GetSetting())->IsScrollEnabled())
143         {
144                 return false;
145         }
146
147         return true;
148 }
149
150
151 bool
152 _TapGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
153 {
154         if (!__pWebCore->Contains(__pressedPosition))
155         {
156                 return OnTouchCanceled(source, touchInfo);
157         }
158         
159         Evas_Object* pView = __pWebCore->GetWebNativeNode();
160         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(__pressedPosition)));
161
162         if (__longPressed)
163         {
164                 Ewk_Hit_Test* pEwkHitTest = ewk_view_hit_test_new(pView, absPoint.x, absPoint.y, EWK_HIT_TEST_MODE_ALL);
165                 SysTryReturn(NID_WEB_CTRL, pEwkHitTest, true, E_SYSTEM, "Failed to get hit test.");
166
167                 String tagName(ewk_hit_test_tag_name_get(pEwkHitTest));
168
169                 Eina_Hash* pAttrHash = ewk_hit_test_attribute_hash_get(pEwkHitTest);
170                 char* pValue = reinterpret_cast< char* >(eina_hash_find(pAttrHash, "contenteditable"));
171                 if (tagName.Equals(L"INPUT", false) || tagName.Equals(L"TEXTAREA", false) || pValue)
172                 {
173                         Eina_Rectangle leftHandle;
174                         Eina_Rectangle rightHandle;
175
176                         ewk_view_text_selection_range_get(pView, &leftHandle, &rightHandle);
177                         if (((rightHandle.x + rightHandle.w) == 0) && ((rightHandle.y + rightHandle.h) == 0))
178                         {
179                                 Ewk_Event_Gesture gestureEvent;
180
181                                 SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, absPoint, Point(0, 0), 0.0, 1);
182
183                                 const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(pView));
184                                 SysAssertf(pSmartData, "Failed to get webkit smart data.");
185                                 pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
186                         }
187                 }
188         }
189         else
190         {
191                 Ewk_Event_Gesture gestureEvent;
192                 int touchCount = 1;
193                 if (__doubleTapped)
194                 {
195                         touchCount = 2;
196                 }
197
198                 SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, touchCount);
199
200                 const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(pView));
201                 SysAssertf(pSmartData, "Failed to get webkit smart data.");
202                 pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
203         }
204
205         return true;
206 }
207
208
209 bool
210 _TapGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
211 {
212         Ewk_Event_Gesture gestureEvent;
213
214         SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, Point(0, 0), Point(0, 0), 0.0, 0);
215
216         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
217         SysAssertf(pSmartData, "Failed to get webkit smart data.");
218         pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
219
220         return true;
221 }
222
223
224 _PanningGestureState::_PanningGestureState(_Web* pWeb)
225         : __pWebCore(pWeb)
226 {
227 }
228
229
230 _PanningGestureState::~_PanningGestureState(void)
231 {
232 }
233
234
235 bool
236 _PanningGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
237 {
238         return true;
239 }
240
241
242 bool
243 _PanningGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
244 {
245         if (!_WebSettingImpl::GetInstance(__pWebCore->GetSetting())->IsScrollEnabled())
246         {
247                 return false;
248         }
249
250         Ewk_Event_Gesture gestureEvent;
251         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
252
253         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
254
255         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
256         SysAssertf(pSmartData, "Failed to get webkit smart data.");
257         pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
258
259         return true;
260 }
261
262
263 bool
264 _PanningGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
265 {
266         Ewk_Event_Gesture gestureEvent;
267         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
268
269         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
270
271         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
272         SysAssertf(pSmartData, "Failed to get webkit smart data.");
273         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
274
275         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
276
277         return true;
278 }
279
280
281 bool
282 _PanningGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
283 {
284         return OnTouchReleased(source, touchInfo);
285 }
286
287
288 _FlickGestureState::_FlickGestureState(_Web* pWeb)
289         : __pWebCore(pWeb)
290         , __velocity(0, 0)
291 {
292 }
293
294
295 _FlickGestureState::~_FlickGestureState(void)
296 {
297 }
298
299
300 bool
301 _FlickGestureState::OnFlickGestureDetected(_TouchFlickGestureDetector& gesture)
302 {
303         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_FLICK);
304
305         if (_WebSettingImpl::GetInstance(__pWebCore->GetSetting())->IsScrollEnabled())
306         {
307                 int duration = gesture.GetDuration();
308                 gesture.GetDistance(__velocity.x, __velocity.y);
309
310                 __velocity.x = (__velocity.x * FLICK_SCROLL_WEIGHT) / duration;
311                 __velocity.y = (__velocity.y * FLICK_SCROLL_WEIGHT) / duration;
312         }
313
314         return true;
315 }
316
317
318 bool
319 _FlickGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
320 {
321         return true;
322 }
323
324
325 bool
326 _FlickGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
327 {
328         return true;
329 }
330
331
332 bool
333 _FlickGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
334 {
335         Ewk_Event_Gesture gestureEvent;
336         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
337
338         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
339
340         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
341         SysAssertf(pSmartData, "Failed to get webkit smart data.");
342         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
343
344         if (_WebSettingImpl::GetInstance(__pWebCore->GetSetting())->IsScrollEnabled())
345         {
346                 SetGestureEvent(gestureEvent, EWK_GESTURE_FLICK, absPoint, __velocity, 0.0, 0);
347
348                 pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
349         }
350
351         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
352
353         return true;
354 }
355
356
357 bool
358 _FlickGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
359 {
360         return OnTouchPressed(source, touchInfo);
361 }
362
363
364 _PinchGestureState::_PinchGestureState(_Web* pWeb)
365         : __pWebCore(pWeb)
366         , __standardScale(0.0)
367 {
368 }
369
370
371 _PinchGestureState::~_PinchGestureState(void)
372 {
373 }
374
375
376 bool
377 _PinchGestureState::OnPinchGestureStarted(_TouchPinchGestureDetector& gesture)
378 {
379         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_PINCH);
380
381         Ewk_Event_Gesture gestureEvent;
382         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(gesture.GetCenterPointF())));
383         __standardScale = static_cast< double >(gesture.GetScaleF());
384
385         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, absPoint, Point(0, 0), 1.0, 0);
386
387         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
388         SysAssertf(pSmartData, "Failed to get webkit smart data.");
389         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
390
391         return true;
392 }
393
394
395 bool
396 _PinchGestureState::OnPinchGestureChanged(_TouchPinchGestureDetector& gesture)
397 {
398         Ewk_Event_Gesture gestureEvent;
399         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(gesture.GetCenterPointF())));
400         double relScale = static_cast< double >(gesture.GetScaleF()) /__standardScale;
401
402         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, absPoint, Point(0, 0), relScale, 0);
403
404         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
405         SysAssertf(pSmartData, "Failed to get webkit smart data.");
406         pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
407
408         return true;
409 }
410
411
412 bool
413 _PinchGestureState::OnPinchGestureFinished(_TouchPinchGestureDetector& gesture)
414 {
415         Ewk_Event_Gesture gestureEvent;
416
417         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, Point(0, 0), Point(0, 0), 0.0, 0);
418
419         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
420         SysAssertf(pSmartData, "Failed to get webkit smart data.");
421         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
422
423         return true;
424 }
425
426
427 bool
428 _PinchGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
429 {
430         return true;
431 }
432
433
434 bool
435 _PinchGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
436 {
437         return true;
438 }
439
440
441 bool
442 _PinchGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
443 {
444         _TouchManager* pTouchManager = _TouchManager::GetInstance();
445         SysAssertf(pTouchManager, "Failed to get touch manager.");
446         if(pTouchManager->GetPointCount() == 0)
447         {
448                 __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
449         }
450
451         return true;
452 }
453
454
455 bool
456 _PinchGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
457 {
458         Ewk_Event_Gesture gestureEvent;
459
460         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, Point(0, 0), Point(0, 0), 0.0, 0);
461
462         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
463         SysAssertf(pSmartData, "Failed to get webkit smart data.");
464         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
465
466         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
467
468         return true;
469 }
470
471
472 }}}