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/graphics/GraphicsContext.h"
30#include "platform/scroll/ScrollbarThemeClient.h"
31#include "wtf/StdLibExtras.h"
32
33namespace blink {
34
35RenderScrollbarTheme* RenderScrollbarTheme::renderScrollbarTheme()
36{
37    DEFINE_STATIC_LOCAL(RenderScrollbarTheme, theme, ());
38    return &theme;
39}
40
41void RenderScrollbarTheme::buttonSizesAlongTrackAxis(ScrollbarThemeClient* scrollbar, int& beforeSize, int& afterSize)
42{
43    IntRect firstButton = backButtonRect(scrollbar, BackButtonStartPart);
44    IntRect secondButton = forwardButtonRect(scrollbar, ForwardButtonStartPart);
45    IntRect thirdButton = backButtonRect(scrollbar, BackButtonEndPart);
46    IntRect fourthButton = forwardButtonRect(scrollbar, ForwardButtonEndPart);
47    if (scrollbar->orientation() == HorizontalScrollbar) {
48        beforeSize = firstButton.width() + secondButton.width();
49        afterSize = thirdButton.width() + fourthButton.width();
50    } else {
51        beforeSize = firstButton.height() + secondButton.height();
52        afterSize = thirdButton.height() + fourthButton.height();
53    }
54}
55
56bool RenderScrollbarTheme::hasButtons(ScrollbarThemeClient* scrollbar)
57{
58    int startSize;
59    int endSize;
60    buttonSizesAlongTrackAxis(scrollbar, startSize, endSize);
61    return (startSize + endSize) <= (scrollbar->orientation() == HorizontalScrollbar ? scrollbar->width() : scrollbar->height());
62}
63
64bool RenderScrollbarTheme::hasThumb(ScrollbarThemeClient* scrollbar)
65{
66    return trackLength(scrollbar) - thumbLength(scrollbar) >= 0;
67}
68
69int RenderScrollbarTheme::minimumThumbLength(ScrollbarThemeClient* scrollbar)
70{
71    return toRenderScrollbar(scrollbar)->minimumThumbLength();
72}
73
74IntRect RenderScrollbarTheme::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart partType, bool)
75{
76    return toRenderScrollbar(scrollbar)->buttonRect(partType);
77}
78
79IntRect RenderScrollbarTheme::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart partType, bool)
80{
81    return toRenderScrollbar(scrollbar)->buttonRect(partType);
82}
83
84IntRect RenderScrollbarTheme::trackRect(ScrollbarThemeClient* scrollbar, bool)
85{
86    if (!hasButtons(scrollbar))
87        return scrollbar->frameRect();
88
89    int startLength;
90    int endLength;
91    buttonSizesAlongTrackAxis(scrollbar, startLength, endLength);
92
93    return toRenderScrollbar(scrollbar)->trackRect(startLength, endLength);
94}
95
96IntRect RenderScrollbarTheme::constrainTrackRectToTrackPieces(ScrollbarThemeClient* scrollbar, const IntRect& rect)
97{
98    IntRect backRect = toRenderScrollbar(scrollbar)->trackPieceRectWithMargins(BackTrackPart, rect);
99    IntRect forwardRect = toRenderScrollbar(scrollbar)->trackPieceRectWithMargins(ForwardTrackPart, rect);
100    IntRect result = rect;
101    if (scrollbar->orientation() == HorizontalScrollbar) {
102        result.setX(backRect.x());
103        result.setWidth(forwardRect.maxX() - backRect.x());
104    } else {
105        result.setY(backRect.y());
106        result.setHeight(forwardRect.maxY() - backRect.y());
107    }
108    return result;
109}
110
111void RenderScrollbarTheme::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
112{
113    // FIXME: Implement.
114    context->fillRect(cornerRect, Color::white);
115}
116
117void RenderScrollbarTheme::paintScrollbarBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar)
118{
119    toRenderScrollbar(scrollbar)->paintPart(context, ScrollbarBGPart, scrollbar->frameRect());
120}
121
122void RenderScrollbarTheme::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
123{
124    toRenderScrollbar(scrollbar)->paintPart(context, TrackBGPart, rect);
125}
126
127void RenderScrollbarTheme::paintTrackPiece(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
128{
129    toRenderScrollbar(scrollbar)->paintPart(context, part, rect);
130}
131
132void RenderScrollbarTheme::paintButton(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
133{
134    toRenderScrollbar(scrollbar)->paintPart(context, part, rect);
135}
136
137void RenderScrollbarTheme::paintThumb(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
138{
139    toRenderScrollbar(scrollbar)->paintPart(context, ThumbPart, rect);
140}
141
142void RenderScrollbarTheme::paintTickmarks(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
143{
144    ScrollbarTheme::theme()->paintTickmarks(context, scrollbar, rect);
145}
146
147}
148