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 typedef struct _GtkAdjustment GtkAdjustment
;
47 class wxScrollWinEvent
;
53 class PlatformWheelEvent
;
56 class ScrollView
: public Widget
, public ScrollbarClient
{
60 // ScrollbarClient method. FrameView overrides the other two.
61 virtual void valueChanged(Scrollbar
*);
63 // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
64 // host window in the window's coordinate space.
65 virtual HostWindow
* hostWindow() const = 0;
67 // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
68 virtual IntRect
windowClipRect(bool clipToContents
= true) const = 0;
70 // Methods for child manipulation and inspection.
71 const HashSet
<RefPtr
<Widget
> >* children() const { return &m_children
; }
72 void addChild(PassRefPtr
<Widget
>);
73 void removeChild(Widget
*);
75 // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These methods
76 // can be used to obtain those scrollbars.
77 Scrollbar
* horizontalScrollbar() const { return m_horizontalScrollbar
.get(); }
78 Scrollbar
* verticalScrollbar() const { return m_verticalScrollbar
.get(); }
79 bool isScrollViewScrollbar(const Widget
* child
) const { return horizontalScrollbar() == child
|| verticalScrollbar() == child
; }
81 // Methods 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
);
87 void setHorizontalScrollbarMode(ScrollbarMode mode
) { setScrollbarModes(mode
, verticalScrollbarMode()); }
88 void setVerticalScrollbarMode(ScrollbarMode mode
) { setScrollbarModes(horizontalScrollbarMode(), mode
); }
89 void scrollbarModes(ScrollbarMode
& horizontalMode
, ScrollbarMode
& verticalMode
) const;
90 ScrollbarMode
horizontalScrollbarMode() const { ScrollbarMode horizontal
, vertical
; scrollbarModes(horizontal
, vertical
); return horizontal
; }
91 ScrollbarMode
verticalScrollbarMode() const { ScrollbarMode horizontal
, vertical
; scrollbarModes(horizontal
, vertical
); return vertical
; }
92 virtual void setCanHaveScrollbars(bool);
93 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff
|| verticalScrollbarMode() != ScrollbarAlwaysOff
; }
95 // By default you only receive paint events for the area that is visible. In the case of using a
96 // tiled backing store, this method can be set, so that the view paints the entire contents.
97 bool paintsEntireContents() const { return m_paintsEntireContents
; }
98 void setPaintsEntireContents(bool);
100 // Overridden by FrameView to create custom CSS scrollbars if applicable.
101 virtual PassRefPtr
<Scrollbar
> createScrollbar(ScrollbarOrientation
);
103 // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off.
104 void setProhibitsScrolling(bool b
) { m_prohibitsScrolling
= b
; }
105 bool prohibitsScrolling() const { return m_prohibitsScrolling
; }
107 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
108 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
109 void setCanBlitOnScroll(bool);
110 bool canBlitOnScroll() const;
112 // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
113 // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
115 IntRect
visibleContentRect(bool includeScrollbars
= false) const;
116 int visibleWidth() const { return visibleContentRect().width(); }
117 int visibleHeight() const { return visibleContentRect().height(); }
119 // Methods for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible
120 // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead.
121 int layoutWidth() const;
122 int layoutHeight() const;
123 IntSize
fixedLayoutSize() const;
124 void setFixedLayoutSize(const IntSize
&);
125 bool useFixedLayout() const;
126 void setUseFixedLayout(bool enable
);
128 // Methods for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
130 IntSize
contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight().
131 int contentsWidth() const { return contentsSize().width(); }
132 int contentsHeight() const { return contentsSize().height(); }
133 virtual void setContentsSize(const IntSize
&);
135 // Methods for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
136 IntPoint
scrollPosition() const { return visibleContentRect().location(); }
137 IntSize
scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
138 IntPoint
maximumScrollPosition() const; // The maximum position we can be scrolled to.
139 int scrollX() const { return scrollPosition().x(); }
140 int scrollY() const { return scrollPosition().y(); }
142 // Methods for scrolling the view. setScrollPosition is the only method that really scrolls the view. The other two methods are helper functions
143 // that ultimately end up calling setScrollPosition.
144 void setScrollPosition(const IntPoint
&);
145 void scrollBy(const IntSize
& s
) { return setScrollPosition(scrollPosition() + s
); }
146 void scrollRectIntoViewRecursively(const IntRect
&);
148 // This method scrolls by lines, pages or pixels.
149 bool scroll(ScrollDirection
, ScrollGranularity
);
151 // Scroll the actual contents of the view (either blitting or invalidating as needed).
152 void scrollContents(const IntSize
& scrollDelta
);
154 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
155 void setScrollbarsSuppressed(bool suppressed
, bool repaintOnUnsuppress
= false);
156 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed
; }
158 // Event coordinates are assumed to be in the coordinate space of a window that contains
159 // the entire widget hierarchy. It is up to the platform to decide what the precise definition
160 // of containing window is. (For example on Mac it is the containing NSWindow.)
161 IntPoint
windowToContents(const IntPoint
&) const;
162 IntPoint
contentsToWindow(const IntPoint
&) const;
163 IntRect
windowToContents(const IntRect
&) const;
164 IntRect
contentsToWindow(const IntRect
&) const;
166 // Methods for converting to and from screen coordinates.
167 IntRect
contentsToScreen(const IntRect
&) const;
168 IntPoint
screenToContents(const IntPoint
&) const;
170 // The purpose of this method is to answer whether or not the scroll view is currently visible. Animations and painting updates can be suspended if
171 // we know that we are either not in a window right now or if that window is not visible.
172 bool isOffscreen() const;
174 // These methods are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
176 virtual IntRect
windowResizerRect() const { return IntRect(); }
177 bool containsScrollbarsAvoidingResizer() const;
178 void adjustScrollbarsAvoidingResizerCount(int overlapDelta
);
179 virtual void setParent(ScrollView
*); // Overridden to update the overlapping scrollbar count.
181 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
182 virtual void frameRectsChanged();
184 // Widget override to update our scrollbars and notify our contents of the resize.
185 virtual void setFrameRect(const IntRect
&);
187 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
188 Scrollbar
* scrollbarAtPoint(const IntPoint
& windowPoint
);
190 // This method exists for scrollviews that need to handle wheel events manually.
191 // On Mac the underlying NSScrollView just does the scrolling, but on other platforms
192 // (like Windows), we need this method in order to do the scroll ourselves.
193 void wheelEvent(PlatformWheelEvent
&);
195 IntPoint
convertChildToSelf(const Widget
* child
, const IntPoint
& point
) const
197 IntPoint newPoint
= point
;
198 if (!isScrollViewScrollbar(child
))
199 newPoint
= point
- scrollOffset();
200 newPoint
.move(child
->x(), child
->y());
204 IntPoint
convertSelfToChild(const Widget
* child
, const IntPoint
& point
) const
206 IntPoint newPoint
= point
;
207 if (!isScrollViewScrollbar(child
))
208 newPoint
= point
+ scrollOffset();
209 newPoint
.move(-child
->x(), -child
->y());
213 // Widget override. Handles painting of the contents of the view as well as the scrollbars.
214 virtual void paint(GraphicsContext
*, const IntRect
&);
215 void paintScrollbars(GraphicsContext
*, const IntRect
&);
217 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
220 virtual void setParentVisible(bool);
223 static const int noPanScrollRadius
= 15;
224 void addPanScrollIcon(const IntPoint
&);
225 void removePanScrollIcon();
226 void paintPanScrollIcon(GraphicsContext
*);
228 virtual bool isPointInScrollbarCorner(const IntPoint
&);
229 virtual bool scrollbarCornerPresent() const;
231 virtual IntRect
convertFromScrollbarToContainingView(const Scrollbar
*, const IntRect
&) const;
232 virtual IntRect
convertFromContainingViewToScrollbar(const Scrollbar
*, const IntRect
&) const;
233 virtual IntPoint
convertFromScrollbarToContainingView(const Scrollbar
*, const IntPoint
&) const;
234 virtual IntPoint
convertFromContainingViewToScrollbar(const Scrollbar
*, const IntPoint
&) const;
239 virtual void repaintContentRectangle(const IntRect
&, bool now
= false);
240 virtual void paintContents(GraphicsContext
*, const IntRect
& damageRect
) = 0;
242 virtual void contentsResized() = 0;
243 virtual void visibleContentsResized() = 0;
245 // These methods are used to create/destroy scrollbars.
246 void setHasHorizontalScrollbar(bool);
247 void setHasVerticalScrollbar(bool);
249 IntRect
scrollCornerRect() const;
250 virtual void updateScrollCorner();
251 virtual void paintScrollCorner(GraphicsContext
*, const IntRect
& cornerRect
);
254 RefPtr
<Scrollbar
> m_horizontalScrollbar
;
255 RefPtr
<Scrollbar
> m_verticalScrollbar
;
256 ScrollbarMode m_horizontalScrollbarMode
;
257 ScrollbarMode m_verticalScrollbarMode
;
258 bool m_prohibitsScrolling
;
260 HashSet
<RefPtr
<Widget
> > m_children
;
262 // This bool is unused on Mac OS because we directly ask the platform widget
263 // whether it is safe to blit on scroll.
264 bool m_canBlitOnScroll
;
266 IntSize m_scrollOffset
; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
267 IntSize m_fixedLayoutSize
;
268 IntSize m_contentsSize
;
270 int m_scrollbarsAvoidingResizer
;
271 bool m_scrollbarsSuppressed
;
273 bool m_inUpdateScrollbars
;
274 unsigned m_updateScrollbarsPass
;
276 IntPoint m_panScrollIconPoint
;
277 bool m_drawPanScrollIcon
;
278 bool m_useFixedLayout
;
280 bool m_paintsEntireContents
;
285 // Called to update the scrollbars to accurately reflect the state of the view.
286 void updateScrollbars(const IntSize
& desiredOffset
);
289 void platformDestroy();
290 void platformAddChild(Widget
*);
291 void platformRemoveChild(Widget
*);
292 void platformSetScrollbarModes();
293 void platformScrollbarModes(ScrollbarMode
& horizontal
, ScrollbarMode
& vertical
) const;
294 void platformSetCanBlitOnScroll(bool);
295 bool platformCanBlitOnScroll() const;
296 IntRect
platformVisibleContentRect(bool includeScrollbars
) const;
297 IntSize
platformContentsSize() const;
298 void platformSetContentsSize();
299 IntRect
platformContentsToScreen(const IntRect
&) const;
300 IntPoint
platformScreenToContents(const IntPoint
&) const;
301 void platformSetScrollPosition(const IntPoint
&);
302 bool platformScroll(ScrollDirection
, ScrollGranularity
);
303 void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress
);
304 void platformRepaintContentRectangle(const IntRect
&, bool now
);
305 bool platformIsOffscreen() const;
307 #if PLATFORM(MAC) && defined __OBJC__
309 NSView
* documentView() const;
312 NSScrollView
<WebCoreFrameScrollView
>* scrollView() const;
317 void adjustWidgetsPreventingBlittingCount(int delta
);
319 bool rootPreventsBlitting() const { return root()->m_widgetsPreventingBlitting
> 0; }
320 unsigned m_widgetsPreventingBlitting
;
322 bool rootPreventsBlitting() const { return false; }
327 void setGtkAdjustments(GtkAdjustment
* hadj
, GtkAdjustment
* vadj
);
328 GtkAdjustment
* m_horizontalAdjustment
;
329 GtkAdjustment
* m_verticalAdjustment
;
330 void setScrollOffset(const IntSize
& offset
) { m_scrollOffset
= offset
; }
335 virtual void setPlatformWidget(wxWindow
*);
336 void adjustScrollbars(int x
= -1, int y
= -1, bool refresh
= true);
338 class ScrollViewPrivate
;
339 ScrollViewPrivate
* m_data
;
342 }; // class ScrollView
344 } // namespace WebCore
346 #endif // ScrollView_h