upload tizen_2.1 source
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / Pan.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "Pan.h"
22
23 #include "EwkViewImpl.h"
24 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
25 #include "LayerTreeCoordinatorProxy.h"
26 #endif
27 #include "PageClientImpl.h"
28 #include "WKAPICast.h"
29 #include "WKPageTizen.h"
30 #include "ewk_view_private.h"
31
32 using namespace WebCore;
33 using namespace WebKit;
34
35 Pan::Pan(Evas_Object* ewkView)
36     : m_ewkView(ewkView)
37     , m_panAnimator(0)
38     , m_lastPoint()
39     , m_currentPoint()
40 #if ENABLE(TIZEN_GESTURE_FEATURE)
41     , m_smoothAlgorithm(SmoothAlgorithm::create())
42 #endif
43 {
44     m_viewImpl = EwkViewImpl::fromEvasObject(m_ewkView);
45 #if ENABLE(TIZEN_GESTURE_FEATURE)
46     evas_object_event_callback_add(m_ewkView, EVAS_CALLBACK_MOUSE_DOWN, onMouseDown, this);
47     evas_object_event_callback_add(m_ewkView, EVAS_CALLBACK_MOUSE_MOVE, onMouseMove, this);
48 #endif
49 }
50
51 Pan::~Pan()
52 {
53     if (m_panAnimator)
54         ecore_animator_del(m_panAnimator);
55 #if ENABLE(TIZEN_GESTURE_FEATURE)
56     evas_object_event_callback_del(m_ewkView, EVAS_CALLBACK_MOUSE_DOWN, onMouseDown);
57     evas_object_event_callback_del(m_ewkView, EVAS_CALLBACK_MOUSE_MOVE, onMouseMove);
58 #endif
59 }
60
61 void Pan::start(const IntPoint& point)
62 {
63     if (m_panAnimator)
64         return;
65
66     PageClientImpl* pageClientImpl = ewkViewGetPageClient(m_ewkView);
67     EINA_SAFETY_ON_NULL_RETURN(pageClientImpl);
68
69 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
70     pageClientImpl->findScrollableNode(point);
71 #endif
72
73     ewkViewClearEdges(pageClientImpl->viewWidget());
74     // Below statement means the update() was not called before start().
75     if (m_lastPoint == IntPoint::zero())
76         m_lastPoint = point;
77     m_currentPoint = point;
78
79 #if ENABLE(TIZEN_GESTURE_FEATURE)
80     m_smoothAlgorithm->initialize();
81 #endif
82     // Process pan directly when Pan is started for situation that Pan is delayed by TouchEvent.
83     process();
84     m_panAnimator = ecore_animator_add(panAnimatorCallback, this);
85 }
86
87 void Pan::update(const IntPoint& point)
88 {
89     if (!m_panAnimator)
90         m_lastPoint = m_currentPoint;
91     m_currentPoint = point;
92 }
93
94 void Pan::stop()
95 {
96     if (m_panAnimator) {
97         ecore_animator_del(m_panAnimator);
98         m_panAnimator = 0;
99
100 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
101     PageClientImpl* pageClientImpl = ewkViewGetPageClient(m_ewkView);
102     EINA_SAFETY_ON_NULL_RETURN(pageClientImpl);
103     pageClientImpl->updateTextSelectionHandlesAndContextMenu(true);
104 #endif
105     }
106     m_lastPoint = m_currentPoint = IntPoint::zero();
107 }
108
109 Eina_Bool Pan::panAnimatorCallback(void* data)
110 {
111     static_cast<Pan*>(data)->process();
112     return ECORE_CALLBACK_RENEW;
113 }
114
115 void Pan::process()
116 {
117 #if ENABLE(TIZEN_GESTURE_FEATURE)
118     m_smoothAlgorithm->correctPoint(m_currentPoint);
119 #endif
120     int deltaX = m_lastPoint.x() - m_currentPoint.x();
121     int deltaY = m_lastPoint.y() - m_currentPoint.y();
122
123     PageClientImpl* pageClientImpl = ewkViewGetPageClient(m_ewkView);
124     EINA_SAFETY_ON_NULL_RETURN(pageClientImpl);
125
126     if (!deltaX && !deltaY)
127         return;
128
129     ewkViewSendScrollEvent(m_ewkView, deltaX, deltaY);
130     if (ewk_view_horizontal_panning_hold_get(m_ewkView))
131         deltaX = 0;
132     if (ewk_view_vertical_panning_hold_get(m_ewkView))
133         deltaY = 0;
134
135     // Get scrollPosition before scrolling.
136     IntPoint scrollPosition = pageClientImpl->scrollPosition();
137     if (WKPageScrollBy(toAPI(pageClientImpl->page()), toAPI(IntSize(deltaX, deltaY))))
138         ewkViewSendEdgeEvent(pageClientImpl->viewWidget(), scrollPosition, deltaX, deltaY);
139 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
140     pageClientImpl->updateTextSelectionHandlesAndContextMenu(false, true);
141 #endif
142     m_lastPoint = m_currentPoint;
143 }
144
145 #if ENABLE(TIZEN_GESTURE_FEATURE)
146 void Pan::onMouseDown(void* data, Evas*, Evas_Object*, void* eventInfo)
147 {
148     static_cast<Pan*>(data)->mouseDown(static_cast<Evas_Event_Mouse_Down*>(eventInfo));
149 }
150
151 void Pan::mouseDown(Evas_Event_Mouse_Down* event)
152 {
153     m_smoothAlgorithm->setEventPropagationTime(ecore_loop_time_get() - event->timestamp / 1000.0);
154 }
155
156 void Pan::onMouseMove(void* data, Evas*, Evas_Object*, void* eventInfo)
157 {
158     static_cast<Pan*>(data)->mouseMove(static_cast<Evas_Event_Mouse_Move*>(eventInfo));
159 }
160
161 void Pan::mouseMove(Evas_Event_Mouse_Move* event)
162 {
163     if (m_panAnimator)
164         m_smoothAlgorithm->storeDataToHistory(event->cur.canvas.x, event->cur.canvas.y, event->timestamp / 1000.0);
165 }
166 #endif