gecko-dev/dom/events/MouseEvent.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

350 строки
11 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 15:12:37 +04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/MouseEvent.h"
#include "mozilla/MouseEvents.h"
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "prtime.h"
namespace mozilla::dom {
MouseEvent::MouseEvent(EventTarget* aOwner, nsPresContext* aPresContext,
WidgetMouseEventBase* aEvent)
: UIEvent(aOwner, aPresContext,
aEvent ? aEvent
: new WidgetMouseEvent(false, eVoidEvent, nullptr,
WidgetMouseEvent::eReal)) {
// There's no way to make this class' ctor allocate an WidgetMouseScrollEvent.
// It's not that important, though, since a scroll event is not a real
// DOM event.
WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
if (aEvent) {
mEventIsInternal = false;
} else {
mEventIsInternal = true;
mEvent->mTime = PR_Now();
mEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
mouseEvent->mInputSource = MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
}
if (mouseEvent) {
MOZ_ASSERT(mouseEvent->mReason != WidgetMouseEvent::eSynthesized,
"Don't dispatch DOM events from synthesized mouse events");
mDetail = mouseEvent->mClickCount;
}
}
void MouseEvent::InitMouseEvent(const nsAString& aType, bool aCanBubble,
bool aCancelable, nsGlobalWindowInner* aView,
int32_t aDetail, int32_t aScreenX,
int32_t aScreenY, int32_t aClientX,
int32_t aClientY, bool aCtrlKey, bool aAltKey,
bool aShiftKey, bool aMetaKey, uint16_t aButton,
EventTarget* aRelatedTarget) {
NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, aDetail);
switch (mEvent->mClass) {
case eMouseEventClass:
case eMouseScrollEventClass:
case eWheelEventClass:
case eDragEventClass:
case ePointerEventClass:
case eSimpleGestureEventClass: {
WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
mouseEventBase->mRelatedTarget = aRelatedTarget;
mouseEventBase->mButton = aButton;
mouseEventBase->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey,
aMetaKey);
mClientPoint.x = aClientX;
mClientPoint.y = aClientY;
mouseEventBase->mRefPoint.x = aScreenX;
mouseEventBase->mRefPoint.y = aScreenY;
WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
if (mouseEvent) {
mouseEvent->mClickCount = aDetail;
}
break;
}
default:
break;
}
}
void MouseEvent::InitMouseEvent(const nsAString& aType, bool aCanBubble,
bool aCancelable, nsGlobalWindowInner* aView,
int32_t aDetail, int32_t aScreenX,
int32_t aScreenY, int32_t aClientX,
int32_t aClientY, int16_t aButton,
EventTarget* aRelatedTarget,
const nsAString& aModifiersList) {
NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
Modifiers modifiers = ComputeModifierState(aModifiersList);
InitMouseEvent(
aType, aCanBubble, aCancelable, aView, aDetail, aScreenX, aScreenY,
aClientX, aClientY, (modifiers & MODIFIER_CONTROL) != 0,
(modifiers & MODIFIER_ALT) != 0, (modifiers & MODIFIER_SHIFT) != 0,
(modifiers & MODIFIER_META) != 0, aButton, aRelatedTarget);
switch (mEvent->mClass) {
case eMouseEventClass:
case eMouseScrollEventClass:
case eWheelEventClass:
case eDragEventClass:
case ePointerEventClass:
case eSimpleGestureEventClass:
mEvent->AsInputEvent()->mModifiers = modifiers;
return;
default:
MOZ_CRASH("There is no space to store the modifiers");
}
}
void MouseEvent::InitializeExtraMouseEventDictionaryMembers(
const MouseEventInit& aParam) {
InitModifiers(aParam);
mEvent->AsMouseEventBase()->mButtons = aParam.mButtons;
mMovementPoint.x = aParam.mMovementX;
mMovementPoint.y = aParam.mMovementY;
}
already_AddRefed<MouseEvent> MouseEvent::Constructor(
const GlobalObject& aGlobal, const nsAString& aType,
const MouseEventInit& aParam) {
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<MouseEvent> e = new MouseEvent(t, nullptr, nullptr);
bool trusted = e->Init(t);
e->InitMouseEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
aParam.mDetail, aParam.mScreenX, aParam.mScreenY,
aParam.mClientX, aParam.mClientY, aParam.mCtrlKey,
aParam.mAltKey, aParam.mShiftKey, aParam.mMetaKey,
aParam.mButton, aParam.mRelatedTarget);
e->InitializeExtraMouseEventDictionaryMembers(aParam);
e->SetTrusted(trusted);
e->SetComposed(aParam.mComposed);
return e.forget();
}
void MouseEvent::InitNSMouseEvent(const nsAString& aType, bool aCanBubble,
bool aCancelable, nsGlobalWindowInner* aView,
int32_t aDetail, int32_t aScreenX,
int32_t aScreenY, int32_t aClientX,
int32_t aClientY, bool aCtrlKey, bool aAltKey,
bool aShiftKey, bool aMetaKey,
uint16_t aButton, EventTarget* aRelatedTarget,
float aPressure, uint16_t aInputSource) {
NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail,
aScreenX, aScreenY, aClientX, aClientY, aCtrlKey,
aAltKey, aShiftKey, aMetaKey, aButton,
aRelatedTarget);
WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
mouseEventBase->mPressure = aPressure;
mouseEventBase->mInputSource = aInputSource;
}
Bug 1528289 - part 2: Dispatch same events on the web contents when autoscroll is canceled with a click r=Gijs,edgar Chrome behaves like this: 1. When user starts autoscroll with a middle click, `mousedown` and `mouseup` are fired, but `auxclick` nor `paste` event is not fired. 2. When user ends autoscroll with a left click, only `mouseup` event is fired. I.e, `mousedown` nor `click` event is not fired. 3. When user ends autoscroll with a middle click, only `mouseup` event is fired. I.e., `mousedown`, `auxclick` nor `paste` events is not fired. 4. When user ends autoscroll with a right click, `mouseup` and `contextmenu` events are fired, but `mousedown` and `auxclick` events are not fired. This patch emulates these Chrome's behavior as far as possible. However, unfortunately, we cannot do exactly same behavior without some big patches because each widget (`nsWindow` or `nsChildView`) discards a mouse event which rolled up a widget before dispatching it into the DOM. Therefore, for now, this patch does not fix the following issues: 1. `mousedown` event is not fired in content when clicking outside the autoscroller to close it except when pressing the secondary button or on any buttons on Linux. 2. `mouseup` event is not fired in content when clicking outside the autoscroller to close it except when pressing the primary button macOS. 3. `click` event and `auxclick` events are fired when clicking outside the autoscroller with the secondary button. So, the middle button `click`/`auxclick` events and `paste` event which is reported to the bug won't be fired with this patch. I'll file follow up bugs. Differential Revision: https://phabricator.services.mozilla.com/D104652
2021-03-23 22:22:48 +03:00
void MouseEvent::PreventClickEvent() {
if (WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent()) {
mouseEvent->mClickEventPrevented = true;
}
}
bool MouseEvent::ClickEventPrevented() {
if (WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent()) {
return mouseEvent->mClickEventPrevented;
}
return false;
}
int16_t MouseEvent::Button() {
switch (mEvent->mClass) {
case eMouseEventClass:
case eMouseScrollEventClass:
case eWheelEventClass:
case eDragEventClass:
case ePointerEventClass:
case eSimpleGestureEventClass:
return mEvent->AsMouseEventBase()->mButton;
default:
NS_WARNING("Tried to get mouse mButton for non-mouse event!");
return MouseButton::ePrimary;
}
}
uint16_t MouseEvent::Buttons() {
switch (mEvent->mClass) {
case eMouseEventClass:
case eMouseScrollEventClass:
case eWheelEventClass:
case eDragEventClass:
case ePointerEventClass:
case eSimpleGestureEventClass:
return mEvent->AsMouseEventBase()->mButtons;
default:
MOZ_CRASH("Tried to get mouse buttons for non-mouse event!");
}
}
already_AddRefed<EventTarget> MouseEvent::GetRelatedTarget() {
nsCOMPtr<EventTarget> relatedTarget;
switch (mEvent->mClass) {
case eMouseEventClass:
case eMouseScrollEventClass:
case eWheelEventClass:
case eDragEventClass:
case ePointerEventClass:
case eSimpleGestureEventClass:
relatedTarget = mEvent->AsMouseEventBase()->mRelatedTarget;
break;
default:
break;
}
return EnsureWebAccessibleRelatedTarget(relatedTarget);
}
void MouseEvent::GetRegion(nsAString& aRegion) {
SetDOMStringToNull(aRegion);
WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
if (mouseEventBase) {
aRegion = mouseEventBase->mRegion;
}
}
int32_t MouseEvent::ScreenX(CallerType aCallerType) {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
if (nsContentUtils::ResistFingerprinting(aCallerType)) {
// Sanitize to something sort of like client cooords, but not quite
// (defaulting to (0,0) instead of our pre-specified client coords).
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
CSSIntPoint(0, 0))
.x;
}
return Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint).x;
}
int32_t MouseEvent::ScreenY(CallerType aCallerType) {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
if (nsContentUtils::ResistFingerprinting(aCallerType)) {
// Sanitize to something sort of like client cooords, but not quite
// (defaulting to (0,0) instead of our pre-specified client coords).
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
CSSIntPoint(0, 0))
.y;
}
return Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint).y;
}
int32_t MouseEvent::PageX() const {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
if (mPrivateDataDuplicated) {
return mPagePoint.x;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.x;
}
int32_t MouseEvent::PageY() const {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
if (mPrivateDataDuplicated) {
return mPagePoint.y;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.y;
}
int32_t MouseEvent::ClientX() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.x;
}
int32_t MouseEvent::ClientY() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.y;
}
int32_t MouseEvent::OffsetX() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
return Event::GetOffsetCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.x;
}
int32_t MouseEvent::OffsetY() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
return Event::GetOffsetCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.y;
}
bool MouseEvent::AltKey() { return mEvent->AsInputEvent()->IsAlt(); }
bool MouseEvent::CtrlKey() { return mEvent->AsInputEvent()->IsControl(); }
bool MouseEvent::ShiftKey() { return mEvent->AsInputEvent()->IsShift(); }
bool MouseEvent::MetaKey() { return mEvent->AsInputEvent()->IsMeta(); }
float MouseEvent::MozPressure() const {
return mEvent->AsMouseEventBase()->mPressure;
}
uint16_t MouseEvent::MozInputSource() const {
return mEvent->AsMouseEventBase()->mInputSource;
}
} // namespace mozilla::dom
using namespace mozilla;
using namespace mozilla::dom;
already_AddRefed<MouseEvent> NS_NewDOMMouseEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetMouseEvent* aEvent) {
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<MouseEvent> it = new MouseEvent(aOwner, aPresContext, aEvent);
return it.forget();
}