2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "WebCoreArgumentCoders.h"
30 #include "WebPageProxyMessages.h"
31 #include "WebProcess.h"
32 #include <WebCore/FocusController.h>
33 #include <WebCore/Frame.h>
34 #include <WebCore/KeyboardEvent.h>
35 #include <WebCore/Page.h>
36 #include <WebCore/PlatformKeyboardEvent.h>
37 #include <WebCore/WindowsKeyboardCodes.h>
39 using namespace WebCore;
43 void WebPage::platformInitialize()
45 m_page->addSchedulePair(SchedulePair::create([NSRunLoop currentRunLoop], kCFRunLoopCommonModes));
48 void WebPage::platformPreferencesDidChange(const WebPreferencesStore&)
52 // FIXME: need to add support for input methods
54 bool WebPage::interceptEditingKeyboardEvent(KeyboardEvent* evt, bool shouldSaveCommand)
56 Node* node = evt->target()->toNode();
58 Frame* frame = node->document()->frame();
61 const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
64 const Vector<KeypressCommand>& commands = evt->keypressCommands();
65 bool hasKeypressCommand = !commands.isEmpty();
67 bool eventWasHandled = false;
69 if (shouldSaveCommand && !hasKeypressCommand) {
70 Vector<KeypressCommand> commandsList;
71 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::InterpretKeyEvent(keyEvent->type()), Messages::WebPageProxy::InterpretKeyEvent::Reply(commandsList), m_pageID))
73 for (size_t i = 0; i < commandsList.size(); i++)
74 evt->keypressCommands().append(commandsList[i]);
76 size_t size = commands.size();
77 // Are there commands that would just cause text insertion if executed via Editor?
78 // WebKit doesn't have enough information about mode to decide how they should be treated, so we leave it upon WebCore
79 // to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
80 // (e.g. Tab that inserts a Tab character, or Enter).
81 bool haveTextInsertionCommands = false;
82 for (size_t i = 0; i < size; ++i) {
83 if (frame->editor()->command(commands[i].commandName).isTextInsertion())
84 haveTextInsertionCommands = true;
86 if (!haveTextInsertionCommands || keyEvent->type() == PlatformKeyboardEvent::Char) {
87 for (size_t i = 0; i < size; ++i) {
88 if (commands[i].commandName == "insertText") {
89 // Don't insert null or control characters as they can result in unexpected behaviour
90 if (evt->charCode() < ' ')
92 eventWasHandled = frame->editor()->insertText(commands[i].text, evt);
94 if (frame->editor()->command(commands[i].commandName).isSupported())
95 eventWasHandled = frame->editor()->command(commands[i].commandName).execute(evt);
99 return eventWasHandled;
102 static inline void scroll(Page* page, ScrollDirection direction, ScrollGranularity granularity)
104 page->focusController()->focusedOrMainFrame()->eventHandler()->scrollRecursively(direction, granularity);
107 bool WebPage::performDefaultBehaviorForKeyEvent(const WebKeyboardEvent& keyboardEvent)
109 if (keyboardEvent.type() != WebEvent::KeyDown)
112 // FIXME: This should be in WebCore.
114 switch (keyboardEvent.windowsVirtualKeyCode()) {
116 if (keyboardEvent.shiftKey())
122 if (keyboardEvent.shiftKey())
123 scroll(m_page.get(), ScrollUp, ScrollByPage);
125 scroll(m_page.get(), ScrollDown, ScrollByPage);
128 scroll(m_page.get(), ScrollUp, ScrollByPage);
131 scroll(m_page.get(), ScrollDown, ScrollByPage);
134 scroll(m_page.get(), ScrollUp, ScrollByDocument);
135 scroll(m_page.get(), ScrollLeft, ScrollByDocument);
138 scroll(m_page.get(), ScrollDown, ScrollByDocument);
139 scroll(m_page.get(), ScrollLeft, ScrollByDocument);
142 if (keyboardEvent.shiftKey())
144 if (keyboardEvent.metaKey()) {
145 scroll(m_page.get(), ScrollUp, ScrollByDocument);
146 scroll(m_page.get(), ScrollLeft, ScrollByDocument);
147 } else if (keyboardEvent.altKey() || keyboardEvent.controlKey())
148 scroll(m_page.get(), ScrollUp, ScrollByPage);
150 scroll(m_page.get(), ScrollUp, ScrollByLine);
153 if (keyboardEvent.shiftKey())
155 if (keyboardEvent.metaKey()) {
156 scroll(m_page.get(), ScrollDown, ScrollByDocument);
157 scroll(m_page.get(), ScrollLeft, ScrollByDocument);
158 } else if (keyboardEvent.altKey() || keyboardEvent.controlKey())
159 scroll(m_page.get(), ScrollDown, ScrollByPage);
161 scroll(m_page.get(), ScrollDown, ScrollByLine);
164 if (keyboardEvent.shiftKey())
166 if (keyboardEvent.metaKey())
169 if (keyboardEvent.altKey() | keyboardEvent.controlKey())
170 scroll(m_page.get(), ScrollLeft, ScrollByPage);
172 scroll(m_page.get(), ScrollLeft, ScrollByLine);
176 if (keyboardEvent.shiftKey())
178 if (keyboardEvent.metaKey())
181 if (keyboardEvent.altKey() || keyboardEvent.controlKey())
182 scroll(m_page.get(), ScrollRight, ScrollByPage);
184 scroll(m_page.get(), ScrollRight, ScrollByLine);
194 } // namespace WebKit