Home | History | Annotate | Download | only in scroll
      1 /*
      2  * Copyright (C) 2008, 2011 Apple Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef ScrollableArea_h
     27 #define ScrollableArea_h
     28 
     29 #include "platform/PlatformExport.h"
     30 #include "platform/scroll//ScrollAnimator.h"
     31 #include "platform/scroll/Scrollbar.h"
     32 #include "wtf/Noncopyable.h"
     33 #include "wtf/Vector.h"
     34 
     35 namespace WebCore {
     36 
     37 class FloatPoint;
     38 class GraphicsContext;
     39 class GraphicsLayer;
     40 class PlatformGestureEvent;
     41 class PlatformWheelEvent;
     42 class ScrollAnimator;
     43 
     44 class PLATFORM_EXPORT ScrollableArea {
     45     WTF_MAKE_NONCOPYABLE(ScrollableArea);
     46 public:
     47     static int pixelsPerLineStep();
     48     static float minFractionToStepWhenPaging();
     49     static int maxOverlapBetweenPages();
     50 
     51     bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1);
     52     void scrollToOffsetWithoutAnimation(const FloatPoint&);
     53     void scrollToOffsetWithoutAnimation(ScrollbarOrientation, float offset);
     54 
     55     // Should be called when the scroll position changes externally, for example if the scroll layer position
     56     // is updated on the scrolling thread and we need to notify the main thread.
     57     void notifyScrollPositionChanged(const IntPoint&);
     58 
     59     bool handleWheelEvent(const PlatformWheelEvent&);
     60 
     61     // Functions for controlling if you can scroll past the end of the document.
     62     bool constrainsScrollingToContentEdge() const { return m_constrainsScrollingToContentEdge; }
     63     void setConstrainsScrollingToContentEdge(bool constrainsScrollingToContentEdge) { m_constrainsScrollingToContentEdge = constrainsScrollingToContentEdge; }
     64 
     65     void setVerticalScrollElasticity(ScrollElasticity scrollElasticity) { m_verticalScrollElasticity = scrollElasticity; }
     66     ScrollElasticity verticalScrollElasticity() const { return static_cast<ScrollElasticity>(m_verticalScrollElasticity); }
     67 
     68     void setHorizontalScrollElasticity(ScrollElasticity scrollElasticity) { m_horizontalScrollElasticity = scrollElasticity; }
     69     ScrollElasticity horizontalScrollElasticity() const { return static_cast<ScrollElasticity>(m_horizontalScrollElasticity); }
     70 
     71     bool inLiveResize() const { return m_inLiveResize; }
     72     void willStartLiveResize();
     73     void willEndLiveResize();
     74 
     75     void contentAreaWillPaint() const;
     76     void mouseEnteredContentArea() const;
     77     void mouseExitedContentArea() const;
     78     void mouseMovedInContentArea() const;
     79     void mouseEnteredScrollbar(Scrollbar*) const;
     80     void mouseExitedScrollbar(Scrollbar*) const;
     81     void contentAreaDidShow() const;
     82     void contentAreaDidHide() const;
     83 
     84     void finishCurrentScrollAnimations() const;
     85 
     86     virtual void didAddScrollbar(Scrollbar*, ScrollbarOrientation);
     87     virtual void willRemoveScrollbar(Scrollbar*, ScrollbarOrientation);
     88 
     89     virtual void contentsResized();
     90 
     91     bool hasOverlayScrollbars() const;
     92     void setScrollbarOverlayStyle(ScrollbarOverlayStyle);
     93     ScrollbarOverlayStyle scrollbarOverlayStyle() const { return static_cast<ScrollbarOverlayStyle>(m_scrollbarOverlayStyle); }
     94 
     95     // This getter will create a ScrollAnimator if it doesn't already exist.
     96     ScrollAnimator* scrollAnimator() const;
     97 
     98     // This getter will return null if the ScrollAnimator hasn't been created yet.
     99     ScrollAnimator* existingScrollAnimator() const { return m_scrollAnimator.get(); }
    100 
    101     const IntPoint& scrollOrigin() const { return m_scrollOrigin; }
    102     bool scrollOriginChanged() const { return m_scrollOriginChanged; }
    103 
    104     virtual bool isActive() const = 0;
    105     virtual int scrollSize(ScrollbarOrientation) const = 0;
    106     virtual void invalidateScrollbar(Scrollbar*, const IntRect&);
    107     virtual bool isScrollCornerVisible() const = 0;
    108     virtual IntRect scrollCornerRect() const = 0;
    109     virtual void invalidateScrollCorner(const IntRect&);
    110     virtual void getTickmarks(Vector<IntRect>&) const { }
    111 
    112     // Convert points and rects between the scrollbar and its containing view.
    113     // The client needs to implement these in order to be aware of layout effects
    114     // like CSS transforms.
    115     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
    116     {
    117         return scrollbar->Widget::convertToContainingView(scrollbarRect);
    118     }
    119     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
    120     {
    121         return scrollbar->Widget::convertFromContainingView(parentRect);
    122     }
    123     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
    124     {
    125         return scrollbar->Widget::convertToContainingView(scrollbarPoint);
    126     }
    127     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
    128     {
    129         return scrollbar->Widget::convertFromContainingView(parentPoint);
    130     }
    131 
    132     virtual Scrollbar* horizontalScrollbar() const { return 0; }
    133     virtual Scrollbar* verticalScrollbar() const { return 0; }
    134 
    135     // scrollPosition is relative to the scrollOrigin. i.e. If the page is RTL
    136     // then scrollPosition will be negative.
    137     virtual IntPoint scrollPosition() const = 0;
    138     virtual IntPoint minimumScrollPosition() const = 0;
    139     virtual IntPoint maximumScrollPosition() const = 0;
    140 
    141     enum IncludeScrollbarsInRect { ExcludeScrollbars, IncludeScrollbars };
    142     virtual IntRect visibleContentRect(IncludeScrollbarsInRect = ExcludeScrollbars) const;
    143     virtual int visibleHeight() const = 0;
    144     virtual int visibleWidth() const = 0;
    145     virtual IntSize contentsSize() const = 0;
    146     virtual IntSize overhangAmount() const { return IntSize(); }
    147     virtual IntPoint lastKnownMousePosition() const { return IntPoint(); }
    148 
    149     virtual bool shouldSuspendScrollAnimations() const { return true; }
    150     virtual void scrollbarStyleChanged(int /*newStyle*/, bool /*forceUpdate*/) { }
    151 
    152     virtual bool scrollbarsCanBeActive() const = 0;
    153 
    154     // Note that this only returns scrollable areas that can actually be scrolled.
    155     virtual ScrollableArea* enclosingScrollableArea() const = 0;
    156 
    157     // Returns the bounding box of this scrollable area, in the coordinate system of the enclosing scroll view.
    158     virtual IntRect scrollableAreaBoundingBox() const = 0;
    159 
    160     virtual bool shouldRubberBandInDirection(ScrollDirection) const { return true; }
    161     virtual bool isRubberBandInProgress() const { return false; }
    162 
    163     virtual bool scrollAnimatorEnabled() const { return false; }
    164 
    165     // NOTE: Only called from Internals for testing.
    166     void setScrollOffsetFromInternals(const IntPoint&);
    167 
    168     IntPoint clampScrollPosition(const IntPoint&) const;
    169 
    170     // Let subclasses provide a way of asking for and servicing scroll
    171     // animations.
    172     virtual bool scheduleAnimation() { return false; }
    173     void serviceScrollAnimations();
    174 
    175     virtual bool usesCompositedScrolling() const { return false; }
    176     virtual void updateNeedsCompositedScrolling() { }
    177     virtual void updateHasVisibleNonLayerContent() { }
    178 
    179     virtual bool userInputScrollable(ScrollbarOrientation) const = 0;
    180     virtual bool shouldPlaceVerticalScrollbarOnLeft() const = 0;
    181 
    182     // Convenience functions
    183     int scrollPosition(ScrollbarOrientation orientation) { return orientation == HorizontalScrollbar ? scrollPosition().x() : scrollPosition().y(); }
    184     int minimumScrollPosition(ScrollbarOrientation orientation) { return orientation == HorizontalScrollbar ? minimumScrollPosition().x() : minimumScrollPosition().y(); }
    185     int maximumScrollPosition(ScrollbarOrientation orientation) { return orientation == HorizontalScrollbar ? maximumScrollPosition().x() : maximumScrollPosition().y(); }
    186     int clampScrollPosition(ScrollbarOrientation orientation, int pos)  { return std::max(std::min(pos, maximumScrollPosition(orientation)), minimumScrollPosition(orientation)); }
    187 
    188 protected:
    189     ScrollableArea();
    190     virtual ~ScrollableArea();
    191 
    192     void setScrollOrigin(const IntPoint&);
    193     void resetScrollOriginChanged() { m_scrollOriginChanged = false; }
    194 
    195     virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) = 0;
    196     virtual void invalidateScrollCornerRect(const IntRect&) = 0;
    197 
    198     friend class ScrollingCoordinator;
    199     virtual GraphicsLayer* layerForScrolling() const { return 0; }
    200     virtual GraphicsLayer* layerForHorizontalScrollbar() const { return 0; }
    201     virtual GraphicsLayer* layerForVerticalScrollbar() const { return 0; }
    202     virtual GraphicsLayer* layerForScrollCorner() const { return 0; }
    203     bool hasLayerForHorizontalScrollbar() const;
    204     bool hasLayerForVerticalScrollbar() const;
    205     bool hasLayerForScrollCorner() const;
    206 
    207 private:
    208     void scrollPositionChanged(const IntPoint&);
    209 
    210     // NOTE: Only called from the ScrollAnimator.
    211     friend class ScrollAnimator;
    212     void setScrollOffsetFromAnimation(const IntPoint&);
    213 
    214     // This function should be overriden by subclasses to perform the actual
    215     // scroll of the content.
    216     virtual void setScrollOffset(const IntPoint&) = 0;
    217 
    218     virtual int lineStep(ScrollbarOrientation) const;
    219     virtual int pageStep(ScrollbarOrientation) const = 0;
    220     virtual int documentStep(ScrollbarOrientation) const;
    221     virtual float pixelStep(ScrollbarOrientation) const;
    222 
    223     mutable OwnPtr<ScrollAnimator> m_scrollAnimator;
    224     unsigned m_constrainsScrollingToContentEdge : 1;
    225 
    226     unsigned m_inLiveResize : 1;
    227 
    228     unsigned m_verticalScrollElasticity : 2; // ScrollElasticity
    229     unsigned m_horizontalScrollElasticity : 2; // ScrollElasticity
    230 
    231     unsigned m_scrollbarOverlayStyle : 2; // ScrollbarOverlayStyle
    232 
    233     unsigned m_scrollOriginChanged : 1;
    234 
    235     // There are 8 possible combinations of writing mode and direction. Scroll origin will be non-zero in the x or y axis
    236     // if there is any reversed direction or writing-mode. The combinations are:
    237     // writing-mode / direction     scrollOrigin.x() set    scrollOrigin.y() set
    238     // horizontal-tb / ltr          NO                      NO
    239     // horizontal-tb / rtl          YES                     NO
    240     // horizontal-bt / ltr          NO                      YES
    241     // horizontal-bt / rtl          YES                     YES
    242     // vertical-lr / ltr            NO                      NO
    243     // vertical-lr / rtl            NO                      YES
    244     // vertical-rl / ltr            YES                     NO
    245     // vertical-rl / rtl            YES                     YES
    246     IntPoint m_scrollOrigin;
    247 };
    248 
    249 } // namespace WebCore
    250 
    251 #endif // ScrollableArea_h
    252