gecko-dev/editor/base/DeleteRangeTxn.cpp

352 lines
10 KiB
C++
Raw Normal View History

/* -*- 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.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*/
#include "DeleteRangeTxn.h"
#include "nsIDOMRange.h"
#include "nsIDOMCharacterData.h"
#include "nsIDOMNodeList.h"
#include "nsIDOMSelection.h"
#include "DeleteTextTxn.h"
#include "DeleteElementTxn.h"
#include "TransactionFactory.h"
1999-03-09 19:22:48 +00:00
#include "nsIContentIterator.h"
#include "nsIContent.h"
#include "nsLayoutCID.h"
#ifdef NS_DEBUG
#include "nsIDOMElement.h"
#endif
#ifdef NS_DEBUG
static PRBool gNoisy = PR_FALSE;
#else
static const PRBool gNoisy = PR_FALSE;
#endif
// note that aEditor is not refcounted
DeleteRangeTxn::DeleteRangeTxn()
: EditAggregateTxn()
{
SetTransactionDescriptionID( kTransactionID );
/* log description initialized in parent constructor */
}
NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, nsIDOMRange *aRange)
{
NS_ASSERTION(aEditor && aRange, "bad state");
if (!aEditor || !aRange) { return NS_ERROR_NOT_INITIALIZED; }
mEditor = aEditor;
mRange = do_QueryInterface(aRange);
nsresult result = aRange->GetStartParent(getter_AddRefs(mStartParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
result = aRange->GetEndParent(getter_AddRefs(mEndParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
result = aRange->GetStartOffset(&mStartOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
result = aRange->GetEndOffset(&mEndOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
result = aRange->GetCommonParent(getter_AddRefs(mCommonParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
#ifdef NS_DEBUG
{
PRUint32 count;
nsCOMPtr<nsIDOMCharacterData> textNode;
textNode = do_QueryInterface(mStartParent);
if (textNode)
textNode->GetLength(&count);
else
{
nsCOMPtr<nsIDOMNodeList> children;
result = mStartParent->GetChildNodes(getter_AddRefs(children));
NS_ASSERTION(((NS_SUCCEEDED(result)) && children), "bad start child list");
children->GetLength(&count);
}
NS_ASSERTION(mStartOffset<=(PRInt32)count, "bad start offset");
textNode = do_QueryInterface(mEndParent);
if (textNode)
textNode->GetLength(&count);
else
{
nsCOMPtr<nsIDOMNodeList> children;
result = mEndParent->GetChildNodes(getter_AddRefs(children));
NS_ASSERTION(((NS_SUCCEEDED(result)) && children), "bad end child list");
children->GetLength(&count);
}
NS_ASSERTION(mEndOffset<=(PRInt32)count, "bad end offset");
if (gNoisy)
1999-03-09 19:22:48 +00:00
{
printf ("DeleteRange: %d of %p to %d of %p\n",
mStartOffset, (void *)mStartParent, mEndOffset, (void *)mEndParent);
1999-03-09 19:22:48 +00:00
}
}
#endif
return result;
}
DeleteRangeTxn::~DeleteRangeTxn()
{
}
NS_IMETHODIMP DeleteRangeTxn::Do(void)
{
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used anywhere any more. * Cleaned up split and join undo/redo. * Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type * Ctrl-B with an insertion point and the next character will be bold. * Added all the logic to handle inline vs. block elements when setting text properties. * Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline. * Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements. * Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ... * Fully undoable and redoable. * Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree. Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 17:58:07 +00:00
if (gNoisy) { printf("Do Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result;
// build the child transactions
if (mStartParent==mEndParent)
{ // the selection begins and ends in the same node
result = CreateTxnsToDeleteBetween(mStartParent, mStartOffset, mEndOffset);
}
else
{ // the selection ends in a different node from where it started
// delete the relevant content in the start node
result = CreateTxnsToDeleteContent(mStartParent, mStartOffset, nsIEditor::eNext);
if (NS_SUCCEEDED(result))
{
// delete the intervening nodes
1999-03-09 19:22:48 +00:00
result = CreateTxnsToDeleteNodesBetween();
if (NS_SUCCEEDED(result))
{
// delete the relevant content in the end node
result = CreateTxnsToDeleteContent(mEndParent, mEndOffset, nsIEditor::ePrevious);
}
}
}
// if we've successfully built this aggregate transaction, then do it.
if (NS_SUCCEEDED(result)) {
result = EditAggregateTxn::Do();
}
if (NS_FAILED(result)) return result;
// only set selection to deletion point if editor gives permission
PRBool bAdjustSelection;
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
if (bAdjustSelection)
{
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
}
else
{
// do nothing - dom range gravity will adjust selection
}
return result;
}
NS_IMETHODIMP DeleteRangeTxn::Undo(void)
{
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used anywhere any more. * Cleaned up split and join undo/redo. * Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type * Ctrl-B with an insertion point and the next character will be bold. * Added all the logic to handle inline vs. block elements when setting text properties. * Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline. * Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements. * Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ... * Fully undoable and redoable. * Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree. Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 17:58:07 +00:00
if (gNoisy) { printf("Undo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Undo();
return result;
}
NS_IMETHODIMP DeleteRangeTxn::Redo(void)
{
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used anywhere any more. * Cleaned up split and join undo/redo. * Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type * Ctrl-B with an insertion point and the next character will be bold. * Added all the logic to handle inline vs. block elements when setting text properties. * Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline. * Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements. * Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ... * Fully undoable and redoable. * Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree. Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 17:58:07 +00:00
if (gNoisy) { printf("Redo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Redo();
return result;
}
NS_IMETHODIMP DeleteRangeTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
{
if (nsnull!=aDidMerge)
*aDidMerge=PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTxn::Write(nsIOutputStream *aOutputStream)
{
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTxn::GetUndoString(nsString *aString)
{
if (nsnull!=aString)
{
*aString="Insert Range: ";
}
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTxn::GetRedoString(nsString *aString)
{
if (nsnull!=aString)
{
*aString="Remove Range: ";
}
return NS_OK;
}
NS_IMETHODIMP
DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
PRUint32 aStartOffset,
PRUint32 aEndOffset)
{
nsresult result;
// see what kind of node we have
nsCOMPtr<nsIDOMCharacterData> textNode;
textNode = do_QueryInterface(aStartParent);
if (textNode)
{ // if the node is a text node, then delete text content
DeleteTextTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
PRInt32 numToDel;
if (aStartOffset==aEndOffset)
numToDel = 1;
else
numToDel = aEndOffset-aStartOffset;
txn->Init(mEditor, textNode, aStartOffset, numToDel);
AppendChild(txn);
}
else
{
PRUint32 childCount;
nsCOMPtr<nsIDOMNodeList> children;
result = aStartParent->GetChildNodes(getter_AddRefs(children));
if (NS_FAILED(result)) return result;
if (!children) return NS_ERROR_NULL_POINTER;
children->GetLength(&childCount);
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
PRUint32 i;
for (i=aStartOffset; i<aEndOffset; i++)
{
nsCOMPtr<nsIDOMNode> child;
result = children->Item(i, getter_AddRefs(child));
if (NS_FAILED(result)) return result;
if (!child) return NS_ERROR_NULL_POINTER;
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(child);
AppendChild(txn);
}
}
return result;
}
NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteContent(nsIDOMNode *aParent,
PRUint32 aOffset,
nsIEditor::EDirection aAction)
{
nsresult result;
// see what kind of node we have
nsCOMPtr<nsIDOMCharacterData> textNode;
textNode = do_QueryInterface(aParent);
if (textNode)
{ // if the node is a text node, then delete text content
PRUint32 start, numToDelete;
if (nsIEditor::eNext == aAction)
{
start=aOffset;
textNode->GetLength(&numToDelete);
numToDelete -= aOffset;
}
else
{
start=0;
numToDelete=aOffset;
}
1999-03-09 19:22:48 +00:00
if (numToDelete)
{
1999-03-09 19:22:48 +00:00
DeleteTextTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(mEditor, textNode, start, numToDelete);
AppendChild(txn);
}
}
return result;
}
1999-03-09 19:22:48 +00:00
static NS_DEFINE_IID(kSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
{
nsresult result;
1999-03-09 19:22:48 +00:00
nsCOMPtr<nsIContentIterator> iter;
result = nsComponentManager::CreateInstance(kSubtreeIteratorCID,
1999-03-09 19:22:48 +00:00
nsnull,
NS_GET_IID(nsIContentIterator),
1999-03-09 19:22:48 +00:00
getter_AddRefs(iter));
if (NS_FAILED(result)) return result;
if (!iter) return NS_ERROR_NULL_POINTER;
1999-03-09 19:22:48 +00:00
result = iter->Init(mRange);
if (NS_FAILED(result)) return result;
1999-03-09 19:22:48 +00:00
while (NS_ENUMERATOR_FALSE == iter->IsDone())
{
1999-03-09 19:22:48 +00:00
nsCOMPtr<nsIDOMNode> node;
nsCOMPtr<nsIContent> content;
result = iter->CurrentNode(getter_AddRefs(content));
node = do_QueryInterface(content);
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(node);
AppendChild(txn);
1999-03-09 19:22:48 +00:00
iter->Next();
}
return result;
}