Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/MouseEvent.h"
7 #include "mozilla/MouseEvents.h"
8 #include "nsContentUtils.h"
9 #include "nsIContent.h"
10 #include "prtime.h"
12 namespace mozilla {
13 namespace dom {
15 MouseEvent::MouseEvent(EventTarget* aOwner,
16 nsPresContext* aPresContext,
17 WidgetMouseEventBase* aEvent)
18 : UIEvent(aOwner, aPresContext,
19 aEvent ? aEvent : new WidgetMouseEvent(false, 0, nullptr,
20 WidgetMouseEvent::eReal))
21 {
22 // There's no way to make this class' ctor allocate an WidgetMouseScrollEvent.
23 // It's not that important, though, since a scroll event is not a real
24 // DOM event.
26 WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
27 if (aEvent) {
28 mEventIsInternal = false;
29 }
30 else {
31 mEventIsInternal = true;
32 mEvent->time = PR_Now();
33 mEvent->refPoint.x = mEvent->refPoint.y = 0;
34 mouseEvent->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
35 }
37 if (mouseEvent) {
38 MOZ_ASSERT(mouseEvent->reason != WidgetMouseEvent::eSynthesized,
39 "Don't dispatch DOM events from synthesized mouse events");
40 mDetail = mouseEvent->clickCount;
41 }
42 }
44 NS_IMPL_ADDREF_INHERITED(MouseEvent, UIEvent)
45 NS_IMPL_RELEASE_INHERITED(MouseEvent, UIEvent)
47 NS_INTERFACE_MAP_BEGIN(MouseEvent)
48 NS_INTERFACE_MAP_ENTRY(nsIDOMMouseEvent)
49 NS_INTERFACE_MAP_END_INHERITING(UIEvent)
51 NS_IMETHODIMP
52 MouseEvent::InitMouseEvent(const nsAString& aType,
53 bool aCanBubble,
54 bool aCancelable,
55 nsIDOMWindow* aView,
56 int32_t aDetail,
57 int32_t aScreenX,
58 int32_t aScreenY,
59 int32_t aClientX,
60 int32_t aClientY,
61 bool aCtrlKey,
62 bool aAltKey,
63 bool aShiftKey,
64 bool aMetaKey,
65 uint16_t aButton,
66 nsIDOMEventTarget* aRelatedTarget)
67 {
68 nsresult rv =
69 UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, aDetail);
70 NS_ENSURE_SUCCESS(rv, rv);
72 switch(mEvent->eventStructType) {
73 case NS_MOUSE_EVENT:
74 case NS_MOUSE_SCROLL_EVENT:
75 case NS_WHEEL_EVENT:
76 case NS_DRAG_EVENT:
77 case NS_POINTER_EVENT:
78 case NS_SIMPLE_GESTURE_EVENT: {
79 WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
80 mouseEventBase->relatedTarget = aRelatedTarget;
81 mouseEventBase->button = aButton;
82 mouseEventBase->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
83 mClientPoint.x = aClientX;
84 mClientPoint.y = aClientY;
85 mouseEventBase->refPoint.x = aScreenX;
86 mouseEventBase->refPoint.y = aScreenY;
88 WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
89 if (mouseEvent) {
90 mouseEvent->clickCount = aDetail;
91 }
92 break;
93 }
94 default:
95 break;
96 }
98 return NS_OK;
99 }
101 nsresult
102 MouseEvent::InitMouseEvent(const nsAString& aType,
103 bool aCanBubble,
104 bool aCancelable,
105 nsIDOMWindow* aView,
106 int32_t aDetail,
107 int32_t aScreenX,
108 int32_t aScreenY,
109 int32_t aClientX,
110 int32_t aClientY,
111 int16_t aButton,
112 nsIDOMEventTarget* aRelatedTarget,
113 const nsAString& aModifiersList)
114 {
115 Modifiers modifiers = ComputeModifierState(aModifiersList);
117 nsresult rv = InitMouseEvent(aType, aCanBubble, aCancelable, aView,
118 aDetail, aScreenX, aScreenY, aClientX, aClientY,
119 (modifiers & MODIFIER_CONTROL) != 0,
120 (modifiers & MODIFIER_ALT) != 0,
121 (modifiers & MODIFIER_SHIFT) != 0,
122 (modifiers & MODIFIER_META) != 0,
123 aButton, aRelatedTarget);
124 NS_ENSURE_SUCCESS(rv, rv);
126 switch(mEvent->eventStructType) {
127 case NS_MOUSE_EVENT:
128 case NS_MOUSE_SCROLL_EVENT:
129 case NS_WHEEL_EVENT:
130 case NS_DRAG_EVENT:
131 case NS_POINTER_EVENT:
132 case NS_SIMPLE_GESTURE_EVENT:
133 mEvent->AsInputEvent()->modifiers = modifiers;
134 return NS_OK;
135 default:
136 MOZ_CRASH("There is no space to store the modifiers");
137 }
138 }
140 already_AddRefed<MouseEvent>
141 MouseEvent::Constructor(const GlobalObject& aGlobal,
142 const nsAString& aType,
143 const MouseEventInit& aParam,
144 ErrorResult& aRv)
145 {
146 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
147 nsRefPtr<MouseEvent> e = new MouseEvent(t, nullptr, nullptr);
148 bool trusted = e->Init(t);
149 e->InitMouseEvent(aType, aParam.mBubbles, aParam.mCancelable,
150 aParam.mView, aParam.mDetail, aParam.mScreenX,
151 aParam.mScreenY, aParam.mClientX, aParam.mClientY,
152 aParam.mCtrlKey, aParam.mAltKey, aParam.mShiftKey,
153 aParam.mMetaKey, aParam.mButton, aParam.mRelatedTarget,
154 aRv);
155 e->SetTrusted(trusted);
157 switch (e->mEvent->eventStructType) {
158 case NS_MOUSE_EVENT:
159 case NS_MOUSE_SCROLL_EVENT:
160 case NS_WHEEL_EVENT:
161 case NS_DRAG_EVENT:
162 case NS_POINTER_EVENT:
163 case NS_SIMPLE_GESTURE_EVENT:
164 e->mEvent->AsMouseEventBase()->buttons = aParam.mButtons;
165 break;
166 default:
167 break;
168 }
170 return e.forget();
171 }
173 NS_IMETHODIMP
174 MouseEvent::InitNSMouseEvent(const nsAString& aType,
175 bool aCanBubble,
176 bool aCancelable,
177 nsIDOMWindow* aView,
178 int32_t aDetail,
179 int32_t aScreenX,
180 int32_t aScreenY,
181 int32_t aClientX,
182 int32_t aClientY,
183 bool aCtrlKey,
184 bool aAltKey,
185 bool aShiftKey,
186 bool aMetaKey,
187 uint16_t aButton,
188 nsIDOMEventTarget* aRelatedTarget,
189 float aPressure,
190 uint16_t aInputSource)
191 {
192 nsresult rv = MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
193 aView, aDetail, aScreenX, aScreenY,
194 aClientX, aClientY,
195 aCtrlKey, aAltKey, aShiftKey,
196 aMetaKey, aButton, aRelatedTarget);
197 NS_ENSURE_SUCCESS(rv, rv);
199 WidgetMouseEventBase* mouseEventBase = mEvent->AsMouseEventBase();
200 mouseEventBase->pressure = aPressure;
201 mouseEventBase->inputSource = aInputSource;
202 return NS_OK;
203 }
205 NS_IMETHODIMP
206 MouseEvent::GetButton(int16_t* aButton)
207 {
208 NS_ENSURE_ARG_POINTER(aButton);
209 *aButton = Button();
210 return NS_OK;
211 }
213 int16_t
214 MouseEvent::Button()
215 {
216 switch(mEvent->eventStructType)
217 {
218 case NS_MOUSE_EVENT:
219 case NS_MOUSE_SCROLL_EVENT:
220 case NS_WHEEL_EVENT:
221 case NS_DRAG_EVENT:
222 case NS_POINTER_EVENT:
223 case NS_SIMPLE_GESTURE_EVENT:
224 return mEvent->AsMouseEventBase()->button;
225 default:
226 NS_WARNING("Tried to get mouse button for non-mouse event!");
227 return WidgetMouseEvent::eLeftButton;
228 }
229 }
231 NS_IMETHODIMP
232 MouseEvent::GetButtons(uint16_t* aButtons)
233 {
234 NS_ENSURE_ARG_POINTER(aButtons);
235 *aButtons = Buttons();
236 return NS_OK;
237 }
239 uint16_t
240 MouseEvent::Buttons()
241 {
242 switch(mEvent->eventStructType)
243 {
244 case NS_MOUSE_EVENT:
245 case NS_MOUSE_SCROLL_EVENT:
246 case NS_WHEEL_EVENT:
247 case NS_DRAG_EVENT:
248 case NS_POINTER_EVENT:
249 case NS_SIMPLE_GESTURE_EVENT:
250 return mEvent->AsMouseEventBase()->buttons;
251 default:
252 MOZ_CRASH("Tried to get mouse buttons for non-mouse event!");
253 }
254 }
256 NS_IMETHODIMP
257 MouseEvent::GetRelatedTarget(nsIDOMEventTarget** aRelatedTarget)
258 {
259 NS_ENSURE_ARG_POINTER(aRelatedTarget);
260 *aRelatedTarget = GetRelatedTarget().take();
261 return NS_OK;
262 }
264 already_AddRefed<EventTarget>
265 MouseEvent::GetRelatedTarget()
266 {
267 nsCOMPtr<EventTarget> relatedTarget;
268 switch(mEvent->eventStructType)
269 {
270 case NS_MOUSE_EVENT:
271 case NS_MOUSE_SCROLL_EVENT:
272 case NS_WHEEL_EVENT:
273 case NS_DRAG_EVENT:
274 case NS_POINTER_EVENT:
275 case NS_SIMPLE_GESTURE_EVENT:
276 relatedTarget =
277 do_QueryInterface(mEvent->AsMouseEventBase()->relatedTarget);
278 break;
279 default:
280 break;
281 }
283 if (relatedTarget) {
284 nsCOMPtr<nsIContent> content = do_QueryInterface(relatedTarget);
285 if (content && content->ChromeOnlyAccess() &&
286 !nsContentUtils::CanAccessNativeAnon()) {
287 relatedTarget = do_QueryInterface(content->FindFirstNonChromeOnlyAccessContent());
288 }
290 if (relatedTarget) {
291 relatedTarget = relatedTarget->GetTargetForDOMEvent();
292 }
293 return relatedTarget.forget();
294 }
295 return nullptr;
296 }
298 NS_IMETHODIMP
299 MouseEvent::GetMozMovementX(int32_t* aMovementX)
300 {
301 NS_ENSURE_ARG_POINTER(aMovementX);
302 *aMovementX = MozMovementX();
304 return NS_OK;
305 }
307 NS_IMETHODIMP
308 MouseEvent::GetMozMovementY(int32_t* aMovementY)
309 {
310 NS_ENSURE_ARG_POINTER(aMovementY);
311 *aMovementY = MozMovementY();
313 return NS_OK;
314 }
316 NS_IMETHODIMP
317 MouseEvent::GetScreenX(int32_t* aScreenX)
318 {
319 NS_ENSURE_ARG_POINTER(aScreenX);
320 *aScreenX = ScreenX();
321 return NS_OK;
322 }
324 int32_t
325 MouseEvent::ScreenX()
326 {
327 return Event::GetScreenCoords(mPresContext, mEvent, mEvent->refPoint).x;
328 }
330 NS_IMETHODIMP
331 MouseEvent::GetScreenY(int32_t* aScreenY)
332 {
333 NS_ENSURE_ARG_POINTER(aScreenY);
334 *aScreenY = ScreenY();
335 return NS_OK;
336 }
338 int32_t
339 MouseEvent::ScreenY()
340 {
341 return Event::GetScreenCoords(mPresContext, mEvent, mEvent->refPoint).y;
342 }
345 NS_IMETHODIMP
346 MouseEvent::GetClientX(int32_t* aClientX)
347 {
348 NS_ENSURE_ARG_POINTER(aClientX);
349 *aClientX = ClientX();
350 return NS_OK;
351 }
353 int32_t
354 MouseEvent::ClientX()
355 {
356 return Event::GetClientCoords(mPresContext, mEvent, mEvent->refPoint,
357 mClientPoint).x;
358 }
360 NS_IMETHODIMP
361 MouseEvent::GetClientY(int32_t* aClientY)
362 {
363 NS_ENSURE_ARG_POINTER(aClientY);
364 *aClientY = ClientY();
365 return NS_OK;
366 }
368 int32_t
369 MouseEvent::ClientY()
370 {
371 return Event::GetClientCoords(mPresContext, mEvent, mEvent->refPoint,
372 mClientPoint).y;
373 }
375 bool
376 MouseEvent::AltKey()
377 {
378 return mEvent->AsInputEvent()->IsAlt();
379 }
381 NS_IMETHODIMP
382 MouseEvent::GetAltKey(bool* aIsDown)
383 {
384 NS_ENSURE_ARG_POINTER(aIsDown);
385 *aIsDown = AltKey();
386 return NS_OK;
387 }
389 bool
390 MouseEvent::CtrlKey()
391 {
392 return mEvent->AsInputEvent()->IsControl();
393 }
395 NS_IMETHODIMP
396 MouseEvent::GetCtrlKey(bool* aIsDown)
397 {
398 NS_ENSURE_ARG_POINTER(aIsDown);
399 *aIsDown = CtrlKey();
400 return NS_OK;
401 }
403 bool
404 MouseEvent::ShiftKey()
405 {
406 return mEvent->AsInputEvent()->IsShift();
407 }
409 NS_IMETHODIMP
410 MouseEvent::GetShiftKey(bool* aIsDown)
411 {
412 NS_ENSURE_ARG_POINTER(aIsDown);
413 *aIsDown = ShiftKey();
414 return NS_OK;
415 }
417 bool
418 MouseEvent::MetaKey()
419 {
420 return mEvent->AsInputEvent()->IsMeta();
421 }
423 NS_IMETHODIMP
424 MouseEvent::GetMetaKey(bool* aIsDown)
425 {
426 NS_ENSURE_ARG_POINTER(aIsDown);
427 *aIsDown = MetaKey();
428 return NS_OK;
429 }
431 NS_IMETHODIMP
432 MouseEvent::GetModifierState(const nsAString& aKey,
433 bool* aState)
434 {
435 NS_ENSURE_ARG_POINTER(aState);
437 *aState = GetModifierState(aKey);
438 return NS_OK;
439 }
441 float
442 MouseEvent::MozPressure() const
443 {
444 return mEvent->AsMouseEventBase()->pressure;
445 }
447 NS_IMETHODIMP
448 MouseEvent::GetMozPressure(float* aPressure)
449 {
450 NS_ENSURE_ARG_POINTER(aPressure);
451 *aPressure = MozPressure();
452 return NS_OK;
453 }
455 uint16_t
456 MouseEvent::MozInputSource() const
457 {
458 return mEvent->AsMouseEventBase()->inputSource;
459 }
461 NS_IMETHODIMP
462 MouseEvent::GetMozInputSource(uint16_t* aInputSource)
463 {
464 NS_ENSURE_ARG_POINTER(aInputSource);
465 *aInputSource = MozInputSource();
466 return NS_OK;
467 }
469 } // namespace dom
470 } // namespace mozilla
472 using namespace mozilla;
473 using namespace mozilla::dom;
475 nsresult
476 NS_NewDOMMouseEvent(nsIDOMEvent** aInstancePtrResult,
477 EventTarget* aOwner,
478 nsPresContext* aPresContext,
479 WidgetMouseEvent* aEvent)
480 {
481 MouseEvent* it = new MouseEvent(aOwner, aPresContext, aEvent);
482 NS_ADDREF(it);
483 *aInstancePtrResult = static_cast<Event*>(it);
484 return NS_OK;
485 }