Wed, 31 Dec 2014 06:09:35 +0100
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 "base/basictypes.h" |
michael@0 | 7 | #include "ipc/IPCMessageUtils.h" |
michael@0 | 8 | #include "mozilla/dom/UIEvent.h" |
michael@0 | 9 | #include "mozilla/ArrayUtils.h" |
michael@0 | 10 | #include "mozilla/Assertions.h" |
michael@0 | 11 | #include "mozilla/ContentEvents.h" |
michael@0 | 12 | #include "mozilla/EventStateManager.h" |
michael@0 | 13 | #include "mozilla/TextEvents.h" |
michael@0 | 14 | #include "nsCOMPtr.h" |
michael@0 | 15 | #include "nsContentUtils.h" |
michael@0 | 16 | #include "nsIContent.h" |
michael@0 | 17 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 18 | #include "nsIDOMWindow.h" |
michael@0 | 19 | #include "nsIDOMNode.h" |
michael@0 | 20 | #include "nsIFrame.h" |
michael@0 | 21 | #include "prtime.h" |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | namespace dom { |
michael@0 | 25 | |
michael@0 | 26 | UIEvent::UIEvent(EventTarget* aOwner, |
michael@0 | 27 | nsPresContext* aPresContext, |
michael@0 | 28 | WidgetGUIEvent* aEvent) |
michael@0 | 29 | : Event(aOwner, aPresContext, |
michael@0 | 30 | aEvent ? aEvent : new InternalUIEvent(false, 0)) |
michael@0 | 31 | , mClientPoint(0, 0) |
michael@0 | 32 | , mLayerPoint(0, 0) |
michael@0 | 33 | , mPagePoint(0, 0) |
michael@0 | 34 | , mMovementPoint(0, 0) |
michael@0 | 35 | , mIsPointerLocked(EventStateManager::sIsPointerLocked) |
michael@0 | 36 | , mLastClientPoint(EventStateManager::sLastClientPoint) |
michael@0 | 37 | { |
michael@0 | 38 | if (aEvent) { |
michael@0 | 39 | mEventIsInternal = false; |
michael@0 | 40 | } |
michael@0 | 41 | else { |
michael@0 | 42 | mEventIsInternal = true; |
michael@0 | 43 | mEvent->time = PR_Now(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | // Fill mDetail and mView according to the mEvent (widget-generated |
michael@0 | 47 | // event) we've got |
michael@0 | 48 | switch(mEvent->eventStructType) |
michael@0 | 49 | { |
michael@0 | 50 | case NS_UI_EVENT: |
michael@0 | 51 | { |
michael@0 | 52 | mDetail = mEvent->AsUIEvent()->detail; |
michael@0 | 53 | break; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | case NS_SCROLLPORT_EVENT: |
michael@0 | 57 | { |
michael@0 | 58 | InternalScrollPortEvent* scrollEvent = mEvent->AsScrollPortEvent(); |
michael@0 | 59 | mDetail = (int32_t)scrollEvent->orient; |
michael@0 | 60 | break; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | default: |
michael@0 | 64 | mDetail = 0; |
michael@0 | 65 | break; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | mView = nullptr; |
michael@0 | 69 | if (mPresContext) |
michael@0 | 70 | { |
michael@0 | 71 | nsISupports* container = mPresContext->GetContainerWeak(); |
michael@0 | 72 | if (container) |
michael@0 | 73 | { |
michael@0 | 74 | nsCOMPtr<nsIDOMWindow> window = do_GetInterface(container); |
michael@0 | 75 | if (window) |
michael@0 | 76 | mView = do_QueryInterface(window); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // static |
michael@0 | 82 | already_AddRefed<UIEvent> |
michael@0 | 83 | UIEvent::Constructor(const GlobalObject& aGlobal, |
michael@0 | 84 | const nsAString& aType, |
michael@0 | 85 | const UIEventInit& aParam, |
michael@0 | 86 | ErrorResult& aRv) |
michael@0 | 87 | { |
michael@0 | 88 | nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports()); |
michael@0 | 89 | nsRefPtr<UIEvent> e = new UIEvent(t, nullptr, nullptr); |
michael@0 | 90 | bool trusted = e->Init(t); |
michael@0 | 91 | aRv = e->InitUIEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView, |
michael@0 | 92 | aParam.mDetail); |
michael@0 | 93 | e->SetTrusted(trusted); |
michael@0 | 94 | return e.forget(); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | NS_IMPL_CYCLE_COLLECTION_INHERITED(UIEvent, Event, |
michael@0 | 98 | mView) |
michael@0 | 99 | |
michael@0 | 100 | NS_IMPL_ADDREF_INHERITED(UIEvent, Event) |
michael@0 | 101 | NS_IMPL_RELEASE_INHERITED(UIEvent, Event) |
michael@0 | 102 | |
michael@0 | 103 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(UIEvent) |
michael@0 | 104 | NS_INTERFACE_MAP_ENTRY(nsIDOMUIEvent) |
michael@0 | 105 | NS_INTERFACE_MAP_END_INHERITING(Event) |
michael@0 | 106 | |
michael@0 | 107 | static nsIntPoint |
michael@0 | 108 | DevPixelsToCSSPixels(const LayoutDeviceIntPoint& aPoint, |
michael@0 | 109 | nsPresContext* aContext) |
michael@0 | 110 | { |
michael@0 | 111 | return nsIntPoint(aContext->DevPixelsToIntCSSPixels(aPoint.x), |
michael@0 | 112 | aContext->DevPixelsToIntCSSPixels(aPoint.y)); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | nsIntPoint |
michael@0 | 116 | UIEvent::GetMovementPoint() |
michael@0 | 117 | { |
michael@0 | 118 | if (mPrivateDataDuplicated) { |
michael@0 | 119 | return mMovementPoint; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | if (!mEvent || |
michael@0 | 123 | (mEvent->eventStructType != NS_MOUSE_EVENT && |
michael@0 | 124 | mEvent->eventStructType != NS_MOUSE_SCROLL_EVENT && |
michael@0 | 125 | mEvent->eventStructType != NS_WHEEL_EVENT && |
michael@0 | 126 | mEvent->eventStructType != NS_DRAG_EVENT && |
michael@0 | 127 | mEvent->eventStructType != NS_POINTER_EVENT && |
michael@0 | 128 | mEvent->eventStructType != NS_SIMPLE_GESTURE_EVENT) || |
michael@0 | 129 | !mEvent->AsGUIEvent()->widget) { |
michael@0 | 130 | return nsIntPoint(0, 0); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | // Calculate the delta between the last screen point and the current one. |
michael@0 | 134 | nsIntPoint current = DevPixelsToCSSPixels(mEvent->refPoint, mPresContext); |
michael@0 | 135 | nsIntPoint last = DevPixelsToCSSPixels(mEvent->lastRefPoint, mPresContext); |
michael@0 | 136 | return current - last; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | NS_IMETHODIMP |
michael@0 | 140 | UIEvent::GetView(nsIDOMWindow** aView) |
michael@0 | 141 | { |
michael@0 | 142 | *aView = mView; |
michael@0 | 143 | NS_IF_ADDREF(*aView); |
michael@0 | 144 | return NS_OK; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | NS_IMETHODIMP |
michael@0 | 148 | UIEvent::GetDetail(int32_t* aDetail) |
michael@0 | 149 | { |
michael@0 | 150 | *aDetail = mDetail; |
michael@0 | 151 | return NS_OK; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | NS_IMETHODIMP |
michael@0 | 155 | UIEvent::InitUIEvent(const nsAString& typeArg, |
michael@0 | 156 | bool canBubbleArg, |
michael@0 | 157 | bool cancelableArg, |
michael@0 | 158 | nsIDOMWindow* viewArg, |
michael@0 | 159 | int32_t detailArg) |
michael@0 | 160 | { |
michael@0 | 161 | if (viewArg) { |
michael@0 | 162 | nsCOMPtr<nsPIDOMWindow> view = do_QueryInterface(viewArg); |
michael@0 | 163 | NS_ENSURE_TRUE(view, NS_ERROR_INVALID_ARG); |
michael@0 | 164 | } |
michael@0 | 165 | nsresult rv = Event::InitEvent(typeArg, canBubbleArg, cancelableArg); |
michael@0 | 166 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 167 | |
michael@0 | 168 | mDetail = detailArg; |
michael@0 | 169 | mView = viewArg; |
michael@0 | 170 | |
michael@0 | 171 | return NS_OK; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | NS_IMETHODIMP |
michael@0 | 175 | UIEvent::GetPageX(int32_t* aPageX) |
michael@0 | 176 | { |
michael@0 | 177 | NS_ENSURE_ARG_POINTER(aPageX); |
michael@0 | 178 | *aPageX = PageX(); |
michael@0 | 179 | return NS_OK; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | int32_t |
michael@0 | 183 | UIEvent::PageX() const |
michael@0 | 184 | { |
michael@0 | 185 | if (mPrivateDataDuplicated) { |
michael@0 | 186 | return mPagePoint.x; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | return Event::GetPageCoords(mPresContext, mEvent, mEvent->refPoint, |
michael@0 | 190 | mClientPoint).x; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | NS_IMETHODIMP |
michael@0 | 194 | UIEvent::GetPageY(int32_t* aPageY) |
michael@0 | 195 | { |
michael@0 | 196 | NS_ENSURE_ARG_POINTER(aPageY); |
michael@0 | 197 | *aPageY = PageY(); |
michael@0 | 198 | return NS_OK; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | int32_t |
michael@0 | 202 | UIEvent::PageY() const |
michael@0 | 203 | { |
michael@0 | 204 | if (mPrivateDataDuplicated) { |
michael@0 | 205 | return mPagePoint.y; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | return Event::GetPageCoords(mPresContext, mEvent, mEvent->refPoint, |
michael@0 | 209 | mClientPoint).y; |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | NS_IMETHODIMP |
michael@0 | 213 | UIEvent::GetWhich(uint32_t* aWhich) |
michael@0 | 214 | { |
michael@0 | 215 | NS_ENSURE_ARG_POINTER(aWhich); |
michael@0 | 216 | *aWhich = Which(); |
michael@0 | 217 | return NS_OK; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | already_AddRefed<nsINode> |
michael@0 | 221 | UIEvent::GetRangeParent() |
michael@0 | 222 | { |
michael@0 | 223 | nsIFrame* targetFrame = nullptr; |
michael@0 | 224 | |
michael@0 | 225 | if (mPresContext) { |
michael@0 | 226 | targetFrame = mPresContext->EventStateManager()->GetEventTarget(); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | if (targetFrame) { |
michael@0 | 230 | nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent, |
michael@0 | 231 | targetFrame); |
michael@0 | 232 | nsCOMPtr<nsIContent> parent = targetFrame->GetContentOffsetsFromPoint(pt).content; |
michael@0 | 233 | if (parent) { |
michael@0 | 234 | if (parent->ChromeOnlyAccess() && |
michael@0 | 235 | !nsContentUtils::CanAccessNativeAnon()) { |
michael@0 | 236 | return nullptr; |
michael@0 | 237 | } |
michael@0 | 238 | return parent.forget(); |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | return nullptr; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | NS_IMETHODIMP |
michael@0 | 246 | UIEvent::GetRangeParent(nsIDOMNode** aRangeParent) |
michael@0 | 247 | { |
michael@0 | 248 | NS_ENSURE_ARG_POINTER(aRangeParent); |
michael@0 | 249 | *aRangeParent = nullptr; |
michael@0 | 250 | nsCOMPtr<nsINode> n = GetRangeParent(); |
michael@0 | 251 | if (n) { |
michael@0 | 252 | CallQueryInterface(n, aRangeParent); |
michael@0 | 253 | } |
michael@0 | 254 | return NS_OK; |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | NS_IMETHODIMP |
michael@0 | 258 | UIEvent::GetRangeOffset(int32_t* aRangeOffset) |
michael@0 | 259 | { |
michael@0 | 260 | NS_ENSURE_ARG_POINTER(aRangeOffset); |
michael@0 | 261 | *aRangeOffset = RangeOffset(); |
michael@0 | 262 | return NS_OK; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | int32_t |
michael@0 | 266 | UIEvent::RangeOffset() const |
michael@0 | 267 | { |
michael@0 | 268 | if (!mPresContext) { |
michael@0 | 269 | return 0; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget(); |
michael@0 | 273 | if (!targetFrame) { |
michael@0 | 274 | return 0; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent, |
michael@0 | 278 | targetFrame); |
michael@0 | 279 | return targetFrame->GetContentOffsetsFromPoint(pt).offset; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | NS_IMETHODIMP |
michael@0 | 283 | UIEvent::GetCancelBubble(bool* aCancelBubble) |
michael@0 | 284 | { |
michael@0 | 285 | NS_ENSURE_ARG_POINTER(aCancelBubble); |
michael@0 | 286 | *aCancelBubble = CancelBubble(); |
michael@0 | 287 | return NS_OK; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | NS_IMETHODIMP |
michael@0 | 291 | UIEvent::SetCancelBubble(bool aCancelBubble) |
michael@0 | 292 | { |
michael@0 | 293 | mEvent->mFlags.mPropagationStopped = aCancelBubble; |
michael@0 | 294 | return NS_OK; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | nsIntPoint |
michael@0 | 298 | UIEvent::GetLayerPoint() const |
michael@0 | 299 | { |
michael@0 | 300 | if (!mEvent || |
michael@0 | 301 | (mEvent->eventStructType != NS_MOUSE_EVENT && |
michael@0 | 302 | mEvent->eventStructType != NS_MOUSE_SCROLL_EVENT && |
michael@0 | 303 | mEvent->eventStructType != NS_WHEEL_EVENT && |
michael@0 | 304 | mEvent->eventStructType != NS_POINTER_EVENT && |
michael@0 | 305 | mEvent->eventStructType != NS_TOUCH_EVENT && |
michael@0 | 306 | mEvent->eventStructType != NS_DRAG_EVENT && |
michael@0 | 307 | mEvent->eventStructType != NS_SIMPLE_GESTURE_EVENT) || |
michael@0 | 308 | !mPresContext || |
michael@0 | 309 | mEventIsInternal) { |
michael@0 | 310 | return mLayerPoint; |
michael@0 | 311 | } |
michael@0 | 312 | // XXX I'm not really sure this is correct; it's my best shot, though |
michael@0 | 313 | nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget(); |
michael@0 | 314 | if (!targetFrame) |
michael@0 | 315 | return mLayerPoint; |
michael@0 | 316 | nsIFrame* layer = nsLayoutUtils::GetClosestLayer(targetFrame); |
michael@0 | 317 | nsPoint pt(nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent, layer)); |
michael@0 | 318 | return nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(pt.x), |
michael@0 | 319 | nsPresContext::AppUnitsToIntCSSPixels(pt.y)); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | NS_IMETHODIMP |
michael@0 | 323 | UIEvent::GetLayerX(int32_t* aLayerX) |
michael@0 | 324 | { |
michael@0 | 325 | NS_ENSURE_ARG_POINTER(aLayerX); |
michael@0 | 326 | *aLayerX = GetLayerPoint().x; |
michael@0 | 327 | return NS_OK; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | NS_IMETHODIMP |
michael@0 | 331 | UIEvent::GetLayerY(int32_t* aLayerY) |
michael@0 | 332 | { |
michael@0 | 333 | NS_ENSURE_ARG_POINTER(aLayerY); |
michael@0 | 334 | *aLayerY = GetLayerPoint().y; |
michael@0 | 335 | return NS_OK; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | NS_IMETHODIMP |
michael@0 | 339 | UIEvent::GetIsChar(bool* aIsChar) |
michael@0 | 340 | { |
michael@0 | 341 | *aIsChar = IsChar(); |
michael@0 | 342 | return NS_OK; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | bool |
michael@0 | 346 | UIEvent::IsChar() const |
michael@0 | 347 | { |
michael@0 | 348 | WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent(); |
michael@0 | 349 | if (keyEvent) { |
michael@0 | 350 | return keyEvent->isChar; |
michael@0 | 351 | } |
michael@0 | 352 | WidgetTextEvent* textEvent = mEvent->AsTextEvent(); |
michael@0 | 353 | return textEvent ? textEvent->isChar : false; |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | NS_IMETHODIMP |
michael@0 | 357 | UIEvent::DuplicatePrivateData() |
michael@0 | 358 | { |
michael@0 | 359 | mClientPoint = |
michael@0 | 360 | Event::GetClientCoords(mPresContext, mEvent, mEvent->refPoint, |
michael@0 | 361 | mClientPoint); |
michael@0 | 362 | mMovementPoint = GetMovementPoint(); |
michael@0 | 363 | mLayerPoint = GetLayerPoint(); |
michael@0 | 364 | mPagePoint = |
michael@0 | 365 | Event::GetPageCoords(mPresContext, mEvent, mEvent->refPoint, mClientPoint); |
michael@0 | 366 | // GetScreenPoint converts mEvent->refPoint to right coordinates. |
michael@0 | 367 | nsIntPoint screenPoint = |
michael@0 | 368 | Event::GetScreenCoords(mPresContext, mEvent, mEvent->refPoint); |
michael@0 | 369 | nsresult rv = Event::DuplicatePrivateData(); |
michael@0 | 370 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 371 | mEvent->refPoint = LayoutDeviceIntPoint::FromUntyped(screenPoint); |
michael@0 | 372 | } |
michael@0 | 373 | return rv; |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | NS_IMETHODIMP_(void) |
michael@0 | 377 | UIEvent::Serialize(IPC::Message* aMsg, bool aSerializeInterfaceType) |
michael@0 | 378 | { |
michael@0 | 379 | if (aSerializeInterfaceType) { |
michael@0 | 380 | IPC::WriteParam(aMsg, NS_LITERAL_STRING("uievent")); |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | Event::Serialize(aMsg, false); |
michael@0 | 384 | |
michael@0 | 385 | int32_t detail = 0; |
michael@0 | 386 | GetDetail(&detail); |
michael@0 | 387 | IPC::WriteParam(aMsg, detail); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | NS_IMETHODIMP_(bool) |
michael@0 | 391 | UIEvent::Deserialize(const IPC::Message* aMsg, void** aIter) |
michael@0 | 392 | { |
michael@0 | 393 | NS_ENSURE_TRUE(Event::Deserialize(aMsg, aIter), false); |
michael@0 | 394 | NS_ENSURE_TRUE(IPC::ReadParam(aMsg, aIter, &mDetail), false); |
michael@0 | 395 | return true; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | // XXX Following struct and array are used only in |
michael@0 | 399 | // UIEvent::ComputeModifierState(), but if we define them in it, |
michael@0 | 400 | // we fail to build on Mac at calling mozilla::ArrayLength(). |
michael@0 | 401 | struct ModifierPair |
michael@0 | 402 | { |
michael@0 | 403 | Modifier modifier; |
michael@0 | 404 | const char* name; |
michael@0 | 405 | }; |
michael@0 | 406 | static const ModifierPair kPairs[] = { |
michael@0 | 407 | { MODIFIER_ALT, NS_DOM_KEYNAME_ALT }, |
michael@0 | 408 | { MODIFIER_ALTGRAPH, NS_DOM_KEYNAME_ALTGRAPH }, |
michael@0 | 409 | { MODIFIER_CAPSLOCK, NS_DOM_KEYNAME_CAPSLOCK }, |
michael@0 | 410 | { MODIFIER_CONTROL, NS_DOM_KEYNAME_CONTROL }, |
michael@0 | 411 | { MODIFIER_FN, NS_DOM_KEYNAME_FN }, |
michael@0 | 412 | { MODIFIER_META, NS_DOM_KEYNAME_META }, |
michael@0 | 413 | { MODIFIER_NUMLOCK, NS_DOM_KEYNAME_NUMLOCK }, |
michael@0 | 414 | { MODIFIER_SCROLLLOCK, NS_DOM_KEYNAME_SCROLLLOCK }, |
michael@0 | 415 | { MODIFIER_SHIFT, NS_DOM_KEYNAME_SHIFT }, |
michael@0 | 416 | { MODIFIER_SYMBOLLOCK, NS_DOM_KEYNAME_SYMBOLLOCK }, |
michael@0 | 417 | { MODIFIER_OS, NS_DOM_KEYNAME_OS } |
michael@0 | 418 | }; |
michael@0 | 419 | |
michael@0 | 420 | // static |
michael@0 | 421 | Modifiers |
michael@0 | 422 | UIEvent::ComputeModifierState(const nsAString& aModifiersList) |
michael@0 | 423 | { |
michael@0 | 424 | if (aModifiersList.IsEmpty()) { |
michael@0 | 425 | return 0; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | // Be careful about the performance. If aModifiersList is too long, |
michael@0 | 429 | // parsing it needs too long time. |
michael@0 | 430 | // XXX Should we abort if aModifiersList is too long? |
michael@0 | 431 | |
michael@0 | 432 | Modifiers modifiers = 0; |
michael@0 | 433 | |
michael@0 | 434 | nsAString::const_iterator listStart, listEnd; |
michael@0 | 435 | aModifiersList.BeginReading(listStart); |
michael@0 | 436 | aModifiersList.EndReading(listEnd); |
michael@0 | 437 | |
michael@0 | 438 | for (uint32_t i = 0; i < ArrayLength(kPairs); i++) { |
michael@0 | 439 | nsAString::const_iterator start(listStart), end(listEnd); |
michael@0 | 440 | if (!FindInReadable(NS_ConvertASCIItoUTF16(kPairs[i].name), start, end)) { |
michael@0 | 441 | continue; |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | if ((start != listStart && !NS_IsAsciiWhitespace(*(--start))) || |
michael@0 | 445 | (end != listEnd && !NS_IsAsciiWhitespace(*(end)))) { |
michael@0 | 446 | continue; |
michael@0 | 447 | } |
michael@0 | 448 | modifiers |= kPairs[i].modifier; |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | return modifiers; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | bool |
michael@0 | 455 | UIEvent::GetModifierStateInternal(const nsAString& aKey) |
michael@0 | 456 | { |
michael@0 | 457 | WidgetInputEvent* inputEvent = mEvent->AsInputEvent(); |
michael@0 | 458 | MOZ_ASSERT(inputEvent, "mEvent must be WidgetInputEvent or derived class"); |
michael@0 | 459 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_SHIFT)) { |
michael@0 | 460 | return inputEvent->IsShift(); |
michael@0 | 461 | } |
michael@0 | 462 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_CONTROL)) { |
michael@0 | 463 | return inputEvent->IsControl(); |
michael@0 | 464 | } |
michael@0 | 465 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_META)) { |
michael@0 | 466 | return inputEvent->IsMeta(); |
michael@0 | 467 | } |
michael@0 | 468 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_ALT)) { |
michael@0 | 469 | return inputEvent->IsAlt(); |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_ALTGRAPH)) { |
michael@0 | 473 | return inputEvent->IsAltGraph(); |
michael@0 | 474 | } |
michael@0 | 475 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_OS)) { |
michael@0 | 476 | return inputEvent->IsOS(); |
michael@0 | 477 | } |
michael@0 | 478 | |
michael@0 | 479 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_CAPSLOCK)) { |
michael@0 | 480 | return inputEvent->IsCapsLocked(); |
michael@0 | 481 | } |
michael@0 | 482 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_NUMLOCK)) { |
michael@0 | 483 | return inputEvent->IsNumLocked(); |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_FN)) { |
michael@0 | 487 | return inputEvent->IsFn(); |
michael@0 | 488 | } |
michael@0 | 489 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_SCROLLLOCK)) { |
michael@0 | 490 | return inputEvent->IsScrollLocked(); |
michael@0 | 491 | } |
michael@0 | 492 | if (aKey.EqualsLiteral(NS_DOM_KEYNAME_SYMBOLLOCK)) { |
michael@0 | 493 | return inputEvent->IsSymbolLocked(); |
michael@0 | 494 | } |
michael@0 | 495 | return false; |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | } // namespace dom |
michael@0 | 499 | } // namespace mozilla |
michael@0 | 500 | |
michael@0 | 501 | using namespace mozilla; |
michael@0 | 502 | using namespace mozilla::dom; |
michael@0 | 503 | |
michael@0 | 504 | nsresult |
michael@0 | 505 | NS_NewDOMUIEvent(nsIDOMEvent** aInstancePtrResult, |
michael@0 | 506 | EventTarget* aOwner, |
michael@0 | 507 | nsPresContext* aPresContext, |
michael@0 | 508 | WidgetGUIEvent* aEvent) |
michael@0 | 509 | { |
michael@0 | 510 | UIEvent* it = new UIEvent(aOwner, aPresContext, aEvent); |
michael@0 | 511 | NS_ADDREF(it); |
michael@0 | 512 | *aInstancePtrResult = static_cast<Event*>(it); |
michael@0 | 513 | return NS_OK; |
michael@0 | 514 | } |