2 * Copyright (C) 2011 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "platform/scroll/ScrollbarTheme.h"
29 #include "platform/PlatformMouseEvent.h"
30 #include "platform/RuntimeEnabledFeatures.h"
31 #include "platform/graphics/Color.h"
32 #include "platform/graphics/GraphicsContext.h"
33 #include "platform/scroll/ScrollbarThemeClient.h"
34 #include "platform/scroll/ScrollbarThemeMock.h"
35 #include "platform/scroll/ScrollbarThemeOverlayMock.h"
36 #include "public/platform/Platform.h"
37 #include "public/platform/WebPoint.h"
38 #include "public/platform/WebRect.h"
39 #include "public/platform/WebScrollbarBehavior.h"
42 #include "public/platform/WebRect.h"
43 #include "public/platform/WebThemeEngine.h"
48 bool ScrollbarTheme::gMockScrollbarsEnabled = false;
50 bool ScrollbarTheme::paint(ScrollbarThemeClient* scrollbar, GraphicsContext* graphicsContext, const IntRect& damageRect)
52 // Create the ScrollbarControlPartMask based on the damageRect
53 ScrollbarControlPartMask scrollMask = NoPart;
55 IntRect backButtonStartPaintRect;
56 IntRect backButtonEndPaintRect;
57 IntRect forwardButtonStartPaintRect;
58 IntRect forwardButtonEndPaintRect;
59 if (hasButtons(scrollbar)) {
60 backButtonStartPaintRect = backButtonRect(scrollbar, BackButtonStartPart, true);
61 if (damageRect.intersects(backButtonStartPaintRect))
62 scrollMask |= BackButtonStartPart;
63 backButtonEndPaintRect = backButtonRect(scrollbar, BackButtonEndPart, true);
64 if (damageRect.intersects(backButtonEndPaintRect))
65 scrollMask |= BackButtonEndPart;
66 forwardButtonStartPaintRect = forwardButtonRect(scrollbar, ForwardButtonStartPart, true);
67 if (damageRect.intersects(forwardButtonStartPaintRect))
68 scrollMask |= ForwardButtonStartPart;
69 forwardButtonEndPaintRect = forwardButtonRect(scrollbar, ForwardButtonEndPart, true);
70 if (damageRect.intersects(forwardButtonEndPaintRect))
71 scrollMask |= ForwardButtonEndPart;
74 IntRect startTrackRect;
77 IntRect trackPaintRect = trackRect(scrollbar, true);
78 if (damageRect.intersects(trackPaintRect))
79 scrollMask |= TrackBGPart;
80 bool thumbPresent = hasThumb(scrollbar);
82 IntRect track = trackRect(scrollbar);
83 splitTrack(scrollbar, track, startTrackRect, thumbRect, endTrackRect);
84 if (damageRect.intersects(thumbRect))
85 scrollMask |= ThumbPart;
86 if (damageRect.intersects(startTrackRect))
87 scrollMask |= BackTrackPart;
88 if (damageRect.intersects(endTrackRect))
89 scrollMask |= ForwardTrackPart;
92 // Paint the scrollbar background (only used by custom CSS scrollbars).
93 paintScrollbarBackground(graphicsContext, scrollbar);
95 // Paint the back and forward buttons.
96 if (scrollMask & BackButtonStartPart)
97 paintButton(graphicsContext, scrollbar, backButtonStartPaintRect, BackButtonStartPart);
98 if (scrollMask & BackButtonEndPart)
99 paintButton(graphicsContext, scrollbar, backButtonEndPaintRect, BackButtonEndPart);
100 if (scrollMask & ForwardButtonStartPart)
101 paintButton(graphicsContext, scrollbar, forwardButtonStartPaintRect, ForwardButtonStartPart);
102 if (scrollMask & ForwardButtonEndPart)
103 paintButton(graphicsContext, scrollbar, forwardButtonEndPaintRect, ForwardButtonEndPart);
105 if (scrollMask & TrackBGPart)
106 paintTrackBackground(graphicsContext, scrollbar, trackPaintRect);
108 if ((scrollMask & ForwardTrackPart) || (scrollMask & BackTrackPart)) {
109 // Paint the track pieces above and below the thumb.
110 if (scrollMask & BackTrackPart)
111 paintTrackPiece(graphicsContext, scrollbar, startTrackRect, BackTrackPart);
112 if (scrollMask & ForwardTrackPart)
113 paintTrackPiece(graphicsContext, scrollbar, endTrackRect, ForwardTrackPart);
115 paintTickmarks(graphicsContext, scrollbar, trackPaintRect);
119 if (scrollMask & ThumbPart)
120 paintThumb(graphicsContext, scrollbar, thumbRect);
125 ScrollbarPart ScrollbarTheme::hitTest(ScrollbarThemeClient* scrollbar, const IntPoint& position)
127 ScrollbarPart result = NoPart;
128 if (!scrollbar->enabled())
131 IntPoint testPosition = scrollbar->convertFromContainingWindow(position);
132 testPosition.move(scrollbar->x(), scrollbar->y());
134 if (!scrollbar->frameRect().contains(testPosition))
137 result = ScrollbarBGPart;
139 IntRect track = trackRect(scrollbar);
140 if (track.contains(testPosition)) {
141 IntRect beforeThumbRect;
143 IntRect afterThumbRect;
144 splitTrack(scrollbar, track, beforeThumbRect, thumbRect, afterThumbRect);
145 if (thumbRect.contains(testPosition))
147 else if (beforeThumbRect.contains(testPosition))
148 result = BackTrackPart;
149 else if (afterThumbRect.contains(testPosition))
150 result = ForwardTrackPart;
152 result = TrackBGPart;
153 } else if (backButtonRect(scrollbar, BackButtonStartPart).contains(testPosition)) {
154 result = BackButtonStartPart;
155 } else if (backButtonRect(scrollbar, BackButtonEndPart).contains(testPosition)) {
156 result = BackButtonEndPart;
157 } else if (forwardButtonRect(scrollbar, ForwardButtonStartPart).contains(testPosition)) {
158 result = ForwardButtonStartPart;
159 } else if (forwardButtonRect(scrollbar, ForwardButtonEndPart).contains(testPosition)) {
160 result = ForwardButtonEndPart;
165 void ScrollbarTheme::invalidatePart(ScrollbarThemeClient* scrollbar, ScrollbarPart part)
172 case BackButtonStartPart:
173 result = backButtonRect(scrollbar, BackButtonStartPart, true);
175 case BackButtonEndPart:
176 result = backButtonRect(scrollbar, BackButtonEndPart, true);
178 case ForwardButtonStartPart:
179 result = forwardButtonRect(scrollbar, ForwardButtonStartPart, true);
181 case ForwardButtonEndPart:
182 result = forwardButtonRect(scrollbar, ForwardButtonEndPart, true);
185 result = trackRect(scrollbar, true);
187 case ScrollbarBGPart:
188 result = scrollbar->frameRect();
191 IntRect beforeThumbRect, thumbRect, afterThumbRect;
192 splitTrack(scrollbar, trackRect(scrollbar), beforeThumbRect, thumbRect, afterThumbRect);
193 if (part == BackTrackPart)
194 result = beforeThumbRect;
195 else if (part == ForwardTrackPart)
196 result = afterThumbRect;
201 result.moveBy(-scrollbar->location());
202 scrollbar->invalidateRect(result);
205 void ScrollbarTheme::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
207 if (cornerRect.isEmpty())
211 context->fillRect(cornerRect, Color::white);
213 blink::Platform::current()->themeEngine()->paint(context->canvas(), blink::WebThemeEngine::PartScrollbarCorner, blink::WebThemeEngine::StateNormal, blink::WebRect(cornerRect), 0);
217 void ScrollbarTheme::paintOverhangBackground(GraphicsContext* context, const IntRect& horizontalOverhangRect, const IntRect& verticalOverhangRect, const IntRect& dirtyRect)
219 context->setFillColor(Color::white);
220 if (!horizontalOverhangRect.isEmpty())
221 context->fillRect(intersection(horizontalOverhangRect, dirtyRect));
222 if (!verticalOverhangRect.isEmpty())
223 context->fillRect(intersection(verticalOverhangRect, dirtyRect));
226 bool ScrollbarTheme::shouldCenterOnThumb(ScrollbarThemeClient* scrollbar, const PlatformMouseEvent& evt)
228 return blink::Platform::current()->scrollbarBehavior()->shouldCenterOnThumb(static_cast<blink::WebScrollbarBehavior::Button>(evt.button()), evt.shiftKey(), evt.altKey());
231 bool ScrollbarTheme::shouldSnapBackToDragOrigin(ScrollbarThemeClient* scrollbar, const PlatformMouseEvent& evt)
233 IntPoint mousePosition = scrollbar->convertFromContainingWindow(evt.position());
234 mousePosition.move(scrollbar->x(), scrollbar->y());
235 return blink::Platform::current()->scrollbarBehavior()->shouldSnapBackToDragOrigin(mousePosition, trackRect(scrollbar), scrollbar->orientation() == HorizontalScrollbar);
238 // Returns the size represented by track taking into account scrolling past
239 // the end of the document.
240 static float usedTotalSize(ScrollbarThemeClient* scrollbar)
242 float overhangAtStart = -scrollbar->currentPos();
243 float overhangAtEnd = scrollbar->currentPos() + scrollbar->visibleSize() - scrollbar->totalSize();
244 float overhang = std::max(0.0f, std::max(overhangAtStart, overhangAtEnd));
245 return scrollbar->totalSize() + overhang;
248 int ScrollbarTheme::thumbPosition(ScrollbarThemeClient* scrollbar)
250 if (scrollbar->enabled()) {
251 float size = usedTotalSize(scrollbar) - scrollbar->visibleSize();
252 // Avoid doing a floating point divide by zero and return 1 when usedTotalSize == visibleSize.
255 float pos = std::max(0.0f, scrollbar->currentPos()) * (trackLength(scrollbar) - thumbLength(scrollbar)) / size;
256 return (pos < 1 && pos > 0) ? 1 : pos;
261 int ScrollbarTheme::thumbLength(ScrollbarThemeClient* scrollbar)
263 if (!scrollbar->enabled())
267 if (scrollbar->currentPos() < 0)
268 overhang = -scrollbar->currentPos();
269 else if (scrollbar->visibleSize() + scrollbar->currentPos() > scrollbar->totalSize())
270 overhang = scrollbar->currentPos() + scrollbar->visibleSize() - scrollbar->totalSize();
271 float proportion = (scrollbar->visibleSize() - overhang) / usedTotalSize(scrollbar);
272 int trackLen = trackLength(scrollbar);
273 int length = round(proportion * trackLen);
274 length = std::max(length, minimumThumbLength(scrollbar));
275 if (length > trackLen)
276 length = 0; // Once the thumb is below the track length, it just goes away (to make more room for the track).
280 int ScrollbarTheme::trackPosition(ScrollbarThemeClient* scrollbar)
282 IntRect constrainedTrackRect = constrainTrackRectToTrackPieces(scrollbar, trackRect(scrollbar));
283 return (scrollbar->orientation() == HorizontalScrollbar) ? constrainedTrackRect.x() - scrollbar->x() : constrainedTrackRect.y() - scrollbar->y();
286 int ScrollbarTheme::trackLength(ScrollbarThemeClient* scrollbar)
288 IntRect constrainedTrackRect = constrainTrackRectToTrackPieces(scrollbar, trackRect(scrollbar));
289 return (scrollbar->orientation() == HorizontalScrollbar) ? constrainedTrackRect.width() : constrainedTrackRect.height();
292 IntRect ScrollbarTheme::thumbRect(ScrollbarThemeClient* scrollbar)
294 if (!hasThumb(scrollbar))
297 IntRect track = trackRect(scrollbar);
298 IntRect startTrackRect;
300 IntRect endTrackRect;
301 splitTrack(scrollbar, track, startTrackRect, thumbRect, endTrackRect);
306 int ScrollbarTheme::thumbThickness(ScrollbarThemeClient* scrollbar)
308 IntRect track = trackRect(scrollbar);
309 return scrollbar->orientation() == HorizontalScrollbar ? track.height() : track.width();
312 int ScrollbarTheme::minimumThumbLength(ScrollbarThemeClient* scrollbar)
314 return scrollbarThickness(scrollbar->controlSize());
317 void ScrollbarTheme::splitTrack(ScrollbarThemeClient* scrollbar, const IntRect& unconstrainedTrackRect, IntRect& beforeThumbRect, IntRect& thumbRect, IntRect& afterThumbRect)
319 // This function won't even get called unless we're big enough to have some combination of these three rects where at least
320 // one of them is non-empty.
321 IntRect trackRect = constrainTrackRectToTrackPieces(scrollbar, unconstrainedTrackRect);
322 int thumbPos = thumbPosition(scrollbar);
323 if (scrollbar->orientation() == HorizontalScrollbar) {
324 thumbRect = IntRect(trackRect.x() + thumbPos, trackRect.y(), thumbLength(scrollbar), scrollbar->height());
325 beforeThumbRect = IntRect(trackRect.x(), trackRect.y(), thumbPos + thumbRect.width() / 2, trackRect.height());
326 afterThumbRect = IntRect(trackRect.x() + beforeThumbRect.width(), trackRect.y(), trackRect.maxX() - beforeThumbRect.maxX(), trackRect.height());
328 thumbRect = IntRect(trackRect.x(), trackRect.y() + thumbPos, scrollbar->width(), thumbLength(scrollbar));
329 beforeThumbRect = IntRect(trackRect.x(), trackRect.y(), trackRect.width(), thumbPos + thumbRect.height() / 2);
330 afterThumbRect = IntRect(trackRect.x(), trackRect.y() + beforeThumbRect.height(), trackRect.width(), trackRect.maxY() - beforeThumbRect.maxY());
334 ScrollbarTheme* ScrollbarTheme::theme()
336 if (ScrollbarTheme::mockScrollbarsEnabled()) {
337 if (RuntimeEnabledFeatures::overlayScrollbarsEnabled()) {
338 DEFINE_STATIC_LOCAL(ScrollbarThemeOverlayMock, overlayMockTheme, ());
339 return &overlayMockTheme;
342 DEFINE_STATIC_LOCAL(ScrollbarThemeMock, mockTheme, ());
345 return nativeTheme();
348 void ScrollbarTheme::setMockScrollbarsEnabled(bool flag)
350 gMockScrollbarsEnabled = flag;
353 bool ScrollbarTheme::mockScrollbarsEnabled()
355 return gMockScrollbarsEnabled;