1999-01-07 04:02:32 +03: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
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "InsertTextTxn.h"
|
1999-02-12 20:18:58 +03:00
|
|
|
#include "nsEditor.h"
|
1999-01-07 04:02:32 +03:00
|
|
|
#include "nsIDOMCharacterData.h"
|
1999-01-29 21:57:56 +03:00
|
|
|
#include "nsIDOMSelection.h"
|
|
|
|
#include "nsIPresShell.h"
|
1999-02-15 21:24:58 +03:00
|
|
|
#include "EditAggregateTxn.h"
|
1999-01-07 04:02:32 +03:00
|
|
|
|
1999-01-21 04:51:09 +03:00
|
|
|
static NS_DEFINE_IID(kInsertTextTxnIID, INSERT_TEXT_TXN_IID);
|
1999-01-29 21:57:56 +03:00
|
|
|
static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID);
|
1999-01-14 21:02:45 +03:00
|
|
|
|
1999-02-15 21:24:58 +03:00
|
|
|
nsIAtom *InsertTextTxn::gInsertTextTxnName;
|
|
|
|
|
|
|
|
nsresult InsertTextTxn::ClassInit()
|
|
|
|
{
|
|
|
|
if (nsnull==gInsertTextTxnName)
|
|
|
|
gInsertTextTxnName = NS_NewAtom("NS_InsertTextTxn");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-21 04:51:09 +03:00
|
|
|
|
|
|
|
InsertTextTxn::InsertTextTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-03-06 00:05:35 +03:00
|
|
|
InsertTextTxn::~InsertTextTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
|
1999-01-07 04:02:32 +03:00
|
|
|
PRUint32 aOffset,
|
1999-01-29 21:57:56 +03:00
|
|
|
const nsString& aStringToInsert,
|
|
|
|
nsIPresShell* aPresShell)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
1999-02-19 14:30:31 +03:00
|
|
|
mElement = do_QueryInterface(aElement);
|
1999-01-07 04:02:32 +03:00
|
|
|
mOffset = aOffset;
|
|
|
|
mStringToInsert = aStringToInsert;
|
1999-01-29 21:57:56 +03:00
|
|
|
mPresShell = aPresShell;
|
1999-01-21 04:51:09 +03:00
|
|
|
return NS_OK;
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Do(void)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
1999-01-29 21:57:56 +03:00
|
|
|
// advance caret: This requires the presentation shell to get the selection.
|
1999-02-12 02:12:28 +03:00
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
1999-02-22 18:53:31 +03:00
|
|
|
nsresult result = mPresShell->GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ASSERTION(selection,"Could not get selection in InsertTextTxn::Do\n");
|
|
|
|
if (NS_SUCCEEDED(result) && selection) {
|
|
|
|
result = mElement->InsertData(mOffset, mStringToInsert);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
|
|
|
|
}
|
1999-01-29 21:57:56 +03:00
|
|
|
}
|
1999-02-22 18:53:31 +03:00
|
|
|
return result;
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Undo(void)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
1999-01-28 22:00:14 +03:00
|
|
|
nsresult result;
|
1999-01-07 04:02:32 +03:00
|
|
|
PRUint32 length = mStringToInsert.Length();
|
1999-01-28 22:00:14 +03:00
|
|
|
result = mElement->DeleteData(mOffset, length);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{ // set the selection to the insertion point where the string was removed
|
1999-02-12 20:18:58 +03:00
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
result = mPresShell->GetSelection(getter_AddRefs(selection));
|
1999-02-22 18:53:31 +03:00
|
|
|
if (NS_SUCCEEDED(result) && selection) {
|
1999-02-12 20:18:58 +03:00
|
|
|
result = selection->Collapse(mElement, mOffset);
|
1999-02-22 18:53:31 +03:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
|
1999-02-12 20:18:58 +03:00
|
|
|
}
|
1999-01-28 22:00:14 +03:00
|
|
|
}
|
|
|
|
return result;
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
1999-01-14 21:02:45 +03:00
|
|
|
// set out param default value
|
|
|
|
if (nsnull!=aDidMerge)
|
|
|
|
*aDidMerge=PR_FALSE;
|
1999-02-17 04:59:56 +03:00
|
|
|
nsresult result = NS_OK;
|
1999-01-14 21:02:45 +03:00
|
|
|
if ((nsnull!=aDidMerge) && (nsnull!=aTransaction))
|
|
|
|
{
|
1999-01-28 20:19:24 +03:00
|
|
|
// if aTransaction isa InsertTextTxn, and if the selection hasn't changed,
|
|
|
|
// then absorb it
|
1999-03-03 01:27:46 +03:00
|
|
|
nsCOMPtr<InsertTextTxn> otherTxn( do_QueryInterface(aTransaction) );
|
1999-02-15 21:24:58 +03:00
|
|
|
if (otherTxn)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
1999-01-28 22:00:14 +03:00
|
|
|
if (PR_TRUE==IsSequentialInsert(otherTxn))
|
|
|
|
{
|
|
|
|
nsAutoString otherData;
|
|
|
|
otherTxn->GetData(otherData);
|
|
|
|
mStringToInsert += otherData;
|
|
|
|
*aDidMerge = PR_TRUE;
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
1999-02-15 21:24:58 +03:00
|
|
|
else
|
|
|
|
{ // the next InsertTextTxn might be inside an aggregate that we have special knowledge of
|
1999-03-03 01:27:46 +03:00
|
|
|
nsCOMPtr<EditAggregateTxn> otherTxn( do_QueryInterface(aTransaction) );
|
1999-02-15 21:24:58 +03:00
|
|
|
if (otherTxn)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> txnName;
|
|
|
|
otherTxn->GetName(getter_AddRefs(txnName));
|
1999-02-17 09:18:11 +03:00
|
|
|
if (txnName.get()==gInsertTextTxnName)
|
1999-02-15 21:24:58 +03:00
|
|
|
{ // yep, it's one of ours. By definition, it must contain only
|
|
|
|
// a single InsertTextTxn
|
|
|
|
nsCOMPtr<EditTxn> childTxn;
|
|
|
|
otherTxn->GetTxnAt(0, getter_AddRefs(childTxn));
|
1999-02-17 04:59:56 +03:00
|
|
|
nsCOMPtr<InsertTextTxn> otherInsertTxn;
|
|
|
|
otherInsertTxn = do_QueryInterface(childTxn, &result);
|
1999-02-15 21:24:58 +03:00
|
|
|
if (otherInsertTxn)
|
|
|
|
{
|
|
|
|
if (PR_TRUE==IsSequentialInsert(otherInsertTxn))
|
|
|
|
{
|
|
|
|
nsAutoString otherData;
|
|
|
|
otherInsertTxn->GetData(otherData);
|
|
|
|
mStringToInsert += otherData;
|
|
|
|
*aDidMerge = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
1999-02-17 04:59:56 +03:00
|
|
|
return result;
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Write(nsIOutputStream *aOutputStream)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::GetUndoString(nsString **aString)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
|
|
|
if (nsnull!=aString)
|
|
|
|
{
|
|
|
|
**aString="Remove Text: ";
|
|
|
|
**aString += mStringToInsert;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::GetRedoString(nsString **aString)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
|
|
|
if (nsnull!=aString)
|
|
|
|
{
|
|
|
|
**aString="Insert Text: ";
|
|
|
|
**aString += mStringToInsert;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
|
|
|
/* ============= nsISupports implementation ====================== */
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP
|
1999-01-14 21:02:45 +03:00
|
|
|
InsertTextTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
if (nsnull == aInstancePtr) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kInsertTextTxnIID)) {
|
|
|
|
*aInstancePtr = (void*)(InsertTextTxn*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return (EditTxn::QueryInterface(aIID, aInstancePtr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============ protected methods ================== */
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertTextTxn::GetData(nsString& aResult)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
|
|
|
aResult = mStringToInsert;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-28 22:00:14 +03:00
|
|
|
|
|
|
|
PRBool InsertTextTxn::IsSequentialInsert(InsertTextTxn *aOtherTxn)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(nsnull!=aOtherTxn, "null param");
|
|
|
|
PRBool result=PR_FALSE;
|
|
|
|
if (nsnull!=aOtherTxn)
|
|
|
|
{
|
|
|
|
if (aOtherTxn->mElement == mElement)
|
|
|
|
{
|
|
|
|
// here, we need to compare offsets.
|
1999-02-15 21:24:58 +03:00
|
|
|
PRInt32 strlen = mStringToInsert.Length();
|
|
|
|
if (aOtherTxn->mOffset==(mOffset+strlen))
|
1999-01-28 22:00:14 +03:00
|
|
|
{
|
|
|
|
result = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|