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, 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
24#ifndef KeyboardEvent_h
25#define KeyboardEvent_h
26
27#include "KeypressCommand.h"
28#include "UIEventWithKeyState.h"
29#include <memory>
30#include <wtf/Vector.h>
31
32namespace WebCore {
33
34class Node;
35class PlatformKeyboardEvent;
36
37struct KeyboardEventInit : public UIEventInit {
38    KeyboardEventInit();
39
40    String keyIdentifier;
41    unsigned location;
42    bool ctrlKey;
43    bool altKey;
44    bool shiftKey;
45    bool metaKey;
46};
47
48class KeyboardEvent : public UIEventWithKeyState {
49public:
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        // FIXME: The following values are not supported yet (crbug.com/265446)
56        // DOM_KEY_LOCATION_MOBILE     = 0x04,
57        // DOM_KEY_LOCATION_JOYSTICK   = 0x05
58    };
59
60    static PassRefPtr<KeyboardEvent> create()
61    {
62        return adoptRef(new KeyboardEvent);
63    }
64
65    static PassRefPtr<KeyboardEvent> create(const PlatformKeyboardEvent& platformEvent, AbstractView* view)
66    {
67        return adoptRef(new KeyboardEvent(platformEvent, view));
68    }
69
70    static PassRefPtr<KeyboardEvent> create(const AtomicString& type, const KeyboardEventInit& initializer)
71    {
72        return adoptRef(new KeyboardEvent(type, initializer));
73    }
74
75    virtual ~KeyboardEvent();
76
77    void initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
78        const String& keyIdentifier, unsigned location,
79        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey = false);
80
81    const String& keyIdentifier() const { return m_keyIdentifier; }
82    unsigned location() const { return m_location; }
83
84    bool getModifierState(const String& keyIdentifier) const;
85
86    bool altGraphKey() const { return m_altGraphKey; }
87
88    const PlatformKeyboardEvent* keyEvent() const { return m_keyEvent.get(); }
89
90    virtual int keyCode() const override; // key code for keydown and keyup, character for keypress
91    virtual int charCode() const override; // character code for keypress, 0 for keydown and keyup
92
93    virtual EventInterface eventInterface() const override;
94    virtual bool isKeyboardEvent() const override;
95    virtual int which() const override;
96
97#if PLATFORM(COCOA)
98    bool handledByInputMethod() const { return m_handledByInputMethod; }
99    const Vector<KeypressCommand>& keypressCommands() const { return m_keypressCommands; }
100
101    // The non-const version is still needed for WebKit1, which doesn't construct a complete KeyboardEvent with interpreted commands yet.
102    Vector<KeypressCommand>& keypressCommands() { return m_keypressCommands; }
103#endif
104
105private:
106    KeyboardEvent();
107    KeyboardEvent(const PlatformKeyboardEvent&, AbstractView*);
108    KeyboardEvent(const AtomicString&, const KeyboardEventInit&);
109
110    std::unique_ptr<PlatformKeyboardEvent> m_keyEvent;
111    String m_keyIdentifier;
112    unsigned m_location;
113    bool m_altGraphKey : 1;
114
115#if PLATFORM(COCOA)
116    // Commands that were sent by AppKit when interpreting the event. Doesn't include input method commands.
117    bool m_handledByInputMethod;
118    Vector<KeypressCommand> m_keypressCommands;
119#endif
120};
121
122EVENT_TYPE_CASTS(KeyboardEvent)
123
124KeyboardEvent* findKeyboardEvent(Event*);
125
126} // namespace WebCore
127
128#endif // KeyboardEvent_h
129