Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | Friends
InsertTextTxn Class Reference

A transaction that inserts text into a content node. More...

#include <InsertTextTxn.h>

Inheritance diagram for InsertTextTxn:
Inheritance graph
[legend]
Collaboration diagram for InsertTextTxn:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual ~InsertTextTxn ()
NS_IMETHOD Init (nsIDOMCharacterData *aElement, PRUint32 aOffset, const nsAString &aString, nsIEditor *aEditor)
 initialize the transaction
NS_IMETHOD DoTransaction (void)
NS_IMETHOD UndoTransaction (void)
NS_IMETHOD Merge (nsITransaction *aTransaction, PRBool *aDidMerge)
NS_IMETHOD GetTxnDescription (nsAString &aTxnDescription)
NS_IMETHOD QueryInterface (const nsIID &aIID, void **aInstancePtr)
NS_IMETHOD GetData (nsString &aResult)
 return the string data associated with this transaction
NS_IMETHOD RedoTransaction (void)
NS_IMETHOD GetIsTransient (PRBool *aIsTransient)
void doTransaction ()
 Executes the transaction.
void undoTransaction ()
 Restores the state to what it was before the transaction was executed.
void redoTransaction ()
 Executes the transaction again.
boolean merge (in nsITransaction aTransaction)
 Attempts to merge a transaction into "this" transaction.

Static Public Member Functions

static const nsIIDGetCID ()
static nsresult ClassInit ()
 must be called before any InsertTextTxn is instantiated
static nsresult ClassShutdown ()
 must be called once we are guaranteed all InsertTextTxn have completed

Public Attributes

readonly attribute boolean isTransient
 The transaction's transient state.
readonly attribute DOMString txnDescription

Static Public Attributes

static nsIAtomgInsertTextTxnName
 used to name aggregate transactions that consist only of a single InsertTextTxn, or a DeleteSelection followed by an InsertTextTxn.

Protected Member Functions

virtual PRBool IsSequentialInsert (InsertTextTxn *aOtherTxn)
 return PR_TRUE if aOtherTxn immediately follows this txn

Protected Attributes

nsCOMPtr< nsIDOMCharacterDatamElement
 the text element to operate upon
PRUint32 mOffset
 the offset into mElement where the insertion is to take place
nsString mStringToInsert
 the text to insert into mElement at mOffset
nsIEditormEditor
 the editor, which we'll need to get the selection

Private Member Functions

 InsertTextTxn ()

Friends

class TransactionFactory
class nsDerivedSafe< InsertTextTxn >

Detailed Description

A transaction that inserts text into a content node.

Definition at line 56 of file InsertTextTxn.h.


Constructor & Destructor Documentation

Definition at line 67 of file InsertTextTxn.cpp.

{
}

Definition at line 62 of file InsertTextTxn.cpp.

  : EditTxn()
{
}

Member Function Documentation

must be called before any InsertTextTxn is instantiated

Definition at line 49 of file InsertTextTxn.cpp.

{
  if (!gInsertTextTxnName)
    gInsertTextTxnName = NS_NewAtom("NS_InsertTextTxn");
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

must be called once we are guaranteed all InsertTextTxn have completed

Definition at line 56 of file InsertTextTxn.cpp.

Here is the caller graph for this function:

Executes the transaction.

Reimplemented from EditTxn.

Definition at line 94 of file InsertTextTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { printf("Do Insert Text element = %p\n", mElement.get()); }
#endif

  NS_ASSERTION(mElement && mEditor, "bad state");
  if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }

  nsresult result = mElement->InsertData(mOffset, mStringToInsert);
  if (NS_FAILED(result)) return result;

  // only set selection to insertion point if editor gives permission
  PRBool bAdjustSelection;
  mEditor->ShouldTxnSetSelection(&bAdjustSelection);
  if (bAdjustSelection)
  {
    nsCOMPtr<nsISelection> selection;
    result = mEditor->GetSelection(getter_AddRefs(selection));
    if (NS_FAILED(result)) return result;
    if (!selection) return NS_ERROR_NULL_POINTER;
    result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
    NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
  }
  else
  {
    // do nothing - dom range gravity will adjust selection
  }

  return result;
}

Here is the call graph for this function:

static const nsIID& InsertTextTxn::GetCID ( ) [inline, static]

Reimplemented from EditTxn.

Definition at line 60 of file InsertTextTxn.h.

{ static const nsIID iid = INSERT_TEXT_TXN_CID; return iid; }

Here is the caller graph for this function:

return the string data associated with this transaction

Definition at line 236 of file InsertTextTxn.cpp.

{
  aResult = mStringToInsert;
  return NS_OK;
}
NS_IMETHODIMP EditTxn::GetIsTransient ( PRBool aIsTransient) [inherited]

Reimplemented in JoinElementTxn, SetDocTitleTxn, and EditAggregateTxn.

Definition at line 66 of file EditTxn.cpp.

{
  if (aIsTransient)
    *aIsTransient = PR_FALSE;
  return NS_OK;
}
NS_IMETHODIMP InsertTextTxn::GetTxnDescription ( nsAString &  aTxnDescription)

Reimplemented from EditTxn.

Definition at line 211 of file InsertTextTxn.cpp.

{
  aString.AssignLiteral("InsertTextTxn: ");
  aString += mStringToInsert;
  return NS_OK;
}
NS_IMETHODIMP InsertTextTxn::Init ( nsIDOMCharacterData aElement,
PRUint32  aOffset,
const nsAString &  aString,
nsIEditor aEditor 
)

initialize the transaction

Parameters:
aElementthe text content node
aOffsetthe location in aElement to do the insertion
aStringthe new text to insert
aPresShellused to get and set the selection

Definition at line 71 of file InsertTextTxn.cpp.

{
#if 0
      nsAutoString text;
      aElement->GetData(text);
      printf("InsertTextTxn: Offset to insert at = %d. Text of the node to insert into:\n", aOffset);
      wprintf(text.get());
      printf("\n");
#endif

  NS_ASSERTION(aElement && aEditor, "bad args");
  if (!aElement || !aEditor) return NS_ERROR_NULL_POINTER;

  mElement = do_QueryInterface(aElement);
  mOffset = aOffset;
  mStringToInsert = aStringToInsert;
  mEditor = aEditor;
  return NS_OK;
}

Here is the call graph for this function:

PRBool InsertTextTxn::IsSequentialInsert ( InsertTextTxn aOtherTxn) [protected, virtual]

return PR_TRUE if aOtherTxn immediately follows this txn

Definition at line 242 of file InsertTextTxn.cpp.

{
  NS_ASSERTION(aOtherTxn, "null param");
  if (aOtherTxn && aOtherTxn->mElement == mElement)
  {
    // here, we need to compare offsets.
    PRInt32 length = mStringToInsert.Length();
    if (aOtherTxn->mOffset == (mOffset + length))
      return PR_TRUE;
  }
  return PR_FALSE;
}

Here is the caller graph for this function:

NS_IMETHODIMP InsertTextTxn::Merge ( nsITransaction aTransaction,
PRBool aDidMerge 
)

Reimplemented from EditTxn.

Definition at line 139 of file InsertTextTxn.cpp.

{
  // set out param default value
  if (aDidMerge)
    *aDidMerge = PR_FALSE;
  nsresult result = NS_OK;
  if (aDidMerge && aTransaction)
  {
    // if aTransaction is a InsertTextTxn, and if the selection hasn't changed, 
    // then absorb it
    InsertTextTxn *otherInsTxn = nsnull;
    aTransaction->QueryInterface(InsertTextTxn::GetCID(), (void **)&otherInsTxn);
    if (otherInsTxn)
    {
      if (IsSequentialInsert(otherInsTxn))
      {
        nsAutoString otherData;
        otherInsTxn->GetData(otherData);
        mStringToInsert += otherData;
        *aDidMerge = PR_TRUE;
#ifdef NS_DEBUG
        if (gNoisy) { printf("InsertTextTxn assimilated %p\n", aTransaction); }
#endif
      }
      NS_RELEASE(otherInsTxn);
    }
    else
    { // the next InsertTextTxn might be inside an aggregate that we have special knowledge of
      EditAggregateTxn *otherTxn = nsnull;
      aTransaction->QueryInterface(EditAggregateTxn::GetCID(), (void **)&otherTxn);
      if (otherTxn)
      {
        nsCOMPtr<nsIAtom> txnName;
        otherTxn->GetName(getter_AddRefs(txnName));
        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
          EditTxn * childTxn;
          otherTxn->GetTxnAt(0, (&childTxn));
          if (childTxn)
          {
            InsertTextTxn * otherInsertTxn = nsnull;
            result = childTxn->QueryInterface(InsertTextTxn::GetCID(), (void**)&otherInsertTxn);
            if (NS_SUCCEEDED(result))
            {
              if (otherInsertTxn)
              {
                if (IsSequentialInsert(otherInsertTxn))
                     {
                       nsAutoString otherData;
                       otherInsertTxn->GetData(otherData);
                       mStringToInsert += otherData;
                       *aDidMerge = PR_TRUE;
#ifdef NS_DEBUG
                       if (gNoisy) { printf("InsertTextTxn assimilated %p\n", aTransaction); }
#endif
                     }
                     NS_RELEASE(otherInsertTxn);
                   }
            }
            
            NS_RELEASE(childTxn);
          }
        }
        NS_RELEASE(otherTxn);
      }
    }
  }
  return result;
}

Here is the call graph for this function:

boolean nsITransaction::merge ( in nsITransaction  aTransaction) [inherited]

Attempts to merge a transaction into "this" transaction.

Both transactions must be in their undo state, doTransaction() methods already called. The transaction manager calls this method to coalesce a new transaction with the transaction on the top of the undo stack. This method returns a boolean value that indicates the merge result. A true value indicates that the transactions were merged successfully, a false value if the merge was not possible or failed. If true, the transaction manager will Release() the new transacton instead of pushing it on the undo stack.

Parameters:
aTransactionthe previously executed transaction to merge.
NS_IMETHODIMP InsertTextTxn::QueryInterface ( const nsIID aIID,
void **  aInstancePtr 
)

Definition at line 221 of file InsertTextTxn.cpp.

{
  if (!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));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Executes the transaction again.

Can only be called on a transaction that was previously undone.

In most cases, the redoTransaction() method will actually call the doTransaction() method to execute the transaction again.

Restores the state to what it was before the transaction was executed.

Reimplemented from EditTxn.

Definition at line 126 of file InsertTextTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { printf("Undo Insert Text element = %p\n", mElement.get()); }
#endif

  NS_ASSERTION(mElement && mEditor, "bad state");
  if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }

  PRUint32 length = mStringToInsert.Length();
  return mElement->DeleteData(mOffset, length);
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class nsDerivedSafe< InsertTextTxn > [friend]

Definition at line 127 of file InsertTextTxn.h.

friend class TransactionFactory [friend]

Definition at line 125 of file InsertTextTxn.h.


Member Data Documentation

used to name aggregate transactions that consist only of a single InsertTextTxn, or a DeleteSelection followed by an InsertTextTxn.

Definition at line 67 of file InsertTextTxn.h.

The transaction's transient state.

This attribute is checked by the transaction manager after the transaction's Execute() method is called. If the transient state is false, a reference to the transaction is held by the transaction manager so that the transactions' undoTransaction() and redoTransaction() methods can be called. If the transient state is true, the transaction manager returns immediately after the transaction's doTransaction() method is called, no references to the transaction are maintained. Transient transactions cannot be undone or redone by the transaction manager.

Definition at line 79 of file nsITransaction.idl.

the editor, which we'll need to get the selection

Definition at line 123 of file InsertTextTxn.h.

the text element to operate upon

Definition at line 114 of file InsertTextTxn.h.

the offset into mElement where the insertion is to take place

Definition at line 117 of file InsertTextTxn.h.

the text to insert into mElement at mOffset

Definition at line 120 of file InsertTextTxn.h.

readonly attribute DOMString nsPIEditorTransaction::txnDescription [inherited]

Definition at line 50 of file nsPIEditorTransaction.idl.


The documentation for this class was generated from the following files: