Bug 939654 - Remove unnecessary wait from instantiating MediaResourceManagerService...
[gecko.git] / widget / TextEvents.h
blobaa0e40ed1a0c045c3e76e734267756b35d25f37a
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__
9 #include <stdint.h>
11 #include "mozilla/Assertions.h"
12 #include "mozilla/BasicEvents.h"
13 #include "mozilla/EventForwards.h" // for KeyNameIndex, temporarily
14 #include "mozilla/TextRange.h"
15 #include "nsCOMPtr.h"
16 #include "nsIDOMKeyEvent.h"
17 #include "nsITransferable.h"
18 #include "nsRect.h"
19 #include "nsStringGlue.h"
20 #include "nsTArray.h"
22 /******************************************************************************
23 * virtual keycode values
24 ******************************************************************************/
26 #define NS_DEFINE_VK(aDOMKeyName, aDOMKeyCode) NS_##aDOMKeyName = aDOMKeyCode
28 enum
30 #include "nsVKList.h"
33 #undef NS_DEFINE_VK
35 namespace mozilla {
37 namespace dom {
38 class PBrowserParent;
39 class PBrowserChild;
40 } // namespace dom
41 namespace plugins {
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
68 private:
69 friend class dom::PBrowserParent;
70 friend class dom::PBrowserChild;
72 WidgetKeyboardEvent()
76 public:
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),
86 mUniqueId(0)
90 // A DOM keyCode value or 0. If a keypress event whose charCode is 0, this
91 // should be 0.
92 uint32_t keyCode;
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.
97 uint32_t charCode;
98 // One of nsIDOMKeyEvent::DOM_KEY_LOCATION_*
99 uint32_t 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
104 bool isChar;
105 // Indicates whether the event is generated by auto repeat or not.
106 // if this is keyup event, always false.
107 bool mIsRepeat;
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.
116 uint32_t mUniqueId;
118 void GetDOMKeyName(nsAString& aKeyName)
120 GetDOMKeyName(mKeyNameIndex, aKeyName);
123 static void GetDOMKeyName(mozilla::KeyNameIndex aKeyNameIndex,
124 nsAString& aKeyName)
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:
132 default:
133 aKeyName.Truncate();
134 return;
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
151 // is destroyed.
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
169 private:
170 friend class dom::PBrowserParent;
171 friend class dom::PBrowserChild;
172 friend class plugins::PPluginInstanceChild;
174 WidgetTextEvent()
178 public:
179 uint32_t seqno;
181 public:
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.
191 nsString theText;
192 // Count of rangeArray.
193 uint32_t rangeCount;
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
197 // array.
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.
202 bool isChar;
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
221 private:
222 friend class mozilla::dom::PBrowserParent;
223 friend class mozilla::dom::PBrowserChild;
225 WidgetCompositionEvent()
229 public:
230 uint32_t seqno;
232 public:
233 virtual WidgetCompositionEvent* AsCompositionEvent() MOZ_OVERRIDE
235 return this;
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.
251 nsString data;
253 void AssignCompositionEventData(const WidgetCompositionEvent& aEvent,
254 bool aCopyTargets)
256 AssignGUIEventData(aEvent, aCopyTargets);
258 data = aEvent.data;
262 /******************************************************************************
263 * mozilla::WidgetQueryContentEvent
264 ******************************************************************************/
266 class WidgetQueryContentEvent : public WidgetGUIEvent
268 private:
269 friend class dom::PBrowserParent;
270 friend class dom::PBrowserChild;
272 WidgetQueryContentEvent()
274 MOZ_CRASH("WidgetQueryContentEvent is created without proper arguments");
277 public:
278 virtual WidgetQueryContentEvent* AsQueryContentEvent() MOZ_OVERRIDE
280 return this;
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");
317 refPoint = aPoint;
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());
334 bool mSucceeded;
335 bool mWasAsync;
336 struct
338 uint32_t mOffset;
339 uint32_t mLength;
340 } mInput;
341 struct
343 void* mContentsRoot;
344 uint32_t mOffset;
345 nsString mString;
346 // Finally, the coordinates is system coordinates.
347 nsIntRect mRect;
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)
351 bool mReversed;
352 // true if the selection exists
353 bool mHasSelection;
354 // true if DOM element under mouse belongs to widget
355 bool mWidgetIsHit;
356 // used by NS_QUERY_SELECTION_AS_TRANSFERABLE
357 nsCOMPtr<nsITransferable> mTransferable;
358 } mReply;
360 enum
362 NOT_FOUND = UINT32_MAX
365 // values of mComputedScrollAction
366 enum
368 SCROLL_ACTION_NONE,
369 SCROLL_ACTION_LINE,
370 SCROLL_ACTION_PAGE
374 /******************************************************************************
375 * mozilla::WidgetSelectionEvent
376 ******************************************************************************/
378 class WidgetSelectionEvent : public WidgetGUIEvent
380 private:
381 friend class mozilla::dom::PBrowserParent;
382 friend class mozilla::dom::PBrowserChild;
384 WidgetSelectionEvent()
386 MOZ_CRASH("WidgetSelectionEvent is created without proper arguments");
389 public:
390 uint32_t seqno;
392 public:
393 virtual WidgetSelectionEvent* AsSelectionEvent() MOZ_OVERRIDE
395 return this;
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
405 uint32_t mOffset;
406 // Length of selection
407 uint32_t mLength;
408 // Selection "anchor" should be in front
409 bool mReversed;
410 // Cluster-based or character-based
411 bool mExpandToClusterBoundary;
412 // true if setting selection succeeded.
413 bool mSucceeded;
416 } // namespace mozilla
418 #endif // mozilla_TextEvents_h__