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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
20 : *
21 : * Contributor(s):
22 : * Neil Deakin <enndeakin@gmail.com>
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsDOMDragEvent.h"
39 : #include "nsIServiceManager.h"
40 : #include "nsGUIEvent.h"
41 : #include "nsContentUtils.h"
42 : #include "nsDOMDataTransfer.h"
43 : #include "nsIDragService.h"
44 :
45 0 : nsDOMDragEvent::nsDOMDragEvent(nsPresContext* aPresContext,
46 : nsInputEvent* aEvent)
47 : : nsDOMMouseEvent(aPresContext, aEvent ? aEvent :
48 0 : new nsDragEvent(false, 0, nsnull))
49 : {
50 0 : if (aEvent) {
51 0 : mEventIsInternal = false;
52 : }
53 : else {
54 0 : mEventIsInternal = true;
55 0 : mEvent->time = PR_Now();
56 0 : mEvent->refPoint.x = mEvent->refPoint.y = 0;
57 0 : static_cast<nsMouseEvent*>(mEvent)->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
58 : }
59 0 : }
60 :
61 0 : nsDOMDragEvent::~nsDOMDragEvent()
62 : {
63 0 : if (mEventIsInternal) {
64 0 : if (mEvent->eventStructType == NS_DRAG_EVENT)
65 0 : delete static_cast<nsDragEvent*>(mEvent);
66 0 : mEvent = nsnull;
67 : }
68 0 : }
69 :
70 0 : NS_IMPL_ADDREF_INHERITED(nsDOMDragEvent, nsDOMMouseEvent)
71 0 : NS_IMPL_RELEASE_INHERITED(nsDOMDragEvent, nsDOMMouseEvent)
72 :
73 : DOMCI_DATA(DragEvent, nsDOMDragEvent)
74 :
75 0 : NS_INTERFACE_MAP_BEGIN(nsDOMDragEvent)
76 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent)
77 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DragEvent)
78 0 : NS_INTERFACE_MAP_END_INHERITING(nsDOMMouseEvent)
79 :
80 : NS_IMETHODIMP
81 0 : nsDOMDragEvent::InitDragEvent(const nsAString & aType,
82 : bool aCanBubble, bool aCancelable,
83 : nsIDOMWindow* aView, PRInt32 aDetail,
84 : PRInt32 aScreenX, PRInt32 aScreenY,
85 : PRInt32 aClientX, PRInt32 aClientY,
86 : bool aCtrlKey, bool aAltKey, bool aShiftKey,
87 : bool aMetaKey, PRUint16 aButton,
88 : nsIDOMEventTarget *aRelatedTarget,
89 : nsIDOMDataTransfer* aDataTransfer)
90 : {
91 : nsresult rv = nsDOMMouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
92 : aView, aDetail, aScreenX, aScreenY, aClientX, aClientY,
93 : aCtrlKey, aAltKey, aShiftKey, aMetaKey, aButton,
94 0 : aRelatedTarget);
95 0 : NS_ENSURE_SUCCESS(rv, rv);
96 :
97 0 : if (mEventIsInternal && mEvent) {
98 0 : nsDragEvent* dragEvent = static_cast<nsDragEvent*>(mEvent);
99 0 : dragEvent->dataTransfer = aDataTransfer;
100 : }
101 :
102 0 : return NS_OK;
103 : }
104 :
105 : NS_IMETHODIMP
106 0 : nsDOMDragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
107 : {
108 : // the dataTransfer field of the event caches the DataTransfer associated
109 : // with the drag. It is initialized when an attempt is made to retrieve it
110 : // rather that when the event is created to avoid duplicating the data when
111 : // no listener ever uses it.
112 0 : *aDataTransfer = nsnull;
113 :
114 0 : if (!mEvent || mEvent->eventStructType != NS_DRAG_EVENT) {
115 0 : NS_WARNING("Tried to get dataTransfer from non-drag event!");
116 0 : return NS_OK;
117 : }
118 :
119 0 : nsDragEvent* dragEvent = static_cast<nsDragEvent*>(mEvent);
120 : // for synthetic events, just use the supplied data transfer object even if null
121 0 : if (!mEventIsInternal) {
122 0 : nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
123 0 : NS_ENSURE_SUCCESS(rv, rv);
124 : }
125 :
126 0 : NS_IF_ADDREF(*aDataTransfer = dragEvent->dataTransfer);
127 0 : return NS_OK;
128 : }
129 :
130 0 : nsresult NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult,
131 : nsPresContext* aPresContext,
132 : nsDragEvent *aEvent)
133 : {
134 0 : nsDOMDragEvent* event = new nsDOMDragEvent(aPresContext, aEvent);
135 0 : return CallQueryInterface(event, aInstancePtrResult);
136 : }
|