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"
40 #if ((OS(POSIX) && !OS(MACOSX)) || OS(WIN))
41 // The position of the scrollbar thumb affects the appearance of the steppers, so
42 // when the thumb moves, we have to invalidate them for painting.
43 #define THUMB_POSITION_AFFECTS_BUTTONS
48 PassRefPtr<Scrollbar> Scrollbar::create(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
50 return adoptRef(new Scrollbar(scrollableArea, orientation, size));
53 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, ScrollbarTheme* theme)
54 : m_scrollableArea(scrollableArea)
55 , m_orientation(orientation)
56 , m_controlSize(controlSize)
62 , m_hoveredPart(NoPart)
63 , m_pressedPart(NoPart)
66 , m_draggingDocument(false)
67 , m_documentDragPos(0)
69 , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
70 , m_overlapsResizer(false)
71 , m_suppressInvalidation(false)
72 , m_isAlphaLocked(false)
75 m_theme = ScrollbarTheme::theme();
77 m_theme->registerScrollbar(this);
79 // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
80 // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
81 // alone when sizing).
82 int thickness = m_theme->scrollbarThickness(controlSize);
83 Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
85 m_currentPos = scrollableAreaCurrentPos();
88 Scrollbar::~Scrollbar()
92 m_theme->unregisterScrollbar(this);
95 void Scrollbar::removeFromParent()
98 toScrollView(parent())->removeChild(this);
101 ScrollView* Scrollbar::parentScrollView() const
103 return toScrollView(parent());
106 ScrollView* Scrollbar::rootScrollView() const
108 return toScrollView(root());
111 ScrollbarOverlayStyle Scrollbar::scrollbarOverlayStyle() const
113 return m_scrollableArea ? m_scrollableArea->scrollbarOverlayStyle() : ScrollbarOverlayStyleDefault;
116 void Scrollbar::getTickmarks(Vector<IntRect>& tickmarks) const
118 if (m_scrollableArea)
119 m_scrollableArea->getTickmarks(tickmarks);
122 bool Scrollbar::isScrollableAreaActive() const
124 return m_scrollableArea && m_scrollableArea->isActive();
127 bool Scrollbar::isScrollViewScrollbar() const
129 return parent() && parent()->isFrameView() && toScrollView(parent())->isScrollViewScrollbar(this);
132 bool Scrollbar::isLeftSideVerticalScrollbar() const
134 if (m_orientation == VerticalScrollbar && m_scrollableArea)
135 return m_scrollableArea->shouldPlaceVerticalScrollbarOnLeft();
139 void Scrollbar::offsetDidChange()
141 ASSERT(m_scrollableArea);
143 float position = scrollableAreaCurrentPos();
144 if (position == m_currentPos)
147 int oldThumbPosition = theme()->thumbPosition(this);
148 m_currentPos = position;
149 updateThumbPosition();
150 if (m_pressedPart == ThumbPart)
151 setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
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 = min(trackLen - thumbLen - thumbPos, delta);
307 delta = 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::setFrameRect(const IntRect& rect)
483 // Get our window resizer rect and see if we overlap. Adjust to avoid the overlap
485 IntRect adjustedRect(rect);
486 bool overlapsResizer = false;
487 ScrollView* view = parentScrollView();
488 if (view && !rect.isEmpty() && !view->windowResizerRect().isEmpty()) {
489 IntRect resizerRect = view->convertFromContainingWindow(view->windowResizerRect());
490 if (rect.intersects(resizerRect)) {
491 if (orientation() == HorizontalScrollbar) {
492 int overlap = rect.maxX() - resizerRect.x();
493 if (overlap > 0 && resizerRect.maxX() >= rect.maxX()) {
494 adjustedRect.setWidth(rect.width() - overlap);
495 overlapsResizer = true;
498 int overlap = rect.maxY() - resizerRect.y();
499 if (overlap > 0 && resizerRect.maxY() >= rect.maxY()) {
500 adjustedRect.setHeight(rect.height() - overlap);
501 overlapsResizer = true;
506 if (overlapsResizer != m_overlapsResizer) {
507 m_overlapsResizer = overlapsResizer;
509 view->adjustScrollbarsAvoidingResizerCount(m_overlapsResizer ? 1 : -1);
512 Widget::setFrameRect(adjustedRect);
515 void Scrollbar::setParent(Widget* parentView)
517 if (!parentView && m_overlapsResizer && parentScrollView())
518 parentScrollView()->adjustScrollbarsAvoidingResizerCount(-1);
519 Widget::setParent(parentView);
522 void Scrollbar::setEnabled(bool e)
527 theme()->updateEnabledState(this);
531 bool Scrollbar::isOverlayScrollbar() const
533 return m_theme->usesOverlayScrollbars();
536 bool Scrollbar::shouldParticipateInHitTesting()
538 // Non-overlay scrollbars should always participate in hit testing.
539 if (!isOverlayScrollbar())
541 return m_scrollableArea->scrollAnimator()->shouldScrollbarParticipateInHitTesting(this);
544 bool Scrollbar::isWindowActive() const
546 return m_scrollableArea && m_scrollableArea->isActive();
549 void Scrollbar::invalidateRect(const IntRect& rect)
551 if (suppressInvalidation())
554 if (m_scrollableArea)
555 m_scrollableArea->invalidateScrollbar(this, rect);
558 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
560 if (m_scrollableArea)
561 return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
563 return Widget::convertToContainingView(localRect);
566 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
568 if (m_scrollableArea)
569 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
571 return Widget::convertFromContainingView(parentRect);
574 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
576 if (m_scrollableArea)
577 return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
579 return Widget::convertToContainingView(localPoint);
582 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
584 if (m_scrollableArea)
585 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
587 return Widget::convertFromContainingView(parentPoint);
590 float Scrollbar::scrollableAreaCurrentPos() const
592 if (!m_scrollableArea)
595 if (m_orientation == HorizontalScrollbar)
596 return m_scrollableArea->scrollPosition().x() - m_scrollableArea->minimumScrollPosition().x();
598 return m_scrollableArea->scrollPosition().y() - m_scrollableArea->minimumScrollPosition().y();