Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / content / events / src / nsDOMDragEvent.cpp
blob292763eb87a3068a3e7762223bf48adb38900134
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
13 * License.
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.
21 * Contributor(s):
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,
47 nsInputEvent* aEvent)
48 : nsDOMMouseEvent(aPresContext, aEvent ? aEvent :
49 new nsDragEvent(PR_FALSE, 0, nsnull))
51 if (aEvent) {
52 mEventIsInternal = PR_FALSE;
54 else {
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);
66 mEvent = nsnull;
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)
78 NS_IMETHODIMP
79 nsDOMDragEvent::InitDragEvent(const nsAString & aType,
80 PRBool aCanBubble, PRBool aCancelable,
81 nsIDOMAbstractView* aView, PRInt32 aDetail,
82 PRInt32 aScreenX, PRInt32 aScreenY,
83 PRInt32 aClientX, PRInt32 aClientY,
84 PRBool aCtrlKey, PRBool aAltKey, PRBool aShiftKey,
85 PRBool aMetaKey, PRUint16 aButton,
86 nsIDOMEventTarget *aRelatedTarget,
87 nsIDOMDataTransfer* aDataTransfer)
89 nsresult rv = nsDOMMouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
90 aView, aDetail, aScreenX, aScreenY, aClientX, aClientY,
91 aCtrlKey, aAltKey, aShiftKey, aMetaKey, aButton,
92 aRelatedTarget);
93 NS_ENSURE_SUCCESS(rv, rv);
95 if (mEventIsInternal && mEvent) {
96 nsDragEvent* dragEvent = static_cast<nsDragEvent*>(mEvent);
97 dragEvent->dataTransfer = aDataTransfer;
100 return NS_OK;
103 NS_IMETHODIMP
104 nsDOMDragEvent::InitDragEventNS(const nsAString & aNamespaceURIArg,
105 const nsAString & aType,
106 PRBool aCanBubble, PRBool aCancelable,
107 nsIDOMAbstractView* aView, PRInt32 aDetail,
108 PRInt32 aScreenX, PRInt32 aScreenY,
109 PRInt32 aClientX, PRInt32 aClientY,
110 PRBool aCtrlKey, PRBool aAltKey, PRBool aShiftKey,
111 PRBool aMetaKey, PRUint16 aButton,
112 nsIDOMEventTarget *aRelatedTarget,
113 nsIDOMDataTransfer* aDataTransfer)
115 return NS_ERROR_NOT_IMPLEMENTED;
118 NS_IMETHODIMP
119 nsDOMDragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
121 *aDataTransfer = nsnull;
123 if (!mEvent || mEvent->eventStructType != NS_DRAG_EVENT) {
124 NS_WARNING("Tried to get dataTransfer from non-drag event!");
125 return NS_OK;
128 // the dataTransfer field of the event caches the DataTransfer associated
129 // with the drag. It is initialized when an attempt is made to retrieve it
130 // rather that when the event is created to avoid duplicating the data when
131 // no listener ever uses it.
132 nsDragEvent* dragEvent = static_cast<nsDragEvent*>(mEvent);
133 if (dragEvent->dataTransfer) {
134 CallQueryInterface(dragEvent->dataTransfer, aDataTransfer);
135 return NS_OK;
138 // for synthetic events, just use the supplied data transfer object
139 if (mEventIsInternal) {
140 NS_IF_ADDREF(*aDataTransfer = dragEvent->dataTransfer);
141 return NS_OK;
144 // For draggesture and dragstart events, the data transfer object is
145 // created before the event fires, so it should already be set. For other
146 // drag events, get the object from the drag session.
147 NS_ASSERTION(mEvent->message != NS_DRAGDROP_GESTURE &&
148 mEvent->message != NS_DRAGDROP_START,
149 "draggesture event created without a dataTransfer");
151 nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
152 NS_ENSURE_TRUE(dragSession, NS_OK); // no drag in progress
154 nsCOMPtr<nsIDOMDataTransfer> initialDataTransfer;
155 dragSession->GetDataTransfer(getter_AddRefs(initialDataTransfer));
156 if (!initialDataTransfer) {
157 // A dataTransfer won't exist when a drag was started by some other
158 // means, for instance calling the drag service directly, or a drag
159 // from another application. In either case, a new dataTransfer should
160 // be created that reflects the data. Pass true to the constructor for
161 // the aIsExternal argument, so that only system access is allowed.
162 PRUint32 action = 0;
163 dragSession->GetDragAction(&action);
164 initialDataTransfer =
165 new nsDOMDataTransfer(mEvent->message, action);
166 NS_ENSURE_TRUE(initialDataTransfer, NS_ERROR_OUT_OF_MEMORY);
168 // now set it in the drag session so we don't need to create it again
169 dragSession->SetDataTransfer(initialDataTransfer);
172 // each event should use a clone of the original dataTransfer.
173 nsCOMPtr<nsIDOMNSDataTransfer> initialDataTransferNS =
174 do_QueryInterface(initialDataTransfer);
175 NS_ENSURE_TRUE(initialDataTransferNS, NS_ERROR_FAILURE);
176 initialDataTransferNS->Clone(mEvent->message,
177 getter_AddRefs(dragEvent->dataTransfer));
178 NS_ENSURE_TRUE(dragEvent->dataTransfer, NS_ERROR_OUT_OF_MEMORY);
180 // for the dragenter and dragover events, initialize the drop effect
181 // from the drop action, which platform specific widget code sets before
182 // the event is fired based on the keyboard state.
183 if (mEvent->message == NS_DRAGDROP_ENTER ||
184 mEvent->message == NS_DRAGDROP_OVER) {
185 nsCOMPtr<nsIDOMNSDataTransfer> newDataTransfer =
186 do_QueryInterface(dragEvent->dataTransfer);
187 NS_ENSURE_TRUE(newDataTransfer, NS_ERROR_FAILURE);
189 PRUint32 action, effectAllowed;
190 dragSession->GetDragAction(&action);
191 newDataTransfer->GetEffectAllowedInt(&effectAllowed);
192 newDataTransfer->SetDropEffectInt(FilterDropEffect(action, effectAllowed));
194 else if (mEvent->message == NS_DRAGDROP_DROP ||
195 mEvent->message == NS_DRAGDROP_DRAGDROP ||
196 mEvent->message == NS_DRAGDROP_END) {
197 // For the drop and dragend events, set the drop effect based on the
198 // last value that the dropEffect had. This will have been set in
199 // nsEventStateManager::PostHandleEvent for the last dragenter or
200 // dragover event.
201 nsCOMPtr<nsIDOMNSDataTransfer> newDataTransfer =
202 do_QueryInterface(dragEvent->dataTransfer);
203 NS_ENSURE_TRUE(newDataTransfer, NS_ERROR_FAILURE);
205 PRUint32 dropEffect;
206 initialDataTransferNS->GetDropEffectInt(&dropEffect);
207 newDataTransfer->SetDropEffectInt(dropEffect);
210 NS_IF_ADDREF(*aDataTransfer = dragEvent->dataTransfer);
211 return NS_OK;
214 // static
215 PRUint32
216 nsDOMDragEvent::FilterDropEffect(PRUint32 aAction, PRUint32 aEffectAllowed)
218 // It is possible for the drag action to include more than one action, but
219 // the widget code which sets the action from the keyboard state should only
220 // be including one. If multiple actions were set, we just consider them in
221 // the following order:
222 // copy, link, move
223 if (aAction & nsIDragService::DRAGDROP_ACTION_COPY)
224 aAction = nsIDragService::DRAGDROP_ACTION_COPY;
225 else if (aAction & nsIDragService::DRAGDROP_ACTION_LINK)
226 aAction = nsIDragService::DRAGDROP_ACTION_LINK;
227 else if (aAction & nsIDragService::DRAGDROP_ACTION_MOVE)
228 aAction = nsIDragService::DRAGDROP_ACTION_MOVE;
230 // Filter the action based on the effectAllowed. If the effectAllowed
231 // doesn't include the action, then that action cannot be done, so adjust
232 // the action to something that is allowed. For a copy, adjust to move or
233 // link. For a move, adjust to copy or link. For a link, adjust to move or
234 // link. Otherwise, use none.
235 if (aAction & aEffectAllowed ||
236 aEffectAllowed == nsIDragService::DRAGDROP_ACTION_UNINITIALIZED)
237 return aAction;
238 if (aEffectAllowed & nsIDragService::DRAGDROP_ACTION_MOVE)
239 return nsIDragService::DRAGDROP_ACTION_MOVE;
240 if (aEffectAllowed & nsIDragService::DRAGDROP_ACTION_COPY)
241 return nsIDragService::DRAGDROP_ACTION_COPY;
242 if (aEffectAllowed & nsIDragService::DRAGDROP_ACTION_LINK)
243 return nsIDragService::DRAGDROP_ACTION_LINK;
244 return nsIDragService::DRAGDROP_ACTION_NONE;
247 nsresult NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult,
248 nsPresContext* aPresContext,
249 nsDragEvent *aEvent)
251 nsDOMDragEvent* event = new nsDOMDragEvent(aPresContext, aEvent);
252 NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);
254 return CallQueryInterface(event, aInstancePtrResult);