michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #ifndef nsEditorUtils_h__ michael@0: #define nsEditorUtils_h__ michael@0: michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsDebug.h" michael@0: #include "nsEditor.h" michael@0: #include "nsIDOMNode.h" michael@0: #include "nsIEditor.h" michael@0: #include "nscore.h" michael@0: michael@0: class nsIAtom; michael@0: class nsIContentIterator; michael@0: class nsIDOMDocument; michael@0: class nsIDOMRange; michael@0: class nsISelection; michael@0: template class nsCOMArray; michael@0: michael@0: /*************************************************************************** michael@0: * stack based helper class for batching a collection of txns inside a michael@0: * placeholder txn. michael@0: */ michael@0: class MOZ_STACK_CLASS nsAutoPlaceHolderBatch michael@0: { michael@0: private: michael@0: nsCOMPtr mEd; michael@0: public: michael@0: nsAutoPlaceHolderBatch( nsIEditor *aEd, nsIAtom *atom) : mEd(do_QueryInterface(aEd)) michael@0: { if (mEd) mEd->BeginPlaceHolderTransaction(atom); } michael@0: ~nsAutoPlaceHolderBatch() { if (mEd) mEd->EndPlaceHolderTransaction(); } michael@0: }; michael@0: michael@0: /*************************************************************************** michael@0: * stack based helper class for batching a collection of txns. michael@0: * Note: I changed this to use placeholder batching so that we get michael@0: * proper selection save/restore across undo/redo. michael@0: */ michael@0: class MOZ_STACK_CLASS nsAutoEditBatch : public nsAutoPlaceHolderBatch michael@0: { michael@0: public: michael@0: nsAutoEditBatch( nsIEditor *aEd) : nsAutoPlaceHolderBatch(aEd,nullptr) {} michael@0: ~nsAutoEditBatch() {} michael@0: }; michael@0: michael@0: /*************************************************************************** michael@0: * stack based helper class for saving/restoring selection. Note that this michael@0: * assumes that the nodes involved are still around afterwards! michael@0: */ michael@0: class MOZ_STACK_CLASS nsAutoSelectionReset michael@0: { michael@0: private: michael@0: /** ref-counted reference to the selection that we are supposed to restore */ michael@0: nsRefPtr mSel; michael@0: nsEditor *mEd; // non-owning ref to nsEditor michael@0: michael@0: public: michael@0: /** constructor responsible for remembering all state needed to restore aSel */ michael@0: nsAutoSelectionReset(mozilla::dom::Selection* aSel, nsEditor* aEd); michael@0: michael@0: /** destructor restores mSel to its former state */ michael@0: ~nsAutoSelectionReset(); michael@0: michael@0: /** Abort: cancel selection saver */ michael@0: void Abort(); michael@0: }; michael@0: michael@0: /*************************************************************************** michael@0: * stack based helper class for StartOperation()/EndOperation() sandwich michael@0: */ michael@0: class MOZ_STACK_CLASS nsAutoRules michael@0: { michael@0: public: michael@0: michael@0: nsAutoRules(nsEditor *ed, EditAction action, michael@0: nsIEditor::EDirection aDirection) : michael@0: mEd(ed), mDoNothing(false) michael@0: { michael@0: if (mEd && !mEd->mAction) // mAction will already be set if this is nested call michael@0: { michael@0: mEd->StartOperation(action, aDirection); michael@0: } michael@0: else mDoNothing = true; // nested calls will end up here michael@0: } michael@0: ~nsAutoRules() michael@0: { michael@0: if (mEd && !mDoNothing) michael@0: { michael@0: mEd->EndOperation(); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: nsEditor *mEd; michael@0: bool mDoNothing; michael@0: }; michael@0: michael@0: michael@0: /*************************************************************************** michael@0: * stack based helper class for turning off active selection adjustment michael@0: * by low level transactions michael@0: */ michael@0: class MOZ_STACK_CLASS nsAutoTxnsConserveSelection michael@0: { michael@0: public: michael@0: michael@0: nsAutoTxnsConserveSelection(nsEditor *ed) : mEd(ed), mOldState(true) michael@0: { michael@0: if (mEd) michael@0: { michael@0: mOldState = mEd->GetShouldTxnSetSelection(); michael@0: mEd->SetShouldTxnSetSelection(false); michael@0: } michael@0: } michael@0: michael@0: ~nsAutoTxnsConserveSelection() michael@0: { michael@0: if (mEd) michael@0: { michael@0: mEd->SetShouldTxnSetSelection(mOldState); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: nsEditor *mEd; michael@0: bool mOldState; michael@0: }; michael@0: michael@0: /*************************************************************************** michael@0: * stack based helper class for batching reflow and paint requests. michael@0: */ michael@0: class MOZ_STACK_CLASS nsAutoUpdateViewBatch michael@0: { michael@0: public: michael@0: michael@0: nsAutoUpdateViewBatch(nsEditor *ed) : mEd(ed) michael@0: { michael@0: NS_ASSERTION(mEd, "null mEd pointer!"); michael@0: michael@0: if (mEd) michael@0: mEd->BeginUpdateViewBatch(); michael@0: } michael@0: michael@0: ~nsAutoUpdateViewBatch() michael@0: { michael@0: if (mEd) michael@0: mEd->EndUpdateViewBatch(); michael@0: } michael@0: michael@0: protected: michael@0: nsEditor *mEd; michael@0: }; michael@0: michael@0: /****************************************************************************** michael@0: * some helper classes for iterating the dom tree michael@0: *****************************************************************************/ michael@0: michael@0: class nsBoolDomIterFunctor michael@0: { michael@0: public: michael@0: virtual bool operator()(nsIDOMNode* aNode)=0; michael@0: }; michael@0: michael@0: class MOZ_STACK_CLASS nsDOMIterator michael@0: { michael@0: public: michael@0: nsDOMIterator(); michael@0: virtual ~nsDOMIterator(); michael@0: michael@0: nsresult Init(nsIDOMRange* aRange); michael@0: nsresult Init(nsIDOMNode* aNode); michael@0: nsresult AppendList(nsBoolDomIterFunctor& functor, michael@0: nsCOMArray& arrayOfNodes) const; michael@0: protected: michael@0: nsCOMPtr mIter; michael@0: }; michael@0: michael@0: class MOZ_STACK_CLASS nsDOMSubtreeIterator : public nsDOMIterator michael@0: { michael@0: public: michael@0: nsDOMSubtreeIterator(); michael@0: virtual ~nsDOMSubtreeIterator(); michael@0: michael@0: nsresult Init(nsIDOMRange* aRange); michael@0: }; michael@0: michael@0: class nsTrivialFunctor : public nsBoolDomIterFunctor michael@0: { michael@0: public: michael@0: virtual bool operator()(nsIDOMNode* aNode) // used to build list of all nodes iterator covers michael@0: { michael@0: return true; michael@0: } michael@0: }; michael@0: michael@0: michael@0: /****************************************************************************** michael@0: * general dom point utility struct michael@0: *****************************************************************************/ michael@0: struct MOZ_STACK_CLASS DOMPoint michael@0: { michael@0: nsCOMPtr node; michael@0: int32_t offset; michael@0: michael@0: DOMPoint() : node(0),offset(0) {} michael@0: DOMPoint(nsIDOMNode *aNode, int32_t aOffset) : michael@0: node(aNode),offset(aOffset) {} michael@0: void SetPoint(nsIDOMNode *aNode, int32_t aOffset) michael@0: { michael@0: node = aNode; offset = aOffset; michael@0: } michael@0: void GetPoint(nsCOMPtr &aNode, int32_t &aOffset) michael@0: { michael@0: aNode = node; aOffset = offset; michael@0: } michael@0: }; michael@0: michael@0: michael@0: class nsEditorUtils michael@0: { michael@0: public: michael@0: static bool IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t *aOffset = 0); michael@0: static bool IsLeafNode(nsIDOMNode *aNode); michael@0: }; michael@0: michael@0: michael@0: class nsIDOMEvent; michael@0: class nsISimpleEnumerator; michael@0: class nsITransferable; michael@0: michael@0: class nsEditorHookUtils michael@0: { michael@0: public: michael@0: static bool DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent, michael@0: nsITransferable *aTrans); michael@0: private: michael@0: static nsresult GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc, michael@0: nsISimpleEnumerator **aEnumerator); michael@0: }; michael@0: michael@0: #endif // nsEditorUtils_h__