Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / events / KeyEventHandler.h
blob5d444cf3227082b4fa94ac0e83e11dbe42834847
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_KeyEventHandler_h_
8 #define mozilla_KeyEventHandler_h_
10 #include "mozilla/EventForwards.h"
11 #include "mozilla/MemoryReporting.h"
12 #include "nsAtom.h"
13 #include "nsString.h"
14 #include "nsCOMPtr.h"
15 #include "nsIController.h"
16 #include "nsIWeakReference.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "js/TypeDecls.h"
19 #include "mozilla/ShortcutKeys.h"
21 namespace mozilla {
23 namespace layers {
24 class KeyboardShortcut;
25 } // namespace layers
27 struct IgnoreModifierState;
29 namespace dom {
30 class Event;
31 class UIEvent;
32 class Element;
33 class EventTarget;
34 class KeyboardEvent;
35 class Element;
36 } // namespace dom
38 // Values of the reserved attribute. When unset, the default value depends on
39 // the permissions.default.shortcuts preference.
40 enum ReservedKey : uint8_t {
41 ReservedKey_False = 0,
42 ReservedKey_True = 1,
43 ReservedKey_Unset = 2,
46 class KeyEventHandler final {
47 public:
48 // This constructor is used only by XUL key handlers (e.g., <key>)
49 explicit KeyEventHandler(dom::Element* aHandlerElement,
50 ReservedKey aReserved);
52 // This constructor is used for keyboard handlers for browser, editor, input
53 // and textarea elements.
54 explicit KeyEventHandler(ShortcutKeyData* aKeyData);
56 ~KeyEventHandler();
58 /**
59 * Try and convert this XBL handler into an APZ KeyboardShortcut for handling
60 * key events on the compositor thread. This only works for XBL handlers that
61 * represent scroll commands.
63 * @param aOut the converted KeyboardShortcut, must be non null
64 * @return whether the handler was converted into a KeyboardShortcut
66 bool TryConvertToKeyboardShortcut(layers::KeyboardShortcut* aOut) const;
68 bool EventTypeEquals(nsAtom* aEventType) const {
69 return mEventName == aEventType;
72 // if aCharCode is not zero, it is used instead of the charCode of
73 // aKeyEventHandler.
74 bool KeyEventMatched(dom::KeyboardEvent* aDomKeyboardEvent,
75 uint32_t aCharCode,
76 const IgnoreModifierState& aIgnoreModifierState);
78 /**
79 * Check whether the handler element is disabled. Note that this requires
80 * a QI to getting GetHandlerELement(). Therefore, this should not be used
81 * first in multiple checks.
83 bool KeyElementIsDisabled() const;
85 already_AddRefed<dom::Element> GetHandlerElement() const;
87 ReservedKey GetIsReserved() { return mReserved; }
89 KeyEventHandler* GetNextHandler() { return mNextHandler; }
90 void SetNextHandler(KeyEventHandler* aHandler) { mNextHandler = aHandler; }
92 MOZ_CAN_RUN_SCRIPT
93 nsresult ExecuteHandler(dom::EventTarget* aTarget, dom::Event* aEvent);
95 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
97 void GetCommand(nsAString& aCommand) const;
99 public:
100 static uint32_t gRefCnt;
102 protected:
103 void Init() { ++gRefCnt; }
105 already_AddRefed<nsIController> GetController(dom::EventTarget* aTarget);
107 inline int32_t GetMatchingKeyCode(const nsAString& aKeyName);
108 void ConstructPrototype(dom::Element* aKeyElement,
109 const char16_t* aEvent = nullptr,
110 const char16_t* aCommand = nullptr,
111 const char16_t* aKeyCode = nullptr,
112 const char16_t* aCharCode = nullptr,
113 const char16_t* aModifiers = nullptr);
114 void BuildModifiers(nsAString& aModifiers);
116 void ReportKeyConflict(const char16_t* aKey, const char16_t* aModifiers,
117 dom::Element* aKeyElement, const char* aMessageName);
118 void GetEventType(nsAString& aEvent);
119 bool ModifiersMatchMask(dom::UIEvent* aEvent,
120 const IgnoreModifierState& aIgnoreModifierState);
121 MOZ_CAN_RUN_SCRIPT
122 nsresult DispatchXBLCommand(dom::EventTarget* aTarget, dom::Event* aEvent);
123 MOZ_CAN_RUN_SCRIPT
124 nsresult DispatchXULKeyCommand(dom::Event* aEvent);
126 Modifiers GetModifiers() const;
127 Modifiers GetModifiersMask() const;
129 static int32_t KeyToMask(uint32_t aKey);
130 static int32_t AccelKeyMask();
132 static const int32_t cShift;
133 static const int32_t cAlt;
134 static const int32_t cControl;
135 static const int32_t cMeta;
136 static const int32_t cOS;
138 static const int32_t cShiftMask;
139 static const int32_t cAltMask;
140 static const int32_t cControlMask;
141 static const int32_t cMetaMask;
142 static const int32_t cOSMask;
144 static const int32_t cAllModifiers;
146 protected:
147 union {
148 nsIWeakReference*
149 mHandlerElement; // For XUL <key> element handlers. [STRONG]
150 char16_t* mCommand; // For built-in shortcuts the command to execute.
153 // The following four values make up 32 bits.
154 bool mIsXULKey; // This handler is either for a XUL <key> element or it is
155 // a command dispatcher.
156 uint8_t mMisc; // Miscellaneous extra information. For key events,
157 // stores whether or not we're a key code or char code.
158 // For mouse events, stores the clickCount.
160 ReservedKey mReserved; // <key> is reserved for chrome. Not used by handlers.
162 int32_t mKeyMask; // Which modifier keys this event handler expects to have
163 // down in order to be matched.
165 // The primary filter information for mouse/key events.
166 int32_t mDetail; // For key events, contains a charcode or keycode. For
167 // mouse events, stores the button info.
169 // Prototype handlers are chained. We own the next handler in the chain.
170 KeyEventHandler* mNextHandler;
171 RefPtr<nsAtom> mEventName; // The type of the event, e.g., "keypress"
174 } // namespace mozilla
176 #endif