2012-09-26 09:47:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2014-03-11 09:08:02 +04:00
|
|
|
#include "ContentEventHandler.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-03-22 04:05:20 +04:00
|
|
|
#include "nsIContent.h"
|
2014-02-12 17:02:56 +04:00
|
|
|
#include "nsIEditor.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
2014-09-26 04:05:11 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/MiscEvents.h"
|
2014-04-03 08:18:37 +04:00
|
|
|
#include "mozilla/TextComposition.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
|
2014-02-18 04:00:15 +04:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-09-26 04:05:11 +04:00
|
|
|
#define IDEOGRAPHIC_SPACE (NS_LITERAL_STRING("\x3000"))
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::TextComposition(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode,
|
2014-02-26 04:48:02 +04:00
|
|
|
WidgetGUIEvent* aEvent)
|
|
|
|
: mPresContext(aPresContext)
|
|
|
|
, mNode(aNode)
|
|
|
|
, mNativeContext(aEvent->widget->GetInputContext().mNativeIMEContext)
|
|
|
|
, mCompositionStartOffset(0)
|
|
|
|
, mCompositionTargetOffset(0)
|
|
|
|
, mIsSynthesizedForTests(aEvent->mFlags.mIsSynthesizedForTests)
|
|
|
|
, mIsComposing(false)
|
|
|
|
, mIsEditorHandlingEvent(false)
|
2014-09-26 04:05:11 +04:00
|
|
|
, mIsRequestingCommit(false)
|
|
|
|
, mIsRequestingCancel(false)
|
|
|
|
, mRequestedToCommitOrCancel(false)
|
2014-09-26 04:05:12 +04:00
|
|
|
, mWasNativeCompositionEndEventDiscarded(false)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-25 10:59:28 +04:00
|
|
|
void
|
|
|
|
TextComposition::Destroy()
|
|
|
|
{
|
|
|
|
mPresContext = nullptr;
|
|
|
|
mNode = nullptr;
|
|
|
|
// TODO: If the editor is still alive and this is held by it, we should tell
|
|
|
|
// this being destroyed for cleaning up the stuff.
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
bool
|
|
|
|
TextComposition::MatchesNativeContext(nsIWidget* aWidget) const
|
|
|
|
{
|
2012-10-27 03:35:20 +04:00
|
|
|
return mNativeContext == aWidget->GetInputContext().mNativeIMEContext;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
bool
|
2014-10-07 14:01:48 +04:00
|
|
|
TextComposition::MaybeDispatchCompositionUpdate(
|
|
|
|
const WidgetCompositionEvent* aEvent)
|
2014-09-26 04:05:12 +04:00
|
|
|
{
|
|
|
|
if (Destroyed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:01:46 +04:00
|
|
|
if (mLastData == aEvent->mData) {
|
2014-09-26 04:05:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetCompositionEvent compositionUpdate(aEvent->mFlags.mIsTrusted,
|
|
|
|
NS_COMPOSITION_UPDATE,
|
|
|
|
aEvent->widget);
|
|
|
|
compositionUpdate.time = aEvent->time;
|
|
|
|
compositionUpdate.timeStamp = aEvent->timeStamp;
|
2014-10-07 14:01:46 +04:00
|
|
|
compositionUpdate.mData = aEvent->mData;
|
2014-09-26 04:05:12 +04:00
|
|
|
compositionUpdate.mFlags.mIsSynthesizedForTests =
|
|
|
|
aEvent->mFlags.mIsSynthesizedForTests;
|
2014-09-26 04:05:12 +04:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eConsumeNoDefault;
|
2014-10-07 14:01:46 +04:00
|
|
|
mLastData = compositionUpdate.mData;
|
2014-10-03 10:33:47 +04:00
|
|
|
EventDispatcher::Dispatch(mNode, mPresContext,
|
|
|
|
&compositionUpdate, nullptr, &status, nullptr);
|
2014-09-26 04:05:12 +04:00
|
|
|
return !Destroyed();
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
void
|
|
|
|
TextComposition::OnCompositionEventDiscarded(const WidgetGUIEvent* aEvent)
|
|
|
|
{
|
|
|
|
// Note that this method is never called for synthesized events for emulating
|
|
|
|
// commit or cancel composition.
|
|
|
|
|
|
|
|
MOZ_ASSERT(aEvent->mFlags.mIsTrusted,
|
|
|
|
"Shouldn't be called with untrusted event");
|
2014-10-07 14:01:48 +04:00
|
|
|
MOZ_ASSERT(aEvent->mClass == eCompositionEventClass);
|
2014-09-26 04:05:12 +04:00
|
|
|
|
|
|
|
// XXX If composition events are discarded, should we dispatch them with
|
|
|
|
// runnable event? However, even if we do so, it might make native IME
|
|
|
|
// confused due to async modification. Especially when native IME is
|
|
|
|
// TSF.
|
|
|
|
if (aEvent->message != NS_COMPOSITION_END) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWasNativeCompositionEndEventDiscarded = true;
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
void
|
2013-10-02 07:46:03 +04:00
|
|
|
TextComposition::DispatchEvent(WidgetGUIEvent* aEvent,
|
2012-09-26 09:47:45 +04:00
|
|
|
nsEventStatus* aStatus,
|
2014-09-26 04:05:11 +04:00
|
|
|
EventDispatchingCallback* aCallBack,
|
|
|
|
bool aIsSynthesized)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2014-09-26 04:05:11 +04:00
|
|
|
if (Destroyed()) {
|
|
|
|
*aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this instance has requested to commit or cancel composition but
|
|
|
|
// is not synthesizing commit event, that means that the IME commits or
|
|
|
|
// cancels the composition asynchronously. Typically, iBus behaves so.
|
|
|
|
// Then, synthesized events which were dispatched immediately after
|
|
|
|
// the request has already committed our editor's composition string and
|
|
|
|
// told it to web apps. Therefore, we should ignore the delayed events.
|
|
|
|
if (mRequestedToCommitOrCancel && !aIsSynthesized) {
|
|
|
|
*aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
// IME may commit composition with empty string for a commit request or
|
|
|
|
// with non-empty string for a cancel request. We should prevent such
|
|
|
|
// unexpected result. E.g., web apps may be confused if they implement
|
|
|
|
// autocomplete which attempts to commit composition forcibly when the user
|
|
|
|
// selects one of suggestions but composition string is cleared by IME.
|
|
|
|
// Note that most Chinese IMEs don't expose actual composition string to us.
|
|
|
|
// They typically tell us an IDEOGRAPHIC SPACE or empty string as composition
|
|
|
|
// string. Therefore, we should hack it only when:
|
|
|
|
// 1. committing string is empty string at requesting commit but the last
|
|
|
|
// data isn't IDEOGRAPHIC SPACE.
|
|
|
|
// 2. non-empty string is committed at requesting cancel.
|
|
|
|
if (!aIsSynthesized && (mIsRequestingCommit || mIsRequestingCancel)) {
|
|
|
|
nsString* committingData = nullptr;
|
|
|
|
switch (aEvent->message) {
|
|
|
|
case NS_COMPOSITION_END:
|
2014-10-07 14:01:47 +04:00
|
|
|
case NS_COMPOSITION_CHANGE:
|
2014-10-07 14:01:48 +04:00
|
|
|
committingData = &aEvent->AsCompositionEvent()->mData;
|
2014-09-26 04:05:12 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected event comes during committing or "
|
|
|
|
"canceling composition");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (committingData) {
|
|
|
|
if (mIsRequestingCommit && committingData->IsEmpty() &&
|
|
|
|
mLastData != IDEOGRAPHIC_SPACE) {
|
|
|
|
committingData->Assign(mLastData);
|
|
|
|
} else if (mIsRequestingCancel && !committingData->IsEmpty()) {
|
|
|
|
committingData->Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:01:47 +04:00
|
|
|
if (aEvent->message == NS_COMPOSITION_CHANGE) {
|
2014-10-07 14:01:48 +04:00
|
|
|
if (!MaybeDispatchCompositionUpdate(aEvent->AsCompositionEvent())) {
|
2014-10-03 10:33:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
EventDispatcher::Dispatch(mNode, mPresContext,
|
|
|
|
aEvent, nullptr, aStatus, aCallBack);
|
2013-11-07 04:11:11 +04:00
|
|
|
|
2014-09-26 04:05:11 +04:00
|
|
|
if (NS_WARN_IF(Destroyed())) {
|
2014-02-25 10:59:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:01:47 +04:00
|
|
|
// Emulate editor behavior of compositionchange event (DOM text event) handler
|
|
|
|
// if no editor handles composition events.
|
|
|
|
if (aEvent->message == NS_COMPOSITION_CHANGE && !HasEditor()) {
|
2014-10-07 14:01:49 +04:00
|
|
|
EditorWillHandleCompositionChangeEvent(aEvent->AsCompositionEvent());
|
|
|
|
EditorDidHandleCompositionChangeEvent();
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
else if (aEvent->message == NS_COMPOSITION_END) {
|
|
|
|
MOZ_ASSERT(!mIsComposing, "Why is the editor still composing?");
|
|
|
|
MOZ_ASSERT(!HasEditor(), "Why does the editor still keep to hold this?");
|
|
|
|
}
|
|
|
|
#endif // #ifdef DEBUG
|
2014-02-12 17:02:56 +04:00
|
|
|
|
2013-11-07 04:11:11 +04:00
|
|
|
// Notify composition update to widget if possible
|
|
|
|
NotityUpdateComposition(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextComposition::NotityUpdateComposition(WidgetGUIEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsEventStatus status;
|
|
|
|
|
|
|
|
// When compositon start, notify the rect of first offset character.
|
|
|
|
// When not compositon start, notify the rect of selected composition
|
2014-10-07 14:01:47 +04:00
|
|
|
// string if compositionchange event.
|
2013-11-07 04:11:11 +04:00
|
|
|
if (aEvent->message == NS_COMPOSITION_START) {
|
|
|
|
nsCOMPtr<nsIWidget> widget = mPresContext->GetRootWidget();
|
|
|
|
// Update composition start offset
|
|
|
|
WidgetQueryContentEvent selectedTextEvent(true,
|
|
|
|
NS_QUERY_SELECTED_TEXT,
|
|
|
|
widget);
|
|
|
|
widget->DispatchEvent(&selectedTextEvent, status);
|
|
|
|
if (selectedTextEvent.mSucceeded) {
|
|
|
|
mCompositionStartOffset = selectedTextEvent.mReply.mOffset;
|
|
|
|
} else {
|
|
|
|
// Unknown offset
|
|
|
|
NS_WARNING("Cannot get start offset of IME composition");
|
|
|
|
mCompositionStartOffset = 0;
|
|
|
|
}
|
|
|
|
mCompositionTargetOffset = mCompositionStartOffset;
|
2014-10-07 14:01:48 +04:00
|
|
|
} else if (aEvent->message == NS_COMPOSITION_CHANGE) {
|
2014-03-04 17:48:26 +04:00
|
|
|
mCompositionTargetOffset =
|
2014-10-07 14:01:48 +04:00
|
|
|
mCompositionStartOffset +
|
|
|
|
aEvent->AsCompositionEvent()->TargetClauseOffset();
|
|
|
|
} else {
|
|
|
|
return;
|
2013-11-07 04:11:11 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 04:00:15 +04:00
|
|
|
NotifyIME(NOTIFY_IME_OF_COMPOSITION_UPDATE);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
void
|
2014-02-18 18:47:55 +04:00
|
|
|
TextComposition::DispatchCompositionEventRunnable(uint32_t aEventMessage,
|
2014-09-26 04:05:11 +04:00
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizingCommit)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
nsContentUtils::AddScriptRunner(
|
2014-09-26 04:05:11 +04:00
|
|
|
new CompositionEventDispatcher(this, mNode, aEventMessage, aData,
|
|
|
|
aIsSynthesizingCommit));
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:11 +04:00
|
|
|
nsresult
|
|
|
|
TextComposition::RequestToCommit(nsIWidget* aWidget, bool aDiscard)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
2014-09-26 04:05:11 +04:00
|
|
|
// If this composition is already requested to be committed or canceled,
|
|
|
|
// we don't need to request it again because even if the first request
|
|
|
|
// failed, new request won't success, probably. And we shouldn't synthesize
|
|
|
|
// events for committing or canceling composition twice or more times.
|
|
|
|
if (mRequestedToCommitOrCancel) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-28 12:19:29 +04:00
|
|
|
nsRefPtr<TextComposition> kungFuDeathGrip(this);
|
2014-09-26 04:05:12 +04:00
|
|
|
const nsAutoString lastData(mLastData);
|
2014-09-26 04:05:11 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
AutoRestore<bool> saveRequestingCancel(mIsRequestingCancel);
|
|
|
|
AutoRestore<bool> saveRequestingCommit(mIsRequestingCommit);
|
|
|
|
if (aDiscard) {
|
|
|
|
mIsRequestingCancel = true;
|
|
|
|
mIsRequestingCommit = false;
|
|
|
|
} else {
|
|
|
|
mIsRequestingCancel = false;
|
|
|
|
mIsRequestingCommit = true;
|
|
|
|
}
|
2014-09-26 04:05:12 +04:00
|
|
|
if (!mIsSynthesizedForTests) {
|
2014-10-07 14:01:49 +04:00
|
|
|
// FYI: CompositionEvents caused by a call of NotifyIME() may be
|
|
|
|
// discarded by PresShell if it's not safe to dispatch the event.
|
2014-09-26 04:05:12 +04:00
|
|
|
nsresult rv =
|
|
|
|
aWidget->NotifyIME(IMENotification(aDiscard ?
|
|
|
|
REQUEST_TO_CANCEL_COMPOSITION :
|
|
|
|
REQUEST_TO_COMMIT_COMPOSITION));
|
|
|
|
if (rv == NS_ERROR_NOT_IMPLEMENTED) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Emulates to commit or cancel the composition
|
|
|
|
// FYI: These events may be discarded by PresShell if it's not safe to
|
|
|
|
// dispatch the event.
|
|
|
|
nsCOMPtr<nsIWidget> widget(aWidget);
|
|
|
|
nsAutoString commitData(aDiscard ? EmptyString() : lastData);
|
|
|
|
bool changingData = lastData != commitData;
|
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
WidgetCompositionEvent changeEvent(true, NS_COMPOSITION_CHANGE, widget);
|
|
|
|
changeEvent.mData = commitData;
|
|
|
|
changeEvent.mFlags.mIsSynthesizedForTests = true;
|
2014-09-26 04:05:12 +04:00
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
MaybeDispatchCompositionUpdate(&changeEvent);
|
2014-09-26 04:05:12 +04:00
|
|
|
|
|
|
|
// If changing the data or committing string isn't empty, we need to
|
2014-10-07 14:01:47 +04:00
|
|
|
// dispatch compositionchange event for setting the composition string
|
|
|
|
// without IME selection.
|
2014-09-26 04:05:12 +04:00
|
|
|
if (!Destroyed() && !widget->Destroyed() &&
|
|
|
|
(changingData || !commitData.IsEmpty())) {
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-10-07 14:01:49 +04:00
|
|
|
widget->DispatchEvent(&changeEvent, status);
|
2014-09-26 04:05:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Destroyed() && !widget->Destroyed()) {
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
WidgetCompositionEvent endEvent(true, NS_COMPOSITION_END, widget);
|
2014-10-07 14:01:46 +04:00
|
|
|
endEvent.mData = commitData;
|
2014-09-26 04:05:12 +04:00
|
|
|
endEvent.mFlags.mIsSynthesizedForTests = true;
|
|
|
|
widget->DispatchEvent(&endEvent, status);
|
|
|
|
}
|
2014-09-26 04:05:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequestedToCommitOrCancel = true;
|
|
|
|
|
|
|
|
// If the request is performed synchronously, this must be already destroyed.
|
|
|
|
if (Destroyed()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, synthesize the commit in content.
|
2014-09-26 04:05:12 +04:00
|
|
|
nsAutoString data(aDiscard ? EmptyString() : lastData);
|
2014-09-26 04:05:11 +04:00
|
|
|
// If the last composition string and new data are different, we need to
|
2014-10-07 14:01:47 +04:00
|
|
|
// dispatch compositionchange event for removing IME selection. However, if
|
|
|
|
// the commit string is empty string and it's not changed from the last data,
|
|
|
|
// we don't need to dispatch compositionchange event.
|
2014-10-03 10:33:47 +04:00
|
|
|
if (lastData != data || !data.IsEmpty()) {
|
2014-10-07 14:01:47 +04:00
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_CHANGE, data, true);
|
2014-09-26 04:05:11 +04:00
|
|
|
}
|
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_END, data, true);
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
nsresult
|
2014-02-18 04:00:15 +04:00
|
|
|
TextComposition::NotifyIME(IMEMessage aMessage)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mPresContext, NS_ERROR_NOT_AVAILABLE);
|
2014-03-08 05:20:07 +04:00
|
|
|
return IMEStateManager::NotifyIME(aMessage, mPresContext);
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::EditorWillHandleCompositionChangeEvent(
|
2014-10-07 14:01:49 +04:00
|
|
|
const WidgetCompositionEvent* aCompositionChangeEvent)
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
2014-10-07 14:01:49 +04:00
|
|
|
mIsComposing = aCompositionChangeEvent->IsComposing();
|
|
|
|
mRanges = aCompositionChangeEvent->mRanges;
|
2014-02-26 04:48:02 +04:00
|
|
|
mIsEditorHandlingEvent = true;
|
2014-02-12 17:02:56 +04:00
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(mLastData == aCompositionChangeEvent->mData,
|
2014-10-07 14:01:47 +04:00
|
|
|
"The text of a compositionchange event must be same as previous data "
|
|
|
|
"attribute value of the latest compositionupdate event");
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::EditorDidHandleCompositionChangeEvent()
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
|
|
|
mString = mLastData;
|
2014-02-26 04:48:02 +04:00
|
|
|
mIsEditorHandlingEvent = false;
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
void
|
|
|
|
TextComposition::StartHandlingComposition(nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!HasEditor(), "There is a handling editor already");
|
|
|
|
mEditorWeak = do_GetWeakReference(aEditor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextComposition::EndHandlingComposition(nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
MOZ_ASSERT(editor == aEditor, "Another editor handled the composition?");
|
|
|
|
#endif // #ifdef DEBUG
|
|
|
|
mEditorWeak = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIEditor>
|
|
|
|
TextComposition::GetEditor() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryReferent(mEditorWeak);
|
|
|
|
return editor.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TextComposition::HasEditor() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
return !!editor;
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition::CompositionEventDispatcher
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::CompositionEventDispatcher::CompositionEventDispatcher(
|
2014-09-26 04:05:11 +04:00
|
|
|
TextComposition* aComposition,
|
2012-09-26 09:47:51 +04:00
|
|
|
nsINode* aEventTarget,
|
|
|
|
uint32_t aEventMessage,
|
2014-09-26 04:05:11 +04:00
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizedEvent)
|
|
|
|
: mTextComposition(aComposition)
|
|
|
|
, mEventTarget(aEventTarget)
|
|
|
|
, mEventMessage(aEventMessage)
|
|
|
|
, mData(aData)
|
|
|
|
, mIsSynthesizedEvent(aIsSynthesizedEvent)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextComposition::CompositionEventDispatcher::Run()
|
|
|
|
{
|
2014-09-26 04:05:11 +04:00
|
|
|
nsRefPtr<nsPresContext> presContext = mTextComposition->mPresContext;
|
|
|
|
if (!presContext || !presContext->GetPresShell() ||
|
|
|
|
presContext->GetPresShell()->IsDestroying()) {
|
|
|
|
return NS_OK; // cannot dispatch any events anymore
|
|
|
|
}
|
|
|
|
|
|
|
|
// The widget can be different from the widget which has dispatched
|
|
|
|
// composition events because GetWidget() returns a widget which is proper
|
|
|
|
// for calling NotifyIME(). However, this must no be problem since both
|
|
|
|
// widget should share native IME context. Therefore, even if an event
|
|
|
|
// handler uses the widget for requesting IME to commit or cancel, it works.
|
|
|
|
nsCOMPtr<nsIWidget> widget(mTextComposition->GetWidget());
|
|
|
|
if (NS_WARN_IF(!widget)) {
|
2012-09-26 09:47:51 +04:00
|
|
|
return NS_OK; // cannot dispatch any events anymore
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
switch (mEventMessage) {
|
|
|
|
case NS_COMPOSITION_START: {
|
2014-09-26 04:05:11 +04:00
|
|
|
WidgetCompositionEvent compStart(true, NS_COMPOSITION_START, widget);
|
2013-10-01 11:23:00 +04:00
|
|
|
WidgetQueryContentEvent selectedText(true, NS_QUERY_SELECTED_TEXT,
|
2014-09-26 04:05:11 +04:00
|
|
|
widget);
|
|
|
|
ContentEventHandler handler(presContext);
|
2012-09-26 09:47:51 +04:00
|
|
|
handler.OnQuerySelectedText(&selectedText);
|
|
|
|
NS_ASSERTION(selectedText.mSucceeded, "Failed to get selected text");
|
2014-10-07 14:01:46 +04:00
|
|
|
compStart.mData = selectedText.mReply.mString;
|
2014-09-26 04:05:12 +04:00
|
|
|
compStart.mFlags.mIsSynthesizedForTests =
|
|
|
|
mTextComposition->IsSynthesizedForTests();
|
2014-09-26 04:05:11 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, presContext,
|
|
|
|
&compStart, &status, nullptr,
|
|
|
|
mIsSynthesizedEvent);
|
2012-09-26 09:47:51 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-10-07 14:01:49 +04:00
|
|
|
case NS_COMPOSITION_END:
|
|
|
|
case NS_COMPOSITION_CHANGE: {
|
2014-09-26 04:05:11 +04:00
|
|
|
WidgetCompositionEvent compEvent(true, mEventMessage, widget);
|
2014-10-07 14:01:46 +04:00
|
|
|
compEvent.mData = mData;
|
2014-09-26 04:05:12 +04:00
|
|
|
compEvent.mFlags.mIsSynthesizedForTests =
|
|
|
|
mTextComposition->IsSynthesizedForTests();
|
2014-09-26 04:05:11 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, presContext,
|
|
|
|
&compEvent, &status, nullptr,
|
|
|
|
mIsSynthesizedEvent);
|
2012-09-26 09:47:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unsupported event");
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextCompositionArray
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 12:19:29 +04:00
|
|
|
if (ElementAt(i - 1)->MatchesNativeContext(aWidget)) {
|
2012-09-26 09:47:45 +04:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsPresContext* aPresContext)
|
|
|
|
{
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 12:19:29 +04:00
|
|
|
if (ElementAt(i - 1)->GetPresContext() == aPresContext) {
|
2012-09-26 09:47:45 +04:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode)
|
|
|
|
{
|
|
|
|
index_type index = IndexOf(aPresContext);
|
|
|
|
if (index == NoIndex) {
|
|
|
|
return NoIndex;
|
|
|
|
}
|
2014-01-28 12:19:29 +04:00
|
|
|
nsINode* node = ElementAt(index)->GetEventTargetNode();
|
2012-09-26 09:47:45 +04:00
|
|
|
return node == aNode ? index : NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aWidget);
|
2014-01-28 12:19:29 +04:00
|
|
|
return i != NoIndex ? ElementAt(i) : nullptr;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aPresContext, aNode);
|
2014-01-28 12:19:29 +04:00
|
|
|
return i != NoIndex ? ElementAt(i) : nullptr;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionInContent(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
// There should be only one composition per content object.
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 12:19:29 +04:00
|
|
|
nsINode* node = ElementAt(i - 1)->GetEventTargetNode();
|
2012-09-26 09:47:45 +04:00
|
|
|
if (node && nsContentUtils::ContentIsDescendantOf(node, aContent)) {
|
2014-01-28 12:19:29 +04:00
|
|
|
return ElementAt(i - 1);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|