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