OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007 Apple, Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007 Apple, Inc. All rights reserved. |
3 * Copyright (C) 2012 Google, Inc. All rights reserved. | 3 * Copyright (C) 2012 Google, Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 */ | 25 */ |
26 | 26 |
27 #include "sky/engine/config.h" | 27 #include "sky/engine/config.h" |
28 #include "sky/engine/core/editing/EditingBehavior.h" | 28 #include "sky/engine/core/editing/EditingBehavior.h" |
29 | 29 |
| 30 #include "gen/sky/core/EventTypeNames.h" |
30 #include "sky/engine/core/events/KeyboardEvent.h" | 31 #include "sky/engine/core/events/KeyboardEvent.h" |
31 #include "sky/engine/platform/KeyboardCodes.h" | 32 #include "sky/engine/platform/KeyboardCodes.h" |
32 #include "sky/engine/platform/PlatformKeyboardEvent.h" | |
33 | 33 |
34 namespace blink { | 34 namespace blink { |
35 | 35 |
36 // | 36 // |
37 // The below code was adapted from the WebKit file webview.cpp | 37 // The below code was adapted from the WebKit file webview.cpp |
38 // | 38 // |
39 | 39 |
40 static const unsigned CtrlKey = 1 << 0; | 40 static const unsigned CtrlKey = 1 << 0; |
41 static const unsigned AltKey = 1 << 1; | 41 static const unsigned AltKey = 1 << 1; |
42 static const unsigned ShiftKey = 1 << 2; | 42 static const unsigned ShiftKey = 1 << 2; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 { 'A', CtrlKey, "SelectAll" }, | 113 { 'A', CtrlKey, "SelectAll" }, |
114 { VKEY_INSERT, 0, "OverWrite" }, | 114 { VKEY_INSERT, 0, "OverWrite" }, |
115 }; | 115 }; |
116 | 116 |
117 static const KeyPressEntry keyPressEntries[] = { | 117 static const KeyPressEntry keyPressEntries[] = { |
118 { '\r', 0, "InsertNewline" }, | 118 { '\r', 0, "InsertNewline" }, |
119 }; | 119 }; |
120 | 120 |
121 const char* EditingBehavior::interpretKeyEvent(const KeyboardEvent& event) const | 121 const char* EditingBehavior::interpretKeyEvent(const KeyboardEvent& event) const |
122 { | 122 { |
123 const PlatformKeyboardEvent* keyEvent = event.keyEvent(); | |
124 if (!keyEvent) | |
125 return ""; | |
126 | |
127 static HashMap<int, const char*>* keyDownCommandsMap = 0; | 123 static HashMap<int, const char*>* keyDownCommandsMap = 0; |
128 static HashMap<int, const char*>* keyPressCommandsMap = 0; | 124 static HashMap<int, const char*>* keyPressCommandsMap = 0; |
129 | 125 |
130 if (!keyDownCommandsMap) { | 126 if (!keyDownCommandsMap) { |
131 keyDownCommandsMap = new HashMap<int, const char*>; | 127 keyDownCommandsMap = new HashMap<int, const char*>; |
132 keyPressCommandsMap = new HashMap<int, const char*>; | 128 keyPressCommandsMap = new HashMap<int, const char*>; |
133 | 129 |
134 for (unsigned i = 0; i < arraysize(keyDownEntries); i++) { | 130 for (unsigned i = 0; i < arraysize(keyDownEntries); i++) { |
135 keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownE
ntries[i].virtualKey, keyDownEntries[i].name); | 131 keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownE
ntries[i].virtualKey, keyDownEntries[i].name); |
136 } | 132 } |
137 | 133 |
138 for (unsigned i = 0; i < arraysize(keyPressEntries); i++) { | 134 for (unsigned i = 0; i < arraysize(keyPressEntries); i++) { |
139 keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPre
ssEntries[i].charCode, keyPressEntries[i].name); | 135 keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPre
ssEntries[i].charCode, keyPressEntries[i].name); |
140 } | 136 } |
141 } | 137 } |
142 | 138 |
143 unsigned modifiers = 0; | 139 unsigned modifiers = 0; |
144 if (keyEvent->shiftKey()) | 140 if (event.shiftKey()) |
145 modifiers |= ShiftKey; | 141 modifiers |= ShiftKey; |
146 if (keyEvent->altKey()) | 142 if (event.altKey()) |
147 modifiers |= AltKey; | 143 modifiers |= AltKey; |
148 if (keyEvent->ctrlKey()) | 144 if (event.ctrlKey()) |
149 modifiers |= CtrlKey; | 145 modifiers |= CtrlKey; |
150 if (keyEvent->metaKey()) | 146 if (event.metaKey()) |
151 modifiers |= MetaKey; | 147 modifiers |= MetaKey; |
152 | 148 |
153 if (keyEvent->type() == PlatformEvent::RawKeyDown) { | 149 if (event.type() == EventTypeNames::keydown) { |
154 int mapKey = modifiers << 16 | event.keyCode(); | 150 int mapKey = modifiers << 16 | event.key(); |
155 return mapKey ? keyDownCommandsMap->get(mapKey) : 0; | 151 return mapKey ? keyDownCommandsMap->get(mapKey) : 0; |
156 } | 152 } |
157 | 153 |
158 int mapKey = modifiers << 16 | event.charCode(); | 154 int mapKey = modifiers << 16 | event.charCode(); |
159 return mapKey ? keyPressCommandsMap->get(mapKey) : 0; | 155 return mapKey ? keyPressCommandsMap->get(mapKey) : 0; |
160 } | 156 } |
161 | 157 |
162 bool EditingBehavior::shouldInsertCharacter(const KeyboardEvent& event) const | 158 bool EditingBehavior::shouldInsertCharacter(const KeyboardEvent& event) const |
163 { | 159 { |
164 if (event.keyEvent()->text().length() != 1) | |
165 return true; | |
166 | |
167 // On Gtk/Linux, it emits key events with ASCII text and ctrl on for ctrl-<x
>. | 160 // On Gtk/Linux, it emits key events with ASCII text and ctrl on for ctrl-<x
>. |
168 // In Webkit, EditorClient::handleKeyboardEvent in | 161 // In Webkit, EditorClient::handleKeyboardEvent in |
169 // WebKit/gtk/WebCoreSupport/EditorClientGtk.cpp drop such events. | 162 // WebKit/gtk/WebCoreSupport/EditorClientGtk.cpp drop such events. |
170 // On Mac, it emits key events with ASCII text and meta on for Command-<x>. | 163 // On Mac, it emits key events with ASCII text and meta on for Command-<x>. |
171 // These key events should not emit text insert event. | 164 // These key events should not emit text insert event. |
172 // Alt key would be used to insert alternative character, so we should let | 165 // Alt key would be used to insert alternative character, so we should let |
173 // through. Also note that Ctrl-Alt combination equals to AltGr key which is | 166 // through. Also note that Ctrl-Alt combination equals to AltGr key which is |
174 // also used to insert alternative character. | 167 // also used to insert alternative character. |
175 // http://code.google.com/p/chromium/issues/detail?id=10846 | 168 // http://code.google.com/p/chromium/issues/detail?id=10846 |
176 // Windows sets both alt and meta are on when "Alt" key pressed. | 169 // Windows sets both alt and meta are on when "Alt" key pressed. |
177 // http://code.google.com/p/chromium/issues/detail?id=2215 | 170 // http://code.google.com/p/chromium/issues/detail?id=2215 |
178 // Also, we should not rely on an assumption that keyboards don't | 171 // Also, we should not rely on an assumption that keyboards don't |
179 // send ASCII characters when pressing a control key on Windows, | 172 // send ASCII characters when pressing a control key on Windows, |
180 // which may be configured to do it so by user. | 173 // which may be configured to do it so by user. |
181 // See also http://en.wikipedia.org/wiki/Keyboard_Layout | 174 // See also http://en.wikipedia.org/wiki/Keyboard_Layout |
182 // FIXME(ukai): investigate more detail for various keyboard layout. | 175 // FIXME(ukai): investigate more detail for various keyboard layout. |
183 UChar ch = event.keyEvent()->text()[0U]; | 176 UChar ch = event.charCode(); |
184 | 177 |
185 // Don't insert null or control characters as they can result in | 178 // Don't insert null or control characters as they can result in |
186 // unexpected behaviour | 179 // unexpected behaviour |
187 if (ch < ' ') | 180 if (ch < ' ') |
188 return false; | 181 return false; |
189 #if !OS(WIN) | 182 #if !OS(WIN) |
190 // Don't insert ASCII character if ctrl w/o alt or meta is on. | 183 // Don't insert ASCII character if ctrl w/o alt or meta is on. |
191 // On Mac, we should ignore events when meta is on (Command-<x>). | 184 // On Mac, we should ignore events when meta is on (Command-<x>). |
192 if (ch < 0x80) { | 185 if (ch < 0x80) { |
193 if (event.keyEvent()->ctrlKey() && !event.keyEvent()->altKey()) | 186 if (event.ctrlKey() && !event.altKey()) |
194 return false; | 187 return false; |
195 #if OS(MACOSX) | 188 #if OS(MACOSX) |
196 if (event.keyEvent()->metaKey()) | 189 if (event.metaKey()) |
197 return false; | 190 return false; |
198 #endif | 191 #endif |
199 } | 192 } |
200 #endif | 193 #endif |
201 | 194 |
202 return true; | 195 return true; |
203 } | 196 } |
204 } // namespace blink | 197 } // namespace blink |
205 | 198 |
OLD | NEW |