Upstream version 5.34.104.0
[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(bool clipToContents = true) 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     // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations
117     // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent).
118     void setCanBlitOnScroll(bool);
119     bool canBlitOnScroll() const;
120
121     // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
122     // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
123     // included.
124     virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
125     IntSize visibleSize() const { return visibleContentRect().size(); }
126     virtual int visibleWidth() const OVERRIDE { return visibleContentRect().width(); }
127     virtual int visibleHeight() const OVERRIDE { return visibleContentRect().height(); }
128
129     // visibleContentRect().size() is computed from unscaledVisibleContentSize() divided by the value of visibleContentScaleFactor.
130     // For the main frame, visibleContentScaleFactor is equal to the page's pageScaleFactor; it's 1 otherwise.
131     IntSize unscaledVisibleContentSize(IncludeScrollbarsInRect = ExcludeScrollbars) const;
132     virtual float visibleContentScaleFactor() const { return 1; }
133
134     // Offset used to convert incoming input events while emulating device metics.
135     virtual IntSize inputEventsOffsetForEmulation() const { return IntSize(); }
136
137     // Scale used to convert incoming input events. Usually the same as visibleContentScaleFactor(), unless specifically changed.
138     virtual float inputEventsScaleFactor() const { return visibleContentScaleFactor(); }
139
140     // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
141     // values).
142     virtual IntSize contentsSize() const OVERRIDE; // Always at least as big as the visibleWidth()/visibleHeight().
143     int contentsWidth() const { return contentsSize().width(); }
144     int contentsHeight() const { return contentsSize().height(); }
145     virtual void setContentsSize(const IntSize&);
146
147     // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
148     virtual IntPoint scrollPosition() const OVERRIDE { return visibleContentRect().location(); }
149     IntSize scrollOffset() const { return toIntSize(visibleContentRect().location()); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
150     virtual IntPoint maximumScrollPosition() const OVERRIDE; // The maximum position we can be scrolled to.
151     virtual IntPoint minimumScrollPosition() const OVERRIDE; // The minimum position we can be scrolled to.
152     // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
153     IntPoint adjustScrollPositionWithinRange(const IntPoint&) const;
154     int scrollX() const { return scrollPosition().x(); }
155     int scrollY() const { return scrollPosition().y(); }
156
157     virtual IntSize overhangAmount() const OVERRIDE;
158
159     void cacheCurrentScrollPosition() { m_cachedScrollPosition = scrollPosition(); }
160     IntPoint cachedScrollPosition() const { return m_cachedScrollPosition; }
161
162     // Functions for scrolling the view.
163     virtual void setScrollPosition(const IntPoint&);
164     void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); }
165
166     bool scroll(ScrollDirection, ScrollGranularity);
167
168     // Scroll the actual contents of the view (either blitting or invalidating as needed).
169     void scrollContents(const IntSize& scrollDelta);
170
171     // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
172     void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
173     bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
174
175     IntPoint rootViewToContents(const IntPoint&) const;
176     IntPoint contentsToRootView(const IntPoint&) const;
177     IntRect rootViewToContents(const IntRect&) const;
178     IntRect contentsToRootView(const IntRect&) const;
179
180     // Event coordinates are assumed to be in the coordinate space of a window that contains
181     // the entire widget hierarchy. It is up to the platform to decide what the precise definition
182     // of containing window is. (For example on Mac it is the containing NSWindow.)
183     IntPoint windowToContents(const IntPoint&) const;
184     IntPoint contentsToWindow(const IntPoint&) const;
185     IntRect windowToContents(const IntRect&) const;
186     IntRect contentsToWindow(const IntRect&) const;
187
188     // Functions for converting to screen coordinates.
189     IntRect contentsToScreen(const IntRect&) const;
190
191     // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
192     // for example.
193     virtual IntRect windowResizerRect() const { return IntRect(); }
194     bool containsScrollbarsAvoidingResizer() const;
195     void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
196     void windowResizerRectChanged();
197
198     virtual void setParent(Widget*) OVERRIDE; // Overridden to update the overlapping scrollbar count.
199
200     // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
201     virtual void frameRectsChanged() OVERRIDE;
202
203     // Widget override to update our scrollbars and notify our contents of the resize.
204     virtual void setFrameRect(const IntRect&) OVERRIDE;
205
206     // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
207     Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint);
208
209     virtual IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const OVERRIDE
210     {
211         IntPoint newPoint = point;
212         if (!isScrollViewScrollbar(child))
213             newPoint = point - scrollOffset();
214         newPoint.moveBy(child->location());
215         return newPoint;
216     }
217
218     virtual IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const OVERRIDE
219     {
220         IntPoint newPoint = point;
221         if (!isScrollViewScrollbar(child))
222             newPoint = point + scrollOffset();
223         newPoint.moveBy(-child->location());
224         return newPoint;
225     }
226
227     // Widget override. Handles painting of the contents of the view as well as the scrollbars.
228     virtual void paint(GraphicsContext*, const IntRect&) OVERRIDE;
229     void paintScrollbars(GraphicsContext*, const IntRect&);
230
231     // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
232     virtual void show() OVERRIDE;
233     virtual void hide() OVERRIDE;
234     virtual void setParentVisible(bool) OVERRIDE;
235
236     // Pan scrolling.
237     static const int noPanScrollRadius = 15;
238     void addPanScrollIcon(const IntPoint&);
239     void removePanScrollIcon();
240     void paintPanScrollIcon(GraphicsContext*);
241
242     virtual bool isPointInScrollbarCorner(const IntPoint&);
243     virtual bool scrollbarCornerPresent() const;
244     virtual IntRect scrollCornerRect() const OVERRIDE;
245     virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
246     virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&);
247
248     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const OVERRIDE;
249     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const OVERRIDE;
250     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const OVERRIDE;
251     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const OVERRIDE;
252
253     void calculateAndPaintOverhangAreas(GraphicsContext*, const IntRect& dirtyRect);
254     void calculateAndPaintOverhangBackground(GraphicsContext*, const IntRect& dirtyRect);
255
256     virtual bool isScrollView() const OVERRIDE FINAL { return true; }
257
258 protected:
259     ScrollView();
260
261     virtual void repaintContentRectangle(const IntRect&);
262     virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
263
264     virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
265
266     virtual void scrollbarExistenceDidChange() = 0;
267     // These functions are used to create/destroy scrollbars.
268     void setHasHorizontalScrollbar(bool);
269     void setHasVerticalScrollbar(bool);
270
271     virtual void updateScrollCorner();
272     virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE;
273
274     // Scroll the content by blitting the pixels.
275     virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect);
276     // Scroll the content by invalidating everything.
277     virtual void scrollContentsSlowPath(const IntRect& updateRect);
278
279     void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
280
281     // Subclassed by FrameView to check the writing-mode of the document.
282     virtual bool isVerticalDocument() const { return true; }
283     virtual bool isFlippedDocument() const { return false; }
284
285     // Called to update the scrollbars to accurately reflect the state of the view.
286     void updateScrollbars(const IntSize& desiredOffset);
287
288     IntSize excludeScrollbars(const IntSize&) const;
289
290 private:
291     RefPtr<Scrollbar> m_horizontalScrollbar;
292     RefPtr<Scrollbar> m_verticalScrollbar;
293     ScrollbarMode m_horizontalScrollbarMode;
294     ScrollbarMode m_verticalScrollbarMode;
295
296     bool m_horizontalScrollbarLock;
297     bool m_verticalScrollbarLock;
298
299     HashSet<RefPtr<Widget> > m_children;
300
301     // This bool is unused on Mac OS because we directly ask the platform widget
302     // whether it is safe to blit on scroll.
303     bool m_canBlitOnScroll;
304
305     IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
306     IntPoint m_cachedScrollPosition;
307     IntSize m_contentsSize;
308
309     int m_scrollbarsAvoidingResizer;
310     bool m_scrollbarsSuppressed;
311
312     bool m_inUpdateScrollbars;
313     unsigned m_updateScrollbarsPass;
314
315     IntPoint m_panScrollIconPoint;
316     bool m_drawPanScrollIcon;
317
318     bool m_paintsEntireContents;
319     bool m_clipsRepaints;
320
321     void init();
322     void destroy();
323
324     IntRect rectToCopyOnScroll() const;
325
326     // Called when the scroll position within this view changes.  FrameView overrides this to generate repaint invalidations.
327     virtual void repaintFixedElementsAfterScrolling() { }
328     virtual void updateFixedElementsAfterScrolling() { }
329
330     void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
331     void updateOverhangAreas();
332
333     virtual int pageStep(ScrollbarOrientation) const OVERRIDE;
334 }; // class ScrollView
335
336 DEFINE_TYPE_CASTS(ScrollView, Widget, widget, widget->isScrollView(), widget.isScrollView());
337
338 } // namespace WebCore
339
340 #endif // ScrollView_h