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
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Steve Clark (buster@netscape.com)
24 : * Ilya Konstantinov (mozilla-code@future.shiny.co.il)
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsDOMMouseEvent.h"
41 : #include "nsGUIEvent.h"
42 : #include "nsIContent.h"
43 : #include "nsContentUtils.h"
44 : #include "DictionaryHelpers.h"
45 :
46 2 : nsDOMMouseEvent::nsDOMMouseEvent(nsPresContext* aPresContext,
47 : nsInputEvent* aEvent)
48 : : nsDOMUIEvent(aPresContext, aEvent ? aEvent :
49 : new nsMouseEvent(false, 0, nsnull,
50 4 : nsMouseEvent::eReal))
51 : {
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
54 : // DOM event.
55 :
56 2 : if (aEvent) {
57 0 : mEventIsInternal = false;
58 : }
59 : else {
60 2 : mEventIsInternal = true;
61 2 : mEvent->time = PR_Now();
62 2 : mEvent->refPoint.x = mEvent->refPoint.y = 0;
63 2 : static_cast<nsMouseEvent*>(mEvent)->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
64 : }
65 :
66 2 : switch (mEvent->eventStructType)
67 : {
68 : case NS_MOUSE_EVENT:
69 2 : NS_ASSERTION(static_cast<nsMouseEvent*>(mEvent)->reason
70 : != nsMouseEvent::eSynthesized,
71 : "Don't dispatch DOM events from synthesized mouse events");
72 2 : mDetail = static_cast<nsMouseEvent*>(mEvent)->clickCount;
73 2 : break;
74 : default:
75 0 : break;
76 : }
77 2 : }
78 :
79 6 : nsDOMMouseEvent::~nsDOMMouseEvent()
80 : {
81 2 : if (mEventIsInternal && mEvent) {
82 2 : switch (mEvent->eventStructType)
83 : {
84 : case NS_MOUSE_EVENT:
85 2 : delete static_cast<nsMouseEvent*>(mEvent);
86 2 : break;
87 : default:
88 0 : delete mEvent;
89 0 : break;
90 : }
91 2 : mEvent = nsnull;
92 : }
93 8 : }
94 :
95 12 : NS_IMPL_ADDREF_INHERITED(nsDOMMouseEvent, nsDOMUIEvent)
96 12 : NS_IMPL_RELEASE_INHERITED(nsDOMMouseEvent, nsDOMUIEvent)
97 :
98 : DOMCI_DATA(MouseEvent, nsDOMMouseEvent)
99 :
100 34 : NS_INTERFACE_MAP_BEGIN(nsDOMMouseEvent)
101 34 : NS_INTERFACE_MAP_ENTRY(nsIDOMMouseEvent)
102 34 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MouseEvent)
103 30 : NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent)
104 :
105 : NS_IMETHODIMP
106 0 : nsDOMMouseEvent::InitMouseEvent(const nsAString & aType, bool aCanBubble, bool aCancelable,
107 : nsIDOMWindow* aView, PRInt32 aDetail, PRInt32 aScreenX,
108 : PRInt32 aScreenY, PRInt32 aClientX, PRInt32 aClientY,
109 : bool aCtrlKey, bool aAltKey, bool aShiftKey,
110 : bool aMetaKey, PRUint16 aButton, nsIDOMEventTarget *aRelatedTarget)
111 : {
112 0 : nsresult rv = nsDOMUIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, aDetail);
113 0 : NS_ENSURE_SUCCESS(rv, rv);
114 :
115 0 : switch(mEvent->eventStructType)
116 : {
117 : case NS_MOUSE_EVENT:
118 : case NS_MOUSE_SCROLL_EVENT:
119 : case NS_DRAG_EVENT:
120 : case NS_SIMPLE_GESTURE_EVENT:
121 : case NS_MOZTOUCH_EVENT:
122 : {
123 0 : static_cast<nsMouseEvent_base*>(mEvent)->relatedTarget = aRelatedTarget;
124 0 : static_cast<nsMouseEvent_base*>(mEvent)->button = aButton;
125 0 : nsInputEvent* inputEvent = static_cast<nsInputEvent*>(mEvent);
126 0 : inputEvent->isControl = aCtrlKey;
127 0 : inputEvent->isAlt = aAltKey;
128 0 : inputEvent->isShift = aShiftKey;
129 0 : inputEvent->isMeta = aMetaKey;
130 0 : mClientPoint.x = aClientX;
131 0 : mClientPoint.y = aClientY;
132 0 : inputEvent->refPoint.x = aScreenX;
133 0 : inputEvent->refPoint.y = aScreenY;
134 :
135 0 : if (mEvent->eventStructType == NS_MOUSE_EVENT) {
136 0 : nsMouseEvent* mouseEvent = static_cast<nsMouseEvent*>(mEvent);
137 0 : mouseEvent->clickCount = aDetail;
138 : }
139 0 : break;
140 : }
141 : default:
142 0 : break;
143 : }
144 :
145 0 : return NS_OK;
146 : }
147 :
148 : nsresult
149 0 : nsDOMMouseEvent::InitFromCtor(const nsAString& aType,
150 : JSContext* aCx, jsval* aVal)
151 : {
152 0 : mozilla::dom::MouseEventInit d;
153 0 : nsresult rv = d.Init(aCx, aVal);
154 0 : NS_ENSURE_SUCCESS(rv, rv);
155 : return InitMouseEvent(aType, d.bubbles, d.cancelable,
156 : d.view, d.detail, d.screenX, d.screenY,
157 : d.clientX, d.clientY,
158 : d.ctrlKey, d.altKey, d.shiftKey, d.metaKey,
159 0 : d.button, d.relatedTarget);
160 : }
161 :
162 : NS_IMETHODIMP
163 0 : nsDOMMouseEvent::InitNSMouseEvent(const nsAString & aType, bool aCanBubble, bool aCancelable,
164 : nsIDOMWindow *aView, PRInt32 aDetail, PRInt32 aScreenX,
165 : PRInt32 aScreenY, PRInt32 aClientX, PRInt32 aClientY,
166 : bool aCtrlKey, bool aAltKey, bool aShiftKey,
167 : bool aMetaKey, PRUint16 aButton, nsIDOMEventTarget *aRelatedTarget,
168 : float aPressure, PRUint16 aInputSource)
169 : {
170 : nsresult rv = nsDOMMouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
171 : aView, aDetail, aScreenX, aScreenY,
172 : aClientX, aClientY, aCtrlKey, aAltKey, aShiftKey,
173 0 : aMetaKey, aButton, aRelatedTarget);
174 0 : NS_ENSURE_SUCCESS(rv, rv);
175 :
176 0 : static_cast<nsMouseEvent_base*>(mEvent)->pressure = aPressure;
177 0 : static_cast<nsMouseEvent_base*>(mEvent)->inputSource = aInputSource;
178 0 : return NS_OK;
179 : }
180 :
181 : NS_IMETHODIMP
182 0 : nsDOMMouseEvent::GetButton(PRUint16* aButton)
183 : {
184 0 : NS_ENSURE_ARG_POINTER(aButton);
185 0 : switch(mEvent->eventStructType)
186 : {
187 : case NS_MOUSE_EVENT:
188 : case NS_MOUSE_SCROLL_EVENT:
189 : case NS_DRAG_EVENT:
190 : case NS_SIMPLE_GESTURE_EVENT:
191 : case NS_MOZTOUCH_EVENT:
192 0 : *aButton = static_cast<nsMouseEvent_base*>(mEvent)->button;
193 0 : break;
194 : default:
195 0 : NS_WARNING("Tried to get mouse button for non-mouse event!");
196 0 : *aButton = nsMouseEvent::eLeftButton;
197 0 : break;
198 : }
199 0 : return NS_OK;
200 : }
201 :
202 : NS_IMETHODIMP
203 0 : nsDOMMouseEvent::GetRelatedTarget(nsIDOMEventTarget** aRelatedTarget)
204 : {
205 0 : NS_ENSURE_ARG_POINTER(aRelatedTarget);
206 0 : *aRelatedTarget = nsnull;
207 0 : nsISupports* relatedTarget = nsnull;
208 0 : switch(mEvent->eventStructType)
209 : {
210 : case NS_MOUSE_EVENT:
211 : case NS_MOUSE_SCROLL_EVENT:
212 : case NS_DRAG_EVENT:
213 : case NS_SIMPLE_GESTURE_EVENT:
214 : case NS_MOZTOUCH_EVENT:
215 0 : relatedTarget = static_cast<nsMouseEvent_base*>(mEvent)->relatedTarget;
216 0 : break;
217 : default:
218 0 : break;
219 : }
220 :
221 0 : if (relatedTarget) {
222 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(relatedTarget);
223 0 : if (content && content->IsInNativeAnonymousSubtree() &&
224 0 : !nsContentUtils::CanAccessNativeAnon()) {
225 0 : relatedTarget = content->FindFirstNonNativeAnonymous();
226 0 : if (!relatedTarget) {
227 0 : return NS_OK;
228 : }
229 : }
230 :
231 0 : CallQueryInterface(relatedTarget, aRelatedTarget);
232 : }
233 0 : return NS_OK;
234 : }
235 :
236 0 : NS_METHOD nsDOMMouseEvent::GetScreenX(PRInt32* aScreenX)
237 : {
238 0 : NS_ENSURE_ARG_POINTER(aScreenX);
239 : #ifdef MOZ_TOUCH
240 : *aScreenX = nsDOMEvent::GetScreenCoords(mPresContext,
241 : mEvent,
242 : mEvent->refPoint).x;
243 : #else
244 0 : *aScreenX = GetScreenPoint().x;
245 : #endif
246 0 : return NS_OK;
247 : }
248 :
249 : NS_IMETHODIMP
250 0 : nsDOMMouseEvent::GetScreenY(PRInt32* aScreenY)
251 : {
252 0 : NS_ENSURE_ARG_POINTER(aScreenY);
253 : #ifdef MOZ_TOUCH
254 : *aScreenY = nsDOMEvent::GetScreenCoords(mPresContext,
255 : mEvent,
256 : mEvent->refPoint).y;
257 : #else
258 0 : *aScreenY = GetScreenPoint().y;
259 : #endif
260 0 : return NS_OK;
261 : }
262 :
263 :
264 0 : NS_METHOD nsDOMMouseEvent::GetClientX(PRInt32* aClientX)
265 : {
266 0 : NS_ENSURE_ARG_POINTER(aClientX);
267 : #ifdef MOZ_TOUCH
268 : *aClientX = nsDOMEvent::GetClientCoords(mPresContext,
269 : mEvent,
270 : mEvent->refPoint,
271 : mClientPoint).x;
272 : #else
273 0 : *aClientX = GetClientPoint().x;
274 : #endif
275 0 : return NS_OK;
276 : }
277 :
278 : NS_IMETHODIMP
279 0 : nsDOMMouseEvent::GetClientY(PRInt32* aClientY)
280 : {
281 0 : NS_ENSURE_ARG_POINTER(aClientY);
282 : #ifdef MOZ_TOUCH
283 : *aClientY = nsDOMEvent::GetClientCoords(mPresContext,
284 : mEvent,
285 : mEvent->refPoint,
286 : mClientPoint).y;
287 : #else
288 0 : *aClientY = GetClientPoint().y;
289 : #endif
290 0 : return NS_OK;
291 : }
292 :
293 : NS_IMETHODIMP
294 0 : nsDOMMouseEvent::GetAltKey(bool* aIsDown)
295 : {
296 0 : NS_ENSURE_ARG_POINTER(aIsDown);
297 0 : *aIsDown = ((nsInputEvent*)mEvent)->isAlt;
298 0 : return NS_OK;
299 : }
300 :
301 : NS_IMETHODIMP
302 0 : nsDOMMouseEvent::GetCtrlKey(bool* aIsDown)
303 : {
304 0 : NS_ENSURE_ARG_POINTER(aIsDown);
305 0 : *aIsDown = ((nsInputEvent*)mEvent)->isControl;
306 0 : return NS_OK;
307 : }
308 :
309 : NS_IMETHODIMP
310 0 : nsDOMMouseEvent::GetShiftKey(bool* aIsDown)
311 : {
312 0 : NS_ENSURE_ARG_POINTER(aIsDown);
313 0 : *aIsDown = ((nsInputEvent*)mEvent)->isShift;
314 0 : return NS_OK;
315 : }
316 :
317 : NS_IMETHODIMP
318 0 : nsDOMMouseEvent::GetMetaKey(bool* aIsDown)
319 : {
320 0 : NS_ENSURE_ARG_POINTER(aIsDown);
321 0 : *aIsDown = ((nsInputEvent*)mEvent)->isMeta;
322 0 : return NS_OK;
323 : }
324 :
325 : /* virtual */
326 : nsresult
327 0 : nsDOMMouseEvent::Which(PRUint32* aWhich)
328 : {
329 0 : NS_ENSURE_ARG_POINTER(aWhich);
330 : PRUint16 button;
331 0 : (void) GetButton(&button);
332 0 : *aWhich = button + 1;
333 0 : return NS_OK;
334 : }
335 :
336 : NS_IMETHODIMP
337 0 : nsDOMMouseEvent::GetMozPressure(float* aPressure)
338 : {
339 0 : NS_ENSURE_ARG_POINTER(aPressure);
340 0 : *aPressure = static_cast<nsMouseEvent_base*>(mEvent)->pressure;
341 0 : return NS_OK;
342 : }
343 :
344 : NS_IMETHODIMP
345 0 : nsDOMMouseEvent::GetMozInputSource(PRUint16* aInputSource)
346 : {
347 0 : NS_ENSURE_ARG_POINTER(aInputSource);
348 0 : *aInputSource = static_cast<nsMouseEvent_base*>(mEvent)->inputSource;
349 0 : return NS_OK;
350 : }
351 :
352 2 : nsresult NS_NewDOMMouseEvent(nsIDOMEvent** aInstancePtrResult,
353 : nsPresContext* aPresContext,
354 : nsInputEvent *aEvent)
355 : {
356 2 : nsDOMMouseEvent* it = new nsDOMMouseEvent(aPresContext, aEvent);
357 2 : return CallQueryInterface(it, aInstancePtrResult);
358 : }
|