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
), isChar(0),
83 mKeyNameIndex(mozilla::KEY_NAME_INDEX_Unidentified
),
84 mNativeKeyEvent(nullptr),
89 // A DOM keyCode value or 0. If a keypress event whose charCode is 0, this
92 // If the instance is a keypress event of a printable key, this is a UTF-16
93 // value of the key. Otherwise, 0. This value must not be a control
94 // character when some modifiers are active. Then, this value should be an
95 // unmodified value except Shift and AltGr.
97 // One of nsIDOMKeyEvent::DOM_KEY_LOCATION_*
99 // OS translated Unicode chars which are used for accesskey and accelkey
100 // handling. The handlers will try from first character to last character.
101 nsTArray
<AlternativeCharCode
> alternativeCharCodes
;
102 // Indicates whether the event signifies a printable character
104 // DOM KeyboardEvent.key
105 KeyNameIndex mKeyNameIndex
;
106 // OS-specific native event can optionally be preserved
107 void* mNativeKeyEvent
;
108 // Unique id associated with a keydown / keypress event. Used in identifing
109 // keypress events for removal from async event dispatch queue in metrofx
110 // after preventDefault is called on keydown events. It's ok if this wraps
111 // over long periods.
114 void GetDOMKeyName(nsAString
& aKeyName
)
116 GetDOMKeyName(mKeyNameIndex
, aKeyName
);
119 static void GetDOMKeyName(mozilla::KeyNameIndex aKeyNameIndex
,
122 #define NS_DEFINE_KEYNAME(aCPPName, aDOMKeyName) \
123 case KEY_NAME_INDEX_##aCPPName: \
124 aKeyName.Assign(NS_LITERAL_STRING(aDOMKeyName)); return;
125 switch (aKeyNameIndex
) {
126 #include "nsDOMKeyNameList.h"
127 case KEY_NAME_INDEX_USE_STRING
:
132 #undef NS_DEFINE_KEYNAME
135 void AssignKeyEventData(const WidgetKeyboardEvent
& aEvent
, bool aCopyTargets
)
137 AssignInputEventData(aEvent
, aCopyTargets
);
139 keyCode
= aEvent
.keyCode
;
140 charCode
= aEvent
.charCode
;
141 location
= aEvent
.location
;
142 alternativeCharCodes
= aEvent
.alternativeCharCodes
;
143 isChar
= aEvent
.isChar
;
144 mKeyNameIndex
= aEvent
.mKeyNameIndex
;
145 // Don't copy mNativeKeyEvent because it may be referred after its instance
147 mNativeKeyEvent
= nullptr;
148 mUniqueId
= aEvent
.mUniqueId
;
152 /******************************************************************************
153 * mozilla::WidgetTextEvent
155 * XXX WidgetTextEvent is fired with compositionupdate event almost every time.
156 * This wastes performance and the cost of mantaining each platform's
157 * implementation. Therefore, we should merge WidgetTextEvent and
158 * WidgetCompositionEvent. Then, DOM compositionupdate should be fired
159 * from TextComposition automatically.
160 ******************************************************************************/
162 class WidgetTextEvent
: public WidgetGUIEvent
165 friend class dom::PBrowserParent
;
166 friend class dom::PBrowserChild
;
167 friend class plugins::PPluginInstanceChild
;
177 virtual WidgetTextEvent
* AsTextEvent() MOZ_OVERRIDE
{ return this; }
179 WidgetTextEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
180 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_TEXT_EVENT
),
181 rangeCount(0), rangeArray(nullptr), isChar(false)
185 // The composition string or the commit string.
187 // Count of rangeArray.
189 // Pointer to the first item of the ranges (clauses).
190 // Note that the range array may not specify a caret position; in that
191 // case there will be no range of type NS_TEXTRANGE_CARETPOSITION in the
193 TextRangeArray rangeArray
;
194 // Indicates whether the event signifies printable text.
195 // XXX This is not a standard, and most platforms don't set this properly.
196 // So, perhaps, we can get rid of this.
199 void AssignTextEventData(const WidgetTextEvent
& aEvent
, bool aCopyTargets
)
201 AssignGUIEventData(aEvent
, aCopyTargets
);
203 isChar
= aEvent
.isChar
;
205 // Currently, we don't need to copy the other members because they are
206 // for internal use only (not available from JS).
210 /******************************************************************************
211 * mozilla::WidgetCompositionEvent
212 ******************************************************************************/
214 class WidgetCompositionEvent
: public WidgetGUIEvent
217 friend class mozilla::dom::PBrowserParent
;
218 friend class mozilla::dom::PBrowserChild
;
220 WidgetCompositionEvent()
228 virtual WidgetCompositionEvent
* AsCompositionEvent() MOZ_OVERRIDE
233 WidgetCompositionEvent(bool aIsTrusted
, uint32_t aMessage
,
234 nsIWidget
* aWidget
) :
235 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_COMPOSITION_EVENT
)
237 // XXX compositionstart is cancelable in draft of DOM3 Events.
238 // However, it doesn't make sense for us, we cannot cancel composition
239 // when we send compositionstart event.
240 mFlags
.mCancelable
= false;
243 // The composition string or the commit string. If the instance is a
244 // compositionstart event, this is initialized with selected text by
245 // TextComposition automatically.
248 void AssignCompositionEventData(const WidgetCompositionEvent
& aEvent
,
251 AssignGUIEventData(aEvent
, aCopyTargets
);
257 /******************************************************************************
258 * mozilla::WidgetQueryContentEvent
259 ******************************************************************************/
261 class WidgetQueryContentEvent
: public WidgetGUIEvent
264 friend class dom::PBrowserParent
;
265 friend class dom::PBrowserChild
;
267 WidgetQueryContentEvent()
269 MOZ_CRASH("WidgetQueryContentEvent is created without proper arguments");
273 virtual WidgetQueryContentEvent
* AsQueryContentEvent() MOZ_OVERRIDE
278 WidgetQueryContentEvent(bool aIsTrusted
, uint32_t aMessage
,
279 nsIWidget
* aWidget
) :
280 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_QUERY_CONTENT_EVENT
),
281 mSucceeded(false), mWasAsync(false)
285 void InitForQueryTextContent(uint32_t aOffset
, uint32_t aLength
)
287 NS_ASSERTION(message
== NS_QUERY_TEXT_CONTENT
,
288 "wrong initializer is called");
289 mInput
.mOffset
= aOffset
;
290 mInput
.mLength
= aLength
;
293 void InitForQueryCaretRect(uint32_t aOffset
)
295 NS_ASSERTION(message
== NS_QUERY_CARET_RECT
,
296 "wrong initializer is called");
297 mInput
.mOffset
= aOffset
;
300 void InitForQueryTextRect(uint32_t aOffset
, uint32_t aLength
)
302 NS_ASSERTION(message
== NS_QUERY_TEXT_RECT
,
303 "wrong initializer is called");
304 mInput
.mOffset
= aOffset
;
305 mInput
.mLength
= aLength
;
308 void InitForQueryDOMWidgetHittest(const mozilla::LayoutDeviceIntPoint
& aPoint
)
310 NS_ASSERTION(message
== NS_QUERY_DOM_WIDGET_HITTEST
,
311 "wrong initializer is called");
315 uint32_t GetSelectionStart(void) const
317 NS_ASSERTION(message
== NS_QUERY_SELECTED_TEXT
,
318 "not querying selection");
319 return mReply
.mOffset
+ (mReply
.mReversed
? mReply
.mString
.Length() : 0);
322 uint32_t GetSelectionEnd(void) const
324 NS_ASSERTION(message
== NS_QUERY_SELECTED_TEXT
,
325 "not querying selection");
326 return mReply
.mOffset
+ (mReply
.mReversed
? 0 : mReply
.mString
.Length());
341 // Finally, the coordinates is system coordinates.
343 // The return widget has the caret. This is set at all query events.
344 nsIWidget
* mFocusedWidget
;
345 // true if selection is reversed (end < start)
347 // true if the selection exists
349 // true if DOM element under mouse belongs to widget
351 // used by NS_QUERY_SELECTION_AS_TRANSFERABLE
352 nsCOMPtr
<nsITransferable
> mTransferable
;
357 NOT_FOUND
= UINT32_MAX
360 // values of mComputedScrollAction
369 /******************************************************************************
370 * mozilla::WidgetSelectionEvent
371 ******************************************************************************/
373 class WidgetSelectionEvent
: public WidgetGUIEvent
376 friend class mozilla::dom::PBrowserParent
;
377 friend class mozilla::dom::PBrowserChild
;
379 WidgetSelectionEvent()
381 MOZ_CRASH("WidgetSelectionEvent is created without proper arguments");
388 virtual WidgetSelectionEvent
* AsSelectionEvent() MOZ_OVERRIDE
393 WidgetSelectionEvent(bool aIsTrusted
, uint32_t aMessage
, nsIWidget
* aWidget
) :
394 WidgetGUIEvent(aIsTrusted
, aMessage
, aWidget
, NS_SELECTION_EVENT
),
395 mExpandToClusterBoundary(true), mSucceeded(false)
399 // Start offset of selection
401 // Length of selection
403 // Selection "anchor" should be in front
405 // Cluster-based or character-based
406 bool mExpandToClusterBoundary
;
407 // true if setting selection succeeded.
411 } // namespace mozilla
413 #endif // mozilla_TextEvents_h__