2012-09-26 05:47:45 +00: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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_TextComposition_h
|
|
|
|
#define mozilla_TextComposition_h
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsINode.h"
|
2014-02-12 13:02:56 +00:00
|
|
|
#include "nsIWeakReference.h"
|
2013-09-14 02:39:41 +00:00
|
|
|
#include "nsIWidget.h"
|
2012-09-26 05:47:45 +00:00
|
|
|
#include "nsTArray.h"
|
2012-09-26 05:47:51 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2013-11-07 00:11:11 +00:00
|
|
|
#include "nsPresContext.h"
|
2012-09-26 05:47:45 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-24 10:04:14 +00:00
|
|
|
#include "mozilla/EventForwards.h"
|
2014-03-04 13:48:26 +00:00
|
|
|
#include "mozilla/TextRange.h"
|
2012-09-26 05:47:45 +00:00
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
class nsIEditor;
|
2012-09-26 05:47:45 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-03-18 04:48:20 +00:00
|
|
|
class EventDispatchingCallback;
|
2014-03-08 01:20:07 +00:00
|
|
|
class IMEStateManager;
|
|
|
|
|
2012-09-26 05:47:45 +00:00
|
|
|
/**
|
|
|
|
* TextComposition represents a text composition. This class stores the
|
|
|
|
* composition event target and its presContext. At dispatching the event via
|
|
|
|
* this class, the instances use the stored event target.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class TextComposition MOZ_FINAL
|
|
|
|
{
|
2014-03-08 01:20:07 +00:00
|
|
|
friend class IMEStateManager;
|
2014-01-28 08:19:29 +00:00
|
|
|
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(TextComposition)
|
|
|
|
|
2012-09-26 05:47:45 +00:00
|
|
|
public:
|
|
|
|
TextComposition(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode,
|
2014-10-07 10:01:49 +00:00
|
|
|
WidgetCompositionEvent* aCompositionEvent);
|
2012-09-26 05:47:45 +00:00
|
|
|
|
2014-02-25 06:59:28 +00:00
|
|
|
bool Destroyed() const { return !mPresContext; }
|
2012-09-26 05:47:45 +00:00
|
|
|
nsPresContext* GetPresContext() const { return mPresContext; }
|
|
|
|
nsINode* GetEventTargetNode() const { return mNode; }
|
2012-09-26 05:47:51 +00:00
|
|
|
// The latest CompositionEvent.data value except compositionstart event.
|
2014-02-12 13:02:56 +00:00
|
|
|
// This value is modified at dispatching compositionupdate.
|
|
|
|
const nsString& LastData() const { return mLastData; }
|
|
|
|
// The composition string which is already handled by the focused editor.
|
|
|
|
// I.e., this value must be same as the composition string on the focused
|
2014-10-07 10:01:49 +00:00
|
|
|
// editor. This value is modified at a call of
|
|
|
|
// EditorDidHandleCompositionChangeEvent().
|
2014-02-12 13:02:56 +00:00
|
|
|
// Note that mString and mLastData are different between dispatcing
|
2014-10-07 10:01:47 +00:00
|
|
|
// compositionupdate and compositionchange event handled by focused editor.
|
2014-02-12 13:02:56 +00:00
|
|
|
const nsString& String() const { return mString; }
|
2014-03-04 13:48:26 +00:00
|
|
|
// Returns the clauses and/or caret range of the composition string.
|
2014-10-07 10:01:49 +00:00
|
|
|
// This is modified at a call of EditorWillHandleCompositionChangeEvent().
|
2014-03-04 13:48:26 +00:00
|
|
|
// This may return null if there is no clauses and caret.
|
|
|
|
// XXX We should return |const TextRangeArray*| here, but it causes compile
|
|
|
|
// error due to inaccessible Release() method.
|
|
|
|
TextRangeArray* GetRanges() const { return mRanges; }
|
2014-09-26 00:05:11 +00:00
|
|
|
// Returns the widget which is proper to call NotifyIME().
|
|
|
|
nsIWidget* GetWidget() const
|
|
|
|
{
|
|
|
|
return mPresContext ? mPresContext->GetRootWidget() : nullptr;
|
|
|
|
}
|
2012-09-26 05:47:51 +00:00
|
|
|
// Returns true if the composition is started with synthesized event which
|
|
|
|
// came from nsDOMWindowUtils.
|
|
|
|
bool IsSynthesizedForTests() const { return mIsSynthesizedForTests; }
|
2012-09-26 05:47:45 +00:00
|
|
|
|
|
|
|
bool MatchesNativeContext(nsIWidget* aWidget) const;
|
|
|
|
|
2014-02-25 06:59:28 +00:00
|
|
|
/**
|
2014-03-08 01:20:07 +00:00
|
|
|
* This is called when IMEStateManager stops managing the instance.
|
2014-02-25 06:59:28 +00:00
|
|
|
*/
|
|
|
|
void Destroy();
|
|
|
|
|
2012-09-26 05:47:51 +00:00
|
|
|
/**
|
2014-09-26 00:05:11 +00:00
|
|
|
* Request to commit (or cancel) the composition to IME. This method should
|
|
|
|
* be called only by IMEStateManager::NotifyIME().
|
2012-09-26 05:47:51 +00:00
|
|
|
*/
|
2014-09-26 00:05:11 +00:00
|
|
|
nsresult RequestToCommit(nsIWidget* aWidget, bool aDiscard);
|
2012-09-26 05:47:51 +00:00
|
|
|
|
2012-09-26 05:47:51 +00:00
|
|
|
/**
|
|
|
|
* Send a notification to IME. It depends on the IME or platform spec what
|
|
|
|
* will occur (or not occur).
|
|
|
|
*/
|
2014-02-18 00:00:15 +00:00
|
|
|
nsresult NotifyIME(widget::IMEMessage aMessage);
|
2012-09-26 05:47:51 +00:00
|
|
|
|
2014-09-15 07:11:08 +00:00
|
|
|
/**
|
|
|
|
* the offset of first composition string
|
|
|
|
*/
|
|
|
|
uint32_t NativeOffsetOfStartComposition() const
|
|
|
|
{
|
|
|
|
return mCompositionStartOffset;
|
|
|
|
}
|
|
|
|
|
2013-11-07 00:11:11 +00:00
|
|
|
/**
|
|
|
|
* the offset of first selected clause or start of of compositon
|
|
|
|
*/
|
|
|
|
uint32_t OffsetOfTargetClause() const { return mCompositionTargetOffset; }
|
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
/**
|
|
|
|
* Returns true if there is non-empty composition string and it's not fixed.
|
|
|
|
* Otherwise, false.
|
|
|
|
*/
|
|
|
|
bool IsComposing() const { return mIsComposing; }
|
|
|
|
|
2014-02-26 00:48:02 +00:00
|
|
|
/**
|
|
|
|
* Returns true while editor is handling an event which is modifying the
|
|
|
|
* composition string.
|
|
|
|
*/
|
|
|
|
bool IsEditorHandlingEvent() const
|
|
|
|
{
|
|
|
|
return mIsEditorHandlingEvent;
|
|
|
|
}
|
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
/**
|
|
|
|
* StartHandlingComposition() and EndHandlingComposition() are called by
|
|
|
|
* editor when it holds a TextComposition instance and release it.
|
|
|
|
*/
|
|
|
|
void StartHandlingComposition(nsIEditor* aEditor);
|
|
|
|
void EndHandlingComposition(nsIEditor* aEditor);
|
|
|
|
|
2014-02-12 13:02:57 +00:00
|
|
|
/**
|
2014-10-07 10:01:49 +00:00
|
|
|
* CompositionChangeEventHandlingMarker class should be created at starting
|
|
|
|
* to handle text event in focused editor. This calls
|
|
|
|
* EditorWillHandleCompositionChangeEvent() and
|
|
|
|
* EditorDidHandleCompositionChangeEvent() automatically.
|
2014-02-12 13:02:57 +00:00
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
class MOZ_STACK_CLASS CompositionChangeEventHandlingMarker
|
2014-02-12 13:02:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-07 10:01:49 +00:00
|
|
|
CompositionChangeEventHandlingMarker(
|
2014-10-07 10:01:49 +00:00
|
|
|
TextComposition* aComposition,
|
|
|
|
const WidgetCompositionEvent* aCompositionChangeEvent)
|
2014-02-12 13:02:57 +00:00
|
|
|
: mComposition(aComposition)
|
|
|
|
{
|
2014-10-07 10:01:49 +00:00
|
|
|
mComposition->EditorWillHandleCompositionChangeEvent(
|
|
|
|
aCompositionChangeEvent);
|
2014-02-12 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 10:01:49 +00:00
|
|
|
~CompositionChangeEventHandlingMarker()
|
2014-02-12 13:02:57 +00:00
|
|
|
{
|
2014-10-07 10:01:49 +00:00
|
|
|
mComposition->EditorDidHandleCompositionChangeEvent();
|
2014-02-12 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<TextComposition> mComposition;
|
2014-10-07 10:01:49 +00:00
|
|
|
CompositionChangeEventHandlingMarker();
|
|
|
|
CompositionChangeEventHandlingMarker(
|
|
|
|
const CompositionChangeEventHandlingMarker& aOther);
|
2014-02-12 13:02:57 +00:00
|
|
|
};
|
|
|
|
|
2012-09-26 05:47:45 +00:00
|
|
|
private:
|
2014-04-02 16:21:03 +00:00
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~TextComposition()
|
|
|
|
{
|
|
|
|
// WARNING: mPresContext may be destroying, so, be careful if you touch it.
|
|
|
|
}
|
|
|
|
|
2012-09-26 05:47:45 +00:00
|
|
|
// This class holds nsPresContext weak. This instance shouldn't block
|
|
|
|
// destroying it. When the presContext is being destroyed, it's notified to
|
2014-03-08 01:20:07 +00:00
|
|
|
// IMEStateManager::OnDestroyPresContext(), and then, it destroy
|
2012-09-26 05:47:45 +00:00
|
|
|
// this instance.
|
|
|
|
nsPresContext* mPresContext;
|
|
|
|
nsCOMPtr<nsINode> mNode;
|
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
// This is the clause and caret range information which is managed by
|
|
|
|
// the focused editor. This may be null if there is no clauses or caret.
|
|
|
|
nsRefPtr<TextRangeArray> mRanges;
|
|
|
|
|
2012-09-26 05:47:45 +00:00
|
|
|
// mNativeContext stores a opaque pointer. This works as the "ID" for this
|
|
|
|
// composition. Don't access the instance, it may not be available.
|
|
|
|
void* mNativeContext;
|
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
// mEditorWeak is a weak reference to the focused editor handling composition.
|
|
|
|
nsWeakPtr mEditorWeak;
|
|
|
|
|
2012-09-26 05:47:51 +00:00
|
|
|
// mLastData stores the data attribute of the latest composition event (except
|
|
|
|
// the compositionstart event).
|
|
|
|
nsString mLastData;
|
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
// mString stores the composition text which has been handled by the focused
|
|
|
|
// editor.
|
|
|
|
nsString mString;
|
|
|
|
|
2013-11-07 00:11:11 +00:00
|
|
|
// Offset of the composition string from start of the editor
|
|
|
|
uint32_t mCompositionStartOffset;
|
|
|
|
// Offset of the selected clause of the composition string from start of the
|
|
|
|
// editor
|
|
|
|
uint32_t mCompositionTargetOffset;
|
|
|
|
|
2012-09-26 05:47:51 +00:00
|
|
|
// See the comment for IsSynthesizedForTests().
|
|
|
|
bool mIsSynthesizedForTests;
|
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
// See the comment for IsComposing().
|
|
|
|
bool mIsComposing;
|
|
|
|
|
2014-02-26 00:48:02 +00:00
|
|
|
// mIsEditorHandlingEvent is true while editor is modifying the composition
|
|
|
|
// string.
|
|
|
|
bool mIsEditorHandlingEvent;
|
|
|
|
|
2014-09-26 00:05:11 +00:00
|
|
|
// mIsRequestingCommit or mIsRequestingCancel is true *only* while we're
|
|
|
|
// requesting commit or canceling the composition. In other words, while
|
|
|
|
// one of these values is true, we're handling the request.
|
|
|
|
bool mIsRequestingCommit;
|
|
|
|
bool mIsRequestingCancel;
|
|
|
|
|
|
|
|
// mRequestedToCommitOrCancel is true *after* we requested IME to commit or
|
|
|
|
// cancel the composition. In other words, we already requested of IME that
|
|
|
|
// it commits or cancels current composition.
|
|
|
|
// NOTE: Before this is set true, both mIsRequestingCommit and
|
|
|
|
// mIsRequestingCancel are set false.
|
|
|
|
bool mRequestedToCommitOrCancel;
|
|
|
|
|
2014-09-26 00:05:12 +00:00
|
|
|
// mWasNativeCompositionEndEventDiscarded is true if this composition was
|
|
|
|
// requested commit or cancel itself but native compositionend event is
|
|
|
|
// discarded by PresShell due to not safe to dispatch events.
|
|
|
|
bool mWasNativeCompositionEndEventDiscarded;
|
|
|
|
|
2014-01-28 08:19:29 +00:00
|
|
|
// Hide the default constructor and copy constructor.
|
2012-09-26 05:47:45 +00:00
|
|
|
TextComposition() {}
|
2014-01-28 08:19:29 +00:00
|
|
|
TextComposition(const TextComposition& aOther);
|
|
|
|
|
2014-02-12 13:02:56 +00:00
|
|
|
/**
|
|
|
|
* GetEditor() returns nsIEditor pointer of mEditorWeak.
|
|
|
|
*/
|
|
|
|
already_AddRefed<nsIEditor> GetEditor() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HasEditor() returns true if mEditorWeak holds nsIEditor instance which is
|
|
|
|
* alive. Otherwise, false.
|
|
|
|
*/
|
|
|
|
bool HasEditor() const;
|
2012-09-26 05:47:45 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-07 10:01:49 +00:00
|
|
|
* EditorWillHandleCompositionChangeEvent() must be called before the focused
|
|
|
|
* editor handles the compositionchange event.
|
2014-02-12 13:02:57 +00:00
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
void EditorWillHandleCompositionChangeEvent(
|
2014-10-07 10:01:49 +00:00
|
|
|
const WidgetCompositionEvent* aCompositionChangeEvent);
|
2014-02-12 13:02:57 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-07 10:01:49 +00:00
|
|
|
* EditorDidHandleCompositionChangeEvent() must be called after the focused
|
|
|
|
* editor handles a compositionchange event.
|
2014-02-12 13:02:57 +00:00
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
void EditorDidHandleCompositionChangeEvent();
|
2014-02-12 13:02:57 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-07 10:01:49 +00:00
|
|
|
* DispatchCompositionEvent() dispatches the aCompositionEvent to the mContent
|
|
|
|
* synchronously. The caller must ensure that it's safe to dispatch the event.
|
2012-09-26 05:47:45 +00:00
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
void DispatchCompositionEvent(WidgetCompositionEvent* aCompositionEvent,
|
|
|
|
nsEventStatus* aStatus,
|
|
|
|
EventDispatchingCallback* aCallBack,
|
|
|
|
bool aIsSynthesized);
|
2012-09-26 05:47:51 +00:00
|
|
|
|
2014-09-26 00:05:12 +00:00
|
|
|
/**
|
|
|
|
* MaybeDispatchCompositionUpdate() may dispatch a compositionupdate event
|
2014-10-07 10:01:49 +00:00
|
|
|
* if aCompositionEvent changes composition string.
|
2014-09-26 00:05:12 +00:00
|
|
|
* @return Returns false if dispatching the compositionupdate event caused
|
|
|
|
* destroying this composition.
|
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
bool MaybeDispatchCompositionUpdate(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent);
|
2014-09-26 00:05:12 +00:00
|
|
|
|
2014-09-26 00:05:12 +00:00
|
|
|
/**
|
|
|
|
* If IME has already dispatched compositionend event but it was discarded
|
|
|
|
* by PresShell due to not safe to dispatch, this returns true.
|
|
|
|
*/
|
|
|
|
bool WasNativeCompositionEndEventDiscarded() const
|
|
|
|
{
|
|
|
|
return mWasNativeCompositionEndEventDiscarded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OnCompositionEventDiscarded() is called when PresShell discards
|
2014-10-07 10:01:47 +00:00
|
|
|
* compositionupdate, compositionend or compositionchange event due to not
|
|
|
|
* safe to dispatch event.
|
2014-09-26 00:05:12 +00:00
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
void OnCompositionEventDiscarded(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent);
|
2014-09-26 00:05:12 +00:00
|
|
|
|
2013-11-07 00:11:11 +00:00
|
|
|
/**
|
|
|
|
* Calculate composition offset then notify composition update to widget
|
|
|
|
*/
|
2014-10-07 10:01:49 +00:00
|
|
|
void NotityUpdateComposition(const WidgetCompositionEvent* aCompositionEvent);
|
2013-11-07 00:11:11 +00:00
|
|
|
|
2012-09-26 05:47:51 +00:00
|
|
|
/**
|
|
|
|
* CompositionEventDispatcher dispatches the specified composition (or text)
|
|
|
|
* event.
|
|
|
|
*/
|
|
|
|
class CompositionEventDispatcher : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-09-26 00:05:11 +00:00
|
|
|
CompositionEventDispatcher(TextComposition* aTextComposition,
|
2012-09-26 05:47:51 +00:00
|
|
|
nsINode* aEventTarget,
|
|
|
|
uint32_t aEventMessage,
|
2014-09-26 00:05:11 +00:00
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizedEvent = false);
|
2013-05-29 20:43:41 +00:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE;
|
2012-09-26 05:47:51 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-26 00:05:11 +00:00
|
|
|
nsRefPtr<TextComposition> mTextComposition;
|
2012-09-26 05:47:51 +00:00
|
|
|
nsCOMPtr<nsINode> mEventTarget;
|
|
|
|
uint32_t mEventMessage;
|
|
|
|
nsString mData;
|
2014-09-26 00:05:11 +00:00
|
|
|
bool mIsSynthesizedEvent;
|
2012-09-26 05:47:51 +00:00
|
|
|
|
|
|
|
CompositionEventDispatcher() {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-07 10:01:47 +00:00
|
|
|
* DispatchCompositionEventRunnable() dispatches a composition event to the
|
|
|
|
* content. Be aware, if you use this method, nsPresShellEventCB isn't used.
|
|
|
|
* That means that nsIFrame::HandleEvent() is never called.
|
2014-03-08 01:20:07 +00:00
|
|
|
* WARNING: The instance which is managed by IMEStateManager may be
|
2012-09-26 05:47:51 +00:00
|
|
|
* destroyed by this method call.
|
|
|
|
*
|
2014-10-07 10:01:47 +00:00
|
|
|
* @param aEventMessage Must be one of composition events.
|
|
|
|
* @param aData Used for mData value.
|
2014-09-26 00:05:11 +00:00
|
|
|
* @param aIsSynthesizingCommit true if this is called for synthesizing
|
|
|
|
* commit or cancel composition. Otherwise,
|
|
|
|
* false.
|
2012-09-26 05:47:51 +00:00
|
|
|
*/
|
2014-02-18 14:47:55 +00:00
|
|
|
void DispatchCompositionEventRunnable(uint32_t aEventMessage,
|
2014-09-26 00:05:11 +00:00
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizingCommit = false);
|
2012-09-26 05:47:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TextCompositionArray manages the instances of TextComposition class.
|
|
|
|
* Managing with array is enough because only one composition is typically
|
|
|
|
* there. Even if user switches native IME context, it's very rare that
|
|
|
|
* second or more composition is started.
|
2014-03-08 01:20:07 +00:00
|
|
|
* It's assumed that this is used by IMEStateManager for storing all active
|
2012-09-26 05:47:45 +00:00
|
|
|
* compositions in the process. If the instance is it, each TextComposition
|
|
|
|
* in the array can be destroyed by calling some methods of itself.
|
|
|
|
*/
|
|
|
|
|
2014-01-28 08:19:29 +00:00
|
|
|
class TextCompositionArray MOZ_FINAL :
|
|
|
|
public nsAutoTArray<nsRefPtr<TextComposition>, 2>
|
2012-09-26 05:47:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
index_type IndexOf(nsIWidget* aWidget);
|
|
|
|
index_type IndexOf(nsPresContext* aPresContext);
|
|
|
|
index_type IndexOf(nsPresContext* aPresContext, nsINode* aNode);
|
|
|
|
|
|
|
|
TextComposition* GetCompositionFor(nsIWidget* aWidget);
|
|
|
|
TextComposition* GetCompositionFor(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode);
|
|
|
|
TextComposition* GetCompositionInContent(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // #ifndef mozilla_TextComposition_h
|