gecko-dev/editor/libeditor/base/IMETextTxn.cpp

302 lines
10 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 11:12:37 +00:00
/* 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/. */
#include "IMETextTxn.h"
#include "mozilla/DebugOnly.h" // for DebugOnly
#include "mozilla/mozalloc.h" // for operator new
#include "mozilla/TextEvents.h" // for TextRangeStyle
#include "nsAString.h" // for nsAString_internal::Length, etc
#include "nsAutoPtr.h" // for nsRefPtr
#include "nsDebug.h" // for NS_ASSERTION, etc
#include "nsError.h" // for NS_SUCCEEDED, NS_FAILED, etc
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
#include "nsIDOMRange.h" // for nsRange::SetEnd, etc
#include "nsIContent.h" // for nsIContent
#include "nsIEditor.h" // for nsIEditor
#include "nsIPresShell.h" // for SelectionType
#include "nsISelection.h" // for nsISelection
#include "nsISelectionController.h" // for nsISelectionController, etc
#include "nsISelectionPrivate.h" // for nsISelectionPrivate
#include "nsISupportsImpl.h" // for nsRange::AddRef, etc
#include "nsISupportsUtils.h" // for NS_ADDREF_THIS, NS_RELEASE
#include "nsITransaction.h" // for nsITransaction
#include "nsRange.h" // for nsRange
#include "nsString.h" // for nsString
1999-12-01 00:35:31 +00:00
using namespace mozilla;
1999-12-01 00:35:31 +00:00
// #define DEBUG_IMETXN
IMETextTxn::IMETextTxn()
: EditTxn()
{
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(IMETextTxn, EditTxn,
mElement)
// mRangeList can't lead to cycles
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IMETextTxn)
if (aIID.Equals(IMETextTxn::GetCID())) {
*aInstancePtr = (void*)(IMETextTxn*)this;
NS_ADDREF_THIS();
return NS_OK;
} else
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement,
uint32_t aOffset,
uint32_t aReplaceLength,
TextRangeArray *aTextRangeArray,
const nsAString &aStringToInsert,
nsIEditor *aEditor)
{
NS_ENSURE_ARG_POINTER(aElement);
mElement = aElement;
mOffset = aOffset;
mReplaceLength = aReplaceLength;
mStringToInsert = aStringToInsert;
mEditor = aEditor;
mRanges = aTextRangeArray;
mFixed = false;
return NS_OK;
}
Fixes for bug #66308 ([embed] XPIDL'ize transaction manager) sr=sfraser@netscape.com,mscott@netscape.com r=jfrancis@netscape.com * nsITransaction, nsITransactionListenter, nsITransactionManager have been XPIDL'ized and moved into mozilla/editor/txmgr/idl. The versions of these interfaces in mozilla/editor/txmgr/public are being CVS removed. * Renamed Do(), Undo(), and Redo() to DoTransaction(), UndoTransaction(), and RedoTransaction() to avoid reserved word problems in languages like JS. I did a sweep through editor and mailnews to remove these methods. * PeekUndoStack() and PeekRedoStack() now return an AddRef'd pointer. * Removed GetUndoString(), GetRedoString() and Write() from the nsITransaction interface. Neither editor or mailnews really made use of these methods. * Removed Write() from the nsITransactionManager.cpp interface. * The Transaction Manager now supports weak references. * Added support for nsITransactionList to the TransactionManager to allow access to all transactions on the Undo and Redo stacks, as well as auto-aggregated transactions. * Removed all references to nsITransactionDescription from txmgr and editor. * Added nsPIEditorTransaction and made all Editor internal transactions inherit from it so we can distinguish between our transactions and ones from 3rd parties. New files checked in: editor/txmgr/idl/nsITransaction.idl editor/txmgr/idl/nsITransactionList.idl editor/txmgr/idl/nsITransactionListener.idl editor/txmgr/idl/nsITransactionManager.idl editor/txmgr/src/nsITransactionList.cpp editor/txmgr/src/nsITransactionList.h editor/idl/nsPIEditorTransaction.idl Files that were CVS removed: editor/txmgr/public/nsITransaction.h editor/txmgr/public/nsITransactionListener.h editor/txmgr/public/nsITransactionManager.h editor/txmgr/idl/nsITransactionDescription.h editor/base/IMECommitTxn.cpp editor/base/IMECommitTxn.h Files modified: editor/Makefile.in editor/makefile.win editor/base/ChangeAttributeTxn.cpp editor/base/ChangeAttributeTxn.h editor/base/CreateElementTxn.cpp editor/base/CreateElementTxn.h editor/base/DeleteElementTxn.cpp editor/base/DeleteElementTxn.h editor/base/DeleteRangeTxn.cpp editor/base/DeleteRangeTxn.h editor/base/DeleteTextTxn.cpp editor/base/DeleteTextTxn.h editor/base/EditAggregateTxn.cpp editor/base/EditAggregateTxn.h editor/base/EditTxn.cpp editor/base/EditTxn.h editor/base/IMECommitTxn.cpp editor/base/IMECommitTxn.h editor/base/IMETextTxn.cpp editor/base/IMETextTxn.h editor/base/InsertElementTxn.cpp editor/base/InsertElementTxn.h editor/base/InsertTextTxn.cpp editor/base/InsertTextTxn.h editor/base/JoinElementTxn.cpp editor/base/JoinElementTxn.h editor/base/nsEditor.cpp editor/base/nsEditorShell.cpp editor/base/nsEditorShell.h editor/base/nsEditorTxnLog.cpp editor/base/nsStyleSheetTxns.cpp editor/base/nsStyleSheetTxns.h editor/base/PlaceholderTxn.cpp editor/base/PlaceholderTxn.h editor/base/SetDocTitleTxn.cpp editor/base/SetDocTitleTxn.h editor/base/SplitElementTxn.cpp editor/base/SplitElementTxn.h editor/idl/Makefile.in editor/idl/makefile.win editor/idl/MANIFEST editor/idl/nsIEditorShell.idl editor/macbuild/editor.mcp editor/macbuild/EditorIDL.mcp editor/txmgr/idl/Makefile.in editor/txmgr/idl/makefile.win editor/txmgr/idl/MANIFEST editor/txmgr/idl/nsITransactionManager.idl editor/txmgr/macbuild/txmgr.mcp editor/txmgr/macbuild/txmgrIDL.mcp editor/txmgr/public/Makefile.in editor/txmgr/public/makefile.win editor/txmgr/public/MANIFEST editor/txmgr/src/Makefile.in editor/txmgr/src/makefile.win editor/txmgr/src/nsTransactionItem.cpp editor/txmgr/src/nsTransactionItem.h editor/txmgr/src/nsTransactionList.cpp editor/txmgr/src/nsTransactionList.h editor/txmgr/src/nsTransactionManager.cpp editor/txmgr/src/nsTransactionManager.h editor/txmgr/src/nsTransactionManagerFactory.cpp editor/txmgr/src/nsTransactionStack.cpp editor/txmgr/src/nsTransactionStack.h editor/txmgr/tests/TestTXMgr.cpp editor/ui/composer/content/EditorCommandsDebug.js editor/ui/composer/content/editorOverlay.xul editor/ui/composer/locale/en-US/editorOverlay.dtd mailnews/base/src/nsMessenger.cpp mailnews/base/util/nsMsgTxn.cpp mailnews/base/util/nsMsgTxn.h mailnews/imap/src/nsImapMailFolder.cpp mailnews/imap/src/nsImapUndoTxn.cpp mailnews/imap/src/nsImapUndoTxn.h mailnews/local/src/nsLocalMailFolder.cpp mailnews/local/src/nsLocalUndoTxn.cpp mailnews/local/src/nsLocalUndoTxn.h
2001-03-09 14:23:59 +00:00
NS_IMETHODIMP IMETextTxn::DoTransaction(void)
{
#ifdef DEBUG_IMETXN
printf("Do IME Text element = %p replace = %d len = %d\n", mElement.get(), mReplaceLength, mStringToInsert.Length());
#endif
nsCOMPtr<nsISelectionController> selCon;
mEditor->GetSelectionController(getter_AddRefs(selCon));
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
// advance caret: This requires the presentation shell to get the selection.
nsresult result;
if (mReplaceLength == 0) {
result = mElement->InsertData(mOffset, mStringToInsert);
} else {
result = mElement->ReplaceData(mOffset, mReplaceLength, mStringToInsert);
}
if (NS_SUCCEEDED(result)) {
result = SetSelectionForRanges();
}
return result;
}
Fixes for bug #66308 ([embed] XPIDL'ize transaction manager) sr=sfraser@netscape.com,mscott@netscape.com r=jfrancis@netscape.com * nsITransaction, nsITransactionListenter, nsITransactionManager have been XPIDL'ized and moved into mozilla/editor/txmgr/idl. The versions of these interfaces in mozilla/editor/txmgr/public are being CVS removed. * Renamed Do(), Undo(), and Redo() to DoTransaction(), UndoTransaction(), and RedoTransaction() to avoid reserved word problems in languages like JS. I did a sweep through editor and mailnews to remove these methods. * PeekUndoStack() and PeekRedoStack() now return an AddRef'd pointer. * Removed GetUndoString(), GetRedoString() and Write() from the nsITransaction interface. Neither editor or mailnews really made use of these methods. * Removed Write() from the nsITransactionManager.cpp interface. * The Transaction Manager now supports weak references. * Added support for nsITransactionList to the TransactionManager to allow access to all transactions on the Undo and Redo stacks, as well as auto-aggregated transactions. * Removed all references to nsITransactionDescription from txmgr and editor. * Added nsPIEditorTransaction and made all Editor internal transactions inherit from it so we can distinguish between our transactions and ones from 3rd parties. New files checked in: editor/txmgr/idl/nsITransaction.idl editor/txmgr/idl/nsITransactionList.idl editor/txmgr/idl/nsITransactionListener.idl editor/txmgr/idl/nsITransactionManager.idl editor/txmgr/src/nsITransactionList.cpp editor/txmgr/src/nsITransactionList.h editor/idl/nsPIEditorTransaction.idl Files that were CVS removed: editor/txmgr/public/nsITransaction.h editor/txmgr/public/nsITransactionListener.h editor/txmgr/public/nsITransactionManager.h editor/txmgr/idl/nsITransactionDescription.h editor/base/IMECommitTxn.cpp editor/base/IMECommitTxn.h Files modified: editor/Makefile.in editor/makefile.win editor/base/ChangeAttributeTxn.cpp editor/base/ChangeAttributeTxn.h editor/base/CreateElementTxn.cpp editor/base/CreateElementTxn.h editor/base/DeleteElementTxn.cpp editor/base/DeleteElementTxn.h editor/base/DeleteRangeTxn.cpp editor/base/DeleteRangeTxn.h editor/base/DeleteTextTxn.cpp editor/base/DeleteTextTxn.h editor/base/EditAggregateTxn.cpp editor/base/EditAggregateTxn.h editor/base/EditTxn.cpp editor/base/EditTxn.h editor/base/IMECommitTxn.cpp editor/base/IMECommitTxn.h editor/base/IMETextTxn.cpp editor/base/IMETextTxn.h editor/base/InsertElementTxn.cpp editor/base/InsertElementTxn.h editor/base/InsertTextTxn.cpp editor/base/InsertTextTxn.h editor/base/JoinElementTxn.cpp editor/base/JoinElementTxn.h editor/base/nsEditor.cpp editor/base/nsEditorShell.cpp editor/base/nsEditorShell.h editor/base/nsEditorTxnLog.cpp editor/base/nsStyleSheetTxns.cpp editor/base/nsStyleSheetTxns.h editor/base/PlaceholderTxn.cpp editor/base/PlaceholderTxn.h editor/base/SetDocTitleTxn.cpp editor/base/SetDocTitleTxn.h editor/base/SplitElementTxn.cpp editor/base/SplitElementTxn.h editor/idl/Makefile.in editor/idl/makefile.win editor/idl/MANIFEST editor/idl/nsIEditorShell.idl editor/macbuild/editor.mcp editor/macbuild/EditorIDL.mcp editor/txmgr/idl/Makefile.in editor/txmgr/idl/makefile.win editor/txmgr/idl/MANIFEST editor/txmgr/idl/nsITransactionManager.idl editor/txmgr/macbuild/txmgr.mcp editor/txmgr/macbuild/txmgrIDL.mcp editor/txmgr/public/Makefile.in editor/txmgr/public/makefile.win editor/txmgr/public/MANIFEST editor/txmgr/src/Makefile.in editor/txmgr/src/makefile.win editor/txmgr/src/nsTransactionItem.cpp editor/txmgr/src/nsTransactionItem.h editor/txmgr/src/nsTransactionList.cpp editor/txmgr/src/nsTransactionList.h editor/txmgr/src/nsTransactionManager.cpp editor/txmgr/src/nsTransactionManager.h editor/txmgr/src/nsTransactionManagerFactory.cpp editor/txmgr/src/nsTransactionStack.cpp editor/txmgr/src/nsTransactionStack.h editor/txmgr/tests/TestTXMgr.cpp editor/ui/composer/content/EditorCommandsDebug.js editor/ui/composer/content/editorOverlay.xul editor/ui/composer/locale/en-US/editorOverlay.dtd mailnews/base/src/nsMessenger.cpp mailnews/base/util/nsMsgTxn.cpp mailnews/base/util/nsMsgTxn.h mailnews/imap/src/nsImapMailFolder.cpp mailnews/imap/src/nsImapUndoTxn.cpp mailnews/imap/src/nsImapUndoTxn.h mailnews/local/src/nsLocalMailFolder.cpp mailnews/local/src/nsLocalUndoTxn.cpp mailnews/local/src/nsLocalUndoTxn.h
2001-03-09 14:23:59 +00:00
NS_IMETHODIMP IMETextTxn::UndoTransaction(void)
{
#ifdef DEBUG_IMETXN
printf("Undo IME Text element = %p\n", mElement.get());
#endif
nsCOMPtr<nsISelectionController> selCon;
mEditor->GetSelectionController(getter_AddRefs(selCon));
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
nsresult result = mElement->DeleteData(mOffset, mStringToInsert.Length());
if (NS_SUCCEEDED(result))
{ // set the selection to the insertion point where the string was removed
nsCOMPtr<nsISelection> selection;
2000-04-27 07:37:12 +00:00
result = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
if (NS_SUCCEEDED(result) && selection) {
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert.");
}
}
return result;
}
NS_IMETHODIMP IMETextTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge)
{
NS_ASSERTION(aDidMerge, "illegal vaule- null ptr- aDidMerge");
NS_ASSERTION(aTransaction, "illegal vaule- null ptr- aTransaction");
NS_ENSURE_TRUE(aDidMerge && aTransaction, NS_ERROR_NULL_POINTER);
#ifdef DEBUG_IMETXN
printf("Merge IME Text element = %p\n", mElement.get());
#endif
//
// check to make sure we aren't fixed, if we are then nothing get's absorbed
//
if (mFixed) {
*aDidMerge = false;
return NS_OK;
}
//
// if aTransaction is another IMETextTxn then absorb it
//
IMETextTxn* otherTxn = nullptr;
nsresult result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn);
if (otherTxn && NS_SUCCEEDED(result))
{
//
// we absorb the next IME transaction by adopting its insert string as our own
//
mStringToInsert = otherTxn->mStringToInsert;
mRanges = otherTxn->mRanges;
*aDidMerge = true;
#ifdef DEBUG_IMETXN
printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction);
#endif
NS_RELEASE(otherTxn);
return NS_OK;
}
*aDidMerge = false;
return NS_OK;
}
2000-07-11 19:51:36 +00:00
NS_IMETHODIMP IMETextTxn::MarkFixed(void)
{
mFixed = true;
2000-07-11 19:51:36 +00:00
return NS_OK;
}
NS_IMETHODIMP IMETextTxn::GetTxnDescription(nsAString& aString)
{
aString.AssignLiteral("IMETextTxn: ");
Fixes for bug #66308 ([embed] XPIDL'ize transaction manager) sr=sfraser@netscape.com,mscott@netscape.com r=jfrancis@netscape.com * nsITransaction, nsITransactionListenter, nsITransactionManager have been XPIDL'ized and moved into mozilla/editor/txmgr/idl. The versions of these interfaces in mozilla/editor/txmgr/public are being CVS removed. * Renamed Do(), Undo(), and Redo() to DoTransaction(), UndoTransaction(), and RedoTransaction() to avoid reserved word problems in languages like JS. I did a sweep through editor and mailnews to remove these methods. * PeekUndoStack() and PeekRedoStack() now return an AddRef'd pointer. * Removed GetUndoString(), GetRedoString() and Write() from the nsITransaction interface. Neither editor or mailnews really made use of these methods. * Removed Write() from the nsITransactionManager.cpp interface. * The Transaction Manager now supports weak references. * Added support for nsITransactionList to the TransactionManager to allow access to all transactions on the Undo and Redo stacks, as well as auto-aggregated transactions. * Removed all references to nsITransactionDescription from txmgr and editor. * Added nsPIEditorTransaction and made all Editor internal transactions inherit from it so we can distinguish between our transactions and ones from 3rd parties. New files checked in: editor/txmgr/idl/nsITransaction.idl editor/txmgr/idl/nsITransactionList.idl editor/txmgr/idl/nsITransactionListener.idl editor/txmgr/idl/nsITransactionManager.idl editor/txmgr/src/nsITransactionList.cpp editor/txmgr/src/nsITransactionList.h editor/idl/nsPIEditorTransaction.idl Files that were CVS removed: editor/txmgr/public/nsITransaction.h editor/txmgr/public/nsITransactionListener.h editor/txmgr/public/nsITransactionManager.h editor/txmgr/idl/nsITransactionDescription.h editor/base/IMECommitTxn.cpp editor/base/IMECommitTxn.h Files modified: editor/Makefile.in editor/makefile.win editor/base/ChangeAttributeTxn.cpp editor/base/ChangeAttributeTxn.h editor/base/CreateElementTxn.cpp editor/base/CreateElementTxn.h editor/base/DeleteElementTxn.cpp editor/base/DeleteElementTxn.h editor/base/DeleteRangeTxn.cpp editor/base/DeleteRangeTxn.h editor/base/DeleteTextTxn.cpp editor/base/DeleteTextTxn.h editor/base/EditAggregateTxn.cpp editor/base/EditAggregateTxn.h editor/base/EditTxn.cpp editor/base/EditTxn.h editor/base/IMECommitTxn.cpp editor/base/IMECommitTxn.h editor/base/IMETextTxn.cpp editor/base/IMETextTxn.h editor/base/InsertElementTxn.cpp editor/base/InsertElementTxn.h editor/base/InsertTextTxn.cpp editor/base/InsertTextTxn.h editor/base/JoinElementTxn.cpp editor/base/JoinElementTxn.h editor/base/nsEditor.cpp editor/base/nsEditorShell.cpp editor/base/nsEditorShell.h editor/base/nsEditorTxnLog.cpp editor/base/nsStyleSheetTxns.cpp editor/base/nsStyleSheetTxns.h editor/base/PlaceholderTxn.cpp editor/base/PlaceholderTxn.h editor/base/SetDocTitleTxn.cpp editor/base/SetDocTitleTxn.h editor/base/SplitElementTxn.cpp editor/base/SplitElementTxn.h editor/idl/Makefile.in editor/idl/makefile.win editor/idl/MANIFEST editor/idl/nsIEditorShell.idl editor/macbuild/editor.mcp editor/macbuild/EditorIDL.mcp editor/txmgr/idl/Makefile.in editor/txmgr/idl/makefile.win editor/txmgr/idl/MANIFEST editor/txmgr/idl/nsITransactionManager.idl editor/txmgr/macbuild/txmgr.mcp editor/txmgr/macbuild/txmgrIDL.mcp editor/txmgr/public/Makefile.in editor/txmgr/public/makefile.win editor/txmgr/public/MANIFEST editor/txmgr/src/Makefile.in editor/txmgr/src/makefile.win editor/txmgr/src/nsTransactionItem.cpp editor/txmgr/src/nsTransactionItem.h editor/txmgr/src/nsTransactionList.cpp editor/txmgr/src/nsTransactionList.h editor/txmgr/src/nsTransactionManager.cpp editor/txmgr/src/nsTransactionManager.h editor/txmgr/src/nsTransactionManagerFactory.cpp editor/txmgr/src/nsTransactionStack.cpp editor/txmgr/src/nsTransactionStack.h editor/txmgr/tests/TestTXMgr.cpp editor/ui/composer/content/EditorCommandsDebug.js editor/ui/composer/content/editorOverlay.xul editor/ui/composer/locale/en-US/editorOverlay.dtd mailnews/base/src/nsMessenger.cpp mailnews/base/util/nsMsgTxn.cpp mailnews/base/util/nsMsgTxn.h mailnews/imap/src/nsImapMailFolder.cpp mailnews/imap/src/nsImapUndoTxn.cpp mailnews/imap/src/nsImapUndoTxn.h mailnews/local/src/nsLocalMailFolder.cpp mailnews/local/src/nsLocalUndoTxn.cpp mailnews/local/src/nsLocalUndoTxn.h
2001-03-09 14:23:59 +00:00
aString += mStringToInsert;
return NS_OK;
}
/* ============ protected methods ================== */
static SelectionType
ToSelectionType(uint32_t aTextRangeType)
{
switch(aTextRangeType) {
case NS_TEXTRANGE_RAWINPUT:
return nsISelectionController::SELECTION_IME_RAWINPUT;
case NS_TEXTRANGE_SELECTEDRAWTEXT:
return nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT;
case NS_TEXTRANGE_CONVERTEDTEXT:
return nsISelectionController::SELECTION_IME_CONVERTEDTEXT;
case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT:
return nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT;
default:
MOZ_CRASH("Selection type is invalid");
return nsISelectionController::SELECTION_NORMAL;
}
}
nsresult
IMETextTxn::SetSelectionForRanges()
{
nsCOMPtr<nsISelectionController> selCon;
mEditor->GetSelectionController(getter_AddRefs(selCon));
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
nsCOMPtr<nsISelection> selection;
nsresult rv =
selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
getter_AddRefs(selection));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
rv = selPriv->StartBatchChanges();
NS_ENSURE_SUCCESS(rv, rv);
// First, remove all selections of IME composition.
static const SelectionType kIMESelections[] = {
nsISelectionController::SELECTION_IME_RAWINPUT,
nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT,
nsISelectionController::SELECTION_IME_CONVERTEDTEXT,
nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT
};
for (uint32_t i = 0; i < ArrayLength(kIMESelections); ++i) {
nsCOMPtr<nsISelection> selectionOfIME;
if (NS_FAILED(selCon->GetSelection(kIMESelections[i],
getter_AddRefs(selectionOfIME)))) {
continue;
}
DebugOnly<nsresult> rv = selectionOfIME->RemoveAllRanges();
NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to remove all ranges of IME selection");
}
// Set caret position and selection of IME composition with TextRangeArray.
bool setCaret = false;
uint32_t countOfRanges = mRanges ? mRanges->Length() : 0;
for (uint32_t i = 0; i < countOfRanges; ++i) {
const TextRange& textRange = mRanges->ElementAt(i);
// Caret needs special handling since its length may be 0 and if it's not
// specified explicitly, we need to handle it ourselves later.
if (textRange.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
NS_ASSERTION(!setCaret, "The ranges already has caret position");
NS_ASSERTION(!textRange.Length(), "nsEditor doesn't support wide caret");
// NOTE: If the caret position is larger than max length of the editor
// content, this may fail.
rv = selection->Collapse(mElement, mOffset + textRange.mStartOffset);
setCaret = setCaret || NS_SUCCEEDED(rv);
NS_ASSERTION(setCaret, "Failed to collapse normal selection");
continue;
}
// If the clause length is 0, it's should be a bug.
if (!textRange.Length()) {
NS_WARNING("Any clauses must not be empty");
continue;
}
2000-04-27 07:37:12 +00:00
nsRefPtr<nsRange> clauseRange;
rv = nsRange::CreateRange(mElement, mOffset + textRange.mStartOffset,
mElement, mOffset + textRange.mEndOffset,
getter_AddRefs(clauseRange));
if (NS_FAILED(rv)) {
NS_WARNING("Failed to create a DOM range for a clause of composition");
break;
}
// Set the range of the clause to selection.
nsCOMPtr<nsISelection> selectionOfIME;
rv = selCon->GetSelection(ToSelectionType(textRange.mRangeType),
getter_AddRefs(selectionOfIME));
if (NS_FAILED(rv)) {
NS_WARNING("Failed to get IME selection");
break;
}
rv = selectionOfIME->AddRange(clauseRange);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to add selection range for a clause of composition");
break;
}
// Set the style of the clause.
nsCOMPtr<nsISelectionPrivate> selectionOfIMEPriv =
do_QueryInterface(selectionOfIME);
if (!selectionOfIMEPriv) {
NS_WARNING("Failed to get nsISelectionPrivate interface from selection");
continue; // Since this is additional feature, we can continue this job.
}
rv = selectionOfIMEPriv->SetTextRangeStyle(clauseRange,
textRange.mRangeStyle);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to set selection style");
break; // but this is unexpected...
}
}
// If the ranges doesn't include explicit caret position, let's set the
// caret to the end of composition string.
if (!setCaret) {
rv = selection->Collapse(mElement, mOffset + mStringToInsert.Length());
NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to set caret at the end of composition string");
}
rv = selPriv->EndBatchChanges();
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to end batch changes");
return rv;
}