Source/WebCore: WK2: Cannot set focus on an element when focus is outside of WKView
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / mac / PageClientImpl.mm
1 /*
2  * Copyright (C) 2010 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 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.
24  */
25
26 #import "config.h"
27 #import "PageClientImpl.h"
28
29 #import "DataReference.h"
30 #import "DictionaryPopupInfo.h"
31 #import "FindIndicator.h"
32 #import "NativeWebKeyboardEvent.h"
33 #import "WKAPICast.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>
48
49 @interface NSApplication (WebNSApplicationDetails)
50 - (NSCursor *)_cursorRectCursor;
51 @end
52
53 using namespace WebCore;
54
55 @interface WebEditCommandObjC : NSObject
56 {
57     RefPtr<WebKit::WebEditCommandProxy> m_command;
58 }
59
60 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command;
61 - (WebKit::WebEditCommandProxy*)command;
62
63 @end
64
65 @implementation WebEditCommandObjC
66
67 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command
68 {
69     self = [super init];
70     if (!self)
71         return nil;
72
73     m_command = command;
74     return self;
75 }
76
77 - (WebKit::WebEditCommandProxy*)command
78 {
79     return m_command.get();
80 }
81
82 @end
83
84 @interface WebEditorUndoTargetObjC : NSObject
85
86 - (void)undoEditing:(id)sender;
87 - (void)redoEditing:(id)sender;
88
89 @end
90
91 @implementation WebEditorUndoTargetObjC
92
93 - (void)undoEditing:(id)sender
94 {
95     ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
96     [sender command]->unapply();
97 }
98
99 - (void)redoEditing:(id)sender
100 {
101     ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
102     [sender command]->reapply();
103 }
104
105 @end
106
107 namespace WebKit {
108
109 NSString* nsStringFromWebCoreString(const String& string)
110 {
111     return string.impl() ? HardAutorelease(WKStringCopyCFString(0, toAPI(string.impl()))) : @"";
112 }
113
114 PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
115 {
116     return adoptPtr(new PageClientImpl(wkView));
117 }
118
119 PageClientImpl::PageClientImpl(WKView* wkView)
120     : m_wkView(wkView)
121     , m_undoTarget(AdoptNS, [[WebEditorUndoTargetObjC alloc] init])
122 {
123 }
124
125 PageClientImpl::~PageClientImpl()
126 {
127 }
128
129 PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
130 {
131     return [m_wkView _createDrawingAreaProxy];
132 }
133
134 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
135 {
136     [m_wkView setNeedsDisplayInRect:rect];
137 }
138
139 void PageClientImpl::displayView()
140 {
141     [m_wkView displayIfNeeded];
142 }
143
144 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
145 {
146     NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
147
148     [m_wkView translateRectsNeedingDisplayInRect:clippedScrollRect by:scrollOffset];
149     [m_wkView scrollRect:clippedScrollRect by:scrollOffset];
150 }
151
152 IntSize PageClientImpl::viewSize()
153 {
154     return IntSize([m_wkView bounds].size);
155 }
156
157 bool PageClientImpl::isViewWindowActive()
158 {
159     return [[m_wkView window] isKeyWindow];
160 }
161
162 bool PageClientImpl::isViewFocused()
163 {
164     return [m_wkView _isFocused];
165 }
166
167 bool PageClientImpl::isViewVisible()
168 {
169     if (![m_wkView window])
170         return false;
171
172     if ([m_wkView isHiddenOrHasHiddenAncestor])
173         return false;
174
175     return true;
176 }
177
178 bool PageClientImpl::isViewInWindow()
179 {
180     return [m_wkView window];
181 }
182
183 void PageClientImpl::processDidCrash()
184 {
185     [m_wkView _processDidCrash];
186 }
187     
188 void PageClientImpl::pageClosed()
189 {
190     [m_wkView _pageClosed];
191 }
192
193 void PageClientImpl::didRelaunchProcess()
194 {
195     [m_wkView _didRelaunchProcess];
196 }
197
198 void PageClientImpl::setFocus(bool focused)
199 {
200     if (focused)
201         [[m_wkView window] makeFirstResponder:m_wkView];
202     else
203         // takeFocus in this context means take focus away from the WKView.
204         takeFocus(true);
205 }
206     
207 void PageClientImpl::takeFocus(bool direction)
208 {
209     [m_wkView _takeFocus:direction];
210 }
211
212 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
213 {
214     [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
215 }
216
217 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
218 {
219     if (![NSApp _cursorRectCursor])
220         [m_wkView _setCursor:cursor.platformCursor()];
221 }
222
223 void PageClientImpl::setViewportArguments(const WebCore::ViewportArguments&)
224 {
225
226 }
227
228 static NSString* nameForEditAction(EditAction editAction)
229 {
230     // FIXME: Use localized strings.
231     // FIXME: Move this to a platform independent location.
232
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";
271     }
272     return nil;
273 }
274
275 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
276 {
277     RefPtr<WebEditCommandProxy> command = prpCommand;
278
279     RetainPtr<WebEditCommandObjC> commandObjC(AdoptNS, [[WebEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
280     NSString *actionName = nameForEditAction(command->editAction());
281
282     NSUndoManager *undoManager = [m_wkView undoManager];
283     [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
284     if (actionName)
285         [undoManager setActionName:actionName];
286 }
287
288 void PageClientImpl::clearAllEditCommands()
289 {
290     [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
291 }
292
293 void PageClientImpl::interceptKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commandsList, uint32_t selectionStart, uint32_t selectionEnd, Vector<WebCore::CompositionUnderline>& underlines)
294 {
295     commandsList = [m_wkView _interceptKeyEvent:event.nativeEvent()];
296     [m_wkView _getTextInputState:selectionStart selectionEnd:selectionEnd underlines:underlines];
297 }
298
299 void PageClientImpl::setDragImage(const IntPoint& clientPosition, const IntSize& imageSize, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
300 {
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];
304 }
305     
306 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
307 {
308     return [m_wkView _convertToDeviceSpace:rect];
309 }
310
311 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
312 {
313     return [m_wkView _convertToUserSpace:rect];
314 }
315     
316 IntRect PageClientImpl::windowToScreen(const IntRect& rect)
317 {
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);
322 }
323
324 void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool wasEventHandled)
325 {
326     NSEvent* nativeEvent = event.nativeEvent();
327     if ([nativeEvent type] != NSKeyDown)
328         return;
329     if (wasEventHandled)
330         [NSCursor setHiddenUntilMouseMoves:YES];
331     else {
332         [m_wkView _setEventBeingResent:nativeEvent];
333         [[NSApplication sharedApplication] sendEvent:nativeEvent];
334     }
335 }
336
337 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
338 {
339     return WebPopupMenuProxyMac::create(m_wkView, page);
340 }
341
342 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
343 {
344     return WebContextMenuProxyMac::create(m_wkView, page);
345 }
346
347 void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut)
348 {
349     [m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut];
350 }
351
352 void PageClientImpl::accessibilityWebProcessTokenReceived(const CoreIPC::DataReference& data)
353 {
354     NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
355     [m_wkView _setAccessibilityWebProcessToken:remoteToken];
356 }
357     
358 #if USE(ACCELERATED_COMPOSITING)
359 void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
360 {
361     [m_wkView _enterAcceleratedCompositingMode:layerTreeContext];
362 }
363
364 void PageClientImpl::exitAcceleratedCompositingMode()
365 {
366     [m_wkView _exitAcceleratedCompositingMode];
367 }
368 #endif // USE(ACCELERATED_COMPOSITING)
369
370 void PageClientImpl::setComplexTextInputEnabled(uint64_t pluginComplexTextInputIdentifier, bool complexTextInputEnabled)
371 {
372     [m_wkView _setComplexTextInputEnabled:complexTextInputEnabled pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
373 }
374
375 void PageClientImpl::setAutodisplay(bool newState)
376 {
377     if (!newState && [[m_wkView window] isAutodisplay])
378         [m_wkView displayIfNeeded];
379     
380     [[m_wkView window] setAutodisplay:newState];
381
382     // For some reason, painting doesn't happen for a long time without this call, <rdar://problem/8975229>.
383     if (newState)
384         [m_wkView displayIfNeeded];
385 }
386
387 CGContextRef PageClientImpl::containingWindowGraphicsContext()
388 {
389     NSWindow *window = [m_wkView window];
390
391     // Don't try to get the graphics context if the NSWindow doesn't have a window device.
392     if ([window windowNumber] <= 0)
393         return 0;
394
395     return static_cast<CGContextRef>([[window graphicsContext] graphicsPort]);
396 }
397
398 void PageClientImpl::didChangeScrollbarsForMainFrame() const
399 {
400     [m_wkView _didChangeScrollbarsForMainFrame];
401 }
402
403 void PageClientImpl::didCommitLoadForMainFrame(bool useCustomRepresentation)
404 {
405     [m_wkView _setPageHasCustomRepresentation:useCustomRepresentation];
406 }
407
408 void PageClientImpl::didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference& dataReference)
409 {
410     [m_wkView _didFinishLoadingDataForCustomRepresentationWithSuggestedFilename:suggestedFilename dataReference:dataReference];
411 }
412
413 double PageClientImpl::customRepresentationZoomFactor()
414 {
415     return [m_wkView _customRepresentationZoomFactor];
416 }
417
418 void PageClientImpl::setCustomRepresentationZoomFactor(double zoomFactor)
419 {
420     [m_wkView _setCustomRepresentationZoomFactor:zoomFactor];
421 }
422
423 void PageClientImpl::flashBackingStoreUpdates(const Vector<IntRect>&)
424 {
425     notImplemented();
426 }
427
428 void PageClientImpl::didPerformDictionaryLookup(const String& text, double scaleFactor, const DictionaryPopupInfo& dictionaryPopupInfo)
429 {
430     NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:(NSDictionary *)dictionaryPopupInfo.fontInfo.fontAttributeDictionary.get()];
431     NSFont *font = [NSFont fontWithDescriptor:fontDescriptor size:((scaleFactor != 1) ? [fontDescriptor pointSize] * scaleFactor : 0)];
432
433     RetainPtr<NSMutableAttributedString> attributedString(AdoptNS, [[NSMutableAttributedString alloc] initWithString:nsStringFromWebCoreString(text)]);
434     [attributedString.get() addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attributedString.get() length])];
435
436     NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
437     textBaselineOrigin.y += [font ascender];
438     
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; }];
442 }
443
444 } // namespace WebKit