Home | History | Annotate | Download | only in scroll
      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 #include "wtf/TemporaryChange.h"
     39 
     40 namespace blink {
     41 
     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() OVERRIDE;
     53     virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE;
     54     virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE;
     55 
     56     virtual void notifyPageThatContentAreaWillPaint() const;
     57 
     58     // The window that hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the
     59     // host window in the window's coordinate space.
     60     virtual HostWindow* hostWindow() const = 0;
     61 
     62     // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll.
     63     virtual IntRect windowClipRect(IncludeScrollbarsInRect = ExcludeScrollbars) const = 0;
     64 
     65     // Functions for child manipulation and inspection.
     66     const HashSet<RefPtr<Widget> >* children() const { return &m_children; }
     67     virtual void addChild(PassRefPtr<Widget>);
     68     virtual void removeChild(Widget*);
     69 
     70     // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions
     71     // can be used to obtain those scrollbars.
     72     virtual Scrollbar* horizontalScrollbar() const OVERRIDE { return m_horizontalScrollbar.get(); }
     73     virtual Scrollbar* verticalScrollbar() const OVERRIDE { return m_verticalScrollbar.get(); }
     74     bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; }
     75 
     76     void positionScrollbarLayers();
     77 
     78     // Functions for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of
     79     // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar.
     80     // Auto means show a scrollbar only when one is needed.
     81     // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native
     82     // widget may choose not to honor the requested modes.
     83     void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool horizontalLock = false, bool verticalLock = false);
     84     void setHorizontalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(mode, verticalScrollbarMode(), lock, verticalScrollbarLock()); }
     85     void setVerticalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(horizontalScrollbarMode(), mode, horizontalScrollbarLock(), lock); };
     86     void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const;
     87     ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; }
     88     ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; }
     89 
     90     void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; }
     91     bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; }
     92     void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; }
     93     bool verticalScrollbarLock() const { return m_verticalScrollbarLock; }
     94 
     95     void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; }
     96 
     97     virtual void setCanHaveScrollbars(bool);
     98     bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; }
     99 
    100     // By default, paint events are clipped to the visible area.  If set to
    101     // false, paint events are no longer clipped.
    102     bool clipsPaintInvalidations() const { return m_clipsRepaints; }
    103     void setClipsRepaints(bool);
    104 
    105     // Overridden by FrameView to create custom CSS scrollbars if applicable.
    106     virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation);
    107 
    108     // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width
    109     // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be
    110     // included.
    111     virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
    112     IntSize visibleSize() const { return visibleContentRect().size(); }
    113 
    114     // visibleContentRect().size() is computed from unscaledVisibleContentSize() divided by the value of visibleContentScaleFactor.
    115     // For the main frame, visibleContentScaleFactor is equal to the page's pageScaleFactor; it's 1 otherwise.
    116     IntSize unscaledVisibleContentSize(IncludeScrollbarsInRect = ExcludeScrollbars) const;
    117     virtual float visibleContentScaleFactor() const { return 1; }
    118 
    119     // Offset used to convert incoming input events while emulating device metics.
    120     virtual IntSize inputEventsOffsetForEmulation() const { return IntSize(); }
    121 
    122     // Scale used to convert incoming input events. Usually the same as visibleContentScaleFactor(), unless specifically changed.
    123     virtual float inputEventsScaleFactor() const { return visibleContentScaleFactor(); }
    124 
    125     // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height
    126     // values).
    127     virtual IntSize contentsSize() const OVERRIDE; // Always at least as big as the visibleWidth()/visibleHeight().
    128     int contentsWidth() const { return contentsSize().width(); }
    129     int contentsHeight() const { return contentsSize().height(); }
    130     virtual void setContentsSize(const IntSize&);
    131 
    132     // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values).
    133     virtual IntPoint scrollPosition() const OVERRIDE { return visibleContentRect().location(); }
    134     IntSize scrollOffset() const { return toIntSize(visibleContentRect().location()); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes.
    135     IntSize pendingScrollDelta() const { return m_pendingScrollDelta; }
    136     virtual IntPoint maximumScrollPosition() const OVERRIDE; // The maximum position we can be scrolled to.
    137     virtual IntPoint minimumScrollPosition() const OVERRIDE; // The minimum position we can be scrolled to.
    138     // Adjust the passed in scroll position to keep it between the minimum and maximum positions.
    139     IntPoint adjustScrollPositionWithinRange(const IntPoint&) const;
    140     int scrollX() const { return scrollPosition().x(); }
    141     int scrollY() const { return scrollPosition().y(); }
    142 
    143     virtual IntSize overhangAmount() const OVERRIDE;
    144 
    145     void cacheCurrentScrollPosition() { m_cachedScrollPosition = scrollPosition(); }
    146     IntPoint cachedScrollPosition() const { return m_cachedScrollPosition; }
    147 
    148     // Functions for scrolling the view.
    149     virtual void setScrollPosition(const IntPoint&, ScrollBehavior = ScrollBehaviorInstant);
    150     void scrollBy(const IntSize& s, ScrollBehavior behavior = ScrollBehaviorInstant)
    151     {
    152         return setScrollPosition(scrollPosition() + s, behavior);
    153     }
    154 
    155     bool scroll(ScrollDirection, ScrollGranularity);
    156 
    157     // Scroll the actual contents of the view (either blitting or invalidating as needed).
    158     void scrollContents(const IntSize& scrollDelta);
    159 
    160     // This gives us a means of blocking painting on our scrollbars until the first layout has occurred.
    161     void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false);
    162     bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; }
    163 
    164     IntPoint rootViewToContents(const IntPoint&) const;
    165     IntPoint contentsToRootView(const IntPoint&) const;
    166     IntRect rootViewToContents(const IntRect&) const;
    167     IntRect contentsToRootView(const IntRect&) const;
    168 
    169     // Event coordinates are assumed to be in the coordinate space of a window that contains
    170     // the entire widget hierarchy. It is up to the platform to decide what the precise definition
    171     // of containing window is. (For example on Mac it is the containing NSWindow.)
    172     IntPoint windowToContents(const IntPoint&) const;
    173     FloatPoint windowToContents(const FloatPoint&) const;
    174     IntPoint contentsToWindow(const IntPoint&) const;
    175     IntRect windowToContents(const IntRect&) const;
    176     IntRect contentsToWindow(const IntRect&) const;
    177 
    178     // Functions for converting to screen coordinates.
    179     IntRect contentsToScreen(const IntRect&) const;
    180 
    181     // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac
    182     // for example.
    183     virtual IntRect windowResizerRect() const { return IntRect(); }
    184     bool containsScrollbarsAvoidingResizer() const;
    185     void adjustScrollbarsAvoidingResizerCount(int overlapDelta);
    186     void windowResizerRectChanged();
    187 
    188     virtual void setParent(Widget*) OVERRIDE; // Overridden to update the overlapping scrollbar count.
    189 
    190     // Called when our frame rect changes (or the rect/scroll position of an ancestor changes).
    191     virtual void frameRectsChanged() OVERRIDE;
    192 
    193     // Widget override to update our scrollbars and notify our contents of the resize.
    194     virtual void setFrameRect(const IntRect&) OVERRIDE;
    195 
    196     // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32).
    197     Scrollbar* scrollbarAtWindowPoint(const IntPoint& windowPoint);
    198     Scrollbar* scrollbarAtViewPoint(const IntPoint& viewPoint);
    199 
    200     virtual IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const OVERRIDE
    201     {
    202         IntPoint newPoint = point;
    203         if (!isScrollViewScrollbar(child))
    204             newPoint = point - scrollOffset();
    205         newPoint.moveBy(child->location());
    206         return newPoint;
    207     }
    208 
    209     virtual IntPoint convertSelfToChild(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     // Widget override. Handles painting of the contents of the view as well as the scrollbars.
    219     virtual void paint(GraphicsContext*, const IntRect&) OVERRIDE;
    220     void paintScrollbars(GraphicsContext*, const IntRect&);
    221 
    222     // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden.
    223     virtual void show() OVERRIDE;
    224     virtual void hide() OVERRIDE;
    225     virtual void setParentVisible(bool) OVERRIDE;
    226 
    227     // Pan scrolling.
    228     static const int noPanScrollRadius = 15;
    229     void addPanScrollIcon(const IntPoint&);
    230     void removePanScrollIcon();
    231     void paintPanScrollIcon(GraphicsContext*);
    232 
    233     virtual bool isPointInScrollbarCorner(const IntPoint&);
    234     virtual bool scrollbarCornerPresent() const;
    235     virtual IntRect scrollCornerRect() const OVERRIDE;
    236     virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect);
    237     virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&);
    238 
    239     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const OVERRIDE;
    240     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const OVERRIDE;
    241     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const OVERRIDE;
    242     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const OVERRIDE;
    243 
    244     void calculateAndPaintOverhangAreas(GraphicsContext*, const IntRect& dirtyRect);
    245     void calculateAndPaintOverhangBackground(GraphicsContext*, const IntRect& dirtyRect);
    246 
    247     virtual bool isScrollView() const OVERRIDE FINAL { return true; }
    248 
    249 protected:
    250     ScrollView();
    251 
    252     // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
    253     virtual void scrollTo(const IntSize& newOffset);
    254 
    255     virtual void contentRectangleForPaintInvalidation(const IntRect&);
    256     virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0;
    257 
    258     virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect);
    259 
    260     virtual void scrollbarExistenceDidChange() = 0;
    261     // These functions are used to create/destroy scrollbars.
    262     void setHasHorizontalScrollbar(bool);
    263     void setHasVerticalScrollbar(bool);
    264 
    265     virtual void updateScrollCorner();
    266     virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE;
    267 
    268     virtual void scrollContentsIfNeeded();
    269     // Scroll the content by via the compositor.
    270     virtual bool scrollContentsFastPath(const IntSize& scrollDelta) { return true; }
    271     // Scroll the content by invalidating everything.
    272     virtual void scrollContentsSlowPath(const IntRect& updateRect);
    273 
    274     void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously);
    275 
    276     // Subclassed by FrameView to check the writing-mode of the document.
    277     virtual bool isVerticalDocument() const { return true; }
    278     virtual bool isFlippedDocument() const { return false; }
    279 
    280     enum ComputeScrollbarExistenceOption {
    281         FirstPass,
    282         Incremental
    283     };
    284     void computeScrollbarExistence(bool& newHasHorizontalScrollbar, bool& newHasVerticalScrollbar, const IntSize& docSize, ComputeScrollbarExistenceOption = FirstPass) const;
    285     void updateScrollbarGeometry();
    286 
    287     // Called to update the scrollbars to accurately reflect the state of the view.
    288     void updateScrollbars(const IntSize& desiredOffset);
    289 
    290     IntSize excludeScrollbars(const IntSize&) const;
    291 
    292     class InUpdateScrollbarsScope {
    293     public:
    294         explicit InUpdateScrollbarsScope(ScrollView* view)
    295             : m_scope(view->m_inUpdateScrollbars, true)
    296         { }
    297     private:
    298         TemporaryChange<bool> m_scope;
    299     };
    300 
    301     virtual bool scrollbarsDisabled() const { return false; }
    302 
    303 private:
    304     bool adjustScrollbarExistence(ComputeScrollbarExistenceOption = FirstPass);
    305     void adjustScrollbarOpacity();
    306     // FIXME(bokan): setScrollOffset, setScrollPosition, scrollTo, scrollToOffsetWithoutAnimation,
    307     // notifyScrollPositionChanged...there's too many ways to scroll this class. This needs
    308     // some cleanup.
    309     void setScrollOffsetFromUpdateScrollbars(const IntSize&);
    310 
    311     RefPtr<Scrollbar> m_horizontalScrollbar;
    312     RefPtr<Scrollbar> m_verticalScrollbar;
    313     ScrollbarMode m_horizontalScrollbarMode;
    314     ScrollbarMode m_verticalScrollbarMode;
    315 
    316     bool m_horizontalScrollbarLock;
    317     bool m_verticalScrollbarLock;
    318 
    319     HashSet<RefPtr<Widget> > m_children;
    320 
    321     IntSize m_pendingScrollDelta;
    322     IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared.
    323     IntPoint m_cachedScrollPosition;
    324     IntSize m_contentsSize;
    325 
    326     int m_scrollbarsAvoidingResizer;
    327     bool m_scrollbarsSuppressed;
    328 
    329     bool m_inUpdateScrollbars;
    330 
    331     IntPoint m_panScrollIconPoint;
    332     bool m_drawPanScrollIcon;
    333 
    334     bool m_clipsRepaints;
    335 
    336     void init();
    337     void destroy();
    338 
    339     IntRect rectToCopyOnScroll() const;
    340 
    341     void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect);
    342     void updateOverhangAreas();
    343 }; // class ScrollView
    344 
    345 DEFINE_TYPE_CASTS(ScrollView, Widget, widget, widget->isScrollView(), widget.isScrollView());
    346 
    347 } // namespace blink
    348 
    349 #endif // ScrollView_h
    350