<rdar://problem/10294191> REGRESSION (WebKit2): Lookup bubble sized and positioned...
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / mac / PageClientImpl.mm
1 /*
2  * Copyright (C) 2010, 2011 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 #import <WebKitSystemInterface.h>
49
50 @interface NSApplication (WebNSApplicationDetails)
51 - (NSCursor *)_cursorRectCursor;
52 @end
53
54 using namespace WebCore;
55 using namespace WebKit;
56
57 @interface WKEditCommandObjC : NSObject
58 {
59     RefPtr<WebEditCommandProxy> m_command;
60 }
61 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command;
62 - (WebEditCommandProxy*)command;
63 @end
64
65 @interface WKEditorUndoTargetObjC : NSObject
66 - (void)undoEditing:(id)sender;
67 - (void)redoEditing:(id)sender;
68 @end
69
70 @implementation WKEditCommandObjC
71
72 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command
73 {
74     self = [super init];
75     if (!self)
76         return nil;
77
78     m_command = command;
79     return self;
80 }
81
82 - (WebEditCommandProxy*)command
83 {
84     return m_command.get();
85 }
86
87 @end
88
89 @implementation WKEditorUndoTargetObjC
90
91 - (void)undoEditing:(id)sender
92 {
93     ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
94     [sender command]->unapply();
95 }
96
97 - (void)redoEditing:(id)sender
98 {
99     ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
100     [sender command]->reapply();
101 }
102
103 @end
104
105 namespace WebKit {
106
107 NSString* nsStringFromWebCoreString(const String& string)
108 {
109     return string.impl() ? HardAutorelease(WKStringCopyCFString(0, toAPI(string.impl()))) : @"";
110 }
111
112 PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
113 {
114     return adoptPtr(new PageClientImpl(wkView));
115 }
116
117 PageClientImpl::PageClientImpl(WKView* wkView)
118     : m_wkView(wkView)
119     , m_undoTarget(AdoptNS, [[WKEditorUndoTargetObjC alloc] init])
120 {
121 }
122
123 PageClientImpl::~PageClientImpl()
124 {
125 }
126
127 PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
128 {
129     return [m_wkView _createDrawingAreaProxy];
130 }
131
132 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
133 {
134     [m_wkView setNeedsDisplayInRect:rect];
135 }
136
137 void PageClientImpl::displayView()
138 {
139     [m_wkView displayIfNeeded];
140 }
141
142 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
143 {
144     NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
145
146     [m_wkView _cacheWindowBottomCornerRect];
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] || [NSApp keyWindow] == [m_wkView window];
160 }
161
162 bool PageClientImpl::isViewFocused()
163 {
164     return [m_wkView _isFocused];
165 }
166
167 void PageClientImpl::makeFirstResponder()
168 {
169      [[m_wkView window] makeFirstResponder:m_wkView];
170 }
171     
172 bool PageClientImpl::isViewVisible()
173 {
174     if (![m_wkView window])
175         return false;
176
177     if (![[m_wkView window] isVisible])
178         return false;
179
180     if ([m_wkView isHiddenOrHasHiddenAncestor])
181         return false;
182
183     return true;
184 }
185
186 bool PageClientImpl::isViewInWindow()
187 {
188     return [m_wkView window];
189 }
190
191 void PageClientImpl::processDidCrash()
192 {
193     [m_wkView _processDidCrash];
194 }
195     
196 void PageClientImpl::pageClosed()
197 {
198     [m_wkView _pageClosed];
199 }
200
201 void PageClientImpl::didRelaunchProcess()
202 {
203     [m_wkView _didRelaunchProcess];
204 }
205
206 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
207 {
208     [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
209 }
210
211 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
212 {
213     if (![NSApp _cursorRectCursor])
214         [m_wkView _setCursor:cursor.platformCursor()];
215 }
216
217 void PageClientImpl::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
218 {
219     [NSCursor setHiddenUntilMouseMoves:hiddenUntilMouseMoves];
220 }
221
222 void PageClientImpl::didChangeViewportProperties(const WebCore::ViewportArguments&)
223 {
224 }
225
226 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
227 {
228     RefPtr<WebEditCommandProxy> command = prpCommand;
229
230     RetainPtr<WKEditCommandObjC> commandObjC(AdoptNS, [[WKEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
231     String actionName = WebEditCommandProxy::nameForEditAction(command->editAction());
232
233     NSUndoManager *undoManager = [m_wkView undoManager];
234     [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
235     if (!actionName.isEmpty())
236         [undoManager setActionName:(NSString *)actionName];
237 }
238
239 void PageClientImpl::clearAllEditCommands()
240 {
241     [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
242 }
243
244 bool PageClientImpl::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
245 {
246     return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] canUndo] : [[m_wkView undoManager] canRedo];
247 }
248
249 void PageClientImpl::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
250 {
251     return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] undo] : [[m_wkView undoManager] redo];
252 }
253
254 bool PageClientImpl::interpretKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commands)
255 {
256     return [m_wkView _interpretKeyEvent:event.nativeEvent() savingCommandsTo:commands];
257 }
258
259 void PageClientImpl::setDragImage(const IntPoint& clientPosition, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
260 {
261     RetainPtr<CGImageRef> dragCGImage = dragImage->makeCGImage();
262     RetainPtr<NSImage> dragNSImage(AdoptNS, [[NSImage alloc] initWithCGImage:dragCGImage.get() size:dragImage->size()]);
263
264     [m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
265 }
266
267 void PageClientImpl::updateTextInputState(bool updateSecureInputState)
268 {
269     [m_wkView _updateTextInputStateIncludingSecureInputState:updateSecureInputState];
270 }
271
272 void PageClientImpl::resetTextInputState()
273 {
274     [m_wkView _resetTextInputState];
275 }
276
277 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
278 {
279     return [m_wkView _convertToDeviceSpace:rect];
280 }
281
282 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
283 {
284     return [m_wkView _convertToUserSpace:rect];
285 }
286    
287 IntPoint PageClientImpl::screenToWindow(const IntPoint& point)
288 {
289     NSPoint windowCoord = [[m_wkView window] convertScreenToBase:point];
290     return IntPoint([m_wkView convertPoint:windowCoord fromView:nil]);
291 }
292     
293 IntRect PageClientImpl::windowToScreen(const IntRect& rect)
294 {
295     NSRect tempRect = rect;
296     tempRect = [m_wkView convertRect:tempRect toView:nil];
297     tempRect.origin = [[m_wkView window] convertBaseToScreen:tempRect.origin];
298     return enclosingIntRect(tempRect);
299 }
300
301 void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool eventWasHandled)
302 {
303     [m_wkView _doneWithKeyEvent:event.nativeEvent() eventWasHandled:eventWasHandled];
304 }
305
306 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
307 {
308     return WebPopupMenuProxyMac::create(m_wkView, page);
309 }
310
311 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
312 {
313     return WebContextMenuProxyMac::create(m_wkView, page);
314 }
315
316 void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut, bool animate)
317 {
318     [m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut animate:animate];
319 }
320
321 void PageClientImpl::accessibilityWebProcessTokenReceived(const CoreIPC::DataReference& data)
322 {
323     NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
324     [m_wkView _setAccessibilityWebProcessToken:remoteToken];
325 }
326     
327 #if USE(ACCELERATED_COMPOSITING)
328 void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
329 {
330     [m_wkView _enterAcceleratedCompositingMode:layerTreeContext];
331 }
332
333 void PageClientImpl::exitAcceleratedCompositingMode()
334 {
335     [m_wkView _exitAcceleratedCompositingMode];
336 }
337 #endif // USE(ACCELERATED_COMPOSITING)
338
339 void PageClientImpl::pluginFocusOrWindowFocusChanged(uint64_t pluginComplexTextInputIdentifier, bool pluginHasFocusAndWindowHasFocus)
340 {
341     [m_wkView _pluginFocusOrWindowFocusChanged:pluginHasFocusAndWindowHasFocus pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
342 }
343
344 void PageClientImpl::setPluginComplexTextInputState(uint64_t pluginComplexTextInputIdentifier, PluginComplexTextInputState pluginComplexTextInputState)
345 {
346     [m_wkView _setPluginComplexTextInputState:pluginComplexTextInputState pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
347 }
348
349 CGContextRef PageClientImpl::containingWindowGraphicsContext()
350 {
351     NSWindow *window = [m_wkView window];
352
353     // Don't try to get the graphics context if the NSWindow doesn't have a window device.
354     if ([window windowNumber] <= 0)
355         return 0;
356
357     return static_cast<CGContextRef>([[window graphicsContext] graphicsPort]);
358 }
359
360 void PageClientImpl::didChangeScrollbarsForMainFrame() const
361 {
362     [m_wkView _didChangeScrollbarsForMainFrame];
363 }
364
365 void PageClientImpl::didCommitLoadForMainFrame(bool useCustomRepresentation)
366 {
367     [m_wkView _setPageHasCustomRepresentation:useCustomRepresentation];
368 }
369
370 void PageClientImpl::didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference& dataReference)
371 {
372     [m_wkView _didFinishLoadingDataForCustomRepresentationWithSuggestedFilename:suggestedFilename dataReference:dataReference];
373 }
374
375 double PageClientImpl::customRepresentationZoomFactor()
376 {
377     return [m_wkView _customRepresentationZoomFactor];
378 }
379
380 void PageClientImpl::setCustomRepresentationZoomFactor(double zoomFactor)
381 {
382     [m_wkView _setCustomRepresentationZoomFactor:zoomFactor];
383 }
384
385 void PageClientImpl::findStringInCustomRepresentation(const String& string, FindOptions options, unsigned maxMatchCount)
386 {
387     [m_wkView _findStringInCustomRepresentation:string withFindOptions:options maxMatchCount:maxMatchCount];
388 }
389
390 void PageClientImpl::countStringMatchesInCustomRepresentation(const String& string, FindOptions options, unsigned maxMatchCount)
391 {
392     [m_wkView _countStringMatchesInCustomRepresentation:string withFindOptions:options maxMatchCount:maxMatchCount];
393 }
394
395 void PageClientImpl::flashBackingStoreUpdates(const Vector<IntRect>&)
396 {
397     notImplemented();
398 }
399
400 void PageClientImpl::didPerformDictionaryLookup(const String& text, double scaleFactor, const DictionaryPopupInfo& dictionaryPopupInfo)
401 {
402     NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:(NSDictionary *)dictionaryPopupInfo.fontInfo.fontAttributeDictionary.get()];
403     NSFont *font = [NSFont fontWithDescriptor:fontDescriptor size:((scaleFactor != 1) ? [fontDescriptor pointSize] * scaleFactor : 0)];
404
405     RetainPtr<NSMutableAttributedString> attributedString(AdoptNS, [[NSMutableAttributedString alloc] initWithString:nsStringFromWebCoreString(text)]);
406     [attributedString.get() addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attributedString.get() length])];
407
408     NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
409
410 #if !defined(BUILDING_ON_SNOW_LEOPARD)
411     // Convert to screen coordinates.
412     textBaselineOrigin = [m_wkView convertPoint:textBaselineOrigin toView:nil];
413     textBaselineOrigin = [m_wkView.window convertRectToScreen:NSMakeRect(textBaselineOrigin.x, textBaselineOrigin.y, 0, 0)].origin;
414
415     WKShowWordDefinitionWindow(attributedString.get(), textBaselineOrigin, (NSDictionary *)dictionaryPopupInfo.options.get());
416 #else
417     // If the dictionary lookup is being triggered by a hot key, force the overlay style.
418     NSDictionary *options = (dictionaryPopupInfo.type == DictionaryPopupInfo::HotKey) ? [NSDictionary dictionaryWithObject:NSDefinitionPresentationTypeOverlay forKey:NSDefinitionPresentationTypeKey] : 0;
419     [m_wkView showDefinitionForAttributedString:attributedString.get() range:NSMakeRange(0, [attributedString.get() length]) options:options baselineOriginProvider:^(NSRange adjustedRange) { return (NSPoint)textBaselineOrigin; }];
420 #endif
421 }
422
423 void PageClientImpl::dismissDictionaryLookupPanel()
424 {
425 #if !defined(BUILDING_ON_SNOW_LEOPARD)
426     WKHideWordDefinitionWindow();
427 #endif
428 }
429
430 void PageClientImpl::showCorrectionPanel(CorrectionPanelInfo::PanelType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
431 {
432 #if !defined(BUILDING_ON_SNOW_LEOPARD)
433     if (!isViewVisible() || !isViewInWindow())
434         return;
435     m_correctionPanel.show(m_wkView, type, boundingBoxOfReplacedString, replacedString, replacementString, alternativeReplacementStrings);
436 #endif
437 }
438
439 void PageClientImpl::dismissCorrectionPanel(ReasonForDismissingCorrectionPanel reason)
440 {
441 #if !defined(BUILDING_ON_SNOW_LEOPARD)
442     m_correctionPanel.dismiss(reason);
443 #endif
444 }
445
446 String PageClientImpl::dismissCorrectionPanelSoon(WebCore::ReasonForDismissingCorrectionPanel reason)
447 {
448 #if !defined(BUILDING_ON_SNOW_LEOPARD)
449     return m_correctionPanel.dismiss(reason);
450 #else
451     return String();
452 #endif
453 }
454
455 void PageClientImpl::recordAutocorrectionResponse(EditorClient::AutocorrectionResponseType responseType, const String& replacedString, const String& replacementString)
456 {
457 #if !defined(BUILDING_ON_SNOW_LEOPARD)
458     NSCorrectionResponse response = responseType == EditorClient::AutocorrectionReverted ? NSCorrectionResponseReverted : NSCorrectionResponseEdited;
459     CorrectionPanel::recordAutocorrectionResponse(m_wkView, response, replacedString, replacementString);
460 #endif
461 }
462
463 bool PageClientImpl::executeSavedCommandBySelector(const String& selectorString)
464 {
465     return [m_wkView _executeSavedCommandBySelector:NSSelectorFromString(selectorString)];
466 }
467
468 } // namespace WebKit