1/*
2 * Copyright (C) 2008 Apple 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 * 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.
12 *
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.
24 */
25
26#include "config.h"
27#include "RenderScrollbarTheme.h"
28#include "RenderScrollbar.h"
29#include <wtf/NeverDestroyed.h>
30#include <wtf/StdLibExtras.h>
31
32namespace WebCore {
33
34RenderScrollbarTheme* RenderScrollbarTheme::renderScrollbarTheme()
35{
36 static NeverDestroyed<RenderScrollbarTheme> theme;
37 return &theme.get();
38}
39
40void RenderScrollbarTheme::buttonSizesAlongTrackAxis(Scrollbar& scrollbar, int& beforeSize, int& afterSize)
41{
42 IntRect firstButton = backButtonRect(scrollbar, BackButtonStartPart);
43 IntRect secondButton = forwardButtonRect(scrollbar, ForwardButtonStartPart);
44 IntRect thirdButton = backButtonRect(scrollbar, BackButtonEndPart);
45 IntRect fourthButton = forwardButtonRect(scrollbar, ForwardButtonEndPart);
46 if (scrollbar.orientation() == HorizontalScrollbar) {
47 beforeSize = firstButton.width() + secondButton.width();
48 afterSize = thirdButton.width() + fourthButton.width();
49 } else {
50 beforeSize = firstButton.height() + secondButton.height();
51 afterSize = thirdButton.height() + fourthButton.height();
52 }
53}
54
55bool RenderScrollbarTheme::hasButtons(Scrollbar& scrollbar)
56{
57 int startSize;
58 int endSize;
59 buttonSizesAlongTrackAxis(scrollbar, startSize, endSize);
60 return (startSize + endSize) <= (scrollbar.orientation() == HorizontalScrollbar ? scrollbar.width() : scrollbar.height());
61}
62
63bool RenderScrollbarTheme::hasThumb(Scrollbar& scrollbar)
64{
65 return trackLength(scrollbar) - thumbLength(scrollbar) >= 0;
66}
67
68int RenderScrollbarTheme::minimumThumbLength(Scrollbar& scrollbar)
69{
70 return downcast<RenderScrollbar>(scrollbar).minimumThumbLength();
71}
72
73IntRect RenderScrollbarTheme::backButtonRect(Scrollbar& scrollbar, ScrollbarPart partType, bool)
74{
75 return downcast<RenderScrollbar>(scrollbar).buttonRect(partType);
76}
77
78IntRect RenderScrollbarTheme::forwardButtonRect(Scrollbar& scrollbar, ScrollbarPart partType, bool)
79{
80 return downcast<RenderScrollbar>(scrollbar).buttonRect(partType);
81}
82
83IntRect RenderScrollbarTheme::trackRect(Scrollbar& scrollbar, bool)
84{
85 if (!hasButtons(scrollbar))
86 return scrollbar.frameRect();
87
88 int startLength;
89 int endLength;
90 buttonSizesAlongTrackAxis(scrollbar, startLength, endLength);
91
92 return downcast<RenderScrollbar>(scrollbar).trackRect(startLength, endLength);
93}
94
95IntRect RenderScrollbarTheme::constrainTrackRectToTrackPieces(Scrollbar& scrollbar, const IntRect& rect)
96{
97 IntRect backRect = downcast<RenderScrollbar>(scrollbar).trackPieceRectWithMargins(BackTrackPart, rect);
98 IntRect forwardRect = downcast<RenderScrollbar>(scrollbar).trackPieceRectWithMargins(ForwardTrackPart, rect);
99 IntRect result = rect;
100 if (scrollbar.orientation() == HorizontalScrollbar) {
101 result.setX(backRect.x());
102 result.setWidth(forwardRect.maxX() - backRect.x());
103 } else {
104 result.setY(backRect.y());
105 result.setHeight(forwardRect.maxY() - backRect.y());
106 }
107 return result;
108}
109
110void RenderScrollbarTheme::willPaintScrollbar(GraphicsContext& context, Scrollbar& scrollbar)
111{
112 float opacity = downcast<RenderScrollbar>(scrollbar).opacity();
113 if (opacity != 1) {
114 context.save();
115 context.clip(scrollbar.frameRect());
116 context.beginTransparencyLayer(opacity);
117 }
118}
119
120void RenderScrollbarTheme::didPaintScrollbar(GraphicsContext& context, Scrollbar& scrollbar)
121{
122 float opacity = downcast<RenderScrollbar>(scrollbar).opacity();
123 if (opacity != 1) {
124 context.endTransparencyLayer();
125 context.restore();
126 }
127}
128
129void RenderScrollbarTheme::paintScrollCorner(GraphicsContext& context, const IntRect& cornerRect)
130{
131 ScrollbarTheme::theme().paintScrollCorner(context, cornerRect);
132}
133
134void RenderScrollbarTheme::paintScrollbarBackground(GraphicsContext& context, Scrollbar& scrollbar)
135{
136 downcast<RenderScrollbar>(scrollbar).paintPart(context, ScrollbarBGPart, scrollbar.frameRect());
137}
138
139void RenderScrollbarTheme::paintTrackBackground(GraphicsContext& context, Scrollbar& scrollbar, const IntRect& rect)
140{
141 downcast<RenderScrollbar>(scrollbar).paintPart(context, TrackBGPart, rect);
142}
143
144void RenderScrollbarTheme::paintTrackPiece(GraphicsContext& context, Scrollbar& scrollbar, const IntRect& rect, ScrollbarPart part)
145{
146 downcast<RenderScrollbar>(scrollbar).paintPart(context, part, rect);
147}
148
149void RenderScrollbarTheme::paintButton(GraphicsContext& context, Scrollbar& scrollbar, const IntRect& rect, ScrollbarPart part)
150{
151 downcast<RenderScrollbar>(scrollbar).paintPart(context, part, rect);
152}
153
154void RenderScrollbarTheme::paintThumb(GraphicsContext& context, Scrollbar& scrollbar, const IntRect& rect)
155{
156 downcast<RenderScrollbar>(scrollbar).paintPart(context, ThumbPart, rect);
157}
158
159void RenderScrollbarTheme::paintTickmarks(GraphicsContext& context, Scrollbar& scrollbar, const IntRect& rect)
160{
161 ScrollbarTheme::theme().paintTickmarks(context, scrollbar, rect);
162}
163
164}
165