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 "RenderScrollbarPart.h"
29 #include "PaintInfo.h"
30 #include "RenderScrollbar.h"
31 #include "RenderScrollbarTheme.h"
32 #include "RenderView.h"
38 RenderScrollbarPart::RenderScrollbarPart(Node* node, RenderScrollbar* scrollbar, ScrollbarPart part)
40 , m_scrollbar(scrollbar)
45 RenderScrollbarPart::~RenderScrollbarPart()
49 void RenderScrollbarPart::layout()
51 setLocation(LayoutPoint()); // We don't worry about positioning ourselves. We're just determining our minimum width/height.
52 if (m_scrollbar->orientation() == HorizontalScrollbar)
53 layoutHorizontalPart();
57 setNeedsLayout(false);
60 void RenderScrollbarPart::layoutHorizontalPart()
62 if (m_part == ScrollbarBGPart) {
63 setWidth(m_scrollbar->width());
64 computeScrollbarHeight();
66 computeScrollbarWidth();
67 setHeight(m_scrollbar->height());
71 void RenderScrollbarPart::layoutVerticalPart()
73 if (m_part == ScrollbarBGPart) {
74 computeScrollbarWidth();
75 setHeight(m_scrollbar->height());
77 setWidth(m_scrollbar->width());
78 computeScrollbarHeight();
82 static int calcScrollbarThicknessUsing(SizeType sizeType, const Length& length, int containingLength, RenderView* renderView)
84 if (!length.isIntrinsicOrAuto() || (sizeType == MinSize && length.isAuto()))
85 return minimumValueForLength(length, containingLength, renderView);
86 return ScrollbarTheme::theme()->scrollbarThickness();
89 void RenderScrollbarPart::computeScrollbarWidth()
91 if (!m_scrollbar->owningRenderer())
93 RenderView* renderView = view();
94 int visibleSize = m_scrollbar->owningRenderer()->width() - m_scrollbar->owningRenderer()->borderLeft() - m_scrollbar->owningRenderer()->borderRight();
95 int w = calcScrollbarThicknessUsing(MainOrPreferredSize, style()->width(), visibleSize, renderView);
96 int minWidth = calcScrollbarThicknessUsing(MinSize, style()->minWidth(), visibleSize, renderView);
97 int maxWidth = style()->maxWidth().isUndefined() ? w : calcScrollbarThicknessUsing(MaxSize, style()->maxWidth(), visibleSize, renderView);
98 setWidth(max(minWidth, min(maxWidth, w)));
100 // Buttons and track pieces can all have margins along the axis of the scrollbar.
101 m_marginBox.setLeft(minimumValueForLength(style()->marginLeft(), visibleSize, renderView));
102 m_marginBox.setRight(minimumValueForLength(style()->marginRight(), visibleSize, renderView));
105 void RenderScrollbarPart::computeScrollbarHeight()
107 if (!m_scrollbar->owningRenderer())
109 RenderView* renderView = view();
110 int visibleSize = m_scrollbar->owningRenderer()->height() - m_scrollbar->owningRenderer()->borderTop() - m_scrollbar->owningRenderer()->borderBottom();
111 int h = calcScrollbarThicknessUsing(MainOrPreferredSize, style()->height(), visibleSize, renderView);
112 int minHeight = calcScrollbarThicknessUsing(MinSize, style()->minHeight(), visibleSize, renderView);
113 int maxHeight = style()->maxHeight().isUndefined() ? h : calcScrollbarThicknessUsing(MaxSize, style()->maxHeight(), visibleSize, renderView);
114 setHeight(max(minHeight, min(maxHeight, h)));
116 // Buttons and track pieces can all have margins along the axis of the scrollbar.
117 m_marginBox.setTop(minimumValueForLength(style()->marginTop(), visibleSize, renderView));
118 m_marginBox.setBottom(minimumValueForLength(style()->marginBottom(), visibleSize, renderView));
121 void RenderScrollbarPart::computePreferredLogicalWidths()
123 if (!preferredLogicalWidthsDirty())
126 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
128 setPreferredLogicalWidthsDirty(false);
131 void RenderScrollbarPart::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
133 RenderBlock::styleWillChange(diff, newStyle);
137 void RenderScrollbarPart::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
139 RenderBlock::styleDidChange(diff, oldStyle);
141 setPositioned(false);
143 setHasOverflowClip(false);
144 if (oldStyle && m_scrollbar && m_part != NoPart && diff >= StyleDifferenceRepaint)
145 m_scrollbar->theme()->invalidatePart(m_scrollbar, m_part);
148 void RenderScrollbarPart::imageChanged(WrappedImagePtr image, const IntRect* rect)
150 if (m_scrollbar && m_part != NoPart)
151 m_scrollbar->theme()->invalidatePart(m_scrollbar, m_part);
153 if (FrameView* frameView = view()->frameView()) {
154 if (frameView->isFrameViewScrollCorner(this)) {
155 frameView->invalidateScrollCorner(frameView->scrollCornerRect());
160 RenderBlock::imageChanged(image, rect);
164 void RenderScrollbarPart::paintIntoRect(GraphicsContext* graphicsContext, const LayoutPoint& paintOffset, const LayoutRect& rect)
166 // Make sure our dimensions match the rect.
167 setLocation(rect.location() - toSize(paintOffset));
168 setWidth(rect.width());
169 setHeight(rect.height());
171 if (graphicsContext->paintingDisabled())
175 PaintInfo paintInfo(graphicsContext, pixelSnappedIntRect(rect), PaintPhaseBlockBackground, false, 0, 0, 0);
176 paint(paintInfo, paintOffset);
177 paintInfo.phase = PaintPhaseChildBlockBackgrounds;
178 paint(paintInfo, paintOffset);
179 paintInfo.phase = PaintPhaseFloat;
180 paint(paintInfo, paintOffset);
181 paintInfo.phase = PaintPhaseForeground;
182 paint(paintInfo, paintOffset);
183 paintInfo.phase = PaintPhaseOutline;
184 paint(paintInfo, paintOffset);
187 RenderObject* RenderScrollbarPart::rendererOwningScrollbar() const
191 return m_scrollbar->owningRenderer();