Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / WebProcess / WebCoreSupport / mac / WebEditorClientMac.mm
1 /*
2  * Copyright (C) 2006, 2010-2016 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #import "config.h"
31 #import "WebEditorClient.h"
32
33 #if PLATFORM(MAC)
34
35 #import "WebPage.h"
36 #import "WebPageProxyMessages.h"
37 #import "WebProcess.h"
38 #import <WebCore/Editor.h>
39 #import <WebCore/FocusController.h>
40 #import <WebCore/Frame.h>
41 #import <WebCore/KeyboardEvent.h>
42 #import <WebCore/NotImplemented.h>
43 #import <wtf/cocoa/NSURLExtras.h>
44
45 namespace WebKit {
46 using namespace WebCore;
47     
48 void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
49 {
50     if (m_page->handleEditingKeyboardEvent(event))
51         event->setDefaultHandled();
52 }
53
54 void WebEditorClient::handleInputMethodKeydown(KeyboardEvent* event)
55 {
56     if (event->handledByInputMethod())
57         event->setDefaultHandled();
58 }
59
60 void WebEditorClient::setInsertionPasteboard(const String&)
61 {
62     // This is used only by Mail, no need to implement it now.
63     notImplemented();
64 }
65
66 static void changeWordCase(WebPage* page, NSString *(*changeCase)(NSString *))
67 {
68     Frame& frame = page->corePage()->focusController().focusedOrMainFrame();
69     if (!frame.editor().canEdit())
70         return;
71
72     frame.editor().command("selectWord").execute();
73
74     NSString *selectedString = frame.displayStringModifiedByEncoding(frame.editor().selectedText());
75     page->replaceSelectionWithText(&frame, changeCase(selectedString));
76 }
77
78 void WebEditorClient::uppercaseWord()
79 {
80     changeWordCase(m_page, [] (NSString *string) {
81         return [string uppercaseString];
82     });
83 }
84
85 void WebEditorClient::lowercaseWord()
86 {
87     changeWordCase(m_page, [] (NSString *string) {
88         return [string lowercaseString];
89     });
90 }
91
92 void WebEditorClient::capitalizeWord()
93 {
94     changeWordCase(m_page, [] (NSString *string) {
95         return [string capitalizedString];
96     });
97 }
98
99 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
100
101 void WebEditorClient::showSubstitutionsPanel(bool)
102 {
103     notImplemented();
104 }
105
106 bool WebEditorClient::substitutionsPanelIsShowing()
107 {
108     bool isShowing { false };
109     m_page->sendSync(Messages::WebPageProxy::SubstitutionsPanelIsShowing(), Messages::WebPageProxy::SubstitutionsPanelIsShowing::Reply(isShowing));
110     return isShowing;
111 }
112
113 void WebEditorClient::toggleSmartInsertDelete()
114 {
115     m_page->send(Messages::WebPageProxy::toggleSmartInsertDelete());
116 }
117
118 bool WebEditorClient::isAutomaticQuoteSubstitutionEnabled()
119 {
120     if (m_page->isControlledByAutomation())
121         return false;
122
123     return WebProcess::singleton().textCheckerState().isAutomaticQuoteSubstitutionEnabled;
124 }
125
126 void WebEditorClient::toggleAutomaticQuoteSubstitution()
127 {
128     m_page->send(Messages::WebPageProxy::toggleAutomaticQuoteSubstitution());
129 }
130
131 bool WebEditorClient::isAutomaticLinkDetectionEnabled()
132 {
133     return WebProcess::singleton().textCheckerState().isAutomaticLinkDetectionEnabled;
134 }
135
136 void WebEditorClient::toggleAutomaticLinkDetection()
137 {
138     m_page->send(Messages::WebPageProxy::toggleAutomaticLinkDetection());
139 }
140
141 bool WebEditorClient::isAutomaticDashSubstitutionEnabled()
142 {
143     if (m_page->isControlledByAutomation())
144         return false;
145
146     return WebProcess::singleton().textCheckerState().isAutomaticDashSubstitutionEnabled;
147 }
148
149 void WebEditorClient::toggleAutomaticDashSubstitution()
150 {
151     m_page->send(Messages::WebPageProxy::toggleAutomaticDashSubstitution());
152 }
153
154 bool WebEditorClient::isAutomaticTextReplacementEnabled()
155 {
156     if (m_page->isControlledByAutomation())
157         return false;
158
159     return WebProcess::singleton().textCheckerState().isAutomaticTextReplacementEnabled;
160 }
161
162 void WebEditorClient::toggleAutomaticTextReplacement()
163 {
164     m_page->send(Messages::WebPageProxy::toggleAutomaticTextReplacement());
165 }
166
167 bool WebEditorClient::isAutomaticSpellingCorrectionEnabled()
168 {
169     if (m_page->isControlledByAutomation())
170         return false;
171
172     return WebProcess::singleton().textCheckerState().isAutomaticSpellingCorrectionEnabled;
173 }
174
175 void WebEditorClient::toggleAutomaticSpellingCorrection()
176 {
177     notImplemented();
178 }
179
180 #endif // USE(AUTOMATIC_TEXT_REPLACEMENT)
181
182 } // namespace WebKit
183
184 #endif // PLATFORM(MAC)