1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
/*
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
* Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
* Copyright (C) 2003, 2005, 2006, 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "core/events/MouseEvent.h"
#include "bindings/core/v8/DOMWrapperWorld.h"
#include "bindings/core/v8/ScriptState.h"
#include "core/dom/Element.h"
#include "core/events/EventDispatcher.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/input/InputDeviceCapabilities.h"
#include "core/layout/LayoutObject.h"
#include "core/paint/PaintLayer.h"
#include "core/svg/SVGElement.h"
#include "platform/PlatformMouseEvent.h"
#include "public/platform/WebPointerProperties.h"
namespace blink {
namespace {
LayoutSize contentsScrollOffset(AbstractView* abstractView) {
if (!abstractView || !abstractView->isLocalDOMWindow())
return LayoutSize();
LocalFrame* frame = toLocalDOMWindow(abstractView)->frame();
if (!frame)
return LayoutSize();
FrameView* frameView = frame->view();
if (!frameView)
return LayoutSize();
float scaleFactor = frame->pageZoomFactor();
return LayoutSize(frameView->scrollX() / scaleFactor,
frameView->scrollY() / scaleFactor);
}
float pageZoomFactor(const UIEvent* event) {
if (!event->view() || !event->view()->isLocalDOMWindow())
return 1;
LocalFrame* frame = toLocalDOMWindow(event->view())->frame();
if (!frame)
return 1;
return frame->pageZoomFactor();
}
const LayoutObject* findTargetLayoutObject(Node*& targetNode) {
LayoutObject* layoutObject = targetNode->layoutObject();
if (!layoutObject || !layoutObject->isSVG())
return layoutObject;
// If this is an SVG node, compute the offset to the padding box of the
// outermost SVG root (== the closest ancestor that has a CSS layout box.)
while (!layoutObject->isSVGRoot())
layoutObject = layoutObject->parent();
// Update the target node to point to the SVG root.
targetNode = layoutObject->node();
DCHECK(!targetNode || (targetNode->isSVGElement() &&
toSVGElement(*targetNode).isOutermostSVGSVGElement()));
return layoutObject;
}
} // namespace
MouseEvent* MouseEvent::create(ScriptState* scriptState,
const AtomicString& type,
const MouseEventInit& initializer) {
if (scriptState && scriptState->world().isIsolatedWorld())
UIEventWithKeyState::didCreateEventInIsolatedWorld(
initializer.ctrlKey(), initializer.altKey(), initializer.shiftKey(),
initializer.metaKey());
return new MouseEvent(type, initializer);
}
MouseEvent* MouseEvent::create(const AtomicString& eventType,
AbstractView* view,
const PlatformMouseEvent& event,
int detail,
Node* relatedTarget) {
bool isMouseEnterOrLeave = eventType == EventTypeNames::mouseenter ||
eventType == EventTypeNames::mouseleave;
bool isCancelable = !isMouseEnterOrLeave;
bool isBubbling = !isMouseEnterOrLeave;
return MouseEvent::create(
eventType, isBubbling, isCancelable, view, detail,
event.globalPosition().x(), event.globalPosition().y(),
event.position().x(), event.position().y(), event.movementDelta().x(),
event.movementDelta().y(), event.getModifiers(),
static_cast<short>(event.pointerProperties().button),
platformModifiersToButtons(event.getModifiers()), relatedTarget,
event.timestamp(), event.getSyntheticEventType(), event.region(), &event);
}
MouseEvent* MouseEvent::create(
const AtomicString& type,
bool canBubble,
bool cancelable,
AbstractView* view,
int detail,
int screenX,
int screenY,
int windowX,
int windowY,
int movementX,
int movementY,
PlatformEvent::Modifiers modifiers,
short button,
unsigned short buttons,
EventTarget* relatedTarget,
TimeTicks platformTimeStamp,
PlatformMouseEvent::SyntheticEventType syntheticEventType,
const String& region,
const PlatformMouseEvent* mouseEvent) {
return new MouseEvent(
type, canBubble, cancelable, view, detail, screenX, screenY, windowX,
windowY, movementX, movementY, modifiers, button, buttons, relatedTarget,
platformTimeStamp, syntheticEventType, region, mouseEvent);
}
MouseEvent* MouseEvent::create(const AtomicString& eventType,
AbstractView* view,
Event* underlyingEvent,
SimulatedClickCreationScope creationScope) {
PlatformEvent::Modifiers modifiers = PlatformEvent::NoModifiers;
if (UIEventWithKeyState* keyStateEvent =
findEventWithKeyState(underlyingEvent)) {
modifiers = keyStateEvent->modifiers();
}
PlatformMouseEvent::SyntheticEventType syntheticType =
PlatformMouseEvent::Positionless;
int screenX = 0;
int screenY = 0;
if (underlyingEvent && underlyingEvent->isMouseEvent()) {
syntheticType = PlatformMouseEvent::RealOrIndistinguishable;
MouseEvent* mouseEvent = toMouseEvent(underlyingEvent);
screenX = mouseEvent->screenX();
screenY = mouseEvent->screenY();
}
TimeTicks timestamp =
underlyingEvent ? underlyingEvent->platformTimeStamp() : TimeTicks::Now();
MouseEvent* createdEvent = MouseEvent::create(
eventType, true, true, view, 0, screenX, screenY, 0, 0, 0, 0, modifiers,
0, 0, nullptr, timestamp, syntheticType, String(), nullptr);
createdEvent->setTrusted(creationScope ==
SimulatedClickCreationScope::FromUserAgent);
createdEvent->setUnderlyingEvent(underlyingEvent);
if (syntheticType == PlatformMouseEvent::RealOrIndistinguishable) {
MouseEvent* mouseEvent = toMouseEvent(createdEvent->underlyingEvent());
createdEvent->initCoordinates(mouseEvent->clientX(), mouseEvent->clientY());
}
return createdEvent;
}
MouseEvent::MouseEvent()
: m_positionType(PositionType::Position),
m_hasCachedRelativePosition(false),
m_button(0),
m_buttons(0),
m_relatedTarget(nullptr),
m_syntheticEventType(PlatformMouseEvent::RealOrIndistinguishable) {}
MouseEvent::MouseEvent(
const AtomicString& eventType,
bool canBubble,
bool cancelable,
AbstractView* abstractView,
PlatformMouseEvent::SyntheticEventType syntheticEventType,
const String& region,
const WebMouseEvent& event)
: UIEventWithKeyState(
eventType,
canBubble,
cancelable,
abstractView,
0,
static_cast<PlatformEvent::Modifiers>(event.modifiers()),
TimeTicks::FromSeconds(event.timeStampSeconds()),
abstractView
? abstractView->getInputDeviceCapabilities()->firesTouchEvents(
syntheticEventType == PlatformMouseEvent::FromTouch)
: nullptr),
m_screenLocation(event.globalX, event.globalY),
m_movementDelta(flooredIntPoint(event.movementInRootFrame())),
m_positionType(syntheticEventType == PlatformMouseEvent::Positionless
? PositionType::Positionless
: PositionType::Position),
m_button(0),
m_buttons(platformModifiersToButtons(event.modifiers())),
m_syntheticEventType(syntheticEventType),
m_region(region) {
IntPoint rootFrameCoordinates = flooredIntPoint(event.positionInRootFrame());
initCoordinatesFromRootFrame(rootFrameCoordinates.x(),
rootFrameCoordinates.y());
}
MouseEvent::MouseEvent(
const AtomicString& eventType,
bool canBubble,
bool cancelable,
AbstractView* abstractView,
int detail,
int screenX,
int screenY,
int windowX,
int windowY,
int movementX,
int movementY,
PlatformEvent::Modifiers modifiers,
short button,
unsigned short buttons,
EventTarget* relatedTarget,
TimeTicks platformTimeStamp,
PlatformMouseEvent::SyntheticEventType syntheticEventType,
const String& region,
const PlatformMouseEvent* mouseEvent)
: UIEventWithKeyState(
eventType,
canBubble,
cancelable,
abstractView,
detail,
modifiers,
platformTimeStamp,
abstractView
? abstractView->getInputDeviceCapabilities()->firesTouchEvents(
syntheticEventType == PlatformMouseEvent::FromTouch)
: nullptr),
m_screenLocation(screenX, screenY),
m_movementDelta(movementX, movementY),
m_positionType(syntheticEventType == PlatformMouseEvent::Positionless
? PositionType::Positionless
: PositionType::Position),
m_button(button),
m_buttons(buttons),
m_relatedTarget(relatedTarget),
m_syntheticEventType(syntheticEventType),
m_region(region) {
if (mouseEvent)
m_mouseEvent.reset(new PlatformMouseEvent(*mouseEvent));
initCoordinatesFromRootFrame(windowX, windowY);
}
MouseEvent::MouseEvent(const AtomicString& eventType,
const MouseEventInit& initializer)
: UIEventWithKeyState(eventType, initializer),
m_screenLocation(
DoublePoint(initializer.screenX(), initializer.screenY())),
m_movementDelta(
IntPoint(initializer.movementX(), initializer.movementY())),
m_positionType(PositionType::Position),
m_button(initializer.button()),
m_buttons(initializer.buttons()),
m_relatedTarget(initializer.relatedTarget()),
m_syntheticEventType(PlatformMouseEvent::RealOrIndistinguishable),
m_region(initializer.region()) {
initCoordinates(initializer.clientX(), initializer.clientY());
}
void MouseEvent::initCoordinates(const double clientX, const double clientY) {
// Set up initial values for coordinates.
// Correct values are computed lazily, see computeRelativePosition.
m_clientLocation = DoublePoint(clientX, clientY);
m_pageLocation = m_clientLocation + DoubleSize(contentsScrollOffset(view()));
m_layerLocation = m_pageLocation;
m_offsetLocation = m_pageLocation;
computePageLocation();
m_hasCachedRelativePosition = false;
}
void MouseEvent::initCoordinatesFromRootFrame(int windowX, int windowY) {
DoublePoint adjustedPageLocation;
DoubleSize scrollOffset;
LocalFrame* frame = view() && view()->isLocalDOMWindow()
? toLocalDOMWindow(view())->frame()
: nullptr;
if (frame && hasPosition()) {
if (FrameView* frameView = frame->view()) {
adjustedPageLocation =
frameView->rootFrameToContents(IntPoint(windowX, windowY));
scrollOffset = frameView->scrollOffsetInt();
float scaleFactor = 1 / frame->pageZoomFactor();
if (scaleFactor != 1.0f) {
adjustedPageLocation.scale(scaleFactor, scaleFactor);
scrollOffset.scale(scaleFactor, scaleFactor);
}
}
}
m_clientLocation = adjustedPageLocation - scrollOffset;
m_pageLocation = adjustedPageLocation;
// Set up initial values for coordinates.
// Correct values are computed lazily, see computeRelativePosition.
m_layerLocation = m_pageLocation;
m_offsetLocation = m_pageLocation;
computePageLocation();
m_hasCachedRelativePosition = false;
}
MouseEvent::~MouseEvent() {}
unsigned short MouseEvent::platformModifiersToButtons(unsigned modifiers) {
unsigned short buttons = 0;
if (modifiers & PlatformEvent::LeftButtonDown)
buttons |= static_cast<unsigned short>(WebPointerProperties::Buttons::Left);
if (modifiers & PlatformEvent::RightButtonDown)
buttons |=
static_cast<unsigned short>(WebPointerProperties::Buttons::Right);
if (modifiers & PlatformEvent::MiddleButtonDown)
buttons |=
static_cast<unsigned short>(WebPointerProperties::Buttons::Middle);
return buttons;
}
void MouseEvent::initMouseEvent(ScriptState* scriptState,
const AtomicString& type,
bool canBubble,
bool cancelable,
AbstractView* view,
int detail,
int screenX,
int screenY,
int clientX,
int clientY,
bool ctrlKey,
bool altKey,
bool shiftKey,
bool metaKey,
short button,
EventTarget* relatedTarget,
unsigned short buttons) {
if (isBeingDispatched())
return;
if (scriptState && scriptState->world().isIsolatedWorld())
UIEventWithKeyState::didCreateEventInIsolatedWorld(ctrlKey, altKey,
shiftKey, metaKey);
initModifiers(ctrlKey, altKey, shiftKey, metaKey);
initMouseEventInternal(type, canBubble, cancelable, view, detail, screenX,
screenY, clientX, clientY, modifiers(), button,
relatedTarget, nullptr, buttons);
}
void MouseEvent::initMouseEventInternal(
const AtomicString& type,
bool canBubble,
bool cancelable,
AbstractView* view,
int detail,
int screenX,
int screenY,
int clientX,
int clientY,
PlatformEvent::Modifiers modifiers,
short button,
EventTarget* relatedTarget,
InputDeviceCapabilities* sourceCapabilities,
unsigned short buttons) {
initUIEventInternal(type, canBubble, cancelable, relatedTarget, view, detail,
sourceCapabilities);
m_screenLocation = IntPoint(screenX, screenY);
m_button = button;
m_buttons = buttons;
m_relatedTarget = relatedTarget;
m_modifiers = modifiers;
initCoordinates(clientX, clientY);
// FIXME: SyntheticEventType is not set to RealOrIndistinguishable here.
}
const AtomicString& MouseEvent::interfaceName() const {
return EventNames::MouseEvent;
}
bool MouseEvent::isMouseEvent() const {
return true;
}
int MouseEvent::which() const {
// For the DOM, the return values for left, middle and right mouse buttons are
// 0, 1, 2, respectively.
// For the Netscape "which" property, the return values for left, middle and
// right mouse buttons are 1, 2, 3, respectively.
// So we must add 1.
return m_button + 1;
}
Node* MouseEvent::toElement() const {
// MSIE extension - "the object toward which the user is moving the mouse
// pointer"
if (type() == EventTypeNames::mouseout ||
type() == EventTypeNames::mouseleave)
return relatedTarget() ? relatedTarget()->toNode() : nullptr;
return target() ? target()->toNode() : nullptr;
}
Node* MouseEvent::fromElement() const {
// MSIE extension - "object from which activation or the mouse pointer is
// exiting during the event" (huh?)
if (type() != EventTypeNames::mouseout &&
type() != EventTypeNames::mouseleave)
return relatedTarget() ? relatedTarget()->toNode() : nullptr;
return target() ? target()->toNode() : nullptr;
}
DEFINE_TRACE(MouseEvent) {
visitor->trace(m_relatedTarget);
UIEventWithKeyState::trace(visitor);
}
EventDispatchMediator* MouseEvent::createMediator() {
return MouseEventDispatchMediator::create(this);
}
MouseEventDispatchMediator* MouseEventDispatchMediator::create(
MouseEvent* mouseEvent) {
return new MouseEventDispatchMediator(mouseEvent);
}
MouseEventDispatchMediator::MouseEventDispatchMediator(MouseEvent* mouseEvent)
: EventDispatchMediator(mouseEvent) {}
MouseEvent& MouseEventDispatchMediator::event() const {
return toMouseEvent(EventDispatchMediator::event());
}
DispatchEventResult MouseEventDispatchMediator::dispatchEvent(
EventDispatcher& dispatcher) const {
MouseEvent& mouseEvent = event();
mouseEvent.eventPath().adjustForRelatedTarget(dispatcher.node(),
mouseEvent.relatedTarget());
if (!mouseEvent.isTrusted())
return dispatcher.dispatch();
if (isDisabledFormControl(&dispatcher.node()))
return DispatchEventResult::CanceledBeforeDispatch;
if (mouseEvent.type().isEmpty())
return DispatchEventResult::NotCanceled; // Shouldn't happen.
DCHECK(!mouseEvent.target() ||
mouseEvent.target() != mouseEvent.relatedTarget());
EventTarget* relatedTarget = mouseEvent.relatedTarget();
DispatchEventResult dispatchResult = dispatcher.dispatch();
if (mouseEvent.type() != EventTypeNames::click || mouseEvent.detail() != 2)
return dispatchResult;
// Special case: If it's a double click event, we also send the dblclick
// event. This is not part of the DOM specs, but is used for compatibility
// with the ondblclick="" attribute. This is treated as a separate event in
// other DOM-compliant browsers like Firefox, and so we do the same.
MouseEvent* doubleClickEvent = MouseEvent::create();
doubleClickEvent->initMouseEventInternal(
EventTypeNames::dblclick, mouseEvent.bubbles(), mouseEvent.cancelable(),
mouseEvent.view(), mouseEvent.detail(), mouseEvent.screenX(),
mouseEvent.screenY(), mouseEvent.clientX(), mouseEvent.clientY(),
mouseEvent.modifiers(), mouseEvent.button(), relatedTarget,
mouseEvent.sourceCapabilities(), mouseEvent.buttons());
doubleClickEvent->setComposed(mouseEvent.composed());
// Inherit the trusted status from the original event.
doubleClickEvent->setTrusted(mouseEvent.isTrusted());
if (mouseEvent.defaultHandled())
doubleClickEvent->setDefaultHandled();
DispatchEventResult doubleClickDispatchResult =
EventDispatcher::dispatchEvent(
dispatcher.node(),
MouseEventDispatchMediator::create(doubleClickEvent));
if (doubleClickDispatchResult != DispatchEventResult::NotCanceled)
return doubleClickDispatchResult;
return dispatchResult;
}
void MouseEvent::computePageLocation() {
float scaleFactor = pageZoomFactor(this);
m_absoluteLocation = m_pageLocation.scaledBy(scaleFactor);
}
void MouseEvent::receivedTarget() {
m_hasCachedRelativePosition = false;
}
void MouseEvent::computeRelativePosition() {
Node* targetNode = target() ? target()->toNode() : nullptr;
if (!targetNode)
return;
// Compute coordinates that are based on the target.
m_layerLocation = m_pageLocation;
m_offsetLocation = m_pageLocation;
// Must have an updated layout tree for this math to work correctly.
targetNode->document().updateStyleAndLayoutIgnorePendingStylesheets();
// Adjust offsetLocation to be relative to the target's padding box.
if (const LayoutObject* layoutObject = findTargetLayoutObject(targetNode)) {
FloatPoint localPos = layoutObject->absoluteToLocal(
FloatPoint(absoluteLocation()), UseTransforms);
// Adding this here to address crbug.com/570666. Basically we'd like to
// find the local coordinates relative to the padding box not the border
// box.
if (layoutObject->isBoxModelObject()) {
const LayoutBoxModelObject* layoutBox =
toLayoutBoxModelObject(layoutObject);
localPos.move(-layoutBox->borderLeft(), -layoutBox->borderTop());
}
m_offsetLocation = DoublePoint(localPos);
float scaleFactor = 1 / pageZoomFactor(this);
if (scaleFactor != 1.0f)
m_offsetLocation.scale(scaleFactor, scaleFactor);
}
// Adjust layerLocation to be relative to the layer.
// FIXME: event.layerX and event.layerY are poorly defined,
// and probably don't always correspond to PaintLayer offsets.
// https://bugs.webkit.org/show_bug.cgi?id=21868
Node* n = targetNode;
while (n && !n->layoutObject())
n = n->parentNode();
if (n) {
// FIXME: This logic is a wrong implementation of convertToLayerCoords.
for (PaintLayer* layer = n->layoutObject()->enclosingLayer(); layer;
layer = layer->parent()) {
m_layerLocation -= DoubleSize(layer->location().x().toDouble(),
layer->location().y().toDouble());
}
}
m_hasCachedRelativePosition = true;
}
int MouseEvent::layerX() {
if (!m_hasCachedRelativePosition)
computeRelativePosition();
// TODO(mustaq): Remove the PointerEvent specific code when mouse has
// fractional coordinates. See crbug.com/655786.
return isPointerEvent() ? m_layerLocation.x()
: static_cast<int>(m_layerLocation.x());
}
int MouseEvent::layerY() {
if (!m_hasCachedRelativePosition)
computeRelativePosition();
// TODO(mustaq): Remove the PointerEvent specific code when mouse has
// fractional coordinates. See crbug.com/655786.
return isPointerEvent() ? m_layerLocation.y()
: static_cast<int>(m_layerLocation.y());
}
int MouseEvent::offsetX() {
if (!hasPosition())
return 0;
if (!m_hasCachedRelativePosition)
computeRelativePosition();
return std::round(m_offsetLocation.x());
}
int MouseEvent::offsetY() {
if (!hasPosition())
return 0;
if (!m_hasCachedRelativePosition)
computeRelativePosition();
return std::round(m_offsetLocation.y());
}
} // namespace blink
|