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 "TextComposition.h"
|
|
|
|
#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-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"
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* 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)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-02 07:46:03 +04:00
|
|
|
TextComposition::DispatchEvent(WidgetGUIEvent* aEvent,
|
2012-09-26 09:47:45 +04:00
|
|
|
nsEventStatus* aStatus,
|
2014-03-18 08:48:20 +04:00
|
|
|
EventDispatchingCallback* aCallBack)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2012-09-26 09:47:51 +04:00
|
|
|
if (aEvent->message == NS_COMPOSITION_UPDATE) {
|
2013-10-18 10:10:25 +04:00
|
|
|
mLastData = aEvent->AsCompositionEvent()->data;
|
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-02-25 10:59:28 +04:00
|
|
|
if (!mPresContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:02:56 +04: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 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
|
|
|
|
// 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 17:48:26 +04:00
|
|
|
mCompositionTargetOffset =
|
|
|
|
mCompositionStartOffset + aEvent->AsTextEvent()->TargetClauseOffset();
|
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,
|
|
|
|
const nsAString& aData)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new CompositionEventDispatcher(mPresContext, mNode,
|
|
|
|
aEventMessage, aData));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextComposition::SynthesizeCommit(bool aDiscard)
|
|
|
|
{
|
2014-01-28 12:19:29 +04:00
|
|
|
nsRefPtr<TextComposition> kungFuDeathGrip(this);
|
|
|
|
nsAutoString data(aDiscard ? EmptyString() : mLastData);
|
|
|
|
if (mLastData != data) {
|
2014-02-18 18:47:55 +04:00
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_UPDATE, data);
|
|
|
|
DispatchCompositionEventRunnable(NS_TEXT_TEXT, data);
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
2014-02-18 18:47:55 +04:00
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_END, data);
|
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
|
|
|
|
TextComposition::EditorWillHandleTextEvent(const WidgetTextEvent* aTextEvent)
|
|
|
|
{
|
|
|
|
mIsComposing = aTextEvent->IsComposing();
|
2014-03-04 17:48:26 +04:00
|
|
|
mRanges = aTextEvent->mRanges;
|
2014-02-26 04:48:02 +04:00
|
|
|
mIsEditorHandlingEvent = true;
|
2014-02-12 17:02:56 +04: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-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(
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsINode* aEventTarget,
|
|
|
|
uint32_t aEventMessage,
|
|
|
|
const nsAString& aData) :
|
|
|
|
mPresContext(aPresContext), mEventTarget(aEventTarget),
|
|
|
|
mEventMessage(aEventMessage), mData(aData)
|
|
|
|
{
|
2013-02-06 06:50:02 +04:00
|
|
|
mWidget = mPresContext->GetRootWidget();
|
2012-09-26 09:47:51 +04: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 11:23:00 +04:00
|
|
|
WidgetCompositionEvent compStart(true, NS_COMPOSITION_START, mWidget);
|
2013-10-01 11:23:00 +04:00
|
|
|
WidgetQueryContentEvent selectedText(true, NS_QUERY_SELECTED_TEXT,
|
|
|
|
mWidget);
|
2014-03-11 09:08:02 +04:00
|
|
|
ContentEventHandler handler(mPresContext);
|
2012-09-26 09:47:51 +04:00
|
|
|
handler.OnQuerySelectedText(&selectedText);
|
|
|
|
NS_ASSERTION(selectedText.mSucceeded, "Failed to get selected text");
|
|
|
|
compStart.data = selectedText.mReply.mString;
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, mPresContext,
|
|
|
|
&compStart, &status, nullptr);
|
2012-09-26 09:47:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NS_COMPOSITION_UPDATE:
|
|
|
|
case NS_COMPOSITION_END: {
|
2013-10-01 11:23:00 +04:00
|
|
|
WidgetCompositionEvent compEvent(true, mEventMessage, mWidget);
|
2012-09-26 09:47:51 +04:00
|
|
|
compEvent.data = mData;
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, mPresContext,
|
|
|
|
&compEvent, &status, nullptr);
|
2012-09-26 09:47:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NS_TEXT_TEXT: {
|
2013-10-01 11:22:59 +04:00
|
|
|
WidgetTextEvent textEvent(true, NS_TEXT_TEXT, mWidget);
|
2012-09-26 09:47:51 +04:00
|
|
|
textEvent.theText = mData;
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, mPresContext,
|
|
|
|
&textEvent, &status, nullptr);
|
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
|