Home | History | Annotate | Download | only in platform
      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 "core/platform/Scrollbar.h"
     30 #include "wtf/Vector.h"
     31 
     32 namespace WebCore {
     33 
     34 class FloatPoint;
     35 class GraphicsContext;
     36 class GraphicsLayer;
     37 class PlatformGestureEvent;
     38 class PlatformWheelEvent;
     39 class ScrollAnimator;
     40 
     41 class ScrollableArea {
     42 public:
     43     static int pixelsPerLineStep();
     44     static float minFractionToStepWhenPaging();
     45     static int maxOverlapBetweenPages();
     46 
     47     bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1);
     48     void scrollToOffsetWithoutAnimation(const FloatPoint&);
     49     void scrollToOffsetWithoutAnimation(ScrollbarOrientation, float offset);
     50 
     51     // Should be called when the scroll position changes externally, for example if the scroll layer position
     52     // is updated on the scrolling thread and we need to notify the main thread.
     53     void notifyScrollPositionChanged(const IntPoint&);
     54 
     55     // Allows subclasses to handle scroll position updates themselves. If this member function
     56     // returns true, the scrollable area won't actually update the scroll position and instead
     57     // expect it to happen sometime in the future.
     58     virtual bool requestScrollPositionUpdate(const IntPoint&) { return false; }
     59 
     60     bool handleWheelEvent(const PlatformWheelEvent&);
     61 
     62     // Functions for controlling if you can scroll past the end of the document.
     63     bool constrainsScrollingToContentEdge() const { return m_constrainsScrollingToContentEdge; }
     64     void setConstrainsScrollingToContentEdge(bool constrainsScrollingToContentEdge) { m_constrainsScrollingToContentEdge = constrainsScrollingToContentEdge; }
     65 
     66     void setVerticalScrollElasticity(ScrollElasticity scrollElasticity) { m_verticalScrollElasticity = scrollElasticity; }
     67     ScrollElasticity verticalScrollElasticity() const { return static_cast<ScrollElasticity>(m_verticalScrollElasticity); }
     68 
     69     void setHorizontalScrollElasticity(ScrollElasticity scrollElasticity) { m_horizontalScrollElasticity = scrollElasticity; }
     70     ScrollElasticity horizontalScrollElasticity() const { return static_cast<ScrollElasticity>(m_horizontalScrollElasticity); }
     71 
     72     bool inLiveResize() const { return m_inLiveResize; }
     73     void willStartLiveResize();
     74     void willEndLiveResize();
     75 
     76     void contentAreaWillPaint() const;
     77     void mouseEnteredContentArea() const;
     78     void mouseExitedContentArea() const;
     79     void mouseMovedInContentArea() const;
     80     void mouseEnteredScrollbar(Scrollbar*) const;
     81     void mouseExitedScrollbar(Scrollbar*) const;
     82     void contentAreaDidShow() const;
     83     void contentAreaDidHide() const;
     84 
     85     void finishCurrentScrollAnimations() const;
     86 
     87     void didAddVerticalScrollbar(Scrollbar*);
     88     void willRemoveVerticalScrollbar(Scrollbar*);
     89     virtual void didAddHorizontalScrollbar(Scrollbar*);
     90     virtual void willRemoveHorizontalScrollbar(Scrollbar*);
     91 
     92     virtual void contentsResized();
     93 
     94     bool hasOverlayScrollbars() const;
     95     void setScrollbarOverlayStyle(ScrollbarOverlayStyle);
     96     ScrollbarOverlayStyle scrollbarOverlayStyle() const { return static_cast<ScrollbarOverlayStyle>(m_scrollbarOverlayStyle); }
     97 
     98     // This getter will create a ScrollAnimator if it doesn't already exist.
     99     ScrollAnimator* scrollAnimator() const;
    100 
    101     // This getter will return null if the ScrollAnimator hasn't been created yet.
    102     ScrollAnimator* existingScrollAnimator() const { return m_scrollAnimator.get(); }
    103 
    104     const IntPoint& scrollOrigin() const { return m_scrollOrigin; }
    105     bool scrollOriginChanged() const { return m_scrollOriginChanged; }
    106 
    107     virtual bool isActive() const = 0;
    108     virtual int scrollSize(ScrollbarOrientation) const = 0;
    109     virtual void invalidateScrollbar(Scrollbar*, const IntRect&);
    110     virtual bool isScrollCornerVisible() const = 0;
    111     virtual IntRect scrollCornerRect() const = 0;
    112     virtual void invalidateScrollCorner(const IntRect&);
    113     virtual void getTickmarks(Vector<IntRect>&) const { }
    114 
    115     // Convert points and rects between the scrollbar and its containing view.
    116     // The client needs to implement these in order to be aware of layout effects
    117     // like CSS transforms.
    118     virtual IntRect convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
    119     {
    120         return scrollbar->Widget::convertToContainingView(scrollbarRect);
    121     }
    122     virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
    123     {
    124         return scrollbar->Widget::convertFromContainingView(parentRect);
    125     }
    126     virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
    127     {
    128         return scrollbar->Widget::convertToContainingView(scrollbarPoint);
    129     }
    130     virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
    131     {
    132         return scrollbar->Widget::convertFromContainingView(parentPoint);
    133     }
    134 
    135     virtual Scrollbar* horizontalScrollbar() const { return 0; }
    136     virtual Scrollbar* verticalScrollbar() const { return 0; }
    137 
    138     // scrollPosition is relative to the scrollOrigin. i.e. If the page is RTL
    139     // then scrollPosition will be negative.
    140     virtual IntPoint scrollPosition() const = 0;
    141     virtual IntPoint minimumScrollPosition() const = 0;
    142     virtual IntPoint maximumScrollPosition() const = 0;
    143 
    144     enum VisibleContentRectIncludesScrollbars { ExcludeScrollbars, IncludeScrollbars };
    145     virtual IntRect visibleContentRect(VisibleContentRectIncludesScrollbars = ExcludeScrollbars) const;
    146     virtual int visibleHeight() const = 0;
    147     virtual int visibleWidth() const = 0;
    148     virtual IntSize contentsSize() const = 0;
    149     virtual IntSize overhangAmount() const { return IntSize(); }
    150     virtual IntPoint lastKnownMousePosition() const { return IntPoint(); }
    151 
    152     virtual bool shouldSuspendScrollAnimations() const { return true; }
    153     virtual void scrollbarStyleChanged(int /*newStyle*/, bool /*forceUpdate*/) { }
    154 
    155     virtual bool scrollbarsCanBeActive() const = 0;
    156 
    157     // Note that this only returns scrollable areas that can actually be scrolled.
    158     virtual ScrollableArea* enclosingScrollableArea() const = 0;
    159 
    160     // Returns the bounding box of this scrollable area, in the coordinate system of the enclosing scroll view.
    161     virtual IntRect scrollableAreaBoundingBox() const = 0;
    162 
    163     virtual bool shouldRubberBandInDirection(ScrollDirection) const { return true; }
    164     virtual bool isRubberBandInProgress() const { return false; }
    165 
    166     virtual bool scrollAnimatorEnabled() const { return false; }
    167 
    168     // NOTE: Only called from Internals for testing.
    169     void setScrollOffsetFromInternals(const IntPoint&);
    170 
    171     IntPoint clampScrollPosition(const IntPoint&) const;
    172 
    173     // Let subclasses provide a way of asking for and servicing scroll
    174     // animations.
    175     virtual bool scheduleAnimation() { return false; }
    176     void serviceScrollAnimations();
    177 
    178     virtual bool usesCompositedScrolling() const { return false; }
    179     virtual void updateNeedsCompositedScrolling() { }
    180 
    181     virtual bool userInputScrollable(ScrollbarOrientation) const = 0;
    182 
    183     // Convenience functions
    184     int scrollPosition(ScrollbarOrientation orientation) { return orientation == HorizontalScrollbar ? scrollPosition().x() : scrollPosition().y(); }
    185     int minimumScrollPosition(ScrollbarOrientation orientation) { return orientation == HorizontalScrollbar ? minimumScrollPosition().x() : minimumScrollPosition().y(); }
    186     int maximumScrollPosition(ScrollbarOrientation orientation) { return orientation == HorizontalScrollbar ? maximumScrollPosition().x() : maximumScrollPosition().y(); }
    187     int clampScrollPosition(ScrollbarOrientation orientation, int pos)  { return std::max(std::min(pos, maximumScrollPosition(orientation)), minimumScrollPosition(orientation)); }
    188 
    189 protected:
    190     ScrollableArea();
    191     virtual ~ScrollableArea();
    192 
    193     void setScrollOrigin(const IntPoint&);
    194     void resetScrollOriginChanged() { m_scrollOriginChanged = false; }
    195 
    196     virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) = 0;
    197     virtual void invalidateScrollCornerRect(const IntRect&) = 0;
    198 
    199     friend class ScrollingCoordinator;
    200     virtual GraphicsLayer* layerForScrolling() const { return 0; }
    201     virtual GraphicsLayer* layerForHorizontalScrollbar() const { return 0; }
    202     virtual GraphicsLayer* layerForVerticalScrollbar() const { return 0; }
    203     virtual GraphicsLayer* layerForScrollCorner() const { return 0; }
    204 #if ENABLE(RUBBER_BANDING)
    205     virtual GraphicsLayer* layerForOverhangAreas() const { return 0; }
    206 #endif
    207     bool hasLayerForHorizontalScrollbar() const;
    208     bool hasLayerForVerticalScrollbar() const;
    209     bool hasLayerForScrollCorner() const;
    210 
    211 private:
    212     void scrollPositionChanged(const IntPoint&);
    213 
    214     // NOTE: Only called from the ScrollAnimator.
    215     friend class ScrollAnimator;
    216     void setScrollOffsetFromAnimation(const IntPoint&);
    217 
    218     // This function should be overriden by subclasses to perform the actual
    219     // scroll of the content.
    220     virtual void setScrollOffset(const IntPoint&) = 0;
    221 
    222     virtual int lineStep(ScrollbarOrientation) const;
    223     virtual int pageStep(ScrollbarOrientation) const = 0;
    224     virtual int documentStep(ScrollbarOrientation) const;
    225     virtual float pixelStep(ScrollbarOrientation) const;
    226 
    227     mutable OwnPtr<ScrollAnimator> m_scrollAnimator;
    228     unsigned m_constrainsScrollingToContentEdge : 1;
    229 
    230     unsigned m_inLiveResize : 1;
    231 
    232     unsigned m_verticalScrollElasticity : 2; // ScrollElasticity
    233     unsigned m_horizontalScrollElasticity : 2; // ScrollElasticity
    234 
    235     unsigned m_scrollbarOverlayStyle : 2; // ScrollbarOverlayStyle
    236 
    237     unsigned m_scrollOriginChanged : 1;
    238 
    239     // There are 8 possible combinations of writing mode and direction. Scroll origin will be non-zero in the x or y axis
    240     // if there is any reversed direction or writing-mode. The combinations are:
    241     // writing-mode / direction     scrollOrigin.x() set    scrollOrigin.y() set
    242     // horizontal-tb / ltr          NO                      NO
    243     // horizontal-tb / rtl          YES                     NO
    244     // horizontal-bt / ltr          NO                      YES
    245     // horizontal-bt / rtl          YES                     YES
    246     // vertical-lr / ltr            NO                      NO
    247     // vertical-lr / rtl            NO                      YES
    248     // vertical-rl / ltr            YES                     NO
    249     // vertical-rl / rtl            YES                     YES
    250     IntPoint m_scrollOrigin;
    251 };
    252 
    253 } // namespace WebCore
    254 
    255 #endif // ScrollableArea_h
    256