2 * Copyright (C) 2008, 2011 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 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 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.
26 #ifndef ScrollableArea_h
27 #define ScrollableArea_h
29 #include "Scrollbar.h"
30 #include <wtf/Vector.h>
35 class GraphicsContext;
36 class PlatformGestureEvent;
37 class PlatformWheelEvent;
39 #if USE(ACCELERATED_COMPOSITING)
44 class ScrollableArea {
46 bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1);
47 void scrollToOffsetWithoutAnimation(const FloatPoint&);
48 void scrollToOffsetWithoutAnimation(ScrollbarOrientation, float offset);
50 // Should be called when the scroll position changes externally, for example if the scroll layer position
51 // is updated on the scrolling thread and we need to notify the main thread.
52 void notifyScrollPositionChanged(const IntPoint&);
54 // Allows subclasses to handle scroll position updates themselves. If this member function
55 // returns true, the scrollable area won't actually update the scroll position and instead
56 // expect it to happen sometime in the future.
57 virtual bool requestScrollPositionUpdate(const IntPoint&) { return false; }
59 bool handleWheelEvent(const PlatformWheelEvent&);
61 // Functions for controlling if you can scroll past the end of the document.
62 bool constrainsScrollingToContentEdge() const { return m_constrainsScrollingToContentEdge; }
63 void setConstrainsScrollingToContentEdge(bool constrainsScrollingToContentEdge) { m_constrainsScrollingToContentEdge = constrainsScrollingToContentEdge; }
65 void setVerticalScrollElasticity(ScrollElasticity scrollElasticity) { m_verticalScrollElasticity = scrollElasticity; }
66 ScrollElasticity verticalScrollElasticity() const { return static_cast<ScrollElasticity>(m_verticalScrollElasticity); }
68 void setHorizontalScrollElasticity(ScrollElasticity scrollElasticity) { m_horizontalScrollElasticity = scrollElasticity; }
69 ScrollElasticity horizontalScrollElasticity() const { return static_cast<ScrollElasticity>(m_horizontalScrollElasticity); }
71 bool inLiveResize() const { return m_inLiveResize; }
72 void willStartLiveResize();
73 void willEndLiveResize();
75 void contentAreaWillPaint() const;
76 void mouseEnteredContentArea() const;
77 void mouseExitedContentArea() const;
78 void mouseMovedInContentArea() const;
79 void mouseEnteredScrollbar(Scrollbar*) const;
80 void mouseExitedScrollbar(Scrollbar*) const;
81 void contentAreaDidShow() const;
82 void contentAreaDidHide() const;
84 void finishCurrentScrollAnimations() const;
86 void didAddVerticalScrollbar(Scrollbar*);
87 void willRemoveVerticalScrollbar(Scrollbar*);
88 virtual void didAddHorizontalScrollbar(Scrollbar*);
89 virtual void willRemoveHorizontalScrollbar(Scrollbar*);
91 virtual void contentsResized();
93 bool hasOverlayScrollbars() const;
94 virtual void setScrollbarOverlayStyle(ScrollbarOverlayStyle);
95 ScrollbarOverlayStyle scrollbarOverlayStyle() const { return static_cast<ScrollbarOverlayStyle>(m_scrollbarOverlayStyle); }
97 // This getter will create a ScrollAnimator if it doesn't already exist.
98 ScrollAnimator* scrollAnimator() const;
100 // This getter will return null if the ScrollAnimator hasn't been created yet.
101 ScrollAnimator* existingScrollAnimator() const { return m_scrollAnimator.get(); }
103 const IntPoint& scrollOrigin() const { return m_scrollOrigin; }
104 bool scrollOriginChanged() const { return m_scrollOriginChanged; }
106 virtual bool isActive() const = 0;
107 virtual int scrollSize(ScrollbarOrientation) const = 0;
108 virtual int scrollPosition(Scrollbar*) const = 0;
109 virtual void invalidateScrollbar(Scrollbar*, const IntRect&);
110 virtual bool isScrollCornerVisible() const = 0;
111 virtual IntRect scrollCornerRect() const = 0;
112 virtual void invalidateScrollCorner(const IntRect&);
113 virtual void getTickmarks(Vector<IntRect>&) const { }
115 // Convert points and rects between the scrollbar and its containing view.
116 // The client needs to implement these in order to be aware of layout effects
117 // like CSS transforms.
118 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
120 return scrollbar->Widget::convertToContainingView(scrollbarRect);
122 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
124 return scrollbar->Widget::convertFromContainingView(parentRect);
126 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
128 return scrollbar->Widget::convertToContainingView(scrollbarPoint);
130 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
132 return scrollbar->Widget::convertFromContainingView(parentPoint);
135 virtual Scrollbar* horizontalScrollbar() const { return 0; }
136 virtual Scrollbar* verticalScrollbar() const { return 0; }
138 virtual IntPoint scrollPosition() const;
139 virtual IntPoint minimumScrollPosition() const;
140 virtual IntPoint maximumScrollPosition() const;
142 enum VisibleContentRectIncludesScrollbars { ExcludeScrollbars, IncludeScrollbars };
143 virtual IntRect visibleContentRect(VisibleContentRectIncludesScrollbars = ExcludeScrollbars) const;
144 virtual int visibleHeight() const = 0;
145 virtual int visibleWidth() const = 0;
146 virtual IntSize contentsSize() const = 0;
147 virtual IntSize overhangAmount() const { return IntSize(); }
148 virtual IntPoint lastKnownMousePosition() const { return IntPoint(); }
150 virtual bool shouldSuspendScrollAnimations() const { return true; }
151 virtual void scrollbarStyleChanged(int /*newStyle*/, bool /*forceUpdate*/) { }
152 virtual void setVisibleScrollerThumbRect(const IntRect&) { }
154 virtual bool scrollbarsCanBeActive() const = 0;
156 // Note that this only returns scrollable areas that can actually be scrolled.
157 virtual ScrollableArea* enclosingScrollableArea() const = 0;
159 // Returns the bounding box of this scrollable area, in the coordinate system of the enclosing scroll view.
160 virtual IntRect scrollableAreaBoundingBox() const = 0;
162 virtual bool shouldRubberBandInDirection(ScrollDirection) const { return true; }
163 virtual bool isRubberBandInProgress() const { return false; }
165 virtual bool scrollAnimatorEnabled() const { return false; }
167 // NOTE: Only called from Internals for testing.
168 void setScrollOffsetFromInternals(const IntPoint&);
170 static IntPoint constrainScrollPositionForOverhang(const IntRect& visibleContentRect, const IntSize& contentsSize, const IntPoint& scrollPosition, const IntPoint& scrollOrigin);
171 IntPoint constrainScrollPositionForOverhang(const IntPoint& scrollPosition);
173 // Let subclasses provide a way of asking for and servicing scroll
175 virtual bool scheduleAnimation() { return false; }
176 void serviceScrollAnimations();
178 #if USE(ACCELERATED_COMPOSITING)
179 virtual TiledBacking* tiledBacking() { return 0; }
180 virtual bool usesCompositedScrolling() const { return false; }
183 virtual void reportMemoryUsage(MemoryObjectInfo*) const;
187 virtual ~ScrollableArea();
189 void setScrollOrigin(const IntPoint&);
190 void resetScrollOriginChanged() { m_scrollOriginChanged = false; }
192 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) = 0;
193 virtual void invalidateScrollCornerRect(const IntRect&) = 0;
195 #if USE(ACCELERATED_COMPOSITING)
196 virtual GraphicsLayer* layerForHorizontalScrollbar() const { return 0; }
197 virtual GraphicsLayer* layerForVerticalScrollbar() const { return 0; }
198 virtual GraphicsLayer* layerForScrollCorner() const { return 0; }
199 #if ENABLE(RUBBER_BANDING)
200 virtual GraphicsLayer* layerForOverhangAreas() const { return 0; }
203 bool hasLayerForHorizontalScrollbar() const;
204 bool hasLayerForVerticalScrollbar() const;
205 bool hasLayerForScrollCorner() const;
208 void scrollPositionChanged(const IntPoint&);
210 // NOTE: Only called from the ScrollAnimator.
211 friend class ScrollAnimator;
212 void setScrollOffsetFromAnimation(const IntPoint&);
214 // This function should be overriden by subclasses to perform the actual
215 // scroll of the content.
216 virtual void setScrollOffset(const IntPoint&) = 0;
218 mutable OwnPtr<ScrollAnimator> m_scrollAnimator;
219 unsigned m_constrainsScrollingToContentEdge : 1;
221 unsigned m_inLiveResize : 1;
223 unsigned m_verticalScrollElasticity : 2; // ScrollElasticity
224 unsigned m_horizontalScrollElasticity : 2; // ScrollElasticity
226 unsigned m_scrollbarOverlayStyle : 2; // ScrollbarOverlayStyle
228 unsigned m_scrollOriginChanged : 1;
230 // There are 8 possible combinations of writing mode and direction. Scroll origin will be non-zero in the x or y axis
231 // if there is any reversed direction or writing-mode. The combinations are:
232 // writing-mode / direction scrollOrigin.x() set scrollOrigin.y() set
233 // horizontal-tb / ltr NO NO
234 // horizontal-tb / rtl YES NO
235 // horizontal-bt / ltr NO YES
236 // horizontal-bt / rtl YES YES
237 // vertical-lr / ltr NO NO
238 // vertical-lr / rtl NO YES
239 // vertical-rl / ltr YES NO
240 // vertical-rl / rtl YES YES
241 IntPoint m_scrollOrigin;
244 } // namespace WebCore
246 #endif // ScrollableArea_h