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 "ScrollbarClient.h"
33 #include "ScrollTypes.h"
36 #include <wtf/HashSet.h>
38 #if PLATFORM(MAC) && defined __OBJC__
39 @protocol WebCoreFrameScrollView;
43 #include "GRefPtrGtk.h"
44 typedef struct _GtkAdjustment GtkAdjustment;
48 class wxScrollWinEvent;
54 class PlatformWheelEvent;
57 class ScrollView : public Widget, public ScrollbarClient {
61 // ScrollbarClient functions. FrameView overrides the others.
62 virtual int scrollSize(ScrollbarOrientation orientation) const;
63 virtual void setScrollOffsetFromAnimation(const IntPoint&);
64 virtual void valueChanged(Scrollbar*);
65 virtual void valueChanged(const IntSize&);
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 Scrollbar* horizontalScrollbar() const { return m_horizontalScrollbar.get(); }
82 Scrollbar* verticalScrollbar() const { return m_verticalScrollbar.get(); }
83 bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
85 // Functions for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of
86 // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar.
87 // Auto means show a scrollbar only when one is needed.
88 // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native
89 // widget may choose not to honor the requested modes.
90 void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool horizontalLock = false, bool verticalLock = false);
91 void setHorizontalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(mode, verticalScrollbarMode(), lock, verticalScrollbarLock()); }
92 void setVerticalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(horizontalScrollbarMode(), mode, horizontalScrollbarLock(), lock); };
93 void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const;
94 ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; }
95 ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; }
97 void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; }
98 bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; }
99 void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; }
100 bool verticalScrollbarLock() const { return m_verticalScrollbarLock; }
102 void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; }
104 virtual void setCanHaveScrollbars(bool);
105 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; }
107 virtual bool avoidScrollbarCreation() const { return false; }
109 // By default you only receive paint events for the area that is visible. In the case of using a
110 // tiled backing store, this function can be set, so that the view paints the entire contents.
111 bool paintsEntireContents() const { return m_paintsEntireContents; }
112 void setPaintsEntireContents(bool);
114 // By default, paint events are clipped to the visible area. If set to
115 // false, paint events are no longer clipped. paintsEntireContents() implies !clipsRepaints().
116 bool clipsRepaints() const { return m_clipsRepaints; }
117 void setClipsRepaints(bool);
119 // By default programmatic scrolling is handled by WebCore and not by the UI application.
120 // In the case of using a tiled backing store, this mode can be set, so that the scroll requests
121 // are delegated to the UI application.
122 bool delegatesScrolling() const { return m_delegatesScrolling; }
123 void setDelegatesScrolling(bool);
125 // Overridden by FrameView to create custom CSS scrollbars if applicable.
126 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
128 // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off.
129 void setProhibitsScrolling(bool b) { m_prohibitsScrolling = b; }
130 bool prohibitsScrolling() const { return m_prohibitsScrolling; }
132 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
133 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
134 void setCanBlitOnScroll(bool);
135 bool canBlitOnScroll() const;
137 // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
138 // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
140 // In the situation the client is responsible for the scrolling (ie. with a tiled backing store) it is possible to use
141 // the actualVisibleContentRect instead, though this must be updated manually, e.g after panning ends.
142 IntRect visibleContentRect(bool includeScrollbars = false) const;
143 IntRect actualVisibleContentRect() const { return m_actualVisibleContentRect.isEmpty() ? visibleContentRect() : m_actualVisibleContentRect; }
144 void setActualVisibleContentRect(const IntRect& actualVisibleContentRect) { m_actualVisibleContentRect = actualVisibleContentRect; }
145 int visibleWidth() const { return visibleContentRect().width(); }
146 int visibleHeight() const { return visibleContentRect().height(); }
148 // Functions for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible
149 // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead.
150 int layoutWidth() const;
151 int layoutHeight() const;
152 IntSize fixedLayoutSize() const;
153 void setFixedLayoutSize(const IntSize&);
154 bool useFixedLayout() const;
155 void setUseFixedLayout(bool enable);
157 // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
159 IntSize contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight().
160 int contentsWidth() const { return contentsSize().width(); }
161 int contentsHeight() const { return contentsSize().height(); }
162 virtual void setContentsSize(const IntSize&);
164 #if PLATFORM(ANDROID)
165 int actualWidth() const;
166 int actualHeight() const;
167 int actualScrollX() const;
168 int actualScrollY() const;
171 // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
172 IntPoint scrollPosition() const { return visibleContentRect().location(); }
173 IntSize scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
174 IntPoint maximumScrollPosition() const; // The maximum position we can be scrolled to.
175 IntPoint minimumScrollPosition() const; // The minimum position we can be scrolled to.
176 // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
177 IntPoint adjustScrollPositionWithinRange(const IntPoint&) const;
178 int scrollX() const { return scrollPosition().x(); }
179 int scrollY() const { return scrollPosition().y(); }
181 // Functions for scrolling the view.
182 void setScrollPosition(const IntPoint&);
183 void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
185 // This function scrolls by lines, pages or pixels.
186 bool scroll(ScrollDirection, ScrollGranularity);
188 // A logical scroll that just ends up calling the corresponding physical scroll() based off the document's writing mode.
189 bool logicalScroll(ScrollLogicalDirection, ScrollGranularity);
191 // Scroll the actual contents of the view (either blitting or invalidating as needed).
192 void scrollContents(const IntSize& scrollDelta);
194 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
195 void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
196 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
198 // Event coordinates are assumed to be in the coordinate space of a window that contains
199 // the entire widget hierarchy. It is up to the platform to decide what the precise definition
200 // of containing window is. (For example on Mac it is the containing NSWindow.)
201 IntPoint windowToContents(const IntPoint&) const;
202 IntPoint contentsToWindow(const IntPoint&) const;
203 IntRect windowToContents(const IntRect&) const;
204 IntRect contentsToWindow(const IntRect&) const;
206 // Functions for converting to and from screen coordinates.
207 IntRect contentsToScreen(const IntRect&) const;
208 IntPoint screenToContents(const IntPoint&) const;
210 // 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
211 // we know that we are either not in a window right now or if that window is not visible.
212 bool isOffscreen() const;
214 // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
216 virtual IntRect windowResizerRect() const { return IntRect(); }
217 bool containsScrollbarsAvoidingResizer() const;
218 void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
219 void windowResizerRectChanged();
221 virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count.
223 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
224 virtual void frameRectsChanged();
226 // Widget override to update our scrollbars and notify our contents of the resize.
227 virtual void setFrameRect(const IntRect&);
229 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
230 Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
232 // This function exists for scrollviews that need to handle wheel events manually.
233 // On Mac the underlying NSScrollView just does the scrolling, but on other platforms
234 // (like Windows), we need this function in order to do the scroll ourselves.
235 void wheelEvent(PlatformWheelEvent&);
237 IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const
239 IntPoint newPoint = point;
240 if (!isScrollViewScrollbar(child))
241 newPoint = point - scrollOffset();
242 newPoint.move(child->x(), child->y());
246 IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const
248 IntPoint newPoint = point;
249 if (!isScrollViewScrollbar(child))
250 newPoint = point + scrollOffset();
251 newPoint.move(-child->x(), -child->y());
255 // Widget override. Handles painting of the contents of the view as well as the scrollbars.
256 virtual void paint(GraphicsContext*, const IntRect&);
257 void paintScrollbars(GraphicsContext*, const IntRect&);
259 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
262 virtual void setParentVisible(bool);
265 static const int noPanScrollRadius = 15;
266 void addPanScrollIcon(const IntPoint&);
267 void removePanScrollIcon();
268 void paintPanScrollIcon(GraphicsContext*);
270 virtual bool isPointInScrollbarCorner(const IntPoint&);
271 virtual bool scrollbarCornerPresent() const;
273 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const;
274 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const;
275 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const;
276 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const;
281 virtual void repaintContentRectangle(const IntRect&, bool now = false);
282 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
284 virtual void contentsResized() = 0;
285 virtual void visibleContentsResized() = 0;
287 // These functions are used to create/destroy scrollbars.
288 void setHasHorizontalScrollbar(bool);
289 void setHasVerticalScrollbar(bool);
291 IntRect scrollCornerRect() const;
292 virtual void updateScrollCorner();
293 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
295 // Scroll the content by blitting the pixels.
296 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect);
297 // Scroll the content by invalidating everything.
298 virtual void scrollContentsSlowPath(const IntRect& updateRect);
300 void setScrollOrigin(const IntPoint&, bool updatePosition);
301 IntPoint scrollOrigin() { return m_scrollOrigin; }
303 // Subclassed by FrameView to check the writing-mode of the document.
304 virtual bool isVerticalDocument() const { return true; }
305 virtual bool isFlippedDocument() const { return false; }
308 RefPtr<Scrollbar> m_horizontalScrollbar;
309 RefPtr<Scrollbar> m_verticalScrollbar;
310 ScrollbarMode m_horizontalScrollbarMode;
311 ScrollbarMode m_verticalScrollbarMode;
313 bool m_horizontalScrollbarLock;
314 bool m_verticalScrollbarLock;
316 bool m_prohibitsScrolling;
318 HashSet<RefPtr<Widget> > m_children;
320 // This bool is unused on Mac OS because we directly ask the platform widget
321 // whether it is safe to blit on scroll.
322 bool m_canBlitOnScroll;
324 IntRect m_actualVisibleContentRect;
325 IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
326 IntSize m_fixedLayoutSize;
327 IntSize m_contentsSize;
329 int m_scrollbarsAvoidingResizer;
330 bool m_scrollbarsSuppressed;
332 bool m_inUpdateScrollbars;
333 unsigned m_updateScrollbarsPass;
335 IntPoint m_panScrollIconPoint;
336 bool m_drawPanScrollIcon;
337 bool m_useFixedLayout;
339 bool m_paintsEntireContents;
340 bool m_clipsRepaints;
341 bool m_delegatesScrolling;
343 // There are 8 possible combinations of writing mode and direction. Scroll origin will be non-zero in the x or y axis
344 // if there is any reversed direction or writing-mode. The combinations are:
345 // writing-mode / direction scrollOrigin.x() set scrollOrigin.y() set
346 // horizontal-tb / ltr NO NO
347 // horizontal-tb / rtl YES NO
348 // horizontal-bt / ltr NO YES
349 // horizontal-bt / rtl YES YES
350 // vertical-lr / ltr NO NO
351 // vertical-lr / rtl NO YES
352 // vertical-rl / ltr YES NO
353 // vertical-rl / rtl YES YES
354 IntPoint m_scrollOrigin;
359 // Called to update the scrollbars to accurately reflect the state of the view.
360 void updateScrollbars(const IntSize& desiredOffset);
362 // Called when the scroll position within this view changes. FrameView overrides this to generate repaint invalidations.
363 virtual void repaintFixedElementsAfterScrolling() {}
366 void platformDestroy();
367 void platformAddChild(Widget*);
368 void platformRemoveChild(Widget*);
369 void platformSetScrollbarModes();
370 void platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const;
371 void platformSetCanBlitOnScroll(bool);
372 bool platformCanBlitOnScroll() const;
373 IntRect platformVisibleContentRect(bool includeScrollbars) const;
374 IntSize platformContentsSize() const;
375 void platformSetContentsSize();
376 IntRect platformContentsToScreen(const IntRect&) const;
377 IntPoint platformScreenToContents(const IntPoint&) const;
378 void platformSetScrollPosition(const IntPoint&);
379 bool platformScroll(ScrollDirection, ScrollGranularity);
380 void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress);
381 void platformRepaintContentRectangle(const IntRect&, bool now);
382 bool platformIsOffscreen() const;
384 void platformSetScrollOrigin(const IntPoint&, bool updatePosition);
386 #if PLATFORM(ANDROID)
387 int platformActualWidth() const;
388 int platformActualHeight() const;
389 int platformActualScrollX() const;
390 int platformActualScrollY() const;
393 #if PLATFORM(MAC) && defined __OBJC__
395 NSView* documentView() const;
398 NSScrollView<WebCoreFrameScrollView>* scrollView() const;
403 void setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj, bool resetValues = true);
404 void setHorizontalAdjustment(GtkAdjustment* hadj, bool resetValues = true);
405 void setVerticalAdjustment(GtkAdjustment* vadj, bool resetValues = true);
406 void setScrollOffset(const IntSize& offset) { m_scrollOffset = offset; }
409 GRefPtr<GtkAdjustment> m_horizontalAdjustment;
410 GRefPtr<GtkAdjustment> m_verticalAdjustment;
415 virtual void setPlatformWidget(wxWindow*);
416 void adjustScrollbars(int x = -1, int y = -1, bool refresh = true);
418 class ScrollViewPrivate;
419 ScrollViewPrivate* m_data;
422 #ifdef ANDROID_CAPTURE_OFFSCREEN_PAINTS
424 // capture parts of rect not contained by vis
425 void platformOffscreenContentRectangle(const IntRect& vis,
426 const IntRect& rect);
428 }; // class ScrollView
430 } // namespace WebCore
432 #endif // ScrollView_h