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/ScrollView.h"
35 #include "platform/scroll/ScrollableArea.h"
36 #include "platform/scroll/ScrollbarTheme.h"
38 #if ((OS(POSIX) && !OS(MACOSX)) || OS(WIN))
39 // The position of the scrollbar thumb affects the appearance of the steppers, so
40 // when the thumb moves, we have to invalidate them for painting.
41 #define THUMB_POSITION_AFFECTS_BUTTONS
46 PassRefPtr<Scrollbar> Scrollbar::create(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
48 return adoptRef(new Scrollbar(scrollableArea, orientation, size));
51 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, ScrollbarTheme* theme)
52 : m_scrollableArea(scrollableArea)
53 , m_orientation(orientation)
54 , m_controlSize(controlSize)
60 , m_hoveredPart(NoPart)
61 , m_pressedPart(NoPart)
64 , m_draggingDocument(false)
65 , m_documentDragPos(0)
67 , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
68 , m_overlapsResizer(false)
69 , m_suppressInvalidation(false)
70 , m_isAlphaLocked(false)
73 m_theme = ScrollbarTheme::theme();
75 m_theme->registerScrollbar(this);
77 // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
78 // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
79 // alone when sizing).
80 int thickness = m_theme->scrollbarThickness(controlSize);
81 Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
83 m_currentPos = scrollableAreaCurrentPos();
86 Scrollbar::~Scrollbar()
90 m_theme->unregisterScrollbar(this);
93 void Scrollbar::removeFromParent()
96 toScrollView(parent())->removeChild(this);
99 ScrollView* Scrollbar::parentScrollView() const
101 return parent() && parent()->isScrollView() ? toScrollView(parent()) : 0;
104 ScrollbarOverlayStyle Scrollbar::scrollbarOverlayStyle() const
106 return m_scrollableArea ? m_scrollableArea->scrollbarOverlayStyle() : ScrollbarOverlayStyleDefault;
109 void Scrollbar::getTickmarks(Vector<IntRect>& tickmarks) const
111 if (m_scrollableArea)
112 m_scrollableArea->getTickmarks(tickmarks);
115 bool Scrollbar::isScrollableAreaActive() const
117 return m_scrollableArea && m_scrollableArea->isActive();
120 bool Scrollbar::isScrollViewScrollbar() const
122 return parent() && parent()->isFrameView() && toScrollView(parent())->isScrollViewScrollbar(this);
125 bool Scrollbar::isLeftSideVerticalScrollbar() const
127 if (m_orientation == VerticalScrollbar && m_scrollableArea)
128 return m_scrollableArea->shouldPlaceVerticalScrollbarOnLeft();
132 void Scrollbar::offsetDidChange()
134 ASSERT(m_scrollableArea);
136 float position = scrollableAreaCurrentPos();
137 if (position == m_currentPos)
140 int oldThumbPosition = theme()->thumbPosition(this);
141 m_currentPos = position;
142 updateThumbPosition();
143 if (m_pressedPart == ThumbPart)
144 setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
147 void Scrollbar::setProportion(int visibleSize, int totalSize)
149 if (visibleSize == m_visibleSize && totalSize == m_totalSize)
152 m_visibleSize = visibleSize;
153 m_totalSize = totalSize;
155 updateThumbProportion();
158 void Scrollbar::updateThumb()
160 #ifdef THUMB_POSITION_AFFECTS_BUTTONS
163 theme()->invalidateParts(this, ForwardTrackPart | BackTrackPart | ThumbPart);
167 void Scrollbar::updateThumbPosition()
172 void Scrollbar::updateThumbProportion()
177 void Scrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
179 if (!frameRect().intersects(damageRect))
182 if (!theme()->paint(this, context, damageRect))
183 Widget::paint(context, damageRect);
186 void Scrollbar::autoscrollTimerFired(Timer<Scrollbar>*)
188 autoscrollPressedPart(theme()->autoscrollTimerDelay());
191 static bool thumbUnderMouse(Scrollbar* scrollbar)
193 int thumbPos = scrollbar->theme()->trackPosition(scrollbar) + scrollbar->theme()->thumbPosition(scrollbar);
194 int thumbLength = scrollbar->theme()->thumbLength(scrollbar);
195 return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength;
198 void Scrollbar::autoscrollPressedPart(double delay)
200 // Don't do anything for the thumb or if nothing was pressed.
201 if (m_pressedPart == ThumbPart || m_pressedPart == NoPart)
205 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
206 theme()->invalidatePart(this, m_pressedPart);
207 setHoveredPart(ThumbPart);
211 // Handle the arrows and track.
212 if (m_scrollableArea && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity()))
213 startTimerIfNeeded(delay);
216 void Scrollbar::startTimerIfNeeded(double delay)
218 // Don't do anything for the thumb.
219 if (m_pressedPart == ThumbPart)
222 // Handle the track. We halt track scrolling once the thumb is level
224 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
225 theme()->invalidatePart(this, m_pressedPart);
226 setHoveredPart(ThumbPart);
230 // We can't scroll if we've hit the beginning or end.
231 ScrollDirection dir = pressedPartScrollDirection();
232 if (dir == ScrollUp || dir == ScrollLeft) {
233 if (m_currentPos == 0)
236 if (m_currentPos == maximum())
240 m_scrollTimer.startOneShot(delay, FROM_HERE);
243 void Scrollbar::stopTimerIfNeeded()
245 if (m_scrollTimer.isActive())
246 m_scrollTimer.stop();
249 ScrollDirection Scrollbar::pressedPartScrollDirection()
251 if (m_orientation == HorizontalScrollbar) {
252 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
256 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
262 ScrollGranularity Scrollbar::pressedPartScrollGranularity()
264 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart)
269 void Scrollbar::moveThumb(int pos, bool draggingDocument)
271 if (!m_scrollableArea)
274 int delta = pos - m_pressedPos;
276 if (draggingDocument) {
277 if (m_draggingDocument)
278 delta = pos - m_documentDragPos;
279 m_draggingDocument = true;
280 FloatPoint currentPosition = m_scrollableArea->scrollAnimator()->currentPosition();
281 float destinationPosition = (m_orientation == HorizontalScrollbar ? currentPosition.x() : currentPosition.y()) + delta;
282 destinationPosition = m_scrollableArea->clampScrollPosition(m_orientation, destinationPosition);
283 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, destinationPosition);
284 m_documentDragPos = pos;
288 if (m_draggingDocument) {
289 delta += m_pressedPos - m_documentDragPos;
290 m_draggingDocument = false;
294 int thumbPos = theme()->thumbPosition(this);
295 int thumbLen = theme()->thumbLength(this);
296 int trackLen = theme()->trackLength(this);
298 delta = std::min(trackLen - thumbLen - thumbPos, delta);
300 delta = std::max(-thumbPos, delta);
302 float minPos = m_scrollableArea->minimumScrollPosition(m_orientation);
303 float maxPos = m_scrollableArea->maximumScrollPosition(m_orientation);
305 float newPosition = static_cast<float>(thumbPos + delta) * (maxPos - minPos) / (trackLen - thumbLen) + minPos;
306 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, newPosition);
310 void Scrollbar::setHoveredPart(ScrollbarPart part)
312 if (part == m_hoveredPart)
315 if ((m_hoveredPart == NoPart || part == NoPart) && theme()->invalidateOnMouseEnterExit())
316 invalidate(); // Just invalidate the whole scrollbar, since the buttons at either end change anyway.
317 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.
318 theme()->invalidatePart(this, part);
319 theme()->invalidatePart(this, m_hoveredPart);
321 m_hoveredPart = part;
324 void Scrollbar::setPressedPart(ScrollbarPart part)
326 if (m_pressedPart != NoPart)
327 theme()->invalidatePart(this, m_pressedPart);
328 m_pressedPart = part;
329 if (m_pressedPart != NoPart)
330 theme()->invalidatePart(this, m_pressedPart);
331 else if (m_hoveredPart != NoPart) // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part.
332 theme()->invalidatePart(this, m_hoveredPart);
335 bool Scrollbar::gestureEvent(const PlatformGestureEvent& evt)
337 switch (evt.type()) {
338 case PlatformEvent::GestureTapDown:
339 setPressedPart(theme()->hitTest(this, evt.position()));
340 m_pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
342 case PlatformEvent::GestureTapDownCancel:
343 case PlatformEvent::GestureScrollBegin:
344 if (m_pressedPart != ThumbPart)
346 m_scrollPos = m_pressedPos;
348 case PlatformEvent::GestureScrollUpdate:
349 case PlatformEvent::GestureScrollUpdateWithoutPropagation:
350 if (m_pressedPart != ThumbPart)
352 m_scrollPos += orientation() == HorizontalScrollbar ? evt.deltaX() : evt.deltaY();
353 moveThumb(m_scrollPos, false);
355 case PlatformEvent::GestureScrollEnd:
356 case PlatformEvent::GestureLongPress:
357 case PlatformEvent::GestureFlingStart:
360 setPressedPart(NoPart);
362 case PlatformEvent::GestureTap: {
363 if (m_pressedPart != ThumbPart && m_pressedPart != NoPart && m_scrollableArea
364 && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity())) {
369 setPressedPart(NoPart);
373 // By default, we assume that gestures don't deselect the scrollbar.
378 void Scrollbar::mouseMoved(const PlatformMouseEvent& evt)
380 if (m_pressedPart == ThumbPart) {
381 if (theme()->shouldSnapBackToDragOrigin(this, evt)) {
382 if (m_scrollableArea)
383 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin + m_scrollableArea->minimumScrollPosition(m_orientation));
385 moveThumb(m_orientation == HorizontalScrollbar ?
386 convertFromContainingWindow(evt.position()).x() :
387 convertFromContainingWindow(evt.position()).y(), theme()->shouldDragDocumentInsteadOfThumb(this, evt));
392 if (m_pressedPart != NoPart)
393 m_pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
395 ScrollbarPart part = theme()->hitTest(this, evt.position());
396 if (part != m_hoveredPart) {
397 if (m_pressedPart != NoPart) {
398 if (part == m_pressedPart) {
399 // The mouse is moving back over the pressed part. We
400 // need to start up the timer action again.
401 startTimerIfNeeded(theme()->autoscrollTimerDelay());
402 theme()->invalidatePart(this, m_pressedPart);
403 } else if (m_hoveredPart == m_pressedPart) {
404 // The mouse is leaving the pressed part. Kill our timer
407 theme()->invalidatePart(this, m_pressedPart);
411 setHoveredPart(part);
417 void Scrollbar::mouseEntered()
419 if (m_scrollableArea)
420 m_scrollableArea->mouseEnteredScrollbar(this);
423 void Scrollbar::mouseExited()
425 if (m_scrollableArea)
426 m_scrollableArea->mouseExitedScrollbar(this);
427 setHoveredPart(NoPart);
430 void Scrollbar::mouseUp(const PlatformMouseEvent& mouseEvent)
432 setPressedPart(NoPart);
434 m_draggingDocument = false;
437 if (m_scrollableArea) {
438 // m_hoveredPart won't be updated until the next mouseMoved or mouseDown, so we have to hit test
439 // to really know if the mouse has exited the scrollbar on a mouseUp.
440 ScrollbarPart part = theme()->hitTest(this, mouseEvent.position());
442 m_scrollableArea->mouseExitedScrollbar(this);
446 void Scrollbar::mouseDown(const PlatformMouseEvent& evt)
448 // Early exit for right click
449 if (evt.button() == RightButton)
452 setPressedPart(theme()->hitTest(this, evt.position()));
453 int pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
455 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && theme()->shouldCenterOnThumb(this, evt)) {
456 setHoveredPart(ThumbPart);
457 setPressedPart(ThumbPart);
458 m_dragOrigin = m_currentPos;
459 int thumbLen = theme()->thumbLength(this);
460 int desiredPos = pressedPos;
461 // Set the pressed position to the middle of the thumb so that when we do the move, the delta
462 // will be from the current pixel position of the thumb to the new desired position for the thumb.
463 m_pressedPos = theme()->trackPosition(this) + theme()->thumbPosition(this) + thumbLen / 2;
464 moveThumb(desiredPos);
466 } else if (m_pressedPart == ThumbPart)
467 m_dragOrigin = m_currentPos;
469 m_pressedPos = pressedPos;
471 autoscrollPressedPart(theme()->initialAutoscrollTimerDelay());
474 void Scrollbar::setFrameRect(const IntRect& rect)
476 // Get our window resizer rect and see if we overlap. Adjust to avoid the overlap
478 IntRect adjustedRect(rect);
479 bool overlapsResizer = false;
480 ScrollView* view = parentScrollView();
481 if (view && !rect.isEmpty() && !view->windowResizerRect().isEmpty()) {
482 IntRect resizerRect = view->convertFromContainingWindow(view->windowResizerRect());
483 if (rect.intersects(resizerRect)) {
484 if (orientation() == HorizontalScrollbar) {
485 int overlap = rect.maxX() - resizerRect.x();
486 if (overlap > 0 && resizerRect.maxX() >= rect.maxX()) {
487 adjustedRect.setWidth(rect.width() - overlap);
488 overlapsResizer = true;
491 int overlap = rect.maxY() - resizerRect.y();
492 if (overlap > 0 && resizerRect.maxY() >= rect.maxY()) {
493 adjustedRect.setHeight(rect.height() - overlap);
494 overlapsResizer = true;
499 if (overlapsResizer != m_overlapsResizer) {
500 m_overlapsResizer = overlapsResizer;
502 view->adjustScrollbarsAvoidingResizerCount(m_overlapsResizer ? 1 : -1);
505 Widget::setFrameRect(adjustedRect);
508 void Scrollbar::setParent(Widget* parentView)
510 if (!parentView && m_overlapsResizer && parentScrollView())
511 parentScrollView()->adjustScrollbarsAvoidingResizerCount(-1);
512 Widget::setParent(parentView);
515 void Scrollbar::setEnabled(bool e)
520 theme()->updateEnabledState(this);
524 bool Scrollbar::isOverlayScrollbar() const
526 return m_theme->usesOverlayScrollbars();
529 bool Scrollbar::shouldParticipateInHitTesting()
531 // Non-overlay scrollbars should always participate in hit testing.
532 if (!isOverlayScrollbar())
534 return m_scrollableArea->scrollAnimator()->shouldScrollbarParticipateInHitTesting(this);
537 bool Scrollbar::isWindowActive() const
539 return m_scrollableArea && m_scrollableArea->isActive();
542 void Scrollbar::invalidateRect(const IntRect& rect)
544 if (suppressInvalidation())
547 if (m_scrollableArea)
548 m_scrollableArea->invalidateScrollbar(this, rect);
551 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
553 if (m_scrollableArea)
554 return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
556 return Widget::convertToContainingView(localRect);
559 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
561 if (m_scrollableArea)
562 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
564 return Widget::convertFromContainingView(parentRect);
567 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
569 if (m_scrollableArea)
570 return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
572 return Widget::convertToContainingView(localPoint);
575 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
577 if (m_scrollableArea)
578 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
580 return Widget::convertFromContainingView(parentPoint);
583 float Scrollbar::scrollableAreaCurrentPos() const
585 if (!m_scrollableArea)
588 if (m_orientation == HorizontalScrollbar)
589 return m_scrollableArea->scrollPosition().x() - m_scrollableArea->minimumScrollPosition().x();
591 return m_scrollableArea->scrollPosition().y() - m_scrollableArea->minimumScrollPosition().y();