2 * Copyright (C) 2004, 2005, 2006, 2008, 2010, 2011 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 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.
29 #ifdef BUILDING_ON_TIGER
30 #import "AutodrainedPool.h"
33 #import "BlockExceptions.h"
37 #import "FloatConversion.h"
40 #import "GraphicsContext.h"
41 #import "NotImplemented.h"
43 #import "PlatformMouseEvent.h"
44 #import "ScrollView.h"
45 #import "WebCoreFrameView.h"
46 #import "WebCoreView.h"
47 #import <wtf/RetainPtr.h>
49 @interface NSWindow (WebWindowDetails)
50 - (BOOL)_needsToResetDragMargins;
51 - (void)_setNeedsToResetDragMargins:(BOOL)needs;
54 @interface NSView (WebSetSelectedMethods)
55 - (void)setIsSelected:(BOOL)isSelected;
56 - (void)webPlugInSetIsSelected:(BOOL)isSelected;
59 @interface NSView (Widget)
60 - (void)visibleRectDidChange;
68 : previousVisibleRect(NSZeroRect)
72 bool mustStayInWindow;
73 bool removeFromSuperviewSoon;
74 NSRect previousVisibleRect;
77 static void safeRemoveFromSuperview(NSView *view)
79 // If the the view is the first responder, then set the window's first responder to nil so
80 // we don't leave the window pointing to a view that's no longer in it.
81 NSWindow *window = [view window];
82 NSResponder *firstResponder = [window firstResponder];
83 if ([firstResponder isKindOfClass:[NSView class]] && [(NSView *)firstResponder isDescendantOf:view])
84 [window makeFirstResponder:nil];
86 // Suppress the resetting of drag margins since we know we can't affect them.
87 BOOL resetDragMargins = [window _needsToResetDragMargins];
88 [window _setNeedsToResetDragMargins:NO];
89 [view removeFromSuperview];
90 [window _setNeedsToResetDragMargins:resetDragMargins];
93 Widget::Widget(NSView *view)
94 : m_data(new WidgetPrivate)
97 m_data->mustStayInWindow = false;
98 m_data->removeFromSuperviewSoon = false;
106 // FIXME: Should move this to Chrome; bad layering that this knows about Frame.
107 void Widget::setFocus(bool focused)
112 Frame* frame = Frame::frameForWidget(this);
116 BEGIN_BLOCK_OBJC_EXCEPTIONS;
118 // If there's no platformWidget, WK2 is running. The focus() method needs to be used
119 // to bring focus to the right view on the UIProcess side.
120 NSView *view = [platformWidget() _webcore_effectiveFirstResponder];
121 if (Page* page = frame->page()) {
122 if (!platformWidget())
123 page->chrome()->focus();
125 page->chrome()->focusNSView(view);
127 END_BLOCK_OBJC_EXCEPTIONS;
130 void Widget::setCursor(const Cursor& cursor)
132 ScrollView* view = root();
135 view->hostWindow()->setCursor(cursor);
143 setSelfVisible(true);
145 BEGIN_BLOCK_OBJC_EXCEPTIONS;
146 [getOuterView() setHidden:NO];
147 END_BLOCK_OBJC_EXCEPTIONS;
152 if (!isSelfVisible())
155 setSelfVisible(false);
157 BEGIN_BLOCK_OBJC_EXCEPTIONS;
158 [getOuterView() setHidden:YES];
159 END_BLOCK_OBJC_EXCEPTIONS;
162 IntRect Widget::frameRect() const
164 if (!platformWidget())
167 BEGIN_BLOCK_OBJC_EXCEPTIONS;
168 return enclosingIntRect([getOuterView() frame]);
169 END_BLOCK_OBJC_EXCEPTIONS;
174 void Widget::setFrameRect(const IntRect& rect)
178 BEGIN_BLOCK_OBJC_EXCEPTIONS;
179 NSView *outerView = getOuterView();
183 // Take a reference to this Widget, because sending messages to outerView can invoke arbitrary
184 // code, which can deref it.
185 RefPtr<Widget> protectedThis(this);
187 NSRect visibleRect = [outerView visibleRect];
189 if (!NSEqualRects(f, [outerView frame])) {
190 [outerView setFrame:f];
191 [outerView setNeedsDisplay:NO];
192 } else if (!NSEqualRects(visibleRect, m_data->previousVisibleRect) && [outerView respondsToSelector:@selector(visibleRectDidChange)])
193 [outerView visibleRectDidChange];
195 m_data->previousVisibleRect = visibleRect;
196 END_BLOCK_OBJC_EXCEPTIONS;
199 void Widget::setBoundsSize(const IntSize& size)
201 NSSize nsSize = size;
203 BEGIN_BLOCK_OBJC_EXCEPTIONS;
204 NSView *outerView = getOuterView();
208 // Take a reference to this Widget, because sending messages to outerView can invoke arbitrary
209 // code, which can deref it.
210 RefPtr<Widget> protectedThis(this);
211 if (!NSEqualSizes(nsSize, [outerView bounds].size)) {
212 [outerView setBoundsSize:nsSize];
213 [outerView setNeedsDisplay:NO];
215 END_BLOCK_OBJC_EXCEPTIONS;
218 NSView *Widget::getOuterView() const
220 NSView *view = platformWidget();
222 // If this widget's view is a WebCoreFrameScrollView then we
223 // resize its containing view, a WebFrameView.
224 if ([view conformsToProtocol:@protocol(WebCoreFrameScrollView)]) {
225 view = [view superview];
232 void Widget::paint(GraphicsContext* p, const IntRect& r)
234 if (p->paintingDisabled())
236 NSView *view = getOuterView();
238 // Take a reference to this Widget, because sending messages to the views can invoke arbitrary
239 // code, which can deref it.
240 RefPtr<Widget> protectedThis(this);
242 IntPoint transformOrigin = frameRect().location();
243 AffineTransform widgetToViewTranform = makeMapBetweenRects(IntRect(IntPoint(), frameRect().size()), [view bounds]);
245 NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
246 if (currentContext == [[view window] graphicsContext] || ![currentContext isDrawingToScreen]) {
247 // This is the common case of drawing into a window or printing.
248 BEGIN_BLOCK_OBJC_EXCEPTIONS;
250 CGContextRef context = (CGContextRef)[currentContext graphicsPort];
252 CGContextSaveGState(context);
253 CGContextTranslateCTM(context, transformOrigin.x(), transformOrigin.y());
254 CGContextScaleCTM(context, narrowPrecisionToFloat(widgetToViewTranform.xScale()), narrowPrecisionToFloat(widgetToViewTranform.yScale()));
255 CGContextTranslateCTM(context, -transformOrigin.x(), -transformOrigin.y());
257 IntRect dirtyRect = r;
258 dirtyRect.move(-transformOrigin.x(), -transformOrigin.y());
259 if (![view isFlipped])
260 dirtyRect.setY([view bounds].size.height - dirtyRect.maxY());
262 [view displayRectIgnoringOpacity:dirtyRect];
264 CGContextRestoreGState(context);
266 END_BLOCK_OBJC_EXCEPTIONS;
268 // This is the case of drawing into a bitmap context other than a window backing store. It gets hit beneath
269 // -cacheDisplayInRect:toBitmapImageRep:, and when painting into compositing layers.
271 // Transparent subframes are in fact implemented with scroll views that return YES from -drawsBackground (whenever the WebView
272 // itself is in drawsBackground mode). In the normal drawing code path, the scroll views are never asked to draw the background,
273 // so this is not an issue, but in this code path they are, so the following code temporarily turns background drwaing off.
274 NSView *innerView = platformWidget();
275 NSScrollView *scrollView = 0;
276 if ([innerView conformsToProtocol:@protocol(WebCoreFrameScrollView)]) {
277 ASSERT([innerView isKindOfClass:[NSScrollView class]]);
278 NSScrollView *scrollView = static_cast<NSScrollView *>(innerView);
279 // -copiesOnScroll will return NO whenever the content view is not fully opaque.
280 if ([scrollView drawsBackground] && ![[scrollView contentView] copiesOnScroll])
281 [scrollView setDrawsBackground:NO];
286 CGContextRef cgContext = p->platformContext();
287 ASSERT(cgContext == [currentContext graphicsPort]);
288 CGContextSaveGState(cgContext);
290 CGContextTranslateCTM(cgContext, transformOrigin.x(), transformOrigin.y());
291 CGContextScaleCTM(cgContext, narrowPrecisionToFloat(widgetToViewTranform.xScale()), narrowPrecisionToFloat(widgetToViewTranform.yScale()));
292 CGContextTranslateCTM(cgContext, -transformOrigin.x(), -transformOrigin.y());
294 NSRect viewFrame = [view frame];
295 NSRect viewBounds = [view bounds];
296 // Set up the translation and (flipped) orientation of the graphics context. In normal drawing, AppKit does it as it descends down
297 // the view hierarchy.
298 CGContextTranslateCTM(cgContext, viewFrame.origin.x - viewBounds.origin.x, viewFrame.origin.y + viewFrame.size.height + viewBounds.origin.y);
299 CGContextScaleCTM(cgContext, 1, -1);
301 IntRect dirtyRect = r;
302 dirtyRect.move(-transformOrigin.x(), -transformOrigin.y());
303 if (![view isFlipped])
304 dirtyRect.setY([view bounds].size.height - dirtyRect.maxY());
306 BEGIN_BLOCK_OBJC_EXCEPTIONS;
308 #ifdef BUILDING_ON_TIGER
309 AutodrainedPool pool;
311 NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:YES];
312 [view displayRectIgnoringOpacity:dirtyRect inContext:nsContext];
314 END_BLOCK_OBJC_EXCEPTIONS;
316 CGContextRestoreGState(cgContext);
319 [scrollView setDrawsBackground:YES];
323 void Widget::setIsSelected(bool isSelected)
325 NSView *view = platformWidget();
327 BEGIN_BLOCK_OBJC_EXCEPTIONS;
328 if ([view respondsToSelector:@selector(webPlugInSetIsSelected:)])
329 [view webPlugInSetIsSelected:isSelected];
330 else if ([view respondsToSelector:@selector(setIsSelected:)])
331 [view setIsSelected:isSelected];
332 END_BLOCK_OBJC_EXCEPTIONS;
335 void Widget::removeFromSuperview()
337 if (m_data->mustStayInWindow)
338 m_data->removeFromSuperviewSoon = true;
340 m_data->removeFromSuperviewSoon = false;
341 BEGIN_BLOCK_OBJC_EXCEPTIONS;
342 safeRemoveFromSuperview(getOuterView());
343 END_BLOCK_OBJC_EXCEPTIONS;
347 void Widget::beforeMouseDown(NSView *unusedView, Widget* widget)
350 ASSERT_UNUSED(unusedView, unusedView == widget->getOuterView());
351 ASSERT(!widget->m_data->mustStayInWindow);
352 widget->m_data->mustStayInWindow = true;
356 void Widget::afterMouseDown(NSView *view, Widget* widget)
359 BEGIN_BLOCK_OBJC_EXCEPTIONS;
360 safeRemoveFromSuperview(view);
361 END_BLOCK_OBJC_EXCEPTIONS;
363 ASSERT(widget->m_data->mustStayInWindow);
364 widget->m_data->mustStayInWindow = false;
365 if (widget->m_data->removeFromSuperviewSoon)
366 widget->removeFromSuperview();
370 // These are here to deal with flipped coords on Mac.
371 IntRect Widget::convertFromRootToContainingWindow(const Widget* rootWidget, const IntRect& rect)
373 if (!rootWidget->platformWidget())
376 BEGIN_BLOCK_OBJC_EXCEPTIONS;
377 return enclosingIntRect([rootWidget->platformWidget() convertRect:rect toView:nil]);
378 END_BLOCK_OBJC_EXCEPTIONS;
383 IntRect Widget::convertFromContainingWindowToRoot(const Widget* rootWidget, const IntRect& rect)
385 if (!rootWidget->platformWidget())
388 BEGIN_BLOCK_OBJC_EXCEPTIONS;
389 return enclosingIntRect([rootWidget->platformWidget() convertRect:rect fromView:nil]);
390 END_BLOCK_OBJC_EXCEPTIONS;
395 IntPoint Widget::convertFromRootToContainingWindow(const Widget* rootWidget, const IntPoint& point)
397 if (!rootWidget->platformWidget())
400 BEGIN_BLOCK_OBJC_EXCEPTIONS;
401 return IntPoint([rootWidget->platformWidget() convertPoint:point toView:nil]);
402 END_BLOCK_OBJC_EXCEPTIONS;
406 IntPoint Widget::convertFromContainingWindowToRoot(const Widget* rootWidget, const IntPoint& point)
408 if (!rootWidget->platformWidget())
411 BEGIN_BLOCK_OBJC_EXCEPTIONS;
412 return IntPoint([rootWidget->platformWidget() convertPoint:point fromView:nil]);
413 END_BLOCK_OBJC_EXCEPTIONS;
418 NSView *Widget::platformWidget() const
420 return m_widget.get();
423 void Widget::setPlatformWidget(NSView *widget)
425 if (widget == m_widget)
429 m_data->previousVisibleRect = NSZeroRect;
432 } // namespace WebCore