editor/libeditor/base/nsEditorUtils.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:d84c30560c42
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
7 #ifndef nsEditorUtils_h__
8 #define nsEditorUtils_h__
9
10
11 #include "nsCOMPtr.h"
12 #include "nsDebug.h"
13 #include "nsEditor.h"
14 #include "nsIDOMNode.h"
15 #include "nsIEditor.h"
16 #include "nscore.h"
17
18 class nsIAtom;
19 class nsIContentIterator;
20 class nsIDOMDocument;
21 class nsIDOMRange;
22 class nsISelection;
23 template <class E> class nsCOMArray;
24
25 /***************************************************************************
26 * stack based helper class for batching a collection of txns inside a
27 * placeholder txn.
28 */
29 class MOZ_STACK_CLASS nsAutoPlaceHolderBatch
30 {
31 private:
32 nsCOMPtr<nsIEditor> mEd;
33 public:
34 nsAutoPlaceHolderBatch( nsIEditor *aEd, nsIAtom *atom) : mEd(do_QueryInterface(aEd))
35 { if (mEd) mEd->BeginPlaceHolderTransaction(atom); }
36 ~nsAutoPlaceHolderBatch() { if (mEd) mEd->EndPlaceHolderTransaction(); }
37 };
38
39 /***************************************************************************
40 * stack based helper class for batching a collection of txns.
41 * Note: I changed this to use placeholder batching so that we get
42 * proper selection save/restore across undo/redo.
43 */
44 class MOZ_STACK_CLASS nsAutoEditBatch : public nsAutoPlaceHolderBatch
45 {
46 public:
47 nsAutoEditBatch( nsIEditor *aEd) : nsAutoPlaceHolderBatch(aEd,nullptr) {}
48 ~nsAutoEditBatch() {}
49 };
50
51 /***************************************************************************
52 * stack based helper class for saving/restoring selection. Note that this
53 * assumes that the nodes involved are still around afterwards!
54 */
55 class MOZ_STACK_CLASS nsAutoSelectionReset
56 {
57 private:
58 /** ref-counted reference to the selection that we are supposed to restore */
59 nsRefPtr<mozilla::dom::Selection> mSel;
60 nsEditor *mEd; // non-owning ref to nsEditor
61
62 public:
63 /** constructor responsible for remembering all state needed to restore aSel */
64 nsAutoSelectionReset(mozilla::dom::Selection* aSel, nsEditor* aEd);
65
66 /** destructor restores mSel to its former state */
67 ~nsAutoSelectionReset();
68
69 /** Abort: cancel selection saver */
70 void Abort();
71 };
72
73 /***************************************************************************
74 * stack based helper class for StartOperation()/EndOperation() sandwich
75 */
76 class MOZ_STACK_CLASS nsAutoRules
77 {
78 public:
79
80 nsAutoRules(nsEditor *ed, EditAction action,
81 nsIEditor::EDirection aDirection) :
82 mEd(ed), mDoNothing(false)
83 {
84 if (mEd && !mEd->mAction) // mAction will already be set if this is nested call
85 {
86 mEd->StartOperation(action, aDirection);
87 }
88 else mDoNothing = true; // nested calls will end up here
89 }
90 ~nsAutoRules()
91 {
92 if (mEd && !mDoNothing)
93 {
94 mEd->EndOperation();
95 }
96 }
97
98 protected:
99 nsEditor *mEd;
100 bool mDoNothing;
101 };
102
103
104 /***************************************************************************
105 * stack based helper class for turning off active selection adjustment
106 * by low level transactions
107 */
108 class MOZ_STACK_CLASS nsAutoTxnsConserveSelection
109 {
110 public:
111
112 nsAutoTxnsConserveSelection(nsEditor *ed) : mEd(ed), mOldState(true)
113 {
114 if (mEd)
115 {
116 mOldState = mEd->GetShouldTxnSetSelection();
117 mEd->SetShouldTxnSetSelection(false);
118 }
119 }
120
121 ~nsAutoTxnsConserveSelection()
122 {
123 if (mEd)
124 {
125 mEd->SetShouldTxnSetSelection(mOldState);
126 }
127 }
128
129 protected:
130 nsEditor *mEd;
131 bool mOldState;
132 };
133
134 /***************************************************************************
135 * stack based helper class for batching reflow and paint requests.
136 */
137 class MOZ_STACK_CLASS nsAutoUpdateViewBatch
138 {
139 public:
140
141 nsAutoUpdateViewBatch(nsEditor *ed) : mEd(ed)
142 {
143 NS_ASSERTION(mEd, "null mEd pointer!");
144
145 if (mEd)
146 mEd->BeginUpdateViewBatch();
147 }
148
149 ~nsAutoUpdateViewBatch()
150 {
151 if (mEd)
152 mEd->EndUpdateViewBatch();
153 }
154
155 protected:
156 nsEditor *mEd;
157 };
158
159 /******************************************************************************
160 * some helper classes for iterating the dom tree
161 *****************************************************************************/
162
163 class nsBoolDomIterFunctor
164 {
165 public:
166 virtual bool operator()(nsIDOMNode* aNode)=0;
167 };
168
169 class MOZ_STACK_CLASS nsDOMIterator
170 {
171 public:
172 nsDOMIterator();
173 virtual ~nsDOMIterator();
174
175 nsresult Init(nsIDOMRange* aRange);
176 nsresult Init(nsIDOMNode* aNode);
177 nsresult AppendList(nsBoolDomIterFunctor& functor,
178 nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
179 protected:
180 nsCOMPtr<nsIContentIterator> mIter;
181 };
182
183 class MOZ_STACK_CLASS nsDOMSubtreeIterator : public nsDOMIterator
184 {
185 public:
186 nsDOMSubtreeIterator();
187 virtual ~nsDOMSubtreeIterator();
188
189 nsresult Init(nsIDOMRange* aRange);
190 };
191
192 class nsTrivialFunctor : public nsBoolDomIterFunctor
193 {
194 public:
195 virtual bool operator()(nsIDOMNode* aNode) // used to build list of all nodes iterator covers
196 {
197 return true;
198 }
199 };
200
201
202 /******************************************************************************
203 * general dom point utility struct
204 *****************************************************************************/
205 struct MOZ_STACK_CLASS DOMPoint
206 {
207 nsCOMPtr<nsIDOMNode> node;
208 int32_t offset;
209
210 DOMPoint() : node(0),offset(0) {}
211 DOMPoint(nsIDOMNode *aNode, int32_t aOffset) :
212 node(aNode),offset(aOffset) {}
213 void SetPoint(nsIDOMNode *aNode, int32_t aOffset)
214 {
215 node = aNode; offset = aOffset;
216 }
217 void GetPoint(nsCOMPtr<nsIDOMNode> &aNode, int32_t &aOffset)
218 {
219 aNode = node; aOffset = offset;
220 }
221 };
222
223
224 class nsEditorUtils
225 {
226 public:
227 static bool IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t *aOffset = 0);
228 static bool IsLeafNode(nsIDOMNode *aNode);
229 };
230
231
232 class nsIDOMEvent;
233 class nsISimpleEnumerator;
234 class nsITransferable;
235
236 class nsEditorHookUtils
237 {
238 public:
239 static bool DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
240 nsITransferable *aTrans);
241 private:
242 static nsresult GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
243 nsISimpleEnumerator **aEnumerator);
244 };
245
246 #endif // nsEditorUtils_h__

mercurial