1 /*
2 * Copyright (C) 2004, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Holger Hans Peter Freyther
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef ScrollView_h
28 #define ScrollView_h
29
30 #include "platform/PlatformExport.h"
31 #include "platform/Widget.h"
32 #include "platform/geometry/IntRect.h"
33 #include "platform/scroll/ScrollTypes.h"
34 #include "platform/scroll/ScrollableArea.h"
35 #include "platform/scroll/Scrollbar.h"
36
37 #include "wtf/HashSet.h"
38
39 namespace WebCore {
40
41 class HostWindow;
42 class Scrollbar;
43
44 class PLATFORM_EXPORT ScrollView : public Widget, public ScrollableArea {
45 public:
46 ~ScrollView();
47
48 // ScrollableArea functions.
49 virtual int scrollSize(ScrollbarOrientation) const OVERRIDE;
50 virtual void setScrollOffset(const IntPoint&) OVERRIDE;
51 virtual bool isScrollCornerVisible() const OVERRIDE;
52 virtual void scrollbarStyleChanged(int newStyle, bool forceUpdate) OVERRIDE;
53 virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE;
54 virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE;
55
56 virtual void notifyPageThatContentAreaWillPaint() const;
57
58 // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
59 virtual void scrollTo(const IntSize& newOffset);
60
61 // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
62 // host window in the window's coordinate space.
63 virtual HostWindow* hostWindow() const = 0;
64
65 // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
66 virtual IntRect windowClipRect(bool clipToContents = true) const = 0;
67
68 // Functions for child manipulation and inspection.
children()69 const HashSet<RefPtr<Widget> >* children() const { return &m_children; }
70 virtual void addChild(PassRefPtr<Widget>);
71 virtual void removeChild(Widget*);
72
73 // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions
74 // can be used to obtain those scrollbars.
horizontalScrollbar()75 virtual Scrollbar* horizontalScrollbar() const OVERRIDE { return m_horizontalScrollbar.get(); }
verticalScrollbar()76 virtual Scrollbar* verticalScrollbar() const OVERRIDE { return m_verticalScrollbar.get(); }
isScrollViewScrollbar(const Widget * child)77 bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
78
79 void positionScrollbarLayers();
80
81 // Functions for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of
82 // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar.
83 // Auto means show a scrollbar only when one is needed.
84 // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native
85 // widget may choose not to honor the requested modes.
86 void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool horizontalLock = false, bool verticalLock = false);
87 void setHorizontalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(mode, verticalScrollbarMode(), lock, verticalScrollbarLock()); }
88 void setVerticalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(horizontalScrollbarMode(), mode, horizontalScrollbarLock(), lock); };
89 void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const;
horizontalScrollbarMode()90 ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; }
verticalScrollbarMode()91 ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; }
92
93 void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; }
horizontalScrollbarLock()94 bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; }
95 void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; }
verticalScrollbarLock()96 bool verticalScrollbarLock() const { return m_verticalScrollbarLock; }
97
98 void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; }
99
100 virtual void setCanHaveScrollbars(bool);
canHaveScrollbars()101 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; }
102
103 // By default you only receive paint events for the area that is visible. In the case of using a
104 // tiled backing store, this function can be set, so that the view paints the entire contents.
paintsEntireContents()105 bool paintsEntireContents() const { return m_paintsEntireContents; }
106 void setPaintsEntireContents(bool);
107
108 // By default, paint events are clipped to the visible area. If set to
109 // false, paint events are no longer clipped. paintsEntireContents() implies !clipsRepaints().
clipsRepaints()110 bool clipsRepaints() const { return m_clipsRepaints; }
111 void setClipsRepaints(bool);
112
113 // Overridden by FrameView to create custom CSS scrollbars if applicable.
114 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
115
116 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
117 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
118 void setCanBlitOnScroll(bool);
119 bool canBlitOnScroll() const;
120
121 // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
122 // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
123 // included.
124 virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
visibleSize()125 IntSize visibleSize() const { return visibleContentRect().size(); }
visibleWidth()126 virtual int visibleWidth() const OVERRIDE { return visibleContentRect().width(); }
visibleHeight()127 virtual int visibleHeight() const OVERRIDE { return visibleContentRect().height(); }
128
129 // visibleContentRect().size() is computed from unscaledVisibleContentSize() divided by the value of visibleContentScaleFactor.
130 // For the main frame, visibleContentScaleFactor is equal to the page's pageScaleFactor; it's 1 otherwise.
131 IntSize unscaledVisibleContentSize(IncludeScrollbarsInRect = ExcludeScrollbars) const;
visibleContentScaleFactor()132 virtual float visibleContentScaleFactor() const { return 1; }
133
134 // Offset used to convert incoming input events while emulating device metics.
inputEventsOffsetForEmulation()135 virtual IntSize inputEventsOffsetForEmulation() const { return IntSize(); }
136
137 // Scale used to convert incoming input events. Usually the same as visibleContentScaleFactor(), unless specifically changed.
inputEventsScaleFactor()138 virtual float inputEventsScaleFactor() const { return visibleContentScaleFactor(); }
139
140 // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
141 // values).
142 virtual IntSize contentsSize() const OVERRIDE; // Always at least as big as the visibleWidth()/visibleHeight().
contentsWidth()143 int contentsWidth() const { return contentsSize().width(); }
contentsHeight()144 int contentsHeight() const { return contentsSize().height(); }
145 virtual void setContentsSize(const IntSize&);
146
147 // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
scrollPosition()148 virtual IntPoint scrollPosition() const OVERRIDE { return visibleContentRect().location(); }
scrollOffset()149 IntSize scrollOffset() const { return toIntSize(visibleContentRect().location()); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
150 virtual IntPoint maximumScrollPosition() const OVERRIDE; // The maximum position we can be scrolled to.
151 virtual IntPoint minimumScrollPosition() const OVERRIDE; // The minimum position we can be scrolled to.
152 // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
153 IntPoint adjustScrollPositionWithinRange(const IntPoint&) const;
scrollX()154 int scrollX() const { return scrollPosition().x(); }
scrollY()155 int scrollY() const { return scrollPosition().y(); }
156
157 virtual IntSize overhangAmount() const OVERRIDE;
158
cacheCurrentScrollPosition()159 void cacheCurrentScrollPosition() { m_cachedScrollPosition = scrollPosition(); }
cachedScrollPosition()160 IntPoint cachedScrollPosition() const { return m_cachedScrollPosition; }
161
162 // Functions for scrolling the view.
163 virtual void setScrollPosition(const IntPoint&);
scrollBy(const IntSize & s)164 void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
165
166 bool scroll(ScrollDirection, ScrollGranularity);
167
168 // Scroll the actual contents of the view (either blitting or invalidating as needed).
169 void scrollContents(const IntSize& scrollDelta);
170
171 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
172 void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
scrollbarsSuppressed()173 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
174
175 IntPoint rootViewToContents(const IntPoint&) const;
176 IntPoint contentsToRootView(const IntPoint&) const;
177 IntRect rootViewToContents(const IntRect&) const;
178 IntRect contentsToRootView(const IntRect&) const;
179
180 // Event coordinates are assumed to be in the coordinate space of a window that contains
181 // the entire widget hierarchy. It is up to the platform to decide what the precise definition
182 // of containing window is. (For example on Mac it is the containing NSWindow.)
183 IntPoint windowToContents(const IntPoint&) const;
184 IntPoint contentsToWindow(const IntPoint&) const;
185 IntRect windowToContents(const IntRect&) const;
186 IntRect contentsToWindow(const IntRect&) const;
187
188 // Functions for converting to and from screen coordinates.
189 IntRect contentsToScreen(const IntRect&) const;
190 IntPoint screenToContents(const IntPoint&) const;
191
192 // The purpose of this function is to answer whether or not the scroll view is currently visible. Animations and painting updates can be suspended if
193 // we know that we are either not in a window right now or if that window is not visible.
194 bool isOffscreen() const;
195
196 // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
197 // for example.
windowResizerRect()198 virtual IntRect windowResizerRect() const { return IntRect(); }
199 bool containsScrollbarsAvoidingResizer() const;
200 void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
201 void windowResizerRectChanged();
202
203 virtual void setParent(Widget*) OVERRIDE; // Overridden to update the overlapping scrollbar count.
204
205 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
206 virtual void frameRectsChanged();
207
208 // Widget override to update our scrollbars and notify our contents of the resize.
209 virtual void setFrameRect(const IntRect&);
210
211 // Widget override to notify our contents of a cliprect change.
212 virtual void clipRectChanged() OVERRIDE;
213
214 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
215 Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
216
convertChildToSelf(const Widget * child,const IntPoint & point)217 virtual IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const
218 {
219 IntPoint newPoint = point;
220 if (!isScrollViewScrollbar(child))
221 newPoint = point - scrollOffset();
222 newPoint.moveBy(child->location());
223 return newPoint;
224 }
225
convertSelfToChild(const Widget * child,const IntPoint & point)226 virtual IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const
227 {
228 IntPoint newPoint = point;
229 if (!isScrollViewScrollbar(child))
230 newPoint = point + scrollOffset();
231 newPoint.moveBy(-child->location());
232 return newPoint;
233 }
234
235 // Widget override. Handles painting of the contents of the view as well as the scrollbars.
236 virtual void paint(GraphicsContext*, const IntRect&);
237 void paintScrollbars(GraphicsContext*, const IntRect&);
238
239 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
240 virtual void show();
241 virtual void hide();
242 virtual void setParentVisible(bool);
243
244 // Pan scrolling.
245 static const int noPanScrollRadius = 15;
246 void addPanScrollIcon(const IntPoint&);
247 void removePanScrollIcon();
248 void paintPanScrollIcon(GraphicsContext*);
249
250 virtual bool isPointInScrollbarCorner(const IntPoint&);
251 virtual bool scrollbarCornerPresent() const;
252 virtual IntRect scrollCornerRect() const;
253 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
254 virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&);
255
256 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const OVERRIDE;
257 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const OVERRIDE;
258 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const OVERRIDE;
259 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const OVERRIDE;
260
261 void calculateAndPaintOverhangAreas(GraphicsContext*, const IntRect& dirtyRect);
262 void calculateAndPaintOverhangBackground(GraphicsContext*, const IntRect& dirtyRect);
263
isScrollView()264 virtual bool isScrollView() const OVERRIDE { return true; }
265
266 protected:
267 ScrollView();
268
269 virtual void repaintContentRectangle(const IntRect&);
270 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
271
272 virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
273
274 virtual void scrollbarExistenceDidChange() = 0;
275 // These functions are used to create/destroy scrollbars.
276 void setHasHorizontalScrollbar(bool);
277 void setHasVerticalScrollbar(bool);
278
279 virtual void updateScrollCorner();
280 virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE;
281
282 // Scroll the content by blitting the pixels.
283 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect);
284 // Scroll the content by invalidating everything.
285 virtual void scrollContentsSlowPath(const IntRect& updateRect);
286
287 void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
288
289 // Subclassed by FrameView to check the writing-mode of the document.
isVerticalDocument()290 virtual bool isVerticalDocument() const { return true; }
isFlippedDocument()291 virtual bool isFlippedDocument() const { return false; }
292
293 // Called to update the scrollbars to accurately reflect the state of the view.
294 void updateScrollbars(const IntSize& desiredOffset);
295
296 IntSize excludeScrollbars(const IntSize&) const;
297
298 private:
299 RefPtr<Scrollbar> m_horizontalScrollbar;
300 RefPtr<Scrollbar> m_verticalScrollbar;
301 ScrollbarMode m_horizontalScrollbarMode;
302 ScrollbarMode m_verticalScrollbarMode;
303
304 bool m_horizontalScrollbarLock;
305 bool m_verticalScrollbarLock;
306
307 HashSet<RefPtr<Widget> > m_children;
308
309 // This bool is unused on Mac OS because we directly ask the platform widget
310 // whether it is safe to blit on scroll.
311 bool m_canBlitOnScroll;
312
313 IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
314 IntPoint m_cachedScrollPosition;
315 IntSize m_contentsSize;
316
317 int m_scrollbarsAvoidingResizer;
318 bool m_scrollbarsSuppressed;
319
320 bool m_inUpdateScrollbars;
321 unsigned m_updateScrollbarsPass;
322
323 IntPoint m_panScrollIconPoint;
324 bool m_drawPanScrollIcon;
325
326 bool m_paintsEntireContents;
327 bool m_clipsRepaints;
328
329 void init();
330 void destroy();
331
332 IntRect rectToCopyOnScroll() const;
333
334 // Called when the scroll position within this view changes. FrameView overrides this to generate repaint invalidations.
repaintFixedElementsAfterScrolling()335 virtual void repaintFixedElementsAfterScrolling() { }
updateFixedElementsAfterScrolling()336 virtual void updateFixedElementsAfterScrolling() { }
337
338 void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
339 void updateOverhangAreas();
340
341 int pageStep(ScrollbarOrientation) const;
342 }; // class ScrollView
343
toScrollView(Widget * widget)344 inline ScrollView* toScrollView(Widget* widget)
345 {
346 ASSERT_WITH_SECURITY_IMPLICATION(!widget || widget->isScrollView());
347 return static_cast<ScrollView*>(widget);
348 }
349
toScrollView(const Widget * widget)350 inline const ScrollView* toScrollView(const Widget* widget)
351 {
352 ASSERT_WITH_SECURITY_IMPLICATION(!widget || widget->isScrollView());
353 return static_cast<const ScrollView*>(widget);
354 }
355
356 // This will catch anyone doing an unnecessary cast.
357 void toScrollView(const ScrollView*);
358
359 } // namespace WebCore
360
361 #endif // ScrollView_h
362