Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / widget / MouseEvents.h
blobc49f4bbb5c420628c4ff2456d30faccb1c6b3def
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__
9 #include <stdint.h>
11 #include "mozilla/BasicEvents.h"
12 #include "mozilla/MathAlgorithms.h"
13 #include "nsCOMPtr.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,
26 // The event is exit
27 nsDragDropEventStatus_eDragExited,
28 // The event is drop
29 nsDragDropEventStatus_eDrop
32 namespace mozilla {
34 namespace dom {
35 class PBrowserParent;
36 class PBrowserChild;
37 } // namespace dom
39 /******************************************************************************
40 * mozilla::WidgetMouseEventBase
41 ******************************************************************************/
43 class WidgetMouseEventBase : public WidgetInputEvent
45 private:
46 friend class dom::PBrowserParent;
47 friend class dom::PBrowserChild;
49 protected:
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)
62 public:
63 virtual WidgetMouseEventBase* AsMouseEventBase() MOZ_OVERRIDE { return this; }
65 /// The possible related target
66 nsCOMPtr<nsISupports> relatedTarget;
68 enum buttonType
70 eLeftButton = 0,
71 eMiddleButton = 1,
72 eRightButton = 2
74 // Pressed button ID of mousedown or mouseup event.
75 // This is set only when pressing a button causes the event.
76 int16_t button;
78 enum buttonsFlag {
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.
87 e5thButtonFlag = 0x10
90 // Flags of all pressed buttons at the event fired.
91 // This is set at any mouse event, don't be confused with |button|.
92 int16_t buttons;
94 // Finger or touch pressure of event. It ranges between 0.0 and 1.0.
95 float pressure;
97 // Possible values at nsIDOMMouseEvent
98 uint16_t inputSource;
100 void AssignMouseEventBaseData(const WidgetMouseEventBase& aEvent,
101 bool aCopyTargets)
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
127 private:
128 friend class mozilla::dom::PBrowserParent;
129 friend class mozilla::dom::PBrowserChild;
131 public:
132 enum reasonType
134 eReal,
135 eSynthesized
138 enum contextType
140 eNormal,
141 eContextMenuKey
144 enum exitType
146 eChild,
147 eTopLevel
150 protected:
151 WidgetMouseEvent()
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)
161 switch (aMessage) {
162 case NS_MOUSE_MOVE:
163 mFlags.mCancelable = false;
164 break;
165 case NS_MOUSEENTER:
166 case NS_MOUSELEAVE:
167 mFlags.mBubbles = false;
168 mFlags.mCancelable = false;
169 break;
170 default:
171 break;
175 public:
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)
184 switch (aMessage) {
185 case NS_MOUSE_MOVE:
186 mFlags.mCancelable = false;
187 break;
188 case NS_MOUSEENTER:
189 case NS_MOUSELEAVE:
190 mFlags.mBubbles = false;
191 mFlags.mCancelable = false;
192 break;
193 case NS_CONTEXTMENU:
194 button = (context == eNormal) ? eRightButton : eLeftButton;
195 break;
196 default:
197 break;
201 #ifdef DEBUG
202 virtual ~WidgetMouseEvent()
204 NS_WARN_IF_FALSE(message != NS_CONTEXTMENU ||
205 button ==
206 ((context == eNormal) ? eRightButton : eLeftButton),
207 "Wrong button set to NS_CONTEXTMENU event?");
209 #endif
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;
219 exitType exit;
221 /// The number of mouse clicks.
222 uint32_t clickCount;
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
248 public:
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)
255 mFlags.mCancelable =
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.
265 bool userCancelled;
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
291 private:
292 WidgetMouseScrollEvent()
296 public:
297 virtual WidgetMouseScrollEvent* AsMouseScrollEvent() MOZ_OVERRIDE
299 return this;
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
314 // amount in pixels.
315 int32_t delta;
317 // If this is true, it may cause to scroll horizontally.
318 // Otherwise, vertically.
319 bool isHorizontal;
321 void AssignMouseScrollEventData(const WidgetMouseScrollEvent& aEvent,
322 bool aCopyTargets)
324 AssignMouseEventBaseData(aEvent, aCopyTargets);
326 delta = aEvent.delta;
327 isHorizontal = aEvent.isHorizontal;
331 /******************************************************************************
332 * mozilla::WidgetWheelEvent
333 ******************************************************************************/
335 class WidgetWheelEvent : public WidgetMouseEventBase
337 private:
338 friend class mozilla::dom::PBrowserParent;
339 friend class mozilla::dom::PBrowserChild;
341 WidgetWheelEvent()
345 public:
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.
363 double deltaX;
364 double deltaY;
365 double deltaZ;
367 // Should be one of nsIDOMWheelEvent::DOM_DELTA_*
368 uint32_t deltaMode;
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.
377 bool isMomentum;
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) {
396 return 0;
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;
412 // Scroll type
413 // The default value is SCROLL_DEFAULT, which means nsEventStateManager will
414 // select preferred scroll type automatically.
415 enum ScrollType
417 SCROLL_DEFAULT,
418 SCROLL_SYNCHRONOUSLY,
419 SCROLL_ASYNCHRONOUSELY,
420 SCROLL_SMOOTHLY
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
439 // overscrolled.
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;
471 WidgetPointerEvent()
475 public:
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)
480 , pointerId(0)
481 , width(0)
482 , height(0)
483 , tiltX(0)
484 , tiltY(0)
485 , isPrimary(true)
489 WidgetPointerEvent(const WidgetMouseEvent& aEvent)
490 : WidgetMouseEvent(aEvent)
491 , pointerId(0)
492 , width(0)
493 , height(0)
494 , tiltX(0)
495 , tiltY(0)
496 , isPrimary(true)
498 eventStructType = NS_POINTER_EVENT;
501 WidgetPointerEvent(bool aIsTrusted, uint32_t aMsg, nsIWidget* w,
502 uint32_t aPointerId,
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)
507 , width(aWidth)
508 , height(aHeight)
509 , tiltX(aTiltX)
510 , tiltY(aTiltY)
511 , isPrimary(aIsPrimary)
515 uint32_t pointerId;
516 uint32_t width;
517 uint32_t height;
518 uint32_t tiltX;
519 uint32_t tiltY;
520 bool isPrimary;
523 } // namespace mozilla
525 #endif // mozilla_MouseEvents_h__