|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "mozilla/dom/WheelEvent.h" |
|
8 #include "mozilla/MouseEvents.h" |
|
9 #include "prtime.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace dom { |
|
13 |
|
14 WheelEvent::WheelEvent(EventTarget* aOwner, |
|
15 nsPresContext* aPresContext, |
|
16 WidgetWheelEvent* aWheelEvent) |
|
17 : MouseEvent(aOwner, aPresContext, |
|
18 aWheelEvent ? aWheelEvent : |
|
19 new WidgetWheelEvent(false, 0, nullptr)) |
|
20 , mAppUnitsPerDevPixel(0) |
|
21 { |
|
22 if (aWheelEvent) { |
|
23 mEventIsInternal = false; |
|
24 // If the delta mode is pixel, the WidgetWheelEvent's delta values are in |
|
25 // device pixels. However, JS contents need the delta values in CSS pixels. |
|
26 // We should store the value of mAppUnitsPerDevPixel here because |
|
27 // it might be changed by changing zoom or something. |
|
28 if (aWheelEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL) { |
|
29 mAppUnitsPerDevPixel = aPresContext->AppUnitsPerDevPixel(); |
|
30 } |
|
31 } else { |
|
32 mEventIsInternal = true; |
|
33 mEvent->time = PR_Now(); |
|
34 mEvent->refPoint.x = mEvent->refPoint.y = 0; |
|
35 mEvent->AsWheelEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN; |
|
36 } |
|
37 } |
|
38 |
|
39 NS_IMPL_ADDREF_INHERITED(WheelEvent, MouseEvent) |
|
40 NS_IMPL_RELEASE_INHERITED(WheelEvent, MouseEvent) |
|
41 |
|
42 NS_INTERFACE_MAP_BEGIN(WheelEvent) |
|
43 NS_INTERFACE_MAP_ENTRY(nsIDOMWheelEvent) |
|
44 NS_INTERFACE_MAP_END_INHERITING(MouseEvent) |
|
45 |
|
46 NS_IMETHODIMP |
|
47 WheelEvent::InitWheelEvent(const nsAString& aType, |
|
48 bool aCanBubble, |
|
49 bool aCancelable, |
|
50 nsIDOMWindow* aView, |
|
51 int32_t aDetail, |
|
52 int32_t aScreenX, |
|
53 int32_t aScreenY, |
|
54 int32_t aClientX, |
|
55 int32_t aClientY, |
|
56 uint16_t aButton, |
|
57 nsIDOMEventTarget* aRelatedTarget, |
|
58 const nsAString& aModifiersList, |
|
59 double aDeltaX, |
|
60 double aDeltaY, |
|
61 double aDeltaZ, |
|
62 uint32_t aDeltaMode) |
|
63 { |
|
64 nsresult rv = |
|
65 MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail, |
|
66 aScreenX, aScreenY, aClientX, aClientY, aButton, |
|
67 aRelatedTarget, aModifiersList); |
|
68 NS_ENSURE_SUCCESS(rv, rv); |
|
69 |
|
70 WidgetWheelEvent* wheelEvent = mEvent->AsWheelEvent(); |
|
71 wheelEvent->deltaX = aDeltaX; |
|
72 wheelEvent->deltaY = aDeltaY; |
|
73 wheelEvent->deltaZ = aDeltaZ; |
|
74 wheelEvent->deltaMode = aDeltaMode; |
|
75 |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 double |
|
80 WheelEvent::DeltaX() |
|
81 { |
|
82 if (!mAppUnitsPerDevPixel) { |
|
83 return mEvent->AsWheelEvent()->deltaX; |
|
84 } |
|
85 return mEvent->AsWheelEvent()->deltaX * |
|
86 mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel(); |
|
87 } |
|
88 |
|
89 NS_IMETHODIMP |
|
90 WheelEvent::GetDeltaX(double* aDeltaX) |
|
91 { |
|
92 NS_ENSURE_ARG_POINTER(aDeltaX); |
|
93 |
|
94 *aDeltaX = DeltaX(); |
|
95 return NS_OK; |
|
96 } |
|
97 |
|
98 double |
|
99 WheelEvent::DeltaY() |
|
100 { |
|
101 if (!mAppUnitsPerDevPixel) { |
|
102 return mEvent->AsWheelEvent()->deltaY; |
|
103 } |
|
104 return mEvent->AsWheelEvent()->deltaY * |
|
105 mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel(); |
|
106 } |
|
107 |
|
108 NS_IMETHODIMP |
|
109 WheelEvent::GetDeltaY(double* aDeltaY) |
|
110 { |
|
111 NS_ENSURE_ARG_POINTER(aDeltaY); |
|
112 |
|
113 *aDeltaY = DeltaY(); |
|
114 return NS_OK; |
|
115 } |
|
116 |
|
117 double |
|
118 WheelEvent::DeltaZ() |
|
119 { |
|
120 if (!mAppUnitsPerDevPixel) { |
|
121 return mEvent->AsWheelEvent()->deltaZ; |
|
122 } |
|
123 return mEvent->AsWheelEvent()->deltaZ * |
|
124 mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel(); |
|
125 } |
|
126 |
|
127 NS_IMETHODIMP |
|
128 WheelEvent::GetDeltaZ(double* aDeltaZ) |
|
129 { |
|
130 NS_ENSURE_ARG_POINTER(aDeltaZ); |
|
131 |
|
132 *aDeltaZ = DeltaZ(); |
|
133 return NS_OK; |
|
134 } |
|
135 |
|
136 uint32_t |
|
137 WheelEvent::DeltaMode() |
|
138 { |
|
139 return mEvent->AsWheelEvent()->deltaMode; |
|
140 } |
|
141 |
|
142 NS_IMETHODIMP |
|
143 WheelEvent::GetDeltaMode(uint32_t* aDeltaMode) |
|
144 { |
|
145 NS_ENSURE_ARG_POINTER(aDeltaMode); |
|
146 |
|
147 *aDeltaMode = DeltaMode(); |
|
148 return NS_OK; |
|
149 } |
|
150 |
|
151 static void |
|
152 GetModifierList(bool aCtrl, bool aShift, bool aAlt, bool aMeta, |
|
153 nsAString& aModifierList) |
|
154 { |
|
155 if (aCtrl) { |
|
156 aModifierList.AppendLiteral(NS_DOM_KEYNAME_CONTROL); |
|
157 } |
|
158 if (aShift) { |
|
159 if (!aModifierList.IsEmpty()) { |
|
160 aModifierList.AppendLiteral(" "); |
|
161 } |
|
162 aModifierList.AppendLiteral(NS_DOM_KEYNAME_SHIFT); |
|
163 } |
|
164 if (aAlt) { |
|
165 if (!aModifierList.IsEmpty()) { |
|
166 aModifierList.AppendLiteral(" "); |
|
167 } |
|
168 aModifierList.AppendLiteral(NS_DOM_KEYNAME_ALT); |
|
169 } |
|
170 if (aMeta) { |
|
171 if (!aModifierList.IsEmpty()) { |
|
172 aModifierList.AppendLiteral(" "); |
|
173 } |
|
174 aModifierList.AppendLiteral(NS_DOM_KEYNAME_META); |
|
175 } |
|
176 } |
|
177 |
|
178 already_AddRefed<WheelEvent> |
|
179 WheelEvent::Constructor(const GlobalObject& aGlobal, |
|
180 const nsAString& aType, |
|
181 const WheelEventInit& aParam, |
|
182 ErrorResult& aRv) |
|
183 { |
|
184 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports()); |
|
185 nsRefPtr<WheelEvent> e = new WheelEvent(t, nullptr, nullptr); |
|
186 bool trusted = e->Init(t); |
|
187 nsAutoString modifierList; |
|
188 GetModifierList(aParam.mCtrlKey, aParam.mShiftKey, |
|
189 aParam.mAltKey, aParam.mMetaKey, |
|
190 modifierList); |
|
191 aRv = e->InitWheelEvent(aType, aParam.mBubbles, aParam.mCancelable, |
|
192 aParam.mView, aParam.mDetail, |
|
193 aParam.mScreenX, aParam.mScreenY, |
|
194 aParam.mClientX, aParam.mClientY, |
|
195 aParam.mButton, aParam.mRelatedTarget, |
|
196 modifierList, aParam.mDeltaX, |
|
197 aParam.mDeltaY, aParam.mDeltaZ, aParam.mDeltaMode); |
|
198 e->mEvent->AsWheelEvent()->buttons = aParam.mButtons; |
|
199 e->SetTrusted(trusted); |
|
200 return e.forget(); |
|
201 } |
|
202 |
|
203 } // namespace dom |
|
204 } // namespace mozilla |
|
205 |
|
206 using namespace mozilla; |
|
207 using namespace mozilla::dom; |
|
208 |
|
209 nsresult |
|
210 NS_NewDOMWheelEvent(nsIDOMEvent** aInstancePtrResult, |
|
211 EventTarget* aOwner, |
|
212 nsPresContext* aPresContext, |
|
213 WidgetWheelEvent* aEvent) |
|
214 { |
|
215 WheelEvent* it = new WheelEvent(aOwner, aPresContext, aEvent); |
|
216 NS_ADDREF(it); |
|
217 *aInstancePtrResult = static_cast<Event*>(it); |
|
218 return NS_OK; |
|
219 } |