1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/basictypes.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/dom/UIEvent.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/TextEvents.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDocShell.h"
#include "nsIDOMWindow.h"
#include "nsIDOMNode.h"
#include "nsIFrame.h"
#include "prtime.h"
namespace mozilla {
namespace dom {
UIEvent::UIEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetGUIEvent* aEvent)
: Event(aOwner, aPresContext,
aEvent ? aEvent : new InternalUIEvent(false, eVoidEvent, nullptr))
, mClientPoint(0, 0)
, mLayerPoint(0, 0)
, mPagePoint(0, 0)
, mMovementPoint(0, 0)
, mIsPointerLocked(EventStateManager::sIsPointerLocked)
, mLastClientPoint(EventStateManager::sLastClientPoint)
{
if (aEvent) {
mEventIsInternal = false;
}
else {
mEventIsInternal = true;
mEvent->mTime = PR_Now();
}
// Fill mDetail and mView according to the mEvent (widget-generated
// event) we've got
switch(mEvent->mClass) {
case eUIEventClass:
{
mDetail = mEvent->AsUIEvent()->mDetail;
break;
}
case eScrollPortEventClass:
{
InternalScrollPortEvent* scrollEvent = mEvent->AsScrollPortEvent();
mDetail = static_cast<int32_t>(scrollEvent->mOrient);
break;
}
default:
mDetail = 0;
break;
}
mView = nullptr;
if (mPresContext)
{
nsIDocShell* docShell = mPresContext->GetDocShell();
if (docShell)
{
mView = docShell->GetWindow();
}
}
}
// static
already_AddRefed<UIEvent>
UIEvent::Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const UIEventInit& aParam,
ErrorResult& aRv)
{
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
RefPtr<UIEvent> e = new UIEvent(t, nullptr, nullptr);
bool trusted = e->Init(t);
e->InitUIEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
aParam.mDetail);
e->SetTrusted(trusted);
e->SetComposed(aParam.mComposed);
return e.forget();
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(UIEvent, Event,
mView)
NS_IMPL_ADDREF_INHERITED(UIEvent, Event)
NS_IMPL_RELEASE_INHERITED(UIEvent, Event)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(UIEvent)
NS_INTERFACE_MAP_ENTRY(nsIDOMUIEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)
static nsIntPoint
DevPixelsToCSSPixels(const LayoutDeviceIntPoint& aPoint,
nsPresContext* aContext)
{
return nsIntPoint(aContext->DevPixelsToIntCSSPixels(aPoint.x),
aContext->DevPixelsToIntCSSPixels(aPoint.y));
}
nsIntPoint
UIEvent::GetMovementPoint()
{
if (mPrivateDataDuplicated || mEventIsInternal) {
return mMovementPoint;
}
if (!mEvent ||
(mEvent->mClass != eMouseEventClass &&
mEvent->mClass != eMouseScrollEventClass &&
mEvent->mClass != eWheelEventClass &&
mEvent->mClass != eDragEventClass &&
mEvent->mClass != ePointerEventClass &&
mEvent->mClass != eSimpleGestureEventClass) ||
!mEvent->AsGUIEvent()->mWidget) {
return nsIntPoint(0, 0);
}
// Calculate the delta between the last screen point and the current one.
nsIntPoint current = DevPixelsToCSSPixels(mEvent->mRefPoint, mPresContext);
nsIntPoint last = DevPixelsToCSSPixels(mEvent->mLastRefPoint, mPresContext);
return current - last;
}
NS_IMETHODIMP
UIEvent::GetView(mozIDOMWindowProxy** aView)
{
*aView = mView;
NS_IF_ADDREF(*aView);
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetDetail(int32_t* aDetail)
{
*aDetail = mDetail;
return NS_OK;
}
void
UIEvent::InitUIEvent(const nsAString& typeArg,
bool canBubbleArg,
bool cancelableArg,
nsGlobalWindow* viewArg,
int32_t detailArg)
{
auto* view = viewArg ? viewArg->AsInner() : nullptr;
InitUIEvent(typeArg, canBubbleArg, cancelableArg, view, detailArg);
}
NS_IMETHODIMP
UIEvent::InitUIEvent(const nsAString& typeArg,
bool canBubbleArg,
bool cancelableArg,
mozIDOMWindow* viewArg,
int32_t detailArg)
{
NS_ENSURE_TRUE(!mEvent->mFlags.mIsBeingDispatched, NS_OK);
Event::InitEvent(typeArg, canBubbleArg, cancelableArg);
mDetail = detailArg;
mView = viewArg ? nsPIDOMWindowInner::From(viewArg)->GetOuterWindow() :
nullptr;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetPageX(int32_t* aPageX)
{
NS_ENSURE_ARG_POINTER(aPageX);
*aPageX = PageX();
return NS_OK;
}
int32_t
UIEvent::PageX() const
{
if (mPrivateDataDuplicated) {
return mPagePoint.x;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint).x;
}
NS_IMETHODIMP
UIEvent::GetPageY(int32_t* aPageY)
{
NS_ENSURE_ARG_POINTER(aPageY);
*aPageY = PageY();
return NS_OK;
}
int32_t
UIEvent::PageY() const
{
if (mPrivateDataDuplicated) {
return mPagePoint.y;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint).y;
}
NS_IMETHODIMP
UIEvent::GetWhich(uint32_t* aWhich)
{
NS_ENSURE_ARG_POINTER(aWhich);
*aWhich = Which();
return NS_OK;
}
already_AddRefed<nsINode>
UIEvent::GetRangeParent()
{
nsIFrame* targetFrame = nullptr;
if (mPresContext) {
targetFrame = mPresContext->EventStateManager()->GetEventTarget();
}
if (targetFrame) {
nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent,
targetFrame);
nsCOMPtr<nsIContent> parent = targetFrame->GetContentOffsetsFromPoint(pt).content;
if (parent) {
if (parent->ChromeOnlyAccess() &&
!nsContentUtils::CanAccessNativeAnon()) {
return nullptr;
}
return parent.forget();
}
}
return nullptr;
}
NS_IMETHODIMP
UIEvent::GetRangeParent(nsIDOMNode** aRangeParent)
{
NS_ENSURE_ARG_POINTER(aRangeParent);
*aRangeParent = nullptr;
nsCOMPtr<nsINode> n = GetRangeParent();
if (n) {
CallQueryInterface(n, aRangeParent);
}
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetRangeOffset(int32_t* aRangeOffset)
{
NS_ENSURE_ARG_POINTER(aRangeOffset);
*aRangeOffset = RangeOffset();
return NS_OK;
}
int32_t
UIEvent::RangeOffset() const
{
if (!mPresContext) {
return 0;
}
nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget();
if (!targetFrame) {
return 0;
}
nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent,
targetFrame);
return targetFrame->GetContentOffsetsFromPoint(pt).offset;
}
nsIntPoint
UIEvent::GetLayerPoint() const
{
if (!mEvent ||
(mEvent->mClass != eMouseEventClass &&
mEvent->mClass != eMouseScrollEventClass &&
mEvent->mClass != eWheelEventClass &&
mEvent->mClass != ePointerEventClass &&
mEvent->mClass != eTouchEventClass &&
mEvent->mClass != eDragEventClass &&
mEvent->mClass != eSimpleGestureEventClass) ||
!mPresContext ||
mEventIsInternal) {
return mLayerPoint;
}
// XXX I'm not really sure this is correct; it's my best shot, though
nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget();
if (!targetFrame)
return mLayerPoint;
nsIFrame* layer = nsLayoutUtils::GetClosestLayer(targetFrame);
nsPoint pt(nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent, layer));
return nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(pt.x),
nsPresContext::AppUnitsToIntCSSPixels(pt.y));
}
NS_IMETHODIMP
UIEvent::GetLayerX(int32_t* aLayerX)
{
NS_ENSURE_ARG_POINTER(aLayerX);
*aLayerX = GetLayerPoint().x;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetLayerY(int32_t* aLayerY)
{
NS_ENSURE_ARG_POINTER(aLayerY);
*aLayerY = GetLayerPoint().y;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetIsChar(bool* aIsChar)
{
*aIsChar = IsChar();
return NS_OK;
}
bool
UIEvent::IsChar() const
{
WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
return keyEvent ? keyEvent->mIsChar : false;
}
mozilla::dom::Event*
UIEvent::AsEvent(void)
{
return this;
}
NS_IMETHODIMP
UIEvent::DuplicatePrivateData()
{
mClientPoint =
Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint);
mMovementPoint = GetMovementPoint();
mLayerPoint = GetLayerPoint();
mPagePoint =
Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint, mClientPoint);
// GetScreenPoint converts mEvent->mRefPoint to right coordinates.
CSSIntPoint screenPoint =
Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint);
nsresult rv = Event::DuplicatePrivateData();
if (NS_SUCCEEDED(rv)) {
CSSToLayoutDeviceScale scale = mPresContext ? mPresContext->CSSToDevPixelScale()
: CSSToLayoutDeviceScale(1);
mEvent->mRefPoint = RoundedToInt(screenPoint * scale);
}
return rv;
}
NS_IMETHODIMP_(void)
UIEvent::Serialize(IPC::Message* aMsg, bool aSerializeInterfaceType)
{
if (aSerializeInterfaceType) {
IPC::WriteParam(aMsg, NS_LITERAL_STRING("uievent"));
}
Event::Serialize(aMsg, false);
int32_t detail = 0;
GetDetail(&detail);
IPC::WriteParam(aMsg, detail);
}
NS_IMETHODIMP_(bool)
UIEvent::Deserialize(const IPC::Message* aMsg, PickleIterator* aIter)
{
NS_ENSURE_TRUE(Event::Deserialize(aMsg, aIter), false);
NS_ENSURE_TRUE(IPC::ReadParam(aMsg, aIter, &mDetail), false);
return true;
}
// XXX Following struct and array are used only in
// UIEvent::ComputeModifierState(), but if we define them in it,
// we fail to build on Mac at calling mozilla::ArrayLength().
struct ModifierPair
{
Modifier modifier;
const char* name;
};
static const ModifierPair kPairs[] = {
{ MODIFIER_ALT, NS_DOM_KEYNAME_ALT },
{ MODIFIER_ALTGRAPH, NS_DOM_KEYNAME_ALTGRAPH },
{ MODIFIER_CAPSLOCK, NS_DOM_KEYNAME_CAPSLOCK },
{ MODIFIER_CONTROL, NS_DOM_KEYNAME_CONTROL },
{ MODIFIER_FN, NS_DOM_KEYNAME_FN },
{ MODIFIER_FNLOCK, NS_DOM_KEYNAME_FNLOCK },
{ MODIFIER_META, NS_DOM_KEYNAME_META },
{ MODIFIER_NUMLOCK, NS_DOM_KEYNAME_NUMLOCK },
{ MODIFIER_SCROLLLOCK, NS_DOM_KEYNAME_SCROLLLOCK },
{ MODIFIER_SHIFT, NS_DOM_KEYNAME_SHIFT },
{ MODIFIER_SYMBOL, NS_DOM_KEYNAME_SYMBOL },
{ MODIFIER_SYMBOLLOCK, NS_DOM_KEYNAME_SYMBOLLOCK },
{ MODIFIER_OS, NS_DOM_KEYNAME_OS }
};
// static
Modifiers
UIEvent::ComputeModifierState(const nsAString& aModifiersList)
{
if (aModifiersList.IsEmpty()) {
return 0;
}
// Be careful about the performance. If aModifiersList is too long,
// parsing it needs too long time.
// XXX Should we abort if aModifiersList is too long?
Modifiers modifiers = 0;
nsAString::const_iterator listStart, listEnd;
aModifiersList.BeginReading(listStart);
aModifiersList.EndReading(listEnd);
for (uint32_t i = 0; i < ArrayLength(kPairs); i++) {
nsAString::const_iterator start(listStart), end(listEnd);
if (!FindInReadable(NS_ConvertASCIItoUTF16(kPairs[i].name), start, end)) {
continue;
}
if ((start != listStart && !NS_IsAsciiWhitespace(*(--start))) ||
(end != listEnd && !NS_IsAsciiWhitespace(*(end)))) {
continue;
}
modifiers |= kPairs[i].modifier;
}
return modifiers;
}
bool
UIEvent::GetModifierStateInternal(const nsAString& aKey)
{
WidgetInputEvent* inputEvent = mEvent->AsInputEvent();
MOZ_ASSERT(inputEvent, "mEvent must be WidgetInputEvent or derived class");
return ((inputEvent->mModifiers & WidgetInputEvent::GetModifier(aKey)) != 0);
}
void
UIEvent::InitModifiers(const EventModifierInit& aParam)
{
if (NS_WARN_IF(!mEvent)) {
return;
}
WidgetInputEvent* inputEvent = mEvent->AsInputEvent();
MOZ_ASSERT(inputEvent,
"This method shouldn't be called if it doesn't have modifiers");
if (NS_WARN_IF(!inputEvent)) {
return;
}
inputEvent->mModifiers = MODIFIER_NONE;
#define SET_MODIFIER(aName, aValue) \
if (aParam.m##aName) { \
inputEvent->mModifiers |= aValue; \
} \
SET_MODIFIER(CtrlKey, MODIFIER_CONTROL)
SET_MODIFIER(ShiftKey, MODIFIER_SHIFT)
SET_MODIFIER(AltKey, MODIFIER_ALT)
SET_MODIFIER(MetaKey, MODIFIER_META)
SET_MODIFIER(ModifierAltGraph, MODIFIER_ALTGRAPH)
SET_MODIFIER(ModifierCapsLock, MODIFIER_CAPSLOCK)
SET_MODIFIER(ModifierFn, MODIFIER_FN)
SET_MODIFIER(ModifierFnLock, MODIFIER_FNLOCK)
SET_MODIFIER(ModifierNumLock, MODIFIER_NUMLOCK)
SET_MODIFIER(ModifierOS, MODIFIER_OS)
SET_MODIFIER(ModifierScrollLock, MODIFIER_SCROLLLOCK)
SET_MODIFIER(ModifierSymbol, MODIFIER_SYMBOL)
SET_MODIFIER(ModifierSymbolLock, MODIFIER_SYMBOLLOCK)
#undef SET_MODIFIER
}
} // namespace dom
} // namespace mozilla
using namespace mozilla;
using namespace mozilla::dom;
already_AddRefed<UIEvent>
NS_NewDOMUIEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetGUIEvent* aEvent)
{
RefPtr<UIEvent> it = new UIEvent(aOwner, aPresContext, aEvent);
return it.forget();
}
|