OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2008, 2009 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 | |
29 #include "core/css/PseudoStyleRequest.h" | |
30 #include "core/frame/FrameView.h" | |
31 #include "core/frame/LocalFrame.h" | |
32 #include "core/layout/LayoutPart.h" | |
33 #include "core/rendering/RenderScrollbarPart.h" | |
34 #include "core/rendering/RenderScrollbarTheme.h" | |
35 #include "platform/graphics/GraphicsContext.h" | |
36 | |
37 namespace blink { | |
38 | |
39 PassRefPtrWillBeRawPtr<Scrollbar> RenderScrollbar::createCustomScrollbar(Scrolla
bleArea* scrollableArea, ScrollbarOrientation orientation, Node* ownerNode, Loca
lFrame* owningFrame) | |
40 { | |
41 return adoptRefWillBeNoop(new RenderScrollbar(scrollableArea, orientation, o
wnerNode, owningFrame)); | |
42 } | |
43 | |
44 RenderScrollbar::RenderScrollbar(ScrollableArea* scrollableArea, ScrollbarOrient
ation orientation, Node* ownerNode, LocalFrame* owningFrame) | |
45 : Scrollbar(scrollableArea, orientation, RegularScrollbar, RenderScrollbarTh
eme::renderScrollbarTheme()) | |
46 , m_owner(ownerNode) | |
47 , m_owningFrame(owningFrame) | |
48 { | |
49 ASSERT(ownerNode || owningFrame); | |
50 | |
51 // FIXME: We need to do this because RenderScrollbar::styleChanged is called
as soon as the scrollbar is created. | |
52 | |
53 // Update the scrollbar size. | |
54 IntRect rect(0, 0, 0, 0); | |
55 updateScrollbarPart(ScrollbarBGPart); | |
56 if (RenderScrollbarPart* part = m_parts.get(ScrollbarBGPart)) { | |
57 part->layout(); | |
58 rect.setSize(flooredIntSize(part->size())); | |
59 } else if (this->orientation() == HorizontalScrollbar) { | |
60 rect.setWidth(this->width()); | |
61 } else { | |
62 rect.setHeight(this->height()); | |
63 } | |
64 | |
65 setFrameRect(rect); | |
66 | |
67 } | |
68 | |
69 RenderScrollbar::~RenderScrollbar() | |
70 { | |
71 if (m_parts.isEmpty()) | |
72 return; | |
73 | |
74 // When a scrollbar is detached from its parent (causing all parts removal)
and | |
75 // ready to be destroyed, its destruction can be delayed because of RefPtr | |
76 // maintained in other classes such as EventHandler (m_lastScrollbarUnderMou
se). | |
77 // Meanwhile, we can have a call to updateScrollbarPart which recreates the | |
78 // scrollbar part. So, we need to destroy these parts since we don't want th
em | |
79 // to call on a destroyed scrollbar. See webkit bug 68009. | |
80 updateScrollbarParts(true); | |
81 } | |
82 | |
83 void RenderScrollbar::trace(Visitor* visitor) | |
84 { | |
85 #if ENABLE(OILPAN) | |
86 visitor->trace(m_owner); | |
87 visitor->trace(m_owningFrame); | |
88 #endif | |
89 Scrollbar::trace(visitor); | |
90 } | |
91 | |
92 RenderBox* RenderScrollbar::owningRenderer() const | |
93 { | |
94 if (m_owningFrame) { | |
95 RenderBox* currentRenderer = m_owningFrame->ownerRenderer(); | |
96 return currentRenderer; | |
97 } | |
98 return m_owner && m_owner->renderer() ? m_owner->renderer()->enclosingBox()
: 0; | |
99 } | |
100 | |
101 void RenderScrollbar::setParent(Widget* parent) | |
102 { | |
103 Scrollbar::setParent(parent); | |
104 if (!parent) { | |
105 // Destroy all of the scrollbar's RenderBoxes. | |
106 updateScrollbarParts(true); | |
107 } | |
108 } | |
109 | |
110 void RenderScrollbar::setEnabled(bool e) | |
111 { | |
112 bool wasEnabled = enabled(); | |
113 Scrollbar::setEnabled(e); | |
114 if (wasEnabled != e) | |
115 updateScrollbarParts(); | |
116 } | |
117 | |
118 void RenderScrollbar::styleChanged() | |
119 { | |
120 updateScrollbarParts(); | |
121 } | |
122 | |
123 void RenderScrollbar::setHoveredPart(ScrollbarPart part) | |
124 { | |
125 if (part == m_hoveredPart) | |
126 return; | |
127 | |
128 ScrollbarPart oldPart = m_hoveredPart; | |
129 m_hoveredPart = part; | |
130 | |
131 updateScrollbarPart(oldPart); | |
132 updateScrollbarPart(m_hoveredPart); | |
133 | |
134 updateScrollbarPart(ScrollbarBGPart); | |
135 updateScrollbarPart(TrackBGPart); | |
136 } | |
137 | |
138 void RenderScrollbar::setPressedPart(ScrollbarPart part) | |
139 { | |
140 ScrollbarPart oldPart = m_pressedPart; | |
141 Scrollbar::setPressedPart(part); | |
142 | |
143 updateScrollbarPart(oldPart); | |
144 updateScrollbarPart(part); | |
145 | |
146 updateScrollbarPart(ScrollbarBGPart); | |
147 updateScrollbarPart(TrackBGPart); | |
148 } | |
149 | |
150 PassRefPtr<LayoutStyle> RenderScrollbar::getScrollbarPseudoStyle(ScrollbarPart p
artType, PseudoId pseudoId) | |
151 { | |
152 if (!owningRenderer()) | |
153 return nullptr; | |
154 | |
155 RefPtr<LayoutStyle> result = owningRenderer()->getUncachedPseudoStyle(Pseudo
StyleRequest(pseudoId, this, partType), owningRenderer()->style()); | |
156 // Scrollbars for root frames should always have background color | |
157 // unless explicitly specified as transparent. So we force it. | |
158 // This is because WebKit assumes scrollbar to be always painted and missing
background | |
159 // causes visual artifact like non-paint invalidated dirty region. | |
160 if (result && m_owningFrame && m_owningFrame->view() && !m_owningFrame->view
()->isTransparent() && !result->hasBackground()) | |
161 result->setBackgroundColor(StyleColor(Color::white)); | |
162 | |
163 return result; | |
164 } | |
165 | |
166 void RenderScrollbar::updateScrollbarParts(bool destroy) | |
167 { | |
168 updateScrollbarPart(ScrollbarBGPart, destroy); | |
169 updateScrollbarPart(BackButtonStartPart, destroy); | |
170 updateScrollbarPart(ForwardButtonStartPart, destroy); | |
171 updateScrollbarPart(BackTrackPart, destroy); | |
172 updateScrollbarPart(ThumbPart, destroy); | |
173 updateScrollbarPart(ForwardTrackPart, destroy); | |
174 updateScrollbarPart(BackButtonEndPart, destroy); | |
175 updateScrollbarPart(ForwardButtonEndPart, destroy); | |
176 updateScrollbarPart(TrackBGPart, destroy); | |
177 | |
178 if (destroy) | |
179 return; | |
180 | |
181 // See if the scrollbar's thickness changed. If so, we need to mark our own
ing object as needing a layout. | |
182 bool isHorizontal = orientation() == HorizontalScrollbar; | |
183 int oldThickness = isHorizontal ? height() : width(); | |
184 int newThickness = 0; | |
185 RenderScrollbarPart* part = m_parts.get(ScrollbarBGPart); | |
186 if (part) { | |
187 part->layout(); | |
188 newThickness = isHorizontal ? part->size().height() : part->size().width
(); | |
189 } | |
190 | |
191 if (newThickness != oldThickness) { | |
192 setFrameRect(IntRect(location(), IntSize(isHorizontal ? width() : newThi
ckness, isHorizontal ? newThickness : height()))); | |
193 if (RenderBox* box = owningRenderer()) { | |
194 if (box->isRenderBlock()) | |
195 toRenderBlock(box)->notifyScrollbarThicknessChanged(); | |
196 box->setChildNeedsLayout(); | |
197 } | |
198 } | |
199 } | |
200 | |
201 static PseudoId pseudoForScrollbarPart(ScrollbarPart part) | |
202 { | |
203 switch (part) { | |
204 case BackButtonStartPart: | |
205 case ForwardButtonStartPart: | |
206 case BackButtonEndPart: | |
207 case ForwardButtonEndPart: | |
208 return SCROLLBAR_BUTTON; | |
209 case BackTrackPart: | |
210 case ForwardTrackPart: | |
211 return SCROLLBAR_TRACK_PIECE; | |
212 case ThumbPart: | |
213 return SCROLLBAR_THUMB; | |
214 case TrackBGPart: | |
215 return SCROLLBAR_TRACK; | |
216 case ScrollbarBGPart: | |
217 return SCROLLBAR; | |
218 case NoPart: | |
219 case AllParts: | |
220 break; | |
221 } | |
222 ASSERT_NOT_REACHED(); | |
223 return SCROLLBAR; | |
224 } | |
225 | |
226 void RenderScrollbar::updateScrollbarPart(ScrollbarPart partType, bool destroy) | |
227 { | |
228 if (partType == NoPart) | |
229 return; | |
230 | |
231 RefPtr<LayoutStyle> partStyle = !destroy ? getScrollbarPseudoStyle(partType,
pseudoForScrollbarPart(partType)) : PassRefPtr<LayoutStyle>(nullptr); | |
232 | |
233 bool needRenderer = !destroy && partStyle && partStyle->display() != NONE; | |
234 | |
235 if (needRenderer && partStyle->display() != BLOCK) { | |
236 // See if we are a button that should not be visible according to OS set
tings. | |
237 ScrollbarButtonsPlacement buttonsPlacement = theme()->buttonsPlacement()
; | |
238 switch (partType) { | |
239 case BackButtonStartPart: | |
240 needRenderer = (buttonsPlacement == ScrollbarButtonsSingle || bu
ttonsPlacement == ScrollbarButtonsDoubleStart || | |
241 buttonsPlacement == ScrollbarButtonsDoubleBoth); | |
242 break; | |
243 case ForwardButtonStartPart: | |
244 needRenderer = (buttonsPlacement == ScrollbarButtonsDoubleStart
|| buttonsPlacement == ScrollbarButtonsDoubleBoth); | |
245 break; | |
246 case BackButtonEndPart: | |
247 needRenderer = (buttonsPlacement == ScrollbarButtonsDoubleEnd ||
buttonsPlacement == ScrollbarButtonsDoubleBoth); | |
248 break; | |
249 case ForwardButtonEndPart: | |
250 needRenderer = (buttonsPlacement == ScrollbarButtonsSingle || bu
ttonsPlacement == ScrollbarButtonsDoubleEnd || | |
251 buttonsPlacement == ScrollbarButtonsDoubleBoth); | |
252 break; | |
253 default: | |
254 break; | |
255 } | |
256 } | |
257 | |
258 RenderScrollbarPart* partRenderer = m_parts.get(partType); | |
259 if (!partRenderer && needRenderer) { | |
260 partRenderer = RenderScrollbarPart::createAnonymous(&owningRenderer()->d
ocument(), this, partType); | |
261 m_parts.set(partType, partRenderer); | |
262 } else if (partRenderer && !needRenderer) { | |
263 m_parts.remove(partType); | |
264 partRenderer->destroy(); | |
265 partRenderer = 0; | |
266 } | |
267 | |
268 if (partRenderer) | |
269 partRenderer->setStyle(partStyle.release()); | |
270 } | |
271 | |
272 IntRect RenderScrollbar::buttonRect(ScrollbarPart partType) | |
273 { | |
274 RenderScrollbarPart* partRenderer = m_parts.get(partType); | |
275 if (!partRenderer) | |
276 return IntRect(); | |
277 | |
278 partRenderer->layout(); | |
279 | |
280 bool isHorizontal = orientation() == HorizontalScrollbar; | |
281 if (partType == BackButtonStartPart) | |
282 return IntRect(location(), IntSize(isHorizontal ? partRenderer->pixelSna
ppedWidth() : width(), isHorizontal ? height() : partRenderer->pixelSnappedHeigh
t())); | |
283 if (partType == ForwardButtonEndPart) | |
284 return IntRect(isHorizontal ? x() + width() - partRenderer->pixelSnapped
Width() : x(), | |
285 isHorizontal ? y() : y() + height() - partRenderer->pixel
SnappedHeight(), | |
286 isHorizontal ? partRenderer->pixelSnappedWidth() : width(
), | |
287 isHorizontal ? height() : partRenderer->pixelSnappedHeigh
t()); | |
288 | |
289 if (partType == ForwardButtonStartPart) { | |
290 IntRect previousButton = buttonRect(BackButtonStartPart); | |
291 return IntRect(isHorizontal ? x() + previousButton.width() : x(), | |
292 isHorizontal ? y() : y() + previousButton.height(), | |
293 isHorizontal ? partRenderer->pixelSnappedWidth() : width(
), | |
294 isHorizontal ? height() : partRenderer->pixelSnappedHeigh
t()); | |
295 } | |
296 | |
297 IntRect followingButton = buttonRect(ForwardButtonEndPart); | |
298 return IntRect(isHorizontal ? x() + width() - followingButton.width() - part
Renderer->pixelSnappedWidth() : x(), | |
299 isHorizontal ? y() : y() + height() - followingButton.height(
) - partRenderer->pixelSnappedHeight(), | |
300 isHorizontal ? partRenderer->pixelSnappedWidth() : width(), | |
301 isHorizontal ? height() : partRenderer->pixelSnappedHeight())
; | |
302 } | |
303 | |
304 IntRect RenderScrollbar::trackRect(int startLength, int endLength) | |
305 { | |
306 RenderScrollbarPart* part = m_parts.get(TrackBGPart); | |
307 if (part) | |
308 part->layout(); | |
309 | |
310 if (orientation() == HorizontalScrollbar) { | |
311 int marginLeft = part ? static_cast<int>(part->marginLeft()) : 0; | |
312 int marginRight = part ? static_cast<int>(part->marginRight()) : 0; | |
313 startLength += marginLeft; | |
314 endLength += marginRight; | |
315 int totalLength = startLength + endLength; | |
316 return IntRect(x() + startLength, y(), width() - totalLength, height()); | |
317 } | |
318 | |
319 int marginTop = part ? static_cast<int>(part->marginTop()) : 0; | |
320 int marginBottom = part ? static_cast<int>(part->marginBottom()) : 0; | |
321 startLength += marginTop; | |
322 endLength += marginBottom; | |
323 int totalLength = startLength + endLength; | |
324 | |
325 return IntRect(x(), y() + startLength, width(), height() - totalLength); | |
326 } | |
327 | |
328 IntRect RenderScrollbar::trackPieceRectWithMargins(ScrollbarPart partType, const
IntRect& oldRect) | |
329 { | |
330 RenderScrollbarPart* partRenderer = m_parts.get(partType); | |
331 if (!partRenderer) | |
332 return oldRect; | |
333 | |
334 partRenderer->layout(); | |
335 | |
336 IntRect rect = oldRect; | |
337 if (orientation() == HorizontalScrollbar) { | |
338 rect.setX(rect.x() + partRenderer->marginLeft()); | |
339 rect.setWidth(rect.width() - partRenderer->marginWidth()); | |
340 } else { | |
341 rect.setY(rect.y() + partRenderer->marginTop()); | |
342 rect.setHeight(rect.height() - partRenderer->marginHeight()); | |
343 } | |
344 return rect; | |
345 } | |
346 | |
347 int RenderScrollbar::minimumThumbLength() | |
348 { | |
349 RenderScrollbarPart* partRenderer = m_parts.get(ThumbPart); | |
350 if (!partRenderer) | |
351 return 0; | |
352 partRenderer->layout(); | |
353 return orientation() == HorizontalScrollbar ? partRenderer->size().width() :
partRenderer->size().height(); | |
354 } | |
355 | |
356 } | |
OLD | NEW |