Bug 932076 - Add check for MediaExtractor creation failure. r=doublec
[gecko.git] / widget / TextEvents.h
blob81719a84e78f94491ce2c6e9a66ae0ad1b2ddc31
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), isChar(0),
83 mKeyNameIndex(mozilla::KEY_NAME_INDEX_Unidentified),
84 mNativeKeyEvent(nullptr),
85 mUniqueId(0)
89 // A DOM keyCode value or 0. If a keypress event whose charCode is 0, this
90 // should be 0.
91 uint32_t keyCode;
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.
96 uint32_t charCode;
97 // One of nsIDOMKeyEvent::DOM_KEY_LOCATION_*
98 uint32_t 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
103 bool isChar;
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.
112 uint32_t mUniqueId;
114 void GetDOMKeyName(nsAString& aKeyName)
116 GetDOMKeyName(mKeyNameIndex, aKeyName);
119 static void GetDOMKeyName(mozilla::KeyNameIndex aKeyNameIndex,
120 nsAString& aKeyName)
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:
128 default:
129 aKeyName.Truncate();
130 return;
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
146 // is destroyed.
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
164 private:
165 friend class dom::PBrowserParent;
166 friend class dom::PBrowserChild;
167 friend class plugins::PPluginInstanceChild;
169 WidgetTextEvent()
173 public:
174 uint32_t seqno;
176 public:
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.
186 nsString theText;
187 // Count of rangeArray.
188 uint32_t rangeCount;
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
192 // array.
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.
197 bool isChar;
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
216 private:
217 friend class mozilla::dom::PBrowserParent;
218 friend class mozilla::dom::PBrowserChild;
220 WidgetCompositionEvent()
224 public:
225 uint32_t seqno;
227 public:
228 virtual WidgetCompositionEvent* AsCompositionEvent() MOZ_OVERRIDE
230 return this;
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.
246 nsString data;
248 void AssignCompositionEventData(const WidgetCompositionEvent& aEvent,
249 bool aCopyTargets)
251 AssignGUIEventData(aEvent, aCopyTargets);
253 data = aEvent.data;
257 /******************************************************************************
258 * mozilla::WidgetQueryContentEvent
259 ******************************************************************************/
261 class WidgetQueryContentEvent : public WidgetGUIEvent
263 private:
264 friend class dom::PBrowserParent;
265 friend class dom::PBrowserChild;
267 WidgetQueryContentEvent()
269 MOZ_CRASH("WidgetQueryContentEvent is created without proper arguments");
272 public:
273 virtual WidgetQueryContentEvent* AsQueryContentEvent() MOZ_OVERRIDE
275 return this;
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");
312 refPoint = aPoint;
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());
329 bool mSucceeded;
330 bool mWasAsync;
331 struct
333 uint32_t mOffset;
334 uint32_t mLength;
335 } mInput;
336 struct
338 void* mContentsRoot;
339 uint32_t mOffset;
340 nsString mString;
341 // Finally, the coordinates is system coordinates.
342 nsIntRect mRect;
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)
346 bool mReversed;
347 // true if the selection exists
348 bool mHasSelection;
349 // true if DOM element under mouse belongs to widget
350 bool mWidgetIsHit;
351 // used by NS_QUERY_SELECTION_AS_TRANSFERABLE
352 nsCOMPtr<nsITransferable> mTransferable;
353 } mReply;
355 enum
357 NOT_FOUND = UINT32_MAX
360 // values of mComputedScrollAction
361 enum
363 SCROLL_ACTION_NONE,
364 SCROLL_ACTION_LINE,
365 SCROLL_ACTION_PAGE
369 /******************************************************************************
370 * mozilla::WidgetSelectionEvent
371 ******************************************************************************/
373 class WidgetSelectionEvent : public WidgetGUIEvent
375 private:
376 friend class mozilla::dom::PBrowserParent;
377 friend class mozilla::dom::PBrowserChild;
379 WidgetSelectionEvent()
381 MOZ_CRASH("WidgetSelectionEvent is created without proper arguments");
384 public:
385 uint32_t seqno;
387 public:
388 virtual WidgetSelectionEvent* AsSelectionEvent() MOZ_OVERRIDE
390 return this;
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
400 uint32_t mOffset;
401 // Length of selection
402 uint32_t mLength;
403 // Selection "anchor" should be in front
404 bool mReversed;
405 // Cluster-based or character-based
406 bool mExpandToClusterBoundary;
407 // true if setting selection succeeded.
408 bool mSucceeded;
411 } // namespace mozilla
413 #endif // mozilla_TextEvents_h__