WebKit2 version of <rdar://problem/6097826> Mail's cursor does not become a resize...
[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 "NativeWebKeyboardEvent.h"
27 #import "PageClientImpl.h"
28
29 #import "DataReference.h"
30 #import "FindIndicator.h"
31 #import "WKAPICast.h"
32 #import "WKStringCF.h"
33 #import "WKViewInternal.h"
34 #import "WebContextMenuProxyMac.h"
35 #import "WebEditCommandProxy.h"
36 #import "WebPopupMenuProxyMac.h"
37 #import <WebCore/Cursor.h>
38 #import <WebCore/FloatRect.h>
39 #import <WebCore/FoundationExtras.h>
40 #import <WebCore/GraphicsContext.h>
41 #import <WebCore/KeyboardEvent.h>
42 #import <wtf/PassOwnPtr.h>
43 #import <wtf/text/CString.h>
44 #import <wtf/text/WTFString.h>
45
46 @interface NSApplication (WebNSApplicationDetails)
47 - (NSCursor *)_cursorRectCursor;
48 @end
49
50 using namespace WebCore;
51
52 @interface WebEditCommandObjC : NSObject
53 {
54     RefPtr<WebKit::WebEditCommandProxy> m_command;
55 }
56
57 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command;
58 - (WebKit::WebEditCommandProxy*)command;
59
60 @end
61
62 @implementation WebEditCommandObjC
63
64 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebKit::WebEditCommandProxy>)command
65 {
66     self = [super init];
67     if (!self)
68         return nil;
69
70     m_command = command;
71     return self;
72 }
73
74 - (WebKit::WebEditCommandProxy*)command
75 {
76     return m_command.get();
77 }
78
79 @end
80
81 @interface WebEditorUndoTargetObjC : NSObject
82
83 - (void)undoEditing:(id)sender;
84 - (void)redoEditing:(id)sender;
85
86 @end
87
88 @implementation WebEditorUndoTargetObjC
89
90 - (void)undoEditing:(id)sender
91 {
92     ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
93     [sender command]->unapply();
94 }
95
96 - (void)redoEditing:(id)sender
97 {
98     ASSERT([sender isKindOfClass:[WebEditCommandObjC class]]);
99     [sender command]->reapply();
100 }
101
102 @end
103
104 namespace WebKit {
105
106 NSString* nsStringFromWebCoreString(const String& string)
107 {
108     return string.impl() ? HardAutorelease(WKStringCopyCFString(0, toAPI(string.impl()))) : @"";
109 }
110
111 PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
112 {
113     return adoptPtr(new PageClientImpl(wkView));
114 }
115
116 PageClientImpl::PageClientImpl(WKView* wkView)
117     : m_wkView(wkView)
118     , m_undoTarget(AdoptNS, [[WebEditorUndoTargetObjC alloc] init])
119 {
120 }
121
122 PageClientImpl::~PageClientImpl()
123 {
124 }
125
126 PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
127 {
128     return [m_wkView _createDrawingAreaProxy];
129 }
130
131 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
132 {
133     [m_wkView setNeedsDisplayInRect:rect];
134 }
135
136 void PageClientImpl::displayView()
137 {
138     [m_wkView displayIfNeeded];
139 }
140
141 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
142 {
143     NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
144
145     [m_wkView translateRectsNeedingDisplayInRect:clippedScrollRect by:scrollOffset];
146     [m_wkView scrollRect:clippedScrollRect by:scrollOffset];
147 }
148
149 IntSize PageClientImpl::viewSize()
150 {
151     return IntSize([m_wkView bounds].size);
152 }
153
154 bool PageClientImpl::isViewWindowActive()
155 {
156     return [[m_wkView window] isKeyWindow];
157 }
158
159 bool PageClientImpl::isViewFocused()
160 {
161     return [m_wkView _isFocused];
162 }
163
164 bool PageClientImpl::isViewVisible()
165 {
166     if (![m_wkView window])
167         return false;
168
169     if ([m_wkView isHiddenOrHasHiddenAncestor])
170         return false;
171
172     return true;
173 }
174
175 bool PageClientImpl::isViewInWindow()
176 {
177     return [m_wkView window];
178 }
179
180 void PageClientImpl::processDidCrash()
181 {
182     [m_wkView _processDidCrash];
183 }
184
185 void PageClientImpl::didRelaunchProcess()
186 {
187     [m_wkView _didRelaunchProcess];
188 }
189
190 void PageClientImpl::takeFocus(bool direction)
191 {
192     [m_wkView _takeFocus:direction];
193 }
194
195 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
196 {
197     [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
198 }
199
200 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
201 {
202     if (![NSApp _cursorRectCursor])
203         [m_wkView _setCursor:cursor.platformCursor()];
204 }
205
206 void PageClientImpl::setViewportArguments(const WebCore::ViewportArguments&)
207 {
208
209 }
210
211 static NSString* nameForEditAction(EditAction editAction)
212 {
213     // FIXME: Use localized strings.
214     // FIXME: Move this to a platform independent location.
215
216     switch (editAction) {
217     case EditActionUnspecified: return nil;
218     case EditActionSetColor: return @"Set Color";
219     case EditActionSetBackgroundColor: return @"Set Background Color";
220     case EditActionTurnOffKerning: return @"Turn Off Kerning";
221     case EditActionTightenKerning: return @"Tighten Kerning";
222     case EditActionLoosenKerning: return @"Loosen Kerning";
223     case EditActionUseStandardKerning: return @"Use Standard Kerning";
224     case EditActionTurnOffLigatures: return @"Turn Off Ligatures";
225     case EditActionUseStandardLigatures: return @"Use Standard Ligatures";
226     case EditActionUseAllLigatures: return @"Use All Ligatures";
227     case EditActionRaiseBaseline: return @"Raise Baseline";
228     case EditActionLowerBaseline: return @"Lower Baseline";
229     case EditActionSetTraditionalCharacterShape: return @"Set Traditional Character Shape";
230     case EditActionSetFont: return @"Set Font";
231     case EditActionChangeAttributes: return @"Change Attributes";
232     case EditActionAlignLeft: return @"Align Left";
233     case EditActionAlignRight: return @"Align Right";
234     case EditActionCenter: return @"Center";
235     case EditActionJustify: return @"Justify";
236     case EditActionSetWritingDirection: return @"Set Writing Direction";
237     case EditActionSubscript: return @"Subscript";
238     case EditActionSuperscript: return @"Superscript";
239     case EditActionUnderline: return @"Underline";
240     case EditActionOutline: return @"Outline";
241     case EditActionUnscript: return @"Unscript";
242     case EditActionDrag: return @"Drag";
243     case EditActionCut: return @"Cut";
244     case EditActionPaste: return @"Paste";
245     case EditActionPasteFont: return @"Paste Font";
246     case EditActionPasteRuler: return @"Paste Ruler";
247     case EditActionTyping: return @"Typing";
248     case EditActionCreateLink: return @"Create Link";
249     case EditActionUnlink: return @"Unlink";
250     case EditActionInsertList: return @"Insert List";
251     case EditActionFormatBlock: return @"Formatting";
252     case EditActionIndent: return @"Indent";
253     case EditActionOutdent: return @"Outdent";
254     }
255     return nil;
256 }
257
258 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
259 {
260     RefPtr<WebEditCommandProxy> command = prpCommand;
261
262     RetainPtr<WebEditCommandObjC> commandObjC(AdoptNS, [[WebEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
263     NSString *actionName = nameForEditAction(command->editAction());
264
265     NSUndoManager *undoManager = [m_wkView undoManager];
266     [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
267     if (actionName)
268         [undoManager setActionName:actionName];
269 }
270
271 void PageClientImpl::clearAllEditCommands()
272 {
273     [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
274 }
275
276 void PageClientImpl::setEditCommandState(const String& commandName, bool isEnabled, int newState)
277 {
278     [m_wkView _setUserInterfaceItemState:nsStringFromWebCoreString(commandName) enabled:isEnabled state:newState];
279 }
280
281 void PageClientImpl::interceptKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commandsList, uint32_t selectionStart, uint32_t selectionEnd, Vector<WebCore::CompositionUnderline>& underlines)
282 {
283     commandsList = [m_wkView _interceptKeyEvent:event.nativeEvent()];
284     [m_wkView _getTextInputState:selectionStart selectionEnd:selectionEnd underlines:underlines];
285 }
286
287 void PageClientImpl::setDragImage(const IntPoint& clientPosition, const IntSize& imageSize, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
288 {
289     OwnPtr<GraphicsContext> graphicsContext = dragImage->createGraphicsContext();
290     RetainPtr<NSImage> dragNSImage(AdoptNS, [[NSImage alloc] initWithCGImage:CGBitmapContextCreateImage(graphicsContext->platformContext()) size:imageSize]);
291     [dragNSImage.get() setFlipped:YES];
292     [m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
293 }
294     
295 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
296 {
297     return [m_wkView _convertToDeviceSpace:rect];
298 }
299
300 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
301 {
302     return [m_wkView _convertToUserSpace:rect];
303 }
304
305 void PageClientImpl::didNotHandleKeyEvent(const NativeWebKeyboardEvent& event)
306 {
307     NSEvent* nativeEvent = event.nativeEvent();
308     if ([nativeEvent type] == NSKeyDown) {
309         [m_wkView _setEventBeingResent:nativeEvent];
310         [[NSApplication sharedApplication] sendEvent:nativeEvent];
311     }
312 }
313
314 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
315 {
316     return WebPopupMenuProxyMac::create(m_wkView, page);
317 }
318
319 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
320 {
321     return WebContextMenuProxyMac::create(m_wkView, page);
322 }
323
324 void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut)
325 {
326     [m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut];
327 }
328
329 void PageClientImpl::accessibilityChildTokenReceived(const CoreIPC::DataReference& data)
330 {
331     NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
332     [m_wkView _setAccessibilityChildToken:remoteToken];
333 }
334     
335 #if USE(ACCELERATED_COMPOSITING)
336 void PageClientImpl::pageDidEnterAcceleratedCompositing()
337 {
338     [m_wkView _pageDidEnterAcceleratedCompositing];
339 }
340
341 void PageClientImpl::pageDidLeaveAcceleratedCompositing()
342 {
343     [m_wkView _pageDidLeaveAcceleratedCompositing];
344 }
345 #endif // USE(ACCELERATED_COMPOSITING)
346
347 void PageClientImpl::setComplexTextInputEnabled(uint64_t pluginComplexTextInputIdentifier, bool complexTextInputEnabled)
348 {
349     [m_wkView _setComplexTextInputEnabled:complexTextInputEnabled pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
350 }
351
352 CGContextRef PageClientImpl::containingWindowGraphicsContext()
353 {
354     return static_cast<CGContextRef>([[[m_wkView window] graphicsContext] graphicsPort]);
355 }
356
357 void PageClientImpl::didCommitLoadForMainFrame(bool useCustomRepresentation)
358 {
359     [m_wkView _setPageHasCustomRepresentation:useCustomRepresentation];
360 }
361
362 void PageClientImpl::didFinishLoadingDataForCustomRepresentation(const CoreIPC::DataReference& dataReference)
363 {
364     [m_wkView _didFinishLoadingDataForCustomRepresentation:dataReference];
365 }
366
367 double PageClientImpl::customRepresentationZoomFactor()
368 {
369     return [m_wkView _customRepresentationZoomFactor];
370 }
371
372 void PageClientImpl::setCustomRepresentationZoomFactor(double zoomFactor)
373 {
374     [m_wkView _setCustomRepresentationZoomFactor:zoomFactor];
375 }
376
377 } // namespace WebKit