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 "DictionaryPopupInfo.h"
31 #import "FindIndicator.h"
32 #import "NativeWebKeyboardEvent.h"
34 #import "WKStringCF.h"
35 #import "WKViewInternal.h"
36 #import "WebContextMenuProxyMac.h"
37 #import "WebEditCommandProxy.h"
38 #import "WebPopupMenuProxyMac.h"
39 #import <WebCore/Cursor.h>
40 #import <WebCore/FloatRect.h>
41 #import <WebCore/FoundationExtras.h>
42 #import <WebCore/GraphicsContext.h>
43 #import <WebCore/KeyboardEvent.h>
44 #import <WebCore/NotImplemented.h>
45 #import <wtf/PassOwnPtr.h>
46 #import <wtf/text/CString.h>
47 #import <wtf/text/WTFString.h>
49 @interface NSApplication (WebNSApplicationDetails)
50 - (NSCursor *)_cursorRectCursor;
53 using namespace WebCore;
55 @interface WebEditCommandObjC : NSObject
57 RefPtr<WebKit::WebEditCommandProxy> m_command;
60 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command;
61 - (WebKit::WebEditCommandProxy*)command;
65 @implementation WebEditCommandObjC
67 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command
77 - (WebKit::WebEditCommandProxy*)command
79 return m_command.get();
84 @interface WebEditorUndoTargetObjC : NSObject
86 - (void)undoEditing:(id)sender;
87 - (void)redoEditing:(id)sender;
91 @implementation WebEditorUndoTargetObjC
93 - (void)undoEditing:(id)sender
95 ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
96 [sender command]->unapply();
99 - (void)redoEditing:(id)sender
101 ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
102 [sender command]->reapply();
109 NSString* nsStringFromWebCoreString(const String& string)
111 return string.impl() ? HardAutorelease(WKStringCopyCFString(0, toAPI(string.impl()))) : @"";
114 PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
116 return adoptPtr(new PageClientImpl(wkView));
119 PageClientImpl::PageClientImpl(WKView* wkView)
121 , m_undoTarget(AdoptNS, [[WebEditorUndoTargetObjC alloc] init])
125 PageClientImpl::~PageClientImpl()
129 PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
131 return [m_wkView _createDrawingAreaProxy];
134 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
136 [m_wkView setNeedsDisplayInRect:rect];
139 void PageClientImpl::displayView()
141 [m_wkView displayIfNeeded];
144 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
146 NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
148 [m_wkView translateRectsNeedingDisplayInRect:clippedScrollRect by:scrollOffset];
149 [m_wkView scrollRect:clippedScrollRect by:scrollOffset];
152 IntSize PageClientImpl::viewSize()
154 return IntSize([m_wkView bounds].size);
157 bool PageClientImpl::isViewWindowActive()
159 return [[m_wkView window] isKeyWindow];
162 bool PageClientImpl::isViewFocused()
164 return [m_wkView _isFocused];
167 bool PageClientImpl::isViewVisible()
169 if (![m_wkView window])
172 if ([m_wkView isHiddenOrHasHiddenAncestor])
178 bool PageClientImpl::isViewInWindow()
180 return [m_wkView window];
183 void PageClientImpl::processDidCrash()
185 [m_wkView _processDidCrash];
188 void PageClientImpl::pageClosed()
190 [m_wkView _pageClosed];
193 void PageClientImpl::didRelaunchProcess()
195 [m_wkView _didRelaunchProcess];
198 void PageClientImpl::setFocus(bool focused)
201 [[m_wkView window] makeFirstResponder:m_wkView];
203 // takeFocus in this context means take focus away from the WKView.
207 void PageClientImpl::takeFocus(bool direction)
209 [m_wkView _takeFocus:direction];
212 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
214 [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
217 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
219 if (![NSApp _cursorRectCursor])
220 [m_wkView _setCursor:cursor.platformCursor()];
223 void PageClientImpl::setViewportArguments(const WebCore::ViewportArguments&)
228 static NSString* nameForEditAction(EditAction editAction)
230 // FIXME: Use localized strings.
231 // FIXME: Move this to a platform independent location.
233 switch (editAction) {
234 case EditActionUnspecified: return nil;
235 case EditActionSetColor: return @"Set Color";
236 case EditActionSetBackgroundColor: return @"Set Background Color";
237 case EditActionTurnOffKerning: return @"Turn Off Kerning";
238 case EditActionTightenKerning: return @"Tighten Kerning";
239 case EditActionLoosenKerning: return @"Loosen Kerning";
240 case EditActionUseStandardKerning: return @"Use Standard Kerning";
241 case EditActionTurnOffLigatures: return @"Turn Off Ligatures";
242 case EditActionUseStandardLigatures: return @"Use Standard Ligatures";
243 case EditActionUseAllLigatures: return @"Use All Ligatures";
244 case EditActionRaiseBaseline: return @"Raise Baseline";
245 case EditActionLowerBaseline: return @"Lower Baseline";
246 case EditActionSetTraditionalCharacterShape: return @"Set Traditional Character Shape";
247 case EditActionSetFont: return @"Set Font";
248 case EditActionChangeAttributes: return @"Change Attributes";
249 case EditActionAlignLeft: return @"Align Left";
250 case EditActionAlignRight: return @"Align Right";
251 case EditActionCenter: return @"Center";
252 case EditActionJustify: return @"Justify";
253 case EditActionSetWritingDirection: return @"Set Writing Direction";
254 case EditActionSubscript: return @"Subscript";
255 case EditActionSuperscript: return @"Superscript";
256 case EditActionUnderline: return @"Underline";
257 case EditActionOutline: return @"Outline";
258 case EditActionUnscript: return @"Unscript";
259 case EditActionDrag: return @"Drag";
260 case EditActionCut: return @"Cut";
261 case EditActionPaste: return @"Paste";
262 case EditActionPasteFont: return @"Paste Font";
263 case EditActionPasteRuler: return @"Paste Ruler";
264 case EditActionTyping: return @"Typing";
265 case EditActionCreateLink: return @"Create Link";
266 case EditActionUnlink: return @"Unlink";
267 case EditActionInsertList: return @"Insert List";
268 case EditActionFormatBlock: return @"Formatting";
269 case EditActionIndent: return @"Indent";
270 case EditActionOutdent: return @"Outdent";
275 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
277 RefPtr<WebEditCommandProxy> command = prpCommand;
279 RetainPtr<WebEditCommandObjC> commandObjC(AdoptNS, [[WebEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
280 NSString *actionName = nameForEditAction(command->editAction());
282 NSUndoManager *undoManager = [m_wkView undoManager];
283 [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
285 [undoManager setActionName:actionName];
288 void PageClientImpl::clearAllEditCommands()
290 [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
293 void PageClientImpl::interceptKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commandsList, uint32_t selectionStart, uint32_t selectionEnd, Vector<WebCore::CompositionUnderline>& underlines)
295 commandsList = [m_wkView _interceptKeyEvent:event.nativeEvent()];
296 [m_wkView _getTextInputState:selectionStart selectionEnd:selectionEnd underlines:underlines];
299 void PageClientImpl::setDragImage(const IntPoint& clientPosition, const IntSize& imageSize, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
301 OwnPtr<GraphicsContext> graphicsContext = dragImage->createGraphicsContext();
302 RetainPtr<NSImage> dragNSImage(AdoptNS, [[NSImage alloc] initWithCGImage:CGBitmapContextCreateImage(graphicsContext->platformContext()) size:imageSize]);
303 [m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
306 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
308 return [m_wkView _convertToDeviceSpace:rect];
311 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
313 return [m_wkView _convertToUserSpace:rect];
316 IntRect PageClientImpl::windowToScreen(const IntRect& rect)
318 NSRect tempRect = rect;
319 tempRect = [m_wkView convertRect:tempRect toView:nil];
320 tempRect.origin = [[m_wkView window] convertBaseToScreen:tempRect.origin];
321 return enclosingIntRect(tempRect);
324 void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool wasEventHandled)
326 NSEvent* nativeEvent = event.nativeEvent();
327 if ([nativeEvent type] != NSKeyDown)
330 [NSCursor setHiddenUntilMouseMoves:YES];
332 [m_wkView _setEventBeingResent:nativeEvent];
333 [[NSApplication sharedApplication] sendEvent:nativeEvent];
337 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
339 return WebPopupMenuProxyMac::create(m_wkView, page);
342 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
344 return WebContextMenuProxyMac::create(m_wkView, page);
347 void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut)
349 [m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut];
352 void PageClientImpl::accessibilityWebProcessTokenReceived(const CoreIPC::DataReference& data)
354 NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
355 [m_wkView _setAccessibilityWebProcessToken:remoteToken];
358 #if USE(ACCELERATED_COMPOSITING)
359 void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
361 [m_wkView _enterAcceleratedCompositingMode:layerTreeContext];
364 void PageClientImpl::exitAcceleratedCompositingMode()
366 [m_wkView _exitAcceleratedCompositingMode];
368 #endif // USE(ACCELERATED_COMPOSITING)
370 void PageClientImpl::setComplexTextInputEnabled(uint64_t pluginComplexTextInputIdentifier, bool complexTextInputEnabled)
372 [m_wkView _setComplexTextInputEnabled:complexTextInputEnabled pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
375 void PageClientImpl::setAutodisplay(bool newState)
377 if (!newState && [[m_wkView window] isAutodisplay])
378 [m_wkView displayIfNeeded];
380 [[m_wkView window] setAutodisplay:newState];
382 // For some reason, painting doesn't happen for a long time without this call, <rdar://problem/8975229>.
384 [m_wkView displayIfNeeded];
387 CGContextRef PageClientImpl::containingWindowGraphicsContext()
389 NSWindow *window = [m_wkView window];
391 // Don't try to get the graphics context if the NSWindow doesn't have a window device.
392 if ([window windowNumber] <= 0)
395 return static_cast<CGContextRef>([[window graphicsContext] graphicsPort]);
398 void PageClientImpl::didChangeScrollbarsForMainFrame() const
400 [m_wkView _didChangeScrollbarsForMainFrame];
403 void PageClientImpl::didCommitLoadForMainFrame(bool useCustomRepresentation)
405 [m_wkView _setPageHasCustomRepresentation:useCustomRepresentation];
408 void PageClientImpl::didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference& dataReference)
410 [m_wkView _didFinishLoadingDataForCustomRepresentationWithSuggestedFilename:suggestedFilename dataReference:dataReference];
413 double PageClientImpl::customRepresentationZoomFactor()
415 return [m_wkView _customRepresentationZoomFactor];
418 void PageClientImpl::setCustomRepresentationZoomFactor(double zoomFactor)
420 [m_wkView _setCustomRepresentationZoomFactor:zoomFactor];
423 void PageClientImpl::flashBackingStoreUpdates(const Vector<IntRect>&)
428 void PageClientImpl::didPerformDictionaryLookup(const String& text, double scaleFactor, const DictionaryPopupInfo& dictionaryPopupInfo)
430 NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:(NSDictionary *)dictionaryPopupInfo.fontInfo.fontAttributeDictionary.get()];
431 NSFont *font = [NSFont fontWithDescriptor:fontDescriptor size:((scaleFactor != 1) ? [fontDescriptor pointSize] * scaleFactor : 0)];
433 RetainPtr<NSMutableAttributedString> attributedString(AdoptNS, [[NSMutableAttributedString alloc] initWithString:nsStringFromWebCoreString(text)]);
434 [attributedString.get() addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attributedString.get() length])];
436 NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
437 textBaselineOrigin.y += [font ascender];
439 // If the dictionary lookup is being triggered by a hot key, force the overlay style.
440 NSDictionary *options = (dictionaryPopupInfo.type == DictionaryPopupInfo::HotKey) ? [NSDictionary dictionaryWithObject:NSDefinitionPresentationTypeOverlay forKey:NSDefinitionPresentationTypeKey] : 0;
441 [m_wkView showDefinitionForAttributedString:attributedString.get() range:NSMakeRange(0, [attributedString.get() length]) options:options baselineOriginProvider:^(NSRange adjustedRange) { return (NSPoint)textBaselineOrigin; }];
444 } // namespace WebKit