2 * Copyright (C) 2004, 2006, 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 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.
27 #include "platform/scroll/Scrollbar.h"
30 #include "platform/graphics/GraphicsContext.h"
31 #include "platform/PlatformGestureEvent.h"
32 #include "platform/PlatformMouseEvent.h"
33 #include "platform/scroll/ScrollAnimator.h"
34 #include "platform/scroll/ScrollableArea.h"
35 #include "platform/scroll/ScrollbarTheme.h"
37 #if ((OS(POSIX) && !OS(MACOSX)) || OS(WIN))
38 // The position of the scrollbar thumb affects the appearance of the steppers, so
39 // when the thumb moves, we have to invalidate them for painting.
40 #define THUMB_POSITION_AFFECTS_BUTTONS
45 PassRefPtrWillBeRawPtr<Scrollbar> Scrollbar::create(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
47 return adoptRefWillBeNoop(new Scrollbar(scrollableArea, orientation, size));
50 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, ScrollbarTheme* theme)
51 : m_scrollableArea(scrollableArea)
52 , m_orientation(orientation)
53 , m_controlSize(controlSize)
59 , m_hoveredPart(NoPart)
60 , m_pressedPart(NoPart)
63 , m_draggingDocument(false)
64 , m_documentDragPos(0)
66 , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
67 , m_overlapsResizer(false)
68 , m_suppressInvalidation(false)
69 , m_isAlphaLocked(false)
72 m_theme = ScrollbarTheme::theme();
74 m_theme->registerScrollbar(this);
76 // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
77 // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
78 // alone when sizing).
79 int thickness = m_theme->scrollbarThickness(controlSize);
80 Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
82 m_currentPos = scrollableAreaCurrentPos();
86 m_animator = m_scrollableArea->scrollAnimator();
90 Scrollbar::~Scrollbar()
94 m_theme->unregisterScrollbar(this);
100 ASSERT(m_scrollableArea);
101 if (m_orientation == VerticalScrollbar)
102 m_animator->willRemoveVerticalScrollbar(this);
104 m_animator->willRemoveHorizontalScrollbar(this);
108 ScrollbarOverlayStyle Scrollbar::scrollbarOverlayStyle() const
110 return m_scrollableArea ? m_scrollableArea->scrollbarOverlayStyle() : ScrollbarOverlayStyleDefault;
113 void Scrollbar::getTickmarks(Vector<IntRect>& tickmarks) const
115 if (m_scrollableArea)
116 m_scrollableArea->getTickmarks(tickmarks);
119 bool Scrollbar::isScrollableAreaActive() const
121 return m_scrollableArea && m_scrollableArea->isActive();
124 bool Scrollbar::isLeftSideVerticalScrollbar() const
126 if (m_orientation == VerticalScrollbar && m_scrollableArea)
127 return m_scrollableArea->shouldPlaceVerticalScrollbarOnLeft();
131 void Scrollbar::offsetDidChange()
133 ASSERT(m_scrollableArea);
135 float position = scrollableAreaCurrentPos();
136 if (position == m_currentPos)
139 int oldThumbPosition = theme()->thumbPosition(this);
140 m_currentPos = position;
141 updateThumbPosition();
142 if (m_pressedPart == ThumbPart)
143 setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
146 void Scrollbar::disconnectFromScrollableArea()
148 m_scrollableArea = nullptr;
150 m_animator = nullptr;
154 void Scrollbar::setProportion(int visibleSize, int totalSize)
156 if (visibleSize == m_visibleSize && totalSize == m_totalSize)
159 m_visibleSize = visibleSize;
160 m_totalSize = totalSize;
162 updateThumbProportion();
165 void Scrollbar::updateThumb()
167 #ifdef THUMB_POSITION_AFFECTS_BUTTONS
170 theme()->invalidateParts(this, ForwardTrackPart | BackTrackPart | ThumbPart);
174 void Scrollbar::updateThumbPosition()
179 void Scrollbar::updateThumbProportion()
184 void Scrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
186 if (!frameRect().intersects(damageRect))
189 if (!theme()->paint(this, context, damageRect))
190 Widget::paint(context, damageRect);
193 void Scrollbar::autoscrollTimerFired(Timer<Scrollbar>*)
195 autoscrollPressedPart(theme()->autoscrollTimerDelay());
198 static bool thumbUnderMouse(Scrollbar* scrollbar)
200 int thumbPos = scrollbar->theme()->trackPosition(scrollbar) + scrollbar->theme()->thumbPosition(scrollbar);
201 int thumbLength = scrollbar->theme()->thumbLength(scrollbar);
202 return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength;
205 void Scrollbar::autoscrollPressedPart(double delay)
207 // Don't do anything for the thumb or if nothing was pressed.
208 if (m_pressedPart == ThumbPart || m_pressedPart == NoPart)
212 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
213 theme()->invalidatePart(this, m_pressedPart);
214 setHoveredPart(ThumbPart);
218 // Handle the arrows and track.
219 if (m_scrollableArea && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity()))
220 startTimerIfNeeded(delay);
223 void Scrollbar::startTimerIfNeeded(double delay)
225 // Don't do anything for the thumb.
226 if (m_pressedPart == ThumbPart)
229 // Handle the track. We halt track scrolling once the thumb is level
231 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
232 theme()->invalidatePart(this, m_pressedPart);
233 setHoveredPart(ThumbPart);
237 // We can't scroll if we've hit the beginning or end.
238 ScrollDirection dir = pressedPartScrollDirection();
239 if (dir == ScrollUp || dir == ScrollLeft) {
240 if (m_currentPos == 0)
243 if (m_currentPos == maximum())
247 m_scrollTimer.startOneShot(delay, FROM_HERE);
250 void Scrollbar::stopTimerIfNeeded()
252 if (m_scrollTimer.isActive())
253 m_scrollTimer.stop();
256 ScrollDirection Scrollbar::pressedPartScrollDirection()
258 if (m_orientation == HorizontalScrollbar) {
259 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
263 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
269 ScrollGranularity Scrollbar::pressedPartScrollGranularity()
271 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart)
276 void Scrollbar::moveThumb(int pos, bool draggingDocument)
278 if (!m_scrollableArea)
281 int delta = pos - m_pressedPos;
283 if (draggingDocument) {
284 if (m_draggingDocument)
285 delta = pos - m_documentDragPos;
286 m_draggingDocument = true;
287 FloatPoint currentPosition = m_scrollableArea->scrollAnimator()->currentPosition();
288 float destinationPosition = (m_orientation == HorizontalScrollbar ? currentPosition.x() : currentPosition.y()) + delta;
289 destinationPosition = m_scrollableArea->clampScrollPosition(m_orientation, destinationPosition);
290 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, destinationPosition);
291 m_documentDragPos = pos;
295 if (m_draggingDocument) {
296 delta += m_pressedPos - m_documentDragPos;
297 m_draggingDocument = false;
301 int thumbPos = theme()->thumbPosition(this);
302 int thumbLen = theme()->thumbLength(this);
303 int trackLen = theme()->trackLength(this);
305 delta = std::min(trackLen - thumbLen - thumbPos, delta);
307 delta = std::max(-thumbPos, delta);
309 float minPos = m_scrollableArea->minimumScrollPosition(m_orientation);
310 float maxPos = m_scrollableArea->maximumScrollPosition(m_orientation);
312 float newPosition = static_cast<float>(thumbPos + delta) * (maxPos - minPos) / (trackLen - thumbLen) + minPos;
313 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, newPosition);
317 void Scrollbar::setHoveredPart(ScrollbarPart part)
319 if (part == m_hoveredPart)
322 if ((m_hoveredPart == NoPart || part == NoPart) && theme()->invalidateOnMouseEnterExit())
323 invalidate(); // Just invalidate the whole scrollbar, since the buttons at either end change anyway.
324 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.
325 theme()->invalidatePart(this, part);
326 theme()->invalidatePart(this, m_hoveredPart);
328 m_hoveredPart = part;
331 void Scrollbar::setPressedPart(ScrollbarPart part)
333 if (m_pressedPart != NoPart)
334 theme()->invalidatePart(this, m_pressedPart);
335 m_pressedPart = part;
336 if (m_pressedPart != NoPart)
337 theme()->invalidatePart(this, m_pressedPart);
338 else if (m_hoveredPart != NoPart) // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part.
339 theme()->invalidatePart(this, m_hoveredPart);
342 bool Scrollbar::gestureEvent(const PlatformGestureEvent& evt)
344 switch (evt.type()) {
345 case PlatformEvent::GestureTapDown:
346 setPressedPart(theme()->hitTest(this, evt.position()));
347 m_pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
349 case PlatformEvent::GestureTapDownCancel:
350 case PlatformEvent::GestureScrollBegin:
351 if (m_pressedPart != ThumbPart)
353 m_scrollPos = m_pressedPos;
355 case PlatformEvent::GestureScrollUpdate:
356 case PlatformEvent::GestureScrollUpdateWithoutPropagation:
357 if (m_pressedPart != ThumbPart)
359 m_scrollPos += orientation() == HorizontalScrollbar ? evt.deltaX() : evt.deltaY();
360 moveThumb(m_scrollPos, false);
362 case PlatformEvent::GestureScrollEnd:
363 case PlatformEvent::GestureLongPress:
364 case PlatformEvent::GestureFlingStart:
367 setPressedPart(NoPart);
369 case PlatformEvent::GestureTap: {
370 if (m_pressedPart != ThumbPart && m_pressedPart != NoPart && m_scrollableArea
371 && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity())) {
376 setPressedPart(NoPart);
380 // By default, we assume that gestures don't deselect the scrollbar.
385 void Scrollbar::mouseMoved(const PlatformMouseEvent& evt)
387 if (m_pressedPart == ThumbPart) {
388 if (theme()->shouldSnapBackToDragOrigin(this, evt)) {
389 if (m_scrollableArea)
390 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin + m_scrollableArea->minimumScrollPosition(m_orientation));
392 moveThumb(m_orientation == HorizontalScrollbar ?
393 convertFromContainingWindow(evt.position()).x() :
394 convertFromContainingWindow(evt.position()).y(), theme()->shouldDragDocumentInsteadOfThumb(this, evt));
399 if (m_pressedPart != NoPart)
400 m_pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
402 ScrollbarPart part = theme()->hitTest(this, evt.position());
403 if (part != m_hoveredPart) {
404 if (m_pressedPart != NoPart) {
405 if (part == m_pressedPart) {
406 // The mouse is moving back over the pressed part. We
407 // need to start up the timer action again.
408 startTimerIfNeeded(theme()->autoscrollTimerDelay());
409 theme()->invalidatePart(this, m_pressedPart);
410 } else if (m_hoveredPart == m_pressedPart) {
411 // The mouse is leaving the pressed part. Kill our timer
414 theme()->invalidatePart(this, m_pressedPart);
418 setHoveredPart(part);
424 void Scrollbar::mouseEntered()
426 if (m_scrollableArea)
427 m_scrollableArea->mouseEnteredScrollbar(this);
430 void Scrollbar::mouseExited()
432 if (m_scrollableArea)
433 m_scrollableArea->mouseExitedScrollbar(this);
434 setHoveredPart(NoPart);
437 void Scrollbar::mouseUp(const PlatformMouseEvent& mouseEvent)
439 setPressedPart(NoPart);
441 m_draggingDocument = false;
444 if (m_scrollableArea) {
445 // m_hoveredPart won't be updated until the next mouseMoved or mouseDown, so we have to hit test
446 // to really know if the mouse has exited the scrollbar on a mouseUp.
447 ScrollbarPart part = theme()->hitTest(this, mouseEvent.position());
449 m_scrollableArea->mouseExitedScrollbar(this);
453 void Scrollbar::mouseDown(const PlatformMouseEvent& evt)
455 // Early exit for right click
456 if (evt.button() == RightButton)
459 setPressedPart(theme()->hitTest(this, evt.position()));
460 int pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
462 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && theme()->shouldCenterOnThumb(this, evt)) {
463 setHoveredPart(ThumbPart);
464 setPressedPart(ThumbPart);
465 m_dragOrigin = m_currentPos;
466 int thumbLen = theme()->thumbLength(this);
467 int desiredPos = pressedPos;
468 // Set the pressed position to the middle of the thumb so that when we do the move, the delta
469 // will be from the current pixel position of the thumb to the new desired position for the thumb.
470 m_pressedPos = theme()->trackPosition(this) + theme()->thumbPosition(this) + thumbLen / 2;
471 moveThumb(desiredPos);
473 } else if (m_pressedPart == ThumbPart)
474 m_dragOrigin = m_currentPos;
476 m_pressedPos = pressedPos;
478 autoscrollPressedPart(theme()->initialAutoscrollTimerDelay());
481 void Scrollbar::setEnabled(bool e)
486 theme()->updateEnabledState(this);
490 bool Scrollbar::isOverlayScrollbar() const
492 return m_theme->usesOverlayScrollbars();
495 bool Scrollbar::shouldParticipateInHitTesting()
497 // Non-overlay scrollbars should always participate in hit testing.
498 if (!isOverlayScrollbar())
500 return m_scrollableArea->scrollAnimator()->shouldScrollbarParticipateInHitTesting(this);
503 bool Scrollbar::isWindowActive() const
505 return m_scrollableArea && m_scrollableArea->isActive();
508 void Scrollbar::invalidateRect(const IntRect& rect)
510 if (suppressInvalidation())
513 if (m_scrollableArea)
514 m_scrollableArea->invalidateScrollbar(this, rect);
517 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
519 if (m_scrollableArea)
520 return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
522 return Widget::convertToContainingView(localRect);
525 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
527 if (m_scrollableArea)
528 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
530 return Widget::convertFromContainingView(parentRect);
533 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
535 if (m_scrollableArea)
536 return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
538 return Widget::convertToContainingView(localPoint);
541 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
543 if (m_scrollableArea)
544 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
546 return Widget::convertFromContainingView(parentPoint);
549 float Scrollbar::scrollableAreaCurrentPos() const
551 if (!m_scrollableArea)
554 if (m_orientation == HorizontalScrollbar)
555 return m_scrollableArea->scrollPosition().x() - m_scrollableArea->minimumScrollPosition().x();
557 return m_scrollableArea->scrollPosition().y() - m_scrollableArea->minimumScrollPosition().y();