|
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 "IntRect.h" |
|
31 #include "Scrollbar.h" |
|
32 #include "ScrollbarClient.h" |
|
33 #include "ScrollTypes.h" |
|
34 #include "Widget.h" |
|
35 |
|
36 #include <wtf/HashSet.h> |
|
37 |
|
38 #if PLATFORM(MAC) && defined __OBJC__ |
|
39 @protocol WebCoreFrameScrollView; |
|
40 #endif |
|
41 |
|
42 #if PLATFORM(GTK) |
|
43 typedef struct _GtkAdjustment GtkAdjustment; |
|
44 #endif |
|
45 |
|
46 #if PLATFORM(WX) |
|
47 class wxScrollWinEvent; |
|
48 #endif |
|
49 |
|
50 namespace WebCore { |
|
51 |
|
52 class HostWindow; |
|
53 class PlatformWheelEvent; |
|
54 class Scrollbar; |
|
55 |
|
56 class ScrollView : public Widget, public ScrollbarClient { |
|
57 public: |
|
58 ~ScrollView(); |
|
59 |
|
60 // ScrollbarClient function. FrameView overrides the other two. |
|
61 virtual void valueChanged(Scrollbar*); |
|
62 |
|
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; |
|
66 |
|
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; |
|
69 |
|
70 // Functions for child manipulation and inspection. |
|
71 const HashSet<RefPtr<Widget> >* children() const { return &m_children; } |
|
72 void addChild(PassRefPtr<Widget>); |
|
73 void removeChild(Widget*); |
|
74 |
|
75 // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions |
|
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; } |
|
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; |
|
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 |
|
93 void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; } |
|
94 bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; } |
|
95 void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; } |
|
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); |
|
101 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; } |
|
102 |
|
103 virtual bool avoidScrollbarCreation() { return false; } |
|
104 |
|
105 // By default you only receive paint events for the area that is visible. In the case of using a |
|
106 // tiled backing store, this function can be set, so that the view paints the entire contents. |
|
107 bool paintsEntireContents() const { return m_paintsEntireContents; } |
|
108 void setPaintsEntireContents(bool); |
|
109 |
|
110 // Overridden by FrameView to create custom CSS scrollbars if applicable. |
|
111 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation); |
|
112 |
|
113 // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off. |
|
114 void setProhibitsScrolling(bool b) { m_prohibitsScrolling = b; } |
|
115 bool prohibitsScrolling() const { return m_prohibitsScrolling; } |
|
116 |
|
117 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations |
|
118 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent). |
|
119 void setCanBlitOnScroll(bool); |
|
120 bool canBlitOnScroll() const; |
|
121 |
|
122 // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width |
|
123 // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be |
|
124 // included. |
|
125 IntRect visibleContentRect(bool includeScrollbars = false) const; |
|
126 int visibleWidth() const { return visibleContentRect().width(); } |
|
127 int visibleHeight() const { return visibleContentRect().height(); } |
|
128 |
|
129 // Functions for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible |
|
130 // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead. |
|
131 int layoutWidth() const; |
|
132 int layoutHeight() const; |
|
133 IntSize fixedLayoutSize() const; |
|
134 void setFixedLayoutSize(const IntSize&); |
|
135 bool useFixedLayout() const; |
|
136 void setUseFixedLayout(bool enable); |
|
137 |
|
138 // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height |
|
139 // values). |
|
140 IntSize contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight(). |
|
141 int contentsWidth() const { return contentsSize().width(); } |
|
142 int contentsHeight() const { return contentsSize().height(); } |
|
143 virtual void setContentsSize(const IntSize&); |
|
144 |
|
145 // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values). |
|
146 IntPoint scrollPosition() const { return visibleContentRect().location(); } |
|
147 IntSize scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes. |
|
148 IntPoint maximumScrollPosition() const; // The maximum position we can be scrolled to. |
|
149 int scrollX() const { return scrollPosition().x(); } |
|
150 int scrollY() const { return scrollPosition().y(); } |
|
151 |
|
152 // Functions for scrolling the view. |
|
153 void setScrollPosition(const IntPoint&); |
|
154 void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); } |
|
155 |
|
156 // This function scrolls by lines, pages or pixels. |
|
157 bool scroll(ScrollDirection, ScrollGranularity); |
|
158 |
|
159 // Scroll the actual contents of the view (either blitting or invalidating as needed). |
|
160 void scrollContents(const IntSize& scrollDelta); |
|
161 |
|
162 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred. |
|
163 void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false); |
|
164 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; } |
|
165 |
|
166 // Event coordinates are assumed to be in the coordinate space of a window that contains |
|
167 // the entire widget hierarchy. It is up to the platform to decide what the precise definition |
|
168 // of containing window is. (For example on Mac it is the containing NSWindow.) |
|
169 IntPoint windowToContents(const IntPoint&) const; |
|
170 IntPoint contentsToWindow(const IntPoint&) const; |
|
171 IntRect windowToContents(const IntRect&) const; |
|
172 IntRect contentsToWindow(const IntRect&) const; |
|
173 |
|
174 // Functions for converting to and from screen coordinates. |
|
175 IntRect contentsToScreen(const IntRect&) const; |
|
176 IntPoint screenToContents(const IntPoint&) const; |
|
177 |
|
178 // 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 |
|
179 // we know that we are either not in a window right now or if that window is not visible. |
|
180 bool isOffscreen() const; |
|
181 |
|
182 // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac |
|
183 // for example. |
|
184 virtual IntRect windowResizerRect() const { return IntRect(); } |
|
185 bool containsScrollbarsAvoidingResizer() const; |
|
186 void adjustScrollbarsAvoidingResizerCount(int overlapDelta); |
|
187 virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count. |
|
188 |
|
189 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes). |
|
190 virtual void frameRectsChanged(); |
|
191 |
|
192 // Widget override to update our scrollbars and notify our contents of the resize. |
|
193 virtual void setFrameRect(const IntRect&); |
|
194 |
|
195 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32). |
|
196 Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint); |
|
197 |
|
198 // This function exists for scrollviews that need to handle wheel events manually. |
|
199 // On Mac the underlying NSScrollView just does the scrolling, but on other platforms |
|
200 // (like Windows), we need this function in order to do the scroll ourselves. |
|
201 void wheelEvent(PlatformWheelEvent&); |
|
202 |
|
203 IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const |
|
204 { |
|
205 IntPoint newPoint = point; |
|
206 if (!isScrollViewScrollbar(child)) |
|
207 newPoint = point - scrollOffset(); |
|
208 newPoint.move(child->x(), child->y()); |
|
209 return newPoint; |
|
210 } |
|
211 |
|
212 IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const |
|
213 { |
|
214 IntPoint newPoint = point; |
|
215 if (!isScrollViewScrollbar(child)) |
|
216 newPoint = point + scrollOffset(); |
|
217 newPoint.move(-child->x(), -child->y()); |
|
218 return newPoint; |
|
219 } |
|
220 |
|
221 // Widget override. Handles painting of the contents of the view as well as the scrollbars. |
|
222 virtual void paint(GraphicsContext*, const IntRect&); |
|
223 void paintScrollbars(GraphicsContext*, const IntRect&); |
|
224 |
|
225 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden. |
|
226 virtual void show(); |
|
227 virtual void hide(); |
|
228 virtual void setParentVisible(bool); |
|
229 |
|
230 // Pan scrolling. |
|
231 static const int noPanScrollRadius = 15; |
|
232 void addPanScrollIcon(const IntPoint&); |
|
233 void removePanScrollIcon(); |
|
234 void paintPanScrollIcon(GraphicsContext*); |
|
235 |
|
236 virtual bool isPointInScrollbarCorner(const IntPoint&); |
|
237 virtual bool scrollbarCornerPresent() const; |
|
238 |
|
239 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const; |
|
240 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const; |
|
241 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const; |
|
242 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const; |
|
243 |
|
244 protected: |
|
245 ScrollView(); |
|
246 |
|
247 virtual void repaintContentRectangle(const IntRect&, bool now = false); |
|
248 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0; |
|
249 |
|
250 virtual void contentsResized() = 0; |
|
251 virtual void visibleContentsResized() = 0; |
|
252 |
|
253 // These functions are used to create/destroy scrollbars. |
|
254 void setHasHorizontalScrollbar(bool); |
|
255 void setHasVerticalScrollbar(bool); |
|
256 |
|
257 IntRect scrollCornerRect() const; |
|
258 virtual void updateScrollCorner(); |
|
259 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect); |
|
260 |
|
261 // Scroll the content by blitting the pixels |
|
262 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect); |
|
263 |
|
264 private: |
|
265 RefPtr<Scrollbar> m_horizontalScrollbar; |
|
266 RefPtr<Scrollbar> m_verticalScrollbar; |
|
267 ScrollbarMode m_horizontalScrollbarMode; |
|
268 ScrollbarMode m_verticalScrollbarMode; |
|
269 |
|
270 bool m_horizontalScrollbarLock; |
|
271 bool m_verticalScrollbarLock; |
|
272 |
|
273 bool m_prohibitsScrolling; |
|
274 |
|
275 HashSet<RefPtr<Widget> > m_children; |
|
276 |
|
277 // This bool is unused on Mac OS because we directly ask the platform widget |
|
278 // whether it is safe to blit on scroll. |
|
279 bool m_canBlitOnScroll; |
|
280 |
|
281 IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared. |
|
282 IntSize m_fixedLayoutSize; |
|
283 IntSize m_contentsSize; |
|
284 |
|
285 int m_scrollbarsAvoidingResizer; |
|
286 bool m_scrollbarsSuppressed; |
|
287 |
|
288 bool m_inUpdateScrollbars; |
|
289 unsigned m_updateScrollbarsPass; |
|
290 |
|
291 IntPoint m_panScrollIconPoint; |
|
292 bool m_drawPanScrollIcon; |
|
293 bool m_useFixedLayout; |
|
294 |
|
295 bool m_paintsEntireContents; |
|
296 |
|
297 void init(); |
|
298 void destroy(); |
|
299 |
|
300 // Called to update the scrollbars to accurately reflect the state of the view. |
|
301 void updateScrollbars(const IntSize& desiredOffset); |
|
302 |
|
303 // Called when the scroll position within this view changes. FrameView overrides this to generate repaint invalidations. |
|
304 virtual void repaintFixedElementsAfterScrolling() {} |
|
305 |
|
306 void platformInit(); |
|
307 void platformDestroy(); |
|
308 void platformAddChild(Widget*); |
|
309 void platformRemoveChild(Widget*); |
|
310 void platformSetScrollbarModes(); |
|
311 void platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const; |
|
312 void platformSetCanBlitOnScroll(bool); |
|
313 bool platformCanBlitOnScroll() const; |
|
314 IntRect platformVisibleContentRect(bool includeScrollbars) const; |
|
315 IntSize platformContentsSize() const; |
|
316 void platformSetContentsSize(); |
|
317 IntRect platformContentsToScreen(const IntRect&) const; |
|
318 IntPoint platformScreenToContents(const IntPoint&) const; |
|
319 void platformSetScrollPosition(const IntPoint&); |
|
320 bool platformScroll(ScrollDirection, ScrollGranularity); |
|
321 void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress); |
|
322 void platformRepaintContentRectangle(const IntRect&, bool now); |
|
323 bool platformIsOffscreen() const; |
|
324 |
|
325 #if PLATFORM(MAC) && defined __OBJC__ |
|
326 public: |
|
327 NSView* documentView() const; |
|
328 |
|
329 private: |
|
330 NSScrollView<WebCoreFrameScrollView>* scrollView() const; |
|
331 #endif |
|
332 |
|
333 #if PLATFORM(GTK) |
|
334 public: |
|
335 void setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj, bool resetValues = true); |
|
336 GtkAdjustment* m_horizontalAdjustment; |
|
337 GtkAdjustment* m_verticalAdjustment; |
|
338 void setScrollOffset(const IntSize& offset) { m_scrollOffset = offset; } |
|
339 #endif |
|
340 |
|
341 #if PLATFORM(WX) |
|
342 public: |
|
343 virtual void setPlatformWidget(wxWindow*); |
|
344 void adjustScrollbars(int x = -1, int y = -1, bool refresh = true); |
|
345 private: |
|
346 class ScrollViewPrivate; |
|
347 ScrollViewPrivate* m_data; |
|
348 #endif |
|
349 |
|
350 }; // class ScrollView |
|
351 |
|
352 } // namespace WebCore |
|
353 |
|
354 #endif // ScrollView_h |