2 * Copyright (C) 2004, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "ScrollView.h"
29 #import "BlockExceptions.h"
33 #import "NotImplemented.h"
34 #import "WAKAppKitStubs.h"
35 #import "WAKClipView.h"
36 #import "WAKScrollView.h"
37 #import "WAKViewInternal.h"
39 #import "WKViewPrivate.h"
40 #import "WebCoreFrameView.h"
41 #import <wtf/CurrentTime.h>
47 inline NSScrollView<WebCoreFrameScrollView> *ScrollView::scrollView() const
49 ASSERT(!platformWidget() || [platformWidget() isKindOfClass:[NSScrollView class]]);
50 ASSERT(!platformWidget() || [platformWidget() conformsToProtocol:@protocol(WebCoreFrameScrollView)]);
51 return static_cast<NSScrollView<WebCoreFrameScrollView> *>(platformWidget());
54 NSView *ScrollView::documentView() const
56 BEGIN_BLOCK_OBJC_EXCEPTIONS;
57 return [scrollView() documentView];
58 END_BLOCK_OBJC_EXCEPTIONS;
62 void ScrollView::platformAddChild(Widget* child)
64 ASSERT(child != this);
66 child->addToSuperview(documentView());
69 void ScrollView::platformRemoveChild(Widget* child)
71 child->removeFromSuperview();
74 void ScrollView::platformSetScrollbarModes()
76 BEGIN_BLOCK_OBJC_EXCEPTIONS;
77 [scrollView() setScrollingModes:m_horizontalScrollbarMode vertical:m_verticalScrollbarMode andLock:NO];
78 END_BLOCK_OBJC_EXCEPTIONS;
81 void ScrollView::platformScrollbarModes(ScrollbarMode& horizontal, ScrollbarMode& vertical) const
83 BEGIN_BLOCK_OBJC_EXCEPTIONS;
84 [scrollView() scrollingModes:&horizontal vertical:&vertical];
85 END_BLOCK_OBJC_EXCEPTIONS;
88 void ScrollView::platformSetCanBlitOnScroll(bool canBlitOnScroll)
90 BEGIN_BLOCK_OBJC_EXCEPTIONS;
91 [[scrollView() contentView] setCopiesOnScroll:canBlitOnScroll];
92 END_BLOCK_OBJC_EXCEPTIONS;
95 bool ScrollView::platformCanBlitOnScroll() const
97 return [[scrollView() contentView] copiesOnScroll];
100 IntRect ScrollView::unobscuredContentRect(VisibleContentRectIncludesScrollbars) const
102 if (WAKScrollView *view = static_cast<WAKScrollView *>(platformWidget())) {
103 CGRect r = CGRectZero;
104 BEGIN_BLOCK_OBJC_EXCEPTIONS;
105 r = [view unobscuredContentRect];
106 END_BLOCK_OBJC_EXCEPTIONS;
107 return enclosingIntRect(r);
110 if (!m_unobscuredContentSize.isEmpty())
111 return IntRect(m_scrollPosition, roundedIntSize(m_unobscuredContentSize));
113 return unobscuredContentRectInternal();
116 void ScrollView::setUnobscuredContentSize(const FloatSize& size)
118 ASSERT(!platformWidget());
119 m_unobscuredContentSize = size;
122 FloatRect ScrollView::exposedContentRect() const
124 if (NSScrollView *view = static_cast<NSScrollView *>(platformWidget())) {
125 CGRect r = CGRectZero;
126 BEGIN_BLOCK_OBJC_EXCEPTIONS;
127 if ([view isKindOfClass:[NSScrollView class]])
128 r = [view exposedContentRect];
130 r.origin = [view visibleRect].origin;
131 r.size = [view bounds].size;
134 END_BLOCK_OBJC_EXCEPTIONS;
138 const ScrollView* parent = this->parent();
140 return m_exposedContentRect;
142 IntRect parentViewExtentContentRect = enclosingIntRect(parent->exposedContentRect());
143 IntRect selfExtentContentRect = rootViewToContents(parentViewExtentContentRect);
144 selfExtentContentRect.intersect(boundsRect());
145 return selfExtentContentRect;
148 void ScrollView::setExposedContentRect(const FloatRect& rect)
150 ASSERT(!platformWidget());
151 m_exposedContentRect = rect;
154 void ScrollView::setActualScrollPosition(const IntPoint& position)
156 NSScrollView *view = static_cast<NSScrollView *>(platformWidget());
158 BEGIN_BLOCK_OBJC_EXCEPTIONS;
159 if ([view isKindOfClass:[NSScrollView class]])
160 [view setActualScrollPosition:position];
161 END_BLOCK_OBJC_EXCEPTIONS;
164 float ScrollView::platformTopContentInset() const
169 void ScrollView::platformSetTopContentInset(float)
173 IntRect ScrollView::platformVisibleContentRect(bool includeScrollbars) const
175 BEGIN_BLOCK_OBJC_EXCEPTIONS;
176 if (includeScrollbars) {
177 if (NSView* documentView = this->documentView())
178 return enclosingIntRect([documentView visibleRect]);
180 return enclosingIntRect([scrollView() documentVisibleRect]);
181 END_BLOCK_OBJC_EXCEPTIONS;
185 IntSize ScrollView::platformVisibleContentSize(bool includeScrollbars) const
187 BEGIN_BLOCK_OBJC_EXCEPTIONS;
188 if (includeScrollbars) {
189 if (NSView* documentView = this->documentView())
190 return IntSize([documentView visibleRect].size);
193 return expandedIntSize(FloatSize([scrollView() documentVisibleRect].size));
194 END_BLOCK_OBJC_EXCEPTIONS;
198 IntRect ScrollView::platformVisibleContentRectIncludingObscuredArea(bool includeScrollbars) const
200 return platformVisibleContentRect(includeScrollbars);
203 IntSize ScrollView::platformVisibleContentSizeIncludingObscuredArea(bool includeScrollbars) const
205 return platformVisibleContentSize(includeScrollbars);
208 LegacyTileCache* ScrollView::legacyTileCache()
210 // Make tile cache pointer available via the main frame only. Tile cache interaction should be managed by
211 // the main frame and this avoids having to add parent checks to all call sites.
214 BEGIN_BLOCK_OBJC_EXCEPTIONS;
215 WAKScrollView *view = static_cast<WAKScrollView *>(platformWidget());
216 return [[view window] tileCache];
217 END_BLOCK_OBJC_EXCEPTIONS;
220 void ScrollView::platformSetContentsSize()
222 BEGIN_BLOCK_OBJC_EXCEPTIONS;
223 int w = m_contentsSize.width();
224 int h = m_contentsSize.height();
226 LOG(Frames, "%p %@ at w %d h %d\n", documentView(), [(id)[documentView() class] className], w, h);
228 LOG(Frames, "%p %@ at w %d h %d\n", documentView(), NSStringFromClass([documentView() class]), w, h);
230 NSSize tempSize = { static_cast<CGFloat>(max(0, w)), static_cast<CGFloat>(max(0, h)) }; // workaround for 4213314
231 [documentView() setBoundsSize:tempSize];
232 END_BLOCK_OBJC_EXCEPTIONS;
235 void ScrollView::platformSetScrollbarsSuppressed(bool repaintOnUnsuppress)
237 BEGIN_BLOCK_OBJC_EXCEPTIONS;
238 [scrollView() setScrollBarsSuppressed:m_scrollbarsSuppressed
239 repaintOnUnsuppress:repaintOnUnsuppress];
240 END_BLOCK_OBJC_EXCEPTIONS;
243 void ScrollView::platformSetScrollPosition(const IntPoint& scrollPoint)
245 BEGIN_BLOCK_OBJC_EXCEPTIONS;
246 NSPoint floatPoint = scrollPoint;
247 NSPoint tempPoint = { max(-[scrollView() scrollOrigin].x, floatPoint.x), max(-[scrollView() scrollOrigin].y, floatPoint.y) }; // Don't use NSMakePoint to work around 4213314.
248 [documentView() scrollPoint:tempPoint];
249 END_BLOCK_OBJC_EXCEPTIONS;
252 bool ScrollView::platformScroll(ScrollDirection, ScrollGranularity)
254 // FIXME: It would be nice to implement this so that all of the code in WebFrameView could go away.
259 void ScrollView::platformRepaintContentRectangle(const IntRect& rect)
261 BEGIN_BLOCK_OBJC_EXCEPTIONS;
263 NSView *view = documentView();
265 [view setNeedsDisplayInRect:rect];
267 END_BLOCK_OBJC_EXCEPTIONS;
270 // "Containing Window" means the NSWindow's coord system, which is origin lower left
272 IntRect ScrollView::platformContentsToScreen(const IntRect& rect) const
274 BEGIN_BLOCK_OBJC_EXCEPTIONS;
275 if (NSView* documentView = this->documentView()) {
276 NSRect tempRect = rect;
277 tempRect = [documentView convertRect:tempRect toView:nil];
278 tempRect.origin = [[documentView window] convertBaseToScreen:tempRect.origin];
279 return enclosingIntRect(tempRect);
281 END_BLOCK_OBJC_EXCEPTIONS;
285 IntPoint ScrollView::platformScreenToContents(const IntPoint& point) const
287 BEGIN_BLOCK_OBJC_EXCEPTIONS;
288 if (NSView* documentView = this->documentView()) {
289 NSPoint windowCoord = [[documentView window] convertScreenToBase: point];
290 return IntPoint([documentView convertPoint:windowCoord fromView:nil]);
292 END_BLOCK_OBJC_EXCEPTIONS;
296 bool ScrollView::platformIsOffscreen() const
298 // FIXME: DDK: ScrollViewMac.mm also checks: ![[platformWidget() window] isVisible]
299 // but -[WAKWindow isVisible] doesn't exist.
300 return ![platformWidget() window];
303 void ScrollView::platformSetScrollbarOverlayStyle(ScrollbarOverlayStyle)
307 void ScrollView::platformSetScrollOrigin(const IntPoint& origin, bool updatePositionAll, bool updatePositionSynchronously)
309 BEGIN_BLOCK_OBJC_EXCEPTIONS;
310 [scrollView() setScrollOrigin:static_cast<CGPoint>(origin) updatePositionAtAll:updatePositionAll immediately:updatePositionSynchronously];
311 END_BLOCK_OBJC_EXCEPTIONS;