initial import
[vuplus_webkit] / Source / WebKit / chromium / src / DragScrollTimer.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "DragScrollTimer.h"
33
34 #include "FrameView.h"
35
36 using namespace WebCore;
37
38 namespace WebKit {
39
40 // Computes the distance from a point outside a rect to the nearest edge of the rect.
41 static IntSize distanceToRect(const IntPoint& point, const IntRect& rect)
42 {
43     int dx = 0, dy = 0;
44     if (point.x() < rect.x())
45         dx = point.x() - rect.x();
46     else if (rect.maxX() < point.x())
47         dx = point.x() - rect.maxX();
48     if (point.y() < rect.y())
49         dy = point.y() - rect.y();
50     else if (rect.maxY() < point.y())
51         dy = point.y() - rect.maxY();
52     return IntSize(dx, dy);
53 }
54
55 DragScrollTimer::DragScrollTimer()
56     : m_timer(this, &DragScrollTimer::fired)
57     , m_view(0)
58     , m_scrolling(false)
59 {
60 }
61
62 DragScrollTimer::~DragScrollTimer()
63 {
64     // We do this for detecting dead object earlier
65     stop(); 
66 }
67
68 void DragScrollTimer::stop()
69 {
70     m_timer.stop();
71     m_view = 0;
72     m_scrolling = false;
73 }
74
75 void DragScrollTimer::scroll()
76 {
77     m_view->scrollBy(m_lastDistance);
78     m_scrolling = true;
79 }
80
81 void DragScrollTimer::update()
82 {
83     if (shouldScroll())
84         scroll();
85     else
86         stop();
87 }
88
89 void DragScrollTimer::triggerScroll(FrameView* view, const WebPoint& location)
90 {
91     if (!view)
92         return;
93
94     // Approximates Safari
95     static const double scrollStartDelay = 0.2;
96
97     m_view = view;
98     m_lastDistance = scrollDistanceFor(view, location);
99
100     if (m_scrolling)
101         update();
102     else if (shouldScroll() && !m_timer.isActive())
103         m_timer.startOneShot(scrollStartDelay);
104 }
105
106 IntSize DragScrollTimer::scrollDistanceFor(FrameView* view, const WebPoint& location) const
107 {
108     static const int scrollMargin = 30;
109
110     IntRect bounds(0, 0, view->visibleWidth(), view->visibleHeight());
111     if (!bounds.contains(location))
112         return IntSize(0, 0); // The location is outside the border belt.
113
114     bounds.setY(bounds.y() + scrollMargin);
115     bounds.setHeight(bounds.height() - scrollMargin * 2);
116     bounds.setX(bounds.x() + scrollMargin);
117     bounds.setWidth(bounds.width() - scrollMargin * 2);
118
119     if (bounds.contains(location))
120         return IntSize(0, 0); // The location is inside the border belt.
121
122     // The location is over the border belt.
123     return distanceToRect(location, bounds);
124 }
125
126 } // namespace WebKit