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, 2004, 2005, 2006, 2007, 2008 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 
24 #ifndef KeyboardEvent_h
25 #define KeyboardEvent_h
26 
27 #include "UIEventWithKeyState.h"
28 #include <wtf/Vector.h>
29 
30 namespace WebCore {
31 
32     class EventDispatcher;
33     class Node;
34     class PlatformKeyboardEvent;
35 
36 #if PLATFORM(MAC)
37     struct KeypressCommand {
KeypressCommandKeypressCommand38         KeypressCommand() { }
KeypressCommandKeypressCommand39         KeypressCommand(const String& commandName) : commandName(commandName) { ASSERT(isASCIILower(commandName[0U])); }
KeypressCommandKeypressCommand40         KeypressCommand(const String& commandName, const String& text) : commandName(commandName), text(text) { ASSERT(commandName == "insertText:"); }
41 
42         String commandName; // Actually, a selector name - it may have a trailing colon, and a name that can be different from an editor command name.
43         String text;
44     };
45 #endif
46 
47     // Introduced in DOM Level 3
48     class KeyboardEvent : public UIEventWithKeyState {
49     public:
50         enum KeyLocationCode {
51             DOM_KEY_LOCATION_STANDARD      = 0x00,
52             DOM_KEY_LOCATION_LEFT          = 0x01,
53             DOM_KEY_LOCATION_RIGHT         = 0x02,
54             DOM_KEY_LOCATION_NUMPAD        = 0x03
55         };
56 
create()57         static PassRefPtr<KeyboardEvent> create()
58         {
59             return adoptRef(new KeyboardEvent);
60         }
create(const PlatformKeyboardEvent & platformEvent,AbstractView * view)61         static PassRefPtr<KeyboardEvent> create(const PlatformKeyboardEvent& platformEvent, AbstractView* view)
62         {
63             return adoptRef(new KeyboardEvent(platformEvent, view));
64         }
create(const AtomicString & type,bool canBubble,bool cancelable,AbstractView * view,const String & keyIdentifier,unsigned keyLocation,bool ctrlKey,bool altKey,bool shiftKey,bool metaKey,bool altGraphKey)65         static PassRefPtr<KeyboardEvent> create(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
66             const String& keyIdentifier, unsigned keyLocation,
67             bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
68         {
69             return adoptRef(new KeyboardEvent(type, canBubble, cancelable, view, keyIdentifier, keyLocation,
70                 ctrlKey, altKey, shiftKey, metaKey, altGraphKey));
71         }
72         virtual ~KeyboardEvent();
73 
74         void initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
75                                const String& keyIdentifier, unsigned keyLocation,
76                                bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey = false);
77 
keyIdentifier()78         const String& keyIdentifier() const { return m_keyIdentifier; }
keyLocation()79         unsigned keyLocation() const { return m_keyLocation; }
80 
81         bool getModifierState(const String& keyIdentifier) const;
82 
altGraphKey()83         bool altGraphKey() const { return m_altGraphKey; }
84 
keyEvent()85         const PlatformKeyboardEvent* keyEvent() const { return m_keyEvent.get(); }
86 
87         int keyCode() const; // key code for keydown and keyup, character for keypress
88         int charCode() const; // character code for keypress, 0 for keydown and keyup
89 
90         virtual bool isKeyboardEvent() const;
91         virtual int which() const;
92 
93 #if PLATFORM(MAC)
94         // We only have this need to store keypress command info on the Mac.
keypressCommands()95         Vector<KeypressCommand>& keypressCommands() { return m_keypressCommands; }
96 #endif
97 
98     private:
99         KeyboardEvent();
100         KeyboardEvent(const PlatformKeyboardEvent&, AbstractView*);
101         KeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
102                       const String& keyIdentifier, unsigned keyLocation,
103                       bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey);
104 
105         OwnPtr<PlatformKeyboardEvent> m_keyEvent;
106         String m_keyIdentifier;
107         unsigned m_keyLocation;
108         bool m_altGraphKey : 1;
109 
110 #if PLATFORM(MAC)
111         // Commands that were sent by AppKit when interpreting the event. Doesn't include input method commands.
112         Vector<KeypressCommand> m_keypressCommands;
113 #endif
114     };
115 
116     KeyboardEvent* findKeyboardEvent(Event*);
117 
118 class KeyboardEventDispatchMediator : public EventDispatchMediator {
119 public:
120     explicit KeyboardEventDispatchMediator(PassRefPtr<KeyboardEvent>);
121 
122 private:
123     virtual bool dispatchEvent(EventDispatcher*) const;
124 };
125 
126 } // namespace WebCore
127 
128 #endif // KeyboardEvent_h
129