dom/events/KeyboardEvent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/KeyboardEvent.h"
michael@0 7 #include "mozilla/TextEvents.h"
michael@0 8 #include "prtime.h"
michael@0 9
michael@0 10 namespace mozilla {
michael@0 11 namespace dom {
michael@0 12
michael@0 13 KeyboardEvent::KeyboardEvent(EventTarget* aOwner,
michael@0 14 nsPresContext* aPresContext,
michael@0 15 WidgetKeyboardEvent* aEvent)
michael@0 16 : UIEvent(aOwner, aPresContext,
michael@0 17 aEvent ? aEvent : new WidgetKeyboardEvent(false, 0, nullptr))
michael@0 18 , mInitializedByCtor(false)
michael@0 19 , mInitialzedWhichValue(0)
michael@0 20 {
michael@0 21 NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
michael@0 22
michael@0 23 if (aEvent) {
michael@0 24 mEventIsInternal = false;
michael@0 25 }
michael@0 26 else {
michael@0 27 mEventIsInternal = true;
michael@0 28 mEvent->time = PR_Now();
michael@0 29 mEvent->AsKeyboardEvent()->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 NS_IMPL_ADDREF_INHERITED(KeyboardEvent, UIEvent)
michael@0 34 NS_IMPL_RELEASE_INHERITED(KeyboardEvent, UIEvent)
michael@0 35
michael@0 36 NS_INTERFACE_MAP_BEGIN(KeyboardEvent)
michael@0 37 NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
michael@0 38 NS_INTERFACE_MAP_END_INHERITING(UIEvent)
michael@0 39
michael@0 40 bool
michael@0 41 KeyboardEvent::AltKey()
michael@0 42 {
michael@0 43 return mEvent->AsKeyboardEvent()->IsAlt();
michael@0 44 }
michael@0 45
michael@0 46 NS_IMETHODIMP
michael@0 47 KeyboardEvent::GetAltKey(bool* aIsDown)
michael@0 48 {
michael@0 49 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 50 *aIsDown = AltKey();
michael@0 51 return NS_OK;
michael@0 52 }
michael@0 53
michael@0 54 bool
michael@0 55 KeyboardEvent::CtrlKey()
michael@0 56 {
michael@0 57 return mEvent->AsKeyboardEvent()->IsControl();
michael@0 58 }
michael@0 59
michael@0 60 NS_IMETHODIMP
michael@0 61 KeyboardEvent::GetCtrlKey(bool* aIsDown)
michael@0 62 {
michael@0 63 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 64 *aIsDown = CtrlKey();
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67
michael@0 68 bool
michael@0 69 KeyboardEvent::ShiftKey()
michael@0 70 {
michael@0 71 return mEvent->AsKeyboardEvent()->IsShift();
michael@0 72 }
michael@0 73
michael@0 74 NS_IMETHODIMP
michael@0 75 KeyboardEvent::GetShiftKey(bool* aIsDown)
michael@0 76 {
michael@0 77 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 78 *aIsDown = ShiftKey();
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81
michael@0 82 bool
michael@0 83 KeyboardEvent::MetaKey()
michael@0 84 {
michael@0 85 return mEvent->AsKeyboardEvent()->IsMeta();
michael@0 86 }
michael@0 87
michael@0 88 NS_IMETHODIMP
michael@0 89 KeyboardEvent::GetMetaKey(bool* aIsDown)
michael@0 90 {
michael@0 91 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 92 *aIsDown = MetaKey();
michael@0 93 return NS_OK;
michael@0 94 }
michael@0 95
michael@0 96 bool
michael@0 97 KeyboardEvent::Repeat()
michael@0 98 {
michael@0 99 return mEvent->AsKeyboardEvent()->mIsRepeat;
michael@0 100 }
michael@0 101
michael@0 102 NS_IMETHODIMP
michael@0 103 KeyboardEvent::GetRepeat(bool* aIsRepeat)
michael@0 104 {
michael@0 105 NS_ENSURE_ARG_POINTER(aIsRepeat);
michael@0 106 *aIsRepeat = Repeat();
michael@0 107 return NS_OK;
michael@0 108 }
michael@0 109
michael@0 110 bool
michael@0 111 KeyboardEvent::IsComposing()
michael@0 112 {
michael@0 113 return mEvent->AsKeyboardEvent()->mIsComposing;
michael@0 114 }
michael@0 115
michael@0 116 NS_IMETHODIMP
michael@0 117 KeyboardEvent::GetModifierState(const nsAString& aKey,
michael@0 118 bool* aState)
michael@0 119 {
michael@0 120 NS_ENSURE_ARG_POINTER(aState);
michael@0 121
michael@0 122 *aState = GetModifierState(aKey);
michael@0 123 return NS_OK;
michael@0 124 }
michael@0 125
michael@0 126 NS_IMETHODIMP
michael@0 127 KeyboardEvent::GetKey(nsAString& aKeyName)
michael@0 128 {
michael@0 129 mEvent->AsKeyboardEvent()->GetDOMKeyName(aKeyName);
michael@0 130 return NS_OK;
michael@0 131 }
michael@0 132
michael@0 133 NS_IMETHODIMP
michael@0 134 KeyboardEvent::GetCharCode(uint32_t* aCharCode)
michael@0 135 {
michael@0 136 NS_ENSURE_ARG_POINTER(aCharCode);
michael@0 137 *aCharCode = CharCode();
michael@0 138 return NS_OK;
michael@0 139 }
michael@0 140
michael@0 141 uint32_t
michael@0 142 KeyboardEvent::CharCode()
michael@0 143 {
michael@0 144 // If this event is initialized with ctor, we shouldn't check event type.
michael@0 145 if (mInitializedByCtor) {
michael@0 146 return mEvent->AsKeyboardEvent()->charCode;
michael@0 147 }
michael@0 148
michael@0 149 switch (mEvent->message) {
michael@0 150 case NS_KEY_UP:
michael@0 151 case NS_KEY_DOWN:
michael@0 152 return 0;
michael@0 153 case NS_KEY_PRESS:
michael@0 154 return mEvent->AsKeyboardEvent()->charCode;
michael@0 155 }
michael@0 156 return 0;
michael@0 157 }
michael@0 158
michael@0 159 NS_IMETHODIMP
michael@0 160 KeyboardEvent::GetKeyCode(uint32_t* aKeyCode)
michael@0 161 {
michael@0 162 NS_ENSURE_ARG_POINTER(aKeyCode);
michael@0 163 *aKeyCode = KeyCode();
michael@0 164 return NS_OK;
michael@0 165 }
michael@0 166
michael@0 167 uint32_t
michael@0 168 KeyboardEvent::KeyCode()
michael@0 169 {
michael@0 170 // If this event is initialized with ctor, we shouldn't check event type.
michael@0 171 if (mInitializedByCtor) {
michael@0 172 return mEvent->AsKeyboardEvent()->keyCode;
michael@0 173 }
michael@0 174
michael@0 175 switch (mEvent->message) {
michael@0 176 case NS_KEY_UP:
michael@0 177 case NS_KEY_PRESS:
michael@0 178 case NS_KEY_DOWN:
michael@0 179 return mEvent->AsKeyboardEvent()->keyCode;
michael@0 180 }
michael@0 181 return 0;
michael@0 182 }
michael@0 183
michael@0 184 uint32_t
michael@0 185 KeyboardEvent::Which()
michael@0 186 {
michael@0 187 // If this event is initialized with ctor, which can have independent value.
michael@0 188 if (mInitializedByCtor) {
michael@0 189 return mInitialzedWhichValue;
michael@0 190 }
michael@0 191
michael@0 192 switch (mEvent->message) {
michael@0 193 case NS_KEY_UP:
michael@0 194 case NS_KEY_DOWN:
michael@0 195 return KeyCode();
michael@0 196 case NS_KEY_PRESS:
michael@0 197 //Special case for 4xp bug 62878. Try to make value of which
michael@0 198 //more closely mirror the values that 4.x gave for RETURN and BACKSPACE
michael@0 199 {
michael@0 200 uint32_t keyCode = mEvent->AsKeyboardEvent()->keyCode;
michael@0 201 if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
michael@0 202 return keyCode;
michael@0 203 }
michael@0 204 return CharCode();
michael@0 205 }
michael@0 206 }
michael@0 207
michael@0 208 return 0;
michael@0 209 }
michael@0 210
michael@0 211 NS_IMETHODIMP
michael@0 212 KeyboardEvent::GetLocation(uint32_t* aLocation)
michael@0 213 {
michael@0 214 NS_ENSURE_ARG_POINTER(aLocation);
michael@0 215
michael@0 216 *aLocation = Location();
michael@0 217 return NS_OK;
michael@0 218 }
michael@0 219
michael@0 220 uint32_t
michael@0 221 KeyboardEvent::Location()
michael@0 222 {
michael@0 223 return mEvent->AsKeyboardEvent()->location;
michael@0 224 }
michael@0 225
michael@0 226 // static
michael@0 227 already_AddRefed<KeyboardEvent>
michael@0 228 KeyboardEvent::Constructor(const GlobalObject& aGlobal,
michael@0 229 const nsAString& aType,
michael@0 230 const KeyboardEventInit& aParam,
michael@0 231 ErrorResult& aRv)
michael@0 232 {
michael@0 233 nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
michael@0 234 nsRefPtr<KeyboardEvent> newEvent =
michael@0 235 new KeyboardEvent(target, nullptr, nullptr);
michael@0 236 bool trusted = newEvent->Init(target);
michael@0 237 aRv = newEvent->InitKeyEvent(aType, aParam.mBubbles, aParam.mCancelable,
michael@0 238 aParam.mView, aParam.mCtrlKey, aParam.mAltKey,
michael@0 239 aParam.mShiftKey, aParam.mMetaKey,
michael@0 240 aParam.mKeyCode, aParam.mCharCode);
michael@0 241 newEvent->SetTrusted(trusted);
michael@0 242 newEvent->mDetail = aParam.mDetail;
michael@0 243 newEvent->mInitializedByCtor = true;
michael@0 244 newEvent->mInitialzedWhichValue = aParam.mWhich;
michael@0 245
michael@0 246 WidgetKeyboardEvent* internalEvent = newEvent->mEvent->AsKeyboardEvent();
michael@0 247 internalEvent->location = aParam.mLocation;
michael@0 248 internalEvent->mIsRepeat = aParam.mRepeat;
michael@0 249 internalEvent->mIsComposing = aParam.mIsComposing;
michael@0 250 internalEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
michael@0 251 internalEvent->mKeyValue = aParam.mKey;
michael@0 252
michael@0 253 return newEvent.forget();
michael@0 254 }
michael@0 255
michael@0 256 NS_IMETHODIMP
michael@0 257 KeyboardEvent::InitKeyEvent(const nsAString& aType,
michael@0 258 bool aCanBubble,
michael@0 259 bool aCancelable,
michael@0 260 nsIDOMWindow* aView,
michael@0 261 bool aCtrlKey,
michael@0 262 bool aAltKey,
michael@0 263 bool aShiftKey,
michael@0 264 bool aMetaKey,
michael@0 265 uint32_t aKeyCode,
michael@0 266 uint32_t aCharCode)
michael@0 267 {
michael@0 268 nsresult rv = UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
michael@0 269 NS_ENSURE_SUCCESS(rv, rv);
michael@0 270
michael@0 271 WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
michael@0 272 keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
michael@0 273 keyEvent->keyCode = aKeyCode;
michael@0 274 keyEvent->charCode = aCharCode;
michael@0 275
michael@0 276 return NS_OK;
michael@0 277 }
michael@0 278
michael@0 279 } // namespace dom
michael@0 280 } // namespace mozilla
michael@0 281
michael@0 282 using namespace mozilla;
michael@0 283 using namespace mozilla::dom;
michael@0 284
michael@0 285 nsresult
michael@0 286 NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
michael@0 287 EventTarget* aOwner,
michael@0 288 nsPresContext* aPresContext,
michael@0 289 WidgetKeyboardEvent* aEvent)
michael@0 290 {
michael@0 291 KeyboardEvent* it = new KeyboardEvent(aOwner, aPresContext, aEvent);
michael@0 292 NS_ADDREF(it);
michael@0 293 *aInstancePtrResult = static_cast<Event*>(it);
michael@0 294 return NS_OK;
michael@0 295 }

mercurial