2 * Copyright (C) 2006, 2007, 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/ScrollView.h"
29 #include "platform/graphics/GraphicsContextStateSaver.h"
30 #include "platform/graphics/GraphicsLayer.h"
31 #include "platform/HostWindow.h"
32 #include "platform/scroll/ScrollbarTheme.h"
33 #include "wtf/StdLibExtras.h"
37 ScrollView::ScrollView()
38 : m_horizontalScrollbarMode(ScrollbarAuto)
39 , m_verticalScrollbarMode(ScrollbarAuto)
40 , m_horizontalScrollbarLock(false)
41 , m_verticalScrollbarLock(false)
42 , m_scrollbarsAvoidingResizer(0)
43 , m_scrollbarsSuppressed(false)
44 , m_inUpdateScrollbars(false)
45 , m_drawPanScrollIcon(false)
46 , m_clipsRepaints(true)
50 ScrollView::~ScrollView()
54 void ScrollView::addChild(PassRefPtr<Widget> prpChild)
56 Widget* child = prpChild.get();
57 ASSERT(child != this && !child->parent());
58 child->setParent(this);
59 m_children.add(prpChild);
62 void ScrollView::removeChild(Widget* child)
64 ASSERT(child->parent() == this);
66 m_children.remove(child);
69 void ScrollView::setHasHorizontalScrollbar(bool hasBar)
71 if (hasBar && !m_horizontalScrollbar) {
72 m_horizontalScrollbar = createScrollbar(HorizontalScrollbar);
73 addChild(m_horizontalScrollbar.get());
74 didAddScrollbar(m_horizontalScrollbar.get(), HorizontalScrollbar);
75 m_horizontalScrollbar->styleChanged();
76 } else if (!hasBar && m_horizontalScrollbar) {
77 willRemoveScrollbar(m_horizontalScrollbar.get(), HorizontalScrollbar);
78 removeChild(m_horizontalScrollbar.get());
79 m_horizontalScrollbar = nullptr;
83 void ScrollView::setHasVerticalScrollbar(bool hasBar)
85 if (hasBar && !m_verticalScrollbar) {
86 m_verticalScrollbar = createScrollbar(VerticalScrollbar);
87 addChild(m_verticalScrollbar.get());
88 didAddScrollbar(m_verticalScrollbar.get(), VerticalScrollbar);
89 m_verticalScrollbar->styleChanged();
90 } else if (!hasBar && m_verticalScrollbar) {
91 willRemoveScrollbar(m_verticalScrollbar.get(), VerticalScrollbar);
92 removeChild(m_verticalScrollbar.get());
93 m_verticalScrollbar = nullptr;
97 PassRefPtr<Scrollbar> ScrollView::createScrollbar(ScrollbarOrientation orientation)
99 return Scrollbar::create(this, orientation, RegularScrollbar);
102 void ScrollView::setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode,
103 bool horizontalLock, bool verticalLock)
105 bool needsUpdate = false;
107 if (horizontalMode != horizontalScrollbarMode() && !m_horizontalScrollbarLock) {
108 m_horizontalScrollbarMode = horizontalMode;
112 if (verticalMode != verticalScrollbarMode() && !m_verticalScrollbarLock) {
113 m_verticalScrollbarMode = verticalMode;
118 setHorizontalScrollbarLock();
121 setVerticalScrollbarLock();
126 updateScrollbars(scrollOffset());
128 if (!layerForScrolling())
130 blink::WebLayer* layer = layerForScrolling()->platformLayer();
133 layer->setUserScrollable(userInputScrollable(HorizontalScrollbar), userInputScrollable(VerticalScrollbar));
136 void ScrollView::scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const
138 horizontalMode = m_horizontalScrollbarMode;
139 verticalMode = m_verticalScrollbarMode;
142 void ScrollView::setCanHaveScrollbars(bool canScroll)
144 ScrollbarMode newHorizontalMode;
145 ScrollbarMode newVerticalMode;
147 scrollbarModes(newHorizontalMode, newVerticalMode);
149 if (canScroll && newVerticalMode == ScrollbarAlwaysOff)
150 newVerticalMode = ScrollbarAuto;
152 newVerticalMode = ScrollbarAlwaysOff;
154 if (canScroll && newHorizontalMode == ScrollbarAlwaysOff)
155 newHorizontalMode = ScrollbarAuto;
157 newHorizontalMode = ScrollbarAlwaysOff;
159 setScrollbarModes(newHorizontalMode, newVerticalMode);
162 void ScrollView::setClipsRepaints(bool clipsRepaints)
164 m_clipsRepaints = clipsRepaints;
167 IntSize ScrollView::unscaledVisibleContentSize(IncludeScrollbarsInRect scrollbarInclusion) const
169 return scrollbarInclusion == ExcludeScrollbars ? excludeScrollbars(frameRect().size()) : frameRect().size();
172 IntSize ScrollView::excludeScrollbars(const IntSize& size) const
174 int verticalScrollbarWidth = 0;
175 int horizontalScrollbarHeight = 0;
177 if (Scrollbar* verticalBar = verticalScrollbar())
178 verticalScrollbarWidth = !verticalBar->isOverlayScrollbar() ? verticalBar->width() : 0;
179 if (Scrollbar* horizontalBar = horizontalScrollbar())
180 horizontalScrollbarHeight = !horizontalBar->isOverlayScrollbar() ? horizontalBar->height() : 0;
182 return IntSize(std::max(0, size.width() - verticalScrollbarWidth),
183 std::max(0, size.height() - horizontalScrollbarHeight));
187 IntRect ScrollView::visibleContentRect(IncludeScrollbarsInRect scollbarInclusion) const
189 FloatSize visibleContentSize = unscaledVisibleContentSize(scollbarInclusion);
190 visibleContentSize.scale(1 / visibleContentScaleFactor());
191 return IntRect(IntPoint(m_scrollOffset), expandedIntSize(visibleContentSize));
194 IntSize ScrollView::contentsSize() const
196 return m_contentsSize;
199 void ScrollView::setContentsSize(const IntSize& newSize)
201 if (contentsSize() == newSize)
203 m_contentsSize = newSize;
204 updateScrollbars(scrollOffset());
205 updateOverhangAreas();
208 IntPoint ScrollView::maximumScrollPosition() const
210 IntPoint maximumOffset(contentsWidth() - visibleWidth() - scrollOrigin().x(), contentsHeight() - visibleHeight() - scrollOrigin().y());
211 maximumOffset.clampNegativeToZero();
212 return maximumOffset;
215 IntPoint ScrollView::minimumScrollPosition() const
217 return IntPoint(-scrollOrigin().x(), -scrollOrigin().y());
220 IntPoint ScrollView::adjustScrollPositionWithinRange(const IntPoint& scrollPoint) const
222 if (!constrainsScrollingToContentEdge())
225 IntPoint newScrollPosition = scrollPoint.shrunkTo(maximumScrollPosition());
226 newScrollPosition = newScrollPosition.expandedTo(minimumScrollPosition());
227 return newScrollPosition;
230 void ScrollView::adjustScrollbarOpacity()
232 if (m_horizontalScrollbar && layerForHorizontalScrollbar()) {
233 bool isOpaqueScrollbar = !m_horizontalScrollbar->isOverlayScrollbar();
234 layerForHorizontalScrollbar()->setContentsOpaque(isOpaqueScrollbar);
236 if (m_verticalScrollbar && layerForVerticalScrollbar()) {
237 bool isOpaqueScrollbar = !m_verticalScrollbar->isOverlayScrollbar();
238 layerForVerticalScrollbar()->setContentsOpaque(isOpaqueScrollbar);
242 int ScrollView::scrollSize(ScrollbarOrientation orientation) const
244 Scrollbar* scrollbar = ((orientation == HorizontalScrollbar) ? m_horizontalScrollbar : m_verticalScrollbar).get();
246 // If no scrollbars are present, the content may still be scrollable.
248 IntSize scrollSize = m_contentsSize - visibleContentRect().size();
249 scrollSize.clampNegativeToZero();
250 return orientation == HorizontalScrollbar ? scrollSize.width() : scrollSize.height();
253 return scrollbar->totalSize() - scrollbar->visibleSize();
256 void ScrollView::notifyPageThatContentAreaWillPaint() const
260 void ScrollView::setScrollOffset(const IntPoint& offset)
262 scrollTo(toIntSize(adjustScrollPositionWithinRange(offset)));
265 void ScrollView::scrollTo(const IntSize& newOffset)
267 IntSize scrollDelta = newOffset - m_scrollOffset;
268 if (scrollDelta == IntSize())
270 m_scrollOffset = newOffset;
272 if (scrollbarsSuppressed())
276 m_pendingScrollDelta += scrollDelta;
278 scrollContents(scrollDelta);
281 void ScrollView::setScrollPosition(const IntPoint& scrollPoint, ScrollBehavior scrollBehavior)
283 IntPoint newScrollPosition = adjustScrollPositionWithinRange(scrollPoint);
285 if (newScrollPosition == scrollPosition())
288 if (scrollBehavior == ScrollBehaviorInstant)
289 updateScrollbars(IntSize(newScrollPosition.x(), newScrollPosition.y()));
291 programmaticallyScrollSmoothlyToOffset(newScrollPosition);
294 bool ScrollView::scroll(ScrollDirection direction, ScrollGranularity granularity)
296 ScrollDirection physicalDirection =
297 toPhysicalDirection(direction, isVerticalDocument(), isFlippedDocument());
299 return ScrollableArea::scroll(physicalDirection, granularity);
302 IntSize ScrollView::overhangAmount() const
306 IntPoint currentScrollPosition = scrollPosition();
307 IntPoint minScrollPosition = minimumScrollPosition();
308 IntPoint maxScrollPosition = maximumScrollPosition();
310 if (currentScrollPosition.x() < minScrollPosition.x())
311 stretch.setWidth(currentScrollPosition.x() - minScrollPosition.x());
312 if (currentScrollPosition.x() > maxScrollPosition.x())
313 stretch.setWidth(currentScrollPosition.x() - maxScrollPosition.x());
315 if (currentScrollPosition.y() < minScrollPosition.y())
316 stretch.setHeight(currentScrollPosition.y() - minScrollPosition.y());
317 if (currentScrollPosition.y() > maxScrollPosition.y())
318 stretch.setHeight(currentScrollPosition.y() - maxScrollPosition.y());
323 void ScrollView::windowResizerRectChanged()
325 updateScrollbars(scrollOffset());
328 static bool useOverlayScrollbars()
330 // FIXME: Need to detect the presence of CSS custom scrollbars, which are non-overlay regardless the ScrollbarTheme.
331 return ScrollbarTheme::theme()->usesOverlayScrollbars();
334 void ScrollView::computeScrollbarExistence(bool& newHasHorizontalScrollbar, bool& newHasVerticalScrollbar, const IntSize& docSize, ComputeScrollbarExistenceOption option) const
336 bool hasHorizontalScrollbar = m_horizontalScrollbar;
337 bool hasVerticalScrollbar = m_verticalScrollbar;
339 newHasHorizontalScrollbar = hasHorizontalScrollbar;
340 newHasVerticalScrollbar = hasVerticalScrollbar;
342 ScrollbarMode hScroll = m_horizontalScrollbarMode;
343 ScrollbarMode vScroll = m_verticalScrollbarMode;
345 if (hScroll != ScrollbarAuto)
346 newHasHorizontalScrollbar = (hScroll == ScrollbarAlwaysOn);
347 if (vScroll != ScrollbarAuto)
348 newHasVerticalScrollbar = (vScroll == ScrollbarAlwaysOn);
350 if (m_scrollbarsSuppressed || (hScroll != ScrollbarAuto && vScroll != ScrollbarAuto))
353 if (hScroll == ScrollbarAuto)
354 newHasHorizontalScrollbar = docSize.width() > visibleWidth();
355 if (vScroll == ScrollbarAuto)
356 newHasVerticalScrollbar = docSize.height() > visibleHeight();
358 if (useOverlayScrollbars())
361 IntSize fullVisibleSize = visibleContentRect(IncludeScrollbars).size();
363 bool attemptToRemoveScrollbars = (option == FirstPass
364 && docSize.width() <= fullVisibleSize.width() && docSize.height() <= fullVisibleSize.height());
365 if (attemptToRemoveScrollbars) {
366 if (hScroll == ScrollbarAuto)
367 newHasHorizontalScrollbar = false;
368 if (vScroll == ScrollbarAuto)
369 newHasVerticalScrollbar = false;
372 // If we ever turn one scrollbar off, always turn the other one off too.
373 // Never ever try to both gain/lose a scrollbar in the same pass.
374 if (!newHasHorizontalScrollbar && hasHorizontalScrollbar && vScroll != ScrollbarAlwaysOn)
375 newHasVerticalScrollbar = false;
376 if (!newHasVerticalScrollbar && hasVerticalScrollbar && hScroll != ScrollbarAlwaysOn)
377 newHasHorizontalScrollbar = false;
380 void ScrollView::updateScrollbarGeometry()
382 if (m_horizontalScrollbar) {
383 int clientWidth = visibleWidth();
384 IntRect oldRect(m_horizontalScrollbar->frameRect());
385 IntRect hBarRect((shouldPlaceVerticalScrollbarOnLeft() && m_verticalScrollbar) ? m_verticalScrollbar->width() : 0,
386 height() - m_horizontalScrollbar->height(),
387 width() - (m_verticalScrollbar ? m_verticalScrollbar->width() : 0),
388 m_horizontalScrollbar->height());
389 m_horizontalScrollbar->setFrameRect(hBarRect);
390 if (!m_scrollbarsSuppressed && oldRect != m_horizontalScrollbar->frameRect())
391 m_horizontalScrollbar->invalidate();
393 if (m_scrollbarsSuppressed)
394 m_horizontalScrollbar->setSuppressInvalidation(true);
395 m_horizontalScrollbar->setEnabled(contentsWidth() > clientWidth);
396 m_horizontalScrollbar->setProportion(clientWidth, contentsWidth());
397 m_horizontalScrollbar->offsetDidChange();
398 if (m_scrollbarsSuppressed)
399 m_horizontalScrollbar->setSuppressInvalidation(false);
402 if (m_verticalScrollbar) {
403 int clientHeight = visibleHeight();
404 IntRect oldRect(m_verticalScrollbar->frameRect());
405 IntRect vBarRect(shouldPlaceVerticalScrollbarOnLeft() ? 0 : (width() - m_verticalScrollbar->width()),
407 m_verticalScrollbar->width(),
408 height() - (m_horizontalScrollbar ? m_horizontalScrollbar->height() : 0));
409 m_verticalScrollbar->setFrameRect(vBarRect);
410 if (!m_scrollbarsSuppressed && oldRect != m_verticalScrollbar->frameRect())
411 m_verticalScrollbar->invalidate();
413 if (m_scrollbarsSuppressed)
414 m_verticalScrollbar->setSuppressInvalidation(true);
415 m_verticalScrollbar->setEnabled(contentsHeight() > clientHeight);
416 m_verticalScrollbar->setProportion(clientHeight, contentsHeight());
417 m_verticalScrollbar->offsetDidChange();
418 if (m_scrollbarsSuppressed)
419 m_verticalScrollbar->setSuppressInvalidation(false);
423 bool ScrollView::adjustScrollbarExistence(ComputeScrollbarExistenceOption option)
425 ASSERT(m_inUpdateScrollbars);
427 // If we came in here with the view already needing a layout, then go ahead and do that
428 // first. (This will be the common case, e.g., when the page changes due to window resizing for example).
429 // This layout will not re-enter updateScrollbars and does not count towards our max layout pass total.
430 if (!m_scrollbarsSuppressed)
431 scrollbarExistenceDidChange();
433 bool hasHorizontalScrollbar = m_horizontalScrollbar;
434 bool hasVerticalScrollbar = m_verticalScrollbar;
436 bool newHasHorizontalScrollbar = false;
437 bool newHasVerticalScrollbar = false;
438 computeScrollbarExistence(newHasHorizontalScrollbar, newHasVerticalScrollbar, contentsSize(), option);
440 bool scrollbarExistenceChanged = hasHorizontalScrollbar != newHasHorizontalScrollbar || hasVerticalScrollbar != newHasVerticalScrollbar;
441 if (!scrollbarExistenceChanged)
444 setHasHorizontalScrollbar(newHasHorizontalScrollbar);
445 setHasVerticalScrollbar(newHasVerticalScrollbar);
447 if (m_scrollbarsSuppressed)
450 if (!useOverlayScrollbars())
452 scrollbarExistenceDidChange();
456 void ScrollView::updateScrollbars(const IntSize& desiredOffset)
458 if (scrollbarsDisabled()) {
459 setScrollOffsetFromUpdateScrollbars(desiredOffset);
463 if (m_inUpdateScrollbars)
465 InUpdateScrollbarsScope inUpdateScrollbarsScope(this);
467 IntSize oldVisibleSize = visibleSize();
469 bool scrollbarExistenceChanged = false;
470 int maxUpdateScrollbarsPass = useOverlayScrollbars() || m_scrollbarsSuppressed ? 1 : 3;
471 for (int updateScrollbarsPass = 0; updateScrollbarsPass < maxUpdateScrollbarsPass; updateScrollbarsPass++) {
472 if (!adjustScrollbarExistence(updateScrollbarsPass ? Incremental : FirstPass))
474 scrollbarExistenceChanged = true;
477 updateScrollbarGeometry();
479 if (scrollbarExistenceChanged) {
480 // FIXME: Is frameRectsChanged really necessary here? Have any frame rects changed?
482 positionScrollbarLayers();
483 updateScrollCorner();
486 // FIXME: We don't need to do this if we are composited.
487 IntSize newVisibleSize = visibleSize();
488 if (newVisibleSize.width() > oldVisibleSize.width()) {
489 if (shouldPlaceVerticalScrollbarOnLeft())
490 invalidateRect(IntRect(0, 0, newVisibleSize.width() - oldVisibleSize.width(), newVisibleSize.height()));
492 invalidateRect(IntRect(oldVisibleSize.width(), 0, newVisibleSize.width() - oldVisibleSize.width(), newVisibleSize.height()));
494 if (newVisibleSize.height() > oldVisibleSize.height())
495 invalidateRect(IntRect(0, oldVisibleSize.height(), newVisibleSize.width(), newVisibleSize.height() - oldVisibleSize.height()));
497 setScrollOffsetFromUpdateScrollbars(desiredOffset);
500 void ScrollView::setScrollOffsetFromUpdateScrollbars(const IntSize& offset)
502 IntPoint adjustedScrollPosition = IntPoint(offset);
504 if (!isRubberBandInProgress())
505 adjustedScrollPosition = adjustScrollPositionWithinRange(adjustedScrollPosition);
507 if (adjustedScrollPosition != scrollPosition() || scrollOriginChanged()) {
508 ScrollableArea::scrollToOffsetWithoutAnimation(adjustedScrollPosition);
509 resetScrollOriginChanged();
513 const int panIconSizeLength = 16;
515 IntRect ScrollView::rectToCopyOnScroll() const
517 IntRect scrollViewRect = convertToContainingWindow(IntRect((shouldPlaceVerticalScrollbarOnLeft() && verticalScrollbar()) ? verticalScrollbar()->width() : 0, 0, visibleWidth(), visibleHeight()));
518 if (hasOverlayScrollbars()) {
519 int verticalScrollbarWidth = (verticalScrollbar() && !hasLayerForVerticalScrollbar()) ? verticalScrollbar()->width() : 0;
520 int horizontalScrollbarHeight = (horizontalScrollbar() && !hasLayerForHorizontalScrollbar()) ? horizontalScrollbar()->height() : 0;
522 scrollViewRect.setWidth(scrollViewRect.width() - verticalScrollbarWidth);
523 scrollViewRect.setHeight(scrollViewRect.height() - horizontalScrollbarHeight);
525 return scrollViewRect;
528 void ScrollView::scrollContentsIfNeeded()
530 if (m_pendingScrollDelta.isZero())
532 IntSize scrollDelta = m_pendingScrollDelta;
533 m_pendingScrollDelta = IntSize();
534 scrollContents(scrollDelta);
537 void ScrollView::scrollContents(const IntSize& scrollDelta)
539 HostWindow* window = hostWindow();
543 IntRect clipRect = windowClipRect();
544 IntRect updateRect = clipRect;
545 updateRect.intersect(rectToCopyOnScroll());
547 if (m_drawPanScrollIcon) {
548 // FIXME: the pan icon is broken when accelerated compositing is on, since it will draw under the compositing layers.
549 // https://bugs.webkit.org/show_bug.cgi?id=47837
550 int panIconDirtySquareSizeLength = 2 * (panIconSizeLength + std::max(abs(scrollDelta.width()), abs(scrollDelta.height()))); // We only want to repaint what's necessary
551 IntPoint panIconDirtySquareLocation = IntPoint(m_panScrollIconPoint.x() - (panIconDirtySquareSizeLength / 2), m_panScrollIconPoint.y() - (panIconDirtySquareSizeLength / 2));
552 IntRect panScrollIconDirtyRect = IntRect(panIconDirtySquareLocation, IntSize(panIconDirtySquareSizeLength, panIconDirtySquareSizeLength));
553 panScrollIconDirtyRect.intersect(clipRect);
554 window->invalidateContentsAndRootView(panScrollIconDirtyRect);
557 if (!scrollContentsFastPath(-scrollDelta))
558 scrollContentsSlowPath(updateRect);
560 // Invalidate the overhang areas if they are visible.
561 updateOverhangAreas();
563 // This call will move children with native widgets (plugins) and invalidate them as well.
567 void ScrollView::scrollContentsSlowPath(const IntRect& updateRect)
569 hostWindow()->invalidateContentsForSlowScroll(updateRect);
572 IntPoint ScrollView::rootViewToContents(const IntPoint& rootViewPoint) const
574 IntPoint viewPoint = convertFromContainingWindow(rootViewPoint);
575 return viewPoint + scrollOffset();
578 IntPoint ScrollView::contentsToRootView(const IntPoint& contentsPoint) const
580 IntPoint viewPoint = contentsPoint - scrollOffset();
581 return convertToContainingWindow(viewPoint);
584 IntRect ScrollView::rootViewToContents(const IntRect& rootViewRect) const
586 IntRect viewRect = convertFromContainingWindow(rootViewRect);
587 viewRect.move(scrollOffset());
591 IntRect ScrollView::contentsToRootView(const IntRect& contentsRect) const
593 IntRect viewRect = contentsRect;
594 viewRect.move(-scrollOffset());
595 return convertToContainingWindow(viewRect);
598 IntPoint ScrollView::windowToContents(const IntPoint& windowPoint) const
600 IntPoint viewPoint = convertFromContainingWindow(windowPoint);
601 return viewPoint + scrollOffset();
604 FloatPoint ScrollView::windowToContents(const FloatPoint& windowPoint) const
606 FloatPoint viewPoint = convertFromContainingWindow(windowPoint);
607 return viewPoint + scrollOffset();
610 IntPoint ScrollView::contentsToWindow(const IntPoint& contentsPoint) const
612 IntPoint viewPoint = contentsPoint - scrollOffset();
613 return convertToContainingWindow(viewPoint);
616 IntRect ScrollView::windowToContents(const IntRect& windowRect) const
618 IntRect viewRect = convertFromContainingWindow(windowRect);
619 viewRect.move(scrollOffset());
623 IntRect ScrollView::contentsToWindow(const IntRect& contentsRect) const
625 IntRect viewRect = contentsRect;
626 viewRect.move(-scrollOffset());
627 return convertToContainingWindow(viewRect);
630 IntRect ScrollView::contentsToScreen(const IntRect& rect) const
632 HostWindow* window = hostWindow();
635 return window->rootViewToScreen(contentsToRootView(rect));
638 bool ScrollView::containsScrollbarsAvoidingResizer() const
640 return !m_scrollbarsAvoidingResizer;
643 void ScrollView::adjustScrollbarsAvoidingResizerCount(int overlapDelta)
645 int oldCount = m_scrollbarsAvoidingResizer;
646 m_scrollbarsAvoidingResizer += overlapDelta;
648 toScrollView(parent())->adjustScrollbarsAvoidingResizerCount(overlapDelta);
649 else if (!scrollbarsSuppressed()) {
650 // If we went from n to 0 or from 0 to n and we're the outermost view,
651 // we need to invalidate the windowResizerRect(), since it will now need to paint
653 if ((oldCount > 0 && m_scrollbarsAvoidingResizer == 0) ||
654 (oldCount == 0 && m_scrollbarsAvoidingResizer > 0))
655 invalidateRect(windowResizerRect());
659 void ScrollView::setParent(Widget* parentView)
661 if (parentView == parent())
664 if (m_scrollbarsAvoidingResizer && parent())
665 toScrollView(parent())->adjustScrollbarsAvoidingResizerCount(-m_scrollbarsAvoidingResizer);
667 Widget::setParent(parentView);
669 if (m_scrollbarsAvoidingResizer && parent())
670 toScrollView(parent())->adjustScrollbarsAvoidingResizerCount(m_scrollbarsAvoidingResizer);
673 void ScrollView::setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress)
675 if (suppressed == m_scrollbarsSuppressed)
678 m_scrollbarsSuppressed = suppressed;
680 if (repaintOnUnsuppress && !suppressed) {
681 if (m_horizontalScrollbar)
682 m_horizontalScrollbar->invalidate();
683 if (m_verticalScrollbar)
684 m_verticalScrollbar->invalidate();
686 // Invalidate the scroll corner too on unsuppress.
687 invalidateRect(scrollCornerRect());
691 Scrollbar* ScrollView::scrollbarAtWindowPoint(const IntPoint& windowPoint)
693 IntPoint viewPoint = convertFromContainingWindow(windowPoint);
694 return scrollbarAtViewPoint(viewPoint);
697 Scrollbar* ScrollView::scrollbarAtViewPoint(const IntPoint& viewPoint)
699 if (m_horizontalScrollbar && m_horizontalScrollbar->shouldParticipateInHitTesting() && m_horizontalScrollbar->frameRect().contains(viewPoint))
700 return m_horizontalScrollbar.get();
701 if (m_verticalScrollbar && m_verticalScrollbar->shouldParticipateInHitTesting() && m_verticalScrollbar->frameRect().contains(viewPoint))
702 return m_verticalScrollbar.get();
706 void ScrollView::setFrameRect(const IntRect& newRect)
708 IntRect oldRect = frameRect();
710 if (newRect == oldRect)
713 Widget::setFrameRect(newRect);
715 updateScrollbars(scrollOffset());
720 void ScrollView::frameRectsChanged()
722 HashSet<RefPtr<Widget> >::const_iterator end = m_children.end();
723 for (HashSet<RefPtr<Widget> >::const_iterator current = m_children.begin(); current != end; ++current)
724 (*current)->frameRectsChanged();
727 static void positionScrollbarLayer(GraphicsLayer* graphicsLayer, Scrollbar* scrollbar)
729 if (!graphicsLayer || !scrollbar)
732 IntRect scrollbarRect = scrollbar->frameRect();
733 graphicsLayer->setPosition(scrollbarRect.location());
735 if (scrollbarRect.size() == graphicsLayer->size())
738 graphicsLayer->setSize(scrollbarRect.size());
740 if (graphicsLayer->hasContentsLayer()) {
741 graphicsLayer->setContentsRect(IntRect(0, 0, scrollbarRect.width(), scrollbarRect.height()));
745 graphicsLayer->setDrawsContent(true);
746 graphicsLayer->setNeedsDisplay();
749 static void positionScrollCornerLayer(GraphicsLayer* graphicsLayer, const IntRect& cornerRect)
753 graphicsLayer->setDrawsContent(!cornerRect.isEmpty());
754 graphicsLayer->setPosition(cornerRect.location());
755 if (cornerRect.size() != graphicsLayer->size())
756 graphicsLayer->setNeedsDisplay();
757 graphicsLayer->setSize(cornerRect.size());
760 void ScrollView::positionScrollbarLayers()
762 positionScrollbarLayer(layerForHorizontalScrollbar(), horizontalScrollbar());
763 positionScrollbarLayer(layerForVerticalScrollbar(), verticalScrollbar());
764 positionScrollCornerLayer(layerForScrollCorner(), scrollCornerRect());
767 bool ScrollView::userInputScrollable(ScrollbarOrientation orientation) const
769 ScrollbarMode mode = (orientation == HorizontalScrollbar) ?
770 m_horizontalScrollbarMode : m_verticalScrollbarMode;
772 return mode == ScrollbarAuto || mode == ScrollbarAlwaysOn;
775 bool ScrollView::shouldPlaceVerticalScrollbarOnLeft() const
780 void ScrollView::contentRectangleForPaintInvalidation(const IntRect& rect)
782 IntRect paintRect = rect;
783 if (clipsPaintInvalidations())
784 paintRect.intersect(visibleContentRect());
785 if (paintRect.isEmpty())
788 if (HostWindow* window = hostWindow())
789 window->invalidateContentsAndRootView(contentsToWindow(paintRect));
792 IntRect ScrollView::scrollCornerRect() const
796 if (hasOverlayScrollbars())
799 if (m_horizontalScrollbar && width() - m_horizontalScrollbar->width() > 0) {
800 cornerRect.unite(IntRect(shouldPlaceVerticalScrollbarOnLeft() ? 0 : m_horizontalScrollbar->width(),
801 height() - m_horizontalScrollbar->height(),
802 width() - m_horizontalScrollbar->width(),
803 m_horizontalScrollbar->height()));
806 if (m_verticalScrollbar && height() - m_verticalScrollbar->height() > 0) {
807 cornerRect.unite(IntRect(shouldPlaceVerticalScrollbarOnLeft() ? 0 : (width() - m_verticalScrollbar->width()),
808 m_verticalScrollbar->height(),
809 m_verticalScrollbar->width(),
810 height() - m_verticalScrollbar->height()));
816 bool ScrollView::isScrollCornerVisible() const
818 return !scrollCornerRect().isEmpty();
821 void ScrollView::scrollbarStyleChanged()
823 adjustScrollbarOpacity();
825 updateScrollbars(scrollOffset());
826 positionScrollbarLayers();
829 void ScrollView::updateScrollCorner()
833 void ScrollView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
835 ScrollbarTheme::theme()->paintScrollCorner(context, cornerRect);
838 void ScrollView::paintScrollbar(GraphicsContext* context, Scrollbar* bar, const IntRect& rect)
840 bar->paint(context, rect);
843 void ScrollView::invalidateScrollCornerRect(const IntRect& rect)
845 invalidateRect(rect);
848 void ScrollView::paintScrollbars(GraphicsContext* context, const IntRect& rect)
850 if (m_horizontalScrollbar && !layerForHorizontalScrollbar())
851 paintScrollbar(context, m_horizontalScrollbar.get(), rect);
852 if (m_verticalScrollbar && !layerForVerticalScrollbar())
853 paintScrollbar(context, m_verticalScrollbar.get(), rect);
855 if (layerForScrollCorner())
857 paintScrollCorner(context, scrollCornerRect());
860 void ScrollView::paintPanScrollIcon(GraphicsContext* context)
862 DEFINE_STATIC_REF(Image, panScrollIcon, (Image::loadPlatformResource("panIcon")));
863 IntPoint iconGCPoint = m_panScrollIconPoint;
865 iconGCPoint = toScrollView(parent())->windowToContents(iconGCPoint);
866 context->drawImage(panScrollIcon, iconGCPoint);
869 void ScrollView::paint(GraphicsContext* context, const IntRect& rect)
871 notifyPageThatContentAreaWillPaint();
873 IntRect documentDirtyRect = rect;
874 IntRect visibleAreaWithoutScrollbars(location(), visibleContentRect().size());
875 documentDirtyRect.intersect(visibleAreaWithoutScrollbars);
877 if (!documentDirtyRect.isEmpty()) {
878 GraphicsContextStateSaver stateSaver(*context);
880 context->translate(x() - scrollX(), y() - scrollY());
881 context->clip(visibleContentRect());
883 documentDirtyRect.moveBy(-location() + scrollPosition());
884 paintContents(context, documentDirtyRect);
887 calculateAndPaintOverhangAreas(context, rect);
889 // Now paint the scrollbars.
890 if (!m_scrollbarsSuppressed && (m_horizontalScrollbar || m_verticalScrollbar)) {
891 GraphicsContextStateSaver stateSaver(*context);
892 IntRect scrollViewDirtyRect = rect;
893 IntRect visibleAreaWithScrollbars(location(), visibleContentRect(IncludeScrollbars).size());
894 scrollViewDirtyRect.intersect(visibleAreaWithScrollbars);
895 context->translate(x(), y());
896 scrollViewDirtyRect.moveBy(-location());
897 context->clip(IntRect(IntPoint(), visibleAreaWithScrollbars.size()));
899 paintScrollbars(context, scrollViewDirtyRect);
902 // Paint the panScroll Icon
903 if (m_drawPanScrollIcon)
904 paintPanScrollIcon(context);
907 void ScrollView::calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect)
909 int verticalScrollbarWidth = (verticalScrollbar() && !verticalScrollbar()->isOverlayScrollbar())
910 ? verticalScrollbar()->width() : 0;
911 int horizontalScrollbarHeight = (horizontalScrollbar() && !horizontalScrollbar()->isOverlayScrollbar())
912 ? horizontalScrollbar()->height() : 0;
914 int physicalScrollY = scrollPosition().y() + scrollOrigin().y();
915 if (physicalScrollY < 0) {
916 horizontalOverhangRect = frameRect();
917 horizontalOverhangRect.setHeight(-physicalScrollY);
918 horizontalOverhangRect.setWidth(horizontalOverhangRect.width() - verticalScrollbarWidth);
919 } else if (contentsHeight() && physicalScrollY > contentsHeight() - visibleHeight()) {
920 int height = physicalScrollY - (contentsHeight() - visibleHeight());
921 horizontalOverhangRect = frameRect();
922 horizontalOverhangRect.setY(frameRect().maxY() - height - horizontalScrollbarHeight);
923 horizontalOverhangRect.setHeight(height);
924 horizontalOverhangRect.setWidth(horizontalOverhangRect.width() - verticalScrollbarWidth);
927 int physicalScrollX = scrollPosition().x() + scrollOrigin().x();
928 if (physicalScrollX < 0) {
929 verticalOverhangRect.setWidth(-physicalScrollX);
930 verticalOverhangRect.setHeight(frameRect().height() - horizontalOverhangRect.height() - horizontalScrollbarHeight);
931 verticalOverhangRect.setX(frameRect().x());
932 if (horizontalOverhangRect.y() == frameRect().y())
933 verticalOverhangRect.setY(frameRect().y() + horizontalOverhangRect.height());
935 verticalOverhangRect.setY(frameRect().y());
936 } else if (contentsWidth() && physicalScrollX > contentsWidth() - visibleWidth()) {
937 int width = physicalScrollX - (contentsWidth() - visibleWidth());
938 verticalOverhangRect.setWidth(width);
939 verticalOverhangRect.setHeight(frameRect().height() - horizontalOverhangRect.height() - horizontalScrollbarHeight);
940 verticalOverhangRect.setX(frameRect().maxX() - width - verticalScrollbarWidth);
941 if (horizontalOverhangRect.y() == frameRect().y())
942 verticalOverhangRect.setY(frameRect().y() + horizontalOverhangRect.height());
944 verticalOverhangRect.setY(frameRect().y());
948 void ScrollView::updateOverhangAreas()
950 HostWindow* window = hostWindow();
954 IntRect horizontalOverhangRect;
955 IntRect verticalOverhangRect;
956 calculateOverhangAreasForPainting(horizontalOverhangRect, verticalOverhangRect);
957 if (!horizontalOverhangRect.isEmpty())
958 window->invalidateContentsAndRootView(horizontalOverhangRect);
959 if (!verticalOverhangRect.isEmpty())
960 window->invalidateContentsAndRootView(verticalOverhangRect);
963 void ScrollView::paintOverhangAreas(GraphicsContext* context, const IntRect& horizontalOverhangRect, const IntRect& verticalOverhangRect, const IntRect& dirtyRect)
965 ScrollbarTheme::theme()->paintOverhangBackground(context, horizontalOverhangRect, verticalOverhangRect, dirtyRect);
966 ScrollbarTheme::theme()->paintOverhangShadows(context, scrollOffset(), horizontalOverhangRect, verticalOverhangRect, dirtyRect);
969 void ScrollView::calculateAndPaintOverhangAreas(GraphicsContext* context, const IntRect& dirtyRect)
971 IntRect horizontalOverhangRect;
972 IntRect verticalOverhangRect;
973 calculateOverhangAreasForPainting(horizontalOverhangRect, verticalOverhangRect);
975 if (dirtyRect.intersects(horizontalOverhangRect) || dirtyRect.intersects(verticalOverhangRect))
976 paintOverhangAreas(context, horizontalOverhangRect, verticalOverhangRect, dirtyRect);
979 void ScrollView::calculateAndPaintOverhangBackground(GraphicsContext* context, const IntRect& dirtyRect)
981 IntRect horizontalOverhangRect;
982 IntRect verticalOverhangRect;
983 calculateOverhangAreasForPainting(horizontalOverhangRect, verticalOverhangRect);
985 if (dirtyRect.intersects(horizontalOverhangRect) || dirtyRect.intersects(verticalOverhangRect))
986 ScrollbarTheme::theme()->paintOverhangBackground(context, horizontalOverhangRect, verticalOverhangRect, dirtyRect);
989 bool ScrollView::isPointInScrollbarCorner(const IntPoint& windowPoint)
991 if (!scrollbarCornerPresent())
994 IntPoint viewPoint = convertFromContainingWindow(windowPoint);
996 if (m_horizontalScrollbar) {
997 int horizontalScrollbarYMin = m_horizontalScrollbar->frameRect().y();
998 int horizontalScrollbarYMax = m_horizontalScrollbar->frameRect().y() + m_horizontalScrollbar->frameRect().height();
999 int horizontalScrollbarXMin = m_horizontalScrollbar->frameRect().x() + m_horizontalScrollbar->frameRect().width();
1001 return viewPoint.y() > horizontalScrollbarYMin && viewPoint.y() < horizontalScrollbarYMax && viewPoint.x() > horizontalScrollbarXMin;
1004 int verticalScrollbarXMin = m_verticalScrollbar->frameRect().x();
1005 int verticalScrollbarXMax = m_verticalScrollbar->frameRect().x() + m_verticalScrollbar->frameRect().width();
1006 int verticalScrollbarYMin = m_verticalScrollbar->frameRect().y() + m_verticalScrollbar->frameRect().height();
1008 return viewPoint.x() > verticalScrollbarXMin && viewPoint.x() < verticalScrollbarXMax && viewPoint.y() > verticalScrollbarYMin;
1011 bool ScrollView::scrollbarCornerPresent() const
1013 return (m_horizontalScrollbar && width() - m_horizontalScrollbar->width() > 0)
1014 || (m_verticalScrollbar && height() - m_verticalScrollbar->height() > 0);
1017 IntRect ScrollView::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& localRect) const
1019 // Scrollbars won't be transformed within us
1020 IntRect newRect = localRect;
1021 newRect.moveBy(scrollbar->location());
1025 IntRect ScrollView::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
1027 IntRect newRect = parentRect;
1028 // Scrollbars won't be transformed within us
1029 newRect.moveBy(-scrollbar->location());
1033 // FIXME: test these on windows
1034 IntPoint ScrollView::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& localPoint) const
1036 // Scrollbars won't be transformed within us
1037 IntPoint newPoint = localPoint;
1038 newPoint.moveBy(scrollbar->location());
1042 IntPoint ScrollView::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
1044 IntPoint newPoint = parentPoint;
1045 // Scrollbars won't be transformed within us
1046 newPoint.moveBy(-scrollbar->location());
1050 void ScrollView::setParentVisible(bool visible)
1052 if (isParentVisible() == visible)
1055 Widget::setParentVisible(visible);
1057 if (!isSelfVisible())
1060 HashSet<RefPtr<Widget> >::iterator end = m_children.end();
1061 for (HashSet<RefPtr<Widget> >::iterator it = m_children.begin(); it != end; ++it)
1062 (*it)->setParentVisible(visible);
1065 void ScrollView::show()
1067 if (!isSelfVisible()) {
1068 setSelfVisible(true);
1069 if (isParentVisible()) {
1070 HashSet<RefPtr<Widget> >::iterator end = m_children.end();
1071 for (HashSet<RefPtr<Widget> >::iterator it = m_children.begin(); it != end; ++it)
1072 (*it)->setParentVisible(true);
1079 void ScrollView::hide()
1081 if (isSelfVisible()) {
1082 if (isParentVisible()) {
1083 HashSet<RefPtr<Widget> >::iterator end = m_children.end();
1084 for (HashSet<RefPtr<Widget> >::iterator it = m_children.begin(); it != end; ++it)
1085 (*it)->setParentVisible(false);
1087 setSelfVisible(false);
1093 void ScrollView::addPanScrollIcon(const IntPoint& iconPosition)
1095 HostWindow* window = hostWindow();
1098 m_drawPanScrollIcon = true;
1099 m_panScrollIconPoint = IntPoint(iconPosition.x() - panIconSizeLength / 2 , iconPosition.y() - panIconSizeLength / 2) ;
1100 window->invalidateContentsAndRootView(IntRect(m_panScrollIconPoint, IntSize(panIconSizeLength, panIconSizeLength)));
1103 void ScrollView::removePanScrollIcon()
1105 HostWindow* window = hostWindow();
1108 m_drawPanScrollIcon = false;
1109 window->invalidateContentsAndRootView(IntRect(m_panScrollIconPoint, IntSize(panIconSizeLength, panIconSizeLength)));
1112 void ScrollView::setScrollOrigin(const IntPoint& origin, bool updatePositionAtAll, bool updatePositionSynchronously)
1114 if (scrollOrigin() == origin)
1117 ScrollableArea::setScrollOrigin(origin);
1119 // Update if the scroll origin changes, since our position will be different if the content size did not change.
1120 if (updatePositionAtAll && updatePositionSynchronously)
1121 updateScrollbars(scrollOffset());
1124 } // namespace blink