dom/events/MouseEvent.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/MouseEvent.h"
michael@0 7 #include "mozilla/MouseEvents.h"
michael@0 8 #include "nsContentUtils.h"
michael@0 9 #include "nsIContent.h"
michael@0 10 #include "prtime.h"
michael@0 11
michael@0 12 namespace mozilla {
michael@0 13 namespace dom {
michael@0 14
michael@0 15 MouseEvent::MouseEvent(EventTarget* aOwner,
michael@0 16 nsPresContext* aPresContext,
michael@0 17 WidgetMouseEventBase* aEvent)
michael@0 18 : UIEvent(aOwner, aPresContext,
michael@0 19 aEvent ? aEvent : new WidgetMouseEvent(false, 0, nullptr,
michael@0 20 WidgetMouseEvent::eReal))
michael@0 21 {
michael@0 22 // There's no way to make this class' ctor allocate an WidgetMouseScrollEvent.
michael@0 23 // It's not that important, though, since a scroll event is not a real
michael@0 24 // DOM event.
michael@0 25
michael@0 26 WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
michael@0 27 if (aEvent) {
michael@0 28 mEventIsInternal = false;
michael@0 29 }
michael@0 30 else {
michael@0 31 mEventIsInternal = true;
michael@0 32 mEvent->time = PR_Now();
michael@0 33 mEvent->refPoint.x = mEvent->refPoint.y = 0;
michael@0 34 mouseEvent->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
michael@0 35 }
michael@0 36
michael@0 37 if (mouseEvent) {
michael@0 38 MOZ_ASSERT(mouseEvent->reason != WidgetMouseEvent::eSynthesized,
michael@0 39 "Don't dispatch DOM events from synthesized mouse events");
michael@0 40 mDetail = mouseEvent->clickCount;
michael@0 41 }
michael@0 42 }
michael@0 43
michael@0 44 NS_IMPL_ADDREF_INHERITED(MouseEvent, UIEvent)
michael@0 45 NS_IMPL_RELEASE_INHERITED(MouseEvent, UIEvent)
michael@0 46
michael@0 47 NS_INTERFACE_MAP_BEGIN(MouseEvent)
michael@0 48 NS_INTERFACE_MAP_ENTRY(nsIDOMMouseEvent)
michael@0 49 NS_INTERFACE_MAP_END_INHERITING(UIEvent)
michael@0 50
michael@0 51 NS_IMETHODIMP
michael@0 52 MouseEvent::InitMouseEvent(const nsAString& aType,
michael@0 53 bool aCanBubble,
michael@0 54 bool aCancelable,
michael@0 55 nsIDOMWindow* aView,
michael@0 56 int32_t aDetail,
michael@0 57 int32_t aScreenX,
michael@0 58 int32_t aScreenY,
michael@0 59 int32_t aClientX,
michael@0 60 int32_t aClientY,
michael@0 61 bool aCtrlKey,
michael@0 62 bool aAltKey,
michael@0 63 bool aShiftKey,
michael@0 64 bool aMetaKey,
michael@0 65 uint16_t aButton,
michael@0 66 nsIDOMEventTarget* aRelatedTarget)
michael@0 67 {
michael@0 68 nsresult rv =
michael@0 69 UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, aDetail);
michael@0 70 NS_ENSURE_SUCCESS(rv, rv);
michael@0 71
michael@0 72 switch(mEvent->eventStructType) {
michael@0 73 case NS_MOUSE_EVENT:
michael@0 74 case NS_MOUSE_SCROLL_EVENT:
michael@0 75 case NS_WHEEL_EVENT:
michael@0 76 case NS_DRAG_EVENT:
michael@0 77 case NS_POINTER_EVENT:
michael@0 78 case NS_SIMPLE_GESTURE_EVENT: {
michael@0 79 WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
michael@0 80 mouseEventBase->relatedTarget = aRelatedTarget;
michael@0 81 mouseEventBase->button = aButton;
michael@0 82 mouseEventBase->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
michael@0 83 mClientPoint.x = aClientX;
michael@0 84 mClientPoint.y = aClientY;
michael@0 85 mouseEventBase->refPoint.x = aScreenX;
michael@0 86 mouseEventBase->refPoint.y = aScreenY;
michael@0 87
michael@0 88 WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
michael@0 89 if (mouseEvent) {
michael@0 90 mouseEvent->clickCount = aDetail;
michael@0 91 }
michael@0 92 break;
michael@0 93 }
michael@0 94 default:
michael@0 95 break;
michael@0 96 }
michael@0 97
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100
michael@0 101 nsresult
michael@0 102 MouseEvent::InitMouseEvent(const nsAString& aType,
michael@0 103 bool aCanBubble,
michael@0 104 bool aCancelable,
michael@0 105 nsIDOMWindow* aView,
michael@0 106 int32_t aDetail,
michael@0 107 int32_t aScreenX,
michael@0 108 int32_t aScreenY,
michael@0 109 int32_t aClientX,
michael@0 110 int32_t aClientY,
michael@0 111 int16_t aButton,
michael@0 112 nsIDOMEventTarget* aRelatedTarget,
michael@0 113 const nsAString& aModifiersList)
michael@0 114 {
michael@0 115 Modifiers modifiers = ComputeModifierState(aModifiersList);
michael@0 116
michael@0 117 nsresult rv = InitMouseEvent(aType, aCanBubble, aCancelable, aView,
michael@0 118 aDetail, aScreenX, aScreenY, aClientX, aClientY,
michael@0 119 (modifiers & MODIFIER_CONTROL) != 0,
michael@0 120 (modifiers & MODIFIER_ALT) != 0,
michael@0 121 (modifiers & MODIFIER_SHIFT) != 0,
michael@0 122 (modifiers & MODIFIER_META) != 0,
michael@0 123 aButton, aRelatedTarget);
michael@0 124 NS_ENSURE_SUCCESS(rv, rv);
michael@0 125
michael@0 126 switch(mEvent->eventStructType) {
michael@0 127 case NS_MOUSE_EVENT:
michael@0 128 case NS_MOUSE_SCROLL_EVENT:
michael@0 129 case NS_WHEEL_EVENT:
michael@0 130 case NS_DRAG_EVENT:
michael@0 131 case NS_POINTER_EVENT:
michael@0 132 case NS_SIMPLE_GESTURE_EVENT:
michael@0 133 mEvent->AsInputEvent()->modifiers = modifiers;
michael@0 134 return NS_OK;
michael@0 135 default:
michael@0 136 MOZ_CRASH("There is no space to store the modifiers");
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140 already_AddRefed<MouseEvent>
michael@0 141 MouseEvent::Constructor(const GlobalObject& aGlobal,
michael@0 142 const nsAString& aType,
michael@0 143 const MouseEventInit& aParam,
michael@0 144 ErrorResult& aRv)
michael@0 145 {
michael@0 146 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
michael@0 147 nsRefPtr<MouseEvent> e = new MouseEvent(t, nullptr, nullptr);
michael@0 148 bool trusted = e->Init(t);
michael@0 149 e->InitMouseEvent(aType, aParam.mBubbles, aParam.mCancelable,
michael@0 150 aParam.mView, aParam.mDetail, aParam.mScreenX,
michael@0 151 aParam.mScreenY, aParam.mClientX, aParam.mClientY,
michael@0 152 aParam.mCtrlKey, aParam.mAltKey, aParam.mShiftKey,
michael@0 153 aParam.mMetaKey, aParam.mButton, aParam.mRelatedTarget,
michael@0 154 aRv);
michael@0 155 e->SetTrusted(trusted);
michael@0 156
michael@0 157 switch (e->mEvent->eventStructType) {
michael@0 158 case NS_MOUSE_EVENT:
michael@0 159 case NS_MOUSE_SCROLL_EVENT:
michael@0 160 case NS_WHEEL_EVENT:
michael@0 161 case NS_DRAG_EVENT:
michael@0 162 case NS_POINTER_EVENT:
michael@0 163 case NS_SIMPLE_GESTURE_EVENT:
michael@0 164 e->mEvent->AsMouseEventBase()->buttons = aParam.mButtons;
michael@0 165 break;
michael@0 166 default:
michael@0 167 break;
michael@0 168 }
michael@0 169
michael@0 170 return e.forget();
michael@0 171 }
michael@0 172
michael@0 173 NS_IMETHODIMP
michael@0 174 MouseEvent::InitNSMouseEvent(const nsAString& aType,
michael@0 175 bool aCanBubble,
michael@0 176 bool aCancelable,
michael@0 177 nsIDOMWindow* aView,
michael@0 178 int32_t aDetail,
michael@0 179 int32_t aScreenX,
michael@0 180 int32_t aScreenY,
michael@0 181 int32_t aClientX,
michael@0 182 int32_t aClientY,
michael@0 183 bool aCtrlKey,
michael@0 184 bool aAltKey,
michael@0 185 bool aShiftKey,
michael@0 186 bool aMetaKey,
michael@0 187 uint16_t aButton,
michael@0 188 nsIDOMEventTarget* aRelatedTarget,
michael@0 189 float aPressure,
michael@0 190 uint16_t aInputSource)
michael@0 191 {
michael@0 192 nsresult rv = MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
michael@0 193 aView, aDetail, aScreenX, aScreenY,
michael@0 194 aClientX, aClientY,
michael@0 195 aCtrlKey, aAltKey, aShiftKey,
michael@0 196 aMetaKey, aButton, aRelatedTarget);
michael@0 197 NS_ENSURE_SUCCESS(rv, rv);
michael@0 198
michael@0 199 WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
michael@0 200 mouseEventBase->pressure = aPressure;
michael@0 201 mouseEventBase->inputSource = aInputSource;
michael@0 202 return NS_OK;
michael@0 203 }
michael@0 204
michael@0 205 NS_IMETHODIMP
michael@0 206 MouseEvent::GetButton(int16_t* aButton)
michael@0 207 {
michael@0 208 NS_ENSURE_ARG_POINTER(aButton);
michael@0 209 *aButton = Button();
michael@0 210 return NS_OK;
michael@0 211 }
michael@0 212
michael@0 213 int16_t
michael@0 214 MouseEvent::Button()
michael@0 215 {
michael@0 216 switch(mEvent->eventStructType)
michael@0 217 {
michael@0 218 case NS_MOUSE_EVENT:
michael@0 219 case NS_MOUSE_SCROLL_EVENT:
michael@0 220 case NS_WHEEL_EVENT:
michael@0 221 case NS_DRAG_EVENT:
michael@0 222 case NS_POINTER_EVENT:
michael@0 223 case NS_SIMPLE_GESTURE_EVENT:
michael@0 224 return mEvent->AsMouseEventBase()->button;
michael@0 225 default:
michael@0 226 NS_WARNING("Tried to get mouse button for non-mouse event!");
michael@0 227 return WidgetMouseEvent::eLeftButton;
michael@0 228 }
michael@0 229 }
michael@0 230
michael@0 231 NS_IMETHODIMP
michael@0 232 MouseEvent::GetButtons(uint16_t* aButtons)
michael@0 233 {
michael@0 234 NS_ENSURE_ARG_POINTER(aButtons);
michael@0 235 *aButtons = Buttons();
michael@0 236 return NS_OK;
michael@0 237 }
michael@0 238
michael@0 239 uint16_t
michael@0 240 MouseEvent::Buttons()
michael@0 241 {
michael@0 242 switch(mEvent->eventStructType)
michael@0 243 {
michael@0 244 case NS_MOUSE_EVENT:
michael@0 245 case NS_MOUSE_SCROLL_EVENT:
michael@0 246 case NS_WHEEL_EVENT:
michael@0 247 case NS_DRAG_EVENT:
michael@0 248 case NS_POINTER_EVENT:
michael@0 249 case NS_SIMPLE_GESTURE_EVENT:
michael@0 250 return mEvent->AsMouseEventBase()->buttons;
michael@0 251 default:
michael@0 252 MOZ_CRASH("Tried to get mouse buttons for non-mouse event!");
michael@0 253 }
michael@0 254 }
michael@0 255
michael@0 256 NS_IMETHODIMP
michael@0 257 MouseEvent::GetRelatedTarget(nsIDOMEventTarget** aRelatedTarget)
michael@0 258 {
michael@0 259 NS_ENSURE_ARG_POINTER(aRelatedTarget);
michael@0 260 *aRelatedTarget = GetRelatedTarget().take();
michael@0 261 return NS_OK;
michael@0 262 }
michael@0 263
michael@0 264 already_AddRefed<EventTarget>
michael@0 265 MouseEvent::GetRelatedTarget()
michael@0 266 {
michael@0 267 nsCOMPtr<EventTarget> relatedTarget;
michael@0 268 switch(mEvent->eventStructType)
michael@0 269 {
michael@0 270 case NS_MOUSE_EVENT:
michael@0 271 case NS_MOUSE_SCROLL_EVENT:
michael@0 272 case NS_WHEEL_EVENT:
michael@0 273 case NS_DRAG_EVENT:
michael@0 274 case NS_POINTER_EVENT:
michael@0 275 case NS_SIMPLE_GESTURE_EVENT:
michael@0 276 relatedTarget =
michael@0 277 do_QueryInterface(mEvent->AsMouseEventBase()->relatedTarget);
michael@0 278 break;
michael@0 279 default:
michael@0 280 break;
michael@0 281 }
michael@0 282
michael@0 283 if (relatedTarget) {
michael@0 284 nsCOMPtr<nsIContent> content = do_QueryInterface(relatedTarget);
michael@0 285 if (content && content->ChromeOnlyAccess() &&
michael@0 286 !nsContentUtils::CanAccessNativeAnon()) {
michael@0 287 relatedTarget = do_QueryInterface(content->FindFirstNonChromeOnlyAccessContent());
michael@0 288 }
michael@0 289
michael@0 290 if (relatedTarget) {
michael@0 291 relatedTarget = relatedTarget->GetTargetForDOMEvent();
michael@0 292 }
michael@0 293 return relatedTarget.forget();
michael@0 294 }
michael@0 295 return nullptr;
michael@0 296 }
michael@0 297
michael@0 298 NS_IMETHODIMP
michael@0 299 MouseEvent::GetMozMovementX(int32_t* aMovementX)
michael@0 300 {
michael@0 301 NS_ENSURE_ARG_POINTER(aMovementX);
michael@0 302 *aMovementX = MozMovementX();
michael@0 303
michael@0 304 return NS_OK;
michael@0 305 }
michael@0 306
michael@0 307 NS_IMETHODIMP
michael@0 308 MouseEvent::GetMozMovementY(int32_t* aMovementY)
michael@0 309 {
michael@0 310 NS_ENSURE_ARG_POINTER(aMovementY);
michael@0 311 *aMovementY = MozMovementY();
michael@0 312
michael@0 313 return NS_OK;
michael@0 314 }
michael@0 315
michael@0 316 NS_IMETHODIMP
michael@0 317 MouseEvent::GetScreenX(int32_t* aScreenX)
michael@0 318 {
michael@0 319 NS_ENSURE_ARG_POINTER(aScreenX);
michael@0 320 *aScreenX = ScreenX();
michael@0 321 return NS_OK;
michael@0 322 }
michael@0 323
michael@0 324 int32_t
michael@0 325 MouseEvent::ScreenX()
michael@0 326 {
michael@0 327 return Event::GetScreenCoords(mPresContext, mEvent, mEvent->refPoint).x;
michael@0 328 }
michael@0 329
michael@0 330 NS_IMETHODIMP
michael@0 331 MouseEvent::GetScreenY(int32_t* aScreenY)
michael@0 332 {
michael@0 333 NS_ENSURE_ARG_POINTER(aScreenY);
michael@0 334 *aScreenY = ScreenY();
michael@0 335 return NS_OK;
michael@0 336 }
michael@0 337
michael@0 338 int32_t
michael@0 339 MouseEvent::ScreenY()
michael@0 340 {
michael@0 341 return Event::GetScreenCoords(mPresContext, mEvent, mEvent->refPoint).y;
michael@0 342 }
michael@0 343
michael@0 344
michael@0 345 NS_IMETHODIMP
michael@0 346 MouseEvent::GetClientX(int32_t* aClientX)
michael@0 347 {
michael@0 348 NS_ENSURE_ARG_POINTER(aClientX);
michael@0 349 *aClientX = ClientX();
michael@0 350 return NS_OK;
michael@0 351 }
michael@0 352
michael@0 353 int32_t
michael@0 354 MouseEvent::ClientX()
michael@0 355 {
michael@0 356 return Event::GetClientCoords(mPresContext, mEvent, mEvent->refPoint,
michael@0 357 mClientPoint).x;
michael@0 358 }
michael@0 359
michael@0 360 NS_IMETHODIMP
michael@0 361 MouseEvent::GetClientY(int32_t* aClientY)
michael@0 362 {
michael@0 363 NS_ENSURE_ARG_POINTER(aClientY);
michael@0 364 *aClientY = ClientY();
michael@0 365 return NS_OK;
michael@0 366 }
michael@0 367
michael@0 368 int32_t
michael@0 369 MouseEvent::ClientY()
michael@0 370 {
michael@0 371 return Event::GetClientCoords(mPresContext, mEvent, mEvent->refPoint,
michael@0 372 mClientPoint).y;
michael@0 373 }
michael@0 374
michael@0 375 bool
michael@0 376 MouseEvent::AltKey()
michael@0 377 {
michael@0 378 return mEvent->AsInputEvent()->IsAlt();
michael@0 379 }
michael@0 380
michael@0 381 NS_IMETHODIMP
michael@0 382 MouseEvent::GetAltKey(bool* aIsDown)
michael@0 383 {
michael@0 384 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 385 *aIsDown = AltKey();
michael@0 386 return NS_OK;
michael@0 387 }
michael@0 388
michael@0 389 bool
michael@0 390 MouseEvent::CtrlKey()
michael@0 391 {
michael@0 392 return mEvent->AsInputEvent()->IsControl();
michael@0 393 }
michael@0 394
michael@0 395 NS_IMETHODIMP
michael@0 396 MouseEvent::GetCtrlKey(bool* aIsDown)
michael@0 397 {
michael@0 398 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 399 *aIsDown = CtrlKey();
michael@0 400 return NS_OK;
michael@0 401 }
michael@0 402
michael@0 403 bool
michael@0 404 MouseEvent::ShiftKey()
michael@0 405 {
michael@0 406 return mEvent->AsInputEvent()->IsShift();
michael@0 407 }
michael@0 408
michael@0 409 NS_IMETHODIMP
michael@0 410 MouseEvent::GetShiftKey(bool* aIsDown)
michael@0 411 {
michael@0 412 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 413 *aIsDown = ShiftKey();
michael@0 414 return NS_OK;
michael@0 415 }
michael@0 416
michael@0 417 bool
michael@0 418 MouseEvent::MetaKey()
michael@0 419 {
michael@0 420 return mEvent->AsInputEvent()->IsMeta();
michael@0 421 }
michael@0 422
michael@0 423 NS_IMETHODIMP
michael@0 424 MouseEvent::GetMetaKey(bool* aIsDown)
michael@0 425 {
michael@0 426 NS_ENSURE_ARG_POINTER(aIsDown);
michael@0 427 *aIsDown = MetaKey();
michael@0 428 return NS_OK;
michael@0 429 }
michael@0 430
michael@0 431 NS_IMETHODIMP
michael@0 432 MouseEvent::GetModifierState(const nsAString& aKey,
michael@0 433 bool* aState)
michael@0 434 {
michael@0 435 NS_ENSURE_ARG_POINTER(aState);
michael@0 436
michael@0 437 *aState = GetModifierState(aKey);
michael@0 438 return NS_OK;
michael@0 439 }
michael@0 440
michael@0 441 float
michael@0 442 MouseEvent::MozPressure() const
michael@0 443 {
michael@0 444 return mEvent->AsMouseEventBase()->pressure;
michael@0 445 }
michael@0 446
michael@0 447 NS_IMETHODIMP
michael@0 448 MouseEvent::GetMozPressure(float* aPressure)
michael@0 449 {
michael@0 450 NS_ENSURE_ARG_POINTER(aPressure);
michael@0 451 *aPressure = MozPressure();
michael@0 452 return NS_OK;
michael@0 453 }
michael@0 454
michael@0 455 uint16_t
michael@0 456 MouseEvent::MozInputSource() const
michael@0 457 {
michael@0 458 return mEvent->AsMouseEventBase()->inputSource;
michael@0 459 }
michael@0 460
michael@0 461 NS_IMETHODIMP
michael@0 462 MouseEvent::GetMozInputSource(uint16_t* aInputSource)
michael@0 463 {
michael@0 464 NS_ENSURE_ARG_POINTER(aInputSource);
michael@0 465 *aInputSource = MozInputSource();
michael@0 466 return NS_OK;
michael@0 467 }
michael@0 468
michael@0 469 } // namespace dom
michael@0 470 } // namespace mozilla
michael@0 471
michael@0 472 using namespace mozilla;
michael@0 473 using namespace mozilla::dom;
michael@0 474
michael@0 475 nsresult
michael@0 476 NS_NewDOMMouseEvent(nsIDOMEvent** aInstancePtrResult,
michael@0 477 EventTarget* aOwner,
michael@0 478 nsPresContext* aPresContext,
michael@0 479 WidgetMouseEvent* aEvent)
michael@0 480 {
michael@0 481 MouseEvent* it = new MouseEvent(aOwner, aPresContext, aEvent);
michael@0 482 NS_ADDREF(it);
michael@0 483 *aInstancePtrResult = static_cast<Event*>(it);
michael@0 484 return NS_OK;
michael@0 485 }

mercurial