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 : #ifndef __selectionstate_h__
39 : #define __selectionstate_h__
40 :
41 : #include "nsCOMPtr.h"
42 : #include "nsTArray.h"
43 : #include "nsIDOMNode.h"
44 : #include "nsIDOMRange.h"
45 : #include "nsCycleCollectionParticipant.h"
46 :
47 : class nsIDOMCharacterData;
48 : class nsISelection;
49 : class nsRange;
50 :
51 : /***************************************************************************
52 : * class for recording selection info. stores selection as collection of
53 : * { {startnode, startoffset} , {endnode, endoffset} } tuples. Can't store
54 : * ranges since dom gravity will possibly change the ranges.
55 : */
56 :
57 : // first a helper struct for saving/setting ranges
58 : struct nsRangeStore
59 : {
60 : nsRangeStore();
61 : ~nsRangeStore();
62 : nsresult StoreRange(nsIDOMRange *aRange);
63 : nsresult GetRange(nsRange** outRange);
64 :
65 : nsCOMPtr<nsIDOMNode> startNode;
66 : PRInt32 startOffset;
67 : nsCOMPtr<nsIDOMNode> endNode;
68 : PRInt32 endOffset;
69 : // DEBUG: static PRInt32 n;
70 : };
71 :
72 : class nsSelectionState
73 : {
74 : public:
75 :
76 : nsSelectionState();
77 : ~nsSelectionState();
78 :
79 : void DoTraverse(nsCycleCollectionTraversalCallback &cb);
80 0 : void DoUnlink() { MakeEmpty(); }
81 :
82 : nsresult SaveSelection(nsISelection *aSel);
83 : nsresult RestoreSelection(nsISelection *aSel);
84 : bool IsCollapsed();
85 : bool IsEqual(nsSelectionState *aSelState);
86 : void MakeEmpty();
87 : bool IsEmpty();
88 : protected:
89 : nsTArray<nsRangeStore> mArray;
90 :
91 : friend class nsRangeUpdater;
92 : };
93 :
94 : class nsRangeUpdater
95 : {
96 : public:
97 :
98 : nsRangeUpdater();
99 : ~nsRangeUpdater();
100 :
101 : void RegisterRangeItem(nsRangeStore *aRangeItem);
102 : void DropRangeItem(nsRangeStore *aRangeItem);
103 : nsresult RegisterSelectionState(nsSelectionState &aSelState);
104 : nsresult DropSelectionState(nsSelectionState &aSelState);
105 :
106 : // editor selection gravity routines. Note that we can't always depend on
107 : // DOM Range gravity to do what we want to the "real" selection. For instance,
108 : // if you move a node, that corresponds to deleting it and reinserting it.
109 : // DOM Range gravity will promote the selection out of the node on deletion,
110 : // which is not what you want if you know you are reinserting it.
111 : nsresult SelAdjCreateNode(nsIDOMNode *aParent, PRInt32 aPosition);
112 : nsresult SelAdjInsertNode(nsIDOMNode *aParent, PRInt32 aPosition);
113 : nsresult SelAdjDeleteNode(nsIDOMNode *aNode);
114 : nsresult SelAdjSplitNode(nsIDOMNode *aOldRightNode, PRInt32 aOffset, nsIDOMNode *aNewLeftNode);
115 : nsresult SelAdjJoinNodes(nsIDOMNode *aLeftNode,
116 : nsIDOMNode *aRightNode,
117 : nsIDOMNode *aParent,
118 : PRInt32 aOffset,
119 : PRInt32 aOldLeftNodeLength);
120 : nsresult SelAdjInsertText(nsIDOMCharacterData *aTextNode, PRInt32 aOffset, const nsAString &aString);
121 : nsresult SelAdjDeleteText(nsIDOMCharacterData *aTextNode, PRInt32 aOffset, PRInt32 aLength);
122 : // the following gravity routines need will/did sandwiches, because the other gravity
123 : // routines will be called inside of these sandwiches, but should be ignored.
124 : nsresult WillReplaceContainer();
125 : nsresult DidReplaceContainer(nsIDOMNode *aOriginalNode, nsIDOMNode *aNewNode);
126 : nsresult WillRemoveContainer();
127 : nsresult DidRemoveContainer(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 aOffset, PRUint32 aNodeOrigLen);
128 : nsresult WillInsertContainer();
129 : nsresult DidInsertContainer();
130 : nsresult WillMoveNode();
131 : nsresult DidMoveNode(nsIDOMNode *aOldParent, PRInt32 aOldOffset, nsIDOMNode *aNewParent, PRInt32 aNewOffset);
132 : protected:
133 : nsTArray<nsRangeStore*> mArray;
134 : bool mLock;
135 : };
136 :
137 :
138 : /***************************************************************************
139 : * helper class for using nsSelectionState. stack based class for doing
140 : * preservation of dom points across editor actions
141 : */
142 :
143 : class NS_STACK_CLASS nsAutoTrackDOMPoint
144 : {
145 : private:
146 : nsRangeUpdater &mRU;
147 : nsCOMPtr<nsIDOMNode> *mNode;
148 : PRInt32 *mOffset;
149 : nsRangeStore mRangeItem;
150 : public:
151 0 : nsAutoTrackDOMPoint(nsRangeUpdater &aRangeUpdater, nsCOMPtr<nsIDOMNode> *aNode, PRInt32 *aOffset) :
152 : mRU(aRangeUpdater)
153 : ,mNode(aNode)
154 0 : ,mOffset(aOffset)
155 : {
156 0 : mRangeItem.startNode = *mNode;
157 0 : mRangeItem.endNode = *mNode;
158 0 : mRangeItem.startOffset = *mOffset;
159 0 : mRangeItem.endOffset = *mOffset;
160 0 : mRU.RegisterRangeItem(&mRangeItem);
161 0 : }
162 :
163 0 : ~nsAutoTrackDOMPoint()
164 0 : {
165 0 : mRU.DropRangeItem(&mRangeItem);
166 0 : *mNode = mRangeItem.startNode;
167 0 : *mOffset = mRangeItem.startOffset;
168 0 : }
169 : };
170 :
171 :
172 :
173 : /***************************************************************************
174 : * another helper class for nsSelectionState. stack based class for doing
175 : * Will/DidReplaceContainer()
176 : */
177 :
178 : class NS_STACK_CLASS nsAutoReplaceContainerSelNotify
179 : {
180 : private:
181 : nsRangeUpdater &mRU;
182 : nsIDOMNode *mOriginalNode;
183 : nsIDOMNode *mNewNode;
184 :
185 : public:
186 0 : nsAutoReplaceContainerSelNotify(nsRangeUpdater &aRangeUpdater, nsIDOMNode *aOriginalNode, nsIDOMNode *aNewNode) :
187 : mRU(aRangeUpdater)
188 : ,mOriginalNode(aOriginalNode)
189 0 : ,mNewNode(aNewNode)
190 : {
191 0 : mRU.WillReplaceContainer();
192 0 : }
193 :
194 0 : ~nsAutoReplaceContainerSelNotify()
195 : {
196 0 : mRU.DidReplaceContainer(mOriginalNode, mNewNode);
197 0 : }
198 : };
199 :
200 :
201 : /***************************************************************************
202 : * another helper class for nsSelectionState. stack based class for doing
203 : * Will/DidRemoveContainer()
204 : */
205 :
206 : class NS_STACK_CLASS nsAutoRemoveContainerSelNotify
207 : {
208 : private:
209 : nsRangeUpdater &mRU;
210 : nsIDOMNode *mNode;
211 : nsIDOMNode *mParent;
212 : PRInt32 mOffset;
213 : PRUint32 mNodeOrigLen;
214 :
215 : public:
216 0 : nsAutoRemoveContainerSelNotify(nsRangeUpdater &aRangeUpdater,
217 : nsIDOMNode *aNode,
218 : nsIDOMNode *aParent,
219 : PRInt32 aOffset,
220 : PRUint32 aNodeOrigLen) :
221 : mRU(aRangeUpdater)
222 : ,mNode(aNode)
223 : ,mParent(aParent)
224 : ,mOffset(aOffset)
225 0 : ,mNodeOrigLen(aNodeOrigLen)
226 : {
227 0 : mRU.WillRemoveContainer();
228 0 : }
229 :
230 0 : ~nsAutoRemoveContainerSelNotify()
231 : {
232 0 : mRU.DidRemoveContainer(mNode, mParent, mOffset, mNodeOrigLen);
233 0 : }
234 : };
235 :
236 : /***************************************************************************
237 : * another helper class for nsSelectionState. stack based class for doing
238 : * Will/DidInsertContainer()
239 : */
240 :
241 : class NS_STACK_CLASS nsAutoInsertContainerSelNotify
242 : {
243 : private:
244 : nsRangeUpdater &mRU;
245 :
246 : public:
247 0 : nsAutoInsertContainerSelNotify(nsRangeUpdater &aRangeUpdater) :
248 0 : mRU(aRangeUpdater)
249 : {
250 0 : mRU.WillInsertContainer();
251 0 : }
252 :
253 0 : ~nsAutoInsertContainerSelNotify()
254 : {
255 0 : mRU.DidInsertContainer();
256 0 : }
257 : };
258 :
259 :
260 : /***************************************************************************
261 : * another helper class for nsSelectionState. stack based class for doing
262 : * Will/DidMoveNode()
263 : */
264 :
265 : class NS_STACK_CLASS nsAutoMoveNodeSelNotify
266 : {
267 : private:
268 : nsRangeUpdater &mRU;
269 : nsIDOMNode *mOldParent;
270 : nsIDOMNode *mNewParent;
271 : PRInt32 mOldOffset;
272 : PRInt32 mNewOffset;
273 :
274 : public:
275 0 : nsAutoMoveNodeSelNotify(nsRangeUpdater &aRangeUpdater,
276 : nsIDOMNode *aOldParent,
277 : PRInt32 aOldOffset,
278 : nsIDOMNode *aNewParent,
279 : PRInt32 aNewOffset) :
280 : mRU(aRangeUpdater)
281 : ,mOldParent(aOldParent)
282 : ,mNewParent(aNewParent)
283 : ,mOldOffset(aOldOffset)
284 0 : ,mNewOffset(aNewOffset)
285 : {
286 0 : mRU.WillMoveNode();
287 0 : }
288 :
289 0 : ~nsAutoMoveNodeSelNotify()
290 : {
291 0 : mRU.DidMoveNode(mOldParent, mOldOffset, mNewParent, mNewOffset);
292 0 : }
293 : };
294 :
295 : #endif
296 :
297 :
|