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