1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is the Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
22 * Neil Deakin <enndeakin@gmail.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsDOMDragEvent.h"
39 #include "nsIServiceManager.h"
40 #include "nsGUIEvent.h"
41 #include "nsContentUtils.h"
42 #include "nsIEventStateManager.h"
43 #include "nsDOMDataTransfer.h"
44 #include "nsIDragService.h"
46 nsDOMDragEvent::nsDOMDragEvent(nsPresContext
* aPresContext
,
48 : nsDOMMouseEvent(aPresContext
, aEvent
? aEvent
:
49 new nsDragEvent(PR_FALSE
, 0, nsnull
))
52 mEventIsInternal
= PR_FALSE
;
55 mEventIsInternal
= PR_TRUE
;
56 mEvent
->time
= PR_Now();
57 mEvent
->refPoint
.x
= mEvent
->refPoint
.y
= 0;
61 nsDOMDragEvent::~nsDOMDragEvent()
63 if (mEventIsInternal
) {
64 if (mEvent
->eventStructType
== NS_DRAG_EVENT
)
65 delete static_cast<nsDragEvent
*>(mEvent
);
70 NS_IMPL_ADDREF_INHERITED(nsDOMDragEvent
, nsDOMMouseEvent
)
71 NS_IMPL_RELEASE_INHERITED(nsDOMDragEvent
, nsDOMMouseEvent
)
73 NS_INTERFACE_MAP_BEGIN(nsDOMDragEvent
)
74 NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent
)
75 NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(DragEvent
)
76 NS_INTERFACE_MAP_END_INHERITING(nsDOMMouseEvent
)
79 nsDOMDragEvent::InitDragEvent(const nsAString
& aType
,
82 nsIDOMAbstractView
* aView
,
84 nsIDOMDataTransfer
* aDataTransfer
)
86 nsresult rv
= nsDOMUIEvent::InitUIEvent(aType
, aCanBubble
, aCancelable
, aView
, aDetail
);
87 NS_ENSURE_SUCCESS(rv
, rv
);
89 if (mEventIsInternal
&& mEvent
) {
90 nsDragEvent
* dragEvent
= static_cast<nsDragEvent
*>(mEvent
);
91 dragEvent
->dataTransfer
= aDataTransfer
;
98 nsDOMDragEvent::InitDragEventNS(const nsAString
& aNamespaceURIArg
,
99 const nsAString
& aType
,
102 nsIDOMAbstractView
* aView
,
104 nsIDOMDataTransfer
* aDataTransfer
)
106 return NS_ERROR_NOT_IMPLEMENTED
;
110 nsDOMDragEvent::GetDataTransfer(nsIDOMDataTransfer
** aDataTransfer
)
112 *aDataTransfer
= nsnull
;
114 if (!mEvent
|| mEvent
->eventStructType
!= NS_DRAG_EVENT
) {
115 NS_WARNING("Tried to get dataTransfer from non-drag event!");
119 // the dataTransfer field of the event caches the DataTransfer associated
120 // with the drag. It is initialized when an attempt is made to retrieve it
121 // rather that when the event is created to avoid duplicating the data when
122 // no listener ever uses it.
123 nsDragEvent
* dragEvent
= static_cast<nsDragEvent
*>(mEvent
);
124 if (dragEvent
->dataTransfer
) {
125 CallQueryInterface(dragEvent
->dataTransfer
, aDataTransfer
);
129 // for synthetic events, just use the supplied data transfer object
130 if (mEventIsInternal
) {
131 NS_IF_ADDREF(*aDataTransfer
= dragEvent
->dataTransfer
);
135 // For draggesture and dragstart events, the data transfer object is
136 // created before the event fires, so it should already be set. For other
137 // drag events, get the object from the drag session.
138 NS_ASSERTION(mEvent
->message
!= NS_DRAGDROP_GESTURE
&&
139 mEvent
->message
!= NS_DRAGDROP_START
,
140 "draggesture event created without a dataTransfer");
142 nsCOMPtr
<nsIDragSession
> dragSession
= nsContentUtils::GetDragSession();
143 NS_ENSURE_TRUE(dragSession
, NS_OK
); // no drag in progress
145 nsCOMPtr
<nsIDOMDataTransfer
> initialDataTransfer
;
146 dragSession
->GetDataTransfer(getter_AddRefs(initialDataTransfer
));
147 if (!initialDataTransfer
) {
148 // A dataTransfer won't exist when a drag was started by some other
149 // means, for instance calling the drag service directly, or a drag
150 // from another application. In either case, a new dataTransfer should
151 // be created that reflects the data. Pass true to the constructor for
152 // the aIsExternal argument, so that only system access is allowed.
154 dragSession
->GetDragAction(&action
);
155 initialDataTransfer
=
156 new nsDOMDataTransfer(mEvent
->message
, action
);
157 NS_ENSURE_TRUE(initialDataTransfer
, NS_ERROR_OUT_OF_MEMORY
);
159 // now set it in the drag session so we don't need to create it again
160 dragSession
->SetDataTransfer(initialDataTransfer
);
163 // each event should use a clone of the original dataTransfer.
164 nsCOMPtr
<nsIDOMNSDataTransfer
> initialDataTransferNS
=
165 do_QueryInterface(initialDataTransfer
);
166 NS_ENSURE_TRUE(initialDataTransferNS
, NS_ERROR_FAILURE
);
167 initialDataTransferNS
->Clone(mEvent
->message
,
168 getter_AddRefs(dragEvent
->dataTransfer
));
169 NS_ENSURE_TRUE(dragEvent
->dataTransfer
, NS_ERROR_OUT_OF_MEMORY
);
171 // for the dragenter and dragover events, initialize the drop effect
172 // from the drop action, which platform specific widget code sets before
173 // the event is fired based on the keyboard state.
174 if (mEvent
->message
== NS_DRAGDROP_ENTER
||
175 mEvent
->message
== NS_DRAGDROP_OVER
) {
176 nsCOMPtr
<nsIDOMNSDataTransfer
> newDataTransfer
=
177 do_QueryInterface(dragEvent
->dataTransfer
);
178 NS_ENSURE_TRUE(newDataTransfer
, NS_ERROR_FAILURE
);
180 PRUint32 action
, effectAllowed
;
181 dragSession
->GetDragAction(&action
);
182 newDataTransfer
->GetEffectAllowedInt(&effectAllowed
);
183 newDataTransfer
->SetDropEffectInt(FilterDropEffect(action
, effectAllowed
));
185 else if (mEvent
->message
== NS_DRAGDROP_DROP
||
186 mEvent
->message
== NS_DRAGDROP_DRAGDROP
||
187 mEvent
->message
== NS_DRAGDROP_END
) {
188 // For the drop and dragend events, set the drop effect based on the
189 // last value that the dropEffect had. This will have been set in
190 // nsEventStateManager::PostHandleEvent for the last dragenter or
192 nsCOMPtr
<nsIDOMNSDataTransfer
> newDataTransfer
=
193 do_QueryInterface(dragEvent
->dataTransfer
);
194 NS_ENSURE_TRUE(newDataTransfer
, NS_ERROR_FAILURE
);
197 initialDataTransferNS
->GetDropEffectInt(&dropEffect
);
198 newDataTransfer
->SetDropEffectInt(dropEffect
);
201 NS_IF_ADDREF(*aDataTransfer
= dragEvent
->dataTransfer
);
207 nsDOMDragEvent::FilterDropEffect(PRUint32 aAction
, PRUint32 aEffectAllowed
)
209 // It is possible for the drag action to include more than one action, but
210 // the widget code which sets the action from the keyboard state should only
211 // be including one. If multiple actions were set, we just consider them in
212 // the following order:
214 if (aAction
& nsIDragService::DRAGDROP_ACTION_COPY
)
215 aAction
= nsIDragService::DRAGDROP_ACTION_COPY
;
216 else if (aAction
& nsIDragService::DRAGDROP_ACTION_LINK
)
217 aAction
= nsIDragService::DRAGDROP_ACTION_LINK
;
218 else if (aAction
& nsIDragService::DRAGDROP_ACTION_MOVE
)
219 aAction
= nsIDragService::DRAGDROP_ACTION_MOVE
;
221 // Filter the action based on the effectAllowed. If the effectAllowed
222 // doesn't include the action, then that action cannot be done, so adjust
223 // the action to something that is allowed. For a copy, adjust to move or
224 // link. For a move, adjust to copy or link. For a link, adjust to move or
225 // link. Otherwise, use none.
226 if (aAction
& aEffectAllowed
||
227 aEffectAllowed
== nsIDragService::DRAGDROP_ACTION_UNINITIALIZED
)
229 if (aEffectAllowed
& nsIDragService::DRAGDROP_ACTION_MOVE
)
230 return nsIDragService::DRAGDROP_ACTION_MOVE
;
231 if (aEffectAllowed
& nsIDragService::DRAGDROP_ACTION_COPY
)
232 return nsIDragService::DRAGDROP_ACTION_COPY
;
233 if (aEffectAllowed
& nsIDragService::DRAGDROP_ACTION_LINK
)
234 return nsIDragService::DRAGDROP_ACTION_LINK
;
235 return nsIDragService::DRAGDROP_ACTION_NONE
;
238 nsresult
NS_NewDOMDragEvent(nsIDOMEvent
** aInstancePtrResult
,
239 nsPresContext
* aPresContext
,
242 nsDOMDragEvent
* event
= new nsDOMDragEvent(aPresContext
, aEvent
);
243 NS_ENSURE_TRUE(event
, NS_ERROR_OUT_OF_MEMORY
);
245 return CallQueryInterface(event
, aInstancePtrResult
);