1999-06-30 00:31:22 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-09-22 02:31:27 +04:00
|
|
|
* Copyright (C) 1998-1999 Netscape Communications Corporation. All Rights
|
1999-06-30 00:31:22 +04:00
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IMETextTxn.h"
|
|
|
|
#include "IMECommitTxn.h"
|
|
|
|
#include "nsEditor.h"
|
|
|
|
#include "nsIDOMCharacterData.h"
|
1999-08-06 06:32:13 +04:00
|
|
|
#include "nsIPrivateTextRange.h"
|
1999-06-30 00:31:22 +04:00
|
|
|
#include "nsIDOMSelection.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "EditAggregateTxn.h"
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID);
|
|
|
|
|
|
|
|
nsIAtom *IMETextTxn::gIMETextTxnName = nsnull;
|
|
|
|
|
|
|
|
nsresult IMETextTxn::ClassInit()
|
|
|
|
{
|
|
|
|
if (nsnull==gIMETextTxnName)
|
|
|
|
gIMETextTxnName = NS_NewAtom("NS_IMETextTxn");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMETextTxn::IMETextTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
1999-09-22 02:31:27 +04:00
|
|
|
SetTransactionDescriptionID( kTransactionID );
|
|
|
|
/* log description initialized in parent constructor */
|
1999-06-30 00:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IMETextTxn::~IMETextTxn()
|
|
|
|
{
|
1999-08-25 14:51:55 +04:00
|
|
|
mRangeList = do_QueryInterface(nsnull);
|
1999-06-30 00:31:22 +04:00
|
|
|
}
|
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aReplaceLength,
|
|
|
|
nsIPrivateTextRangeList *aTextRangeList,
|
|
|
|
const nsString &aStringToInsert,
|
|
|
|
nsWeakPtr aPresShellWeak)
|
1999-06-30 00:31:22 +04:00
|
|
|
{
|
|
|
|
mElement = do_QueryInterface(aElement);
|
|
|
|
mOffset = aOffset;
|
|
|
|
mReplaceLength = aReplaceLength;
|
|
|
|
mStringToInsert = aStringToInsert;
|
1999-08-25 14:51:55 +04:00
|
|
|
mPresShellWeak = aPresShellWeak;
|
1999-06-30 00:31:22 +04:00
|
|
|
mRangeList = do_QueryInterface(aTextRangeList);
|
|
|
|
mFixed = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::Do(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef DEBUG_TAGUE
|
|
|
|
printf("Do IME Text element = %p\n", mElement.get());
|
|
|
|
#endif
|
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// advance caret: This requires the presentation shell to get the selection.
|
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
NS_ASSERTION(selection,"Could not get selection in IMEtextTxn::Do\n");
|
|
|
|
if (NS_SUCCEEDED(result) && selection) {
|
|
|
|
if (mReplaceLength==0) {
|
|
|
|
result = mElement->InsertData(mOffset,mStringToInsert);
|
|
|
|
} else {
|
|
|
|
result = mElement->ReplaceData(mOffset,mReplaceLength,mStringToInsert);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = CollapseTextSelection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1999-06-30 00:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::Undo(void)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_TAGUE
|
|
|
|
printf("Undo IME Text element = %p\n", mElement.get());
|
|
|
|
#endif
|
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
1999-06-30 00:31:22 +04:00
|
|
|
nsresult result;
|
|
|
|
PRUint32 length = mStringToInsert.Length();
|
|
|
|
result = mElement->DeleteData(mOffset, length);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{ // set the selection to the insertion point where the string was removed
|
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
1999-08-25 14:51:55 +04:00
|
|
|
result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
|
1999-06-30 00:31:22 +04:00
|
|
|
if (NS_SUCCEEDED(result) && selection) {
|
1999-07-18 06:27:19 +04:00
|
|
|
result = selection->Collapse(mElement, mOffset);
|
1999-06-30 00:31:22 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
|
|
|
|
{
|
1999-08-25 14:51:55 +04:00
|
|
|
nsresult result;
|
1999-06-30 00:31:22 +04:00
|
|
|
#ifdef DEBUG_TAGUE
|
|
|
|
printf("Merge IME Text element = %p\n", mElement.get());
|
|
|
|
#endif
|
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
//
|
|
|
|
// check to make sure we have valid return pointers
|
|
|
|
//
|
|
|
|
if ((nsnull==aDidMerge) && (nsnull==aTransaction))
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// check to make sure we aren't fixed, if we are then nothing get's absorbed
|
|
|
|
//
|
|
|
|
if (mFixed) {
|
|
|
|
*aDidMerge = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// if aTransaction is another IMETextTxn then absorbe it
|
|
|
|
//
|
|
|
|
IMETextTxn* otherTxn = nsnull;
|
|
|
|
result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn);
|
1999-08-31 10:34:56 +04:00
|
|
|
if (otherTxn && NS_SUCCEEDED(NS_OK))
|
1999-08-25 14:51:55 +04:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// we absorbe the next IME transaction by adopting it's insert string as our own
|
|
|
|
//
|
|
|
|
nsIPrivateTextRangeList* newTextRangeList;
|
|
|
|
otherTxn->GetData(mStringToInsert,&newTextRangeList);
|
|
|
|
mRangeList = do_QueryInterface(newTextRangeList);
|
|
|
|
*aDidMerge = PR_TRUE;
|
1999-06-30 00:31:22 +04:00
|
|
|
#ifdef DEBUG_TAGUE
|
1999-08-25 14:51:55 +04:00
|
|
|
printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction);
|
1999-06-30 00:31:22 +04:00
|
|
|
#endif
|
1999-08-25 14:51:55 +04:00
|
|
|
NS_RELEASE(otherTxn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// second possible case is that we have a commit transaction
|
|
|
|
//
|
|
|
|
IMECommitTxn* commitTxn = nsnull;
|
|
|
|
result = aTransaction->QueryInterface(IMECommitTxn::GetCID(),(void**)&commitTxn);
|
1999-08-31 10:34:56 +04:00
|
|
|
if (commitTxn && NS_SUCCEEDED(NS_OK))
|
1999-08-25 14:51:55 +04:00
|
|
|
{
|
|
|
|
(void)CollapseTextSelectionOnCommit();
|
|
|
|
mFixed = PR_TRUE;
|
|
|
|
*aDidMerge = PR_TRUE; // absorbe the commit transaction
|
1999-06-30 00:31:22 +04:00
|
|
|
#ifdef DEBUG_TAGUE
|
1999-08-25 14:51:55 +04:00
|
|
|
printf("IMETextTxn assimilated IMECommitTxn%p\n", aTransaction);
|
1999-06-30 00:31:22 +04:00
|
|
|
#endif
|
1999-08-25 14:51:55 +04:00
|
|
|
NS_RELEASE(commitTxn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-06-30 00:31:22 +04:00
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
*aDidMerge = PR_FALSE;
|
|
|
|
return NS_OK;
|
1999-06-30 00:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::Write(nsIOutputStream *aOutputStream)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::GetUndoString(nsString *aString)
|
|
|
|
{
|
|
|
|
if (nsnull!=aString)
|
|
|
|
{
|
|
|
|
*aString="Remove Text: ";
|
|
|
|
*aString += mStringToInsert;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::GetRedoString(nsString *aString)
|
|
|
|
{
|
|
|
|
if (nsnull!=aString)
|
|
|
|
{
|
|
|
|
*aString="Insert Text: ";
|
|
|
|
*aString += mStringToInsert;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============= nsISupports implementation ====================== */
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
if (nsnull == aInstancePtr) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-07-15 23:13:46 +04:00
|
|
|
if (aIID.Equals(IMETextTxn::GetCID())) {
|
1999-06-30 00:31:22 +04:00
|
|
|
*aInstancePtr = (void*)(IMETextTxn*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return (EditTxn::QueryInterface(aIID, aInstancePtr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============ protected methods ================== */
|
|
|
|
|
1999-08-06 06:32:13 +04:00
|
|
|
NS_IMETHODIMP IMETextTxn::GetData(nsString& aResult,nsIPrivateTextRangeList** aTextRangeList)
|
1999-06-30 00:31:22 +04:00
|
|
|
{
|
|
|
|
aResult = mStringToInsert;
|
|
|
|
*aTextRangeList = mRangeList;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::CollapseTextSelection(void)
|
|
|
|
{
|
1999-08-25 14:51:55 +04:00
|
|
|
nsresult result;
|
|
|
|
PRBool haveSelectedRange, haveCaretPosition;
|
|
|
|
PRUint16 textRangeListLength,selectionStart,selectionEnd,
|
|
|
|
textRangeType, caretPosition, i;
|
|
|
|
nsIPrivateTextRange* textRange;
|
1999-06-30 00:31:22 +04:00
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
haveSelectedRange = PR_FALSE;
|
|
|
|
haveCaretPosition = PR_FALSE;
|
|
|
|
|
1999-06-30 00:31:22 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_tague
|
1999-08-25 14:51:55 +04:00
|
|
|
PRUint16 listlen,start,stop,type;
|
|
|
|
nsIPrivateTextRange* rangePtr;
|
|
|
|
result = mRangeList->GetLength(&listlen);
|
|
|
|
printf("nsIPrivateTextRangeList[%p]\n",mRangeList);
|
|
|
|
for (i=0;i<listlen;i++) {
|
|
|
|
(void)mRangeList->Item(i,&rangePtr);
|
|
|
|
rangePtr->GetRangeStart(&start);
|
|
|
|
rangePtr->GetRangeEnd(&stop);
|
|
|
|
rangePtr->GetRangeType(&type);
|
|
|
|
printf("range[%d] start=%d end=%d type=",i,start,stop,type);
|
|
|
|
if (type==nsIPrivateTextRange::TEXTRANGE_RAWINPUT) printf("TEXTRANGE_RAWINPUT\n");
|
|
|
|
if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT) printf("TEXTRANGE_SELECTEDRAWTEXT\n");
|
|
|
|
if (type==nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT) printf("TEXTRANGE_CONVERTEDTEXT\n");
|
|
|
|
if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) printf("TEXTRANGE_SELECTEDCONVERTEDTEXT\n");
|
|
|
|
}
|
1999-06-30 00:31:22 +04:00
|
|
|
#endif
|
1999-08-25 14:51:55 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// run through the text range list
|
|
|
|
//
|
|
|
|
result = mRangeList->GetLength(&textRangeListLength);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
for(i=0;i<textRangeListLength;i++) {
|
|
|
|
result = mRangeList->Item(i,&textRange);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
result = textRange->GetRangeType(&textRangeType);
|
|
|
|
if (textRangeType==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT)
|
|
|
|
{
|
|
|
|
haveSelectedRange = PR_TRUE;
|
|
|
|
textRange->GetRangeStart(&selectionStart);
|
|
|
|
textRange->GetRangeEnd(&selectionEnd);
|
|
|
|
}
|
|
|
|
if (textRangeType==nsIPrivateTextRange::TEXTRANGE_CARETPOSITION)
|
|
|
|
{
|
|
|
|
haveCaretPosition = PR_TRUE;
|
|
|
|
textRange->GetRangeStart(&caretPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(result) && selection){
|
|
|
|
if (haveSelectedRange) {
|
|
|
|
result = selection->Collapse(mElement,mOffset+selectionStart);
|
|
|
|
result = selection->Extend(mElement,mOffset+selectionEnd);
|
|
|
|
} else {
|
|
|
|
if (haveCaretPosition)
|
|
|
|
result = selection->Collapse(mElement,mOffset+caretPosition);
|
|
|
|
else
|
|
|
|
result = selection->Collapse(mElement,mOffset+mStringToInsert.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1999-06-30 00:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP IMETextTxn::CollapseTextSelectionOnCommit(void)
|
|
|
|
{
|
1999-08-25 14:51:55 +04:00
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(result) && selection){
|
|
|
|
result = selection->Collapse(mElement,mOffset+mStringToInsert.Length());
|
|
|
|
}
|
1999-06-30 00:31:22 +04:00
|
|
|
|
1999-08-25 14:51:55 +04:00
|
|
|
return result;
|
1999-06-30 00:31:22 +04:00
|
|
|
}
|