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