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 "core/rendering/RenderScrollbar.h"
28 #include "core/rendering/RenderScrollbarTheme.h"
29 #include "platform/scroll/ScrollbarThemeClient.h"
30 #include "wtf/StdLibExtras.h"
31
32 namespace WebCore {
33
renderScrollbarTheme()34 RenderScrollbarTheme* RenderScrollbarTheme::renderScrollbarTheme()
35 {
36 DEFINE_STATIC_LOCAL(RenderScrollbarTheme, theme, ());
37 return &theme;
38 }
39
buttonSizesAlongTrackAxis(ScrollbarThemeClient * scrollbar,int & beforeSize,int & afterSize)40 void RenderScrollbarTheme::buttonSizesAlongTrackAxis(ScrollbarThemeClient* 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
hasButtons(ScrollbarThemeClient * scrollbar)55 bool RenderScrollbarTheme::hasButtons(ScrollbarThemeClient* 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
hasThumb(ScrollbarThemeClient * scrollbar)63 bool RenderScrollbarTheme::hasThumb(ScrollbarThemeClient* scrollbar)
64 {
65 return trackLength(scrollbar) - thumbLength(scrollbar) >= 0;
66 }
67
minimumThumbLength(ScrollbarThemeClient * scrollbar)68 int RenderScrollbarTheme::minimumThumbLength(ScrollbarThemeClient* scrollbar)
69 {
70 return toRenderScrollbar(scrollbar)->minimumThumbLength();
71 }
72
backButtonRect(ScrollbarThemeClient * scrollbar,ScrollbarPart partType,bool)73 IntRect RenderScrollbarTheme::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart partType, bool)
74 {
75 return toRenderScrollbar(scrollbar)->buttonRect(partType);
76 }
77
forwardButtonRect(ScrollbarThemeClient * scrollbar,ScrollbarPart partType,bool)78 IntRect RenderScrollbarTheme::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart partType, bool)
79 {
80 return toRenderScrollbar(scrollbar)->buttonRect(partType);
81 }
82
trackRect(ScrollbarThemeClient * scrollbar,bool)83 IntRect RenderScrollbarTheme::trackRect(ScrollbarThemeClient* 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 toRenderScrollbar(scrollbar)->trackRect(startLength, endLength);
93 }
94
constrainTrackRectToTrackPieces(ScrollbarThemeClient * scrollbar,const IntRect & rect)95 IntRect RenderScrollbarTheme::constrainTrackRectToTrackPieces(ScrollbarThemeClient* scrollbar, const IntRect& rect)
96 {
97 IntRect backRect = toRenderScrollbar(scrollbar)->trackPieceRectWithMargins(BackTrackPart, rect);
98 IntRect forwardRect = toRenderScrollbar(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
paintScrollCorner(GraphicsContext * context,const IntRect & cornerRect)110 void RenderScrollbarTheme::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
111 {
112 // FIXME: Implement.
113 context->fillRect(cornerRect, Color::white);
114 }
115
paintScrollbarBackground(GraphicsContext * context,ScrollbarThemeClient * scrollbar)116 void RenderScrollbarTheme::paintScrollbarBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar)
117 {
118 toRenderScrollbar(scrollbar)->paintPart(context, ScrollbarBGPart, scrollbar->frameRect());
119 }
120
paintTrackBackground(GraphicsContext * context,ScrollbarThemeClient * scrollbar,const IntRect & rect)121 void RenderScrollbarTheme::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
122 {
123 toRenderScrollbar(scrollbar)->paintPart(context, TrackBGPart, rect);
124 }
125
paintTrackPiece(GraphicsContext * context,ScrollbarThemeClient * scrollbar,const IntRect & rect,ScrollbarPart part)126 void RenderScrollbarTheme::paintTrackPiece(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
127 {
128 toRenderScrollbar(scrollbar)->paintPart(context, part, rect);
129 }
130
paintButton(GraphicsContext * context,ScrollbarThemeClient * scrollbar,const IntRect & rect,ScrollbarPart part)131 void RenderScrollbarTheme::paintButton(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
132 {
133 toRenderScrollbar(scrollbar)->paintPart(context, part, rect);
134 }
135
paintThumb(GraphicsContext * context,ScrollbarThemeClient * scrollbar,const IntRect & rect)136 void RenderScrollbarTheme::paintThumb(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
137 {
138 toRenderScrollbar(scrollbar)->paintPart(context, ThumbPart, rect);
139 }
140
paintTickmarks(GraphicsContext * context,ScrollbarThemeClient * scrollbar,const IntRect & rect)141 void RenderScrollbarTheme::paintTickmarks(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
142 {
143 ScrollbarTheme::theme()->paintTickmarks(context, scrollbar, rect);
144 }
145
146 }
147