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