2 * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #import "PageClientImpl.h"
29 #import "DataReference.h"
30 #import "FindIndicator.h"
31 #import "NativeWebKeyboardEvent.h"
33 #import "WKStringCF.h"
34 #import "WKViewInternal.h"
35 #import "WebContextMenuProxyMac.h"
36 #import "WebEditCommandProxy.h"
37 #import "WebPopupMenuProxyMac.h"
38 #import <WebCore/Cursor.h>
39 #import <WebCore/FloatRect.h>
40 #import <WebCore/FoundationExtras.h>
41 #import <WebCore/GraphicsContext.h>
42 #import <WebCore/KeyboardEvent.h>
43 #import <wtf/PassOwnPtr.h>
44 #import <wtf/text/CString.h>
45 #import <wtf/text/WTFString.h>
47 @interface NSApplication (WebNSApplicationDetails)
48 - (NSCursor *)_cursorRectCursor;
51 using namespace WebCore;
53 @interface WebEditCommandObjC : NSObject
55 RefPtr<WebKit::WebEditCommandProxy> m_command;
58 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command;
59 - (WebKit::WebEditCommandProxy*)command;
63 @implementation WebEditCommandObjC
65 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command
75 - (WebKit::WebEditCommandProxy*)command
77 return m_command.get();
82 @interface WebEditorUndoTargetObjC : NSObject
84 - (void)undoEditing:(id)sender;
85 - (void)redoEditing:(id)sender;
89 @implementation WebEditorUndoTargetObjC
91 - (void)undoEditing:(id)sender
93 ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
94 [sender command]->unapply();
97 - (void)redoEditing:(id)sender
99 ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
100 [sender command]->reapply();
107 NSString* nsStringFromWebCoreString(const String& string)
109 return string.impl() ? HardAutorelease(WKStringCopyCFString(0, toAPI(string.impl()))) : @"";
112 PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
114 return adoptPtr(new PageClientImpl(wkView));
117 PageClientImpl::PageClientImpl(WKView* wkView)
119 , m_undoTarget(AdoptNS, [[WebEditorUndoTargetObjC alloc] init])
123 PageClientImpl::~PageClientImpl()
127 PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
129 return [m_wkView _createDrawingAreaProxy];
132 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
134 [m_wkView setNeedsDisplayInRect:rect];
137 void PageClientImpl::displayView()
139 [m_wkView displayIfNeeded];
142 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
144 NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
146 [m_wkView translateRectsNeedingDisplayInRect:clippedScrollRect by:scrollOffset];
147 [m_wkView scrollRect:clippedScrollRect by:scrollOffset];
150 IntSize PageClientImpl::viewSize()
152 return IntSize([m_wkView bounds].size);
155 bool PageClientImpl::isViewWindowActive()
157 return [[m_wkView window] isKeyWindow];
160 bool PageClientImpl::isViewFocused()
162 return [m_wkView _isFocused];
165 bool PageClientImpl::isViewVisible()
167 if (![m_wkView window])
170 if ([m_wkView isHiddenOrHasHiddenAncestor])
176 bool PageClientImpl::isViewInWindow()
178 return [m_wkView window];
181 void PageClientImpl::processDidCrash()
183 [m_wkView _processDidCrash];
186 void PageClientImpl::pageClosed()
188 [m_wkView _pageClosed];
191 void PageClientImpl::didRelaunchProcess()
193 [m_wkView _didRelaunchProcess];
196 void PageClientImpl::takeFocus(bool direction)
198 [m_wkView _takeFocus:direction];
201 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
203 [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
206 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
208 if (![NSApp _cursorRectCursor])
209 [m_wkView _setCursor:cursor.platformCursor()];
212 void PageClientImpl::setViewportArguments(const WebCore::ViewportArguments&)
217 static NSString* nameForEditAction(EditAction editAction)
219 // FIXME: Use localized strings.
220 // FIXME: Move this to a platform independent location.
222 switch (editAction) {
223 case EditActionUnspecified: return nil;
224 case EditActionSetColor: return @"Set Color";
225 case EditActionSetBackgroundColor: return @"Set Background Color";
226 case EditActionTurnOffKerning: return @"Turn Off Kerning";
227 case EditActionTightenKerning: return @"Tighten Kerning";
228 case EditActionLoosenKerning: return @"Loosen Kerning";
229 case EditActionUseStandardKerning: return @"Use Standard Kerning";
230 case EditActionTurnOffLigatures: return @"Turn Off Ligatures";
231 case EditActionUseStandardLigatures: return @"Use Standard Ligatures";
232 case EditActionUseAllLigatures: return @"Use All Ligatures";
233 case EditActionRaiseBaseline: return @"Raise Baseline";
234 case EditActionLowerBaseline: return @"Lower Baseline";
235 case EditActionSetTraditionalCharacterShape: return @"Set Traditional Character Shape";
236 case EditActionSetFont: return @"Set Font";
237 case EditActionChangeAttributes: return @"Change Attributes";
238 case EditActionAlignLeft: return @"Align Left";
239 case EditActionAlignRight: return @"Align Right";
240 case EditActionCenter: return @"Center";
241 case EditActionJustify: return @"Justify";
242 case EditActionSetWritingDirection: return @"Set Writing Direction";
243 case EditActionSubscript: return @"Subscript";
244 case EditActionSuperscript: return @"Superscript";
245 case EditActionUnderline: return @"Underline";
246 case EditActionOutline: return @"Outline";
247 case EditActionUnscript: return @"Unscript";
248 case EditActionDrag: return @"Drag";
249 case EditActionCut: return @"Cut";
250 case EditActionPaste: return @"Paste";
251 case EditActionPasteFont: return @"Paste Font";
252 case EditActionPasteRuler: return @"Paste Ruler";
253 case EditActionTyping: return @"Typing";
254 case EditActionCreateLink: return @"Create Link";
255 case EditActionUnlink: return @"Unlink";
256 case EditActionInsertList: return @"Insert List";
257 case EditActionFormatBlock: return @"Formatting";
258 case EditActionIndent: return @"Indent";
259 case EditActionOutdent: return @"Outdent";
264 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
266 RefPtr<WebEditCommandProxy> command = prpCommand;
268 RetainPtr<WebEditCommandObjC> commandObjC(AdoptNS, [[WebEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
269 NSString *actionName = nameForEditAction(command->editAction());
271 NSUndoManager *undoManager = [m_wkView undoManager];
272 [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
274 [undoManager setActionName:actionName];
277 void PageClientImpl::clearAllEditCommands()
279 [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
282 void PageClientImpl::setEditCommandState(const String& commandName, bool isEnabled, int newState)
284 [m_wkView _setUserInterfaceItemState:nsStringFromWebCoreString(commandName) enabled:isEnabled state:newState];
287 void PageClientImpl::interceptKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commandsList, uint32_t selectionStart, uint32_t selectionEnd, Vector<WebCore::CompositionUnderline>& underlines)
289 commandsList = [m_wkView _interceptKeyEvent:event.nativeEvent()];
290 [m_wkView _getTextInputState:selectionStart selectionEnd:selectionEnd underlines:underlines];
293 void PageClientImpl::setDragImage(const IntPoint& clientPosition, const IntSize& imageSize, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
295 OwnPtr<GraphicsContext> graphicsContext = dragImage->createGraphicsContext();
296 RetainPtr<NSImage> dragNSImage(AdoptNS, [[NSImage alloc] initWithCGImage:CGBitmapContextCreateImage(graphicsContext->platformContext()) size:imageSize]);
297 [dragNSImage.get() setFlipped:YES];
298 [m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
301 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
303 return [m_wkView _convertToDeviceSpace:rect];
306 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
308 return [m_wkView _convertToUserSpace:rect];
311 void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool wasEventHandled)
313 NSEvent* nativeEvent = event.nativeEvent();
314 if ([nativeEvent type] != NSKeyDown)
317 [NSCursor setHiddenUntilMouseMoves:YES];
319 [m_wkView _setEventBeingResent:nativeEvent];
320 [[NSApplication sharedApplication] sendEvent:nativeEvent];
324 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
326 return WebPopupMenuProxyMac::create(m_wkView, page);
329 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
331 return WebContextMenuProxyMac::create(m_wkView, page);
334 void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut)
336 [m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut];
339 void PageClientImpl::accessibilityChildTokenReceived(const CoreIPC::DataReference& data)
341 NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
342 [m_wkView _setAccessibilityChildToken:remoteToken];
345 #if USE(ACCELERATED_COMPOSITING)
346 void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
348 [m_wkView _enterAcceleratedCompositingMode:layerTreeContext];
351 void PageClientImpl::exitAcceleratedCompositingMode()
353 [m_wkView _exitAcceleratedCompositingMode];
356 void PageClientImpl::pageDidEnterAcceleratedCompositing()
358 [m_wkView _pageDidEnterAcceleratedCompositing];
361 void PageClientImpl::pageDidLeaveAcceleratedCompositing()
363 [m_wkView _pageDidLeaveAcceleratedCompositing];
365 #endif // USE(ACCELERATED_COMPOSITING)
367 void PageClientImpl::setComplexTextInputEnabled(uint64_t pluginComplexTextInputIdentifier, bool complexTextInputEnabled)
369 [m_wkView _setComplexTextInputEnabled:complexTextInputEnabled pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
372 void PageClientImpl::setAutodisplay(bool newState)
374 if (!newState && [[m_wkView window] isAutodisplay])
375 [m_wkView displayIfNeeded];
377 [[m_wkView window] setAutodisplay:newState];
380 CGContextRef PageClientImpl::containingWindowGraphicsContext()
382 return static_cast<CGContextRef>([[[m_wkView window] graphicsContext] graphicsPort]);
385 void PageClientImpl::didCommitLoadForMainFrame(bool useCustomRepresentation)
387 [m_wkView _setPageHasCustomRepresentation:useCustomRepresentation];
390 void PageClientImpl::didFinishLoadingDataForCustomRepresentation(const CoreIPC::DataReference& dataReference)
392 [m_wkView _didFinishLoadingDataForCustomRepresentation:dataReference];
395 double PageClientImpl::customRepresentationZoomFactor()
397 return [m_wkView _customRepresentationZoomFactor];
400 void PageClientImpl::setCustomRepresentationZoomFactor(double zoomFactor)
402 [m_wkView _setCustomRepresentationZoomFactor:zoomFactor];
405 } // namespace WebKit