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