Back to index

lightning-sunbird  0.9+nobinonly
PlaceholderTxn.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-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "PlaceholderTxn.h"
00040 #include "nsEditor.h"
00041 #include "IMETextTxn.h"
00042 #include "nsAutoPtr.h"
00043 
00044 PlaceholderTxn::PlaceholderTxn() :  EditAggregateTxn(), 
00045                                     mAbsorb(PR_TRUE), 
00046                                     mForwarding(nsnull),
00047                                     mIMETextTxn(nsnull),
00048                                     mCommitted(PR_FALSE),
00049                                     mStartSel(nsnull),
00050                                     mEndSel(),
00051                                     mEditor(nsnull)
00052 {
00053 }
00054 
00055 
00056 PlaceholderTxn::~PlaceholderTxn()
00057 {
00058   delete mStartSel;
00059 }
00060 
00061 NS_IMPL_ADDREF_INHERITED(PlaceholderTxn, EditAggregateTxn)
00062 NS_IMPL_RELEASE_INHERITED(PlaceholderTxn, EditAggregateTxn)
00063 
00064 //NS_IMPL_QUERY_INTERFACE_INHERITED1(Class, Super, AdditionalInterface)
00065 NS_IMETHODIMP PlaceholderTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00066 {
00067   if (!aInstancePtr) return NS_ERROR_NULL_POINTER;
00068  
00069   if (aIID.Equals(NS_GET_IID(nsIAbsorbingTransaction))) {
00070     *aInstancePtr = (nsISupports*)(nsIAbsorbingTransaction*)(this);
00071     NS_ADDREF_THIS();
00072     return NS_OK;
00073   }
00074   if (aIID.Equals(NS_GET_IID(nsISupportsWeakReference))) {
00075     *aInstancePtr = (nsISupports*)(nsISupportsWeakReference*) this;
00076     NS_ADDREF_THIS();
00077     return NS_OK;
00078   }
00079   return EditAggregateTxn::QueryInterface(aIID, aInstancePtr);
00080 }
00081 
00082 NS_IMETHODIMP PlaceholderTxn::Init(nsIAtom *aName, nsSelectionState *aSelState, nsIEditor *aEditor)
00083 {
00084   if (!aEditor || !aSelState) return NS_ERROR_NULL_POINTER;
00085 
00086   mName = aName;
00087   mStartSel = aSelState;
00088   mEditor = aEditor;
00089   return NS_OK;
00090 }
00091 
00092 NS_IMETHODIMP PlaceholderTxn::DoTransaction(void)
00093 {
00094   return NS_OK;
00095 }
00096 
00097 NS_IMETHODIMP PlaceholderTxn::UndoTransaction(void)
00098 {
00099   // undo txns
00100   nsresult res = EditAggregateTxn::UndoTransaction();
00101   if (NS_FAILED(res)) return res;
00102   
00103   if (!mStartSel) return NS_ERROR_NULL_POINTER;
00104 
00105   // now restore selection
00106   nsCOMPtr<nsISelection> selection;
00107   res = mEditor->GetSelection(getter_AddRefs(selection));
00108   if (NS_FAILED(res)) return res;
00109   if (!selection) return NS_ERROR_NULL_POINTER;
00110   return mStartSel->RestoreSelection(selection);
00111 }
00112 
00113 
00114 NS_IMETHODIMP PlaceholderTxn::RedoTransaction(void)
00115 {
00116   // redo txns
00117   nsresult res = EditAggregateTxn::RedoTransaction();
00118   if (NS_FAILED(res)) return res;
00119   
00120   // now restore selection
00121   nsCOMPtr<nsISelection> selection;
00122   res = mEditor->GetSelection(getter_AddRefs(selection));
00123   if (NS_FAILED(res)) return res;
00124   if (!selection) return NS_ERROR_NULL_POINTER;
00125   return mEndSel.RestoreSelection(selection);
00126 }
00127 
00128 
00129 NS_IMETHODIMP PlaceholderTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00130 {
00131   if (!aDidMerge || !aTransaction) return NS_ERROR_NULL_POINTER;
00132 
00133   // set out param default value
00134   *aDidMerge=PR_FALSE;
00135     
00136   if (mForwarding) 
00137   {
00138     NS_NOTREACHED("tried to merge into a placeholder that was in forwarding mode!");
00139     return NS_ERROR_FAILURE;
00140   }
00141 
00142   // check to see if aTransaction is one of the editor's
00143   // private transactions. If not, we want to avoid merging
00144   // the foreign transaction into our placeholder since we
00145   // don't know what it does.
00146 
00147   nsCOMPtr<nsPIEditorTransaction> pTxn = do_QueryInterface(aTransaction);
00148   if (!pTxn) return NS_OK; // it's foreign so just bail!
00149 
00150   EditTxn *editTxn = (EditTxn*)aTransaction;  //XXX: hack, not safe!  need nsIEditTransaction!
00151   // determine if this incoming txn is a placeholder txn
00152   nsCOMPtr<nsIAbsorbingTransaction> plcTxn;// = do_QueryInterface(editTxn);
00153   // cant do_QueryInterface() above due to our broken transaction interfaces.
00154   // instead have to brute it below. ugh. 
00155   editTxn->QueryInterface(NS_GET_IID(nsIAbsorbingTransaction), getter_AddRefs(plcTxn));
00156 
00157   // we are absorbing all txn's if mAbsorb is lit.
00158   if (mAbsorb)
00159   { 
00160     nsRefPtr<IMETextTxn> otherTxn;
00161     if (NS_SUCCEEDED(aTransaction->QueryInterface(IMETextTxn::GetCID(), getter_AddRefs(otherTxn))) && otherTxn)
00162     {
00163       // special handling for IMETextTxn's: they need to merge with any previous
00164       // IMETextTxn in this placeholder, if possible.
00165       if (!mIMETextTxn) 
00166       {
00167         // this is the first IME txn in the placeholder
00168         mIMETextTxn =otherTxn;
00169         AppendChild(editTxn);
00170       }
00171       else  
00172       {
00173         PRBool didMerge;
00174         mIMETextTxn->Merge(otherTxn, &didMerge);
00175         if (!didMerge)
00176         {
00177           // it wouldn't merge.  Earlier IME txn is already commited and will 
00178           // not absorb further IME txns.  So just stack this one after it
00179           // and remember it as a candidate for further merges.
00180           mIMETextTxn =otherTxn;
00181           AppendChild(editTxn);
00182         }
00183       }
00184     }
00185     else if (!plcTxn)  // see bug 171243: just drop incoming placeholders on the floor.
00186     {                  // their children will be swallowed by this preexisting one.
00187       AppendChild(editTxn);
00188     }
00189     *aDidMerge = PR_TRUE;
00190 //  RememberEndingSelection();
00191 //  efficiency hack: no need to remember selection here, as we haven't yet 
00192 //  finished the inital batch and we know we will be told when the batch ends.
00193 //  we can remeber the selection then.
00194   }
00195   else
00196   { // merge typing or IME or deletion transactions if the selection matches
00197     if (((mName.get() == nsEditor::gTypingTxnName) ||
00198          (mName.get() == nsEditor::gIMETxnName)    ||
00199          (mName.get() == nsEditor::gDeleteTxnName)) 
00200          && !mCommitted ) 
00201     {
00202       nsCOMPtr<nsIAbsorbingTransaction> plcTxn;// = do_QueryInterface(editTxn);
00203       // can't do_QueryInterface() above due to our broken transaction interfaces.
00204       // instead have to brute it below. ugh. 
00205       editTxn->QueryInterface(NS_GET_IID(nsIAbsorbingTransaction), getter_AddRefs(plcTxn));
00206       if (plcTxn)
00207       {
00208         nsCOMPtr<nsIAtom> atom;
00209         plcTxn->GetTxnName(getter_AddRefs(atom));
00210         if (atom && (atom == mName))
00211         {
00212           // check if start selection of next placeholder matches
00213           // end selection of this placeholder
00214           PRBool isSame;
00215           plcTxn->StartSelectionEquals(&mEndSel, &isSame);
00216           if (isSame)
00217           {
00218             mAbsorb = PR_TRUE;  // we need to start absorbing again
00219             plcTxn->ForwardEndBatchTo(this);
00220             // AppendChild(editTxn);
00221             // see bug 171243: we don't need to merge placeholders
00222             // into placeholders.  We just reactivate merging in the pre-existing
00223             // placeholder and drop the new one on the floor.  The EndPlaceHolderBatch()
00224             // call on the new placeholder will be forwarded to this older one.
00225             RememberEndingSelection();
00226             *aDidMerge = PR_TRUE;
00227           }
00228         }
00229       }
00230     }
00231   }
00232   return NS_OK;
00233 }
00234 
00235 NS_IMETHODIMP PlaceholderTxn::GetTxnDescription(nsAString& aString)
00236 {
00237   aString.AssignLiteral("PlaceholderTxn: ");
00238 
00239   if (mName)
00240   {
00241     nsAutoString name;
00242     mName->ToString(name);
00243     aString += name;
00244   }
00245 
00246   return NS_OK;
00247 }
00248 
00249 NS_IMETHODIMP PlaceholderTxn::GetTxnName(nsIAtom **aName)
00250 {
00251   return GetName(aName);
00252 }
00253 
00254 NS_IMETHODIMP PlaceholderTxn::StartSelectionEquals(nsSelectionState *aSelState, PRBool *aResult)
00255 {
00256   // determine if starting selection matches the given selection state.
00257   // note that we only care about collapsed selections.
00258   if (!aResult || !aSelState) return NS_ERROR_NULL_POINTER;
00259   if (!mStartSel->IsCollapsed() || !aSelState->IsCollapsed())
00260   {
00261     *aResult = PR_FALSE;
00262     return NS_OK;
00263   }
00264   *aResult = mStartSel->IsEqual(aSelState);
00265   return NS_OK;
00266 }
00267 
00268 NS_IMETHODIMP PlaceholderTxn::EndPlaceHolderBatch()
00269 {
00270   mAbsorb = PR_FALSE;
00271   
00272   if (mForwarding) 
00273   {
00274     nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryReferent(mForwarding);
00275     if (plcTxn) plcTxn->EndPlaceHolderBatch();
00276   }
00277   
00278   // remember our selection state.
00279   return RememberEndingSelection();
00280 }
00281 
00282 NS_IMETHODIMP PlaceholderTxn::ForwardEndBatchTo(nsIAbsorbingTransaction *aForwardingAddress)
00283 {   
00284   mForwarding = do_GetWeakReference(aForwardingAddress);
00285   return NS_OK;
00286 }
00287 
00288 NS_IMETHODIMP PlaceholderTxn::Commit()
00289 {
00290   mCommitted = PR_TRUE;
00291   return NS_OK;
00292 }
00293 
00294 NS_IMETHODIMP PlaceholderTxn::RememberEndingSelection()
00295 {
00296   nsCOMPtr<nsISelection> selection;
00297   nsresult res = mEditor->GetSelection(getter_AddRefs(selection));
00298   if (NS_FAILED(res)) return res;
00299   if (!selection) return NS_ERROR_NULL_POINTER;
00300   return mEndSel.SaveSelection(selection);
00301 }
00302