Back to index

lightning-sunbird  0.9+nobinonly
DeleteTextTxn.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "DeleteTextTxn.h"
00039 #include "nsIDOMCharacterData.h"
00040 #include "nsISelection.h"
00041 #include "nsSelectionState.h"
00042 
00043 #ifdef NS_DEBUG
00044 static PRBool gNoisy = PR_FALSE;
00045 #endif
00046 
00047 DeleteTextTxn::DeleteTextTxn()
00048 : EditTxn()
00049 ,mEditor(nsnull)
00050 ,mElement()
00051 ,mOffset(0)
00052 ,mNumCharsToDelete(0)
00053 ,mRangeUpdater(nsnull)
00054 {
00055 }
00056 
00057 DeleteTextTxn::~DeleteTextTxn()
00058 {
00059 }
00060 
00061 NS_IMETHODIMP DeleteTextTxn::Init(nsIEditor *aEditor,
00062                                   nsIDOMCharacterData *aElement,
00063                                   PRUint32 aOffset,
00064                                   PRUint32 aNumCharsToDelete,
00065                                   nsRangeUpdater *aRangeUpdater)
00066 {
00067   NS_ASSERTION(aEditor&&aElement, "bad arg");
00068   if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
00069 
00070   mEditor = aEditor;
00071   mElement = do_QueryInterface(aElement);
00072   mOffset = aOffset;
00073   mNumCharsToDelete = aNumCharsToDelete;
00074   NS_ASSERTION(0!=aNumCharsToDelete, "bad arg, numCharsToDelete");
00075   PRUint32 count;
00076   aElement->GetLength(&count);
00077   NS_ASSERTION(count>=aNumCharsToDelete, "bad arg, numCharsToDelete.  Not enough characters in node");
00078   NS_ASSERTION(count>=aOffset+aNumCharsToDelete, "bad arg, numCharsToDelete.  Not enough characters in node");
00079   mDeletedText.Truncate();
00080   mRangeUpdater = aRangeUpdater;
00081   return NS_OK;
00082 }
00083 
00084 NS_IMETHODIMP DeleteTextTxn::DoTransaction(void)
00085 {
00086 #ifdef NS_DEBUG
00087   if (gNoisy) { printf("Do Delete Text\n"); }
00088 #endif
00089 
00090   NS_ASSERTION(mEditor && mElement, "bad state");
00091   if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
00092   // get the text that we're about to delete
00093   nsresult result = mElement->SubstringData(mOffset, mNumCharsToDelete, mDeletedText);
00094   NS_ASSERTION(NS_SUCCEEDED(result), "could not get text to delete.");
00095   result = mElement->DeleteData(mOffset, mNumCharsToDelete);
00096   if (NS_FAILED(result)) return result;
00097 
00098   if (mRangeUpdater) 
00099     mRangeUpdater->SelAdjDeleteText(mElement, mOffset, mNumCharsToDelete);
00100 
00101   // only set selection to deletion point if editor gives permission
00102   PRBool bAdjustSelection;
00103   mEditor->ShouldTxnSetSelection(&bAdjustSelection);
00104   if (bAdjustSelection)
00105   {
00106     nsCOMPtr<nsISelection> selection;
00107     result = mEditor->GetSelection(getter_AddRefs(selection));
00108     if (NS_FAILED(result)) return result;
00109     if (!selection) return NS_ERROR_NULL_POINTER;
00110     result = selection->Collapse(mElement, mOffset);
00111     NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of deletetext.");
00112   }
00113   else
00114   {
00115     // do nothing - dom range gravity will adjust selection
00116   }
00117   return result;
00118 }
00119 
00120 //XXX: we may want to store the selection state and restore it properly
00121 //     was it an insertion point or an extended selection?
00122 NS_IMETHODIMP DeleteTextTxn::UndoTransaction(void)
00123 {
00124 #ifdef NS_DEBUG
00125   if (gNoisy) { printf("Undo Delete Text\n"); }
00126 #endif
00127 
00128   NS_ASSERTION(mEditor && mElement, "bad state");
00129   if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
00130 
00131   return mElement->InsertData(mOffset, mDeletedText);
00132 }
00133 
00134 NS_IMETHODIMP DeleteTextTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00135 {
00136   if (aDidMerge)
00137     *aDidMerge = PR_FALSE;
00138   return NS_OK;
00139 }
00140 
00141 NS_IMETHODIMP DeleteTextTxn::GetTxnDescription(nsAString& aString)
00142 {
00143   aString.AssignLiteral("DeleteTextTxn: ");
00144   aString += mDeletedText;
00145   return NS_OK;
00146 }