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 "Scrollbar.h"
29 #include "GraphicsContext.h"
30 #include "PlatformMouseEvent.h"
31 #include "ScrollAnimator.h"
32 #include "ScrollableArea.h"
33 #include "ScrollbarTheme.h"
36 // FIXME: The following #includes are a layering violation and should be removed.
37 #include "AXObjectCache.h"
38 #include "AccessibilityScrollbar.h"
40 #include "EventHandler.h"
42 #include "FrameView.h"
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
55 PassRefPtr<Scrollbar> Scrollbar::createNativeScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
57 return adoptRef(new Scrollbar(scrollableArea, orientation, size));
61 int Scrollbar::maxOverlapBetweenPages()
63 static int maxOverlapBetweenPages = ScrollbarTheme::nativeTheme()->maxOverlapBetweenPages();
64 return maxOverlapBetweenPages;
67 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize,
68 ScrollbarTheme* theme)
69 : m_scrollableArea(scrollableArea)
70 , m_orientation(orientation)
71 , m_controlSize(controlSize)
80 , m_hoveredPart(NoPart)
81 , m_pressedPart(NoPart)
83 , m_draggingDocument(false)
84 , m_documentDragPos(0)
86 , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
87 , m_overlapsResizer(false)
88 , m_suppressInvalidation(false)
91 m_theme = ScrollbarTheme::nativeTheme();
93 m_theme->registerScrollbar(this);
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));
101 if (m_scrollableArea)
102 m_currentPos = static_cast<float>(m_scrollableArea->scrollPosition(this));
105 Scrollbar::~Scrollbar()
107 if (AXObjectCache::accessibilityEnabled() && axObjectCache())
108 axObjectCache()->remove(this);
112 m_theme->unregisterScrollbar(this);
115 void Scrollbar::offsetDidChange()
117 ASSERT(m_scrollableArea);
119 float position = static_cast<float>(m_scrollableArea->scrollPosition(this));
120 if (position == m_currentPos)
123 int oldThumbPosition = theme()->thumbPosition(this);
124 m_currentPos = position;
125 updateThumbPosition();
126 if (m_pressedPart == ThumbPart)
127 setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
130 void Scrollbar::setProportion(int visibleSize, int totalSize)
132 if (visibleSize == m_visibleSize && totalSize == m_totalSize)
135 m_visibleSize = visibleSize;
136 m_totalSize = totalSize;
138 updateThumbProportion();
141 void Scrollbar::setSteps(int lineStep, int pageStep, int pixelsPerStep)
143 m_lineStep = lineStep;
144 m_pageStep = pageStep;
145 m_pixelStep = 1.0f / pixelsPerStep;
148 void Scrollbar::updateThumb()
150 #ifdef THUMB_POSITION_AFFECTS_BUTTONS
153 theme()->invalidateParts(this, ForwardTrackPart | BackTrackPart | ThumbPart);
157 void Scrollbar::updateThumbPosition()
162 void Scrollbar::updateThumbProportion()
167 void Scrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
169 if (context->updatingControlTints() && theme()->supportsControlTints()) {
174 if (context->paintingDisabled() || !frameRect().intersects(damageRect))
177 if (!theme()->paint(this, context, damageRect))
178 Widget::paint(context, damageRect);
181 void Scrollbar::autoscrollTimerFired(Timer<Scrollbar>*)
183 autoscrollPressedPart(theme()->autoscrollTimerDelay());
186 static bool thumbUnderMouse(Scrollbar* scrollbar)
188 int thumbPos = scrollbar->theme()->trackPosition(scrollbar) + scrollbar->theme()->thumbPosition(scrollbar);
189 int thumbLength = scrollbar->theme()->thumbLength(scrollbar);
190 return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength;
193 void Scrollbar::autoscrollPressedPart(double delay)
195 // Don't do anything for the thumb or if nothing was pressed.
196 if (m_pressedPart == ThumbPart || m_pressedPart == NoPart)
200 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
201 theme()->invalidatePart(this, m_pressedPart);
202 setHoveredPart(ThumbPart);
206 // Handle the arrows and track.
207 if (m_scrollableArea && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity()))
208 startTimerIfNeeded(delay);
211 void Scrollbar::startTimerIfNeeded(double delay)
213 // Don't do anything for the thumb.
214 if (m_pressedPart == ThumbPart)
217 // Handle the track. We halt track scrolling once the thumb is level
219 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
220 theme()->invalidatePart(this, m_pressedPart);
221 setHoveredPart(ThumbPart);
225 // We can't scroll if we've hit the beginning or end.
226 ScrollDirection dir = pressedPartScrollDirection();
227 if (dir == ScrollUp || dir == ScrollLeft) {
228 if (m_currentPos == 0)
231 if (m_currentPos == maximum())
235 m_scrollTimer.startOneShot(delay);
238 void Scrollbar::stopTimerIfNeeded()
240 if (m_scrollTimer.isActive())
241 m_scrollTimer.stop();
244 ScrollDirection Scrollbar::pressedPartScrollDirection()
246 if (m_orientation == HorizontalScrollbar) {
247 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
251 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
257 ScrollGranularity Scrollbar::pressedPartScrollGranularity()
259 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart)
264 void Scrollbar::moveThumb(int pos, bool draggingDocument)
266 if (!m_scrollableArea)
269 int delta = pos - m_pressedPos;
271 if (draggingDocument) {
272 if (m_draggingDocument)
273 delta = pos - m_documentDragPos;
274 m_draggingDocument = true;
275 FloatPoint currentPosition = m_scrollableArea->scrollAnimator()->currentPosition();
276 int destinationPosition = (m_orientation == HorizontalScrollbar ? currentPosition.x() : currentPosition.y()) + delta;
278 destinationPosition = min(destinationPosition + delta, maximum());
280 destinationPosition = max(destinationPosition + delta, 0);
281 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, destinationPosition);
282 m_documentDragPos = pos;
286 if (m_draggingDocument) {
287 delta += m_pressedPos - m_documentDragPos;
288 m_draggingDocument = false;
292 int thumbPos = theme()->thumbPosition(this);
293 int thumbLen = theme()->thumbLength(this);
294 int trackLen = theme()->trackLength(this);
295 int maxPos = trackLen - thumbLen;
297 delta = min(maxPos - thumbPos, delta);
299 delta = max(-thumbPos, delta);
302 float newPosition = static_cast<float>(thumbPos + delta) * maximum() / (trackLen - thumbLen);
303 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, newPosition);
307 void Scrollbar::setHoveredPart(ScrollbarPart part)
309 if (part == m_hoveredPart)
312 if ((m_hoveredPart == NoPart || part == NoPart) && theme()->invalidateOnMouseEnterExit())
313 invalidate(); // Just invalidate the whole scrollbar, since the buttons at either end change anyway.
314 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.
315 theme()->invalidatePart(this, part);
316 theme()->invalidatePart(this, m_hoveredPart);
318 m_hoveredPart = part;
321 void Scrollbar::setPressedPart(ScrollbarPart part)
323 if (m_pressedPart != NoPart)
324 theme()->invalidatePart(this, m_pressedPart);
325 m_pressedPart = part;
326 if (m_pressedPart != NoPart)
327 theme()->invalidatePart(this, m_pressedPart);
328 else if (m_hoveredPart != NoPart) // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part.
329 theme()->invalidatePart(this, m_hoveredPart);
332 bool Scrollbar::mouseMoved(const PlatformMouseEvent& evt)
334 if (m_pressedPart == ThumbPart) {
335 if (theme()->shouldSnapBackToDragOrigin(this, evt)) {
336 if (m_scrollableArea)
337 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin);
339 moveThumb(m_orientation == HorizontalScrollbar ?
340 convertFromContainingWindow(evt.pos()).x() :
341 convertFromContainingWindow(evt.pos()).y(), theme()->shouldDragDocumentInsteadOfThumb(this, evt));
346 if (m_pressedPart != NoPart)
347 m_pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.pos()).x() : convertFromContainingWindow(evt.pos()).y());
349 ScrollbarPart part = theme()->hitTest(this, evt);
350 if (part != m_hoveredPart) {
351 if (m_pressedPart != NoPart) {
352 if (part == m_pressedPart) {
353 // The mouse is moving back over the pressed part. We
354 // need to start up the timer action again.
355 startTimerIfNeeded(theme()->autoscrollTimerDelay());
356 theme()->invalidatePart(this, m_pressedPart);
357 } else if (m_hoveredPart == m_pressedPart) {
358 // The mouse is leaving the pressed part. Kill our timer
361 theme()->invalidatePart(this, m_pressedPart);
365 setHoveredPart(part);
371 bool Scrollbar::mouseExited()
373 setHoveredPart(NoPart);
377 bool Scrollbar::mouseUp()
379 setPressedPart(NoPart);
381 m_draggingDocument = false;
384 if (parent() && parent()->isFrameView())
385 static_cast<FrameView*>(parent())->frame()->eventHandler()->setMousePressed(false);
390 bool Scrollbar::mouseDown(const PlatformMouseEvent& evt)
392 // Early exit for right click
393 if (evt.button() == RightButton)
394 return true; // FIXME: Handled as context menu by Qt right now. Should just avoid even calling this method on a right click though.
396 setPressedPart(theme()->hitTest(this, evt));
397 int pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.pos()).x() : convertFromContainingWindow(evt.pos()).y());
399 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && theme()->shouldCenterOnThumb(this, evt)) {
400 setHoveredPart(ThumbPart);
401 setPressedPart(ThumbPart);
402 m_dragOrigin = m_currentPos;
403 int thumbLen = theme()->thumbLength(this);
404 int desiredPos = pressedPos;
405 // Set the pressed position to the middle of the thumb so that when we do the move, the delta
406 // will be from the current pixel position of the thumb to the new desired position for the thumb.
407 m_pressedPos = theme()->trackPosition(this) + theme()->thumbPosition(this) + thumbLen / 2;
408 moveThumb(desiredPos);
410 } else if (m_pressedPart == ThumbPart)
411 m_dragOrigin = m_currentPos;
413 m_pressedPos = pressedPos;
415 autoscrollPressedPart(theme()->initialAutoscrollTimerDelay());
419 void Scrollbar::setFrameRect(const IntRect& rect)
421 // Get our window resizer rect and see if we overlap. Adjust to avoid the overlap
423 IntRect adjustedRect(rect);
424 bool overlapsResizer = false;
425 ScrollView* view = parent();
426 if (view && !rect.isEmpty() && !view->windowResizerRect().isEmpty()) {
427 IntRect resizerRect = view->convertFromContainingWindow(view->windowResizerRect());
428 if (rect.intersects(resizerRect)) {
429 if (orientation() == HorizontalScrollbar) {
430 int overlap = rect.maxX() - resizerRect.x();
431 if (overlap > 0 && resizerRect.maxX() >= rect.maxX()) {
432 adjustedRect.setWidth(rect.width() - overlap);
433 overlapsResizer = true;
436 int overlap = rect.maxY() - resizerRect.y();
437 if (overlap > 0 && resizerRect.maxY() >= rect.maxY()) {
438 adjustedRect.setHeight(rect.height() - overlap);
439 overlapsResizer = true;
444 if (overlapsResizer != m_overlapsResizer) {
445 m_overlapsResizer = overlapsResizer;
447 view->adjustScrollbarsAvoidingResizerCount(m_overlapsResizer ? 1 : -1);
450 Widget::setFrameRect(adjustedRect);
453 void Scrollbar::setParent(ScrollView* parentView)
455 if (!parentView && m_overlapsResizer && parent())
456 parent()->adjustScrollbarsAvoidingResizerCount(-1);
457 Widget::setParent(parentView);
460 void Scrollbar::setEnabled(bool e)
465 theme()->updateEnabledState(this);
469 bool Scrollbar::isOverlayScrollbar() const
471 return m_theme->usesOverlayScrollbars();
474 bool Scrollbar::isWindowActive() const
476 return m_scrollableArea && m_scrollableArea->isActive();
479 AXObjectCache* Scrollbar::axObjectCache() const
481 if (!parent() || !parent()->isFrameView())
484 // FIXME: Accessing the FrameView and Document here is a layering violation
485 // and should be removed.
486 Document* document = static_cast<FrameView*>(parent())->frame()->document();
487 return document->axObjectCache();
490 void Scrollbar::invalidateRect(const IntRect& rect)
492 if (suppressInvalidation())
495 if (m_scrollableArea)
496 m_scrollableArea->invalidateScrollbar(this, rect);
499 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
501 if (m_scrollableArea)
502 return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
504 return Widget::convertToContainingView(localRect);
507 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
509 if (m_scrollableArea)
510 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
512 return Widget::convertFromContainingView(parentRect);
515 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
517 if (m_scrollableArea)
518 return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
520 return Widget::convertToContainingView(localPoint);
523 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
525 if (m_scrollableArea)
526 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
528 return Widget::convertFromContainingView(parentPoint);
531 } // namespace WebCore