Home | History | Annotate | Download | only in events
      1 /*
      2  * Copyright (C) 2001 Peter Kelly (pmk (at) post.com)
      3  * Copyright (C) 2001 Tobias Anton (anton (at) stud.fbi.fh-darmstadt.de)
      4  * Copyright (C) 2006 Samuel Weinig (sam.weinig (at) 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 "core/events/EventDispatchMediator.h"
     28 #include "core/events/UIEventWithKeyState.h"
     29 
     30 namespace WebCore {
     31 
     32 class EventDispatcher;
     33 class Node;
     34 class PlatformKeyboardEvent;
     35 
     36 struct KeyboardEventInit : public UIEventInit {
     37     KeyboardEventInit();
     38 
     39     String keyIdentifier;
     40     unsigned location;
     41     bool ctrlKey;
     42     bool altKey;
     43     bool shiftKey;
     44     bool metaKey;
     45     bool repeat;
     46 };
     47 
     48 class KeyboardEvent FINAL : 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 
     57     static PassRefPtrWillBeRawPtr<KeyboardEvent> create()
     58     {
     59         return adoptRefWillBeNoop(new KeyboardEvent);
     60     }
     61 
     62     static PassRefPtrWillBeRawPtr<KeyboardEvent> create(const PlatformKeyboardEvent& platformEvent, AbstractView* view)
     63     {
     64         return adoptRefWillBeNoop(new KeyboardEvent(platformEvent, view));
     65     }
     66 
     67     static PassRefPtrWillBeRawPtr<KeyboardEvent> create(const AtomicString& type, const KeyboardEventInit& initializer)
     68     {
     69         return adoptRefWillBeNoop(new KeyboardEvent(type, initializer));
     70     }
     71 
     72     static PassRefPtrWillBeRawPtr<KeyboardEvent> create(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
     73         const String& keyIdentifier, unsigned location,
     74         bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
     75     {
     76         return adoptRefWillBeNoop(new KeyboardEvent(type, canBubble, cancelable, view, keyIdentifier, location,
     77         ctrlKey, altKey, shiftKey, metaKey, altGraphKey));
     78     }
     79 
     80     virtual ~KeyboardEvent();
     81 
     82     void initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
     83         const String& keyIdentifier, unsigned location,
     84         bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey = false);
     85 
     86     const String& keyIdentifier() const { return m_keyIdentifier; }
     87     unsigned location() const { return m_location; }
     88 
     89     bool getModifierState(const String& keyIdentifier) const;
     90 
     91     bool altGraphKey() const { return m_altGraphKey; }
     92 
     93     const PlatformKeyboardEvent* keyEvent() const { return m_keyEvent.get(); }
     94 
     95     virtual int keyCode() const OVERRIDE; // key code for keydown and keyup, character for keypress
     96     virtual int charCode() const OVERRIDE; // character code for keypress, 0 for keydown and keyup
     97     bool repeat() const { return m_isAutoRepeat; }
     98 
     99     virtual const AtomicString& interfaceName() const OVERRIDE;
    100     virtual bool isKeyboardEvent() const OVERRIDE;
    101     virtual int which() const OVERRIDE;
    102 
    103     virtual void trace(Visitor*) OVERRIDE;
    104 
    105 private:
    106     KeyboardEvent();
    107     KeyboardEvent(const PlatformKeyboardEvent&, AbstractView*);
    108     KeyboardEvent(const AtomicString&, const KeyboardEventInit&);
    109     KeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
    110         const String& keyIdentifier, unsigned location,
    111         bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey);
    112 
    113     OwnPtr<PlatformKeyboardEvent> m_keyEvent;
    114     String m_keyIdentifier;
    115     unsigned m_location;
    116     bool m_altGraphKey : 1;
    117     bool m_isAutoRepeat : 1;
    118 };
    119 
    120 class KeyboardEventDispatchMediator : public EventDispatchMediator {
    121 public:
    122     static PassRefPtrWillBeRawPtr<KeyboardEventDispatchMediator> create(PassRefPtrWillBeRawPtr<KeyboardEvent>);
    123 private:
    124     explicit KeyboardEventDispatchMediator(PassRefPtrWillBeRawPtr<KeyboardEvent>);
    125     virtual bool dispatchEvent(EventDispatcher*) const OVERRIDE;
    126 };
    127 
    128 DEFINE_EVENT_TYPE_CASTS(KeyboardEvent);
    129 
    130 } // namespace WebCore
    131 
    132 #endif // KeyboardEvent_h
    133