1 /*
2 * Copyright (C) 2004, 2006, 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 COMPUTER, 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 COMPUTER, 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 "Scrollbar.h"
28
29 #include "GraphicsContext.h"
30 #include "PlatformMouseEvent.h"
31 #include "ScrollAnimator.h"
32 #include "ScrollableArea.h"
33 #include "ScrollbarTheme.h"
34 #include <algorithm>
35
36 // FIXME: The following #includes are a layering violation and should be removed.
37 #include "AXObjectCache.h"
38 #include "AccessibilityScrollbar.h"
39 #include "Document.h"
40 #include "EventHandler.h"
41 #include "Frame.h"
42 #include "FrameView.h"
43
44 using namespace std;
45
46 #if (PLATFORM(CHROMIUM) && (OS(UNIX) && !OS(DARWIN))) || PLATFORM(GTK)
47 // The position of the scrollbar thumb affects the appearance of the steppers, so
48 // when the thumb moves, we have to invalidate them for painting.
49 #define THUMB_POSITION_AFFECTS_BUTTONS
50 #endif
51
52 namespace WebCore {
53
54 #if !PLATFORM(EFL)
createNativeScrollbar(ScrollableArea * scrollableArea,ScrollbarOrientation orientation,ScrollbarControlSize size)55 PassRefPtr<Scrollbar> Scrollbar::createNativeScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
56 {
57 return adoptRef(new Scrollbar(scrollableArea, orientation, size));
58 }
59 #endif
60
maxOverlapBetweenPages()61 int Scrollbar::maxOverlapBetweenPages()
62 {
63 static int maxOverlapBetweenPages = ScrollbarTheme::nativeTheme()->maxOverlapBetweenPages();
64 return maxOverlapBetweenPages;
65 }
66
Scrollbar(ScrollableArea * scrollableArea,ScrollbarOrientation orientation,ScrollbarControlSize controlSize,ScrollbarTheme * theme)67 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize,
68 ScrollbarTheme* theme)
69 : m_scrollableArea(scrollableArea)
70 , m_orientation(orientation)
71 , m_controlSize(controlSize)
72 , m_theme(theme)
73 , m_visibleSize(0)
74 , m_totalSize(0)
75 , m_currentPos(0)
76 , m_dragOrigin(0)
77 , m_lineStep(0)
78 , m_pageStep(0)
79 , m_pixelStep(1)
80 , m_hoveredPart(NoPart)
81 , m_pressedPart(NoPart)
82 , m_pressedPos(0)
83 , m_draggingDocument(false)
84 , m_documentDragPos(0)
85 , m_enabled(true)
86 , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
87 , m_overlapsResizer(false)
88 , m_suppressInvalidation(false)
89 {
90 if (!m_theme)
91 m_theme = ScrollbarTheme::nativeTheme();
92
93 m_theme->registerScrollbar(this);
94
95 // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
96 // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
97 // alone when sizing).
98 int thickness = m_theme->scrollbarThickness(controlSize);
99 Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
100 }
101
~Scrollbar()102 Scrollbar::~Scrollbar()
103 {
104 if (AXObjectCache::accessibilityEnabled() && axObjectCache())
105 axObjectCache()->remove(this);
106
107 stopTimerIfNeeded();
108
109 m_theme->unregisterScrollbar(this);
110 }
111
offsetDidChange()112 void Scrollbar::offsetDidChange()
113 {
114 ASSERT(m_scrollableArea);
115
116 float position = static_cast<float>(m_scrollableArea->scrollPosition(this));
117 if (position == m_currentPos)
118 return;
119
120 int oldThumbPosition = theme()->thumbPosition(this);
121 m_currentPos = position;
122 updateThumbPosition();
123 if (m_pressedPart == ThumbPart)
124 setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
125 }
126
setProportion(int visibleSize,int totalSize)127 void Scrollbar::setProportion(int visibleSize, int totalSize)
128 {
129 if (visibleSize == m_visibleSize && totalSize == m_totalSize)
130 return;
131
132 m_visibleSize = visibleSize;
133 m_totalSize = totalSize;
134
135 updateThumbProportion();
136 }
137
setSteps(int lineStep,int pageStep,int pixelsPerStep)138 void Scrollbar::setSteps(int lineStep, int pageStep, int pixelsPerStep)
139 {
140 m_lineStep = lineStep;
141 m_pageStep = pageStep;
142 m_pixelStep = 1.0f / pixelsPerStep;
143 }
144
updateThumb()145 void Scrollbar::updateThumb()
146 {
147 #ifdef THUMB_POSITION_AFFECTS_BUTTONS
148 invalidate();
149 #else
150 theme()->invalidateParts(this, ForwardTrackPart | BackTrackPart | ThumbPart);
151 #endif
152 }
153
updateThumbPosition()154 void Scrollbar::updateThumbPosition()
155 {
156 updateThumb();
157 }
158
updateThumbProportion()159 void Scrollbar::updateThumbProportion()
160 {
161 updateThumb();
162 }
163
paint(GraphicsContext * context,const IntRect & damageRect)164 void Scrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
165 {
166 if (context->updatingControlTints() && theme()->supportsControlTints()) {
167 invalidate();
168 return;
169 }
170
171 if (context->paintingDisabled() || !frameRect().intersects(damageRect))
172 return;
173
174 if (!theme()->paint(this, context, damageRect))
175 Widget::paint(context, damageRect);
176 }
177
autoscrollTimerFired(Timer<Scrollbar> *)178 void Scrollbar::autoscrollTimerFired(Timer<Scrollbar>*)
179 {
180 autoscrollPressedPart(theme()->autoscrollTimerDelay());
181 }
182
thumbUnderMouse(Scrollbar * scrollbar)183 static bool thumbUnderMouse(Scrollbar* scrollbar)
184 {
185 int thumbPos = scrollbar->theme()->trackPosition(scrollbar) + scrollbar->theme()->thumbPosition(scrollbar);
186 int thumbLength = scrollbar->theme()->thumbLength(scrollbar);
187 return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength;
188 }
189
autoscrollPressedPart(double delay)190 void Scrollbar::autoscrollPressedPart(double delay)
191 {
192 // Don't do anything for the thumb or if nothing was pressed.
193 if (m_pressedPart == ThumbPart || m_pressedPart == NoPart)
194 return;
195
196 // Handle the track.
197 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
198 theme()->invalidatePart(this, m_pressedPart);
199 setHoveredPart(ThumbPart);
200 return;
201 }
202
203 // Handle the arrows and track.
204 if (m_scrollableArea && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity()))
205 startTimerIfNeeded(delay);
206 }
207
startTimerIfNeeded(double delay)208 void Scrollbar::startTimerIfNeeded(double delay)
209 {
210 // Don't do anything for the thumb.
211 if (m_pressedPart == ThumbPart)
212 return;
213
214 // Handle the track. We halt track scrolling once the thumb is level
215 // with us.
216 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
217 theme()->invalidatePart(this, m_pressedPart);
218 setHoveredPart(ThumbPart);
219 return;
220 }
221
222 // We can't scroll if we've hit the beginning or end.
223 ScrollDirection dir = pressedPartScrollDirection();
224 if (dir == ScrollUp || dir == ScrollLeft) {
225 if (m_currentPos == 0)
226 return;
227 } else {
228 if (m_currentPos == maximum())
229 return;
230 }
231
232 m_scrollTimer.startOneShot(delay);
233 }
234
stopTimerIfNeeded()235 void Scrollbar::stopTimerIfNeeded()
236 {
237 if (m_scrollTimer.isActive())
238 m_scrollTimer.stop();
239 }
240
pressedPartScrollDirection()241 ScrollDirection Scrollbar::pressedPartScrollDirection()
242 {
243 if (m_orientation == HorizontalScrollbar) {
244 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
245 return ScrollLeft;
246 return ScrollRight;
247 } else {
248 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
249 return ScrollUp;
250 return ScrollDown;
251 }
252 }
253
pressedPartScrollGranularity()254 ScrollGranularity Scrollbar::pressedPartScrollGranularity()
255 {
256 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart)
257 return ScrollByLine;
258 return ScrollByPage;
259 }
260
moveThumb(int pos,bool draggingDocument)261 void Scrollbar::moveThumb(int pos, bool draggingDocument)
262 {
263 if (!m_scrollableArea)
264 return;
265
266 int delta = pos - m_pressedPos;
267
268 if (draggingDocument) {
269 if (m_draggingDocument)
270 delta = pos - m_documentDragPos;
271 m_draggingDocument = true;
272 FloatPoint currentPosition = m_scrollableArea->scrollAnimator()->currentPosition();
273 int destinationPosition = (m_orientation == HorizontalScrollbar ? currentPosition.x() : currentPosition.y()) + delta;
274 if (delta > 0)
275 destinationPosition = min(destinationPosition + delta, maximum());
276 else if (delta < 0)
277 destinationPosition = max(destinationPosition + delta, 0);
278 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, destinationPosition);
279 m_documentDragPos = pos;
280 return;
281 }
282
283 if (m_draggingDocument) {
284 delta += m_pressedPos - m_documentDragPos;
285 m_draggingDocument = false;
286 }
287
288 // Drag the thumb.
289 int thumbPos = theme()->thumbPosition(this);
290 int thumbLen = theme()->thumbLength(this);
291 int trackLen = theme()->trackLength(this);
292 int maxPos = trackLen - thumbLen;
293 if (delta > 0)
294 delta = min(maxPos - thumbPos, delta);
295 else if (delta < 0)
296 delta = max(-thumbPos, delta);
297
298 if (delta) {
299 float newPosition = static_cast<float>(thumbPos + delta) * maximum() / (trackLen - thumbLen);
300 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, newPosition);
301 }
302 }
303
setHoveredPart(ScrollbarPart part)304 void Scrollbar::setHoveredPart(ScrollbarPart part)
305 {
306 if (part == m_hoveredPart)
307 return;
308
309 if ((m_hoveredPart == NoPart || part == NoPart) && theme()->invalidateOnMouseEnterExit())
310 invalidate(); // Just invalidate the whole scrollbar, since the buttons at either end change anyway.
311 else if (m_pressedPart == NoPart) { // When there's a pressed part, we don't draw a hovered state, so there's no reason to invalidate.
312 theme()->invalidatePart(this, part);
313 theme()->invalidatePart(this, m_hoveredPart);
314 }
315 m_hoveredPart = part;
316 }
317
setPressedPart(ScrollbarPart part)318 void Scrollbar::setPressedPart(ScrollbarPart part)
319 {
320 if (m_pressedPart != NoPart)
321 theme()->invalidatePart(this, m_pressedPart);
322 m_pressedPart = part;
323 if (m_pressedPart != NoPart)
324 theme()->invalidatePart(this, m_pressedPart);
325 else if (m_hoveredPart != NoPart) // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part.
326 theme()->invalidatePart(this, m_hoveredPart);
327 }
328
mouseMoved(const PlatformMouseEvent & evt)329 bool Scrollbar::mouseMoved(const PlatformMouseEvent& evt)
330 {
331 if (m_pressedPart == ThumbPart) {
332 if (theme()->shouldSnapBackToDragOrigin(this, evt)) {
333 if (m_scrollableArea)
334 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin);
335 } else {
336 moveThumb(m_orientation == HorizontalScrollbar ?
337 convertFromContainingWindow(evt.pos()).x() :
338 convertFromContainingWindow(evt.pos()).y(), theme()->shouldDragDocumentInsteadOfThumb(this, evt));
339 }
340 return true;
341 }
342
343 if (m_pressedPart != NoPart)
344 m_pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.pos()).x() : convertFromContainingWindow(evt.pos()).y());
345
346 ScrollbarPart part = theme()->hitTest(this, evt);
347 if (part != m_hoveredPart) {
348 if (m_pressedPart != NoPart) {
349 if (part == m_pressedPart) {
350 // The mouse is moving back over the pressed part. We
351 // need to start up the timer action again.
352 startTimerIfNeeded(theme()->autoscrollTimerDelay());
353 theme()->invalidatePart(this, m_pressedPart);
354 } else if (m_hoveredPart == m_pressedPart) {
355 // The mouse is leaving the pressed part. Kill our timer
356 // if needed.
357 stopTimerIfNeeded();
358 theme()->invalidatePart(this, m_pressedPart);
359 }
360 }
361
362 setHoveredPart(part);
363 }
364
365 return true;
366 }
367
mouseExited()368 bool Scrollbar::mouseExited()
369 {
370 setHoveredPart(NoPart);
371 return true;
372 }
373
mouseUp()374 bool Scrollbar::mouseUp()
375 {
376 setPressedPart(NoPart);
377 m_pressedPos = 0;
378 m_draggingDocument = false;
379 stopTimerIfNeeded();
380
381 if (parent() && parent()->isFrameView())
382 static_cast<FrameView*>(parent())->frame()->eventHandler()->setMousePressed(false);
383
384 return true;
385 }
386
mouseDown(const PlatformMouseEvent & evt)387 bool Scrollbar::mouseDown(const PlatformMouseEvent& evt)
388 {
389 // Early exit for right click
390 if (evt.button() == RightButton)
391 return true; // FIXME: Handled as context menu by Qt right now. Should just avoid even calling this method on a right click though.
392
393 setPressedPart(theme()->hitTest(this, evt));
394 int pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.pos()).x() : convertFromContainingWindow(evt.pos()).y());
395
396 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && theme()->shouldCenterOnThumb(this, evt)) {
397 setHoveredPart(ThumbPart);
398 setPressedPart(ThumbPart);
399 m_dragOrigin = m_currentPos;
400 int thumbLen = theme()->thumbLength(this);
401 int desiredPos = pressedPos;
402 // Set the pressed position to the middle of the thumb so that when we do the move, the delta
403 // will be from the current pixel position of the thumb to the new desired position for the thumb.
404 m_pressedPos = theme()->trackPosition(this) + theme()->thumbPosition(this) + thumbLen / 2;
405 moveThumb(desiredPos);
406 return true;
407 } else if (m_pressedPart == ThumbPart)
408 m_dragOrigin = m_currentPos;
409
410 m_pressedPos = pressedPos;
411
412 autoscrollPressedPart(theme()->initialAutoscrollTimerDelay());
413 return true;
414 }
415
setFrameRect(const IntRect & rect)416 void Scrollbar::setFrameRect(const IntRect& rect)
417 {
418 // Get our window resizer rect and see if we overlap. Adjust to avoid the overlap
419 // if necessary.
420 IntRect adjustedRect(rect);
421 bool overlapsResizer = false;
422 ScrollView* view = parent();
423 if (view && !rect.isEmpty() && !view->windowResizerRect().isEmpty()) {
424 IntRect resizerRect = view->convertFromContainingWindow(view->windowResizerRect());
425 if (rect.intersects(resizerRect)) {
426 if (orientation() == HorizontalScrollbar) {
427 int overlap = rect.maxX() - resizerRect.x();
428 if (overlap > 0 && resizerRect.maxX() >= rect.maxX()) {
429 adjustedRect.setWidth(rect.width() - overlap);
430 overlapsResizer = true;
431 }
432 } else {
433 int overlap = rect.maxY() - resizerRect.y();
434 if (overlap > 0 && resizerRect.maxY() >= rect.maxY()) {
435 adjustedRect.setHeight(rect.height() - overlap);
436 overlapsResizer = true;
437 }
438 }
439 }
440 }
441 if (overlapsResizer != m_overlapsResizer) {
442 m_overlapsResizer = overlapsResizer;
443 if (view)
444 view->adjustScrollbarsAvoidingResizerCount(m_overlapsResizer ? 1 : -1);
445 }
446
447 Widget::setFrameRect(adjustedRect);
448 }
449
setParent(ScrollView * parentView)450 void Scrollbar::setParent(ScrollView* parentView)
451 {
452 if (!parentView && m_overlapsResizer && parent())
453 parent()->adjustScrollbarsAvoidingResizerCount(-1);
454 Widget::setParent(parentView);
455 }
456
setEnabled(bool e)457 void Scrollbar::setEnabled(bool e)
458 {
459 if (m_enabled == e)
460 return;
461 m_enabled = e;
462 invalidate();
463 }
464
isOverlayScrollbar() const465 bool Scrollbar::isOverlayScrollbar() const
466 {
467 return m_theme->usesOverlayScrollbars();
468 }
469
isWindowActive() const470 bool Scrollbar::isWindowActive() const
471 {
472 return m_scrollableArea && m_scrollableArea->isActive();
473 }
474
axObjectCache() const475 AXObjectCache* Scrollbar::axObjectCache() const
476 {
477 if (!parent() || !parent()->isFrameView())
478 return 0;
479
480 // FIXME: Accessing the FrameView and Document here is a layering violation
481 // and should be removed.
482 Document* document = static_cast<FrameView*>(parent())->frame()->document();
483 return document->axObjectCache();
484 }
485
invalidateRect(const IntRect & rect)486 void Scrollbar::invalidateRect(const IntRect& rect)
487 {
488 if (suppressInvalidation())
489 return;
490
491 if (m_scrollableArea)
492 m_scrollableArea->invalidateScrollbar(this, rect);
493 }
494
convertToContainingView(const IntRect & localRect) const495 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
496 {
497 if (m_scrollableArea)
498 return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
499
500 return Widget::convertToContainingView(localRect);
501 }
502
convertFromContainingView(const IntRect & parentRect) const503 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
504 {
505 if (m_scrollableArea)
506 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
507
508 return Widget::convertFromContainingView(parentRect);
509 }
510
convertToContainingView(const IntPoint & localPoint) const511 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
512 {
513 if (m_scrollableArea)
514 return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
515
516 return Widget::convertToContainingView(localPoint);
517 }
518
convertFromContainingView(const IntPoint & parentPoint) const519 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
520 {
521 if (m_scrollableArea)
522 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
523
524 return Widget::convertFromContainingView(parentPoint);
525 }
526
527 } // namespace WebCore
528