Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/DragEvent.h"
8 : #include "mozilla/MouseEvents.h"
9 : #include "nsContentUtils.h"
10 : #include "prtime.h"
11 :
12 : namespace mozilla {
13 : namespace dom {
14 :
15 0 : DragEvent::DragEvent(EventTarget* aOwner,
16 : nsPresContext* aPresContext,
17 0 : WidgetDragEvent* aEvent)
18 : : MouseEvent(aOwner, aPresContext,
19 : aEvent ? aEvent :
20 0 : new WidgetDragEvent(false, eVoidEvent, nullptr))
21 : {
22 0 : if (aEvent) {
23 0 : mEventIsInternal = false;
24 : }
25 : else {
26 0 : mEventIsInternal = true;
27 0 : mEvent->mTime = PR_Now();
28 0 : mEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
29 0 : mEvent->AsMouseEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
30 : }
31 0 : }
32 :
33 0 : NS_IMPL_ADDREF_INHERITED(DragEvent, MouseEvent)
34 0 : NS_IMPL_RELEASE_INHERITED(DragEvent, MouseEvent)
35 :
36 0 : NS_INTERFACE_MAP_BEGIN(DragEvent)
37 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent)
38 0 : NS_INTERFACE_MAP_END_INHERITING(MouseEvent)
39 :
40 : void
41 0 : DragEvent::InitDragEvent(const nsAString& aType,
42 : bool aCanBubble,
43 : bool aCancelable,
44 : nsGlobalWindow* aView,
45 : int32_t aDetail,
46 : int32_t aScreenX,
47 : int32_t aScreenY,
48 : int32_t aClientX,
49 : int32_t aClientY,
50 : bool aCtrlKey,
51 : bool aAltKey,
52 : bool aShiftKey,
53 : bool aMetaKey,
54 : uint16_t aButton,
55 : EventTarget* aRelatedTarget,
56 : DataTransfer* aDataTransfer)
57 : {
58 0 : NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
59 :
60 0 : MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
61 : aView, aDetail, aScreenX, aScreenY,
62 : aClientX, aClientY, aCtrlKey, aAltKey,
63 0 : aShiftKey, aMetaKey, aButton, aRelatedTarget);
64 0 : if (mEventIsInternal) {
65 0 : mEvent->AsDragEvent()->mDataTransfer = aDataTransfer;
66 : }
67 : }
68 :
69 : NS_IMETHODIMP
70 0 : DragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
71 : {
72 0 : NS_IF_ADDREF(*aDataTransfer = GetDataTransfer());
73 0 : return NS_OK;
74 : }
75 :
76 : DataTransfer*
77 0 : DragEvent::GetDataTransfer()
78 : {
79 : // the dataTransfer field of the event caches the DataTransfer associated
80 : // with the drag. It is initialized when an attempt is made to retrieve it
81 : // rather that when the event is created to avoid duplicating the data when
82 : // no listener ever uses it.
83 0 : if (!mEvent || mEvent->mClass != eDragEventClass) {
84 0 : NS_WARNING("Tried to get dataTransfer from non-drag event!");
85 0 : return nullptr;
86 : }
87 :
88 0 : WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
89 : // for synthetic events, just use the supplied data transfer object even if null
90 0 : if (!mEventIsInternal) {
91 0 : nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
92 0 : NS_ENSURE_SUCCESS(rv, nullptr);
93 : }
94 :
95 0 : return dragEvent->mDataTransfer;
96 : }
97 :
98 : // static
99 : already_AddRefed<DragEvent>
100 0 : DragEvent::Constructor(const GlobalObject& aGlobal,
101 : const nsAString& aType,
102 : const DragEventInit& aParam,
103 : ErrorResult& aRv)
104 : {
105 0 : nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
106 0 : RefPtr<DragEvent> e = new DragEvent(t, nullptr, nullptr);
107 0 : bool trusted = e->Init(t);
108 0 : e->InitDragEvent(aType, aParam.mBubbles, aParam.mCancelable,
109 0 : aParam.mView, aParam.mDetail, aParam.mScreenX,
110 0 : aParam.mScreenY, aParam.mClientX, aParam.mClientY,
111 0 : aParam.mCtrlKey, aParam.mAltKey, aParam.mShiftKey,
112 0 : aParam.mMetaKey, aParam.mButton, aParam.mRelatedTarget,
113 0 : aParam.mDataTransfer);
114 0 : e->InitializeExtraMouseEventDictionaryMembers(aParam);
115 0 : e->SetTrusted(trusted);
116 0 : e->SetComposed(aParam.mComposed);
117 0 : return e.forget();
118 : }
119 :
120 : } // namespace dom
121 : } // namespace mozilla
122 :
123 : using namespace mozilla;
124 : using namespace mozilla::dom;
125 :
126 : already_AddRefed<DragEvent>
127 0 : NS_NewDOMDragEvent(EventTarget* aOwner,
128 : nsPresContext* aPresContext,
129 : WidgetDragEvent* aEvent)
130 : {
131 : RefPtr<DragEvent> event =
132 0 : new DragEvent(aOwner, aPresContext, aEvent);
133 0 : return event.forget();
134 : }
|