| 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
| 4 | * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies) |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "AutoscrollController.h" |
| 30 | |
| 31 | #include "EventHandler.h" |
| 32 | #include "Frame.h" |
| 33 | #include "FrameView.h" |
| 34 | #include "HitTestResult.h" |
| 35 | #include "Page.h" |
| 36 | #include "RenderBox.h" |
| 37 | #include "RenderView.h" |
| 38 | #include "ScrollView.h" |
| 39 | #include "Settings.h" |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | // Delay time in second for start autoscroll if pointer is in border edge of scrollable element. |
| 44 | static const Seconds autoscrollDelay { 200_ms }; |
| 45 | |
| 46 | // When the autoscroll or the panScroll is triggered when do the scroll every 50ms to make it smooth. |
| 47 | static const Seconds autoscrollInterval { 50_ms }; |
| 48 | |
| 49 | #if ENABLE(PAN_SCROLLING) |
| 50 | static Frame* getMainFrame(Frame* frame) |
| 51 | { |
| 52 | Page* page = frame->page(); |
| 53 | return page ? &page->mainFrame() : 0; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | AutoscrollController::AutoscrollController() |
| 58 | : m_autoscrollTimer(*this, &AutoscrollController::autoscrollTimerFired) |
| 59 | , m_autoscrollRenderer(nullptr) |
| 60 | , m_autoscrollType(NoAutoscroll) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | RenderBox* AutoscrollController::autoscrollRenderer() const |
| 65 | { |
| 66 | return m_autoscrollRenderer; |
| 67 | } |
| 68 | |
| 69 | bool AutoscrollController::autoscrollInProgress() const |
| 70 | { |
| 71 | return m_autoscrollType == AutoscrollForSelection; |
| 72 | } |
| 73 | |
| 74 | void AutoscrollController::startAutoscrollForSelection(RenderObject* renderer) |
| 75 | { |
| 76 | // We don't want to trigger the autoscroll or the panScroll if it's already active |
| 77 | if (m_autoscrollTimer.isActive()) |
| 78 | return; |
| 79 | RenderBox* scrollable = RenderBox::findAutoscrollable(renderer); |
| 80 | if (!scrollable) |
| 81 | return; |
| 82 | m_autoscrollType = AutoscrollForSelection; |
| 83 | m_autoscrollRenderer = scrollable; |
| 84 | startAutoscrollTimer(); |
| 85 | } |
| 86 | |
| 87 | void AutoscrollController::stopAutoscrollTimer(bool rendererIsBeingDestroyed) |
| 88 | { |
| 89 | RenderBox* scrollable = m_autoscrollRenderer; |
| 90 | m_autoscrollTimer.stop(); |
| 91 | m_autoscrollRenderer = nullptr; |
| 92 | |
| 93 | if (!scrollable) |
| 94 | return; |
| 95 | |
| 96 | Frame& frame = scrollable->frame(); |
| 97 | if (autoscrollInProgress() && frame.eventHandler().mouseDownWasInSubframe()) { |
| 98 | if (Frame* subframe = frame.eventHandler().subframeForTargetNode(frame.eventHandler().mousePressNode())) |
| 99 | subframe->eventHandler().stopAutoscrollTimer(rendererIsBeingDestroyed); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (!rendererIsBeingDestroyed) |
| 104 | scrollable->stopAutoscroll(); |
| 105 | #if ENABLE(PAN_SCROLLING) |
| 106 | if (panScrollInProgress()) { |
| 107 | FrameView& frameView = scrollable->view().frameView(); |
| 108 | frameView.removePanScrollIcon(); |
| 109 | frameView.setCursor(pointerCursor()); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | m_autoscrollType = NoAutoscroll; |
| 114 | |
| 115 | #if ENABLE(PAN_SCROLLING) |
| 116 | // If we're not in the top frame we notify it that we are not doing a panScroll any more. |
| 117 | if (!frame.isMainFrame()) |
| 118 | frame.mainFrame().eventHandler().didPanScrollStop(); |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | void AutoscrollController::updateAutoscrollRenderer() |
| 123 | { |
| 124 | if (!m_autoscrollRenderer) |
| 125 | return; |
| 126 | |
| 127 | RenderObject* renderer = m_autoscrollRenderer; |
| 128 | |
| 129 | #if ENABLE(PAN_SCROLLING) |
| 130 | HitTestResult hitTest = m_autoscrollRenderer->frame().eventHandler().hitTestResultAtPoint(m_panScrollStartPos, HitTestRequest::ReadOnly | HitTestRequest::Active); |
| 131 | |
| 132 | if (Node* nodeAtPoint = hitTest.innerNode()) |
| 133 | renderer = nodeAtPoint->renderer(); |
| 134 | #endif |
| 135 | |
| 136 | while (renderer && !(is<RenderBox>(*renderer) && downcast<RenderBox>(*renderer).canAutoscroll())) |
| 137 | renderer = renderer->parent(); |
| 138 | m_autoscrollRenderer = is<RenderBox>(renderer) ? downcast<RenderBox>(renderer) : nullptr; |
| 139 | } |
| 140 | |
| 141 | void AutoscrollController::updateDragAndDrop(Node* dropTargetNode, const IntPoint& eventPosition, WallTime eventTime) |
| 142 | { |
| 143 | if (!dropTargetNode) { |
| 144 | stopAutoscrollTimer(); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | RenderBox* scrollable = RenderBox::findAutoscrollable(dropTargetNode->renderer()); |
| 149 | if (!scrollable) { |
| 150 | stopAutoscrollTimer(); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | Frame& frame = scrollable->frame(); |
| 155 | |
| 156 | Page* page = frame.page(); |
| 157 | if (!page || !page->settings().autoscrollForDragAndDropEnabled()) { |
| 158 | stopAutoscrollTimer(); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | IntSize offset = scrollable->calculateAutoscrollDirection(eventPosition); |
| 163 | if (offset.isZero()) { |
| 164 | stopAutoscrollTimer(); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | m_dragAndDropAutoscrollReferencePosition = eventPosition + offset; |
| 169 | |
| 170 | if (m_autoscrollType == NoAutoscroll) { |
| 171 | m_autoscrollType = AutoscrollForDragAndDrop; |
| 172 | m_autoscrollRenderer = scrollable; |
| 173 | m_dragAndDropAutoscrollStartTime = eventTime; |
| 174 | startAutoscrollTimer(); |
| 175 | } else if (m_autoscrollRenderer != scrollable) { |
| 176 | m_dragAndDropAutoscrollStartTime = eventTime; |
| 177 | m_autoscrollRenderer = scrollable; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | #if ENABLE(PAN_SCROLLING) |
| 182 | void AutoscrollController::didPanScrollStart() |
| 183 | { |
| 184 | m_autoscrollType = AutoscrollForPan; |
| 185 | } |
| 186 | |
| 187 | void AutoscrollController::didPanScrollStop() |
| 188 | { |
| 189 | m_autoscrollType = NoAutoscroll; |
| 190 | } |
| 191 | |
| 192 | void AutoscrollController::handleMouseReleaseEvent(const PlatformMouseEvent& mouseEvent) |
| 193 | { |
| 194 | switch (m_autoscrollType) { |
| 195 | case AutoscrollForPan: |
| 196 | if (mouseEvent.button() == MiddleButton) |
| 197 | m_autoscrollType = AutoscrollForPanCanStop; |
| 198 | break; |
| 199 | case AutoscrollForPanCanStop: |
| 200 | stopAutoscrollTimer(); |
| 201 | break; |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | bool AutoscrollController::panScrollInProgress() const |
| 208 | { |
| 209 | return m_autoscrollType == AutoscrollForPan || m_autoscrollType == AutoscrollForPanCanStop; |
| 210 | } |
| 211 | |
| 212 | void AutoscrollController::startPanScrolling(RenderBox* scrollable, const IntPoint& lastKnownMousePosition) |
| 213 | { |
| 214 | // We don't want to trigger the autoscroll or the panScroll if it's already active |
| 215 | if (m_autoscrollTimer.isActive()) |
| 216 | return; |
| 217 | |
| 218 | m_autoscrollType = AutoscrollForPan; |
| 219 | m_autoscrollRenderer = scrollable; |
| 220 | m_panScrollStartPos = lastKnownMousePosition; |
| 221 | |
| 222 | if (FrameView* view = scrollable->frame().view()) |
| 223 | view->addPanScrollIcon(lastKnownMousePosition); |
| 224 | scrollable->frame().eventHandler().didPanScrollStart(); |
| 225 | startAutoscrollTimer(); |
| 226 | } |
| 227 | #else |
| 228 | bool AutoscrollController::panScrollInProgress() const |
| 229 | { |
| 230 | return false; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | void AutoscrollController::autoscrollTimerFired() |
| 235 | { |
| 236 | if (!m_autoscrollRenderer) { |
| 237 | stopAutoscrollTimer(); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | Frame& frame = m_autoscrollRenderer->frame(); |
| 242 | switch (m_autoscrollType) { |
| 243 | case AutoscrollForDragAndDrop: |
| 244 | if (WallTime::now() - m_dragAndDropAutoscrollStartTime > autoscrollDelay) |
| 245 | m_autoscrollRenderer->autoscroll(m_dragAndDropAutoscrollReferencePosition); |
| 246 | break; |
| 247 | case AutoscrollForSelection: { |
| 248 | if (!frame.eventHandler().shouldUpdateAutoscroll()) { |
| 249 | stopAutoscrollTimer(); |
| 250 | return; |
| 251 | } |
| 252 | #if ENABLE(DRAG_SUPPORT) |
| 253 | frame.eventHandler().updateSelectionForMouseDrag(); |
| 254 | #endif |
| 255 | m_autoscrollRenderer->autoscroll(frame.eventHandler().targetPositionInWindowForSelectionAutoscroll()); |
| 256 | break; |
| 257 | } |
| 258 | case NoAutoscroll: |
| 259 | break; |
| 260 | #if ENABLE(PAN_SCROLLING) |
| 261 | case AutoscrollForPanCanStop: |
| 262 | case AutoscrollForPan: |
| 263 | // we verify that the main frame hasn't received the order to stop the panScroll |
| 264 | if (Frame* mainFrame = getMainFrame(&frame)) { |
| 265 | if (!mainFrame->eventHandler().panScrollInProgress()) { |
| 266 | stopAutoscrollTimer(); |
| 267 | return; |
| 268 | } |
| 269 | } |
| 270 | if (FrameView* view = frame.view()) |
| 271 | updatePanScrollState(view, frame.eventHandler().lastKnownMousePosition()); |
| 272 | m_autoscrollRenderer->panScroll(m_panScrollStartPos); |
| 273 | break; |
| 274 | #endif |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void AutoscrollController::startAutoscrollTimer() |
| 279 | { |
| 280 | m_autoscrollTimer.startRepeating(autoscrollInterval); |
| 281 | } |
| 282 | |
| 283 | #if ENABLE(PAN_SCROLLING) |
| 284 | void AutoscrollController::updatePanScrollState(FrameView* view, const IntPoint& lastKnownMousePosition) |
| 285 | { |
| 286 | // At the original click location we draw a 4 arrowed icon. Over this icon there won't be any scroll |
| 287 | // So we don't want to change the cursor over this area |
| 288 | bool east = m_panScrollStartPos.x() < (lastKnownMousePosition.x() - ScrollView::noPanScrollRadius); |
| 289 | bool west = m_panScrollStartPos.x() > (lastKnownMousePosition.x() + ScrollView::noPanScrollRadius); |
| 290 | bool north = m_panScrollStartPos.y() > (lastKnownMousePosition.y() + ScrollView::noPanScrollRadius); |
| 291 | bool south = m_panScrollStartPos.y() < (lastKnownMousePosition.y() - ScrollView::noPanScrollRadius); |
| 292 | |
| 293 | if (m_autoscrollType == AutoscrollForPan && (east || west || north || south)) |
| 294 | m_autoscrollType = AutoscrollForPanCanStop; |
| 295 | |
| 296 | if (north) { |
| 297 | if (east) |
| 298 | view->setCursor(northEastPanningCursor()); |
| 299 | else if (west) |
| 300 | view->setCursor(northWestPanningCursor()); |
| 301 | else |
| 302 | view->setCursor(northPanningCursor()); |
| 303 | } else if (south) { |
| 304 | if (east) |
| 305 | view->setCursor(southEastPanningCursor()); |
| 306 | else if (west) |
| 307 | view->setCursor(southWestPanningCursor()); |
| 308 | else |
| 309 | view->setCursor(southPanningCursor()); |
| 310 | } else if (east) |
| 311 | view->setCursor(eastPanningCursor()); |
| 312 | else if (west) |
| 313 | view->setCursor(westPanningCursor()); |
| 314 | else |
| 315 | view->setCursor(middlePanningCursor()); |
| 316 | } |
| 317 | #endif |
| 318 | |
| 319 | } // namespace WebCore |
| 320 | |