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