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/rendering/RenderScrollbarPart.h"
29 #include "core/frame/UseCounter.h"
30 #include "core/rendering/PaintInfo.h"
31 #include "core/rendering/RenderScrollbar.h"
32 #include "core/rendering/RenderScrollbarTheme.h"
33 #include "core/rendering/RenderView.h"
34 #include "platform/LengthFunctions.h"
38 RenderScrollbarPart::RenderScrollbarPart(RenderScrollbar* scrollbar, ScrollbarPart part)
40 , m_scrollbar(scrollbar)
45 RenderScrollbarPart::~RenderScrollbarPart()
49 static void recordScrollbarPartStats(Document& document, ScrollbarPart part)
52 case BackButtonStartPart:
53 case ForwardButtonStartPart:
54 case BackButtonEndPart:
55 case ForwardButtonEndPart:
56 UseCounter::count(document, UseCounter::CSSSelectorPseudoScrollbarButton);
59 case ForwardTrackPart:
60 UseCounter::count(document, UseCounter::CSSSelectorPseudoScrollbarTrackPiece);
63 UseCounter::count(document, UseCounter::CSSSelectorPseudoScrollbarThumb);
66 UseCounter::count(document, UseCounter::CSSSelectorPseudoScrollbarTrack);
69 UseCounter::count(document, UseCounter::CSSSelectorPseudoScrollbar);
77 RenderScrollbarPart* RenderScrollbarPart::createAnonymous(Document* document, RenderScrollbar* scrollbar, ScrollbarPart part)
79 RenderScrollbarPart* renderer = new RenderScrollbarPart(scrollbar, part);
80 recordScrollbarPartStats(*document, part);
81 renderer->setDocumentForAnonymous(document);
85 void RenderScrollbarPart::layout()
87 setLocation(LayoutPoint()); // We don't worry about positioning ourselves. We're just determining our minimum width/height.
88 if (m_scrollbar->orientation() == HorizontalScrollbar)
89 layoutHorizontalPart();
96 void RenderScrollbarPart::layoutHorizontalPart()
98 if (m_part == ScrollbarBGPart) {
99 setWidth(m_scrollbar->width());
100 computeScrollbarHeight();
102 computeScrollbarWidth();
103 setHeight(m_scrollbar->height());
107 void RenderScrollbarPart::layoutVerticalPart()
109 if (m_part == ScrollbarBGPart) {
110 computeScrollbarWidth();
111 setHeight(m_scrollbar->height());
113 setWidth(m_scrollbar->width());
114 computeScrollbarHeight();
118 static int calcScrollbarThicknessUsing(SizeType sizeType, const Length& length, int containingLength)
120 if (!length.isIntrinsicOrAuto() || (sizeType == MinSize && length.isAuto()))
121 return minimumValueForLength(length, containingLength);
122 return ScrollbarTheme::theme()->scrollbarThickness();
125 void RenderScrollbarPart::computeScrollbarWidth()
127 if (!m_scrollbar->owningRenderer())
129 // FIXME: We are querying layout information but nothing guarantees that it's up-to-date, especially since we are called at style change.
130 // FIXME: Querying the style's border information doesn't work on table cells with collapsing borders.
131 int visibleSize = m_scrollbar->owningRenderer()->width() - m_scrollbar->owningRenderer()->style()->borderLeftWidth() - m_scrollbar->owningRenderer()->style()->borderRightWidth();
132 int w = calcScrollbarThicknessUsing(MainOrPreferredSize, style()->width(), visibleSize);
133 int minWidth = calcScrollbarThicknessUsing(MinSize, style()->minWidth(), visibleSize);
134 int maxWidth = style()->maxWidth().isMaxSizeNone() ? w : calcScrollbarThicknessUsing(MaxSize, style()->maxWidth(), visibleSize);
135 setWidth(std::max(minWidth, std::min(maxWidth, w)));
137 // Buttons and track pieces can all have margins along the axis of the scrollbar.
138 m_marginBox.setLeft(minimumValueForLength(style()->marginLeft(), visibleSize));
139 m_marginBox.setRight(minimumValueForLength(style()->marginRight(), visibleSize));
142 void RenderScrollbarPart::computeScrollbarHeight()
144 if (!m_scrollbar->owningRenderer())
146 // FIXME: We are querying layout information but nothing guarantees that it's up-to-date, especially since we are called at style change.
147 // FIXME: Querying the style's border information doesn't work on table cells with collapsing borders.
148 int visibleSize = m_scrollbar->owningRenderer()->height() - m_scrollbar->owningRenderer()->style()->borderTopWidth() - m_scrollbar->owningRenderer()->style()->borderBottomWidth();
149 int h = calcScrollbarThicknessUsing(MainOrPreferredSize, style()->height(), visibleSize);
150 int minHeight = calcScrollbarThicknessUsing(MinSize, style()->minHeight(), visibleSize);
151 int maxHeight = style()->maxHeight().isMaxSizeNone() ? h : calcScrollbarThicknessUsing(MaxSize, style()->maxHeight(), visibleSize);
152 setHeight(std::max(minHeight, std::min(maxHeight, h)));
154 // Buttons and track pieces can all have margins along the axis of the scrollbar.
155 m_marginBox.setTop(minimumValueForLength(style()->marginTop(), visibleSize));
156 m_marginBox.setBottom(minimumValueForLength(style()->marginBottom(), visibleSize));
159 void RenderScrollbarPart::computePreferredLogicalWidths()
161 if (!preferredLogicalWidthsDirty())
164 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
166 clearPreferredLogicalWidthsDirty();
169 void RenderScrollbarPart::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
171 RenderBlock::styleWillChange(diff, newStyle);
175 void RenderScrollbarPart::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
177 RenderBlock::styleDidChange(diff, oldStyle);
179 clearPositionedState();
181 setHasOverflowClip(false);
182 if (oldStyle && m_scrollbar && m_part != NoPart && (diff.needsPaintInvalidation() || diff.needsLayout()))
183 m_scrollbar->theme()->invalidatePart(m_scrollbar, m_part);
186 void RenderScrollbarPart::imageChanged(WrappedImagePtr image, const IntRect* rect)
188 if (m_scrollbar && m_part != NoPart)
189 m_scrollbar->theme()->invalidatePart(m_scrollbar, m_part);
191 if (FrameView* frameView = view()->frameView()) {
192 if (frameView->isFrameViewScrollCorner(this)) {
193 frameView->invalidateScrollCorner(frameView->scrollCornerRect());
198 RenderBlock::imageChanged(image, rect);
202 RenderObject* RenderScrollbarPart::rendererOwningScrollbar() const
206 return m_scrollbar->owningRenderer();