2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
5 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/page/AutoscrollController.h"
32 #include "core/page/EventHandler.h"
33 #include "core/frame/Frame.h"
34 #include "core/frame/FrameView.h"
35 #include "core/page/Chrome.h"
36 #include "core/page/Page.h"
37 #include "core/rendering/HitTestResult.h"
38 #include "core/rendering/RenderBox.h"
39 #include "wtf/CurrentTime.h"
43 // Delay time in second for start autoscroll if pointer is in border edge of scrollable element.
44 static double autoscrollDelay = 0.2;
46 PassOwnPtr<AutoscrollController> AutoscrollController::create(Page& page)
48 return adoptPtr(new AutoscrollController(page));
51 AutoscrollController::AutoscrollController(Page& page)
53 , m_autoscrollRenderer(0)
54 , m_autoscrollType(NoAutoscroll)
55 , m_dragAndDropAutoscrollStartTime(0)
59 bool AutoscrollController::autoscrollInProgress() const
61 return m_autoscrollType == AutoscrollForSelection;
64 bool AutoscrollController::autoscrollInProgress(const RenderBox* renderer) const
66 return m_autoscrollRenderer == renderer;
69 void AutoscrollController::startAutoscrollForSelection(RenderObject* renderer)
71 // We don't want to trigger the autoscroll or the panScroll if it's already active
72 if (m_autoscrollType != NoAutoscroll)
74 RenderBox* scrollable = RenderBox::findAutoscrollable(renderer);
77 m_autoscrollType = AutoscrollForSelection;
78 m_autoscrollRenderer = scrollable;
82 void AutoscrollController::stopAutoscroll()
84 RenderBox* scrollable = m_autoscrollRenderer;
85 m_autoscrollRenderer = 0;
90 scrollable->stopAutoscroll();
92 if (panScrollInProgress()) {
93 if (FrameView* view = scrollable->frame()->view()) {
94 view->removePanScrollIcon();
95 view->setCursor(pointerCursor());
100 m_autoscrollType = NoAutoscroll;
103 void AutoscrollController::stopAutoscrollIfNeeded(RenderObject* renderer)
105 if (m_autoscrollRenderer != renderer)
107 m_autoscrollRenderer = 0;
108 m_autoscrollType = NoAutoscroll;
111 void AutoscrollController::updateAutoscrollRenderer()
113 if (!m_autoscrollRenderer)
116 RenderObject* renderer = m_autoscrollRenderer;
119 HitTestResult hitTest = renderer->frame()->eventHandler().hitTestResultAtPoint(m_panScrollStartPos, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent);
121 if (Node* nodeAtPoint = hitTest.innerNode())
122 renderer = nodeAtPoint->renderer();
125 while (renderer && !(renderer->isBox() && toRenderBox(renderer)->canAutoscroll()))
126 renderer = renderer->parent();
127 m_autoscrollRenderer = renderer && renderer->isBox() ? toRenderBox(renderer) : 0;
130 void AutoscrollController::updateDragAndDrop(Node* dropTargetNode, const IntPoint& eventPosition, double eventTime)
132 if (!dropTargetNode || !dropTargetNode->renderer()) {
137 if (m_autoscrollRenderer && m_autoscrollRenderer->frame() != dropTargetNode->renderer()->frame())
140 RenderBox* scrollable = RenderBox::findAutoscrollable(dropTargetNode->renderer());
146 Page* page = scrollable->frame() ? scrollable->frame()->page() : 0;
152 IntSize offset = scrollable->calculateAutoscrollDirection(eventPosition);
153 if (offset.isZero()) {
158 m_dragAndDropAutoscrollReferencePosition = eventPosition + offset;
160 if (m_autoscrollType == NoAutoscroll) {
161 m_autoscrollType = AutoscrollForDragAndDrop;
162 m_autoscrollRenderer = scrollable;
163 m_dragAndDropAutoscrollStartTime = eventTime;
165 } else if (m_autoscrollRenderer != scrollable) {
166 m_dragAndDropAutoscrollStartTime = eventTime;
167 m_autoscrollRenderer = scrollable;
172 void AutoscrollController::handleMouseReleaseForPanScrolling(Frame* frame, const PlatformMouseEvent& mouseEvent)
174 Page* page = frame->page();
175 if (!page || page->mainFrame() != frame)
177 switch (m_autoscrollType) {
178 case AutoscrollForPan:
179 if (mouseEvent.button() == MiddleButton)
180 m_autoscrollType = AutoscrollForPanCanStop;
182 case AutoscrollForPanCanStop:
188 bool AutoscrollController::panScrollInProgress() const
190 return m_autoscrollType == AutoscrollForPanCanStop || m_autoscrollType == AutoscrollForPan;
193 void AutoscrollController::startPanScrolling(RenderBox* scrollable, const IntPoint& lastKnownMousePosition)
195 // We don't want to trigger the autoscroll or the panScroll if it's already active
196 if (m_autoscrollType != NoAutoscroll)
199 m_autoscrollType = AutoscrollForPan;
200 m_autoscrollRenderer = scrollable;
201 m_panScrollStartPos = lastKnownMousePosition;
203 if (FrameView* view = scrollable->frame()->view())
204 view->addPanScrollIcon(lastKnownMousePosition);
208 bool AutoscrollController::panScrollInProgress() const
214 // FIXME: This would get get better animation fidelity if it used the monotonicFrameBeginTime instead
215 // of WTF::currentTime().
216 void AutoscrollController::animate(double)
218 if (!m_autoscrollRenderer) {
223 EventHandler& eventHandler = m_autoscrollRenderer->frame()->eventHandler();
224 switch (m_autoscrollType) {
225 case AutoscrollForDragAndDrop:
226 if (WTF::currentTime() - m_dragAndDropAutoscrollStartTime > autoscrollDelay)
227 m_autoscrollRenderer->autoscroll(m_dragAndDropAutoscrollReferencePosition);
229 case AutoscrollForSelection:
230 if (!eventHandler.mousePressed()) {
234 eventHandler.updateSelectionForMouseDrag();
235 m_autoscrollRenderer->autoscroll(eventHandler.lastKnownMousePosition());
240 case AutoscrollForPanCanStop:
241 case AutoscrollForPan:
242 if (!panScrollInProgress()) {
246 if (FrameView* view = m_autoscrollRenderer->frame()->view())
247 updatePanScrollState(view, eventHandler.lastKnownMousePosition());
248 m_autoscrollRenderer->panScroll(m_panScrollStartPos);
252 if (m_autoscrollType != NoAutoscroll)
253 m_page.chrome().scheduleAnimation();
256 void AutoscrollController::startAutoscroll()
258 m_page.chrome().scheduleAnimation();
262 void AutoscrollController::updatePanScrollState(FrameView* view, const IntPoint& lastKnownMousePosition)
264 // At the original click location we draw a 4 arrowed icon. Over this icon there won't be any scroll
265 // So we don't want to change the cursor over this area
266 bool east = m_panScrollStartPos.x() < (lastKnownMousePosition.x() - ScrollView::noPanScrollRadius);
267 bool west = m_panScrollStartPos.x() > (lastKnownMousePosition.x() + ScrollView::noPanScrollRadius);
268 bool north = m_panScrollStartPos.y() > (lastKnownMousePosition.y() + ScrollView::noPanScrollRadius);
269 bool south = m_panScrollStartPos.y() < (lastKnownMousePosition.y() - ScrollView::noPanScrollRadius);
271 if (m_autoscrollType == AutoscrollForPan && (east || west || north || south))
272 m_autoscrollType = AutoscrollForPanCanStop;
276 view->setCursor(northEastPanningCursor());
278 view->setCursor(northWestPanningCursor());
280 view->setCursor(northPanningCursor());
283 view->setCursor(southEastPanningCursor());
285 view->setCursor(southWestPanningCursor());
287 view->setCursor(southPanningCursor());
289 view->setCursor(eastPanningCursor());
291 view->setCursor(westPanningCursor());
293 view->setCursor(middlePanningCursor());
297 } // namespace WebCore