pjs/editor/base/InsertTextTxn.cpp

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

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"
#include "nsEditor.h"
1999-01-07 04:02:32 +03:00
#include "nsIDOMCharacterData.h"
#include "nsIDOMSelection.h"
#include "nsIPresShell.h"
#include "EditAggregateTxn.h"
1999-01-07 04:02:32 +03:00
static NS_DEFINE_IID(kIDOMSelectionIID, NS_IDOMSELECTION_IID);
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
#ifdef NS_DEBUG
static PRBool gNoisy = PR_FALSE;
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
#else
static const PRBool gNoisy = PR_FALSE;
#endif
nsIAtom *InsertTextTxn::gInsertTextTxnName;
nsresult InsertTextTxn::ClassInit()
{
if (nsnull==gInsertTextTxnName)
gInsertTextTxnName = NS_NewAtom("NS_InsertTextTxn");
return NS_OK;
}
InsertTextTxn::InsertTextTxn()
: EditTxn()
{
}
InsertTextTxn::~InsertTextTxn()
{
}
NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
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
PRUint32 aOffset,
const nsString &aStringToInsert,
nsWeakPtr aPresShellWeak)
1999-01-07 04:02:32 +03:00
{
#if 0 //def DEBUG_cmanske
nsString text;
aElement->GetData(text);
printf("InsertTextTxn: Offset to insert at = %d. Text of the node to insert into:\n", aOffset);
wprintf(text.GetUnicode());
printf("\n");
#endif
NS_ASSERTION(aElement && aPresShellWeak, "bad args");
if (!aElement || !aPresShellWeak) return NS_ERROR_NULL_POINTER;
mElement = do_QueryInterface(aElement);
1999-01-07 04:02:32 +03:00
mOffset = aOffset;
mStringToInsert = aStringToInsert;
mPresShellWeak = aPresShellWeak;
return NS_OK;
1999-01-07 04:02:32 +03:00
}
NS_IMETHODIMP InsertTextTxn::Do(void)
1999-01-07 04:02:32 +03:00
{
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 Insert Text element = %p\n", mElement.get()); }
NS_ASSERTION(mElement && mPresShellWeak, "bad state");
if (!mElement || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; }
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));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
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.");
}
return result;
1999-01-07 04:02:32 +03:00
}
NS_IMETHODIMP InsertTextTxn::Undo(void)
1999-01-07 04:02:32 +03:00
{
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 Insert Text element = %p\n", mElement.get()); }
NS_ASSERTION(mElement && mPresShellWeak, "bad state");
if (!mElement || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; }
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
if (!ps) return NS_ERROR_NOT_INITIALIZED;
nsresult result;
1999-01-07 04:02:32 +03:00
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;
result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
1999-01-07 04:02:32 +03:00
}
NS_IMETHODIMP InsertTextTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
1999-01-07 04:02:32 +03:00
{
// set out param default value
if (nsnull!=aDidMerge)
*aDidMerge=PR_FALSE;
nsresult result = NS_OK;
if ((nsnull!=aDidMerge) && (nsnull!=aTransaction))
{
// if aTransaction isa InsertTextTxn, and if the selection hasn't changed,
// then absorb it
1999-06-28 17:44:40 +04:00
InsertTextTxn *otherInsTxn = nsnull;
aTransaction->QueryInterface(InsertTextTxn::GetCID(), (void **)&otherInsTxn);
1999-06-28 17:44:40 +04:00
if (otherInsTxn)
{
1999-06-28 17:44:40 +04:00
if (PR_TRUE==IsSequentialInsert(otherInsTxn))
{
nsAutoString otherData;
1999-06-28 17:44:40 +04:00
otherInsTxn->GetData(otherData);
mStringToInsert += otherData;
*aDidMerge = PR_TRUE;
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("InsertTextTxn assimilated %p\n", aTransaction); }
}
1999-06-28 17:44:40 +04:00
NS_RELEASE(otherInsTxn);
}
else
{ // the next InsertTextTxn might be inside an aggregate that we have special knowledge of
1999-06-25 03:33:18 +04:00
EditAggregateTxn *otherTxn = nsnull;
aTransaction->QueryInterface(EditAggregateTxn::GetCID(), (void **)&otherTxn);
if (otherTxn)
{
nsCOMPtr<nsIAtom> txnName;
otherTxn->GetName(getter_AddRefs(txnName));
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 (txnName && txnName.get()==gInsertTextTxnName)
{ // yep, it's one of ours. By definition, it must contain only
// another aggregate with a single child,
// or a single InsertTextTxn
nsCOMPtr<EditTxn> childTxn;
otherTxn->GetTxnAt(0, getter_AddRefs(childTxn));
if (childTxn)
{
nsCOMPtr<InsertTextTxn> otherInsertTxn;
otherInsertTxn = do_QueryInterface(childTxn);
if (otherInsertTxn)
{
if (PR_TRUE==IsSequentialInsert(otherInsertTxn))
{
nsAutoString otherData;
otherInsertTxn->GetData(otherData);
mStringToInsert += otherData;
*aDidMerge = PR_TRUE;
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("InsertTextTxn assimilated %p\n", aTransaction); }
}
}
}
}
1999-06-25 03:33:18 +04:00
NS_RELEASE(otherTxn);
}
}
}
return result;
1999-01-07 04:02:32 +03:00
}
NS_IMETHODIMP InsertTextTxn::Write(nsIOutputStream *aOutputStream)
1999-01-07 04:02:32 +03:00
{
return NS_OK;
}
NS_IMETHODIMP InsertTextTxn::GetUndoString(nsString *aString)
1999-01-07 04:02:32 +03:00
{
if (nsnull!=aString)
{
*aString="Remove Text: ";
*aString += mStringToInsert;
1999-01-07 04:02:32 +03:00
}
return NS_OK;
}
NS_IMETHODIMP InsertTextTxn::GetRedoString(nsString *aString)
1999-01-07 04:02:32 +03:00
{
if (nsnull!=aString)
{
*aString="Insert Text: ";
*aString += mStringToInsert;
1999-01-07 04:02:32 +03:00
}
return NS_OK;
}
/* ============= nsISupports implementation ====================== */
NS_IMETHODIMP
InsertTextTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
if (nsnull == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
if (aIID.Equals(InsertTextTxn::GetCID())) {
*aInstancePtr = (void*)(InsertTextTxn*)this;
NS_ADDREF_THIS();
return NS_OK;
}
return (EditTxn::QueryInterface(aIID, aInstancePtr));
}
/* ============ protected methods ================== */
NS_IMETHODIMP InsertTextTxn::GetData(nsString& aResult)
{
aResult = mStringToInsert;
return NS_OK;
}
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-06-28 17:44:40 +04:00
PRInt32 length = mStringToInsert.Length();
if (aOtherTxn->mOffset==(mOffset+length))
{
result = PR_TRUE;
}
}
}
return result;
}