|
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 #include "WebPage.h" |
|
27 |
|
28 #include <WebCore/KeyboardEvent.h> |
|
29 #include <WebCore/Page.h> |
|
30 #include <WebCore/PlatformKeyboardEvent.h> |
|
31 |
|
32 using namespace WebCore; |
|
33 |
|
34 namespace WebKit { |
|
35 |
|
36 void WebPage::platformInitialize() |
|
37 { |
|
38 m_page->addSchedulePair(SchedulePair::create([NSRunLoop currentRunLoop], kCFRunLoopCommonModes)); |
|
39 } |
|
40 |
|
41 // FIXME: Editor commands should not be hard coded and instead should come from AppKit. |
|
42 |
|
43 static const unsigned CtrlKey = 1 << 0; |
|
44 static const unsigned AltKey = 1 << 1; |
|
45 static const unsigned ShiftKey = 1 << 2; |
|
46 static const unsigned MetaKey = 1 << 3; |
|
47 |
|
48 static const unsigned VKEY_BACK = 0x08; |
|
49 static const unsigned VKEY_TAB = 0x09; |
|
50 static const unsigned VKEY_RETURN = 0x0D; |
|
51 static const unsigned VKEY_ESCAPE = 0x1B; |
|
52 static const unsigned VKEY_PRIOR = 0x21; |
|
53 static const unsigned VKEY_NEXT = 0x22; |
|
54 static const unsigned VKEY_END = 0x23; |
|
55 static const unsigned VKEY_HOME = 0x24; |
|
56 static const unsigned VKEY_LEFT = 0x25; |
|
57 static const unsigned VKEY_UP = 0x26; |
|
58 static const unsigned VKEY_RIGHT = 0x27; |
|
59 static const unsigned VKEY_DOWN = 0x28; |
|
60 static const unsigned VKEY_INSERT = 0x2D; |
|
61 static const unsigned VKEY_DELETE = 0x2E; |
|
62 static const unsigned VKEY_OEM_PERIOD = 0xBE; |
|
63 |
|
64 // Keys with special meaning. These will be delegated to the editor using |
|
65 // the execCommand() method |
|
66 struct KeyDownEntry { |
|
67 unsigned virtualKey; |
|
68 unsigned modifiers; |
|
69 const char* name; |
|
70 }; |
|
71 |
|
72 struct KeyPressEntry { |
|
73 unsigned charCode; |
|
74 unsigned modifiers; |
|
75 const char* name; |
|
76 }; |
|
77 |
|
78 static const KeyDownEntry keyDownEntries[] = { |
|
79 { VKEY_LEFT, 0, "MoveLeft" }, |
|
80 { VKEY_LEFT, ShiftKey, "MoveLeftAndModifySelection" }, |
|
81 { VKEY_LEFT, AltKey, "MoveWordLeft" }, |
|
82 { VKEY_LEFT, AltKey | ShiftKey, "MoveWordLeftAndModifySelection" }, |
|
83 { VKEY_RIGHT, 0, "MoveRight" }, |
|
84 { VKEY_RIGHT, ShiftKey, "MoveRightAndModifySelection" }, |
|
85 { VKEY_RIGHT, AltKey, "MoveWordRight" }, |
|
86 { VKEY_RIGHT, AltKey | ShiftKey, "MoveWordRightAndModifySelection" }, |
|
87 { VKEY_UP, 0, "MoveUp" }, |
|
88 { VKEY_UP, ShiftKey, "MoveUpAndModifySelection" }, |
|
89 { VKEY_PRIOR, ShiftKey, "MovePageUpAndModifySelection" }, |
|
90 { VKEY_DOWN, 0, "MoveDown" }, |
|
91 { VKEY_DOWN, ShiftKey, "MoveDownAndModifySelection" }, |
|
92 { VKEY_NEXT, ShiftKey, "MovePageDownAndModifySelection" }, |
|
93 { VKEY_PRIOR, 0, "MovePageUp" }, |
|
94 { VKEY_NEXT, 0, "MovePageDown" }, |
|
95 |
|
96 { VKEY_HOME, 0, "MoveToBeginningOfLine" }, |
|
97 { VKEY_HOME, ShiftKey, "MoveToBeginningOfLineAndModifySelection" }, |
|
98 { VKEY_LEFT, MetaKey, "MoveToBeginningOfLine" }, |
|
99 { VKEY_LEFT, MetaKey | ShiftKey, "MoveToBeginningOfLineAndModifySelection" }, |
|
100 { VKEY_UP, MetaKey, "MoveToBeginningOfDocument" }, |
|
101 { VKEY_UP, MetaKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" }, |
|
102 |
|
103 { VKEY_END, 0, "MoveToEndOfLine" }, |
|
104 { VKEY_END, ShiftKey, "MoveToEndOfLineAndModifySelection" }, |
|
105 { VKEY_DOWN, MetaKey, "MoveToEndOfDocument" }, |
|
106 { VKEY_DOWN, MetaKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection" }, |
|
107 { VKEY_RIGHT, MetaKey, "MoveToEndOfLine" }, |
|
108 { VKEY_RIGHT, MetaKey | ShiftKey, "MoveToEndOfLineAndModifySelection" }, |
|
109 |
|
110 { VKEY_BACK, 0, "DeleteBackward" }, |
|
111 { VKEY_BACK, ShiftKey, "DeleteBackward" }, |
|
112 { VKEY_DELETE, 0, "DeleteForward" }, |
|
113 { VKEY_BACK, AltKey, "DeleteWordBackward" }, |
|
114 { VKEY_DELETE, AltKey, "DeleteWordForward" }, |
|
115 |
|
116 { 'B', CtrlKey, "ToggleBold" }, |
|
117 { 'I', CtrlKey, "ToggleItalic" }, |
|
118 |
|
119 { VKEY_ESCAPE, 0, "Cancel" }, |
|
120 { VKEY_OEM_PERIOD, CtrlKey, "Cancel" }, |
|
121 { VKEY_TAB, 0, "InsertTab" }, |
|
122 { VKEY_TAB, ShiftKey, "InsertBacktab" }, |
|
123 { VKEY_RETURN, 0, "InsertNewline" }, |
|
124 { VKEY_RETURN, CtrlKey, "InsertNewline" }, |
|
125 { VKEY_RETURN, AltKey, "InsertNewline" }, |
|
126 { VKEY_RETURN, AltKey | ShiftKey, "InsertNewline" }, |
|
127 { VKEY_RETURN, ShiftKey, "InsertLineBreak" }, |
|
128 |
|
129 { 'C', MetaKey, "Copy" }, |
|
130 { 'V', MetaKey, "Paste" }, |
|
131 { 'X', MetaKey, "Cut" }, |
|
132 { 'A', MetaKey, "SelectAll" }, |
|
133 { VKEY_INSERT, CtrlKey, "Copy" }, |
|
134 { VKEY_INSERT, ShiftKey, "Paste" }, |
|
135 { VKEY_DELETE, ShiftKey, "Cut" }, |
|
136 { 'Z', MetaKey, "Undo" }, |
|
137 { 'Z', MetaKey | ShiftKey, "Redo" }, |
|
138 }; |
|
139 |
|
140 static const KeyPressEntry keyPressEntries[] = { |
|
141 { '\t', 0, "InsertTab" }, |
|
142 { '\t', ShiftKey, "InsertBacktab" }, |
|
143 { '\r', 0, "InsertNewline" }, |
|
144 { '\r', CtrlKey, "InsertNewline" }, |
|
145 { '\r', ShiftKey, "InsertLineBreak" }, |
|
146 { '\r', AltKey, "InsertNewline" }, |
|
147 { '\r', AltKey | ShiftKey, "InsertNewline" }, |
|
148 }; |
|
149 |
|
150 const char* WebPage::interpretKeyEvent(const KeyboardEvent* evt) |
|
151 { |
|
152 const PlatformKeyboardEvent* keyEvent = evt->keyEvent(); |
|
153 ASSERT(keyEvent); |
|
154 |
|
155 static HashMap<int, const char*>* keyDownCommandsMap = 0; |
|
156 static HashMap<int, const char*>* keyPressCommandsMap = 0; |
|
157 |
|
158 if (!keyDownCommandsMap) { |
|
159 keyDownCommandsMap = new HashMap<int, const char*>; |
|
160 keyPressCommandsMap = new HashMap<int, const char*>; |
|
161 |
|
162 for (unsigned i = 0; i < (sizeof(keyDownEntries) / sizeof(keyDownEntries[0])); i++) { |
|
163 keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, |
|
164 keyDownEntries[i].name); |
|
165 } |
|
166 |
|
167 for (unsigned i = 0; i < (sizeof(keyPressEntries) / sizeof(keyPressEntries[0])); i++) { |
|
168 keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, |
|
169 keyPressEntries[i].name); |
|
170 } |
|
171 } |
|
172 |
|
173 unsigned modifiers = 0; |
|
174 if (keyEvent->shiftKey()) |
|
175 modifiers |= ShiftKey; |
|
176 if (keyEvent->altKey()) |
|
177 modifiers |= AltKey; |
|
178 if (keyEvent->ctrlKey()) |
|
179 modifiers |= CtrlKey; |
|
180 if (keyEvent->metaKey()) |
|
181 modifiers |= MetaKey; |
|
182 |
|
183 if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) { |
|
184 int mapKey = modifiers << 16 | evt->keyCode(); |
|
185 return mapKey ? keyDownCommandsMap->get(mapKey) : 0; |
|
186 } |
|
187 |
|
188 int mapKey = modifiers << 16 | evt->charCode(); |
|
189 return mapKey ? keyPressCommandsMap->get(mapKey) : 0; |
|
190 } |
|
191 |
|
192 } // namespace WebKit |