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/. */
6 #ifndef mozilla_TextEvents_h__
7 #define mozilla_TextEvents_h__
11 #include "mozilla/Assertions.h"
12 #include "mozilla/BasicEvents.h"
13 #include "mozilla/EventForwards.h" // for KeyNameIndex, temporarily
14 #include "mozilla/TextRange.h"
16 #include "nsIDOMKeyEvent.h"
17 #include "nsITransferable.h"
19 #include "nsStringGlue.h"
22 /******************************************************************************
23 * virtual keycode values
24 ******************************************************************************/
26 #define NS_DEFINE_VK(aDOMKeyName, aDOMKeyCode) NS_##aDOMKeyName = aDOMKeyCode
42 class PPluginInstanceChild
;
43 } // namespace plugins
45 /******************************************************************************
46 * mozilla::AlternativeCharCode
48 * This stores alternative charCode values of a key event with some modifiers.
49 * The stored values proper for testing shortcut key or access key.
50 ******************************************************************************/
52 struct AlternativeCharCode
54 AlternativeCharCode(uint32_t aUnshiftedCharCode
, uint32_t aShiftedCharCode
) :
55 mUnshiftedCharCode(aUnshiftedCharCode
), mShiftedCharCode(aShiftedCharCode
)
58 uint32_t mUnshiftedCharCode
;
59 uint32_t mShiftedCharCode
;
62 /******************************************************************************
63 * mozilla::WidgetKeyboardEvent
64 ******************************************************************************/
66 class WidgetKeyboardEvent
: public WidgetInputEvent
69 friend class dom::PBrowserParent
;
70 friend class dom::PBrowserChild
;
77 virtual WidgetKeyboardEvent
* AsKeyboardEvent() MOZ_OVERRIDE
{ return this; }
79 WidgetKeyboardEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
80 WidgetInputEvent(aIsTrusted
, aMessage
, aWidget
, NS_KEY_EVENT
),
81 keyCode(0), charCode(0),
82 location(nsIDOMKeyEvent::DOM_KEY_LOCATION_STANDARD
),
83 isChar(false), mIsRepeat(false),
84 mKeyNameIndex(mozilla::KEY_NAME_INDEX_Unidentified
),
85 mNativeKeyEvent(nullptr),
90 // A DOM keyCode value or 0. If a keypress event whose charCode is 0, this
93 // If the instance is a keypress event of a printable key, this is a UTF-16
94 // value of the key. Otherwise, 0. This value must not be a control
95 // character when some modifiers are active. Then, this value should be an
96 // unmodified value except Shift and AltGr.
98 // One of nsIDOMKeyEvent::DOM_KEY_LOCATION_*
100 // OS translated Unicode chars which are used for accesskey and accelkey
101 // handling. The handlers will try from first character to last character.
102 nsTArray
<AlternativeCharCode
> alternativeCharCodes
;
103 // Indicates whether the event signifies a printable character
105 // Indicates whether the event is generated by auto repeat or not.
106 // if this is keyup event, always false.
108 // DOM KeyboardEvent.key
109 KeyNameIndex mKeyNameIndex
;
110 // OS-specific native event can optionally be preserved
111 void* mNativeKeyEvent
;
112 // Unique id associated with a keydown / keypress event. Used in identifing
113 // keypress events for removal from async event dispatch queue in metrofx
114 // after preventDefault is called on keydown events. It's ok if this wraps
115 // over long periods.
118 void GetDOMKeyName(nsAString
& aKeyName
)
120 GetDOMKeyName(mKeyNameIndex
, aKeyName
);
123 static void GetDOMKeyName(mozilla::KeyNameIndex aKeyNameIndex
,
126 #define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) \
127 case KEY_NAME_INDEX_##aCPPName: \
128 aKeyName.Assign(NS_LITERAL_STRING(aDOMKeyName)); return;
129 switch (aKeyNameIndex
) {
130 #include "nsDOMKeyNameList.h"
131 case KEY_NAME_INDEX_USE_STRING
:
136 #undef NS_DEFINE_KEYNAME
139 void AssignKeyEventData(const WidgetKeyboardEvent
& aEvent
, bool aCopyTargets
)
141 AssignInputEventData(aEvent
, aCopyTargets
);
143 keyCode
= aEvent
.keyCode
;
144 charCode
= aEvent
.charCode
;
145 location
= aEvent
.location
;
146 alternativeCharCodes
= aEvent
.alternativeCharCodes
;
147 isChar
= aEvent
.isChar
;
148 mIsRepeat
= aEvent
.mIsRepeat
;
149 mKeyNameIndex
= aEvent
.mKeyNameIndex
;
150 // Don't copy mNativeKeyEvent because it may be referred after its instance
152 mNativeKeyEvent
= nullptr;
153 mUniqueId
= aEvent
.mUniqueId
;
157 /******************************************************************************
158 * mozilla::WidgetTextEvent
160 * XXX WidgetTextEvent is fired with compositionupdate event almost every time.
161 * This wastes performance and the cost of mantaining each platform's
162 * implementation. Therefore, we should merge WidgetTextEvent and
163 * WidgetCompositionEvent. Then, DOM compositionupdate should be fired
164 * from TextComposition automatically.
165 ******************************************************************************/
167 class WidgetTextEvent
: public WidgetGUIEvent
170 friend class dom::PBrowserParent
;
171 friend class dom::PBrowserChild
;
172 friend class plugins::PPluginInstanceChild
;
182 virtual WidgetTextEvent
* AsTextEvent() MOZ_OVERRIDE
{ return this; }
184 WidgetTextEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
185 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_TEXT_EVENT
),
186 rangeCount(0), rangeArray(nullptr), isChar(false)
190 // The composition string or the commit string.
192 // Count of rangeArray.
194 // Pointer to the first item of the ranges (clauses).
195 // Note that the range array may not specify a caret position; in that
196 // case there will be no range of type NS_TEXTRANGE_CARETPOSITION in the
198 TextRangeArray rangeArray
;
199 // Indicates whether the event signifies printable text.
200 // XXX This is not a standard, and most platforms don't set this properly.
201 // So, perhaps, we can get rid of this.
204 void AssignTextEventData(const WidgetTextEvent
& aEvent
, bool aCopyTargets
)
206 AssignGUIEventData(aEvent
, aCopyTargets
);
208 isChar
= aEvent
.isChar
;
210 // Currently, we don't need to copy the other members because they are
211 // for internal use only (not available from JS).
215 /******************************************************************************
216 * mozilla::WidgetCompositionEvent
217 ******************************************************************************/
219 class WidgetCompositionEvent
: public WidgetGUIEvent
222 friend class mozilla::dom::PBrowserParent
;
223 friend class mozilla::dom::PBrowserChild
;
225 WidgetCompositionEvent()
233 virtual WidgetCompositionEvent
* AsCompositionEvent() MOZ_OVERRIDE
238 WidgetCompositionEvent(bool aIsTrusted
, uint32_t aMessage
,
239 nsIWidget
* aWidget
) :
240 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_COMPOSITION_EVENT
)
242 // XXX compositionstart is cancelable in draft of DOM3 Events.
243 // However, it doesn't make sense for us, we cannot cancel composition
244 // when we send compositionstart event.
245 mFlags
.mCancelable
= false;
248 // The composition string or the commit string. If the instance is a
249 // compositionstart event, this is initialized with selected text by
250 // TextComposition automatically.
253 void AssignCompositionEventData(const WidgetCompositionEvent
& aEvent
,
256 AssignGUIEventData(aEvent
, aCopyTargets
);
262 /******************************************************************************
263 * mozilla::WidgetQueryContentEvent
264 ******************************************************************************/
266 class WidgetQueryContentEvent
: public WidgetGUIEvent
269 friend class dom::PBrowserParent
;
270 friend class dom::PBrowserChild
;
272 WidgetQueryContentEvent()
274 MOZ_CRASH("WidgetQueryContentEvent is created without proper arguments");
278 virtual WidgetQueryContentEvent
* AsQueryContentEvent() MOZ_OVERRIDE
283 WidgetQueryContentEvent(bool aIsTrusted
, uint32_t aMessage
,
284 nsIWidget
* aWidget
) :
285 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_QUERY_CONTENT_EVENT
),
286 mSucceeded(false), mWasAsync(false)
290 void InitForQueryTextContent(uint32_t aOffset
, uint32_t aLength
)
292 NS_ASSERTION(message
== NS_QUERY_TEXT_CONTENT
,
293 "wrong initializer is called");
294 mInput
.mOffset
= aOffset
;
295 mInput
.mLength
= aLength
;
298 void InitForQueryCaretRect(uint32_t aOffset
)
300 NS_ASSERTION(message
== NS_QUERY_CARET_RECT
,
301 "wrong initializer is called");
302 mInput
.mOffset
= aOffset
;
305 void InitForQueryTextRect(uint32_t aOffset
, uint32_t aLength
)
307 NS_ASSERTION(message
== NS_QUERY_TEXT_RECT
,
308 "wrong initializer is called");
309 mInput
.mOffset
= aOffset
;
310 mInput
.mLength
= aLength
;
313 void InitForQueryDOMWidgetHittest(const mozilla::LayoutDeviceIntPoint
& aPoint
)
315 NS_ASSERTION(message
== NS_QUERY_DOM_WIDGET_HITTEST
,
316 "wrong initializer is called");
320 uint32_t GetSelectionStart(void) const
322 NS_ASSERTION(message
== NS_QUERY_SELECTED_TEXT
,
323 "not querying selection");
324 return mReply
.mOffset
+ (mReply
.mReversed
? mReply
.mString
.Length() : 0);
327 uint32_t GetSelectionEnd(void) const
329 NS_ASSERTION(message
== NS_QUERY_SELECTED_TEXT
,
330 "not querying selection");
331 return mReply
.mOffset
+ (mReply
.mReversed
? 0 : mReply
.mString
.Length());
346 // Finally, the coordinates is system coordinates.
348 // The return widget has the caret. This is set at all query events.
349 nsIWidget
* mFocusedWidget
;
350 // true if selection is reversed (end < start)
352 // true if the selection exists
354 // true if DOM element under mouse belongs to widget
356 // used by NS_QUERY_SELECTION_AS_TRANSFERABLE
357 nsCOMPtr
<nsITransferable
> mTransferable
;
362 NOT_FOUND
= UINT32_MAX
365 // values of mComputedScrollAction
374 /******************************************************************************
375 * mozilla::WidgetSelectionEvent
376 ******************************************************************************/
378 class WidgetSelectionEvent
: public WidgetGUIEvent
381 friend class mozilla::dom::PBrowserParent
;
382 friend class mozilla::dom::PBrowserChild
;
384 WidgetSelectionEvent()
386 MOZ_CRASH("WidgetSelectionEvent is created without proper arguments");
393 virtual WidgetSelectionEvent
* AsSelectionEvent() MOZ_OVERRIDE
398 WidgetSelectionEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
399 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_SELECTION_EVENT
),
400 mExpandToClusterBoundary(true), mSucceeded(false)
404 // Start offset of selection
406 // Length of selection
408 // Selection "anchor" should be in front
410 // Cluster-based or character-based
411 bool mExpandToClusterBoundary
;
412 // true if setting selection succeeded.
416 } // namespace mozilla
418 #endif // mozilla_TextEvents_h__