|
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/. */ |
|
5 |
|
6 #ifndef mozilla_dom_UIEvent_h_ |
|
7 #define mozilla_dom_UIEvent_h_ |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 #include "mozilla/dom/Event.h" |
|
11 #include "mozilla/dom/UIEventBinding.h" |
|
12 #include "nsDeviceContext.h" |
|
13 #include "nsIDOMUIEvent.h" |
|
14 #include "nsLayoutUtils.h" |
|
15 #include "nsPresContext.h" |
|
16 |
|
17 class nsINode; |
|
18 |
|
19 namespace mozilla { |
|
20 namespace dom { |
|
21 |
|
22 class UIEvent : public Event, |
|
23 public nsIDOMUIEvent |
|
24 { |
|
25 public: |
|
26 UIEvent(EventTarget* aOwner, |
|
27 nsPresContext* aPresContext, |
|
28 WidgetGUIEvent* aEvent); |
|
29 |
|
30 NS_DECL_ISUPPORTS_INHERITED |
|
31 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(UIEvent, Event) |
|
32 |
|
33 // nsIDOMUIEvent Interface |
|
34 NS_DECL_NSIDOMUIEVENT |
|
35 |
|
36 // Forward to Event |
|
37 NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION |
|
38 NS_IMETHOD DuplicatePrivateData() MOZ_OVERRIDE; |
|
39 NS_IMETHOD_(void) Serialize(IPC::Message* aMsg, bool aSerializeInterfaceType) MOZ_OVERRIDE; |
|
40 NS_IMETHOD_(bool) Deserialize(const IPC::Message* aMsg, void** aIter) MOZ_OVERRIDE; |
|
41 |
|
42 static nsIntPoint CalculateScreenPoint(nsPresContext* aPresContext, |
|
43 WidgetEvent* aEvent) |
|
44 { |
|
45 if (!aEvent || |
|
46 (aEvent->eventStructType != NS_MOUSE_EVENT && |
|
47 aEvent->eventStructType != NS_MOUSE_SCROLL_EVENT && |
|
48 aEvent->eventStructType != NS_WHEEL_EVENT && |
|
49 aEvent->eventStructType != NS_DRAG_EVENT && |
|
50 aEvent->eventStructType != NS_POINTER_EVENT && |
|
51 aEvent->eventStructType != NS_SIMPLE_GESTURE_EVENT)) { |
|
52 return nsIntPoint(0, 0); |
|
53 } |
|
54 |
|
55 WidgetGUIEvent* event = aEvent->AsGUIEvent(); |
|
56 if (!event->widget) { |
|
57 return LayoutDeviceIntPoint::ToUntyped(aEvent->refPoint); |
|
58 } |
|
59 |
|
60 LayoutDeviceIntPoint offset = aEvent->refPoint + |
|
61 LayoutDeviceIntPoint::FromUntyped(event->widget->WidgetToScreenOffset()); |
|
62 nscoord factor = |
|
63 aPresContext->DeviceContext()->UnscaledAppUnitsPerDevPixel(); |
|
64 return nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(offset.x * factor), |
|
65 nsPresContext::AppUnitsToIntCSSPixels(offset.y * factor)); |
|
66 } |
|
67 |
|
68 static CSSIntPoint CalculateClientPoint(nsPresContext* aPresContext, |
|
69 WidgetEvent* aEvent, |
|
70 CSSIntPoint* aDefaultClientPoint) |
|
71 { |
|
72 if (!aEvent || |
|
73 (aEvent->eventStructType != NS_MOUSE_EVENT && |
|
74 aEvent->eventStructType != NS_MOUSE_SCROLL_EVENT && |
|
75 aEvent->eventStructType != NS_WHEEL_EVENT && |
|
76 aEvent->eventStructType != NS_DRAG_EVENT && |
|
77 aEvent->eventStructType != NS_POINTER_EVENT && |
|
78 aEvent->eventStructType != NS_SIMPLE_GESTURE_EVENT) || |
|
79 !aPresContext || |
|
80 !aEvent->AsGUIEvent()->widget) { |
|
81 return aDefaultClientPoint |
|
82 ? *aDefaultClientPoint |
|
83 : CSSIntPoint(0, 0); |
|
84 } |
|
85 |
|
86 nsIPresShell* shell = aPresContext->GetPresShell(); |
|
87 if (!shell) { |
|
88 return CSSIntPoint(0, 0); |
|
89 } |
|
90 nsIFrame* rootFrame = shell->GetRootFrame(); |
|
91 if (!rootFrame) { |
|
92 return CSSIntPoint(0, 0); |
|
93 } |
|
94 nsPoint pt = |
|
95 nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, rootFrame); |
|
96 |
|
97 return CSSIntPoint::FromAppUnitsRounded(pt); |
|
98 } |
|
99 |
|
100 static already_AddRefed<UIEvent> Constructor(const GlobalObject& aGlobal, |
|
101 const nsAString& aType, |
|
102 const UIEventInit& aParam, |
|
103 ErrorResult& aRv); |
|
104 |
|
105 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE |
|
106 { |
|
107 return UIEventBinding::Wrap(aCx, this); |
|
108 } |
|
109 |
|
110 nsIDOMWindow* GetView() const |
|
111 { |
|
112 return mView; |
|
113 } |
|
114 |
|
115 int32_t Detail() const |
|
116 { |
|
117 return mDetail; |
|
118 } |
|
119 |
|
120 int32_t LayerX() const |
|
121 { |
|
122 return GetLayerPoint().x; |
|
123 } |
|
124 |
|
125 int32_t LayerY() const |
|
126 { |
|
127 return GetLayerPoint().y; |
|
128 } |
|
129 |
|
130 int32_t PageX() const; |
|
131 int32_t PageY() const; |
|
132 |
|
133 virtual uint32_t Which() |
|
134 { |
|
135 MOZ_ASSERT(mEvent->eventStructType != NS_KEY_EVENT, |
|
136 "Key events should override Which()"); |
|
137 MOZ_ASSERT(mEvent->eventStructType != NS_MOUSE_EVENT, |
|
138 "Mouse events should override Which()"); |
|
139 return 0; |
|
140 } |
|
141 |
|
142 already_AddRefed<nsINode> GetRangeParent(); |
|
143 |
|
144 int32_t RangeOffset() const; |
|
145 |
|
146 bool CancelBubble() const |
|
147 { |
|
148 return mEvent->mFlags.mPropagationStopped; |
|
149 } |
|
150 |
|
151 bool IsChar() const; |
|
152 |
|
153 protected: |
|
154 // Internal helper functions |
|
155 nsIntPoint GetMovementPoint(); |
|
156 nsIntPoint GetLayerPoint() const; |
|
157 |
|
158 nsCOMPtr<nsIDOMWindow> mView; |
|
159 int32_t mDetail; |
|
160 CSSIntPoint mClientPoint; |
|
161 // Screenpoint is mEvent->refPoint. |
|
162 nsIntPoint mLayerPoint; |
|
163 CSSIntPoint mPagePoint; |
|
164 nsIntPoint mMovementPoint; |
|
165 bool mIsPointerLocked; |
|
166 CSSIntPoint mLastClientPoint; |
|
167 |
|
168 static Modifiers ComputeModifierState(const nsAString& aModifiersList); |
|
169 bool GetModifierStateInternal(const nsAString& aKey); |
|
170 }; |
|
171 |
|
172 } // namespace dom |
|
173 } // namespace mozilla |
|
174 |
|
175 #define NS_FORWARD_TO_UIEVENT \ |
|
176 NS_FORWARD_NSIDOMUIEVENT(UIEvent::) \ |
|
177 NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION \ |
|
178 NS_IMETHOD DuplicatePrivateData() \ |
|
179 { \ |
|
180 return UIEvent::DuplicatePrivateData(); \ |
|
181 } \ |
|
182 NS_IMETHOD_(void) Serialize(IPC::Message* aMsg, \ |
|
183 bool aSerializeInterfaceType) \ |
|
184 { \ |
|
185 UIEvent::Serialize(aMsg, aSerializeInterfaceType); \ |
|
186 } \ |
|
187 NS_IMETHOD_(bool) Deserialize(const IPC::Message* aMsg, \ |
|
188 void** aIter) \ |
|
189 { \ |
|
190 return UIEvent::Deserialize(aMsg, aIter); \ |
|
191 } |
|
192 |
|
193 #endif // mozilla_dom_UIEvent_h_ |