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 #ifndef mozilla_MouseEvents_h__
7 #define mozilla_MouseEvents_h__
11 #include "mozilla/BasicEvents.h"
12 #include "mozilla/MathAlgorithms.h"
14 #include "nsIDOMDataTransfer.h"
15 #include "nsIDOMMouseEvent.h"
16 #include "nsIDOMWheelEvent.h"
18 /******************************************************************************
19 * nsDragDropEventStatus
20 ******************************************************************************/
22 enum nsDragDropEventStatus
24 // The event is a enter
25 nsDragDropEventStatus_eDragEntered
,
27 nsDragDropEventStatus_eDragExited
,
29 nsDragDropEventStatus_eDrop
39 /******************************************************************************
40 * mozilla::WidgetMouseEventBase
41 ******************************************************************************/
43 class WidgetMouseEventBase
: public WidgetInputEvent
46 friend class dom::PBrowserParent
;
47 friend class dom::PBrowserChild
;
50 WidgetMouseEventBase()
54 WidgetMouseEventBase(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
,
55 nsEventStructType aStructType
) :
56 WidgetInputEvent(aIsTrusted
, aMessage
, aWidget
, aStructType
),
57 button(0), buttons(0), pressure(0),
58 inputSource(nsIDOMMouseEvent::MOZ_SOURCE_MOUSE
)
63 virtual WidgetMouseEventBase
* AsMouseEventBase() MOZ_OVERRIDE
{ return this; }
65 /// The possible related target
66 nsCOMPtr
<nsISupports
> relatedTarget
;
74 // Pressed button ID of mousedown or mouseup event.
75 // This is set only when pressing a button causes the event.
79 eLeftButtonFlag
= 0x01,
80 eRightButtonFlag
= 0x02,
81 eMiddleButtonFlag
= 0x04,
82 // typicall, "back" button being left side of 5-button
83 // mice, see "buttons" attribute document of DOM3 Events.
84 e4thButtonFlag
= 0x08,
85 // typicall, "forward" button being right side of 5-button
86 // mice, see "buttons" attribute document of DOM3 Events.
90 // Flags of all pressed buttons at the event fired.
91 // This is set at any mouse event, don't be confused with |button|.
94 // Finger or touch pressure of event. It ranges between 0.0 and 1.0.
97 // Possible values at nsIDOMMouseEvent
100 void AssignMouseEventBaseData(const WidgetMouseEventBase
& aEvent
,
103 AssignInputEventData(aEvent
, aCopyTargets
);
105 relatedTarget
= aCopyTargets
? aEvent
.relatedTarget
: nullptr;
106 button
= aEvent
.button
;
107 buttons
= aEvent
.buttons
;
108 pressure
= aEvent
.pressure
;
109 inputSource
= aEvent
.inputSource
;
113 * Returns true if left click event.
115 bool IsLeftClickEvent() const
117 return message
== NS_MOUSE_CLICK
&& button
== eLeftButton
;
121 /******************************************************************************
122 * mozilla::WidgetMouseEvent
123 ******************************************************************************/
125 class WidgetMouseEvent
: public WidgetMouseEventBase
128 friend class mozilla::dom::PBrowserParent
;
129 friend class mozilla::dom::PBrowserChild
;
155 WidgetMouseEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
,
156 nsEventStructType aStructType
, reasonType aReason
) :
157 WidgetMouseEventBase(aIsTrusted
, aMessage
, aWidget
, aStructType
),
158 acceptActivation(false), ignoreRootScrollFrame(false),
159 reason(aReason
), context(eNormal
), exit(eChild
), clickCount(0)
163 mFlags
.mCancelable
= false;
167 mFlags
.mBubbles
= false;
168 mFlags
.mCancelable
= false;
176 virtual WidgetMouseEvent
* AsMouseEvent() MOZ_OVERRIDE
{ return this; }
178 WidgetMouseEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
,
179 reasonType aReason
, contextType aContext
= eNormal
) :
180 WidgetMouseEventBase(aIsTrusted
, aMessage
, aWidget
, NS_MOUSE_EVENT
),
181 acceptActivation(false), ignoreRootScrollFrame(false),
182 reason(aReason
), context(aContext
), exit(eChild
), clickCount(0)
186 mFlags
.mCancelable
= false;
190 mFlags
.mBubbles
= false;
191 mFlags
.mCancelable
= false;
194 button
= (context
== eNormal
) ? eRightButton
: eLeftButton
;
202 virtual ~WidgetMouseEvent()
204 NS_WARN_IF_FALSE(message
!= NS_CONTEXTMENU
||
206 ((context
== eNormal
) ? eRightButton
: eLeftButton
),
207 "Wrong button set to NS_CONTEXTMENU event?");
211 // Special return code for MOUSE_ACTIVATE to signal.
212 // If the target accepts activation (1), or denies it (0).
213 bool acceptActivation
;
214 // Whether the event should ignore scroll frame bounds during dispatch.
215 bool ignoreRootScrollFrame
;
217 reasonType reason
: 4;
218 contextType context
: 4;
221 /// The number of mouse clicks.
224 void AssignMouseEventData(const WidgetMouseEvent
& aEvent
, bool aCopyTargets
)
226 AssignMouseEventBaseData(aEvent
, aCopyTargets
);
228 acceptActivation
= aEvent
.acceptActivation
;
229 ignoreRootScrollFrame
= aEvent
.ignoreRootScrollFrame
;
230 clickCount
= aEvent
.clickCount
;
234 * Returns true if the event is a context menu event caused by key.
236 bool IsContextMenuKeyEvent() const
238 return message
== NS_CONTEXTMENU
&& context
== eContextMenuKey
;
242 /******************************************************************************
243 * mozilla::WidgetDragEvent
244 ******************************************************************************/
246 class WidgetDragEvent
: public WidgetMouseEvent
249 virtual WidgetDragEvent
* AsDragEvent() MOZ_OVERRIDE
{ return this; }
251 WidgetDragEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
252 WidgetMouseEvent(aIsTrusted
, aMessage
, aWidget
, NS_DRAG_EVENT
, eReal
),
253 userCancelled(false), mDefaultPreventedOnContent(false)
256 (aMessage
!= NS_DRAGDROP_EXIT_SYNTH
&&
257 aMessage
!= NS_DRAGDROP_LEAVE_SYNTH
&&
258 aMessage
!= NS_DRAGDROP_END
);
261 // The dragging data.
262 nsCOMPtr
<nsIDOMDataTransfer
> dataTransfer
;
264 // If this is true, user has cancelled the drag operation.
266 // If this is true, the drag event's preventDefault() is called on content.
267 bool mDefaultPreventedOnContent
;
269 // XXX Not tested by test_assign_event_data.html
270 void AssignDragEventData(const WidgetDragEvent
& aEvent
, bool aCopyTargets
)
272 AssignMouseEventData(aEvent
, aCopyTargets
);
274 dataTransfer
= aEvent
.dataTransfer
;
275 // XXX userCancelled isn't copied, is this instentionally?
276 userCancelled
= false;
277 mDefaultPreventedOnContent
= aEvent
.mDefaultPreventedOnContent
;
281 /******************************************************************************
282 * mozilla::WidgetMouseScrollEvent
284 * This is used for legacy DOM mouse scroll events, i.e.,
285 * DOMMouseScroll and MozMousePixelScroll event. These events are NOT hanbled
286 * by ESM even if widget dispatches them. Use new WidgetWheelEvent instead.
287 ******************************************************************************/
289 class WidgetMouseScrollEvent
: public WidgetMouseEventBase
292 WidgetMouseScrollEvent()
297 virtual WidgetMouseScrollEvent
* AsMouseScrollEvent() MOZ_OVERRIDE
302 WidgetMouseScrollEvent(bool aIsTrusted
, uint32_t aMessage
,
303 nsIWidget
* aWidget
) :
304 WidgetMouseEventBase(aIsTrusted
, aMessage
, aWidget
, NS_MOUSE_SCROLL_EVENT
),
305 delta(0), isHorizontal(false)
309 // The delta value of mouse scroll event.
310 // If the event message is NS_MOUSE_SCROLL, the value indicates scroll amount
311 // in lines. However, if the value is nsIDOMUIEvent::SCROLL_PAGE_UP or
312 // nsIDOMUIEvent::SCROLL_PAGE_DOWN, the value inducates one page scroll.
313 // If the event message is NS_MOUSE_PIXEL_SCROLL, the value indicates scroll
317 // If this is true, it may cause to scroll horizontally.
318 // Otherwise, vertically.
321 void AssignMouseScrollEventData(const WidgetMouseScrollEvent
& aEvent
,
324 AssignMouseEventBaseData(aEvent
, aCopyTargets
);
326 delta
= aEvent
.delta
;
327 isHorizontal
= aEvent
.isHorizontal
;
331 /******************************************************************************
332 * mozilla::WidgetWheelEvent
333 ******************************************************************************/
335 class WidgetWheelEvent
: public WidgetMouseEventBase
338 friend class mozilla::dom::PBrowserParent
;
339 friend class mozilla::dom::PBrowserChild
;
346 virtual WidgetWheelEvent
* AsWheelEvent() MOZ_OVERRIDE
{ return this; }
348 WidgetWheelEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
349 WidgetMouseEventBase(aIsTrusted
, aMessage
, aWidget
, NS_WHEEL_EVENT
),
350 deltaX(0.0), deltaY(0.0), deltaZ(0.0),
351 deltaMode(nsIDOMWheelEvent::DOM_DELTA_PIXEL
),
352 customizedByUserPrefs(false), isMomentum(false), isPixelOnlyDevice(false),
353 lineOrPageDeltaX(0), lineOrPageDeltaY(0), scrollType(SCROLL_DEFAULT
),
354 overflowDeltaX(0.0), overflowDeltaY(0.0),
355 mViewPortIsOverscrolled(false)
359 // NOTE: deltaX, deltaY and deltaZ may be customized by
360 // mousewheel.*.delta_multiplier_* prefs which are applied by
361 // nsEventStateManager. So, after widget dispatches this event,
362 // these delta values may have different values than before.
367 // Should be one of nsIDOMWheelEvent::DOM_DELTA_*
370 // Following members are for internal use only, not for DOM event.
372 // If the delta values are computed from prefs, this value is true.
373 // Otherwise, i.e., they are computed from native events, false.
374 bool customizedByUserPrefs
;
376 // true if the event is caused by momentum.
379 // If device event handlers don't know when they should set lineOrPageDeltaX
380 // and lineOrPageDeltaY, this is true. Otherwise, false.
381 // If isPixelOnlyDevice is true, ESM will generate NS_MOUSE_SCROLL events
382 // when accumulated pixel delta values reach a line height.
383 bool isPixelOnlyDevice
;
385 // If widget sets lineOrPageDelta, nsEventStateManager will dispatch
386 // NS_MOUSE_SCROLL event for compatibility. Note that the delta value means
387 // pages if the deltaMode is DOM_DELTA_PAGE, otherwise, lines.
388 int32_t lineOrPageDeltaX
;
389 int32_t lineOrPageDeltaY
;
391 // When the default action for an wheel event is moving history or zooming,
392 // need to chose a delta value for doing it.
393 int32_t GetPreferredIntDelta()
395 if (!lineOrPageDeltaX
&& !lineOrPageDeltaY
) {
398 if (lineOrPageDeltaY
&& !lineOrPageDeltaX
) {
399 return lineOrPageDeltaY
;
401 if (lineOrPageDeltaX
&& !lineOrPageDeltaY
) {
402 return lineOrPageDeltaX
;
404 if ((lineOrPageDeltaX
< 0 && lineOrPageDeltaY
> 0) ||
405 (lineOrPageDeltaX
> 0 && lineOrPageDeltaY
< 0)) {
406 return 0; // We cannot guess the answer in this case.
408 return (Abs(lineOrPageDeltaX
) > Abs(lineOrPageDeltaY
)) ?
409 lineOrPageDeltaX
: lineOrPageDeltaY
;
413 // The default value is SCROLL_DEFAULT, which means nsEventStateManager will
414 // select preferred scroll type automatically.
418 SCROLL_SYNCHRONOUSLY
,
419 SCROLL_ASYNCHRONOUSELY
,
422 ScrollType scrollType
;
424 // overflowed delta values for scroll, these values are set by
425 // nsEventStateManger. If the default action of the wheel event isn't scroll,
426 // these values always zero. Otherwise, remaning delta values which are
427 // not used by scroll are set.
428 // NOTE: deltaX, deltaY and deltaZ may be modified by nsEventStateManager.
429 // However, overflowDeltaX and overflowDeltaY indicate unused original
430 // delta values which are not applied the delta_multiplier prefs.
431 // So, if widget wanted to know the actual direction to be scrolled,
432 // it would need to check the deltaX and deltaY.
433 double overflowDeltaX
;
434 double overflowDeltaY
;
436 // Whether or not the parent of the currently overscrolled frame is the
437 // ViewPort. This is false in situations when an element on the page is being
438 // overscrolled (such as a text field), but true when the 'page' is being
440 bool mViewPortIsOverscrolled
;
442 void AssignWheelEventData(const WidgetWheelEvent
& aEvent
, bool aCopyTargets
)
444 AssignMouseEventBaseData(aEvent
, aCopyTargets
);
446 deltaX
= aEvent
.deltaX
;
447 deltaY
= aEvent
.deltaY
;
448 deltaZ
= aEvent
.deltaZ
;
449 deltaMode
= aEvent
.deltaMode
;
450 customizedByUserPrefs
= aEvent
.customizedByUserPrefs
;
451 isMomentum
= aEvent
.isMomentum
;
452 isPixelOnlyDevice
= aEvent
.isPixelOnlyDevice
;
453 lineOrPageDeltaX
= aEvent
.lineOrPageDeltaX
;
454 lineOrPageDeltaY
= aEvent
.lineOrPageDeltaY
;
455 scrollType
= aEvent
.scrollType
;
456 overflowDeltaX
= aEvent
.overflowDeltaX
;
457 overflowDeltaY
= aEvent
.overflowDeltaY
;
458 mViewPortIsOverscrolled
= aEvent
.mViewPortIsOverscrolled
;
462 /******************************************************************************
463 * mozilla::WidgetPointerEvent
464 ******************************************************************************/
466 class WidgetPointerEvent
: public WidgetMouseEvent
468 friend class mozilla::dom::PBrowserParent
;
469 friend class mozilla::dom::PBrowserChild
;
476 virtual WidgetPointerEvent
* AsPointerEvent() MOZ_OVERRIDE
{ return this; }
478 WidgetPointerEvent(bool aIsTrusted
, uint32_t aMsg
, nsIWidget
* w
)
479 : WidgetMouseEvent(aIsTrusted
, aMsg
, w
, NS_POINTER_EVENT
, eReal
)
489 WidgetPointerEvent(const WidgetMouseEvent
& aEvent
)
490 : WidgetMouseEvent(aEvent
)
498 eventStructType
= NS_POINTER_EVENT
;
501 WidgetPointerEvent(bool aIsTrusted
, uint32_t aMsg
, nsIWidget
* w
,
503 uint32_t aWidth
, uint32_t aHeight
,
504 uint32_t aTiltX
, uint32_t aTiltY
, bool aIsPrimary
)
505 : WidgetMouseEvent(aIsTrusted
, aMsg
, w
, NS_POINTER_EVENT
, eReal
)
506 , pointerId(aPointerId
)
511 , isPrimary(aIsPrimary
)
523 } // namespace mozilla
525 #endif // mozilla_MouseEvents_h__