Define DOM_KEY_LOCATION_* constants on KeyboardEvent
[WebKit-https.git] / Source / WebCore / dom / KeyboardEvent.cpp
1 /**
2  * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3  * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5  * Copyright (C) 2003, 2005, 2006, 2007, 2013 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "config.h"
24 #include "KeyboardEvent.h"
25
26 #include "Document.h"
27 #include "DOMWindow.h"
28 #include "EventDispatcher.h"
29 #include "EventNames.h"
30 #include "EventHandler.h"
31 #include "Frame.h"
32 #include "PlatformKeyboardEvent.h"
33 #include "Settings.h"
34 #include "WindowsKeyboardCodes.h"
35
36 namespace WebCore {
37
38 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::Type type)
39 {
40     switch (type) {
41         case PlatformEvent::KeyUp:
42             return eventNames().keyupEvent;
43         case PlatformEvent::RawKeyDown:
44             return eventNames().keydownEvent;
45         case PlatformEvent::Char:
46             return eventNames().keypressEvent;
47         case PlatformEvent::KeyDown:
48             // The caller should disambiguate the combined event into RawKeyDown or Char events.
49             break;
50         default:
51             break;
52     }
53     ASSERT_NOT_REACHED();
54     return eventNames().keydownEvent;
55 }
56
57 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode)
58 {
59     switch (keycode) {
60     case VK_LCONTROL:
61     case VK_RCONTROL:
62         return VK_CONTROL;
63     case VK_LSHIFT:
64     case VK_RSHIFT:
65         return VK_SHIFT;
66     case VK_LMENU:
67     case VK_RMENU:
68         return VK_MENU;
69     default:
70         return keycode;
71     }
72 }
73
74 static inline KeyboardEvent::KeyLocationCode keyLocationCode(const PlatformKeyboardEvent& key)
75 {
76     if (key.isKeypad())
77         return KeyboardEvent::DOM_KEY_LOCATION_NUMPAD;
78
79     switch (key.windowsVirtualKeyCode()) {
80     case VK_LCONTROL:
81     case VK_LSHIFT:
82     case VK_LMENU:
83     case VK_LWIN:
84         return KeyboardEvent::DOM_KEY_LOCATION_LEFT;
85     case VK_RCONTROL:
86     case VK_RSHIFT:
87     case VK_RMENU:
88     case VK_RWIN:
89         return KeyboardEvent::DOM_KEY_LOCATION_RIGHT;
90     default:
91         return KeyboardEvent::DOM_KEY_LOCATION_STANDARD;
92     }
93 }
94
95 KeyboardEventInit::KeyboardEventInit()
96     : location(0)
97     , ctrlKey(false)
98     , altKey(false)
99     , shiftKey(false)
100     , metaKey(false)
101 {
102 }
103
104 KeyboardEvent::KeyboardEvent()
105     : m_location(DOM_KEY_LOCATION_STANDARD)
106     , m_altGraphKey(false)
107 {
108 }
109
110 KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view)
111     : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()),
112                           true, true, key.timestamp(), view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey())
113     , m_keyEvent(adoptPtr(new PlatformKeyboardEvent(key)))
114     , m_keyIdentifier(key.keyIdentifier())
115     , m_location(keyLocationCode(key))
116     , m_altGraphKey(false)
117 {
118 }
119
120 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, const KeyboardEventInit& initializer)
121     : UIEventWithKeyState(eventType, initializer.bubbles, initializer.cancelable, initializer.view, initializer.detail, initializer.ctrlKey, initializer.altKey, initializer.shiftKey, initializer.metaKey)
122     , m_keyIdentifier(initializer.keyIdentifier)
123     , m_location(initializer.location)
124     , m_altGraphKey(false)
125 {
126 }
127
128 KeyboardEvent::~KeyboardEvent()
129 {
130 }
131
132 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
133                                       const String &keyIdentifier, unsigned location,
134                                       bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
135 {
136     if (dispatched())
137         return;
138
139     initUIEvent(type, canBubble, cancelable, view, 0);
140
141     m_keyIdentifier = keyIdentifier;
142     m_location = location;
143     m_ctrlKey = ctrlKey;
144     m_shiftKey = shiftKey;
145     m_altKey = altKey;
146     m_metaKey = metaKey;
147     m_altGraphKey = altGraphKey;
148 }
149
150 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
151 {
152     if (keyIdentifier == "Control")
153         return ctrlKey();
154     if (keyIdentifier == "Shift")
155         return shiftKey();
156     if (keyIdentifier == "Alt")
157         return altKey();
158     if (keyIdentifier == "Meta")
159         return metaKey();
160     return false;
161 }
162
163 int KeyboardEvent::keyCode() const
164 {
165     // IE: virtual key code for keyup/keydown, character code for keypress
166     // Firefox: virtual key code for keyup/keydown, zero for keypress
167     // We match IE.
168     if (!m_keyEvent)
169         return 0;
170     if (type() == eventNames().keydownEvent || type() == eventNames().keyupEvent)
171         return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode());
172
173     return charCode();
174 }
175
176 int KeyboardEvent::charCode() const
177 {
178     // IE: not supported
179     // Firefox: 0 for keydown/keyup events, character code for keypress
180     // We match Firefox, unless in backward compatibility mode, where we always return the character code.
181     bool backwardCompatibilityMode = false;
182     if (view() && view()->frame())
183         backwardCompatibilityMode = view()->frame()->eventHandler()->needsKeyboardEventDisambiguationQuirks();
184
185     if (!m_keyEvent || (type() != eventNames().keypressEvent && !backwardCompatibilityMode))
186         return 0;
187     String text = m_keyEvent->text();
188     return static_cast<int>(text.characterStartingAt(0));
189 }
190
191 const AtomicString& KeyboardEvent::interfaceName() const
192 {
193     return eventNames().interfaceForKeyboardEvent;
194 }
195
196 bool KeyboardEvent::isKeyboardEvent() const
197 {
198     return true;
199 }
200
201 int KeyboardEvent::which() const
202 {
203     // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
204     // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
205     return keyCode();
206 }
207
208 KeyboardEvent* findKeyboardEvent(Event* event)
209 {
210     for (Event* e = event; e; e = e->underlyingEvent())
211         if (e->isKeyboardEvent())
212             return static_cast<KeyboardEvent*>(e);
213     return 0;
214 }
215
216 PassRefPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtr<KeyboardEvent> event)
217 {
218     return adoptRef(new KeyboardEventDispatchMediator(event));
219 }
220
221 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtr<KeyboardEvent> event)
222     : EventDispatchMediator(event)
223 {
224 }
225
226 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
227 {
228     // Make sure not to return true if we already took default action while handling the event.
229     return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
230 }
231
232 } // namespace WebCore