OSDN Git Service

Merge changes I55c6d71a,Ifb3277d4,Ia1b847a2,I7ba9cf3f,Ida2b2a8a,I1280ec90,I72f818d5...
[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     virtual void notifyPageThatContentAreaWillPaint() const;
66
67     // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
68     virtual void scrollTo(const IntSize& newOffset);
69
70     // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
71     // host window in the window's coordinate space.
72     virtual HostWindow* hostWindow() const = 0;
73
74     // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
75     virtual IntRect windowClipRect(bool clipToContents = true) const = 0;
76
77     // Functions for child manipulation and inspection.
78     const HashSet<RefPtr<Widget> >* children() const { return &m_children; }
79     void addChild(PassRefPtr<Widget>);
80     void removeChild(Widget*);
81     
82     // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions
83     // can be used to obtain those scrollbars.
84     virtual Scrollbar* horizontalScrollbar() const { return m_horizontalScrollbar.get(); }
85     virtual Scrollbar* verticalScrollbar() const { return m_verticalScrollbar.get(); }
86     bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
87
88     // Functions for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of
89     // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar.
90     // Auto means show a scrollbar only when one is needed.
91     // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native
92     // widget may choose not to honor the requested modes.
93     void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool horizontalLock = false, bool verticalLock = false);
94     void setHorizontalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(mode, verticalScrollbarMode(), lock, verticalScrollbarLock()); }
95     void setVerticalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(horizontalScrollbarMode(), mode, horizontalScrollbarLock(), lock); };
96     void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const;
97     ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; }
98     ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; }
99
100     void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; }
101     bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; }
102     void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; }
103     bool verticalScrollbarLock() const { return m_verticalScrollbarLock; }
104
105     void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; }
106
107     virtual void setCanHaveScrollbars(bool);
108     bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; }
109
110     virtual bool avoidScrollbarCreation() const { return false; }
111
112     // By default you only receive paint events for the area that is visible. In the case of using a
113     // tiled backing store, this function can be set, so that the view paints the entire contents.
114     bool paintsEntireContents() const { return m_paintsEntireContents; }
115     void setPaintsEntireContents(bool);
116
117     // By default, paint events are clipped to the visible area.  If set to
118     // false, paint events are no longer clipped.  paintsEntireContents() implies !clipsRepaints().
119     bool clipsRepaints() const { return m_clipsRepaints; }
120     void setClipsRepaints(bool);
121
122     // By default programmatic scrolling is handled by WebCore and not by the UI application.
123     // In the case of using a tiled backing store, this mode can be set, so that the scroll requests
124     // are delegated to the UI application.
125     bool delegatesScrolling() const { return m_delegatesScrolling; }
126     void setDelegatesScrolling(bool);
127
128     // Overridden by FrameView to create custom CSS scrollbars if applicable.
129     virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
130
131     // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off.
132     void setProhibitsScrolling(bool b) { m_prohibitsScrolling = b; }
133     bool prohibitsScrolling() const { return m_prohibitsScrolling; }
134
135     // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
136     // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
137     void setCanBlitOnScroll(bool);
138     bool canBlitOnScroll() const;
139
140     // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
141     // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
142     // included.
143     // In the situation the client is responsible for the scrolling (ie. with a tiled backing store) it is possible to use
144     // the actualVisibleContentRect instead, though this must be updated manually, e.g after panning ends.
145     IntRect visibleContentRect(bool includeScrollbars = false) const;
146     IntRect actualVisibleContentRect() const { return m_actualVisibleContentRect.isEmpty() ? visibleContentRect() : m_actualVisibleContentRect; }
147     void setActualVisibleContentRect(const IntRect& actualVisibleContentRect) { m_actualVisibleContentRect = actualVisibleContentRect; }
148     int visibleWidth() const { return visibleContentRect().width(); }
149     int visibleHeight() const { return visibleContentRect().height(); }
150
151     // Functions for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible
152     // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead.
153     int layoutWidth() const;
154     int layoutHeight() const;
155     IntSize fixedLayoutSize() const;
156     void setFixedLayoutSize(const IntSize&);
157     bool useFixedLayout() const;
158     void setUseFixedLayout(bool enable);
159     
160     // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
161     // values).
162     IntSize contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight().
163     int contentsWidth() const { return contentsSize().width(); }
164     int contentsHeight() const { return contentsSize().height(); }
165     virtual void setContentsSize(const IntSize&);
166
167 #if PLATFORM(ANDROID)
168     int actualWidth() const;
169     int actualHeight() const;
170     int actualScrollX() const;
171     int actualScrollY() const;
172 #endif
173
174     // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
175     IntPoint scrollPosition() const { return visibleContentRect().location(); }
176     IntSize scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
177     IntPoint maximumScrollPosition() const; // The maximum position we can be scrolled to.
178     IntPoint minimumScrollPosition() const; // The minimum position we can be scrolled to.
179     // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
180     IntPoint adjustScrollPositionWithinRange(const IntPoint&) const; 
181     int scrollX() const { return scrollPosition().x(); }
182     int scrollY() const { return scrollPosition().y(); }
183
184     IntSize overhangAmount() const;
185
186     // Functions for scrolling the view.
187     void setScrollPosition(const IntPoint&);
188     void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
189
190     // This function scrolls by lines, pages or pixels.
191     bool scroll(ScrollDirection, ScrollGranularity);
192     
193     // A logical scroll that just ends up calling the corresponding physical scroll() based off the document's writing mode.
194     bool logicalScroll(ScrollLogicalDirection, ScrollGranularity);
195
196     // Scroll the actual contents of the view (either blitting or invalidating as needed).
197     void scrollContents(const IntSize& scrollDelta);
198
199     // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
200     void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
201     bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
202
203     // Event coordinates are assumed to be in the coordinate space of a window that contains
204     // the entire widget hierarchy. It is up to the platform to decide what the precise definition
205     // of containing window is. (For example on Mac it is the containing NSWindow.)
206     IntPoint windowToContents(const IntPoint&) const;
207     IntPoint contentsToWindow(const IntPoint&) const;
208     IntRect windowToContents(const IntRect&) const;
209     IntRect contentsToWindow(const IntRect&) const;
210
211     // Functions for converting to and from screen coordinates.
212     IntRect contentsToScreen(const IntRect&) const;
213     IntPoint screenToContents(const IntPoint&) const;
214
215     // 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
216     // we know that we are either not in a window right now or if that window is not visible.
217     bool isOffscreen() const;
218     
219     // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
220     // for example.
221     virtual IntRect windowResizerRect() const { return IntRect(); }
222     bool containsScrollbarsAvoidingResizer() const;
223     void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
224     void windowResizerRectChanged();
225
226     virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count.
227
228     // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
229     virtual void frameRectsChanged();
230     
231     // Widget override to update our scrollbars and notify our contents of the resize.
232     virtual void setFrameRect(const IntRect&);
233     virtual void setBoundsSize(const IntSize&);
234
235     // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
236     Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
237
238     // This function exists for scrollviews that need to handle wheel events manually.
239     // On Mac the underlying NSScrollView just does the scrolling, but on other platforms
240     // (like Windows), we need this function in order to do the scroll ourselves.
241     void wheelEvent(PlatformWheelEvent&);
242 #if ENABLE(GESTURE_EVENTS)
243     void gestureEvent(const PlatformGestureEvent&);
244 #endif
245
246     IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const
247     {
248         IntPoint newPoint = point;
249         if (!isScrollViewScrollbar(child))
250             newPoint = point - scrollOffset();
251         newPoint.move(child->x(), child->y());
252         return newPoint;
253     }
254
255     IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const
256     {
257         IntPoint newPoint = point;
258         if (!isScrollViewScrollbar(child))
259             newPoint = point + scrollOffset();
260         newPoint.move(-child->x(), -child->y());
261         return newPoint;
262     }
263
264     // Widget override. Handles painting of the contents of the view as well as the scrollbars.
265     virtual void paint(GraphicsContext*, const IntRect&);
266     void paintScrollbars(GraphicsContext*, const IntRect&);
267
268     // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
269     virtual void show();
270     virtual void hide();
271     virtual void setParentVisible(bool);
272     
273     // Pan scrolling.
274     static const int noPanScrollRadius = 15;
275     void addPanScrollIcon(const IntPoint&);
276     void removePanScrollIcon();
277     void paintPanScrollIcon(GraphicsContext*);
278
279     virtual bool isPointInScrollbarCorner(const IntPoint&);
280     virtual bool scrollbarCornerPresent() const;
281
282     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const;
283     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const;
284     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const;
285     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const;
286
287 protected:
288     ScrollView();
289
290     virtual void repaintContentRectangle(const IntRect&, bool now = false);
291     virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
292
293     void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
294     virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
295
296     virtual void contentsResized() = 0;
297     virtual void visibleContentsResized() = 0;
298
299     IntSize boundsSize() const { return m_boundsSize; }
300     void setInitialBoundsSize(const IntSize&);
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     IntSize m_boundsSize;
359
360     void init();
361     void destroy();
362
363     // Called to update the scrollbars to accurately reflect the state of the view.
364     void updateScrollbars(const IntSize& desiredOffset);
365
366     // Called when the scroll position within this view changes.  FrameView overrides this to generate repaint invalidations.
367     virtual void repaintFixedElementsAfterScrolling() {}
368
369     void platformInit();
370     void platformDestroy();
371     void platformAddChild(Widget*);
372     void platformRemoveChild(Widget*);
373     void platformSetScrollbarModes();
374     void platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const;
375     void platformSetCanBlitOnScroll(bool);
376     bool platformCanBlitOnScroll() const;
377     IntRect platformVisibleContentRect(bool includeScrollbars) const;
378     IntSize platformContentsSize() const;
379     void platformSetContentsSize();
380     IntRect platformContentsToScreen(const IntRect&) const;
381     IntPoint platformScreenToContents(const IntPoint&) const;
382     void platformSetScrollPosition(const IntPoint&);
383     bool platformScroll(ScrollDirection, ScrollGranularity);
384     void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress);
385     void platformRepaintContentRectangle(const IntRect&, bool now);
386     bool platformIsOffscreen() const;
387    
388     void platformSetScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
389
390 #if PLATFORM(ANDROID)
391     int platformActualWidth() const;
392     int platformActualHeight() const;
393     int platformActualScrollX() const;
394     int platformActualScrollY() const;
395 #endif
396
397 #if PLATFORM(MAC) && defined __OBJC__
398 public:
399     NSView* documentView() const;
400
401 private:
402     NSScrollView<WebCoreFrameScrollView>* scrollView() const;
403 #endif
404
405 #if PLATFORM(GTK)
406 public:
407     void setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj, bool resetValues = true);
408     void setHorizontalAdjustment(GtkAdjustment* hadj, bool resetValues = true);
409     void setVerticalAdjustment(GtkAdjustment* vadj, bool resetValues = true);
410     void setScrollOffset(const IntSize& offset) { m_scrollOffset = offset; }
411
412 private:
413     GRefPtr<GtkAdjustment> m_horizontalAdjustment;
414     GRefPtr<GtkAdjustment> m_verticalAdjustment;
415 #endif
416
417 #if PLATFORM(WX)
418 public:
419     virtual void setPlatformWidget(wxWindow*);
420     void adjustScrollbars(int x = -1, int y = -1, bool refresh = true);
421 private:
422     class ScrollViewPrivate;
423     ScrollViewPrivate* m_data;
424 #endif
425
426 #ifdef ANDROID_CAPTURE_OFFSCREEN_PAINTS
427 public:
428     // capture parts of rect not contained by vis
429     void platformOffscreenContentRectangle(const IntRect& vis,
430         const IntRect& rect);
431 #endif
432 }; // class ScrollView
433
434 } // namespace WebCore
435
436 #endif // ScrollView_h