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_EventForwards_h__
7 #define mozilla_EventForwards_h__
8 
9 #include <stdint.h>
10 
11 #include "nsStringFwd.h"
12 #include "nsTArray.h"
13 
14 #ifdef DEBUG
15 #  include "mozilla/StaticPrefs_dom.h"
16 #endif  // #ifdef DEBUG
17 
18 class nsCommandParams;
19 
20 /**
21  * XXX Following enums should be in BasicEvents.h.  However, currently, it's
22  *     impossible to use foward delearation for enum.
23  */
24 
25 /**
26  * Return status for event processors.
27  */
28 enum nsEventStatus {
29   // The event is ignored, do default processing
30   nsEventStatus_eIgnore,
31   // The event is consumed, don't do default processing
32   nsEventStatus_eConsumeNoDefault,
33   // The event is consumed, but do default processing
34   nsEventStatus_eConsumeDoDefault,
35   // Value is not for use, only for serialization
36   nsEventStatus_eSentinel
37 };
38 
39 namespace mozilla {
40 
41 enum class CanBubble { eYes, eNo };
42 
43 enum class Cancelable { eYes, eNo };
44 
45 enum class ChromeOnlyDispatch { eYes, eNo };
46 
47 enum class Trusted { eYes, eNo };
48 
49 enum class Composed { eYes, eNo, eDefault };
50 
51 /**
52  * Event messages
53  */
54 
55 typedef uint16_t EventMessageType;
56 
57 enum EventMessage : EventMessageType {
58 
59 #define NS_EVENT_MESSAGE(aMessage) aMessage,
60 #define NS_EVENT_MESSAGE_FIRST_LAST(aMessage, aFirst, aLast) \
61   aMessage##First = aFirst, aMessage##Last = aLast,
62 
63 #include "mozilla/EventMessageList.h"
64 
65 #undef NS_EVENT_MESSAGE
66 #undef NS_EVENT_MESSAGE_FIRST_LAST
67 
68   // For preventing bustage due to "," after the last item.
69   eEventMessage_MaxValue
70 };
71 
72 const char* ToChar(EventMessage aEventMessage);
73 
74 /**
75  * Event class IDs
76  */
77 
78 typedef uint8_t EventClassIDType;
79 
80 enum EventClassID : EventClassIDType {
81 // The event class name will be:
82 //   eBasicEventClass for WidgetEvent
83 //   eFooEventClass for WidgetFooEvent or InternalFooEvent
84 #define NS_ROOT_EVENT_CLASS(aPrefix, aName) eBasic##aName##Class
85 #define NS_EVENT_CLASS(aPrefix, aName) , e##aName##Class
86 
87 #include "mozilla/EventClassList.h"
88 
89 #undef NS_EVENT_CLASS
90 #undef NS_ROOT_EVENT_CLASS
91 };
92 
93 const char* ToChar(EventClassID aEventClassID);
94 
95 typedef uint16_t Modifiers;
96 
97 #define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) KEY_NAME_INDEX_##aCPPName,
98 
99 typedef uint16_t KeyNameIndexType;
100 enum KeyNameIndex : KeyNameIndexType {
101 #include "mozilla/KeyNameList.h"
102   // If a DOM keyboard event is synthesized by script, this is used.  Then,
103   // specified key name should be stored and use it as .key value.
104   KEY_NAME_INDEX_USE_STRING
105 };
106 
107 #undef NS_DEFINE_KEYNAME
108 
109 const nsCString ToString(KeyNameIndex aKeyNameIndex);
110 
111 #define NS_DEFINE_PHYSICAL_KEY_CODE_NAME(aCPPName, aDOMCodeName) \
112   CODE_NAME_INDEX_##aCPPName,
113 
114 typedef uint8_t CodeNameIndexType;
115 enum CodeNameIndex : CodeNameIndexType {
116 #include "mozilla/PhysicalKeyCodeNameList.h"
117   // If a DOM keyboard event is synthesized by script, this is used.  Then,
118   // specified code name should be stored and use it as .code value.
119   CODE_NAME_INDEX_USE_STRING
120 };
121 
122 #undef NS_DEFINE_PHYSICAL_KEY_CODE_NAME
123 
124 const nsCString ToString(CodeNameIndex aCodeNameIndex);
125 
126 #define NS_DEFINE_INPUTTYPE(aCPPName, aDOMName) e##aCPPName,
127 
128 typedef uint8_t EditorInputTypeType;
129 enum class EditorInputType : EditorInputTypeType {
130 #include "mozilla/InputTypeList.h"
131   // If a DOM input event is synthesized by script, this is used.  Then,
132   // specified input type should be stored as string and use it as .inputType
133   // value.
134   eUnknown,
135 };
136 
137 #undef NS_DEFINE_INPUTTYPE
138 
ExposesClipboardDataOrDataTransfer(EditorInputType aInputType)139 inline bool ExposesClipboardDataOrDataTransfer(EditorInputType aInputType) {
140   switch (aInputType) {
141     case EditorInputType::eInsertFromPaste:
142     case EditorInputType::eInsertFromPasteAsQuotation:
143       return true;
144     default:
145       return false;
146   }
147 }
148 
149 /**
150  * IsDataAvailableOnTextEditor() returns true if aInputType on TextEditor
151  * should have non-null InputEvent.data value.
152  */
IsDataAvailableOnTextEditor(EditorInputType aInputType)153 inline bool IsDataAvailableOnTextEditor(EditorInputType aInputType) {
154   switch (aInputType) {
155     case EditorInputType::eInsertText:
156     case EditorInputType::eInsertCompositionText:
157     case EditorInputType::eInsertFromComposition:  // Only level 2
158     case EditorInputType::eInsertFromPaste:
159     case EditorInputType::eInsertFromPasteAsQuotation:
160     case EditorInputType::eInsertTranspose:
161     case EditorInputType::eInsertFromDrop:
162     case EditorInputType::eInsertReplacementText:
163     case EditorInputType::eInsertFromYank:
164     case EditorInputType::eFormatSetBlockTextDirection:
165     case EditorInputType::eFormatSetInlineTextDirection:
166       return true;
167     default:
168       return false;
169   }
170 }
171 
172 /**
173  * IsDataAvailableOnHTMLEditor() returns true if aInputType on HTMLEditor
174  * should have non-null InputEvent.data value.
175  */
IsDataAvailableOnHTMLEditor(EditorInputType aInputType)176 inline bool IsDataAvailableOnHTMLEditor(EditorInputType aInputType) {
177   switch (aInputType) {
178     case EditorInputType::eInsertText:
179     case EditorInputType::eInsertCompositionText:
180     case EditorInputType::eInsertFromComposition:  // Only level 2
181     case EditorInputType::eFormatSetBlockTextDirection:
182     case EditorInputType::eFormatSetInlineTextDirection:
183     case EditorInputType::eInsertLink:
184     case EditorInputType::eFormatBackColor:
185     case EditorInputType::eFormatFontColor:
186     case EditorInputType::eFormatFontName:
187       return true;
188     default:
189       return false;
190   }
191 }
192 
193 /**
194  * IsDataTransferAvailableOnHTMLEditor() returns true if aInputType on
195  * HTMLEditor should have non-null InputEvent.dataTransfer value.
196  */
IsDataTransferAvailableOnHTMLEditor(EditorInputType aInputType)197 inline bool IsDataTransferAvailableOnHTMLEditor(EditorInputType aInputType) {
198   switch (aInputType) {
199     case EditorInputType::eInsertFromPaste:
200     case EditorInputType::eInsertFromPasteAsQuotation:
201     case EditorInputType::eInsertFromDrop:
202     case EditorInputType::eInsertTranspose:
203     case EditorInputType::eInsertReplacementText:
204     case EditorInputType::eInsertFromYank:
205       return true;
206     default:
207       return false;
208   }
209 }
210 
211 /**
212  * MayHaveTargetRangesOnHTMLEditor() returns true if "beforeinput" event whose
213  * whose inputType is aInputType on HTMLEditor may return non-empty static
214  * range array from getTargetRanges().
215  * Note that TextEditor always sets empty array.  Therefore, there is no
216  * method for TextEditor.
217  */
MayHaveTargetRangesOnHTMLEditor(EditorInputType aInputType)218 inline bool MayHaveTargetRangesOnHTMLEditor(EditorInputType aInputType) {
219   switch (aInputType) {
220     // Explicitly documented by the specs.
221     case EditorInputType::eHistoryRedo:
222     case EditorInputType::eHistoryUndo:
223     // Not documented, but other browsers use empty array.
224     case EditorInputType::eFormatSetBlockTextDirection:
225       return false;
226     default:
227       return true;
228   }
229 }
230 
231 /**
232  * IsCancelableBeforeInputEvent() returns true if `beforeinput` event for
233  * aInputType should be cancelable.
234  *
235  * Input Events Level 1:
236  *   https://rawgit.com/w3c/input-events/v1/index.html#x5-1-2-attributes
237  * Input Events Level 2:
238  *   https://w3c.github.io/input-events/#interface-InputEvent-Attributes
239  */
IsCancelableBeforeInputEvent(EditorInputType aInputType)240 inline bool IsCancelableBeforeInputEvent(EditorInputType aInputType) {
241   switch (aInputType) {
242     case EditorInputType::eInsertText:
243       return true;  // In Level 1, undefined.
244     case EditorInputType::eInsertReplacementText:
245       return true;  // In Level 1, undefined.
246     case EditorInputType::eInsertLineBreak:
247       return true;  // In Level 1, undefined.
248     case EditorInputType::eInsertParagraph:
249       return true;  // In Level 1, undefined.
250     case EditorInputType::eInsertOrderedList:
251       return true;
252     case EditorInputType::eInsertUnorderedList:
253       return true;
254     case EditorInputType::eInsertHorizontalRule:
255       return true;
256     case EditorInputType::eInsertFromYank:
257       return true;
258     case EditorInputType::eInsertFromDrop:
259       return true;
260     case EditorInputType::eInsertFromPaste:
261       return true;
262     case EditorInputType::eInsertFromPasteAsQuotation:
263       return true;
264     case EditorInputType::eInsertTranspose:
265       return true;
266     case EditorInputType::eInsertCompositionText:
267       return false;
268     case EditorInputType::eInsertFromComposition:
269       MOZ_ASSERT(!StaticPrefs::dom_input_events_conform_to_level_1());
270       return true;
271     case EditorInputType::eInsertLink:
272       return true;
273     case EditorInputType::eDeleteByComposition:
274       MOZ_ASSERT(!StaticPrefs::dom_input_events_conform_to_level_1());
275       return true;
276     case EditorInputType::eDeleteCompositionText:
277       MOZ_ASSERT(!StaticPrefs::dom_input_events_conform_to_level_1());
278       return false;
279     case EditorInputType::eDeleteWordBackward:
280       return true;  // In Level 1, undefined.
281     case EditorInputType::eDeleteWordForward:
282       return true;  // In Level 1, undefined.
283     case EditorInputType::eDeleteSoftLineBackward:
284       return true;  // In Level 1, undefined.
285     case EditorInputType::eDeleteSoftLineForward:
286       return true;  // In Level 1, undefined.
287     case EditorInputType::eDeleteEntireSoftLine:
288       return true;  // In Level 1, undefined.
289     case EditorInputType::eDeleteHardLineBackward:
290       return true;  // In Level 1, undefined.
291     case EditorInputType::eDeleteHardLineForward:
292       return true;  // In Level 1, undefined.
293     case EditorInputType::eDeleteByDrag:
294       return true;
295     case EditorInputType::eDeleteByCut:
296       return true;
297     case EditorInputType::eDeleteContent:
298       return true;  // In Level 1, undefined.
299     case EditorInputType::eDeleteContentBackward:
300       return true;  // In Level 1, undefined.
301     case EditorInputType::eDeleteContentForward:
302       return true;  // In Level 1, undefined.
303     case EditorInputType::eHistoryUndo:
304       return true;
305     case EditorInputType::eHistoryRedo:
306       return true;
307     case EditorInputType::eFormatBold:
308       return true;
309     case EditorInputType::eFormatItalic:
310       return true;
311     case EditorInputType::eFormatUnderline:
312       return true;
313     case EditorInputType::eFormatStrikeThrough:
314       return true;
315     case EditorInputType::eFormatSuperscript:
316       return true;
317     case EditorInputType::eFormatSubscript:
318       return true;
319     case EditorInputType::eFormatJustifyFull:
320       return true;
321     case EditorInputType::eFormatJustifyCenter:
322       return true;
323     case EditorInputType::eFormatJustifyRight:
324       return true;
325     case EditorInputType::eFormatJustifyLeft:
326       return true;
327     case EditorInputType::eFormatIndent:
328       return true;
329     case EditorInputType::eFormatOutdent:
330       return true;
331     case EditorInputType::eFormatRemove:
332       return true;
333     case EditorInputType::eFormatSetBlockTextDirection:
334       return true;
335     case EditorInputType::eFormatSetInlineTextDirection:
336       return true;
337     case EditorInputType::eFormatBackColor:
338       return true;
339     case EditorInputType::eFormatFontColor:
340       return true;
341     case EditorInputType::eFormatFontName:
342       return true;
343     case EditorInputType::eUnknown:
344       // This is not declared by Input Events, but it does not make sense to
345       // allow web apps to cancel default action without inputType value check.
346       // If some our specific edit actions should be cancelable, new inputType
347       // value for them should be declared by the spec.
348       return false;
349     default:
350       MOZ_ASSERT_UNREACHABLE("The new input type is not handled");
351       return false;
352   }
353 }
354 
355 #define NS_DEFINE_COMMAND(aName, aCommandStr) , aName
356 #define NS_DEFINE_COMMAND_WITH_PARAM(aName, aCommandStr, aParam) , aName
357 #define NS_DEFINE_COMMAND_NO_EXEC_COMMAND(aName) , aName
358 
359 typedef uint8_t CommandInt;
360 enum class Command : CommandInt {
361   DoNothing
362 
363 #include "mozilla/CommandList.h"
364 };
365 #undef NS_DEFINE_COMMAND
366 #undef NS_DEFINE_COMMAND_WITH_PARAM
367 #undef NS_DEFINE_COMMAND_NO_EXEC_COMMAND
368 
369 const char* ToChar(Command aCommand);
370 
371 /**
372  * Return a command value for aCommandName.
373  * XXX: Is there a better place to put `Command` related methods instead of
374  *      global scope in `mozilla` namespace?
375  *
376  * @param aCommandName          Should be a XUL command name like "cmd_bold"
377  *                              (case sensitive).
378  * @param aCommandparams        Additional parameter value of aCommandName.
379  *                              Can be nullptr, but if aCommandName requires
380  *                              additional parameter and sets this to nullptr,
381  *                              will return Command::DoNothing with warning.
382  */
383 Command GetInternalCommand(const char* aCommandName,
384                            const nsCommandParams* aCommandParams = nullptr);
385 
386 }  // namespace mozilla
387 
388 /**
389  * All header files should include this header instead of *Events.h.
390  */
391 
392 namespace mozilla {
393 
394 template <class T>
395 class OwningNonNull;
396 
397 namespace dom {
398 class StaticRange;
399 }
400 
401 #define NS_EVENT_CLASS(aPrefix, aName) class aPrefix##aName;
402 #define NS_ROOT_EVENT_CLASS(aPrefix, aName) NS_EVENT_CLASS(aPrefix, aName)
403 
404 #include "mozilla/EventClassList.h"
405 
406 #undef NS_EVENT_CLASS
407 #undef NS_ROOT_EVENT_CLASS
408 
409 // BasicEvents.h
410 struct BaseEventFlags;
411 struct EventFlags;
412 
413 class WidgetEventTime;
414 
415 class NativeEventData;
416 
417 // TextEvents.h
418 enum class AccessKeyType;
419 
420 struct AlternativeCharCode;
421 struct ShortcutKeyCandidate;
422 
423 typedef nsTArray<ShortcutKeyCandidate> ShortcutKeyCandidateArray;
424 typedef AutoTArray<ShortcutKeyCandidate, 10> AutoShortcutKeyCandidateArray;
425 
426 // TextRange.h
427 typedef uint8_t RawTextRangeType;
428 enum class TextRangeType : RawTextRangeType;
429 
430 struct TextRangeStyle;
431 struct TextRange;
432 
433 class EditCommands;
434 class TextRangeArray;
435 
436 typedef nsTArray<OwningNonNull<dom::StaticRange>> OwningNonNullStaticRangeArray;
437 
438 // FontRange.h
439 struct FontRange;
440 
441 enum MouseButton { eNotPressed = -1, eLeft = 0, eMiddle = 1, eRight = 2 };
442 
443 enum MouseButtonsFlag {
444   eNoButtons = 0x00,
445   eLeftFlag = 0x01,
446   eRightFlag = 0x02,
447   eMiddleFlag = 0x04,
448   // typicall, "back" button being left side of 5-button
449   // mice, see "buttons" attribute document of DOM3 Events.
450   e4thFlag = 0x08,
451   // typicall, "forward" button being right side of 5-button
452   // mice, see "buttons" attribute document of DOM3 Events.
453   e5thFlag = 0x10
454 };
455 
456 enum class TextRangeType : RawTextRangeType;
457 
458 }  // namespace mozilla
459 
460 #endif  // mozilla_EventForwards_h__
461