2015-01-28 06:27:30 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-02-19 06:50:18 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-01-28 06:27:30 +00:00
|
|
|
#include "mozilla/TextEvents.h"
|
|
|
|
#include "mozilla/TextEventDispatcher.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsView.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* TextEventDispatcher
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-02-19 06:50:18 +00:00
|
|
|
bool TextEventDispatcher::sDispatchKeyEventsDuringComposition = false;
|
|
|
|
|
2015-01-28 06:27:30 +00:00
|
|
|
TextEventDispatcher::TextEventDispatcher(nsIWidget* aWidget)
|
|
|
|
: mWidget(aWidget)
|
2015-02-19 06:50:20 +00:00
|
|
|
, mDispatchingEvent(0)
|
2016-03-16 04:47:47 +00:00
|
|
|
, mInputTransactionType(eNoInputTransaction)
|
2015-01-28 06:27:31 +00:00
|
|
|
, mIsComposing(false)
|
2015-01-28 06:27:30 +00:00
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mWidget, "aWidget must not be nullptr");
|
2015-02-19 06:50:18 +00:00
|
|
|
|
|
|
|
static bool sInitialized = false;
|
|
|
|
if (!sInitialized) {
|
|
|
|
Preferences::AddBoolVarCache(
|
|
|
|
&sDispatchKeyEventsDuringComposition,
|
|
|
|
"dom.keyboardevent.dispatch_during_composition",
|
|
|
|
false);
|
|
|
|
sInitialized = true;
|
|
|
|
}
|
2015-01-28 06:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-10 08:09:29 +00:00
|
|
|
TextEventDispatcher::BeginInputTransaction(
|
|
|
|
TextEventDispatcherListener* aListener)
|
2015-01-28 06:27:30 +00:00
|
|
|
{
|
2016-03-16 04:47:48 +00:00
|
|
|
return BeginInputTransactionInternal(aListener,
|
|
|
|
eSameProcessSyncInputTransaction);
|
2015-01-28 06:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-03-16 04:47:47 +00:00
|
|
|
TextEventDispatcher::BeginTestInputTransaction(
|
2016-03-16 04:47:48 +00:00
|
|
|
TextEventDispatcherListener* aListener,
|
|
|
|
bool aIsAPZAware)
|
2015-01-28 06:27:30 +00:00
|
|
|
{
|
2016-03-16 04:47:48 +00:00
|
|
|
return BeginInputTransactionInternal(aListener,
|
|
|
|
aIsAPZAware ? eAsyncTestInputTransaction :
|
|
|
|
eSameProcessSyncTestInputTransaction);
|
2016-03-16 04:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-03-16 04:47:47 +00:00
|
|
|
TextEventDispatcher::BeginNativeInputTransaction()
|
2016-03-16 04:47:47 +00:00
|
|
|
{
|
2016-03-16 04:47:47 +00:00
|
|
|
if (NS_WARN_IF(!mWidget)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
RefPtr<TextEventDispatcherListener> listener =
|
|
|
|
mWidget->GetNativeTextEventDispatcherListener();
|
|
|
|
if (NS_WARN_IF(!listener)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return BeginInputTransactionInternal(listener, eNativeInputTransaction);
|
2015-01-28 06:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-10 08:09:29 +00:00
|
|
|
TextEventDispatcher::BeginInputTransactionInternal(
|
|
|
|
TextEventDispatcherListener* aListener,
|
2016-03-16 04:47:47 +00:00
|
|
|
InputTransactionType aType)
|
2015-01-28 06:27:32 +00:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aListener)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
nsCOMPtr<TextEventDispatcherListener> listener = do_QueryReferent(mListener);
|
|
|
|
if (listener) {
|
2016-03-16 04:47:47 +00:00
|
|
|
if (listener == aListener && mInputTransactionType == aType) {
|
2015-01-28 06:27:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-02-19 06:50:20 +00:00
|
|
|
// If this has composition or is dispatching an event, any other listener
|
2015-02-19 06:50:21 +00:00
|
|
|
// can steal ownership. Especially, if the latter case is allowed,
|
|
|
|
// nobody cannot begin input transaction with this if a modal dialog is
|
|
|
|
// opened during dispatching an event.
|
|
|
|
if (IsComposing() || IsDispatchingEvent()) {
|
2015-01-28 06:27:32 +00:00
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mListener = do_GetWeakReference(aListener);
|
2016-03-16 04:47:47 +00:00
|
|
|
mInputTransactionType = aType;
|
2015-01-28 06:27:32 +00:00
|
|
|
if (listener && listener != aListener) {
|
|
|
|
listener->OnRemovedFrom(this);
|
2015-01-28 06:27:30 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-11 03:53:42 +00:00
|
|
|
void
|
|
|
|
TextEventDispatcher::EndInputTransaction(TextEventDispatcherListener* aListener)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(IsComposing()) || NS_WARN_IF(IsDispatchingEvent())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:47:47 +00:00
|
|
|
mInputTransactionType = eNoInputTransaction;
|
|
|
|
|
2015-06-11 03:53:42 +00:00
|
|
|
nsCOMPtr<TextEventDispatcherListener> listener = do_QueryReferent(mListener);
|
|
|
|
if (NS_WARN_IF(!listener)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(listener != aListener)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListener = nullptr;
|
|
|
|
listener->OnRemovedFrom(this);
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:30 +00:00
|
|
|
void
|
|
|
|
TextEventDispatcher::OnDestroyWidget()
|
|
|
|
{
|
|
|
|
mWidget = nullptr;
|
|
|
|
mPendingComposition.Clear();
|
2015-01-28 06:27:32 +00:00
|
|
|
nsCOMPtr<TextEventDispatcherListener> listener = do_QueryReferent(mListener);
|
|
|
|
mListener = nullptr;
|
2016-03-16 04:47:47 +00:00
|
|
|
mInputTransactionType = eNoInputTransaction;
|
2015-01-28 06:27:32 +00:00
|
|
|
if (listener) {
|
|
|
|
listener->OnRemovedFrom(this);
|
|
|
|
}
|
2015-01-28 06:27:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::GetState() const
|
|
|
|
{
|
2015-01-28 06:27:32 +00:00
|
|
|
nsCOMPtr<TextEventDispatcherListener> listener = do_QueryReferent(mListener);
|
|
|
|
if (!listener) {
|
2015-01-28 06:27:31 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
if (!mWidget || mWidget->Destroyed()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-19 06:50:18 +00:00
|
|
|
TextEventDispatcher::InitEvent(WidgetGUIEvent& aEvent) const
|
2015-01-28 06:27:31 +00:00
|
|
|
{
|
2016-03-28 04:29:42 +00:00
|
|
|
aEvent.mTime = PR_IntervalNow();
|
2015-02-19 06:50:18 +00:00
|
|
|
aEvent.refPoint = LayoutDeviceIntPoint(0, 0);
|
2016-03-16 04:47:48 +00:00
|
|
|
aEvent.mFlags.mIsSynthesizedForTests = IsForTests();
|
2015-12-11 06:15:57 +00:00
|
|
|
if (aEvent.mClass != eCompositionEventClass) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-16 04:47:47 +00:00
|
|
|
void* pseudoIMEContext = GetPseudoIMEContext();
|
|
|
|
if (pseudoIMEContext) {
|
|
|
|
aEvent.AsCompositionEvent()->mNativeIMEContext.
|
|
|
|
InitWithRawNativeIMEContext(pseudoIMEContext);
|
2015-12-11 06:15:57 +00:00
|
|
|
}
|
2016-03-16 04:47:47 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(!XRE_IsContentProcess(),
|
|
|
|
"Why did the content process start native event transaction?");
|
|
|
|
MOZ_ASSERT(aEvent.AsCompositionEvent()->mNativeIMEContext.IsValid(),
|
|
|
|
"Native IME context shouldn't be invalid");
|
|
|
|
}
|
|
|
|
#endif // #ifdef DEBUG
|
2015-01-28 06:27:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 06:50:20 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::DispatchEvent(nsIWidget* aWidget,
|
|
|
|
WidgetGUIEvent& aEvent,
|
|
|
|
nsEventStatus& aStatus)
|
2015-08-04 09:52:00 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aEvent.AsInputEvent(), "Use DispatchInputEvent()");
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<TextEventDispatcher> kungFuDeathGrip(this);
|
2015-08-04 09:52:00 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget(aWidget);
|
|
|
|
mDispatchingEvent++;
|
|
|
|
nsresult rv = widget->DispatchEvent(&aEvent, aStatus);
|
|
|
|
mDispatchingEvent--;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::DispatchInputEvent(nsIWidget* aWidget,
|
|
|
|
WidgetInputEvent& aEvent,
|
2016-03-16 04:47:48 +00:00
|
|
|
nsEventStatus& aStatus)
|
2015-02-19 06:50:20 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<TextEventDispatcher> kungFuDeathGrip(this);
|
2015-02-19 06:50:20 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget(aWidget);
|
|
|
|
mDispatchingEvent++;
|
2015-03-24 22:00:52 +00:00
|
|
|
|
2015-08-04 09:52:00 +00:00
|
|
|
// If the event is dispatched via nsIWidget::DispatchInputEvent(), it
|
|
|
|
// sends the event to the parent process first since APZ needs to handle it
|
|
|
|
// first. However, some callers (e.g., keyboard apps on B2G and tests
|
|
|
|
// expecting synchronous dispatch) don't want this to do that.
|
2015-03-24 22:00:52 +00:00
|
|
|
nsresult rv = NS_OK;
|
2016-03-16 04:47:48 +00:00
|
|
|
if (ShouldSendInputEventToAPZ()) {
|
2015-08-04 09:52:00 +00:00
|
|
|
aStatus = widget->DispatchInputEvent(&aEvent);
|
2015-03-24 22:00:52 +00:00
|
|
|
} else {
|
|
|
|
rv = widget->DispatchEvent(&aEvent, aStatus);
|
|
|
|
}
|
|
|
|
|
2015-02-19 06:50:20 +00:00
|
|
|
mDispatchingEvent--;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsresult
|
2016-03-16 04:47:48 +00:00
|
|
|
TextEventDispatcher::StartComposition(nsEventStatus& aStatus,
|
|
|
|
const WidgetEventTime* aEventTime)
|
2015-01-28 06:27:31 +00:00
|
|
|
{
|
|
|
|
aStatus = nsEventStatus_eIgnore;
|
|
|
|
|
|
|
|
nsresult rv = GetState();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
if (NS_WARN_IF(mIsComposing)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsComposing = true;
|
2015-09-11 12:21:27 +00:00
|
|
|
WidgetCompositionEvent compositionStartEvent(true, eCompositionStart,
|
2015-02-19 06:50:20 +00:00
|
|
|
mWidget);
|
2015-01-28 06:27:31 +00:00
|
|
|
InitEvent(compositionStartEvent);
|
2016-03-16 04:47:48 +00:00
|
|
|
if (aEventTime) {
|
|
|
|
compositionStartEvent.AssignEventTime(*aEventTime);
|
|
|
|
}
|
2015-02-19 06:50:20 +00:00
|
|
|
rv = DispatchEvent(mWidget, compositionStartEvent, aStatus);
|
2015-01-28 06:27:31 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::StartCompositionAutomaticallyIfNecessary(
|
2016-03-16 04:47:48 +00:00
|
|
|
nsEventStatus& aStatus,
|
|
|
|
const WidgetEventTime* aEventTime)
|
2015-01-28 06:27:31 +00:00
|
|
|
{
|
|
|
|
if (IsComposing()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:47:48 +00:00
|
|
|
nsresult rv = StartComposition(aStatus, aEventTime);
|
2015-01-28 06:27:31 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If started composition has already been committed, we shouldn't dispatch
|
|
|
|
// the compositionchange event.
|
|
|
|
if (!IsComposing()) {
|
|
|
|
aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that the widget might be destroyed during a call of
|
|
|
|
// StartComposition(). In such case, we shouldn't keep dispatching next
|
|
|
|
// event.
|
|
|
|
rv = GetState();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(rv != NS_ERROR_NOT_INITIALIZED,
|
|
|
|
"aDispatcher must still be initialized in this case");
|
|
|
|
aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
return NS_OK; // Don't throw exception in this case
|
|
|
|
}
|
|
|
|
|
|
|
|
aStatus = nsEventStatus_eIgnore;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::CommitComposition(nsEventStatus& aStatus,
|
2016-03-16 04:47:48 +00:00
|
|
|
const nsAString* aCommitString,
|
|
|
|
const WidgetEventTime* aEventTime)
|
2015-01-28 06:27:31 +00:00
|
|
|
{
|
|
|
|
aStatus = nsEventStatus_eIgnore;
|
|
|
|
|
|
|
|
nsresult rv = GetState();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:32 +00:00
|
|
|
// When there is no composition, caller shouldn't try to commit composition
|
|
|
|
// with non-existing composition string nor commit composition with empty
|
|
|
|
// string.
|
|
|
|
if (NS_WARN_IF(!IsComposing() &&
|
|
|
|
(!aCommitString || aCommitString->IsEmpty()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget(mWidget);
|
2016-03-16 04:47:48 +00:00
|
|
|
rv = StartCompositionAutomaticallyIfNecessary(aStatus, aEventTime);
|
2015-01-28 06:27:31 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (aStatus == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
// End current composition and make this free for other IMEs.
|
|
|
|
mIsComposing = false;
|
|
|
|
|
2015-09-11 12:21:27 +00:00
|
|
|
EventMessage message = aCommitString ? eCompositionCommit :
|
2015-09-11 12:21:26 +00:00
|
|
|
eCompositionCommitAsIs;
|
2015-01-28 06:27:31 +00:00
|
|
|
WidgetCompositionEvent compositionCommitEvent(true, message, widget);
|
|
|
|
InitEvent(compositionCommitEvent);
|
2016-03-16 04:47:48 +00:00
|
|
|
if (aEventTime) {
|
|
|
|
compositionCommitEvent.AssignEventTime(*aEventTime);
|
|
|
|
}
|
2015-09-11 12:21:27 +00:00
|
|
|
if (message == eCompositionCommit) {
|
2015-01-28 06:27:31 +00:00
|
|
|
compositionCommitEvent.mData = *aCommitString;
|
2016-03-16 04:47:48 +00:00
|
|
|
// Don't send CRLF, replace it with LF here.
|
|
|
|
compositionCommitEvent.mData.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
|
|
|
|
NS_LITERAL_STRING("\n"));
|
2015-01-28 06:27:31 +00:00
|
|
|
}
|
2015-02-19 06:50:20 +00:00
|
|
|
rv = DispatchEvent(widget, compositionCommitEvent, aStatus);
|
2015-01-28 06:27:31 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::NotifyIME(const IMENotification& aIMENotification)
|
|
|
|
{
|
2016-03-16 04:47:47 +00:00
|
|
|
nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
// First, send the notification to current input transaction's listener.
|
2015-01-28 06:27:32 +00:00
|
|
|
nsCOMPtr<TextEventDispatcherListener> listener = do_QueryReferent(mListener);
|
2016-03-16 04:47:47 +00:00
|
|
|
if (listener) {
|
|
|
|
rv = listener->NotifyIME(this, aIMENotification);
|
2015-01-28 06:27:32 +00:00
|
|
|
}
|
2016-03-16 04:47:47 +00:00
|
|
|
|
|
|
|
if (mInputTransactionType == eNativeInputTransaction || !mWidget) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If current input transaction isn't for native event handler, we should
|
|
|
|
// send the notification to the native text event dispatcher listener
|
|
|
|
// since native event handler may need to do something from
|
|
|
|
// TextEventDispatcherListener::NotifyIME() even before there is no
|
|
|
|
// input transaction yet. For example, native IME handler may need to
|
|
|
|
// create new context at receiving NOTIFY_IME_OF_FOCUS. In this case,
|
|
|
|
// mListener may not be initialized since input transaction should be
|
|
|
|
// initialized immediately before dispatching every WidgetKeyboardEvent
|
|
|
|
// and WidgetCompositionEvent (dispatching events always occurs after
|
|
|
|
// focus move).
|
|
|
|
nsCOMPtr<TextEventDispatcherListener> nativeListener =
|
|
|
|
mWidget->GetNativeTextEventDispatcherListener();
|
|
|
|
if (!nativeListener) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
switch (aIMENotification.mMessage) {
|
|
|
|
case REQUEST_TO_COMMIT_COMPOSITION:
|
|
|
|
case REQUEST_TO_CANCEL_COMPOSITION:
|
|
|
|
// It's not necessary to notify native IME of requests.
|
|
|
|
return rv;
|
|
|
|
default: {
|
|
|
|
// Even if current input transaction's listener returns NS_OK or
|
|
|
|
// something, we need to notify native IME of notifications because
|
|
|
|
// when user typing after TIP does something, the changed information
|
|
|
|
// is necessary for them.
|
|
|
|
nsresult rv2 =
|
|
|
|
nativeListener->NotifyIME(this, aIMENotification);
|
|
|
|
// But return the result from current listener except when the
|
|
|
|
// notification isn't handled.
|
|
|
|
return rv == NS_ERROR_NOT_IMPLEMENTED ? rv2 : rv;
|
|
|
|
}
|
2015-01-28 06:27:32 +00:00
|
|
|
}
|
2015-01-28 06:27:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 06:50:18 +00:00
|
|
|
bool
|
|
|
|
TextEventDispatcher::DispatchKeyboardEvent(
|
2015-08-26 12:56:59 +00:00
|
|
|
EventMessage aMessage,
|
2015-02-19 06:50:18 +00:00
|
|
|
const WidgetKeyboardEvent& aKeyboardEvent,
|
2016-03-16 04:47:48 +00:00
|
|
|
nsEventStatus& aStatus,
|
|
|
|
void* aData)
|
2015-02-19 06:50:18 +00:00
|
|
|
{
|
2016-03-16 04:47:48 +00:00
|
|
|
return DispatchKeyboardEventInternal(aMessage, aKeyboardEvent, aStatus,
|
|
|
|
aData);
|
2015-02-19 06:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TextEventDispatcher::DispatchKeyboardEventInternal(
|
2015-08-26 12:56:59 +00:00
|
|
|
EventMessage aMessage,
|
2015-02-19 06:50:18 +00:00
|
|
|
const WidgetKeyboardEvent& aKeyboardEvent,
|
|
|
|
nsEventStatus& aStatus,
|
2016-03-16 04:47:48 +00:00
|
|
|
void* aData,
|
2015-02-19 06:50:18 +00:00
|
|
|
uint32_t aIndexOfKeypress)
|
|
|
|
{
|
2015-08-28 23:58:27 +00:00
|
|
|
MOZ_ASSERT(aMessage == eKeyDown || aMessage == eKeyUp ||
|
2015-08-28 23:58:27 +00:00
|
|
|
aMessage == eKeyPress, "Invalid aMessage value");
|
2015-02-19 06:50:18 +00:00
|
|
|
nsresult rv = GetState();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the key shouldn't cause keypress events, don't this patch them.
|
2015-08-28 23:58:27 +00:00
|
|
|
if (aMessage == eKeyPress && !aKeyboardEvent.ShouldCauseKeypressEvents()) {
|
2015-02-19 06:50:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-19 06:50:18 +00:00
|
|
|
// Basically, key events shouldn't be dispatched during composition.
|
|
|
|
if (IsComposing()) {
|
|
|
|
// However, if we need to behave like other browsers, we need the keydown
|
|
|
|
// and keyup events. Note that this behavior is also allowed by D3E spec.
|
|
|
|
// FYI: keypress events must not be fired during composition.
|
2015-08-28 23:58:27 +00:00
|
|
|
if (!sDispatchKeyEventsDuringComposition || aMessage == eKeyPress) {
|
2015-02-19 06:50:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// XXX If there was mOnlyContentDispatch for this case, it might be useful
|
|
|
|
// because our chrome doesn't assume that key events are fired during
|
|
|
|
// composition.
|
|
|
|
}
|
|
|
|
|
2015-02-19 06:50:20 +00:00
|
|
|
WidgetKeyboardEvent keyEvent(true, aMessage, mWidget);
|
2015-02-19 06:50:18 +00:00
|
|
|
InitEvent(keyEvent);
|
|
|
|
keyEvent.AssignKeyEventData(aKeyboardEvent, false);
|
|
|
|
|
|
|
|
if (aStatus == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
// If the key event should be dispatched as consumed event, marking it here.
|
|
|
|
// This is useful to prevent double action. E.g., when the key was already
|
|
|
|
// handled by system, our chrome shouldn't handle it.
|
2016-03-22 07:26:27 +00:00
|
|
|
keyEvent.PreventDefaultBeforeDispatch();
|
2015-02-19 06:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Corrects each member for the specific key event type.
|
2016-03-19 11:57:11 +00:00
|
|
|
if (keyEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING) {
|
2015-02-19 06:50:18 +00:00
|
|
|
MOZ_ASSERT(!aIndexOfKeypress,
|
2016-03-19 11:57:11 +00:00
|
|
|
"aIndexOfKeypress must be 0 for non-printable key");
|
|
|
|
// If the keyboard event isn't caused by printable key, its charCode should
|
2015-02-19 06:50:18 +00:00
|
|
|
// be 0.
|
2016-03-19 11:57:11 +00:00
|
|
|
keyEvent.SetCharCode(0);
|
2015-02-19 06:50:18 +00:00
|
|
|
} else {
|
2016-03-19 11:57:11 +00:00
|
|
|
if (aMessage == eKeyDown || aMessage == eKeyUp) {
|
|
|
|
MOZ_RELEASE_ASSERT(!aIndexOfKeypress,
|
|
|
|
"aIndexOfKeypress must be 0 for either eKeyDown or eKeyUp");
|
|
|
|
} else {
|
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
!aIndexOfKeypress || aIndexOfKeypress < keyEvent.mKeyValue.Length(),
|
|
|
|
"aIndexOfKeypress must be 0 - mKeyValue.Length() - 1");
|
|
|
|
}
|
2015-02-19 06:50:18 +00:00
|
|
|
wchar_t ch =
|
|
|
|
keyEvent.mKeyValue.IsEmpty() ? 0 : keyEvent.mKeyValue[aIndexOfKeypress];
|
2016-03-19 11:57:11 +00:00
|
|
|
keyEvent.SetCharCode(static_cast<uint32_t>(ch));
|
|
|
|
if (aMessage == eKeyPress) {
|
|
|
|
// keyCode of eKeyPress events of printable keys should be always 0.
|
|
|
|
keyEvent.keyCode = 0;
|
|
|
|
// eKeyPress events are dispatched for every character.
|
|
|
|
// So, each key value of eKeyPress events should be a character.
|
|
|
|
if (ch) {
|
|
|
|
keyEvent.mKeyValue.Assign(ch);
|
|
|
|
} else {
|
|
|
|
keyEvent.mKeyValue.Truncate();
|
|
|
|
}
|
2015-02-19 06:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 23:58:27 +00:00
|
|
|
if (aMessage == eKeyUp) {
|
2015-02-19 06:50:18 +00:00
|
|
|
// mIsRepeat of keyup event must be false.
|
|
|
|
keyEvent.mIsRepeat = false;
|
|
|
|
}
|
|
|
|
// mIsComposing should be initialized later.
|
|
|
|
keyEvent.mIsComposing = false;
|
2016-03-16 04:47:48 +00:00
|
|
|
if (mInputTransactionType == eNativeInputTransaction) {
|
|
|
|
// Copy mNativeKeyEvent here because for safety for other users of
|
|
|
|
// AssignKeyEventData(), it doesn't copy this.
|
|
|
|
keyEvent.mNativeKeyEvent = aKeyboardEvent.mNativeKeyEvent;
|
|
|
|
} else {
|
|
|
|
// If it's not a keyboard event for native key event, we should ensure that
|
|
|
|
// mNativeKeyEvent and mPluginEvent are null/empty.
|
|
|
|
keyEvent.mNativeKeyEvent = nullptr;
|
|
|
|
keyEvent.mPluginEvent.Clear();
|
|
|
|
}
|
2015-02-19 06:50:18 +00:00
|
|
|
// TODO: Manage mUniqueId here.
|
|
|
|
|
2016-03-16 04:47:48 +00:00
|
|
|
// Request the alternative char codes for the key event.
|
2016-03-19 11:57:11 +00:00
|
|
|
// eKeyDown also needs alternative char codes because nsXBLWindowKeyHandler
|
|
|
|
// needs to check if a following keypress event is reserved by chrome for
|
|
|
|
// stopping propagation of its preceding keydown event.
|
2016-03-16 04:47:48 +00:00
|
|
|
keyEvent.alternativeCharCodes.Clear();
|
2016-03-19 11:57:11 +00:00
|
|
|
if ((aMessage == eKeyDown || aMessage == eKeyPress) &&
|
2016-03-16 04:47:48 +00:00
|
|
|
(keyEvent.IsControl() || keyEvent.IsAlt() ||
|
|
|
|
keyEvent.IsMeta() || keyEvent.IsOS())) {
|
|
|
|
nsCOMPtr<TextEventDispatcherListener> listener =
|
|
|
|
do_QueryReferent(mListener);
|
|
|
|
if (listener) {
|
2016-03-16 04:47:48 +00:00
|
|
|
DebugOnly<WidgetKeyboardEvent> original(keyEvent);
|
2016-03-16 04:47:48 +00:00
|
|
|
listener->WillDispatchKeyboardEvent(this, keyEvent, aIndexOfKeypress,
|
|
|
|
aData);
|
2016-03-16 04:47:48 +00:00
|
|
|
MOZ_ASSERT(keyEvent.mMessage ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mMessage);
|
|
|
|
MOZ_ASSERT(keyEvent.keyCode ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).keyCode);
|
|
|
|
MOZ_ASSERT(keyEvent.location ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).location);
|
|
|
|
MOZ_ASSERT(keyEvent.mIsRepeat ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mIsRepeat);
|
|
|
|
MOZ_ASSERT(keyEvent.mIsComposing ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mIsComposing);
|
|
|
|
MOZ_ASSERT(keyEvent.mKeyNameIndex ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mKeyNameIndex);
|
|
|
|
MOZ_ASSERT(keyEvent.mCodeNameIndex ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mCodeNameIndex);
|
|
|
|
MOZ_ASSERT(keyEvent.mKeyValue ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mKeyValue);
|
|
|
|
MOZ_ASSERT(keyEvent.mCodeValue ==
|
|
|
|
static_cast<WidgetKeyboardEvent&>(original).mCodeValue);
|
2016-03-16 04:47:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:47:48 +00:00
|
|
|
DispatchInputEvent(mWidget, keyEvent, aStatus);
|
2015-02-19 06:50:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TextEventDispatcher::MaybeDispatchKeypressEvents(
|
|
|
|
const WidgetKeyboardEvent& aKeyboardEvent,
|
2016-03-16 04:47:48 +00:00
|
|
|
nsEventStatus& aStatus,
|
|
|
|
void* aData)
|
2015-02-19 06:50:18 +00:00
|
|
|
{
|
|
|
|
// If the key event was consumed, keypress event shouldn't be fired.
|
|
|
|
if (aStatus == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:47:49 +00:00
|
|
|
// If the key shouldn't cause keypress events, don't fire them.
|
|
|
|
if (!aKeyboardEvent.ShouldCauseKeypressEvents()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-19 06:50:18 +00:00
|
|
|
// If the key isn't a printable key or just inputting one character or
|
|
|
|
// no character, we should dispatch only one keypress. Otherwise, i.e.,
|
|
|
|
// if the key is a printable key and inputs multiple characters, keypress
|
|
|
|
// event should be dispatched the count of inputting characters times.
|
|
|
|
size_t keypressCount =
|
|
|
|
aKeyboardEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING ?
|
|
|
|
1 : std::max(static_cast<nsAString::size_type>(1),
|
|
|
|
aKeyboardEvent.mKeyValue.Length());
|
|
|
|
bool isDispatched = false;
|
|
|
|
bool consumed = false;
|
|
|
|
for (size_t i = 0; i < keypressCount; i++) {
|
|
|
|
aStatus = nsEventStatus_eIgnore;
|
2015-08-28 23:58:27 +00:00
|
|
|
if (!DispatchKeyboardEventInternal(eKeyPress, aKeyboardEvent,
|
2016-03-16 04:47:48 +00:00
|
|
|
aStatus, aData, i)) {
|
2015-02-19 06:50:18 +00:00
|
|
|
// The widget must have been gone.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
isDispatched = true;
|
|
|
|
if (!consumed) {
|
|
|
|
consumed = (aStatus == nsEventStatus_eConsumeNoDefault);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If one of the keypress event was consumed, return ConsumeNoDefault.
|
|
|
|
if (consumed) {
|
|
|
|
aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isDispatched;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:30 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* TextEventDispatcher::PendingComposition
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
TextEventDispatcher::PendingComposition::PendingComposition()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextEventDispatcher::PendingComposition::Clear()
|
|
|
|
{
|
|
|
|
mString.Truncate();
|
2015-01-28 06:27:31 +00:00
|
|
|
mClauses = nullptr;
|
2015-01-28 06:27:30 +00:00
|
|
|
mCaret.mRangeType = 0;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
void
|
|
|
|
TextEventDispatcher::PendingComposition::EnsureClauseArray()
|
|
|
|
{
|
|
|
|
if (mClauses) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mClauses = new TextRangeArray();
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:30 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::PendingComposition::SetString(const nsAString& aString)
|
|
|
|
{
|
|
|
|
mString = aString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::PendingComposition::AppendClause(uint32_t aLength,
|
|
|
|
uint32_t aAttribute)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aLength)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aAttribute) {
|
|
|
|
case NS_TEXTRANGE_RAWINPUT:
|
|
|
|
case NS_TEXTRANGE_SELECTEDRAWTEXT:
|
|
|
|
case NS_TEXTRANGE_CONVERTEDTEXT:
|
|
|
|
case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT: {
|
2015-01-28 06:27:31 +00:00
|
|
|
EnsureClauseArray();
|
2015-01-28 06:27:30 +00:00
|
|
|
TextRange textRange;
|
|
|
|
textRange.mStartOffset =
|
|
|
|
mClauses->IsEmpty() ? 0 : mClauses->LastElement().mEndOffset;
|
|
|
|
textRange.mEndOffset = textRange.mStartOffset + aLength;
|
|
|
|
textRange.mRangeType = aAttribute;
|
|
|
|
mClauses->AppendElement(textRange);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::PendingComposition::SetCaret(uint32_t aOffset,
|
|
|
|
uint32_t aLength)
|
|
|
|
{
|
|
|
|
mCaret.mStartOffset = aOffset;
|
|
|
|
mCaret.mEndOffset = mCaret.mStartOffset + aLength;
|
|
|
|
mCaret.mRangeType = NS_TEXTRANGE_CARETPOSITION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:47:48 +00:00
|
|
|
nsresult
|
|
|
|
TextEventDispatcher::PendingComposition::Set(const nsAString& aString,
|
|
|
|
const TextRangeArray* aRanges)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
nsAutoString str(aString);
|
|
|
|
// Don't expose CRLF to web contents, instead, use LF.
|
|
|
|
str.ReplaceSubstring(NS_LITERAL_STRING("\r\n"), NS_LITERAL_STRING("\n"));
|
|
|
|
nsresult rv = SetString(str);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aRanges || aRanges->IsEmpty()) {
|
|
|
|
// Create dummy range if aString isn't empty.
|
|
|
|
if (!aString.IsEmpty()) {
|
|
|
|
rv = AppendClause(str.Length(), NS_TEXTRANGE_RAWINPUT);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust offsets in the ranges for XP linefeed character (only \n).
|
|
|
|
// XXX Following code is the safest approach. However, it wastes performance.
|
|
|
|
// For ensuring the clauses do not overlap each other, we should redesign
|
|
|
|
// TextRange later.
|
|
|
|
for (uint32_t i = 0; i < aRanges->Length(); ++i) {
|
|
|
|
TextRange range = aRanges->ElementAt(i);
|
|
|
|
TextRange nativeRange = range;
|
|
|
|
if (nativeRange.mStartOffset > 0) {
|
|
|
|
nsAutoString preText(Substring(aString, 0, nativeRange.mStartOffset));
|
|
|
|
preText.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
|
|
|
|
NS_LITERAL_STRING("\n"));
|
|
|
|
range.mStartOffset = preText.Length();
|
|
|
|
}
|
|
|
|
if (nativeRange.Length() == 0) {
|
|
|
|
range.mEndOffset = range.mStartOffset;
|
|
|
|
} else {
|
|
|
|
nsAutoString clause(
|
|
|
|
Substring(aString, nativeRange.mStartOffset, nativeRange.Length()));
|
|
|
|
clause.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
|
|
|
|
NS_LITERAL_STRING("\n"));
|
|
|
|
range.mEndOffset = range.mStartOffset + clause.Length();
|
|
|
|
}
|
|
|
|
if (range.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
|
|
|
|
mCaret = range;
|
|
|
|
} else {
|
|
|
|
EnsureClauseArray();
|
|
|
|
mClauses->AppendElement(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:30 +00:00
|
|
|
nsresult
|
2016-03-16 04:47:48 +00:00
|
|
|
TextEventDispatcher::PendingComposition::Flush(
|
|
|
|
TextEventDispatcher* aDispatcher,
|
|
|
|
nsEventStatus& aStatus,
|
|
|
|
const WidgetEventTime* aEventTime)
|
2015-01-28 06:27:30 +00:00
|
|
|
{
|
|
|
|
aStatus = nsEventStatus_eIgnore;
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
nsresult rv = aDispatcher->GetState();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-01-28 06:27:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
if (mClauses && !mClauses->IsEmpty() &&
|
2015-01-28 06:27:30 +00:00
|
|
|
mClauses->LastElement().mEndOffset != mString.Length()) {
|
|
|
|
NS_WARNING("Sum of length of the all clauses must be same as the string "
|
|
|
|
"length");
|
|
|
|
Clear();
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
if (mCaret.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
|
|
|
|
if (mCaret.mEndOffset > mString.Length()) {
|
|
|
|
NS_WARNING("Caret position is out of the composition string");
|
|
|
|
Clear();
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
2015-01-28 06:27:31 +00:00
|
|
|
EnsureClauseArray();
|
2015-01-28 06:27:30 +00:00
|
|
|
mClauses->AppendElement(mCaret);
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<TextEventDispatcher> kungFuDeathGrip(aDispatcher);
|
2015-01-28 06:27:31 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget(aDispatcher->mWidget);
|
2015-09-11 12:21:27 +00:00
|
|
|
WidgetCompositionEvent compChangeEvent(true, eCompositionChange, widget);
|
2015-01-28 06:27:31 +00:00
|
|
|
aDispatcher->InitEvent(compChangeEvent);
|
2016-03-16 04:47:48 +00:00
|
|
|
if (aEventTime) {
|
|
|
|
compChangeEvent.AssignEventTime(*aEventTime);
|
|
|
|
}
|
2015-01-28 06:27:30 +00:00
|
|
|
compChangeEvent.mData = mString;
|
2015-01-28 06:27:31 +00:00
|
|
|
if (mClauses) {
|
|
|
|
MOZ_ASSERT(!mClauses->IsEmpty(),
|
|
|
|
"mClauses must be non-empty array when it's not nullptr");
|
2015-01-28 06:27:30 +00:00
|
|
|
compChangeEvent.mRanges = mClauses;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:27:31 +00:00
|
|
|
// While this method dispatches a composition event, some other event handler
|
|
|
|
// cause more clauses to be added. So, we should clear pending composition
|
|
|
|
// before dispatching the event.
|
2015-01-28 06:27:30 +00:00
|
|
|
Clear();
|
2015-01-28 06:27:31 +00:00
|
|
|
|
2016-03-16 04:47:48 +00:00
|
|
|
rv = aDispatcher->StartCompositionAutomaticallyIfNecessary(aStatus,
|
|
|
|
aEventTime);
|
2015-01-28 06:27:31 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (aStatus == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-02-19 06:50:20 +00:00
|
|
|
rv = aDispatcher->DispatchEvent(widget, compChangeEvent, aStatus);
|
2015-01-28 06:27:30 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|