|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "mozilla/dom/DragEvent.h" |
|
7 #include "mozilla/MouseEvents.h" |
|
8 #include "nsContentUtils.h" |
|
9 #include "prtime.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace dom { |
|
13 |
|
14 DragEvent::DragEvent(EventTarget* aOwner, |
|
15 nsPresContext* aPresContext, |
|
16 WidgetDragEvent* aEvent) |
|
17 : MouseEvent(aOwner, aPresContext, |
|
18 aEvent ? aEvent : new WidgetDragEvent(false, 0, nullptr)) |
|
19 { |
|
20 if (aEvent) { |
|
21 mEventIsInternal = false; |
|
22 } |
|
23 else { |
|
24 mEventIsInternal = true; |
|
25 mEvent->time = PR_Now(); |
|
26 mEvent->refPoint.x = mEvent->refPoint.y = 0; |
|
27 mEvent->AsMouseEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN; |
|
28 } |
|
29 } |
|
30 |
|
31 NS_IMPL_ADDREF_INHERITED(DragEvent, MouseEvent) |
|
32 NS_IMPL_RELEASE_INHERITED(DragEvent, MouseEvent) |
|
33 |
|
34 NS_INTERFACE_MAP_BEGIN(DragEvent) |
|
35 NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent) |
|
36 NS_INTERFACE_MAP_END_INHERITING(MouseEvent) |
|
37 |
|
38 void |
|
39 DragEvent::InitDragEvent(const nsAString& aType, |
|
40 bool aCanBubble, |
|
41 bool aCancelable, |
|
42 nsIDOMWindow* aView, |
|
43 int32_t aDetail, |
|
44 int32_t aScreenX, |
|
45 int32_t aScreenY, |
|
46 int32_t aClientX, |
|
47 int32_t aClientY, |
|
48 bool aCtrlKey, |
|
49 bool aAltKey, |
|
50 bool aShiftKey, |
|
51 bool aMetaKey, |
|
52 uint16_t aButton, |
|
53 EventTarget* aRelatedTarget, |
|
54 DataTransfer* aDataTransfer, |
|
55 ErrorResult& aError) |
|
56 { |
|
57 aError = |
|
58 MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, |
|
59 aView, aDetail, aScreenX, aScreenY, |
|
60 aClientX, aClientY, aCtrlKey, aAltKey, |
|
61 aShiftKey, aMetaKey, aButton, aRelatedTarget); |
|
62 if (aError.Failed()) { |
|
63 return; |
|
64 } |
|
65 |
|
66 if (mEventIsInternal && mEvent) { |
|
67 mEvent->AsDragEvent()->dataTransfer = aDataTransfer; |
|
68 } |
|
69 } |
|
70 |
|
71 NS_IMETHODIMP |
|
72 DragEvent::InitDragEvent(const nsAString& aType, |
|
73 bool aCanBubble, |
|
74 bool aCancelable, |
|
75 nsIDOMWindow* aView, |
|
76 int32_t aDetail, |
|
77 int32_t aScreenX, |
|
78 int32_t aScreenY, |
|
79 int32_t aClientX, |
|
80 int32_t aClientY, |
|
81 bool aCtrlKey, |
|
82 bool aAltKey, |
|
83 bool aShiftKey, |
|
84 bool aMetaKey, |
|
85 uint16_t aButton, |
|
86 nsIDOMEventTarget* aRelatedTarget, |
|
87 nsIDOMDataTransfer* aDataTransfer) |
|
88 { |
|
89 nsCOMPtr<DataTransfer> dataTransfer = do_QueryInterface(aDataTransfer); |
|
90 |
|
91 nsresult rv = |
|
92 MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail, |
|
93 aScreenX, aScreenY, aClientX, aClientY, |
|
94 aCtrlKey, aAltKey, aShiftKey, aMetaKey, aButton, |
|
95 aRelatedTarget); |
|
96 NS_ENSURE_SUCCESS(rv, rv); |
|
97 |
|
98 if (mEventIsInternal && mEvent) { |
|
99 mEvent->AsDragEvent()->dataTransfer = dataTransfer; |
|
100 } |
|
101 |
|
102 return NS_OK; |
|
103 } |
|
104 |
|
105 NS_IMETHODIMP |
|
106 DragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer) |
|
107 { |
|
108 NS_IF_ADDREF(*aDataTransfer = GetDataTransfer()); |
|
109 return NS_OK; |
|
110 } |
|
111 |
|
112 DataTransfer* |
|
113 DragEvent::GetDataTransfer() |
|
114 { |
|
115 // the dataTransfer field of the event caches the DataTransfer associated |
|
116 // with the drag. It is initialized when an attempt is made to retrieve it |
|
117 // rather that when the event is created to avoid duplicating the data when |
|
118 // no listener ever uses it. |
|
119 if (!mEvent || mEvent->eventStructType != NS_DRAG_EVENT) { |
|
120 NS_WARNING("Tried to get dataTransfer from non-drag event!"); |
|
121 return nullptr; |
|
122 } |
|
123 |
|
124 WidgetDragEvent* dragEvent = mEvent->AsDragEvent(); |
|
125 // for synthetic events, just use the supplied data transfer object even if null |
|
126 if (!mEventIsInternal) { |
|
127 nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent); |
|
128 NS_ENSURE_SUCCESS(rv, nullptr); |
|
129 } |
|
130 |
|
131 return dragEvent->dataTransfer; |
|
132 } |
|
133 |
|
134 } // namespace dom |
|
135 } // namespace mozilla |
|
136 |
|
137 using namespace mozilla; |
|
138 using namespace mozilla::dom; |
|
139 |
|
140 nsresult |
|
141 NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult, |
|
142 EventTarget* aOwner, |
|
143 nsPresContext* aPresContext, |
|
144 WidgetDragEvent* aEvent) |
|
145 { |
|
146 DragEvent* event = new DragEvent(aOwner, aPresContext, aEvent); |
|
147 return CallQueryInterface(event, aInstancePtrResult); |
|
148 } |