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 "IntRect.h" 31 #include "Scrollbar.h" 32 #include "ScrollbarClient.h" 33 #include "ScrollTypes.h" 34 #include "Widget.h" 35 36 #include <wtf/HashSet.h> 37 38 #if PLATFORM(MAC) && defined __OBJC__ 39 @protocol WebCoreFrameScrollView; 40 #endif 41 42 #if PLATFORM(GTK) 43 typedef struct _GtkAdjustment GtkAdjustment; 44 #endif 45 46 #if PLATFORM(WX) 47 class wxScrollWinEvent; 48 #endif 49 50 namespace WebCore { 51 52 class HostWindow; 53 class PlatformWheelEvent; 54 class Scrollbar; 55 56 class ScrollView : public Widget, public ScrollbarClient { 57 public: 58 ~ScrollView(); 59 60 // ScrollbarClient method. FrameView overrides the other two. 61 virtual void valueChanged(Scrollbar*); 62 63 // The window thats hosts the ScrollView. The ScrollView will communicate scrolls and repaints to the 64 // host window in the window's coordinate space. 65 virtual HostWindow* hostWindow() const = 0; 66 67 // Returns a clip rect in host window coordinates. Used to clip the blit on a scroll. 68 virtual IntRect windowClipRect(bool clipToContents = true) const = 0; 69 70 // Methods for child manipulation and inspection. children()71 const HashSet<RefPtr<Widget> >* children() const { return &m_children; } 72 void addChild(PassRefPtr<Widget>); 73 void removeChild(Widget*); 74 75 // If the scroll view does not use a native widget, then it will have cross-platform Scrollbars. These methods 76 // can be used to obtain those scrollbars. horizontalScrollbar()77 Scrollbar* horizontalScrollbar() const { return m_horizontalScrollbar.get(); } verticalScrollbar()78 Scrollbar* verticalScrollbar() const { return m_verticalScrollbar.get(); } isScrollViewScrollbar(const Widget * child)79 bool isScrollViewScrollbar(const Widget* child) const { return horizontalScrollbar() == child || verticalScrollbar() == child; } 80 81 // Methods for setting and retrieving the scrolling mode in each axis (horizontal/vertical). The mode has values of 82 // AlwaysOff, AlwaysOn, and Auto. AlwaysOff means never show a scrollbar, AlwaysOn means always show a scrollbar. 83 // Auto means show a scrollbar only when one is needed. 84 // Note that for platforms with native widgets, these modes are considered advisory. In other words the underlying native 85 // widget may choose not to honor the requested modes. 86 void setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode); setHorizontalScrollbarMode(ScrollbarMode mode)87 void setHorizontalScrollbarMode(ScrollbarMode mode) { setScrollbarModes(mode, verticalScrollbarMode()); } setVerticalScrollbarMode(ScrollbarMode mode)88 void setVerticalScrollbarMode(ScrollbarMode mode) { setScrollbarModes(horizontalScrollbarMode(), mode); } 89 void scrollbarModes(ScrollbarMode& horizontalMode, ScrollbarMode& verticalMode) const; horizontalScrollbarMode()90 ScrollbarMode horizontalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return horizontal; } verticalScrollbarMode()91 ScrollbarMode verticalScrollbarMode() const { ScrollbarMode horizontal, vertical; scrollbarModes(horizontal, vertical); return vertical; } 92 virtual void setCanHaveScrollbars(bool flag); canHaveScrollbars()93 bool canHaveScrollbars() const { return horizontalScrollbarMode() != ScrollbarAlwaysOff || verticalScrollbarMode() != ScrollbarAlwaysOff; } 94 95 // Overridden by FrameView to create custom CSS scrollbars if applicable. 96 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation); 97 98 // If the prohibits scrolling flag is set, then all scrolling in the view (even programmatic scrolling) is turned off. setProhibitsScrolling(bool b)99 void setProhibitsScrolling(bool b) { m_prohibitsScrolling = b; } prohibitsScrolling()100 bool prohibitsScrolling() const { return m_prohibitsScrolling; } 101 102 // Whether or not a scroll view will blit visible contents when it is scrolled. Blitting is disabled in situations 103 // where it would cause rendering glitches (such as with fixed backgrounds or when the view is partially transparent). 104 void setCanBlitOnScroll(bool); 105 bool canBlitOnScroll() const; 106 107 // The visible content rect has a location that is the scrolled offset of the document. The width and height are the viewport width 108 // and height. By default the scrollbars themselves are excluded from this rectangle, but an optional boolean argument allows them to be 109 // included. 110 IntRect visibleContentRect(bool includeScrollbars = false) const; visibleWidth()111 int visibleWidth() const { return visibleContentRect().width(); } visibleHeight()112 int visibleHeight() const { return visibleContentRect().height(); } 113 114 // Methods for getting/setting the size webkit should use to layout the contents. By default this is the same as the visible 115 // content size. Explicitly setting a layout size value will cause webkit to layout the contents using this size instead. 116 int layoutWidth() const; 117 int layoutHeight() const; 118 IntSize fixedLayoutSize() const; 119 void setFixedLayoutSize(const IntSize&); 120 bool useFixedLayout() const; 121 void setUseFixedLayout(bool enable); 122 123 // Methods for getting/setting the size of the document contained inside the ScrollView (as an IntSize or as individual width and height 124 // values). 125 IntSize contentsSize() const; // Always at least as big as the visibleWidth()/visibleHeight(). contentsWidth()126 int contentsWidth() const { return contentsSize().width(); } contentsHeight()127 int contentsHeight() const { return contentsSize().height(); } 128 virtual void setContentsSize(const IntSize&); 129 130 // Methods for querying the current scrolled position (both as a point, a size, or as individual X and Y values). scrollPosition()131 IntPoint scrollPosition() const { return visibleContentRect().location(); } scrollOffset()132 IntSize scrollOffset() const { return visibleContentRect().location() - IntPoint(); } // Gets the scrolled position as an IntSize. Convenient for adding to other sizes. 133 IntPoint maximumScrollPosition() const; // The maximum position we can be scrolled to. scrollX()134 int scrollX() const { return scrollPosition().x(); } scrollY()135 int scrollY() const { return scrollPosition().y(); } 136 137 // Methods for scrolling the view. setScrollPosition is the only method that really scrolls the view. The other two methods are helper functions 138 // that ultimately end up calling setScrollPosition. 139 void setScrollPosition(const IntPoint&); scrollBy(const IntSize & s)140 void scrollBy(const IntSize& s) { return setScrollPosition(scrollPosition() + s); } 141 void scrollRectIntoViewRecursively(const IntRect&); 142 143 // This method scrolls by lines, pages or pixels. 144 bool scroll(ScrollDirection, ScrollGranularity); 145 146 // Scroll the actual contents of the view (either blitting or invalidating as needed). 147 void scrollContents(const IntSize& scrollDelta); 148 149 // This gives us a means of blocking painting on our scrollbars until the first layout has occurred. 150 void setScrollbarsSuppressed(bool suppressed, bool repaintOnUnsuppress = false); scrollbarsSuppressed()151 bool scrollbarsSuppressed() const { return m_scrollbarsSuppressed; } 152 153 // Event coordinates are assumed to be in the coordinate space of a window that contains 154 // the entire widget hierarchy. It is up to the platform to decide what the precise definition 155 // of containing window is. (For example on Mac it is the containing NSWindow.) 156 IntPoint windowToContents(const IntPoint&) const; 157 IntPoint contentsToWindow(const IntPoint&) const; 158 IntRect windowToContents(const IntRect&) const; 159 IntRect contentsToWindow(const IntRect&) const; 160 161 // Methods for converting to and from screen coordinates. 162 IntRect contentsToScreen(const IntRect&) const; 163 IntPoint screenToContents(const IntPoint&) const; 164 165 // The purpose of this method is to answer whether or not the scroll view is currently visible. Animations and painting updates can be suspended if 166 // we know that we are either not in a window right now or if that window is not visible. 167 bool isOffscreen() const; 168 169 // These methods are used to enable scrollbars to avoid window resizer controls that overlap the scroll view. This happens on Mac 170 // for example. windowResizerRect()171 virtual IntRect windowResizerRect() const { return IntRect(); } 172 bool containsScrollbarsAvoidingResizer() const; 173 void adjustScrollbarsAvoidingResizerCount(int overlapDelta); 174 virtual void setParent(ScrollView*); // Overridden to update the overlapping scrollbar count. 175 176 // Called when our frame rect changes (or the rect/scroll position of an ancestor changes). 177 virtual void frameRectsChanged(); 178 179 // Widget override to update our scrollbars and notify our contents of the resize. 180 virtual void setFrameRect(const IntRect&); 181 182 // For platforms that need to hit test scrollbars from within the engine's event handlers (like Win32). 183 Scrollbar* scrollbarAtPoint(const IntPoint& windowPoint); 184 185 // This method exists for scrollviews that need to handle wheel events manually. 186 // On Mac the underlying NSScrollView just does the scrolling, but on other platforms 187 // (like Windows), we need this method in order to do the scroll ourselves. 188 void wheelEvent(PlatformWheelEvent&); 189 convertChildToSelf(const Widget * child,const IntPoint & point)190 IntPoint convertChildToSelf(const Widget* child, const IntPoint& point) const 191 { 192 IntPoint newPoint = point; 193 if (!isScrollViewScrollbar(child)) 194 newPoint = point - scrollOffset(); 195 newPoint.move(child->x(), child->y()); 196 return newPoint; 197 } 198 convertSelfToChild(const Widget * child,const IntPoint & point)199 IntPoint convertSelfToChild(const Widget* child, const IntPoint& point) const 200 { 201 IntPoint newPoint = point; 202 if (!isScrollViewScrollbar(child)) 203 newPoint = point + scrollOffset(); 204 newPoint.move(-child->x(), -child->y()); 205 return newPoint; 206 } 207 208 // Widget override. Handles painting of the contents of the view as well as the scrollbars. 209 virtual void paint(GraphicsContext*, const IntRect&); 210 211 // Widget overrides to ensure that our children's visibility status is kept up to date when we get shown and hidden. 212 virtual void show(); 213 virtual void hide(); 214 virtual void setParentVisible(bool); 215 216 // Pan scrolling. 217 static const int noPanScrollRadius = 15; 218 void addPanScrollIcon(const IntPoint&); 219 void removePanScrollIcon(); 220 221 virtual bool isPointInScrollbarCorner(const IntPoint&); 222 virtual bool scrollbarCornerPresent() const; 223 224 virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const; 225 virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const; 226 virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const; 227 virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const; 228 229 protected: 230 ScrollView(); 231 232 virtual void repaintContentRectangle(const IntRect&, bool now = false); 233 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) = 0; 234 235 virtual void contentsResized() = 0; 236 virtual void visibleContentsResized() = 0; 237 238 // These methods are used to create/destroy scrollbars. 239 void setHasHorizontalScrollbar(bool); 240 void setHasVerticalScrollbar(bool); 241 242 private: 243 RefPtr<Scrollbar> m_horizontalScrollbar; 244 RefPtr<Scrollbar> m_verticalScrollbar; 245 ScrollbarMode m_horizontalScrollbarMode; 246 ScrollbarMode m_verticalScrollbarMode; 247 bool m_prohibitsScrolling; 248 249 HashSet<RefPtr<Widget> > m_children; 250 251 // This bool is unused on Mac OS because we directly ask the platform widget 252 // whether it is safe to blit on scroll. 253 bool m_canBlitOnScroll; 254 255 IntSize m_scrollOffset; // FIXME: Would rather store this as a position, but we will wait to make this change until more code is shared. 256 IntSize m_fixedLayoutSize; 257 IntSize m_contentsSize; 258 259 int m_scrollbarsAvoidingResizer; 260 bool m_scrollbarsSuppressed; 261 262 bool m_inUpdateScrollbars; 263 unsigned m_updateScrollbarsPass; 264 265 IntPoint m_panScrollIconPoint; 266 bool m_drawPanScrollIcon; 267 bool m_useFixedLayout; 268 269 void init(); 270 void destroy(); 271 272 // Called to update the scrollbars to accurately reflect the state of the view. 273 void updateScrollbars(const IntSize& desiredOffset); 274 275 void platformInit(); 276 void platformDestroy(); 277 void platformAddChild(Widget*); 278 void platformRemoveChild(Widget*); 279 void platformSetScrollbarModes(); 280 void platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const; 281 void platformSetCanBlitOnScroll(bool); 282 bool platformCanBlitOnScroll() const; 283 IntRect platformVisibleContentRect(bool includeScrollbars) const; 284 IntSize platformContentsSize() const; 285 void platformSetContentsSize(); 286 IntRect platformContentsToScreen(const IntRect&) const; 287 IntPoint platformScreenToContents(const IntPoint&) const; 288 void platformSetScrollPosition(const IntPoint&); 289 bool platformScroll(ScrollDirection, ScrollGranularity); 290 void platformSetScrollbarsSuppressed(bool repaintOnUnsuppress); 291 void platformRepaintContentRectangle(const IntRect&, bool now); 292 bool platformIsOffscreen() const; 293 294 #if PLATFORM(MAC) && defined __OBJC__ 295 public: 296 NSView* documentView() const; 297 298 private: 299 NSScrollView<WebCoreFrameScrollView>* scrollView() const; 300 #endif 301 302 #if PLATFORM(QT) 303 public: 304 void adjustWidgetsPreventingBlittingCount(int delta); 305 private: rootPreventsBlitting()306 bool rootPreventsBlitting() const { return root()->m_widgetsPreventingBlitting > 0; } 307 unsigned m_widgetsPreventingBlitting; 308 #else rootPreventsBlitting()309 bool rootPreventsBlitting() const { return false; } 310 #endif 311 312 #if PLATFORM(GTK) 313 public: 314 void setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj); 315 GtkAdjustment* m_horizontalAdjustment; 316 GtkAdjustment* m_verticalAdjustment; setScrollOffset(const IntSize & offset)317 void setScrollOffset(const IntSize& offset) { m_scrollOffset = offset; } 318 #endif 319 320 #if PLATFORM(WX) 321 public: 322 virtual void setPlatformWidget(wxWindow*); 323 void adjustScrollbars(int x = -1, int y = -1, bool refresh = true); 324 private: 325 class ScrollViewPrivate; 326 ScrollViewPrivate* m_data; 327 #endif 328 329 #if PLATFORM(ANDROID) 330 public: 331 bool platformProhibitsScrolling(); 332 #ifdef ANDROID_CAPTURE_OFFSCREEN_PAINTS 333 void platformOffscreenContentRectangle(const IntRect& ); 334 #endif 335 #endif 336 }; // class ScrollView 337 338 } // namespace WebCore 339 340 #endif // ScrollView_h 341