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

361 строка
10 KiB
C++
Исходник Обычный вид История

/* -*- 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-1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#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 22:22:48 +03: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 22:22:48 +03:00
{
printf ("DeleteRange: %d of %p to %d of %p\n",
mStartOffset, (void *)mStartParent, mEndOffset, (void *)mEndParent);
1999-03-09 22:22:48 +03: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 21:58:07 +04: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::eDeleteNext);
if (NS_SUCCEEDED(result))
{
// delete the intervening nodes
1999-03-09 22:22:48 +03:00
result = CreateTxnsToDeleteNodesBetween();
if (NS_SUCCEEDED(result))
{
// delete the relevant content in the end node
result = CreateTxnsToDeleteContent(mEndParent, mEndOffset, nsIEditor::eDeletePrevious);
}
}
}
// if we've successfully built this aggregate transaction, then do it.
if (NS_SUCCEEDED(result)) {
result = EditAggregateTxn::Do();
}
if (NS_SUCCEEDED(result)) {
// set the resulting selection
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);
}
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 21:58:07 +04:00
if (gNoisy) { printf("Undo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Undo();
if (NS_SUCCEEDED(result))
{
// set the resulting selection
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);
if (NS_FAILED(result)) return result;
result = selection->Extend(mEndParent, mEndOffset);
}
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 21:58:07 +04:00
if (gNoisy) { printf("Redo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Redo();
if (NS_SUCCEEDED(result)) {
// set the resulting selection
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);
}
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::ESelectionCollapseDirection 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::eDeleteNext == aAction)
{
start=aOffset;
textNode->GetLength(&numToDelete);
numToDelete -= aOffset;
}
else
{
start=0;
numToDelete=aOffset;
}
1999-03-09 22:22:48 +03:00
if (numToDelete)
{
1999-03-09 22:22:48 +03: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 22:22:48 +03: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 22:22:48 +03:00
nsCOMPtr<nsIContentIterator> iter;
result = nsComponentManager::CreateInstance(kSubtreeIteratorCID,
1999-03-09 22:22:48 +03:00
nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if (NS_FAILED(result)) return result;
if (!iter) return NS_ERROR_NULL_POINTER;
1999-03-09 22:22:48 +03:00
result = iter->Init(mRange);
if (NS_FAILED(result)) return result;
1999-03-09 22:22:48 +03:00
while (NS_COMFALSE == iter->IsDone())
{
1999-03-09 22:22:48 +03: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 22:22:48 +03:00
iter->Next();
}
return result;
}