1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/DragEvent.h"
8 #include "mozilla/dom/MouseEventBinding.h"
9 #include "mozilla/MouseEvents.h"
10 #include "nsContentUtils.h"
13 namespace mozilla::dom
{
15 DragEvent::DragEvent(EventTarget
* aOwner
, nsPresContext
* aPresContext
,
16 WidgetDragEvent
* aEvent
)
19 aEvent
? aEvent
: new WidgetDragEvent(false, eVoidEvent
, nullptr)) {
21 mEventIsInternal
= false;
23 mEventIsInternal
= true;
24 mEvent
->mRefPoint
= LayoutDeviceIntPoint(0, 0);
25 mEvent
->AsMouseEvent()->mInputSource
=
26 MouseEvent_Binding::MOZ_SOURCE_UNKNOWN
;
30 void DragEvent::InitDragEvent(const nsAString
& aType
, bool aCanBubble
,
31 bool aCancelable
, nsGlobalWindowInner
* aView
,
32 int32_t aDetail
, int32_t aScreenX
,
33 int32_t aScreenY
, int32_t aClientX
,
34 int32_t aClientY
, bool aCtrlKey
, bool aAltKey
,
35 bool aShiftKey
, bool aMetaKey
, uint16_t aButton
,
36 EventTarget
* aRelatedTarget
,
37 DataTransfer
* aDataTransfer
) {
38 NS_ENSURE_TRUE_VOID(!mEvent
->mFlags
.mIsBeingDispatched
);
40 MouseEvent::InitMouseEvent(aType
, aCanBubble
, aCancelable
, aView
, aDetail
,
41 aScreenX
, aScreenY
, aClientX
, aClientY
, aCtrlKey
,
42 aAltKey
, aShiftKey
, aMetaKey
, aButton
,
44 if (mEventIsInternal
) {
45 mEvent
->AsDragEvent()->mDataTransfer
= aDataTransfer
;
49 DataTransfer
* DragEvent::GetDataTransfer() {
50 // the dataTransfer field of the event caches the DataTransfer associated
51 // with the drag. It is initialized when an attempt is made to retrieve it
52 // rather that when the event is created to avoid duplicating the data when
53 // no listener ever uses it.
54 if (!mEvent
|| mEvent
->mClass
!= eDragEventClass
) {
55 NS_WARNING("Tried to get dataTransfer from non-drag event!");
59 WidgetDragEvent
* dragEvent
= mEvent
->AsDragEvent();
60 // for synthetic events, just use the supplied data transfer object even if
62 if (!mEventIsInternal
) {
63 nsresult rv
= nsContentUtils::SetDataTransferInEvent(dragEvent
);
64 NS_ENSURE_SUCCESS(rv
, nullptr);
67 return dragEvent
->mDataTransfer
;
71 already_AddRefed
<DragEvent
> DragEvent::Constructor(
72 const GlobalObject
& aGlobal
, const nsAString
& aType
,
73 const DragEventInit
& aParam
) {
74 nsCOMPtr
<EventTarget
> t
= do_QueryInterface(aGlobal
.GetAsSupports());
75 RefPtr
<DragEvent
> e
= new DragEvent(t
, nullptr, nullptr);
76 bool trusted
= e
->Init(t
);
77 e
->InitDragEvent(aType
, aParam
.mBubbles
, aParam
.mCancelable
, aParam
.mView
,
78 aParam
.mDetail
, aParam
.mScreenX
, aParam
.mScreenY
,
79 aParam
.mClientX
, aParam
.mClientY
, aParam
.mCtrlKey
,
80 aParam
.mAltKey
, aParam
.mShiftKey
, aParam
.mMetaKey
,
81 aParam
.mButton
, aParam
.mRelatedTarget
, aParam
.mDataTransfer
);
82 e
->InitializeExtraMouseEventDictionaryMembers(aParam
);
83 e
->SetTrusted(trusted
);
84 e
->SetComposed(aParam
.mComposed
);
88 } // namespace mozilla::dom
90 using namespace mozilla
;
91 using namespace mozilla::dom
;
93 already_AddRefed
<DragEvent
> NS_NewDOMDragEvent(EventTarget
* aOwner
,
94 nsPresContext
* aPresContext
,
95 WidgetDragEvent
* aEvent
) {
96 RefPtr
<DragEvent
> event
= new DragEvent(aOwner
, aPresContext
, aEvent
);
97 return event
.forget();