1 /*
2 * Copyright (C) 2011 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "web/ScrollbarGroup.h"
28
29 #include "core/frame/FrameView.h"
30 #include "platform/scroll/Scrollbar.h"
31 #include "platform/scroll/ScrollbarTheme.h"
32 #include "public/platform/WebRect.h"
33 #include "web/WebPluginScrollbarImpl.h"
34
35 namespace blink {
36
ScrollbarGroup(FrameView * frameView,const IntRect & frameRect)37 ScrollbarGroup::ScrollbarGroup(FrameView* frameView, const IntRect& frameRect)
38 : m_frameView(frameView)
39 , m_frameRect(frameRect)
40 , m_horizontalScrollbar(0)
41 , m_verticalScrollbar(0)
42 {
43 }
44
~ScrollbarGroup()45 ScrollbarGroup::~ScrollbarGroup()
46 {
47 ASSERT(!m_horizontalScrollbar);
48 ASSERT(!m_verticalScrollbar);
49 }
50
scrollbarCreated(WebPluginScrollbarImpl * scrollbar)51 void ScrollbarGroup::scrollbarCreated(WebPluginScrollbarImpl* scrollbar)
52 {
53 bool hadScrollbars = m_horizontalScrollbar || m_verticalScrollbar;
54 if (scrollbar->scrollbar()->orientation() == HorizontalScrollbar) {
55 ASSERT(!m_horizontalScrollbar);
56 m_horizontalScrollbar = scrollbar;
57 didAddScrollbar(scrollbar->scrollbar(), HorizontalScrollbar);
58 } else {
59 ASSERT(!m_verticalScrollbar);
60 m_verticalScrollbar = scrollbar;
61 didAddScrollbar(scrollbar->scrollbar(), VerticalScrollbar);
62 }
63
64 if (!hadScrollbars) {
65 m_frameView->addScrollableArea(this);
66 m_frameView->setNeedsLayout();
67 }
68 }
69
scrollbarDestroyed(WebPluginScrollbarImpl * scrollbar)70 void ScrollbarGroup::scrollbarDestroyed(WebPluginScrollbarImpl* scrollbar)
71 {
72 if (scrollbar == m_horizontalScrollbar) {
73 willRemoveScrollbar(scrollbar->scrollbar(), HorizontalScrollbar);
74 m_horizontalScrollbar = 0;
75 } else {
76 ASSERT(scrollbar == m_verticalScrollbar);
77 willRemoveScrollbar(scrollbar->scrollbar(), VerticalScrollbar);
78 m_verticalScrollbar = 0;
79 }
80
81 if (!m_horizontalScrollbar && !m_verticalScrollbar) {
82 m_frameView->removeScrollableArea(this);
83 m_frameView->setNeedsLayout();
84 }
85 }
86
setLastMousePosition(const IntPoint & point)87 void ScrollbarGroup::setLastMousePosition(const IntPoint& point)
88 {
89 m_lastMousePosition = point;
90 }
91
scrollSize(ScrollbarOrientation orientation) const92 int ScrollbarGroup::scrollSize(ScrollbarOrientation orientation) const
93 {
94 WebPluginScrollbarImpl* webScrollbar = orientation == HorizontalScrollbar ? m_horizontalScrollbar : m_verticalScrollbar;
95 if (!webScrollbar)
96 return 0;
97 Scrollbar* scrollbar = webScrollbar->scrollbar();
98 return scrollbar->totalSize() - scrollbar->visibleSize();
99 }
100
setScrollOffset(const IntPoint & offset)101 void ScrollbarGroup::setScrollOffset(const IntPoint& offset)
102 {
103 if (m_horizontalScrollbar && m_horizontalScrollbar->scrollOffset() != offset.x())
104 m_horizontalScrollbar->setScrollOffset(offset.x());
105 else if (m_verticalScrollbar && m_verticalScrollbar->scrollOffset() != offset.y())
106 m_verticalScrollbar->setScrollOffset(offset.y());
107 }
108
invalidateScrollbarRect(Scrollbar * scrollbar,const IntRect & rect)109 void ScrollbarGroup::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
110 {
111 if (m_horizontalScrollbar && scrollbar == m_horizontalScrollbar->scrollbar())
112 m_horizontalScrollbar->invalidateScrollbarRect(rect);
113 else if (m_verticalScrollbar && scrollbar == m_verticalScrollbar->scrollbar())
114 m_verticalScrollbar->invalidateScrollbarRect(rect);
115 }
116
invalidateScrollCornerRect(const IntRect &)117 void ScrollbarGroup::invalidateScrollCornerRect(const IntRect&)
118 {
119 }
120
isActive() const121 bool ScrollbarGroup::isActive() const
122 {
123 return true;
124 }
125
setFrameRect(const IntRect & frameRect)126 void ScrollbarGroup::setFrameRect(const IntRect& frameRect)
127 {
128 m_frameRect = frameRect;
129 }
130
scrollableAreaBoundingBox() const131 IntRect ScrollbarGroup::scrollableAreaBoundingBox() const
132 {
133 return m_frameRect;
134 }
135
isScrollCornerVisible() const136 bool ScrollbarGroup::isScrollCornerVisible() const
137 {
138 return false;
139 }
140
getTickmarks(Vector<IntRect> & tickmarks) const141 void ScrollbarGroup::getTickmarks(Vector<IntRect>& tickmarks) const
142 {
143 if (m_verticalScrollbar)
144 m_verticalScrollbar->getTickmarks(tickmarks);
145 }
146
convertFromContainingViewToScrollbar(const Scrollbar * scrollbar,const IntPoint & parentPoint) const147 IntPoint ScrollbarGroup::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
148 {
149 if (m_horizontalScrollbar && scrollbar == m_horizontalScrollbar->scrollbar())
150 return m_horizontalScrollbar->convertFromContainingViewToScrollbar(parentPoint);
151 if (m_verticalScrollbar && scrollbar == m_verticalScrollbar->scrollbar())
152 return m_verticalScrollbar->convertFromContainingViewToScrollbar(parentPoint);
153 BLINK_ASSERT_NOT_REACHED();
154 return IntPoint();
155 }
156
horizontalScrollbar() const157 Scrollbar* ScrollbarGroup::horizontalScrollbar() const
158 {
159 return m_horizontalScrollbar ? m_horizontalScrollbar->scrollbar() : 0;
160 }
161
verticalScrollbar() const162 Scrollbar* ScrollbarGroup::verticalScrollbar() const
163 {
164 return m_verticalScrollbar ? m_verticalScrollbar->scrollbar() : 0;
165 }
166
scrollPosition() const167 IntPoint ScrollbarGroup::scrollPosition() const
168 {
169 int x = m_horizontalScrollbar ? m_horizontalScrollbar->scrollOffset() : 0;
170 int y = m_verticalScrollbar ? m_verticalScrollbar->scrollOffset() : 0;
171 return IntPoint(x, y);
172 }
173
minimumScrollPosition() const174 IntPoint ScrollbarGroup::minimumScrollPosition() const
175 {
176 return IntPoint();
177 }
178
maximumScrollPosition() const179 IntPoint ScrollbarGroup::maximumScrollPosition() const
180 {
181 return IntPoint(contentsSize().width() - visibleWidth(), contentsSize().height() - visibleHeight());
182 }
183
visibleHeight() const184 int ScrollbarGroup::visibleHeight() const
185 {
186 if (m_verticalScrollbar)
187 return m_verticalScrollbar->scrollbar()->height();
188 if (m_horizontalScrollbar)
189 return m_horizontalScrollbar->scrollbar()->height();
190 BLINK_ASSERT_NOT_REACHED();
191 return 0;
192 }
193
visibleWidth() const194 int ScrollbarGroup::visibleWidth() const
195 {
196 if (m_horizontalScrollbar)
197 return m_horizontalScrollbar->scrollbar()->width();
198 if (m_verticalScrollbar)
199 return m_verticalScrollbar->scrollbar()->width();
200 BLINK_ASSERT_NOT_REACHED();
201 return 0;
202 }
203
contentsSize() const204 IntSize ScrollbarGroup::contentsSize() const
205 {
206 IntSize size;
207 if (m_horizontalScrollbar)
208 size.setWidth(m_horizontalScrollbar->scrollbar()->totalSize());
209 else if (m_verticalScrollbar) {
210 size.setWidth(m_verticalScrollbar->scrollbar()->x());
211 if (m_verticalScrollbar->scrollbar()->isOverlayScrollbar())
212 size.expand(WebPluginScrollbar::defaultThickness(), 0);
213 }
214 if (m_verticalScrollbar)
215 size.setHeight(m_verticalScrollbar->scrollbar()->totalSize());
216 else if (m_horizontalScrollbar) {
217 size.setHeight(m_horizontalScrollbar->scrollbar()->y());
218 if (m_horizontalScrollbar->scrollbar()->isOverlayScrollbar())
219 size.expand(0, WebPluginScrollbar::defaultThickness());
220 }
221 return size;
222 }
223
overhangAmount() const224 IntSize ScrollbarGroup::overhangAmount() const
225 {
226 return IntSize();
227 }
228
lastKnownMousePosition() const229 IntPoint ScrollbarGroup::lastKnownMousePosition() const
230 {
231 return m_lastMousePosition;
232 }
233
shouldSuspendScrollAnimations() const234 bool ScrollbarGroup::shouldSuspendScrollAnimations() const
235 {
236 return false;
237 }
238
scrollbarStyleChanged()239 void ScrollbarGroup::scrollbarStyleChanged()
240 {
241 if (m_horizontalScrollbar)
242 m_horizontalScrollbar->scrollbarStyleChanged();
243 if (m_verticalScrollbar)
244 m_verticalScrollbar->scrollbarStyleChanged();
245 }
246
scrollbarsCanBeActive() const247 bool ScrollbarGroup::scrollbarsCanBeActive() const
248 {
249 return true;
250 }
251
userInputScrollable(ScrollbarOrientation orientation) const252 bool ScrollbarGroup::userInputScrollable(ScrollbarOrientation orientation) const
253 {
254 return orientation == HorizontalScrollbar ? horizontalScrollbar() : verticalScrollbar();
255 }
256
shouldPlaceVerticalScrollbarOnLeft() const257 bool ScrollbarGroup::shouldPlaceVerticalScrollbarOnLeft() const
258 {
259 return false;
260 }
261
pageStep(ScrollbarOrientation orientation) const262 int ScrollbarGroup::pageStep(ScrollbarOrientation orientation) const
263 {
264 int length;
265 if (orientation == VerticalScrollbar) {
266 if (!m_verticalScrollbar)
267 return 0;
268
269 length = m_verticalScrollbar->scrollbar()->height();
270 } else {
271 if (!m_horizontalScrollbar)
272 return 0;
273
274 length = m_horizontalScrollbar->scrollbar()->width();
275 }
276
277 int pageStep = std::max(
278 static_cast<int>(static_cast<float>(length) * ScrollableArea::minFractionToStepWhenPaging()),
279 length - ScrollableArea::maxOverlapBetweenPages());
280
281 return std::max(pageStep, 1);
282 }
283
284 } // namespace blink
285