add patch
[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         //To delete magnifier
94         _TouchManager* pTouchManager = _TouchManager::GetInstance();
95         SysAssertf(pTouchManager, "Failed to get touch manager.");
96
97         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(pTouchManager->GetPosition(pTouchManager->GetCurrentPointId())));
98
99         Ewk_Touch_Point* pPoint = static_cast< Ewk_Touch_Point* >(calloc(1, sizeof(Ewk_Touch_Point)));
100         pPoint->id = 0;
101         pPoint->x = absPoint.x;
102         pPoint->y = absPoint.y;
103         pPoint->state = EVAS_TOUCH_POINT_CANCEL;
104
105         Eina_List* pPointList = null;
106         pPointList = eina_list_append(pPointList, pPoint);
107
108         ewk_view_feed_touch_event(__pWebCore->GetWebNativeNode(), EWK_TOUCH_CANCEL, pPointList, null);
109
110         void* pData = null;
111         EINA_LIST_FREE(pPointList, pData)
112         free(pData);
113
114         __longPressed = true;
115
116         return true;
117 }
118
119
120 bool
121 _TapGestureState::OnTapGestureDetected(_TouchTapGestureDetector& gesture)
122 {
123         __doubleTapped = true;
124
125         return true;
126 }
127
128
129 bool
130 _TapGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
131 {
132         Ewk_Event_Gesture gestureEvent;
133         __pressedPosition = touchInfo.GetCurrentPosition();
134         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(__pressedPosition)));
135
136         SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, 1);
137
138         const Ewk_View_Smart_Data* pSmartData = static_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
139         SysAssertf(pSmartData, "Failed to get webkit smart data.");
140         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
141
142         __longPressed = false;
143         __doubleTapped = false;
144
145         return true;
146 }
147
148
149 bool
150 _TapGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
151 {
152         Ewk_Event_Gesture gestureEvent;
153         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
154
155         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
156
157         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
158         SysAssertf(pSmartData, "Failed to get webkit smart data.");
159         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
160
161         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_PANNING);
162
163         return true;
164 }
165
166
167 bool
168 _TapGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
169 {
170         if (__pWebCore->Contains(__pressedPosition))
171         {
172                 Evas_Object* pView = __pWebCore->GetWebNativeNode();
173                 Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(__pressedPosition)));
174
175                 if (__longPressed)
176                 {
177                         Ewk_Hit_Test* pEwkHitTest = ewk_view_hit_test_new(pView, absPoint.x, absPoint.y, EWK_HIT_TEST_MODE_ALL);
178                         SysTryReturn(NID_WEB_CTRL, pEwkHitTest, true, E_SYSTEM, "Failed to get hit test.");
179
180                         String tagName(ewk_hit_test_tag_name_get(pEwkHitTest));
181
182                         Eina_Hash* pAttrHash = ewk_hit_test_attribute_hash_get(pEwkHitTest);
183                         char* pValue = reinterpret_cast< char* >(eina_hash_find(pAttrHash, "contenteditable"));
184                         if (tagName.Equals(L"INPUT", false) || tagName.Equals(L"TEXTAREA", false) || pValue)
185                         {
186                                 Eina_Rectangle leftHandle;
187                                 Eina_Rectangle rightHandle;
188
189                                 ewk_view_text_selection_range_get(pView, &leftHandle, &rightHandle);
190                                 if (((rightHandle.x + rightHandle.w) == 0) && ((rightHandle.y + rightHandle.h) == 0))
191                                 {
192                                         Ewk_Event_Gesture gestureEvent;
193
194                                         SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, absPoint, Point(0, 0), 0.0, 1);
195
196                                         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
197                                         SysAssertf(pSmartData, "Failed to get webkit smart data.");
198                                         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
199                                 }
200                         }
201                 }
202                 else
203                 {
204                         Ewk_Event_Gesture gestureEvent;
205                         int touchCount = 1;
206                         if (__doubleTapped)
207                         {
208                                 touchCount = 2;
209                         }
210
211                         SetGestureEvent(gestureEvent, EWK_GESTURE_TAP, absPoint, Point(0, 0), 0.0, touchCount);
212
213                         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
214                         SysAssertf(pSmartData, "Failed to get webkit smart data.");
215                         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
216                 }
217
218                 String selectedText(ewk_view_text_selection_text_get(pView));
219                 if (selectedText.GetLength() > 0)
220                 {
221                         evas_object_smart_callback_call(pView, "text,selected", NULL);
222                 }
223         }
224         else
225         {
226                 OnTouchCanceled(source, touchInfo);
227         }
228
229         return true;
230 }
231
232
233 bool
234 _TapGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
235 {
236         Ewk_Event_Gesture gestureEvent;
237
238         SetGestureEvent(gestureEvent, EWK_GESTURE_LONG_PRESS, Point(0, 0), Point(0, 0), 0.0, 0);
239
240         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
241         SysAssertf(pSmartData, "Failed to get webkit smart data.");
242         pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
243
244         return true;
245 }
246
247
248 _PanningGestureState::_PanningGestureState(_Web* pWeb)
249         : __pWebCore(pWeb)
250         , __textSelected(false)
251         , __textChanged(false)
252         , __selectedText(L"")
253 {
254 }
255
256
257 _PanningGestureState::~_PanningGestureState(void)
258 {
259 }
260
261
262 void
263 _PanningGestureState::InitializeTextSelectionStatus(void)
264 {
265         __selectedText = String(ewk_view_text_selection_text_get(__pWebCore->GetWebNativeNode()));
266         if (__selectedText.GetLength() > 0)
267         {
268                 __textSelected = true;
269         }
270         else
271         {
272                 __textSelected = false;
273         }
274         __textChanged = false;
275 }
276
277
278 bool
279 _PanningGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
280 {
281         return true;
282 }
283
284
285 bool
286 _PanningGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
287 {
288         Evas_Object* pView = __pWebCore->GetWebNativeNode();
289         if (_WebSettingImpl::GetInstance(__pWebCore->GetSetting())->IsScrollEnabled())
290         {
291                 Ewk_Event_Gesture gestureEvent;
292                 Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
293
294                 SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
295
296                 const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(pView));
297                 SysAssertf(pSmartData, "Failed to get webkit smart data.");
298                 pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
299         }
300
301         if (__textSelected && !__textChanged)
302         {
303                 if (!__selectedText.Equals(String(ewk_view_text_selection_text_get(pView))))
304                 {
305                         __textChanged = true;
306                 }
307         }
308
309         return true;
310 }
311
312
313 bool
314 _PanningGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
315 {
316         Ewk_Event_Gesture gestureEvent;
317         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
318
319         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
320
321         Evas_Object* pView = __pWebCore->GetWebNativeNode();
322         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(pView));
323         SysAssertf(pSmartData, "Failed to get webkit smart data.");
324         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
325
326         if (__textChanged)
327         {
328                 evas_object_smart_callback_call(pView, "text,selected", NULL);
329         }
330
331         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
332
333         return true;
334 }
335
336
337 bool
338 _PanningGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
339 {
340         return OnTouchReleased(source, touchInfo);
341 }
342
343
344 _FlickGestureState::_FlickGestureState(_Web* pWeb)
345         : __pWebCore(pWeb)
346         , __velocity(0, 0)
347 {
348 }
349
350
351 _FlickGestureState::~_FlickGestureState(void)
352 {
353 }
354
355
356 bool
357 _FlickGestureState::OnFlickGestureDetected(_TouchFlickGestureDetector& gesture)
358 {
359         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_FLICK);
360
361         int duration = gesture.GetDuration();
362         gesture.GetDistance(__velocity.x, __velocity.y);
363
364         __velocity.x = (__velocity.x * FLICK_SCROLL_WEIGHT) / duration;
365         __velocity.y = (__velocity.y * FLICK_SCROLL_WEIGHT) / duration;
366
367         return true;
368 }
369
370
371 bool
372 _FlickGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
373 {
374         return true;
375 }
376
377
378 bool
379 _FlickGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
380 {
381         return true;
382 }
383
384
385 bool
386 _FlickGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
387 {
388         Ewk_Event_Gesture gestureEvent;
389         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(touchInfo.GetCurrentPosition())));
390
391         SetGestureEvent(gestureEvent, EWK_GESTURE_PAN, absPoint, Point(0, 0), 0.0, 0);
392
393         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
394         SysAssertf(pSmartData, "Failed to get webkit smart data.");
395         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
396
397         SetGestureEvent(gestureEvent, EWK_GESTURE_FLICK, absPoint, __velocity, 0.0, 0);
398
399         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
400
401         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
402
403         return true;
404 }
405
406
407 bool
408 _FlickGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
409 {
410         return OnTouchPressed(source, touchInfo);
411 }
412
413
414 _PinchGestureState::_PinchGestureState(_Web* pWeb)
415         : __pWebCore(pWeb)
416         , __standardScale(0.0)
417 {
418 }
419
420
421 _PinchGestureState::~_PinchGestureState(void)
422 {
423 }
424
425
426 bool
427 _PinchGestureState::OnPinchGestureStarted(_TouchPinchGestureDetector& gesture)
428 {
429         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_PINCH);
430
431         Ewk_Event_Gesture gestureEvent;
432         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(gesture.GetCenterPointF())));
433         __standardScale = static_cast< double >(gesture.GetScaleF());
434
435         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, absPoint, Point(0, 0), 1.0, 0);
436
437         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
438         SysAssertf(pSmartData, "Failed to get webkit smart data.");
439         pSmartData->api->gesture_start(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
440
441         return true;
442 }
443
444
445 bool
446 _PinchGestureState::OnPinchGestureChanged(_TouchPinchGestureDetector& gesture)
447 {
448         Ewk_Event_Gesture gestureEvent;
449         Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(gesture.GetCenterPointF())));
450         double relScale = static_cast< double >(gesture.GetScaleF()) /__standardScale;
451
452         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, absPoint, Point(0, 0), relScale, 0);
453
454         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
455         SysAssertf(pSmartData, "Failed to get webkit smart data.");
456         pSmartData->api->gesture_move(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
457
458         return true;
459 }
460
461
462 bool
463 _PinchGestureState::OnPinchGestureFinished(_TouchPinchGestureDetector& gesture)
464 {
465         Ewk_Event_Gesture gestureEvent;
466
467         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, Point(0, 0), Point(0, 0), 0.0, 0);
468
469         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
470         SysAssertf(pSmartData, "Failed to get webkit smart data.");
471         pSmartData->api->gesture_end(const_cast<Ewk_View_Smart_Data*>(pSmartData), &gestureEvent);
472
473         return true;
474 }
475
476
477 bool
478 _PinchGestureState::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
479 {
480         return true;
481 }
482
483
484 bool
485 _PinchGestureState::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
486 {
487         return true;
488 }
489
490
491 bool
492 _PinchGestureState::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
493 {
494         _TouchManager* pTouchManager = _TouchManager::GetInstance();
495         SysAssertf(pTouchManager, "Failed to get touch manager.");
496         if(pTouchManager->GetPointCount() == 0)
497         {
498                 __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
499         }
500
501         return true;
502 }
503
504
505 bool
506 _PinchGestureState::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
507 {
508         Ewk_Event_Gesture gestureEvent;
509
510         SetGestureEvent(gestureEvent, EWK_GESTURE_PINCH, Point(0, 0), Point(0, 0), 0.0, 0);
511
512         const Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(__pWebCore->GetWebNativeNode()));
513         SysAssertf(pSmartData, "Failed to get webkit smart data.");
514         pSmartData->api->gesture_end(const_cast< Ewk_View_Smart_Data* >(pSmartData), &gestureEvent);
515
516         __pWebCore->ChangeGesture(WEB_GESTURE_TYPE_TAP);
517
518         return true;
519 }
520
521
522 }}}