1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 :
39 : #ifndef nsEditorUtils_h__
40 : #define nsEditorUtils_h__
41 :
42 :
43 : #include "nsCOMPtr.h"
44 : #include "nsIDOMNode.h"
45 : #include "nsISelection.h"
46 : #include "nsIEditor.h"
47 : #include "nsIAtom.h"
48 : #include "nsEditor.h"
49 : #include "nsIContentIterator.h"
50 : #include "nsCOMArray.h"
51 :
52 : class nsPlaintextEditor;
53 :
54 : /***************************************************************************
55 : * stack based helper class for batching a collection of txns inside a
56 : * placeholder txn.
57 : */
58 : class NS_STACK_CLASS nsAutoPlaceHolderBatch
59 : {
60 : private:
61 : nsCOMPtr<nsIEditor> mEd;
62 : public:
63 0 : nsAutoPlaceHolderBatch( nsIEditor *aEd, nsIAtom *atom) : mEd(do_QueryInterface(aEd))
64 0 : { if (mEd) mEd->BeginPlaceHolderTransaction(atom); }
65 0 : ~nsAutoPlaceHolderBatch() { if (mEd) mEd->EndPlaceHolderTransaction(); }
66 : };
67 :
68 : /***************************************************************************
69 : * stack based helper class for batching a collection of txns.
70 : * Note: I changed this to use placeholder batching so that we get
71 : * proper selection save/restore across undo/redo.
72 : */
73 : class nsAutoEditBatch : public nsAutoPlaceHolderBatch
74 : {
75 : public:
76 0 : nsAutoEditBatch( nsIEditor *aEd) : nsAutoPlaceHolderBatch(aEd,nsnull) {}
77 0 : ~nsAutoEditBatch() {}
78 : };
79 :
80 : /***************************************************************************
81 : * stack based helper class for saving/restoring selection. Note that this
82 : * assumes that the nodes involved are still around afterwards!
83 : */
84 : class NS_STACK_CLASS nsAutoSelectionReset
85 : {
86 : private:
87 : /** ref-counted reference to the selection that we are supposed to restore */
88 : nsCOMPtr<nsISelection> mSel;
89 : nsEditor *mEd; // non-owning ref to nsEditor
90 :
91 : public:
92 : /** constructor responsible for remembering all state needed to restore aSel */
93 : nsAutoSelectionReset(nsISelection *aSel, nsEditor *aEd);
94 :
95 : /** destructor restores mSel to its former state */
96 : ~nsAutoSelectionReset();
97 :
98 : /** Abort: cancel selection saver */
99 : void Abort();
100 : };
101 :
102 : /***************************************************************************
103 : * stack based helper class for StartOperation()/EndOperation() sandwich
104 : */
105 : class NS_STACK_CLASS nsAutoRules
106 : {
107 : public:
108 :
109 0 : nsAutoRules(nsEditor *ed, PRInt32 action, nsIEditor::EDirection aDirection) :
110 0 : mEd(ed), mDoNothing(false)
111 : {
112 0 : if (mEd && !mEd->mAction) // mAction will already be set if this is nested call
113 : {
114 0 : mEd->StartOperation(action, aDirection);
115 : }
116 0 : else mDoNothing = true; // nested calls will end up here
117 0 : }
118 0 : ~nsAutoRules()
119 : {
120 0 : if (mEd && !mDoNothing)
121 : {
122 0 : mEd->EndOperation();
123 : }
124 0 : }
125 :
126 : protected:
127 : nsEditor *mEd;
128 : bool mDoNothing;
129 : };
130 :
131 :
132 : /***************************************************************************
133 : * stack based helper class for turning off active selection adjustment
134 : * by low level transactions
135 : */
136 : class NS_STACK_CLASS nsAutoTxnsConserveSelection
137 : {
138 : public:
139 :
140 0 : nsAutoTxnsConserveSelection(nsEditor *ed) : mEd(ed), mOldState(true)
141 : {
142 0 : if (mEd)
143 : {
144 0 : mOldState = mEd->GetShouldTxnSetSelection();
145 0 : mEd->SetShouldTxnSetSelection(false);
146 : }
147 0 : }
148 :
149 0 : ~nsAutoTxnsConserveSelection()
150 : {
151 0 : if (mEd)
152 : {
153 0 : mEd->SetShouldTxnSetSelection(mOldState);
154 : }
155 0 : }
156 :
157 : protected:
158 : nsEditor *mEd;
159 : bool mOldState;
160 : };
161 :
162 : /***************************************************************************
163 : * stack based helper class for batching reflow and paint requests.
164 : */
165 : class NS_STACK_CLASS nsAutoUpdateViewBatch
166 : {
167 : public:
168 :
169 0 : nsAutoUpdateViewBatch(nsEditor *ed) : mEd(ed)
170 : {
171 0 : NS_ASSERTION(mEd, "null mEd pointer!");
172 :
173 0 : if (mEd)
174 0 : mEd->BeginUpdateViewBatch();
175 0 : }
176 :
177 0 : ~nsAutoUpdateViewBatch()
178 : {
179 0 : if (mEd)
180 0 : mEd->EndUpdateViewBatch();
181 0 : }
182 :
183 : protected:
184 : nsEditor *mEd;
185 : };
186 :
187 : /******************************************************************************
188 : * some helper classes for iterating the dom tree
189 : *****************************************************************************/
190 :
191 : class nsBoolDomIterFunctor
192 0 : {
193 : public:
194 : virtual bool operator()(nsIDOMNode* aNode)=0;
195 : };
196 :
197 : class NS_STACK_CLASS nsDOMIterator
198 : {
199 : public:
200 : nsDOMIterator();
201 : virtual ~nsDOMIterator();
202 :
203 : nsresult Init(nsIDOMRange* aRange);
204 : nsresult Init(nsIDOMNode* aNode);
205 : nsresult AppendList(nsBoolDomIterFunctor& functor,
206 : nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
207 : protected:
208 : nsCOMPtr<nsIContentIterator> mIter;
209 : };
210 :
211 : class nsDOMSubtreeIterator : public nsDOMIterator
212 : {
213 : public:
214 : nsDOMSubtreeIterator();
215 : virtual ~nsDOMSubtreeIterator();
216 :
217 : nsresult Init(nsIDOMRange* aRange);
218 : };
219 :
220 : class nsTrivialFunctor : public nsBoolDomIterFunctor
221 0 : {
222 : public:
223 0 : virtual bool operator()(nsIDOMNode* aNode) // used to build list of all nodes iterator covers
224 : {
225 0 : return true;
226 : }
227 : };
228 :
229 :
230 : /******************************************************************************
231 : * general dom point utility struct
232 : *****************************************************************************/
233 : struct NS_STACK_CLASS DOMPoint
234 0 : {
235 : nsCOMPtr<nsIDOMNode> node;
236 : PRInt32 offset;
237 :
238 : DOMPoint() : node(0),offset(0) {}
239 0 : DOMPoint(nsIDOMNode *aNode, PRInt32 aOffset) :
240 0 : node(aNode),offset(aOffset) {}
241 0 : void SetPoint(nsIDOMNode *aNode, PRInt32 aOffset)
242 : {
243 0 : node = aNode; offset = aOffset;
244 0 : }
245 0 : void GetPoint(nsCOMPtr<nsIDOMNode> &aNode, PRInt32 &aOffset)
246 : {
247 0 : aNode = node; aOffset = offset;
248 0 : }
249 : };
250 :
251 :
252 : class nsEditorUtils
253 : {
254 : public:
255 : static bool IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 *aOffset = 0);
256 : static bool IsLeafNode(nsIDOMNode *aNode);
257 : };
258 :
259 :
260 : class nsITransferable;
261 : class nsIDOMEvent;
262 : class nsISimpleEnumerator;
263 :
264 : class nsEditorHookUtils
265 : {
266 : public:
267 : static bool DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
268 : nsITransferable *aTrans);
269 : private:
270 : static nsresult GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
271 : nsISimpleEnumerator **aEnumerator);
272 : };
273 :
274 : #endif // nsEditorUtils_h__
|