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