0b29ff78efc7b533e4f734d0107d0a685aa86fa8
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / scroll / 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 "platform/PlatformExport.h"
31 #include "platform/Widget.h"
32 #include "platform/geometry/IntRect.h"
33 #include "platform/scroll/ScrollTypes.h"
34 #include "platform/scroll/ScrollableArea.h"
35 #include "platform/scroll/Scrollbar.h"
36
37 #include "wtf/HashSet.h"
38
39 namespace WebCore {
40
41 class HostWindow;
42 class Scrollbar;
43
44 class PLATFORM_EXPORT ScrollView : public Widget, public ScrollableArea {
45 public:
46     virtual ~ScrollView();
47
48     // ScrollableArea functions.
49     virtual int scrollSize(ScrollbarOrientation) const OVERRIDE;
50     virtual void setScrollOffset(const IntPoint&) OVERRIDE;
51     virtual bool isScrollCornerVisible() const OVERRIDE;
52     virtual void scrollbarStyleChanged(int newStyle, bool forceUpdate) OVERRIDE;
53     virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE;
54     virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE;
55
56     virtual void notifyPageThatContentAreaWillPaint() const;
57
58     // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
59     virtual void scrollTo(const IntSize& newOffset);
60
61     // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
62     // host window in the window's coordinate space.
63     virtual HostWindow* hostWindow() const = 0;
64
65     // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
66     virtual IntRect windowClipRect(IncludeScrollbarsInRect = ExcludeScrollbars) const = 0;
67
68     // Functions for child manipulation and inspection.
69     const HashSet<RefPtr<Widget> >* children() const { return &m_children; }
70     virtual void addChild(PassRefPtr<Widget>);
71     virtual void removeChild(Widget*);
72
73     // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions
74     // can be used to obtain those scrollbars.
75     virtual Scrollbar* horizontalScrollbar() const OVERRIDE { return m_horizontalScrollbar.get(); }
76     virtual Scrollbar* verticalScrollbar() const OVERRIDE { return m_verticalScrollbar.get(); }
77     bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
78
79     void positionScrollbarLayers();
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     // By default you only receive paint events for the area that is visible. In the case of using a
104     // tiled backing store, this function can be set, so that the view paints the entire contents.
105     bool paintsEntireContents() const { return m_paintsEntireContents; }
106     void setPaintsEntireContents(bool);
107
108     // By default, paint events are clipped to the visible area.  If set to
109     // false, paint events are no longer clipped.  paintsEntireContents() implies !clipsRepaints().
110     bool clipsRepaints() const { return m_clipsRepaints; }
111     void setClipsRepaints(bool);
112
113     // Overridden by FrameView to create custom CSS scrollbars if applicable.
114     virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
115
116     virtual bool shouldAttemptToScrollUsingFastPath() const;
117
118     // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
119     // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
120     // included.
121     virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
122     IntSize visibleSize() const { return visibleContentRect().size(); }
123     virtual int visibleWidth() const OVERRIDE { return visibleContentRect().width(); }
124     virtual int visibleHeight() const OVERRIDE { return visibleContentRect().height(); }
125
126     // visibleContentRect().size() is computed from unscaledVisibleContentSize() divided by the value of visibleContentScaleFactor.
127     // For the main frame, visibleContentScaleFactor is equal to the page's pageScaleFactor; it's 1 otherwise.
128     IntSize unscaledVisibleContentSize(IncludeScrollbarsInRect = ExcludeScrollbars) const;
129     virtual float visibleContentScaleFactor() const { return 1; }
130
131     // Offset used to convert incoming input events while emulating device metics.
132     virtual IntSize inputEventsOffsetForEmulation() const { return IntSize(); }
133
134     // Scale used to convert incoming input events. Usually the same as visibleContentScaleFactor(), unless specifically changed.
135     virtual float inputEventsScaleFactor() const { return visibleContentScaleFactor(); }
136
137     // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
138     // values).
139     virtual IntSize contentsSize() const OVERRIDE; // Always at least as big as the visibleWidth()/visibleHeight().
140     int contentsWidth() const { return contentsSize().width(); }
141     int contentsHeight() const { return contentsSize().height(); }
142     virtual void setContentsSize(const IntSize&);
143
144     // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
145     virtual IntPoint scrollPosition() const OVERRIDE { return visibleContentRect().location(); }
146     IntSize scrollOffset() const { return toIntSize(visibleContentRect().location()); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
147     IntSize pendingScrollDelta() const { return m_pendingScrollDelta; }
148     virtual IntPoint maximumScrollPosition() const OVERRIDE; // The maximum position we can be scrolled to.
149     virtual IntPoint minimumScrollPosition() const OVERRIDE; // The minimum position we can be scrolled to.
150     // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
151     IntPoint adjustScrollPositionWithinRange(const IntPoint&) const;
152     int scrollX() const { return scrollPosition().x(); }
153     int scrollY() const { return scrollPosition().y(); }
154
155     virtual IntSize overhangAmount() const OVERRIDE;
156
157     void cacheCurrentScrollPosition() { m_cachedScrollPosition = scrollPosition(); }
158     IntPoint cachedScrollPosition() const { return m_cachedScrollPosition; }
159
160     // Functions for scrolling the view.
161     virtual void setScrollPosition(const IntPoint&);
162     void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
163
164     bool scroll(ScrollDirection, ScrollGranularity);
165
166     // Scroll the actual contents of the view (either blitting or invalidating as needed).
167     void scrollContents(const IntSize& scrollDelta);
168
169     // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
170     void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
171     bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
172
173     IntPoint rootViewToContents(const IntPoint&) const;
174     IntPoint contentsToRootView(const IntPoint&) const;
175     IntRect rootViewToContents(const IntRect&) const;
176     IntRect contentsToRootView(const IntRect&) const;
177
178     // Event coordinates are assumed to be in the coordinate space of a window that contains
179     // the entire widget hierarchy. It is up to the platform to decide what the precise definition
180     // of containing window is. (For example on Mac it is the containing NSWindow.)
181     IntPoint windowToContents(const IntPoint&) const;
182     IntPoint contentsToWindow(const IntPoint&) const;
183     IntRect windowToContents(const IntRect&) const;
184     IntRect contentsToWindow(const IntRect&) const;
185
186     // Functions for converting to screen coordinates.
187     IntRect contentsToScreen(const IntRect&) const;
188
189     // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
190     // for example.
191     virtual IntRect windowResizerRect() const { return IntRect(); }
192     bool containsScrollbarsAvoidingResizer() const;
193     void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
194     void windowResizerRectChanged();
195
196     virtual void setParent(Widget*) OVERRIDE; // Overridden to update the overlapping scrollbar count.
197
198     // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
199     virtual void frameRectsChanged() OVERRIDE;
200
201     // Widget override to update our scrollbars and notify our contents of the resize.
202     virtual void setFrameRect(const IntRect&) OVERRIDE;
203
204     // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
205     Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
206
207     virtual IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const OVERRIDE
208     {
209         IntPoint newPoint = point;
210         if (!isScrollViewScrollbar(child))
211             newPoint = point - scrollOffset();
212         newPoint.moveBy(child->location());
213         return newPoint;
214     }
215
216     virtual IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const OVERRIDE
217     {
218         IntPoint newPoint = point;
219         if (!isScrollViewScrollbar(child))
220             newPoint = point + scrollOffset();
221         newPoint.moveBy(-child->location());
222         return newPoint;
223     }
224
225     // Widget override. Handles painting of the contents of the view as well as the scrollbars.
226     virtual void paint(GraphicsContext*, const IntRect&) OVERRIDE;
227     void paintScrollbars(GraphicsContext*, const IntRect&);
228
229     // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
230     virtual void show() OVERRIDE;
231     virtual void hide() OVERRIDE;
232     virtual void setParentVisible(bool) OVERRIDE;
233
234     // Pan scrolling.
235     static const int noPanScrollRadius = 15;
236     void addPanScrollIcon(const IntPoint&);
237     void removePanScrollIcon();
238     void paintPanScrollIcon(GraphicsContext*);
239
240     virtual bool isPointInScrollbarCorner(const IntPoint&);
241     virtual bool scrollbarCornerPresent() const;
242     virtual IntRect scrollCornerRect() const OVERRIDE;
243     virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
244     virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&);
245
246     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const OVERRIDE;
247     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const OVERRIDE;
248     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const OVERRIDE;
249     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const OVERRIDE;
250
251     void calculateAndPaintOverhangAreas(GraphicsContext*, const IntRect& dirtyRect);
252     void calculateAndPaintOverhangBackground(GraphicsContext*, const IntRect& dirtyRect);
253
254     virtual bool isScrollView() const OVERRIDE FINAL { return true; }
255
256 protected:
257     ScrollView();
258
259     virtual void repaintContentRectangle(const IntRect&);
260     virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
261
262     virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
263
264     virtual void scrollbarExistenceDidChange() = 0;
265     // These functions are used to create/destroy scrollbars.
266     void setHasHorizontalScrollbar(bool);
267     void setHasVerticalScrollbar(bool);
268
269     virtual void updateScrollCorner();
270     virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE;
271
272     virtual void scrollContentsIfNeeded();
273     // Scroll the content by blitting the pixels.
274     virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect);
275     // Scroll the content by invalidating everything.
276     virtual void scrollContentsSlowPath(const IntRect& updateRect);
277
278     void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
279
280     // Subclassed by FrameView to check the writing-mode of the document.
281     virtual bool isVerticalDocument() const { return true; }
282     virtual bool isFlippedDocument() const { return false; }
283
284     // Called to update the scrollbars to accurately reflect the state of the view.
285     void updateScrollbars(const IntSize& desiredOffset);
286
287     IntSize excludeScrollbars(const IntSize&) const;
288
289 private:
290     RefPtr<Scrollbar> m_horizontalScrollbar;
291     RefPtr<Scrollbar> m_verticalScrollbar;
292     ScrollbarMode m_horizontalScrollbarMode;
293     ScrollbarMode m_verticalScrollbarMode;
294
295     bool m_horizontalScrollbarLock;
296     bool m_verticalScrollbarLock;
297
298     HashSet<RefPtr<Widget> > m_children;
299
300     IntSize m_pendingScrollDelta;
301     IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
302     IntPoint m_cachedScrollPosition;
303     IntSize m_contentsSize;
304
305     int m_scrollbarsAvoidingResizer;
306     bool m_scrollbarsSuppressed;
307
308     bool m_inUpdateScrollbars;
309     unsigned m_updateScrollbarsPass;
310
311     IntPoint m_panScrollIconPoint;
312     bool m_drawPanScrollIcon;
313
314     bool m_paintsEntireContents;
315     bool m_clipsRepaints;
316
317     void init();
318     void destroy();
319
320     IntRect rectToCopyOnScroll() const;
321
322     void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
323     void updateOverhangAreas();
324 }; // class ScrollView
325
326 DEFINE_TYPE_CASTS(ScrollView, Widget, widget, widget->isScrollView(), widget.isScrollView());
327
328 } // namespace WebCore
329
330 #endif // ScrollView_h