forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEvents.h
592 lines (509 loc) · 17.1 KB
/
TextEvents.h
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* -*- Mode: C++; tab-width: 2; 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/. */
#ifndef mozilla_TextEvents_h__
#define mozilla_TextEvents_h__
#include <stdint.h>
#include "mozilla/Assertions.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/EventForwards.h" // for KeyNameIndex, temporarily
#include "mozilla/TextRange.h"
#include "nsCOMPtr.h"
#include "nsIDOMKeyEvent.h"
#include "nsITransferable.h"
#include "nsRect.h"
#include "nsStringGlue.h"
#include "nsTArray.h"
/******************************************************************************
* virtual keycode values
******************************************************************************/
#define NS_DEFINE_VK(aDOMKeyName, aDOMKeyCode) NS_##aDOMKeyName = aDOMKeyCode
enum
{
#include "mozilla/VirtualKeyCodeList.h"
};
#undef NS_DEFINE_VK
#define kLatestSeqno UINT32_MAX
namespace mozilla {
namespace dom {
class PBrowserParent;
class PBrowserChild;
} // namespace dom
namespace plugins {
class PPluginInstanceChild;
} // namespace plugins
/******************************************************************************
* mozilla::AlternativeCharCode
*
* This stores alternative charCode values of a key event with some modifiers.
* The stored values proper for testing shortcut key or access key.
******************************************************************************/
struct AlternativeCharCode
{
AlternativeCharCode() :
mUnshiftedCharCode(0), mShiftedCharCode(0)
{
}
AlternativeCharCode(uint32_t aUnshiftedCharCode, uint32_t aShiftedCharCode) :
mUnshiftedCharCode(aUnshiftedCharCode), mShiftedCharCode(aShiftedCharCode)
{
}
uint32_t mUnshiftedCharCode;
uint32_t mShiftedCharCode;
};
/******************************************************************************
* mozilla::WidgetKeyboardEvent
******************************************************************************/
class WidgetKeyboardEvent : public WidgetInputEvent
{
private:
friend class dom::PBrowserParent;
friend class dom::PBrowserChild;
WidgetKeyboardEvent()
{
}
public:
virtual WidgetKeyboardEvent* AsKeyboardEvent() MOZ_OVERRIDE { return this; }
WidgetKeyboardEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget)
: WidgetInputEvent(aIsTrusted, aMessage, aWidget, eKeyboardEventClass)
, keyCode(0)
, charCode(0)
, location(nsIDOMKeyEvent::DOM_KEY_LOCATION_STANDARD)
, isChar(false)
, mIsRepeat(false)
, mIsComposing(false)
, mKeyNameIndex(mozilla::KEY_NAME_INDEX_Unidentified)
, mCodeNameIndex(CODE_NAME_INDEX_UNKNOWN)
, mNativeKeyEvent(nullptr)
, mUniqueId(0)
{
}
virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
{
MOZ_ASSERT(mClass == eKeyboardEventClass,
"Duplicate() must be overridden by sub class");
// Not copying widget, it is a weak reference.
WidgetKeyboardEvent* result =
new WidgetKeyboardEvent(false, message, nullptr);
result->AssignKeyEventData(*this, true);
result->mFlags = mFlags;
return result;
}
// A DOM keyCode value or 0. If a keypress event whose charCode is 0, this
// should be 0.
uint32_t keyCode;
// If the instance is a keypress event of a printable key, this is a UTF-16
// value of the key. Otherwise, 0. This value must not be a control
// character when some modifiers are active. Then, this value should be an
// unmodified value except Shift and AltGr.
uint32_t charCode;
// One of nsIDOMKeyEvent::DOM_KEY_LOCATION_*
uint32_t location;
// OS translated Unicode chars which are used for accesskey and accelkey
// handling. The handlers will try from first character to last character.
nsTArray<AlternativeCharCode> alternativeCharCodes;
// Indicates whether the event signifies a printable character
bool isChar;
// Indicates whether the event is generated by auto repeat or not.
// if this is keyup event, always false.
bool mIsRepeat;
// Indicates whether the event is generated during IME (or deadkey)
// composition. This is initialized by EventStateManager. So, key event
// dispatchers don't need to initialize this.
bool mIsComposing;
// DOM KeyboardEvent.key
KeyNameIndex mKeyNameIndex;
// DOM KeyboardEvent.code
CodeNameIndex mCodeNameIndex;
// DOM KeyboardEvent.key only when mKeyNameIndex is KEY_NAME_INDEX_USE_STRING.
nsString mKeyValue;
// DOM KeyboardEvent.code only when mCodeNameIndex is
// CODE_NAME_INDEX_USE_STRING.
nsString mCodeValue;
// OS-specific native event can optionally be preserved
void* mNativeKeyEvent;
// Unique id associated with a keydown / keypress event. Used in identifing
// keypress events for removal from async event dispatch queue in metrofx
// after preventDefault is called on keydown events. It's ok if this wraps
// over long periods.
uint32_t mUniqueId;
void GetDOMKeyName(nsAString& aKeyName)
{
if (mKeyNameIndex == KEY_NAME_INDEX_USE_STRING) {
aKeyName = mKeyValue;
return;
}
GetDOMKeyName(mKeyNameIndex, aKeyName);
}
void GetDOMCodeName(nsAString& aCodeName)
{
if (mCodeNameIndex == CODE_NAME_INDEX_USE_STRING) {
aCodeName = mCodeValue;
return;
}
GetDOMCodeName(mCodeNameIndex, aCodeName);
}
static void GetDOMKeyName(KeyNameIndex aKeyNameIndex,
nsAString& aKeyName);
static void GetDOMCodeName(CodeNameIndex aCodeNameIndex,
nsAString& aCodeName);
static const char* GetCommandStr(Command aCommand);
void AssignKeyEventData(const WidgetKeyboardEvent& aEvent, bool aCopyTargets)
{
AssignInputEventData(aEvent, aCopyTargets);
keyCode = aEvent.keyCode;
charCode = aEvent.charCode;
location = aEvent.location;
alternativeCharCodes = aEvent.alternativeCharCodes;
isChar = aEvent.isChar;
mIsRepeat = aEvent.mIsRepeat;
mIsComposing = aEvent.mIsComposing;
mKeyNameIndex = aEvent.mKeyNameIndex;
mCodeNameIndex = aEvent.mCodeNameIndex;
mKeyValue = aEvent.mKeyValue;
mCodeValue = aEvent.mCodeValue;
// Don't copy mNativeKeyEvent because it may be referred after its instance
// is destroyed.
mNativeKeyEvent = nullptr;
mUniqueId = aEvent.mUniqueId;
}
};
/******************************************************************************
* mozilla::WidgetTextEvent
*
* XXX WidgetTextEvent is fired with compositionupdate event almost every time.
* This wastes performance and the cost of mantaining each platform's
* implementation. Therefore, we should merge WidgetTextEvent and
* WidgetCompositionEvent. Then, DOM compositionupdate should be fired
* from TextComposition automatically.
******************************************************************************/
class WidgetTextEvent : public WidgetGUIEvent
{
private:
friend class dom::PBrowserParent;
friend class dom::PBrowserChild;
friend class plugins::PPluginInstanceChild;
WidgetTextEvent()
: mSeqno(kLatestSeqno)
, isChar(false)
{
}
public:
uint32_t mSeqno;
public:
virtual WidgetTextEvent* AsTextEvent() MOZ_OVERRIDE { return this; }
WidgetTextEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget)
: WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eTextEventClass)
, mSeqno(kLatestSeqno)
, isChar(false)
{
}
virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
{
MOZ_ASSERT(mClass == eTextEventClass,
"Duplicate() must be overridden by sub class");
// Not copying widget, it is a weak reference.
WidgetTextEvent* result = new WidgetTextEvent(false, message, nullptr);
result->AssignTextEventData(*this, true);
result->mFlags = mFlags;
return result;
}
// The composition string or the commit string.
nsString theText;
// Indicates whether the event signifies printable text.
// XXX This is not a standard, and most platforms don't set this properly.
// So, perhaps, we can get rid of this.
bool isChar;
nsRefPtr<TextRangeArray> mRanges;
void AssignTextEventData(const WidgetTextEvent& aEvent, bool aCopyTargets)
{
AssignGUIEventData(aEvent, aCopyTargets);
isChar = aEvent.isChar;
// Currently, we don't need to copy the other members because they are
// for internal use only (not available from JS).
}
bool IsComposing() const
{
return mRanges && mRanges->IsComposing();
}
uint32_t TargetClauseOffset() const
{
return mRanges ? mRanges->TargetClauseOffset() : 0;
}
uint32_t RangeCount() const
{
return mRanges ? mRanges->Length() : 0;
}
};
/******************************************************************************
* mozilla::WidgetCompositionEvent
******************************************************************************/
class WidgetCompositionEvent : public WidgetGUIEvent
{
private:
friend class mozilla::dom::PBrowserParent;
friend class mozilla::dom::PBrowserChild;
WidgetCompositionEvent()
: mSeqno(kLatestSeqno)
{
}
public:
uint32_t mSeqno;
public:
virtual WidgetCompositionEvent* AsCompositionEvent() MOZ_OVERRIDE
{
return this;
}
WidgetCompositionEvent(bool aIsTrusted, uint32_t aMessage,
nsIWidget* aWidget)
: WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eCompositionEventClass)
, mSeqno(kLatestSeqno)
{
// XXX compositionstart is cancelable in draft of DOM3 Events.
// However, it doesn't make sense for us, we cannot cancel composition
// when we send compositionstart event.
mFlags.mCancelable = false;
}
virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
{
MOZ_ASSERT(mClass == eCompositionEventClass,
"Duplicate() must be overridden by sub class");
// Not copying widget, it is a weak reference.
WidgetCompositionEvent* result =
new WidgetCompositionEvent(false, message, nullptr);
result->AssignCompositionEventData(*this, true);
result->mFlags = mFlags;
return result;
}
// The composition string or the commit string. If the instance is a
// compositionstart event, this is initialized with selected text by
// TextComposition automatically.
nsString data;
void AssignCompositionEventData(const WidgetCompositionEvent& aEvent,
bool aCopyTargets)
{
AssignGUIEventData(aEvent, aCopyTargets);
data = aEvent.data;
}
};
/******************************************************************************
* mozilla::WidgetQueryContentEvent
******************************************************************************/
class WidgetQueryContentEvent : public WidgetGUIEvent
{
private:
friend class dom::PBrowserParent;
friend class dom::PBrowserChild;
WidgetQueryContentEvent()
{
MOZ_CRASH("WidgetQueryContentEvent is created without proper arguments");
}
public:
virtual WidgetQueryContentEvent* AsQueryContentEvent() MOZ_OVERRIDE
{
return this;
}
WidgetQueryContentEvent(bool aIsTrusted, uint32_t aMessage,
nsIWidget* aWidget)
: WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eQueryContentEventClass)
, mSucceeded(false)
, mWasAsync(false)
, mUseNativeLineBreak(true)
{
}
virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
{
// This event isn't an internal event of any DOM event.
NS_ASSERTION(!IsAllowedToDispatchDOMEvent(),
"WidgetQueryContentEvent needs to support Duplicate()");
MOZ_CRASH("WidgetQueryContentEvent doesn't support Duplicate()");
return nullptr;
}
void InitForQueryTextContent(uint32_t aOffset, uint32_t aLength,
bool aUseNativeLineBreak = true)
{
NS_ASSERTION(message == NS_QUERY_TEXT_CONTENT,
"wrong initializer is called");
mInput.mOffset = aOffset;
mInput.mLength = aLength;
mUseNativeLineBreak = aUseNativeLineBreak;
}
void InitForQueryCaretRect(uint32_t aOffset,
bool aUseNativeLineBreak = true)
{
NS_ASSERTION(message == NS_QUERY_CARET_RECT,
"wrong initializer is called");
mInput.mOffset = aOffset;
mUseNativeLineBreak = aUseNativeLineBreak;
}
void InitForQueryTextRect(uint32_t aOffset, uint32_t aLength,
bool aUseNativeLineBreak = true)
{
NS_ASSERTION(message == NS_QUERY_TEXT_RECT,
"wrong initializer is called");
mInput.mOffset = aOffset;
mInput.mLength = aLength;
mUseNativeLineBreak = aUseNativeLineBreak;
}
void InitForQueryDOMWidgetHittest(const mozilla::LayoutDeviceIntPoint& aPoint)
{
NS_ASSERTION(message == NS_QUERY_DOM_WIDGET_HITTEST,
"wrong initializer is called");
refPoint = aPoint;
}
uint32_t GetSelectionStart(void) const
{
NS_ASSERTION(message == NS_QUERY_SELECTED_TEXT,
"not querying selection");
return mReply.mOffset + (mReply.mReversed ? mReply.mString.Length() : 0);
}
uint32_t GetSelectionEnd(void) const
{
NS_ASSERTION(message == NS_QUERY_SELECTED_TEXT,
"not querying selection");
return mReply.mOffset + (mReply.mReversed ? 0 : mReply.mString.Length());
}
bool mSucceeded;
bool mWasAsync;
bool mUseNativeLineBreak;
struct
{
uint32_t mOffset;
uint32_t mLength;
} mInput;
struct
{
void* mContentsRoot;
uint32_t mOffset;
nsString mString;
// Finally, the coordinates is system coordinates.
nsIntRect mRect;
// The return widget has the caret. This is set at all query events.
nsIWidget* mFocusedWidget;
// true if selection is reversed (end < start)
bool mReversed;
// true if the selection exists
bool mHasSelection;
// true if DOM element under mouse belongs to widget
bool mWidgetIsHit;
// used by NS_QUERY_SELECTION_AS_TRANSFERABLE
nsCOMPtr<nsITransferable> mTransferable;
} mReply;
enum
{
NOT_FOUND = UINT32_MAX
};
// values of mComputedScrollAction
enum
{
SCROLL_ACTION_NONE,
SCROLL_ACTION_LINE,
SCROLL_ACTION_PAGE
};
};
/******************************************************************************
* mozilla::WidgetSelectionEvent
******************************************************************************/
class WidgetSelectionEvent : public WidgetGUIEvent
{
private:
friend class mozilla::dom::PBrowserParent;
friend class mozilla::dom::PBrowserChild;
WidgetSelectionEvent()
: mSeqno(kLatestSeqno)
, mOffset(0)
, mLength(0)
, mReversed(false)
, mExpandToClusterBoundary(true)
, mSucceeded(false)
{
}
public:
uint32_t mSeqno;
public:
virtual WidgetSelectionEvent* AsSelectionEvent() MOZ_OVERRIDE
{
return this;
}
WidgetSelectionEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget)
: WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eSelectionEventClass)
, mSeqno(kLatestSeqno)
, mOffset(0)
, mLength(0)
, mReversed(false)
, mExpandToClusterBoundary(true)
, mSucceeded(false)
, mUseNativeLineBreak(true)
{
}
virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
{
// This event isn't an internal event of any DOM event.
NS_ASSERTION(!IsAllowedToDispatchDOMEvent(),
"WidgetSelectionEvent needs to support Duplicate()");
MOZ_CRASH("WidgetSelectionEvent doesn't support Duplicate()");
return nullptr;
}
// Start offset of selection
uint32_t mOffset;
// Length of selection
uint32_t mLength;
// Selection "anchor" should be in front
bool mReversed;
// Cluster-based or character-based
bool mExpandToClusterBoundary;
// true if setting selection succeeded.
bool mSucceeded;
// true if native line breaks are used for mOffset and mLength
bool mUseNativeLineBreak;
};
/******************************************************************************
* mozilla::InternalEditorInputEvent
******************************************************************************/
class InternalEditorInputEvent : public InternalUIEvent
{
private:
InternalEditorInputEvent()
: mIsComposing(false)
{
}
public:
virtual InternalEditorInputEvent* AsEditorInputEvent() MOZ_OVERRIDE
{
return this;
}
InternalEditorInputEvent(bool aIsTrusted, uint32_t aMessage,
nsIWidget* aWidget)
: InternalUIEvent(aIsTrusted, aMessage, aWidget, eEditorInputEventClass)
, mIsComposing(false)
{
if (!aIsTrusted) {
mFlags.mBubbles = false;
mFlags.mCancelable = false;
return;
}
mFlags.mBubbles = true;
mFlags.mCancelable = false;
}
virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
{
MOZ_ASSERT(mClass == eEditorInputEventClass,
"Duplicate() must be overridden by sub class");
// Not copying widget, it is a weak reference.
InternalEditorInputEvent* result =
new InternalEditorInputEvent(false, message, nullptr);
result->AssignEditorInputEventData(*this, true);
result->mFlags = mFlags;
return result;
}
bool mIsComposing;
void AssignEditorInputEventData(const InternalEditorInputEvent& aEvent,
bool aCopyTargets)
{
AssignUIEventData(aEvent, aCopyTargets);
mIsComposing = aEvent.mIsComposing;
}
};
} // namespace mozilla
#endif // mozilla_TextEvents_h__