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
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Steve Clark (buster@netscape.com)
24 * Ilya Konstantinov (mozilla-code@future.shiny.co.il)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsDOMMouseEvent.h"
41 #include "nsGUIEvent.h"
42 #include "nsIContent.h"
43 #include "nsIEventStateManager.h"
44 #include "nsContentUtils.h"
46 nsDOMMouseEvent::nsDOMMouseEvent(nsPresContext
* aPresContext
,
48 : nsDOMUIEvent(aPresContext
, aEvent
? aEvent
:
49 new nsMouseEvent(PR_FALSE
, 0, nsnull
,
52 // There's no way to make this class' ctor allocate an nsMouseScrollEvent.
53 // It's not that important, though, since a scroll event is not a real
57 mEventIsInternal
= PR_FALSE
;
60 mEventIsInternal
= PR_TRUE
;
61 mEvent
->time
= PR_Now();
62 mEvent
->refPoint
.x
= mEvent
->refPoint
.y
= 0;
65 switch (mEvent
->eventStructType
)
68 mDetail
= static_cast<nsMouseEvent
*>(mEvent
)->clickCount
;
75 nsDOMMouseEvent::~nsDOMMouseEvent()
77 if (mEventIsInternal
&& mEvent
) {
78 switch (mEvent
->eventStructType
)
81 delete static_cast<nsMouseEvent
*>(mEvent
);
91 NS_IMPL_ADDREF_INHERITED(nsDOMMouseEvent
, nsDOMUIEvent
)
92 NS_IMPL_RELEASE_INHERITED(nsDOMMouseEvent
, nsDOMUIEvent
)
94 NS_INTERFACE_MAP_BEGIN(nsDOMMouseEvent
)
95 NS_INTERFACE_MAP_ENTRY(nsIDOMMouseEvent
)
96 NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(MouseEvent
)
97 NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent
)
100 nsDOMMouseEvent::InitMouseEvent(const nsAString
& aType
, PRBool aCanBubble
, PRBool aCancelable
,
101 nsIDOMAbstractView
*aView
, PRInt32 aDetail
, PRInt32 aScreenX
,
102 PRInt32 aScreenY
, PRInt32 aClientX
, PRInt32 aClientY
,
103 PRBool aCtrlKey
, PRBool aAltKey
, PRBool aShiftKey
,
104 PRBool aMetaKey
, PRUint16 aButton
, nsIDOMEventTarget
*aRelatedTarget
)
106 nsresult rv
= nsDOMUIEvent::InitUIEvent(aType
, aCanBubble
, aCancelable
, aView
, aDetail
);
107 NS_ENSURE_SUCCESS(rv
, rv
);
109 switch(mEvent
->eventStructType
)
112 case NS_MOUSE_SCROLL_EVENT
:
114 static_cast<nsMouseEvent_base
*>(mEvent
)->relatedTarget
= aRelatedTarget
;
115 static_cast<nsMouseEvent_base
*>(mEvent
)->button
= aButton
;
116 nsInputEvent
* inputEvent
= static_cast<nsInputEvent
*>(mEvent
);
117 inputEvent
->isControl
= aCtrlKey
;
118 inputEvent
->isAlt
= aAltKey
;
119 inputEvent
->isShift
= aShiftKey
;
120 inputEvent
->isMeta
= aMetaKey
;
121 mClientPoint
.x
= aClientX
;
122 mClientPoint
.y
= aClientY
;
123 inputEvent
->refPoint
.x
= aScreenX
;
124 inputEvent
->refPoint
.y
= aScreenY
;
126 if (mEvent
->eventStructType
== NS_MOUSE_EVENT
) {
127 nsMouseEvent
* mouseEvent
= static_cast<nsMouseEvent
*>(mEvent
);
128 mouseEvent
->clickCount
= aDetail
;
140 nsDOMMouseEvent::GetButton(PRUint16
* aButton
)
142 NS_ENSURE_ARG_POINTER(aButton
);
143 switch(mEvent
->eventStructType
)
146 case NS_MOUSE_SCROLL_EVENT
:
147 *aButton
= static_cast<nsMouseEvent_base
*>(mEvent
)->button
;
150 NS_WARNING("Tried to get mouse button for non-mouse event!");
151 *aButton
= nsMouseEvent::eLeftButton
;
158 nsDOMMouseEvent::GetRelatedTarget(nsIDOMEventTarget
** aRelatedTarget
)
160 NS_ENSURE_ARG_POINTER(aRelatedTarget
);
161 *aRelatedTarget
= nsnull
;
162 nsISupports
* relatedTarget
= nsnull
;
163 switch(mEvent
->eventStructType
)
166 case NS_MOUSE_SCROLL_EVENT
:
167 relatedTarget
= static_cast<nsMouseEvent_base
*>(mEvent
)->relatedTarget
;
174 CallQueryInterface(relatedTarget
, aRelatedTarget
);
179 NS_METHOD
nsDOMMouseEvent::GetScreenX(PRInt32
* aScreenX
)
181 NS_ENSURE_ARG_POINTER(aScreenX
);
182 *aScreenX
= GetScreenPoint().x
;
187 nsDOMMouseEvent::GetScreenY(PRInt32
* aScreenY
)
189 NS_ENSURE_ARG_POINTER(aScreenY
);
190 *aScreenY
= GetScreenPoint().y
;
195 NS_METHOD
nsDOMMouseEvent::GetClientX(PRInt32
* aClientX
)
197 NS_ENSURE_ARG_POINTER(aClientX
);
198 *aClientX
= GetClientPoint().x
;
203 nsDOMMouseEvent::GetClientY(PRInt32
* aClientY
)
205 NS_ENSURE_ARG_POINTER(aClientY
);
206 *aClientY
= GetClientPoint().y
;
211 nsDOMMouseEvent::GetAltKey(PRBool
* aIsDown
)
213 NS_ENSURE_ARG_POINTER(aIsDown
);
214 *aIsDown
= ((nsInputEvent
*)mEvent
)->isAlt
;
219 nsDOMMouseEvent::GetCtrlKey(PRBool
* aIsDown
)
221 NS_ENSURE_ARG_POINTER(aIsDown
);
222 *aIsDown
= ((nsInputEvent
*)mEvent
)->isControl
;
227 nsDOMMouseEvent::GetShiftKey(PRBool
* aIsDown
)
229 NS_ENSURE_ARG_POINTER(aIsDown
);
230 *aIsDown
= ((nsInputEvent
*)mEvent
)->isShift
;
235 nsDOMMouseEvent::GetMetaKey(PRBool
* aIsDown
)
237 NS_ENSURE_ARG_POINTER(aIsDown
);
238 *aIsDown
= ((nsInputEvent
*)mEvent
)->isMeta
;
243 nsDOMMouseEvent::GetWhich(PRUint32
* aWhich
)
245 NS_ENSURE_ARG_POINTER(aWhich
);
247 (void) GetButton(&button
);
248 *aWhich
= button
+ 1;
252 nsresult
NS_NewDOMMouseEvent(nsIDOMEvent
** aInstancePtrResult
,
253 nsPresContext
* aPresContext
,
254 nsInputEvent
*aEvent
)
256 nsDOMMouseEvent
* it
= new nsDOMMouseEvent(aPresContext
, aEvent
);
258 return NS_ERROR_OUT_OF_MEMORY
;
261 return CallQueryInterface(it
, aInstancePtrResult
);