2 * Copyright (C) 2011 Google 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
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.
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.
27 #include "platform/scroll/ScrollbarThemeOverlay.h"
29 #include "platform/PlatformMouseEvent.h"
30 #include "platform/graphics/GraphicsContext.h"
31 #include "platform/scroll/ScrollbarThemeClient.h"
32 #include "platform/transforms/TransformationMatrix.h"
33 #include "public/platform/Platform.h"
34 #include "public/platform/WebRect.h"
35 #include "public/platform/WebThemeEngine.h"
43 ScrollbarThemeOverlay::ScrollbarThemeOverlay(int thumbThickness, int scrollbarMargin, HitTestBehavior allowHitTest, Color color)
45 , m_thumbThickness(thumbThickness)
46 , m_scrollbarMargin(scrollbarMargin)
47 , m_allowHitTest(allowHitTest)
49 , m_useSolidColor(true)
53 ScrollbarThemeOverlay::ScrollbarThemeOverlay(int thumbThickness, int scrollbarMargin, HitTestBehavior allowHitTest)
55 , m_thumbThickness(thumbThickness)
56 , m_scrollbarMargin(scrollbarMargin)
57 , m_allowHitTest(allowHitTest)
58 , m_useSolidColor(false)
62 int ScrollbarThemeOverlay::scrollbarThickness(ScrollbarControlSize controlSize)
64 return m_thumbThickness + m_scrollbarMargin;
67 bool ScrollbarThemeOverlay::usesOverlayScrollbars() const
72 int ScrollbarThemeOverlay::thumbPosition(ScrollbarThemeClient* scrollbar)
74 if (!scrollbar->totalSize())
77 int trackLen = trackLength(scrollbar);
78 float proportion = static_cast<float>(scrollbar->currentPos()) / scrollbar->totalSize();
79 return round(proportion * trackLen);
82 int ScrollbarThemeOverlay::thumbLength(ScrollbarThemeClient* scrollbar)
84 int trackLen = trackLength(scrollbar);
86 if (!scrollbar->totalSize())
89 float proportion = static_cast<float>(scrollbar->visibleSize()) / scrollbar->totalSize();
90 int length = round(proportion * trackLen);
91 length = min(max(length, minimumThumbLength(scrollbar)), trackLen);
95 bool ScrollbarThemeOverlay::hasThumb(ScrollbarThemeClient* scrollbar)
100 IntRect ScrollbarThemeOverlay::backButtonRect(ScrollbarThemeClient*, ScrollbarPart, bool)
105 IntRect ScrollbarThemeOverlay::forwardButtonRect(ScrollbarThemeClient*, ScrollbarPart, bool)
110 IntRect ScrollbarThemeOverlay::trackRect(ScrollbarThemeClient* scrollbar, bool)
112 IntRect rect = scrollbar->frameRect();
113 if (scrollbar->orientation() == HorizontalScrollbar)
114 rect.inflateX(-m_scrollbarMargin);
116 rect.inflateY(-m_scrollbarMargin);
120 int ScrollbarThemeOverlay::thumbThickness(ScrollbarThemeClient*)
122 return m_thumbThickness;
125 void ScrollbarThemeOverlay::paintThumb(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
127 if (context->paintingDisabled())
129 IntRect thumbRect = rect;
130 if (scrollbar->orientation() == HorizontalScrollbar) {
131 thumbRect.setHeight(thumbRect.height() - m_scrollbarMargin);
133 thumbRect.setWidth(thumbRect.width() - m_scrollbarMargin);
134 if (scrollbar->isLeftSideVerticalScrollbar())
135 thumbRect.setX(thumbRect.x() + m_scrollbarMargin);
138 if (m_useSolidColor) {
139 context->fillRect(thumbRect, m_color);
143 blink::WebThemeEngine::State state = blink::WebThemeEngine::StateNormal;
144 if (scrollbar->pressedPart() == ThumbPart)
145 state = blink::WebThemeEngine::StatePressed;
146 else if (scrollbar->hoveredPart() == ThumbPart)
147 state = blink::WebThemeEngine::StateHover;
149 blink::WebCanvas* canvas = context->canvas();
151 blink::WebThemeEngine::Part part = blink::WebThemeEngine::PartScrollbarHorizontalThumb;
152 if (scrollbar->orientation() == VerticalScrollbar)
153 part = blink::WebThemeEngine::PartScrollbarVerticalThumb;
155 blink::Platform::current()->themeEngine()->paint(canvas, part, state, blink::WebRect(rect), 0);
158 ScrollbarPart ScrollbarThemeOverlay::hitTest(ScrollbarThemeClient* scrollbar, const IntPoint& position)
160 if (m_allowHitTest == DisallowHitTest)
163 return ScrollbarTheme::hitTest(scrollbar, position);
166 } // namespace WebCore