2 * Copyright (C) 2004, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Holger Hans Peter Freyther
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "Scrollbar.h"
32 #include "ScrollableArea.h"
33 #include "ScrollTypes.h"
36 #include <wtf/HashSet.h>
38 #if PLATFORM(MAC) && defined __OBJC__
39 @protocol WebCoreFrameScrollView;
43 class wxScrollWinEvent;
51 class ScrollView : public Widget, public ScrollableArea {
55 // ScrollableArea functions. FrameView overrides the others.
56 virtual int scrollSize(ScrollbarOrientation orientation) const;
57 virtual int scrollPosition(Scrollbar*) const;
58 virtual void setScrollOffset(const IntPoint&);
59 virtual void didCompleteRubberBand(const IntSize&) const;
60 virtual void notifyPageThatContentAreaWillPaint() const;
61 virtual bool isScrollCornerVisible() const;
62 virtual void scrollbarStyleChanged();
64 // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
65 virtual void scrollTo(const IntSize& newOffset);
67 // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
68 // host window in the window's coordinate space.
69 virtual HostWindow* hostWindow() const = 0;
71 // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
72 virtual IntRect windowClipRect(bool clipToContents = true) const = 0;
74 // Functions for child manipulation and inspection.
75 const HashSet<RefPtr<Widget> >* children() const { return &m_children; }
76 void addChild(PassRefPtr<Widget>);
77 void removeChild(Widget*);
79 // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions
80 // can be used to obtain those scrollbars.
81 virtual Scrollbar* horizontalScrollbar() const { return m_horizontalScrollbar.get(); }
82 virtual Scrollbar* verticalScrollbar() const { return m_verticalScrollbar.get(); }
83 bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
85 void positionScrollbarLayers();
87 // Functions for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of
88 // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar.
89 // Auto means show a scrollbar only when one is needed.
90 // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native
91 // widget may choose not to honor the requested modes.
92 void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool horizontalLock = false, bool verticalLock = false);
93 void setHorizontalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(mode, verticalScrollbarMode(), lock, verticalScrollbarLock()); }
94 void setVerticalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(horizontalScrollbarMode(), mode, horizontalScrollbarLock(), lock); };
95 void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const;
96 ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; }
97 ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; }
99 void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; }
100 bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; }
101 void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; }
102 bool verticalScrollbarLock() const { return m_verticalScrollbarLock; }
104 void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; }
106 virtual void setCanHaveScrollbars(bool);
107 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; }
109 virtual bool avoidScrollbarCreation() const { return false; }
111 virtual void setScrollbarOverlayStyle(ScrollbarOverlayStyle);
113 // By default you only receive paint events for the area that is visible. In the case of using a
114 // tiled backing store, this function can be set, so that the view paints the entire contents.
115 bool paintsEntireContents() const { return m_paintsEntireContents; }
116 void setPaintsEntireContents(bool);
118 // By default, paint events are clipped to the visible area. If set to
119 // false, paint events are no longer clipped. paintsEntireContents() implies !clipsRepaints().
120 bool clipsRepaints() const { return m_clipsRepaints; }
121 void setClipsRepaints(bool);
123 // By default programmatic scrolling is handled by WebCore and not by the UI application.
124 // In the case of using a tiled backing store, this mode can be set, so that the scroll requests
125 // are delegated to the UI application.
126 bool delegatesScrolling() const { return m_delegatesScrolling; }
127 void setDelegatesScrolling(bool);
129 // Overridden by FrameView to create custom CSS scrollbars if applicable.
130 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
132 // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off.
133 void setProhibitsScrolling(bool b) { m_prohibitsScrolling = b; }
134 bool prohibitsScrolling() const { return m_prohibitsScrolling; }
136 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
137 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
138 void setCanBlitOnScroll(bool);
139 bool canBlitOnScroll() const;
141 // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
142 // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
144 // In the situation the client is responsible for the scrolling (ie. with a tiled backing store) it is possible to use
145 // the setFixedVisibleContentRect instead for the mainframe, though this must be updated manually, e.g just before resuming the page
146 // which usually will happen when panning, pinching and rotation ends, or when scale or position are changed manually.
147 #if ENABLE(TIZEN_CAIRO_SCALE_PATCH)
148 virtual IntRect visibleContentRect(bool includeScrollbars = false, bool useScale = true) const;
150 virtual IntRect visibleContentRect(bool includeScrollbars = false) const;
152 void setFixedVisibleContentRect(const IntRect& visibleContentRect) { m_fixedVisibleContentRect = visibleContentRect; }
153 LayoutUnit visibleWidth() const { return visibleContentRect().width(); }
154 LayoutUnit visibleHeight() const { return visibleContentRect().height(); }
156 // Functions for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible
157 // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead.
158 int layoutWidth() const;
159 int layoutHeight() const;
160 IntSize fixedLayoutSize() const;
161 void setFixedLayoutSize(const IntSize&);
162 bool useFixedLayout() const;
163 void setUseFixedLayout(bool enable);
165 // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
167 IntSize contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight().
168 int contentsWidth() const { return contentsSize().width(); }
169 int contentsHeight() const { return contentsSize().height(); }
170 virtual void setContentsSize(const IntSize&);
172 // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
173 IntPoint scrollPosition() const { return visibleContentRect().location(); }
174 IntSize scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
175 IntPoint maximumScrollPosition() const; // The maximum position we can be scrolled to.
176 IntPoint minimumScrollPosition() const; // The minimum position we can be scrolled to.
177 // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
178 IntPoint adjustScrollPositionWithinRange(const IntPoint&) const;
179 int scrollX() const { return scrollPosition().x(); }
180 int scrollY() const { return scrollPosition().y(); }
182 IntSize overhangAmount() const;
183 #if PLATFORM(EFL) && ENABLE(TIZEN_CAIRO_SCALE_PATCH) && ENABLE(TIZEN_PAGE_CACHE)
184 void cacheCurrentScrollPosition() { m_cachedScrollPosition = visibleContentRect(false, false).location(); }
186 void cacheCurrentScrollPosition() { m_cachedScrollPosition = scrollPosition(); }
188 IntPoint cachedScrollPosition() const { return m_cachedScrollPosition; }
190 // Functions for scrolling the view.
191 void setScrollPosition(const IntPoint&);
192 #if ENABLE(TIZEN_CAIRO_SCALE_PATCH)
193 void scrollBy(const IntSize& s);
195 void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
198 // This function scrolls by lines, pages or pixels.
199 bool scroll(ScrollDirection, ScrollGranularity);
201 // A logical scroll that just ends up calling the corresponding physical scroll() based off the document's writing mode.
202 bool logicalScroll(ScrollLogicalDirection, ScrollGranularity);
204 // Scroll the actual contents of the view (either blitting or invalidating as needed).
205 void scrollContents(const IntSize& scrollDelta);
207 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
208 void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
209 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
211 IntPoint rootViewToContents(const IntPoint&) const;
212 IntPoint contentsToRootView(const IntPoint&) const;
213 IntRect rootViewToContents(const IntRect&) const;
214 IntRect contentsToRootView(const IntRect&) const;
216 // Event coordinates are assumed to be in the coordinate space of a window that contains
217 // the entire widget hierarchy. It is up to the platform to decide what the precise definition
218 // of containing window is. (For example on Mac it is the containing NSWindow.)
219 IntPoint windowToContents(const IntPoint&) const;
220 IntPoint contentsToWindow(const IntPoint&) const;
221 IntRect windowToContents(const IntRect&) const;
222 IntRect contentsToWindow(const IntRect&) const;
224 // Functions for converting to and from screen coordinates.
225 IntRect contentsToScreen(const IntRect&) const;
226 IntPoint screenToContents(const IntPoint&) const;
228 // 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
229 // we know that we are either not in a window right now or if that window is not visible.
230 bool isOffscreen() const;
232 #if ENABLE(TIZEN_CAIRO_SCALE_PATCH)
233 void setScaleFactor(float scaleFactor) { m_scaleFactor = scaleFactor; }
234 float scaleFactor() const { return m_scaleFactor; }
237 // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
239 virtual IntRect windowResizerRect() const { return IntRect(); }
240 bool containsScrollbarsAvoidingResizer() const;
241 void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
242 void windowResizerRectChanged();
244 virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count.
246 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
247 virtual void frameRectsChanged();
249 // Widget override to update our scrollbars and notify our contents of the resize.
250 virtual void setFrameRect(const IntRect&);
252 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
253 Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
255 // This function exists for scrollviews that need to handle wheel events manually.
256 // On Mac the underlying NSScrollView just does the scrolling, but on other platforms
257 // (like Windows), we need this function in order to do the scroll ourselves.
258 bool wheelEvent(const PlatformWheelEvent&);
259 #if ENABLE(GESTURE_EVENTS)
260 void gestureEvent(const PlatformGestureEvent&);
263 IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const
265 IntPoint newPoint = point;
266 if (!isScrollViewScrollbar(child))
267 newPoint = point - scrollOffset();
268 newPoint.moveBy(child->location());
272 IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const
274 IntPoint newPoint = point;
275 if (!isScrollViewScrollbar(child))
276 newPoint = point + scrollOffset();
277 newPoint.moveBy(-child->location());
281 // Widget override. Handles painting of the contents of the view as well as the scrollbars.
282 virtual void paint(GraphicsContext*, const IntRect&);
283 void paintScrollbars(GraphicsContext*, const IntRect&);
285 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
288 virtual void setParentVisible(bool);
291 static const int noPanScrollRadius = 15;
292 void addPanScrollIcon(const IntPoint&);
293 void removePanScrollIcon();
294 void paintPanScrollIcon(GraphicsContext*);
296 virtual bool isPointInScrollbarCorner(const IntPoint&);
297 virtual bool scrollbarCornerPresent() const;
298 virtual IntRect scrollCornerRect() const;
299 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
301 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const;
302 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const;
303 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const;
304 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const;
306 bool containsScrollableAreaWithOverlayScrollbars() const { return m_containsScrollableAreaWithOverlayScrollbars; }
307 void setContainsScrollableAreaWithOverlayScrollbars(bool contains) { m_containsScrollableAreaWithOverlayScrollbars = contains; }
309 void calculateAndPaintOverhangAreas(GraphicsContext*, const IntRect& dirtyRect);
314 virtual void repaintContentRectangle(const IntRect&, bool now = false);
315 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
317 void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
318 virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
320 virtual void contentsResized() = 0;
321 virtual void visibleContentsResized() = 0;
323 IntRect fixedVisibleContentRect() const { return m_fixedVisibleContentRect; }
325 // These functions are used to create/destroy scrollbars.
326 void setHasHorizontalScrollbar(bool);
327 void setHasVerticalScrollbar(bool);
329 virtual void updateScrollCorner();
330 virtual void invalidateScrollCornerRect(const IntRect&);
332 // Scroll the content by blitting the pixels.
333 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect);
334 // Scroll the content by invalidating everything.
335 virtual void scrollContentsSlowPath(const IntRect& updateRect);
337 void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
338 IntPoint scrollOrigin() const { return m_scrollOrigin; }
340 // Subclassed by FrameView to check the writing-mode of the document.
341 virtual bool isVerticalDocument() const { return true; }
342 virtual bool isFlippedDocument() const { return false; }
345 RefPtr<Scrollbar> m_horizontalScrollbar;
346 RefPtr<Scrollbar> m_verticalScrollbar;
347 ScrollbarMode m_horizontalScrollbarMode;
348 ScrollbarMode m_verticalScrollbarMode;
350 bool m_horizontalScrollbarLock;
351 bool m_verticalScrollbarLock;
353 bool m_prohibitsScrolling;
355 HashSet<RefPtr<Widget> > m_children;
357 // This bool is unused on Mac OS because we directly ask the platform widget
358 // whether it is safe to blit on scroll.
359 bool m_canBlitOnScroll;
361 IntRect m_fixedVisibleContentRect;
362 IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
363 IntPoint m_cachedScrollPosition;
364 IntSize m_fixedLayoutSize;
365 IntSize m_contentsSize;
367 int m_scrollbarsAvoidingResizer;
368 bool m_scrollbarsSuppressed;
370 bool m_inUpdateScrollbars;
371 unsigned m_updateScrollbarsPass;
373 IntPoint m_panScrollIconPoint;
374 bool m_drawPanScrollIcon;
375 bool m_useFixedLayout;
377 bool m_paintsEntireContents;
378 bool m_clipsRepaints;
379 bool m_delegatesScrolling;
381 bool m_containsScrollableAreaWithOverlayScrollbars;
383 #if ENABLE(TIZEN_CAIRO_SCALE_PATCH)
390 // Called to update the scrollbars to accurately reflect the state of the view.
391 void updateScrollbars(const IntSize& desiredOffset);
392 IntRect rectToCopyOnScroll() const;
394 // Called when the scroll position within this view changes. FrameView overrides this to generate repaint invalidations.
395 virtual void repaintFixedElementsAfterScrolling() {}
398 void platformDestroy();
399 void platformAddChild(Widget*);
400 void platformRemoveChild(Widget*);
401 void platformSetScrollbarModes();
402 void platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const;
403 void platformSetCanBlitOnScroll(bool);
404 bool platformCanBlitOnScroll() const;
405 IntRect platformVisibleContentRect(bool includeScrollbars) const;
406 void platformSetContentsSize();
407 IntRect platformContentsToScreen(const IntRect&) const;
408 IntPoint platformScreenToContents(const IntPoint&) const;
409 void platformSetScrollPosition(const IntPoint&);
410 bool platformScroll(ScrollDirection, ScrollGranularity);
411 void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress);
412 void platformRepaintContentRectangle(const IntRect&, bool now);
413 bool platformIsOffscreen() const;
414 void platformSetScrollbarOverlayStyle(ScrollbarOverlayStyle);
416 void platformSetScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
418 #if PLATFORM(MAC) && defined __OBJC__
420 NSView* documentView() const;
423 NSScrollView<WebCoreFrameScrollView>* scrollView() const;
428 virtual void setPlatformWidget(wxWindow*);
429 void adjustScrollbars(int x = -1, int y = -1, bool refresh = true);
431 class ScrollViewPrivate;
432 ScrollViewPrivate* m_data;
435 }; // class ScrollView
437 } // namespace WebCore
439 #endif // ScrollView_h