OSDN Git Service

Merge changes I78ff6a85,Ic85c6405,Ibf903baa,I3a0459db,I35140385,I54790419,I6bfe5d24...
[android-x86/external-webkit.git] / Source / WebCore / platform / ScrollView.h
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 "ScrollableArea.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 #include "GRefPtrGtk.h"
44 typedef struct _GtkAdjustment GtkAdjustment;
45 #endif
46
47 #if PLATFORM(WX)
48 class wxScrollWinEvent;
49 #endif
50
51 namespace WebCore {
52
53 class HostWindow;
54 class Scrollbar;
55
56 class ScrollView : public Widget, public ScrollableArea {
57 public:
58     ~ScrollView();
59
60     // ScrollableArea functions.  FrameView overrides the others.
61     virtual int scrollSize(ScrollbarOrientation orientation) const;
62     virtual int scrollPosition(Scrollbar*) const;
63     virtual void setScrollOffset(const IntPoint&);
64     virtual void didCompleteRubberBand(const IntSize&) const;
65
66     // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
67     virtual void scrollTo(const IntSize& newOffset);
68
69     // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
70     // host window in the window's coordinate space.
71     virtual HostWindow* hostWindow() const = 0;
72
73     // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
74     virtual IntRect windowClipRect(bool clipToContents = true) const = 0;
75
76     // Functions for child manipulation and inspection.
77     const HashSet<RefPtr<Widget> >* children() const { return &m_children; }
78     void addChild(PassRefPtr<Widget>);
79     void removeChild(Widget*);
80     
81     // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions
82     // can be used to obtain those scrollbars.
83     virtual Scrollbar* horizontalScrollbar() const { return m_horizontalScrollbar.get(); }
84     virtual Scrollbar* verticalScrollbar() const { return m_verticalScrollbar.get(); }
85     bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
86
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; }
98
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; }
103
104     void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; }
105
106     virtual void setCanHaveScrollbars(bool);
107     bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; }
108
109     virtual bool avoidScrollbarCreation() const { return false; }
110
111     // By default you only receive paint events for the area that is visible. In the case of using a
112     // tiled backing store, this function can be set, so that the view paints the entire contents.
113     bool paintsEntireContents() const { return m_paintsEntireContents; }
114     void setPaintsEntireContents(bool);
115
116     // By default, paint events are clipped to the visible area.  If set to
117     // false, paint events are no longer clipped.  paintsEntireContents() implies !clipsRepaints().
118     bool clipsRepaints() const { return m_clipsRepaints; }
119     void setClipsRepaints(bool);
120
121     // By default programmatic scrolling is handled by WebCore and not by the UI application.
122     // In the case of using a tiled backing store, this mode can be set, so that the scroll requests
123     // are delegated to the UI application.
124     bool delegatesScrolling() const { return m_delegatesScrolling; }
125     void setDelegatesScrolling(bool);
126
127     // Overridden by FrameView to create custom CSS scrollbars if applicable.
128     virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
129
130     // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off.
131     void setProhibitsScrolling(bool b) { m_prohibitsScrolling = b; }
132     bool prohibitsScrolling() const { return m_prohibitsScrolling; }
133
134     // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
135     // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
136     void setCanBlitOnScroll(bool);
137     bool canBlitOnScroll() const;
138
139     // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
140     // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
141     // included.
142     // In the situation the client is responsible for the scrolling (ie. with a tiled backing store) it is possible to use
143     // the actualVisibleContentRect instead, though this must be updated manually, e.g after panning ends.
144     IntRect visibleContentRect(bool includeScrollbars = false) const;
145     IntRect actualVisibleContentRect() const { return m_actualVisibleContentRect.isEmpty() ? visibleContentRect() : m_actualVisibleContentRect; }
146     void setActualVisibleContentRect(const IntRect& actualVisibleContentRect) { m_actualVisibleContentRect = actualVisibleContentRect; }
147     int visibleWidth() const { return visibleContentRect().width(); }
148     int visibleHeight() const { return visibleContentRect().height(); }
149
150     // Functions for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible
151     // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead.
152     int layoutWidth() const;
153     int layoutHeight() const;
154     IntSize fixedLayoutSize() const;
155     void setFixedLayoutSize(const IntSize&);
156     bool useFixedLayout() const;
157     void setUseFixedLayout(bool enable);
158     
159     // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
160     // values).
161     IntSize contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight().
162     int contentsWidth() const { return contentsSize().width(); }
163     int contentsHeight() const { return contentsSize().height(); }
164     virtual void setContentsSize(const IntSize&);
165
166 #if PLATFORM(ANDROID)
167     int actualWidth() const;
168     int actualHeight() const;
169     int actualScrollX() const;
170     int actualScrollY() const;
171 #endif
172
173     // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
174     IntPoint scrollPosition() const { return visibleContentRect().location(); }
175     IntSize scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
176     IntPoint maximumScrollPosition() const; // The maximum position we can be scrolled to.
177     IntPoint minimumScrollPosition() const; // The minimum position we can be scrolled to.
178     // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
179     IntPoint adjustScrollPositionWithinRange(const IntPoint&) const; 
180     int scrollX() const { return scrollPosition().x(); }
181     int scrollY() const { return scrollPosition().y(); }
182
183     // Functions for querying the current scrolled position, negating the effects of overhang.
184     int scrollXForFixedPosition() const;
185     int scrollYForFixedPosition() const;
186     IntSize scrollOffsetForFixedPosition() const;
187
188     IntSize overhangAmount() const;
189
190     // Functions for scrolling the view.
191     void setScrollPosition(const IntPoint&);
192     void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
193
194     // This function scrolls by lines, pages or pixels.
195     bool scroll(ScrollDirection, ScrollGranularity);
196     
197     // A logical scroll that just ends up calling the corresponding physical scroll() based off the document's writing mode.
198     bool logicalScroll(ScrollLogicalDirection, ScrollGranularity);
199
200     // Scroll the actual contents of the view (either blitting or invalidating as needed).
201     void scrollContents(const IntSize& scrollDelta);
202
203     // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
204     void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
205     bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
206
207     // Event coordinates are assumed to be in the coordinate space of a window that contains
208     // the entire widget hierarchy. It is up to the platform to decide what the precise definition
209     // of containing window is. (For example on Mac it is the containing NSWindow.)
210     IntPoint windowToContents(const IntPoint&) const;
211     IntPoint contentsToWindow(const IntPoint&) const;
212     IntRect windowToContents(const IntRect&) const;
213     IntRect contentsToWindow(const IntRect&) const;
214
215     // Functions for converting to and from screen coordinates.
216     IntRect contentsToScreen(const IntRect&) const;
217     IntPoint screenToContents(const IntPoint&) const;
218
219     // 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
220     // we know that we are either not in a window right now or if that window is not visible.
221     bool isOffscreen() const;
222     
223     // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
224     // for example.
225     virtual IntRect windowResizerRect() const { return IntRect(); }
226     bool containsScrollbarsAvoidingResizer() const;
227     void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
228     void windowResizerRectChanged();
229
230     virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count.
231
232     // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
233     virtual void frameRectsChanged();
234     
235     // Widget override to update our scrollbars and notify our contents of the resize.
236     virtual void setFrameRect(const IntRect&);
237
238     // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
239     Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
240
241     // This function exists for scrollviews that need to handle wheel events manually.
242     // On Mac the underlying NSScrollView just does the scrolling, but on other platforms
243     // (like Windows), we need this function in order to do the scroll ourselves.
244     void wheelEvent(PlatformWheelEvent&);
245 #if ENABLE(GESTURE_EVENTS)
246     void gestureEvent(const PlatformGestureEvent&);
247 #endif
248
249     IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const
250     {
251         IntPoint newPoint = point;
252         if (!isScrollViewScrollbar(child))
253             newPoint = point - scrollOffset();
254         newPoint.move(child->x(), child->y());
255         return newPoint;
256     }
257
258     IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const
259     {
260         IntPoint newPoint = point;
261         if (!isScrollViewScrollbar(child))
262             newPoint = point + scrollOffset();
263         newPoint.move(-child->x(), -child->y());
264         return newPoint;
265     }
266
267     // Widget override. Handles painting of the contents of the view as well as the scrollbars.
268     virtual void paint(GraphicsContext*, const IntRect&);
269     void paintScrollbars(GraphicsContext*, const IntRect&);
270
271     // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
272     virtual void show();
273     virtual void hide();
274     virtual void setParentVisible(bool);
275     
276     // Pan scrolling.
277     static const int noPanScrollRadius = 15;
278     void addPanScrollIcon(const IntPoint&);
279     void removePanScrollIcon();
280     void paintPanScrollIcon(GraphicsContext*);
281
282     virtual bool isPointInScrollbarCorner(const IntPoint&);
283     virtual bool scrollbarCornerPresent() const;
284
285     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const;
286     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const;
287     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const;
288     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const;
289
290 protected:
291     ScrollView();
292
293     virtual void repaintContentRectangle(const IntRect&, bool now = false);
294     virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
295
296     void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
297     virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
298
299     virtual void contentsResized() = 0;
300     virtual void visibleContentsResized() = 0;
301
302     // These functions are used to create/destroy scrollbars.
303     void setHasHorizontalScrollbar(bool);
304     void setHasVerticalScrollbar(bool);
305
306     IntRect scrollCornerRect() const;
307     virtual void updateScrollCorner();
308     virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
309
310     // Scroll the content by blitting the pixels.
311     virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect);
312     // Scroll the content by invalidating everything.
313     virtual void scrollContentsSlowPath(const IntRect& updateRect);
314
315     void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
316     IntPoint scrollOrigin() { return m_scrollOrigin; }
317
318     // Subclassed by FrameView to check the writing-mode of the document.
319     virtual bool isVerticalDocument() const { return true; }
320     virtual bool isFlippedDocument() const { return false; }
321
322 private:
323     RefPtr<Scrollbar> m_horizontalScrollbar;
324     RefPtr<Scrollbar> m_verticalScrollbar;
325     ScrollbarMode m_horizontalScrollbarMode;
326     ScrollbarMode m_verticalScrollbarMode;
327
328     bool m_horizontalScrollbarLock;
329     bool m_verticalScrollbarLock;
330
331     bool m_prohibitsScrolling;
332
333     HashSet<RefPtr<Widget> > m_children;
334
335     // This bool is unused on Mac OS because we directly ask the platform widget
336     // whether it is safe to blit on scroll.
337     bool m_canBlitOnScroll;
338
339     IntRect m_actualVisibleContentRect;
340     IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
341     IntSize m_fixedLayoutSize;
342     IntSize m_contentsSize;
343
344     int m_scrollbarsAvoidingResizer;
345     bool m_scrollbarsSuppressed;
346
347     bool m_inUpdateScrollbars;
348     unsigned m_updateScrollbarsPass;
349
350     IntPoint m_panScrollIconPoint;
351     bool m_drawPanScrollIcon;
352     bool m_useFixedLayout;
353
354     bool m_paintsEntireContents;
355     bool m_clipsRepaints;
356     bool m_delegatesScrolling;
357
358     // There are 8 possible combinations of writing mode and direction.  Scroll origin will be non-zero in the x or y axis
359     // if there is any reversed direction or writing-mode.  The combinations are:
360     // writing-mode / direction     scrollOrigin.x() set    scrollOrigin.y() set
361     // horizontal-tb / ltr          NO                      NO
362     // horizontal-tb / rtl          YES                     NO
363     // horizontal-bt / ltr          NO                      YES
364     // horizontal-bt / rtl          YES                     YES
365     // vertical-lr / ltr            NO                      NO
366     // vertical-lr / rtl            NO                      YES
367     // vertical-rl / ltr            YES                     NO
368     // vertical-rl / rtl            YES                     YES
369     IntPoint m_scrollOrigin;
370
371     void init();
372     void destroy();
373
374     // Called to update the scrollbars to accurately reflect the state of the view.
375     void updateScrollbars(const IntSize& desiredOffset);
376
377     // Called when the scroll position within this view changes.  FrameView overrides this to generate repaint invalidations.
378     virtual void repaintFixedElementsAfterScrolling() {}
379
380     void platformInit();
381     void platformDestroy();
382     void platformAddChild(Widget*);
383     void platformRemoveChild(Widget*);
384     void platformSetScrollbarModes();
385     void platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const;
386     void platformSetCanBlitOnScroll(bool);
387     bool platformCanBlitOnScroll() const;
388     IntRect platformVisibleContentRect(bool includeScrollbars) const;
389     IntSize platformContentsSize() const;
390     void platformSetContentsSize();
391     IntRect platformContentsToScreen(const IntRect&) const;
392     IntPoint platformScreenToContents(const IntPoint&) const;
393     void platformSetScrollPosition(const IntPoint&);
394     bool platformScroll(ScrollDirection, ScrollGranularity);
395     void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress);
396     void platformRepaintContentRectangle(const IntRect&, bool now);
397     bool platformIsOffscreen() const;
398    
399     void platformSetScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
400
401 #if PLATFORM(ANDROID)
402     int platformActualWidth() const;
403     int platformActualHeight() const;
404     int platformActualScrollX() const;
405     int platformActualScrollY() const;
406 #endif
407
408 #if PLATFORM(MAC) && defined __OBJC__
409 public:
410     NSView* documentView() const;
411
412 private:
413     NSScrollView<WebCoreFrameScrollView>* scrollView() const;
414 #endif
415
416 #if PLATFORM(GTK)
417 public:
418     void setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj, bool resetValues = true);
419     void setHorizontalAdjustment(GtkAdjustment* hadj, bool resetValues = true);
420     void setVerticalAdjustment(GtkAdjustment* vadj, bool resetValues = true);
421     void setScrollOffset(const IntSize& offset) { m_scrollOffset = offset; }
422
423 private:
424     GRefPtr<GtkAdjustment> m_horizontalAdjustment;
425     GRefPtr<GtkAdjustment> m_verticalAdjustment;
426 #endif
427
428 #if PLATFORM(WX)
429 public:
430     virtual void setPlatformWidget(wxWindow*);
431     void adjustScrollbars(int x = -1, int y = -1, bool refresh = true);
432 private:
433     class ScrollViewPrivate;
434     ScrollViewPrivate* m_data;
435 #endif
436
437 #ifdef ANDROID_CAPTURE_OFFSCREEN_PAINTS
438 public:
439     // capture parts of rect not contained by vis
440     void platformOffscreenContentRectangle(const IntRect& vis,
441         const IntRect& rect);
442 #endif
443 }; // class ScrollView
444
445 } // namespace WebCore
446
447 #endif // ScrollView_h