Back to index

lightning-sunbird  0.9+nobinonly
nsEditorUtils.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 
00039 #include "nsEditorUtils.h"
00040 #include "nsIDOMDocument.h"
00041 #include "nsIDOMRange.h"
00042 #include "nsIContent.h"
00043 #include "nsLayoutCID.h"
00044 
00045 // hooks
00046 #include "nsIClipboardDragDropHooks.h"
00047 #include "nsIClipboardDragDropHookList.h"
00048 #include "nsISimpleEnumerator.h"
00049 #include "nsIDocShell.h"
00050 #include "nsIDocument.h"
00051 #include "nsIInterfaceRequestorUtils.h"
00052 
00053 
00054 /******************************************************************************
00055  * nsAutoSelectionReset
00056  *****************************************************************************/
00057 
00058 nsAutoSelectionReset::nsAutoSelectionReset(nsISelection *aSel, nsEditor *aEd) : 
00059 mSel(nsnull)
00060 ,mEd(nsnull)
00061 { 
00062   if (!aSel || !aEd) return;    // not much we can do, bail.
00063   if (aEd->ArePreservingSelection()) return;   // we already have initted mSavedSel, so this must be nested call.
00064   mSel = do_QueryInterface(aSel);
00065   mEd = aEd;
00066   if (mSel)
00067   {
00068     mEd->PreserveSelectionAcrossActions(mSel);
00069   }
00070 }
00071 
00072 nsAutoSelectionReset::~nsAutoSelectionReset()
00073 {
00074   if (mSel && mEd->ArePreservingSelection())   // mSel will be null if this was nested call
00075   {
00076     mEd->RestorePreservedSelection(mSel);
00077   }
00078 }
00079 
00080 void
00081 nsAutoSelectionReset::Abort()
00082 {
00083   mEd->StopPreservingSelection();
00084 }
00085 
00086 
00087 /******************************************************************************
00088  * some helper classes for iterating the dom tree
00089  *****************************************************************************/
00090 
00091 nsDOMIterator::nsDOMIterator() :
00092 mIter(nsnull)
00093 {
00094 }
00095     
00096 nsDOMIterator::~nsDOMIterator()
00097 {
00098 }
00099     
00100 nsresult
00101 nsDOMIterator::Init(nsIDOMRange* aRange)
00102 {
00103   nsresult res;
00104   mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
00105   if (NS_FAILED(res)) return res;
00106   if (!mIter) return NS_ERROR_FAILURE;
00107   return mIter->Init(aRange);
00108 }
00109 
00110 nsresult
00111 nsDOMIterator::Init(nsIDOMNode* aNode)
00112 {
00113   nsresult res;
00114   mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
00115   if (NS_FAILED(res)) return res;
00116   if (!mIter) return NS_ERROR_FAILURE;
00117   nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
00118   return mIter->Init(content);
00119 }
00120 
00121 void
00122 nsDOMIterator::ForEach(nsDomIterFunctor& functor) const
00123 {
00124   nsCOMPtr<nsIDOMNode> node;
00125   
00126   // iterate through dom
00127   while (!mIter->IsDone())
00128   {
00129     node = do_QueryInterface(mIter->GetCurrentNode());
00130     if (!node)
00131       return;
00132 
00133     functor(node);
00134     mIter->Next();
00135   }
00136 }
00137 
00138 nsresult
00139 nsDOMIterator::AppendList(nsBoolDomIterFunctor& functor,
00140                           nsCOMArray<nsIDOMNode>& arrayOfNodes) const
00141 {
00142   nsCOMPtr<nsIDOMNode> node;
00143   
00144   // iterate through dom and build list
00145   while (!mIter->IsDone())
00146   {
00147     node = do_QueryInterface(mIter->GetCurrentNode());
00148     if (!node)
00149       return NS_ERROR_NULL_POINTER;
00150 
00151     if (functor(node))
00152     {
00153       arrayOfNodes.AppendObject(node);
00154     }
00155     mIter->Next();
00156   }
00157   return NS_OK;
00158 }
00159 
00160 nsDOMSubtreeIterator::nsDOMSubtreeIterator()
00161 {
00162 }
00163     
00164 nsDOMSubtreeIterator::~nsDOMSubtreeIterator()
00165 {
00166 }
00167     
00168 nsresult
00169 nsDOMSubtreeIterator::Init(nsIDOMRange* aRange)
00170 {
00171   nsresult res;
00172   mIter = do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
00173   if (NS_FAILED(res)) return res;
00174   if (!mIter) return NS_ERROR_FAILURE;
00175   return mIter->Init(aRange);
00176 }
00177 
00178 nsresult
00179 nsDOMSubtreeIterator::Init(nsIDOMNode* aNode)
00180 {
00181   nsresult res;
00182   mIter = do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
00183   if (NS_FAILED(res)) return res;
00184   if (!mIter) return NS_ERROR_FAILURE;
00185   nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
00186   return mIter->Init(content);
00187 }
00188 
00189 /******************************************************************************
00190  * some general purpose editor utils
00191  *****************************************************************************/
00192 
00193 PRBool 
00194 nsEditorUtils::IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 *aOffset) 
00195 {
00196   if (!aNode && !aParent) return PR_FALSE;
00197   if (aNode == aParent) return PR_FALSE;
00198   
00199   nsCOMPtr<nsIDOMNode> parent, node = do_QueryInterface(aNode);
00200   nsresult res;
00201   
00202   do
00203   {
00204     res = node->GetParentNode(getter_AddRefs(parent));
00205     if (NS_FAILED(res)) return PR_FALSE;
00206     if (parent == aParent) 
00207     {
00208       if (aOffset)
00209       {
00210         nsCOMPtr<nsIContent> pCon(do_QueryInterface(parent));
00211         nsCOMPtr<nsIContent> cCon(do_QueryInterface(node));
00212         if (pCon)
00213         {
00214           *aOffset = pCon->IndexOf(cCon);
00215         }
00216       }
00217       return PR_TRUE;
00218     }
00219     node = parent;
00220   } while (parent);
00221   
00222   return PR_FALSE;
00223 }
00224 
00225 PRBool
00226 nsEditorUtils::IsLeafNode(nsIDOMNode *aNode)
00227 {
00228   PRBool hasChildren = PR_FALSE;
00229   if (aNode)
00230     aNode->HasChildNodes(&hasChildren);
00231   return !hasChildren;
00232 }
00233 
00234 /******************************************************************************
00235  * utility methods for drag/drop/copy/paste hooks
00236  *****************************************************************************/
00237 
00238 nsresult
00239 nsEditorHookUtils::GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
00240                                                  nsISimpleEnumerator **aResult)
00241 {
00242   nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
00243   if (!doc) return NS_ERROR_FAILURE;
00244 
00245   nsCOMPtr<nsISupports> container = doc->GetContainer();
00246   nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(container);
00247   nsCOMPtr<nsIClipboardDragDropHookList> hookObj = do_GetInterface(docShell);
00248   if (!hookObj) return NS_ERROR_FAILURE;
00249 
00250   return hookObj->GetHookEnumerator(aResult);
00251 }
00252 
00253 PRBool
00254 nsEditorHookUtils::DoAllowDragHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDragEvent)
00255 {
00256   nsCOMPtr<nsISimpleEnumerator> enumerator;
00257   GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
00258   if (!enumerator)
00259     return PR_TRUE;
00260 
00261   PRBool hasMoreHooks = PR_FALSE;
00262   while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks)) && hasMoreHooks)
00263   {
00264     nsCOMPtr<nsISupports> isupp;
00265     if (NS_FAILED(enumerator->GetNext(getter_AddRefs(isupp))))
00266       break;
00267 
00268     nsCOMPtr<nsIClipboardDragDropHooks> override = do_QueryInterface(isupp);
00269     if (override)
00270     {
00271       PRBool canDrag = PR_TRUE;
00272       nsresult hookres = override->AllowStartDrag(aDragEvent, &canDrag);
00273       NS_ASSERTION(NS_SUCCEEDED(hookres), "hook failure in AllowStartDrag");
00274       if (!canDrag)
00275         return PR_FALSE;
00276     }
00277   }
00278 
00279   return PR_TRUE;
00280 }
00281 
00282 PRBool
00283 nsEditorHookUtils::DoDragHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
00284                               nsITransferable *aTrans)
00285 {
00286   nsCOMPtr<nsISimpleEnumerator> enumerator;
00287   GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
00288   if (!enumerator)
00289     return PR_TRUE;
00290 
00291   PRBool hasMoreHooks = PR_FALSE;
00292   while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks)) && hasMoreHooks)
00293   {
00294     nsCOMPtr<nsISupports> isupp;
00295     if (NS_FAILED(enumerator->GetNext(getter_AddRefs(isupp))))
00296       break;
00297 
00298     nsCOMPtr<nsIClipboardDragDropHooks> override = do_QueryInterface(isupp);
00299     if (override)
00300     {
00301       PRBool canInvokeDrag = PR_TRUE;
00302       nsresult hookResult = override->OnCopyOrDrag(aEvent, aTrans, &canInvokeDrag);
00303       NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in OnCopyOrDrag");
00304       if (!canInvokeDrag)
00305         return PR_FALSE;
00306     }
00307   }
00308 
00309   return PR_TRUE;
00310 }
00311 
00312 PRBool
00313 nsEditorHookUtils::DoAllowDropHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,   
00314                                    nsIDragSession *aSession)
00315 {
00316   nsCOMPtr<nsISimpleEnumerator> enumerator;
00317   GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
00318   if (!enumerator)
00319     return PR_TRUE;
00320 
00321   PRBool hasMoreHooks = PR_FALSE;
00322   while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks)) && hasMoreHooks)
00323   {
00324     nsCOMPtr<nsISupports> isupp;
00325     if (NS_FAILED(enumerator->GetNext(getter_AddRefs(isupp))))
00326       break;
00327 
00328     nsCOMPtr<nsIClipboardDragDropHooks> override = do_QueryInterface(isupp);
00329     if (override)
00330     {
00331       PRBool allowDrop = PR_TRUE;
00332       nsresult hookResult = override->AllowDrop(aEvent, aSession, &allowDrop);
00333       NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in AllowDrop");
00334       if (!allowDrop)
00335         return PR_FALSE;
00336     }
00337   }
00338 
00339   return PR_TRUE;
00340 }
00341 
00342 PRBool
00343 nsEditorHookUtils::DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDropEvent,  
00344                                    nsITransferable *aTrans)
00345 {
00346   nsCOMPtr<nsISimpleEnumerator> enumerator;
00347   GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
00348   if (!enumerator)
00349     return PR_TRUE;
00350 
00351   PRBool hasMoreHooks = PR_FALSE;
00352   while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks)) && hasMoreHooks)
00353   {
00354     nsCOMPtr<nsISupports> isupp;
00355     if (NS_FAILED(enumerator->GetNext(getter_AddRefs(isupp))))
00356       break;
00357 
00358     nsCOMPtr<nsIClipboardDragDropHooks> override = do_QueryInterface(isupp);
00359     if (override)
00360     {
00361       PRBool doInsert = PR_TRUE;
00362       nsresult hookResult = override->OnPasteOrDrop(aDropEvent, aTrans, &doInsert);
00363       NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in OnPasteOrDrop");
00364       if (!doInsert)
00365         return PR_FALSE;
00366     }
00367   }
00368 
00369   return PR_TRUE;
00370 }