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 "AXObjectCache.h"
30 #include "AccessibilityScrollbar.h"
31 #include "EventHandler.h"
33 #include "FrameView.h"
34 #include "GraphicsContext.h"
35 #include "PlatformMouseEvent.h"
36 #include "ScrollableArea.h"
37 #include "ScrollbarTheme.h"
43 #if (PLATFORM(CHROMIUM) && (OS(LINUX) || OS(FREEBSD))) || PLATFORM(GTK)
44 // The position of the scrollbar thumb affects the appearance of the steppers, so
45 // when the thumb moves, we have to invalidate them for painting.
46 #define THUMB_POSITION_AFFECTS_BUTTONS
52 PassRefPtr<Scrollbar> Scrollbar::createNativeScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
54 return adoptRef(new Scrollbar(scrollableArea, orientation, size));
58 int Scrollbar::maxOverlapBetweenPages()
60 static int maxOverlapBetweenPages = ScrollbarTheme::nativeTheme()->maxOverlapBetweenPages();
61 return maxOverlapBetweenPages;
64 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize,
65 ScrollbarTheme* theme)
66 : m_scrollableArea(scrollableArea)
67 , m_orientation(orientation)
68 , m_controlSize(controlSize)
77 , m_hoveredPart(NoPart)
78 , m_pressedPart(NoPart)
81 , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
82 , m_overlapsResizer(false)
83 , m_suppressInvalidation(false)
86 m_theme = ScrollbarTheme::nativeTheme();
88 m_theme->registerScrollbar(this);
90 // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
91 // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
92 // alone when sizing).
93 int thickness = m_theme->scrollbarThickness(controlSize);
94 Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
97 Scrollbar::~Scrollbar()
99 if (AXObjectCache::accessibilityEnabled() && axObjectCache())
100 axObjectCache()->remove(this);
104 m_theme->unregisterScrollbar(this);
107 void Scrollbar::offsetDidChange()
109 ASSERT(m_scrollableArea);
111 float position = static_cast<float>(m_scrollableArea->scrollPosition(this));
112 if (position == m_currentPos)
115 int oldThumbPosition = theme()->thumbPosition(this);
116 m_currentPos = position;
117 updateThumbPosition();
118 if (m_pressedPart == ThumbPart)
119 setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
122 void Scrollbar::setProportion(int visibleSize, int totalSize)
124 if (visibleSize == m_visibleSize && totalSize == m_totalSize)
127 m_visibleSize = visibleSize;
128 m_totalSize = totalSize;
130 updateThumbProportion();
133 void Scrollbar::setSteps(int lineStep, int pageStep, int pixelsPerStep)
135 m_lineStep = lineStep;
136 m_pageStep = pageStep;
137 m_pixelStep = 1.0f / pixelsPerStep;
140 void Scrollbar::updateThumb()
142 #ifdef THUMB_POSITION_AFFECTS_BUTTONS
145 theme()->invalidateParts(this, ForwardTrackPart | BackTrackPart | ThumbPart);
149 void Scrollbar::updateThumbPosition()
154 void Scrollbar::updateThumbProportion()
159 void Scrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
161 if (context->updatingControlTints() && theme()->supportsControlTints()) {
166 if (context->paintingDisabled() || !frameRect().intersects(damageRect))
169 if (!theme()->paint(this, context, damageRect))
170 Widget::paint(context, damageRect);
173 void Scrollbar::autoscrollTimerFired(Timer<Scrollbar>*)
175 autoscrollPressedPart(theme()->autoscrollTimerDelay());
178 static bool thumbUnderMouse(Scrollbar* scrollbar)
180 int thumbPos = scrollbar->theme()->trackPosition(scrollbar) + scrollbar->theme()->thumbPosition(scrollbar);
181 int thumbLength = scrollbar->theme()->thumbLength(scrollbar);
182 return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength;
185 void Scrollbar::autoscrollPressedPart(double delay)
187 // Don't do anything for the thumb or if nothing was pressed.
188 if (m_pressedPart == ThumbPart || m_pressedPart == NoPart)
192 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
193 theme()->invalidatePart(this, m_pressedPart);
194 setHoveredPart(ThumbPart);
198 // Handle the arrows and track.
199 if (scrollableArea()->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity()))
200 startTimerIfNeeded(delay);
203 void Scrollbar::startTimerIfNeeded(double delay)
205 // Don't do anything for the thumb.
206 if (m_pressedPart == ThumbPart)
209 // Handle the track. We halt track scrolling once the thumb is level
211 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
212 theme()->invalidatePart(this, m_pressedPart);
213 setHoveredPart(ThumbPart);
217 // We can't scroll if we've hit the beginning or end.
218 ScrollDirection dir = pressedPartScrollDirection();
219 if (dir == ScrollUp || dir == ScrollLeft) {
220 if (m_currentPos == 0)
223 if (m_currentPos == maximum())
227 m_scrollTimer.startOneShot(delay);
230 void Scrollbar::stopTimerIfNeeded()
232 if (m_scrollTimer.isActive())
233 m_scrollTimer.stop();
236 ScrollDirection Scrollbar::pressedPartScrollDirection()
238 if (m_orientation == HorizontalScrollbar) {
239 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
243 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
249 ScrollGranularity Scrollbar::pressedPartScrollGranularity()
251 if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart)
256 void Scrollbar::moveThumb(int pos)
259 int thumbPos = theme()->thumbPosition(this);
260 int thumbLen = theme()->thumbLength(this);
261 int trackLen = theme()->trackLength(this);
262 int maxPos = trackLen - thumbLen;
263 int delta = pos - m_pressedPos;
265 delta = min(maxPos - thumbPos, delta);
267 delta = max(-thumbPos, delta);
270 float newPosition = static_cast<float>(thumbPos + delta) * maximum() / (trackLen - thumbLen);
271 scrollableArea()->scrollToOffsetWithoutAnimation(m_orientation, newPosition);
275 void Scrollbar::setHoveredPart(ScrollbarPart part)
277 if (part == m_hoveredPart)
280 if ((m_hoveredPart == NoPart || part == NoPart) && theme()->invalidateOnMouseEnterExit())
281 invalidate(); // Just invalidate the whole scrollbar, since the buttons at either end change anyway.
282 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.
283 theme()->invalidatePart(this, part);
284 theme()->invalidatePart(this, m_hoveredPart);
286 m_hoveredPart = part;
289 void Scrollbar::setPressedPart(ScrollbarPart part)
291 if (m_pressedPart != NoPart)
292 theme()->invalidatePart(this, m_pressedPart);
293 m_pressedPart = part;
294 if (m_pressedPart != NoPart)
295 theme()->invalidatePart(this, m_pressedPart);
296 else if (m_hoveredPart != NoPart) // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part.
297 theme()->invalidatePart(this, m_hoveredPart);
300 bool Scrollbar::mouseMoved(const PlatformMouseEvent& evt)
302 if (m_pressedPart == ThumbPart) {
303 if (theme()->shouldSnapBackToDragOrigin(this, evt))
304 scrollableArea()->scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin);
306 moveThumb(m_orientation == HorizontalScrollbar ?
307 convertFromContainingWindow(evt.pos()).x() :
308 convertFromContainingWindow(evt.pos()).y());
313 if (m_pressedPart != NoPart)
314 m_pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.pos()).x() : convertFromContainingWindow(evt.pos()).y());
316 ScrollbarPart part = theme()->hitTest(this, evt);
317 if (part != m_hoveredPart) {
318 if (m_pressedPart != NoPart) {
319 if (part == m_pressedPart) {
320 // The mouse is moving back over the pressed part. We
321 // need to start up the timer action again.
322 startTimerIfNeeded(theme()->autoscrollTimerDelay());
323 theme()->invalidatePart(this, m_pressedPart);
324 } else if (m_hoveredPart == m_pressedPart) {
325 // The mouse is leaving the pressed part. Kill our timer
328 theme()->invalidatePart(this, m_pressedPart);
332 setHoveredPart(part);
338 bool Scrollbar::mouseExited()
340 setHoveredPart(NoPart);
344 bool Scrollbar::mouseUp()
346 setPressedPart(NoPart);
350 if (parent() && parent()->isFrameView())
351 static_cast<FrameView*>(parent())->frame()->eventHandler()->setMousePressed(false);
356 bool Scrollbar::mouseDown(const PlatformMouseEvent& evt)
358 // Early exit for right click
359 if (evt.button() == RightButton)
360 return true; // FIXME: Handled as context menu by Qt right now. Should just avoid even calling this method on a right click though.
362 setPressedPart(theme()->hitTest(this, evt));
363 int pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.pos()).x() : convertFromContainingWindow(evt.pos()).y());
365 if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && theme()->shouldCenterOnThumb(this, evt)) {
366 setHoveredPart(ThumbPart);
367 setPressedPart(ThumbPart);
368 m_dragOrigin = m_currentPos;
369 int thumbLen = theme()->thumbLength(this);
370 int desiredPos = pressedPos;
371 // Set the pressed position to the middle of the thumb so that when we do the move, the delta
372 // will be from the current pixel position of the thumb to the new desired position for the thumb.
373 m_pressedPos = theme()->trackPosition(this) + theme()->thumbPosition(this) + thumbLen / 2;
374 moveThumb(desiredPos);
376 } else if (m_pressedPart == ThumbPart)
377 m_dragOrigin = m_currentPos;
379 m_pressedPos = pressedPos;
381 autoscrollPressedPart(theme()->initialAutoscrollTimerDelay());
385 void Scrollbar::setFrameRect(const IntRect& rect)
387 // Get our window resizer rect and see if we overlap. Adjust to avoid the overlap
389 IntRect adjustedRect(rect);
390 bool overlapsResizer = false;
391 ScrollView* view = parent();
392 if (view && !rect.isEmpty() && !view->windowResizerRect().isEmpty()) {
393 IntRect resizerRect = view->convertFromContainingWindow(view->windowResizerRect());
394 if (rect.intersects(resizerRect)) {
395 if (orientation() == HorizontalScrollbar) {
396 int overlap = rect.right() - resizerRect.x();
397 if (overlap > 0 && resizerRect.right() >= rect.right()) {
398 adjustedRect.setWidth(rect.width() - overlap);
399 overlapsResizer = true;
402 int overlap = rect.bottom() - resizerRect.y();
403 if (overlap > 0 && resizerRect.bottom() >= rect.bottom()) {
404 adjustedRect.setHeight(rect.height() - overlap);
405 overlapsResizer = true;
410 if (overlapsResizer != m_overlapsResizer) {
411 m_overlapsResizer = overlapsResizer;
413 view->adjustScrollbarsAvoidingResizerCount(m_overlapsResizer ? 1 : -1);
416 Widget::setFrameRect(adjustedRect);
419 void Scrollbar::setParent(ScrollView* parentView)
421 if (!parentView && m_overlapsResizer && parent())
422 parent()->adjustScrollbarsAvoidingResizerCount(-1);
423 Widget::setParent(parentView);
426 void Scrollbar::setEnabled(bool e)
434 bool Scrollbar::isWindowActive() const
436 return m_scrollableArea && m_scrollableArea->isActive();
439 AXObjectCache* Scrollbar::axObjectCache() const
441 if (!parent() || !parent()->isFrameView())
444 Document* document = static_cast<FrameView*>(parent())->frame()->document();
445 return document->axObjectCache();
448 void Scrollbar::invalidateRect(const IntRect& rect)
450 if (suppressInvalidation())
452 if (m_scrollableArea)
453 m_scrollableArea->invalidateScrollbarRect(this, rect);
456 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
458 if (m_scrollableArea)
459 return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
461 return Widget::convertToContainingView(localRect);
464 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
466 if (m_scrollableArea)
467 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
469 return Widget::convertFromContainingView(parentRect);
472 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
474 if (m_scrollableArea)
475 return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
477 return Widget::convertToContainingView(localPoint);
480 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
482 if (m_scrollableArea)
483 return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
485 return Widget::convertFromContainingView(parentPoint);
488 } // namespace WebCore