2012-09-25 22:47:45 -07: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-10 22:08:02 -07:00
|
|
|
#include "ContentEventHandler.h"
|
2012-09-25 22:47:45 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-03-21 17:05:20 -07:00
|
|
|
#include "nsIContent.h"
|
2014-02-12 05:02:56 -08:00
|
|
|
#include "nsIEditor.h"
|
2012-09-25 22:47:45 -07:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
2014-03-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-03-07 17:20:07 -08:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "mozilla/MiscEvents.h"
|
2014-04-02 21:18:37 -07:00
|
|
|
#include "mozilla/TextComposition.h"
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "mozilla/TextEvents.h"
|
2012-09-25 22:47:45 -07:00
|
|
|
|
2014-02-17 16:00:15 -08:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2012-09-25 22:47:45 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::TextComposition(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode,
|
2014-02-25 16:48:02 -08:00
|
|
|
WidgetGUIEvent* aEvent)
|
|
|
|
: mPresContext(aPresContext)
|
|
|
|
, mNode(aNode)
|
|
|
|
, mNativeContext(aEvent->widget->GetInputContext().mNativeIMEContext)
|
|
|
|
, mCompositionStartOffset(0)
|
|
|
|
, mCompositionTargetOffset(0)
|
|
|
|
, mIsSynthesizedForTests(aEvent->mFlags.mIsSynthesizedForTests)
|
|
|
|
, mIsComposing(false)
|
|
|
|
, mIsEditorHandlingEvent(false)
|
2012-09-25 22:47:45 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-24 22:59:28 -08: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-25 22:47:45 -07:00
|
|
|
bool
|
|
|
|
TextComposition::MatchesNativeContext(nsIWidget* aWidget) const
|
|
|
|
{
|
2012-10-26 16:35:20 -07:00
|
|
|
return mNativeContext == aWidget->GetInputContext().mNativeIMEContext;
|
2012-09-25 22:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-01 20:46:03 -07:00
|
|
|
TextComposition::DispatchEvent(WidgetGUIEvent* aEvent,
|
2012-09-25 22:47:45 -07:00
|
|
|
nsEventStatus* aStatus,
|
2014-03-17 21:48:20 -07:00
|
|
|
EventDispatchingCallback* aCallBack)
|
2012-09-25 22:47:45 -07:00
|
|
|
{
|
2012-09-25 22:47:51 -07:00
|
|
|
if (aEvent->message == NS_COMPOSITION_UPDATE) {
|
2013-10-17 23:10:25 -07:00
|
|
|
mLastData = aEvent->AsCompositionEvent()->data;
|
2012-09-25 22:47:51 -07:00
|
|
|
}
|
|
|
|
|
2014-03-17 21:48:21 -07:00
|
|
|
EventDispatcher::Dispatch(mNode, mPresContext,
|
|
|
|
aEvent, nullptr, aStatus, aCallBack);
|
2013-11-06 16:11:11 -08:00
|
|
|
|
2014-02-24 22:59:28 -08:00
|
|
|
if (!mPresContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-12 05:02:56 -08:00
|
|
|
// Emulate editor behavior of text event handler if no editor handles
|
|
|
|
// composition/text events.
|
|
|
|
if (aEvent->message == NS_TEXT_TEXT && !HasEditor()) {
|
|
|
|
EditorWillHandleTextEvent(aEvent->AsTextEvent());
|
|
|
|
EditorDidHandleTextEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 05:02:56 -08:00
|
|
|
|
2013-11-06 16:11:11 -08: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
|
|
|
|
// string if text event.
|
|
|
|
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;
|
|
|
|
} else if (aEvent->eventStructType != NS_TEXT_EVENT) {
|
|
|
|
return;
|
|
|
|
} else {
|
2014-03-04 05:48:26 -08:00
|
|
|
mCompositionTargetOffset =
|
|
|
|
mCompositionStartOffset + aEvent->AsTextEvent()->TargetClauseOffset();
|
2013-11-06 16:11:11 -08:00
|
|
|
}
|
|
|
|
|
2014-02-17 16:00:15 -08:00
|
|
|
NotifyIME(NOTIFY_IME_OF_COMPOSITION_UPDATE);
|
2012-09-25 22:47:45 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 22:47:51 -07:00
|
|
|
void
|
2014-02-18 06:47:55 -08:00
|
|
|
TextComposition::DispatchCompositionEventRunnable(uint32_t aEventMessage,
|
|
|
|
const nsAString& aData)
|
2012-09-25 22:47:51 -07:00
|
|
|
{
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new CompositionEventDispatcher(mPresContext, mNode,
|
|
|
|
aEventMessage, aData));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextComposition::SynthesizeCommit(bool aDiscard)
|
|
|
|
{
|
2014-01-28 00:19:29 -08:00
|
|
|
nsRefPtr<TextComposition> kungFuDeathGrip(this);
|
|
|
|
nsAutoString data(aDiscard ? EmptyString() : mLastData);
|
|
|
|
if (mLastData != data) {
|
2014-02-18 06:47:55 -08:00
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_UPDATE, data);
|
|
|
|
DispatchCompositionEventRunnable(NS_TEXT_TEXT, data);
|
2012-09-25 22:47:51 -07:00
|
|
|
}
|
2014-02-18 06:47:55 -08:00
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_END, data);
|
2012-09-25 22:47:51 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 22:47:51 -07:00
|
|
|
nsresult
|
2014-02-17 16:00:15 -08:00
|
|
|
TextComposition::NotifyIME(IMEMessage aMessage)
|
2012-09-25 22:47:51 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mPresContext, NS_ERROR_NOT_AVAILABLE);
|
2014-03-07 17:20:07 -08:00
|
|
|
return IMEStateManager::NotifyIME(aMessage, mPresContext);
|
2012-09-25 22:47:51 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 05:02:56 -08:00
|
|
|
void
|
|
|
|
TextComposition::EditorWillHandleTextEvent(const WidgetTextEvent* aTextEvent)
|
|
|
|
{
|
|
|
|
mIsComposing = aTextEvent->IsComposing();
|
2014-03-04 05:48:26 -08:00
|
|
|
mRanges = aTextEvent->mRanges;
|
2014-02-25 16:48:02 -08:00
|
|
|
mIsEditorHandlingEvent = true;
|
2014-02-12 05:02:56 -08:00
|
|
|
|
|
|
|
MOZ_ASSERT(mLastData == aTextEvent->theText,
|
|
|
|
"The text of a text event must be same as previous data attribute value "
|
|
|
|
"of the latest compositionupdate event");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextComposition::EditorDidHandleTextEvent()
|
|
|
|
{
|
|
|
|
mString = mLastData;
|
2014-02-25 16:48:02 -08:00
|
|
|
mIsEditorHandlingEvent = false;
|
2014-02-12 05:02:56 -08:00
|
|
|
}
|
|
|
|
|
2014-02-12 05:02:56 -08: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-25 22:47:51 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition::CompositionEventDispatcher
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::CompositionEventDispatcher::CompositionEventDispatcher(
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsINode* aEventTarget,
|
|
|
|
uint32_t aEventMessage,
|
|
|
|
const nsAString& aData) :
|
|
|
|
mPresContext(aPresContext), mEventTarget(aEventTarget),
|
|
|
|
mEventMessage(aEventMessage), mData(aData)
|
|
|
|
{
|
2013-02-05 18:50:02 -08:00
|
|
|
mWidget = mPresContext->GetRootWidget();
|
2012-09-25 22:47:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextComposition::CompositionEventDispatcher::Run()
|
|
|
|
{
|
|
|
|
if (!mPresContext->GetPresShell() ||
|
|
|
|
mPresContext->GetPresShell()->IsDestroying()) {
|
|
|
|
return NS_OK; // cannot dispatch any events anymore
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
switch (mEventMessage) {
|
|
|
|
case NS_COMPOSITION_START: {
|
2013-10-01 00:23:00 -07:00
|
|
|
WidgetCompositionEvent compStart(true, NS_COMPOSITION_START, mWidget);
|
2013-10-01 00:23:00 -07:00
|
|
|
WidgetQueryContentEvent selectedText(true, NS_QUERY_SELECTED_TEXT,
|
|
|
|
mWidget);
|
2014-03-10 22:08:02 -07:00
|
|
|
ContentEventHandler handler(mPresContext);
|
2012-09-25 22:47:51 -07:00
|
|
|
handler.OnQuerySelectedText(&selectedText);
|
|
|
|
NS_ASSERTION(selectedText.mSucceeded, "Failed to get selected text");
|
|
|
|
compStart.data = selectedText.mReply.mString;
|
2014-03-07 17:20:07 -08:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, mPresContext,
|
|
|
|
&compStart, &status, nullptr);
|
2012-09-25 22:47:51 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NS_COMPOSITION_UPDATE:
|
|
|
|
case NS_COMPOSITION_END: {
|
2013-10-01 00:23:00 -07:00
|
|
|
WidgetCompositionEvent compEvent(true, mEventMessage, mWidget);
|
2012-09-25 22:47:51 -07:00
|
|
|
compEvent.data = mData;
|
2014-03-07 17:20:07 -08:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, mPresContext,
|
|
|
|
&compEvent, &status, nullptr);
|
2012-09-25 22:47:51 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NS_TEXT_TEXT: {
|
2013-10-01 00:22:59 -07:00
|
|
|
WidgetTextEvent textEvent(true, NS_TEXT_TEXT, mWidget);
|
2012-09-25 22:47:51 -07:00
|
|
|
textEvent.theText = mData;
|
2014-03-07 17:20:07 -08:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, mPresContext,
|
|
|
|
&textEvent, &status, nullptr);
|
2012-09-25 22:47:51 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unsupported event");
|
2012-09-25 22:47:51 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-25 22:47:45 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* TextCompositionArray
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 00:19:29 -08:00
|
|
|
if (ElementAt(i - 1)->MatchesNativeContext(aWidget)) {
|
2012-09-25 22:47:45 -07:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsPresContext* aPresContext)
|
|
|
|
{
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 00:19:29 -08:00
|
|
|
if (ElementAt(i - 1)->GetPresContext() == aPresContext) {
|
2012-09-25 22:47:45 -07: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 00:19:29 -08:00
|
|
|
nsINode* node = ElementAt(index)->GetEventTargetNode();
|
2012-09-25 22:47:45 -07:00
|
|
|
return node == aNode ? index : NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aWidget);
|
2014-01-28 00:19:29 -08:00
|
|
|
return i != NoIndex ? ElementAt(i) : nullptr;
|
2012-09-25 22:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aPresContext, aNode);
|
2014-01-28 00:19:29 -08:00
|
|
|
return i != NoIndex ? ElementAt(i) : nullptr;
|
2012-09-25 22:47:45 -07: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 00:19:29 -08:00
|
|
|
nsINode* node = ElementAt(i - 1)->GetEventTargetNode();
|
2012-09-25 22:47:45 -07:00
|
|
|
if (node && nsContentUtils::ContentIsDescendantOf(node, aContent)) {
|
2014-01-28 00:19:29 -08:00
|
|
|
return ElementAt(i - 1);
|
2012-09-25 22:47:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|