2 * Copyright (C) 2008 Apple 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
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.
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.
27 #include "core/layout/LayoutScrollbarTheme.h"
29 #include "core/layout/LayoutScrollbar.h"
30 #include "core/paint/ScrollbarPainter.h"
31 #include "platform/graphics/GraphicsContext.h"
32 #include "platform/graphics/paint/DrawingRecorder.h"
33 #include "platform/scroll/ScrollbarThemeClient.h"
34 #include "wtf/StdLibExtras.h"
38 LayoutScrollbarTheme
* LayoutScrollbarTheme::layoutScrollbarTheme()
40 DEFINE_STATIC_LOCAL(LayoutScrollbarTheme
, theme
, ());
44 void LayoutScrollbarTheme::buttonSizesAlongTrackAxis(ScrollbarThemeClient
* scrollbar
, int& beforeSize
, int& afterSize
)
46 IntRect firstButton
= backButtonRect(scrollbar
, BackButtonStartPart
);
47 IntRect secondButton
= forwardButtonRect(scrollbar
, ForwardButtonStartPart
);
48 IntRect thirdButton
= backButtonRect(scrollbar
, BackButtonEndPart
);
49 IntRect fourthButton
= forwardButtonRect(scrollbar
, ForwardButtonEndPart
);
50 if (scrollbar
->orientation() == HorizontalScrollbar
) {
51 beforeSize
= firstButton
.width() + secondButton
.width();
52 afterSize
= thirdButton
.width() + fourthButton
.width();
54 beforeSize
= firstButton
.height() + secondButton
.height();
55 afterSize
= thirdButton
.height() + fourthButton
.height();
59 bool LayoutScrollbarTheme::hasButtons(ScrollbarThemeClient
* scrollbar
)
63 buttonSizesAlongTrackAxis(scrollbar
, startSize
, endSize
);
64 return (startSize
+ endSize
) <= (scrollbar
->orientation() == HorizontalScrollbar
? scrollbar
->width() : scrollbar
->height());
67 bool LayoutScrollbarTheme::hasThumb(ScrollbarThemeClient
* scrollbar
)
69 return trackLength(scrollbar
) - thumbLength(scrollbar
) >= 0;
72 int LayoutScrollbarTheme::minimumThumbLength(ScrollbarThemeClient
* scrollbar
)
74 return toLayoutScrollbar(scrollbar
)->minimumThumbLength();
77 IntRect
LayoutScrollbarTheme::backButtonRect(ScrollbarThemeClient
* scrollbar
, ScrollbarPart partType
, bool)
79 return toLayoutScrollbar(scrollbar
)->buttonRect(partType
);
82 IntRect
LayoutScrollbarTheme::forwardButtonRect(ScrollbarThemeClient
* scrollbar
, ScrollbarPart partType
, bool)
84 return toLayoutScrollbar(scrollbar
)->buttonRect(partType
);
87 IntRect
LayoutScrollbarTheme::trackRect(ScrollbarThemeClient
* scrollbar
, bool)
89 if (!hasButtons(scrollbar
))
90 return scrollbar
->frameRect();
94 buttonSizesAlongTrackAxis(scrollbar
, startLength
, endLength
);
96 return toLayoutScrollbar(scrollbar
)->trackRect(startLength
, endLength
);
99 IntRect
LayoutScrollbarTheme::constrainTrackRectToTrackPieces(ScrollbarThemeClient
* scrollbar
, const IntRect
& rect
)
101 IntRect backRect
= toLayoutScrollbar(scrollbar
)->trackPieceRectWithMargins(BackTrackPart
, rect
);
102 IntRect forwardRect
= toLayoutScrollbar(scrollbar
)->trackPieceRectWithMargins(ForwardTrackPart
, rect
);
103 IntRect result
= rect
;
104 if (scrollbar
->orientation() == HorizontalScrollbar
) {
105 result
.setX(backRect
.x());
106 result
.setWidth(forwardRect
.maxX() - backRect
.x());
108 result
.setY(backRect
.y());
109 result
.setHeight(forwardRect
.maxY() - backRect
.y());
114 void LayoutScrollbarTheme::paintScrollCorner(GraphicsContext
* context
, const DisplayItemClientWrapper
& displayItemClient
, const IntRect
& cornerRect
)
116 if (DrawingRecorder::useCachedDrawingIfPossible(*context
, displayItemClient
, DisplayItem::ScrollbarCorner
))
119 DrawingRecorder
recorder(*context
, displayItemClient
, DisplayItem::ScrollbarCorner
, cornerRect
);
121 context
->fillRect(cornerRect
, Color::white
);
124 void LayoutScrollbarTheme::paintScrollbarBackground(GraphicsContext
* context
, ScrollbarThemeClient
* scrollbar
)
126 ScrollbarPainter(*toLayoutScrollbar(scrollbar
)).paintPart(context
, ScrollbarBGPart
, scrollbar
->frameRect());
129 void LayoutScrollbarTheme::paintTrackBackground(GraphicsContext
* context
, ScrollbarThemeClient
* scrollbar
, const IntRect
& rect
)
131 ScrollbarPainter(*toLayoutScrollbar(scrollbar
)).paintPart(context
, TrackBGPart
, rect
);
134 void LayoutScrollbarTheme::paintTrackPiece(GraphicsContext
* context
, ScrollbarThemeClient
* scrollbar
, const IntRect
& rect
, ScrollbarPart part
)
136 ScrollbarPainter(*toLayoutScrollbar(scrollbar
)).paintPart(context
, part
, rect
);
139 void LayoutScrollbarTheme::paintButton(GraphicsContext
* context
, ScrollbarThemeClient
* scrollbar
, const IntRect
& rect
, ScrollbarPart part
)
141 ScrollbarPainter(*toLayoutScrollbar(scrollbar
)).paintPart(context
, part
, rect
);
144 void LayoutScrollbarTheme::paintThumb(GraphicsContext
* context
, ScrollbarThemeClient
* scrollbar
, const IntRect
& rect
)
146 ScrollbarPainter(*toLayoutScrollbar(scrollbar
)).paintPart(context
, ThumbPart
, rect
);
149 void LayoutScrollbarTheme::paintTickmarks(GraphicsContext
* context
, ScrollbarThemeClient
* scrollbar
, const IntRect
& rect
)
151 ScrollbarTheme::theme()->paintTickmarks(context
, scrollbar
, rect
);