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