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 "core/platform/ScrollTypes.h" 31 #include "core/platform/ScrollableArea.h" 32 #include "core/platform/Scrollbar.h" 33 #include "core/platform/Widget.h" 34 #include "core/platform/graphics/IntRect.h" 35 36 #include "wtf/HashSet.h" 37 38 namespace WebCore { 39 40 class HostWindow; 41 class Scrollbar; 42 43 class ScrollView : public Widget, public ScrollableArea { 44 public: 45 ~ScrollView(); 46 47 // ScrollableArea functions. 48 virtual int scrollSize(ScrollbarOrientation) const OVERRIDE; 49 virtual void setScrollOffset(const IntPoint&) OVERRIDE; 50 virtual bool isScrollCornerVisible() const OVERRIDE; 51 virtual void scrollbarStyleChanged(int newStyle, bool forceUpdate) OVERRIDE; 52 virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE; 53 54 virtual void notifyPageThatContentAreaWillPaint() const; 55 56 // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea. 57 virtual void scrollTo(const IntSize& newOffset); 58 59 // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the 60 // host window in the window's coordinate space. 61 virtual HostWindow* hostWindow() const = 0; 62 63 // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll. 64 virtual IntRect windowClipRect(bool clipToContents = true) const = 0; 65 66 // Functions for child manipulation and inspection. 67 const HashSet<RefPtr<Widget> >* children() const { return &m_children; } 68 virtual void addChild(PassRefPtr<Widget>); 69 virtual void removeChild(Widget*); 70 71 // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These functions 72 // can be used to obtain those scrollbars. 73 virtual Scrollbar* horizontalScrollbar() const OVERRIDE { return m_horizontalScrollbar.get(); } 74 virtual Scrollbar* verticalScrollbar() const OVERRIDE { return m_verticalScrollbar.get(); } 75 bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; } 76 77 void positionScrollbarLayers(); 78 79 // Functions for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of 80 // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar. 81 // Auto means show a scrollbar only when one is needed. 82 // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native 83 // widget may choose not to honor the requested modes. 84 void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool horizontalLock = false, bool verticalLock = false); 85 void setHorizontalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(mode, verticalScrollbarMode(), lock, verticalScrollbarLock()); } 86 void setVerticalScrollbarMode(ScrollbarMode mode, bool lock = false) { setScrollbarModes(horizontalScrollbarMode(), mode, horizontalScrollbarLock(), lock); }; 87 void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const; 88 ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; } 89 ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; } 90 91 void setHorizontalScrollbarLock(bool lock = true) { m_horizontalScrollbarLock = lock; } 92 bool horizontalScrollbarLock() const { return m_horizontalScrollbarLock; } 93 void setVerticalScrollbarLock(bool lock = true) { m_verticalScrollbarLock = lock; } 94 bool verticalScrollbarLock() const { return m_verticalScrollbarLock; } 95 96 void setScrollingModesLock(bool lock = true) { m_horizontalScrollbarLock = m_verticalScrollbarLock = lock; } 97 98 virtual void setCanHaveScrollbars(bool); 99 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; } 100 101 // By default you only receive paint events for the area that is visible. In the case of using a 102 // tiled backing store, this function can be set, so that the view paints the entire contents. 103 bool paintsEntireContents() const { return m_paintsEntireContents; } 104 void setPaintsEntireContents(bool); 105 106 // By default, paint events are clipped to the visible area. If set to 107 // false, paint events are no longer clipped. paintsEntireContents() implies !clipsRepaints(). 108 bool clipsRepaints() const { return m_clipsRepaints; } 109 void setClipsRepaints(bool); 110 111 // Overridden by FrameView to create custom CSS scrollbars if applicable. 112 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation); 113 114 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations 115 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent). 116 void setCanBlitOnScroll(bool); 117 bool canBlitOnScroll() 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(VisibleContentRectIncludesScrollbars = ExcludeScrollbars) const OVERRIDE; 123 IntSize visibleSize() const { return visibleContentRect().size(); } 124 virtual int visibleWidth() const OVERRIDE { return visibleContentRect().width(); } 125 virtual int visibleHeight() const OVERRIDE { 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(VisibleContentRectIncludesScrollbars = ExcludeScrollbars) const; 130 virtual float visibleContentScaleFactor() const { return 1; } 131 132 // Functions for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible 133 // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead. 134 IntSize layoutSize(VisibleContentRectIncludesScrollbars = ExcludeScrollbars) const; 135 int layoutWidth(VisibleContentRectIncludesScrollbars scrollbarInclusion = ExcludeScrollbars) const { return layoutSize(scrollbarInclusion).width(); } 136 int layoutHeight(VisibleContentRectIncludesScrollbars scrollbarInclusion = ExcludeScrollbars) const { return layoutSize(scrollbarInclusion).height(); } 137 IntSize fixedLayoutSize() const; 138 void setFixedLayoutSize(const IntSize&); 139 bool useFixedLayout() const; 140 void setUseFixedLayout(bool enable); 141 142 // Functions for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height 143 // values). 144 virtual IntSize contentsSize() const OVERRIDE; // Always at least as big as the visibleWidth()/visibleHeight(). 145 int contentsWidth() const { return contentsSize().width(); } 146 int contentsHeight() const { return contentsSize().height(); } 147 virtual void setContentsSize(const IntSize&); 148 149 // Functions for querying the current scrolled position (both as a point, a size, or as individual X and Y values). 150 virtual IntPoint scrollPosition() const OVERRIDE { return visibleContentRect().location(); } 151 IntSize scrollOffset() const { return toIntSize(visibleContentRect().location()); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes. 152 virtual IntPoint maximumScrollPosition() const OVERRIDE; // The maximum position we can be scrolled to. 153 virtual IntPoint minimumScrollPosition() const OVERRIDE; // The minimum position we can be scrolled to. 154 // Adjust the passed in scroll position to keep it between the minimum and maximum positions. 155 IntPoint adjustScrollPositionWithinRange(const IntPoint&) const; 156 int scrollX() const { return scrollPosition().x(); } 157 int scrollY() const { return scrollPosition().y(); } 158 159 virtual IntSize overhangAmount() const OVERRIDE; 160 161 void cacheCurrentScrollPosition() { m_cachedScrollPosition = scrollPosition(); } 162 IntPoint cachedScrollPosition() const { return m_cachedScrollPosition; } 163 164 // Functions for scrolling the view. 165 virtual void setScrollPosition(const IntPoint&); 166 void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); } 167 168 // A logical scroll that just ends up calling the corresponding physical scroll() based off the document's writing mode. 169 bool logicalScroll(ScrollLogicalDirection, ScrollGranularity); 170 171 // Scroll the actual contents of the view (either blitting or invalidating as needed). 172 void scrollContents(const IntSize& scrollDelta); 173 174 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred. 175 void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false); 176 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; } 177 178 IntPoint rootViewToContents(const IntPoint&) const; 179 IntPoint contentsToRootView(const IntPoint&) const; 180 IntRect rootViewToContents(const IntRect&) const; 181 IntRect contentsToRootView(const IntRect&) const; 182 183 // Event coordinates are assumed to be in the coordinate space of a window that contains 184 // the entire widget hierarchy. It is up to the platform to decide what the precise definition 185 // of containing window is. (For example on Mac it is the containing NSWindow.) 186 IntPoint windowToContents(const IntPoint&) const; 187 IntPoint contentsToWindow(const IntPoint&) const; 188 IntRect windowToContents(const IntRect&) const; 189 IntRect contentsToWindow(const IntRect&) const; 190 191 // Functions for converting to and from screen coordinates. 192 IntRect contentsToScreen(const IntRect&) const; 193 IntPoint screenToContents(const IntPoint&) const; 194 195 // 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 196 // we know that we are either not in a window right now or if that window is not visible. 197 bool isOffscreen() const; 198 199 // These functions are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac 200 // for example. 201 virtual IntRect windowResizerRect() const { return IntRect(); } 202 bool containsScrollbarsAvoidingResizer() const; 203 void adjustScrollbarsAvoidingResizerCount(int overlapDelta); 204 void windowResizerRectChanged(); 205 206 virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count. 207 208 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes). 209 virtual void frameRectsChanged(); 210 211 // Widget override to update our scrollbars and notify our contents of the resize. 212 virtual void setFrameRect(const IntRect&); 213 214 // Widget override to notify our contents of a cliprect change. 215 virtual void clipRectChanged() OVERRIDE; 216 217 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32). 218 Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint); 219 220 IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const 221 { 222 IntPoint newPoint = point; 223 if (!isScrollViewScrollbar(child)) 224 newPoint = point - scrollOffset(); 225 newPoint.moveBy(child->location()); 226 return newPoint; 227 } 228 229 IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const 230 { 231 IntPoint newPoint = point; 232 if (!isScrollViewScrollbar(child)) 233 newPoint = point + scrollOffset(); 234 newPoint.moveBy(-child->location()); 235 return newPoint; 236 } 237 238 // Widget override. Handles painting of the contents of the view as well as the scrollbars. 239 virtual void paint(GraphicsContext*, const IntRect&); 240 void paintScrollbars(GraphicsContext*, const IntRect&); 241 242 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden. 243 virtual void show(); 244 virtual void hide(); 245 virtual void setParentVisible(bool); 246 247 // Pan scrolling. 248 static const int noPanScrollRadius = 15; 249 void addPanScrollIcon(const IntPoint&); 250 void removePanScrollIcon(); 251 void paintPanScrollIcon(GraphicsContext*); 252 253 virtual bool isPointInScrollbarCorner(const IntPoint&); 254 virtual bool scrollbarCornerPresent() const; 255 virtual IntRect scrollCornerRect() const; 256 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect); 257 virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&); 258 259 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const OVERRIDE; 260 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const OVERRIDE; 261 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const OVERRIDE; 262 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const OVERRIDE; 263 264 void calculateAndPaintOverhangAreas(GraphicsContext*, const IntRect& dirtyRect); 265 266 virtual bool isScrollView() const OVERRIDE { return true; } 267 268 protected: 269 ScrollView(); 270 271 virtual void repaintContentRectangle(const IntRect&); 272 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0; 273 274 virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect); 275 276 virtual void visibleContentsResized() = 0; 277 // These functions are used to create/destroy scrollbars. 278 void setHasHorizontalScrollbar(bool); 279 void setHasVerticalScrollbar(bool); 280 281 virtual void updateScrollCorner(); 282 virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE; 283 284 // Scroll the content by blitting the pixels. 285 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect); 286 // Scroll the content by invalidating everything. 287 virtual void scrollContentsSlowPath(const IntRect& updateRect); 288 289 void setScrollOrigin(const IntPoint&, bool updatePositionAtAll, bool updatePositionSynchronously); 290 291 // Subclassed by FrameView to check the writing-mode of the document. 292 virtual bool isVerticalDocument() const { return true; } 293 virtual bool isFlippedDocument() const { return false; } 294 295 // Called to update the scrollbars to accurately reflect the state of the view. 296 void updateScrollbars(const IntSize& desiredOffset); 297 298 private: 299 RefPtr<Scrollbar> m_horizontalScrollbar; 300 RefPtr<Scrollbar> m_verticalScrollbar; 301 ScrollbarMode m_horizontalScrollbarMode; 302 ScrollbarMode m_verticalScrollbarMode; 303 304 bool m_horizontalScrollbarLock; 305 bool m_verticalScrollbarLock; 306 307 HashSet<RefPtr<Widget> > m_children; 308 309 // This bool is unused on Mac OS because we directly ask the platform widget 310 // whether it is safe to blit on scroll. 311 bool m_canBlitOnScroll; 312 313 IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared. 314 IntPoint m_cachedScrollPosition; 315 IntSize m_fixedLayoutSize; 316 IntSize m_contentsSize; 317 318 int m_scrollbarsAvoidingResizer; 319 bool m_scrollbarsSuppressed; 320 321 bool m_inUpdateScrollbars; 322 unsigned m_updateScrollbarsPass; 323 324 IntPoint m_panScrollIconPoint; 325 bool m_drawPanScrollIcon; 326 bool m_useFixedLayout; 327 328 bool m_paintsEntireContents; 329 bool m_clipsRepaints; 330 331 void init(); 332 void destroy(); 333 334 IntRect rectToCopyOnScroll() const; 335 336 // Called when the scroll position within this view changes. FrameView overrides this to generate repaint invalidations. 337 virtual void repaintFixedElementsAfterScrolling() { } 338 virtual void updateFixedElementsAfterScrolling() { } 339 340 void calculateOverhangAreasForPainting(IntRect& horizontalOverhangRect, IntRect& verticalOverhangRect); 341 void updateOverhangAreas(); 342 343 int pageStep(ScrollbarOrientation) const; 344 }; // class ScrollView 345 346 inline ScrollView* toScrollView(Widget* widget) 347 { 348 ASSERT(!widget || widget->isScrollView()); 349 return static_cast<ScrollView*>(widget); 350 } 351 352 inline const ScrollView* toScrollView(const Widget* widget) 353 { 354 ASSERT(!widget || widget->isScrollView()); 355 return static_cast<const ScrollView*>(widget); 356 } 357 358 // This will catch anyone doing an unnecessary cast. 359 void toScrollView(const ScrollView*); 360 361 } // namespace WebCore 362 363 #endif // ScrollView_h 364