Bug 574778 - Fix win widget's ConstrainPosition so that it supports full screen windo...
[mozilla-central.git] / editor / libeditor / text / nsTextEditRules.h
blobba1eadb4a5e64a75aa779b011fdd6b0dd2a89cf2
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsTextEditRules_h__
39 #define nsTextEditRules_h__
41 #include "nsCOMPtr.h"
43 #include "nsPlaintextEditor.h"
44 #include "nsIDOMNode.h"
46 #include "nsEditRules.h"
47 #include "nsITimer.h"
49 /** Object that encapsulates HTML text-specific editing rules.
51 * To be a good citizen, edit rules must live by these restrictions:
52 * 1. All data manipulation is through the editor.
53 * Content nodes in the document tree must <B>not</B> be manipulated directly.
54 * Content nodes in document fragments that are not part of the document itself
55 * may be manipulated at will. Operations on document fragments must <B>not</B>
56 * go through the editor.
57 * 2. Selection must not be explicitly set by the rule method.
58 * Any manipulation of Selection must be done by the editor.
60 class nsTextEditRules : public nsIEditRules, public nsITimerCallback
62 public:
63 NS_DECL_NSITIMERCALLBACK
64 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
65 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextEditRules, nsIEditRules)
67 nsTextEditRules();
68 virtual ~nsTextEditRules();
70 // nsIEditRules methods
71 NS_IMETHOD Init(nsPlaintextEditor *aEditor);
72 NS_IMETHOD DetachEditor();
73 NS_IMETHOD BeforeEdit(PRInt32 action, nsIEditor::EDirection aDirection);
74 NS_IMETHOD AfterEdit(PRInt32 action, nsIEditor::EDirection aDirection);
75 NS_IMETHOD WillDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, PRBool *aCancel, PRBool *aHandled);
76 NS_IMETHOD DidDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, nsresult aResult);
77 NS_IMETHOD DocumentIsEmpty(PRBool *aDocumentIsEmpty);
79 // nsTextEditRules action id's
80 enum
82 kDefault = 0,
83 // any editor that has a txn mgr
84 kUndo = 1000,
85 kRedo = 1001,
86 // text actions
87 kInsertText = 2000,
88 kInsertTextIME = 2001,
89 kDeleteSelection = 2002,
90 kSetTextProperty = 2003,
91 kRemoveTextProperty = 2004,
92 kOutputText = 2005,
93 // html only action
94 kInsertBreak = 3000,
95 kMakeList = 3001,
96 kIndent = 3002,
97 kOutdent = 3003,
98 kAlign = 3004,
99 kMakeBasicBlock = 3005,
100 kRemoveList = 3006,
101 kMakeDefListItem = 3007,
102 kInsertElement = 3008,
103 kLoadHTML = 3013,
104 kSetAbsolutePosition = 3015,
105 kRemoveAbsolutePosition = 3016,
106 kDecreaseZIndex = 3017,
107 kIncreaseZIndex = 3018
111 public:
112 nsresult ResetIMETextPWBuf();
115 * Handles the newline characters either according to aNewLineHandling
116 * or to the default system prefs if aNewLineHandling is negative.
118 * @param aString the string to be modified in place.
119 * @param aNewLineHandling determine the desired type of newline handling:
120 * * negative values:
121 * handle newlines according to platform defaults.
122 * * nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
123 * replace newlines with spaces.
124 * * nsIPlaintextEditor::eNewlinesStrip:
125 * remove newlines from the string.
126 * * nsIPlaintextEditor::eNewlinesReplaceWithCommas:
127 * replace newlines with commas.
128 * * nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace:
129 * collapse newlines and surrounding whitespace characters and
130 * remove them from the string.
131 * * nsIPlaintextEditor::eNewlinesPasteIntact:
132 * only remove the leading and trailing newlines.
133 * * nsIPlaintextEditor::eNewlinesPasteToFirst or any other value:
134 * remove the first newline and all characters following it.
136 static void HandleNewLines(nsString &aString, PRInt32 aNewLineHandling);
139 * Prepare a string buffer for being displayed as the contents of a password
140 * field. This function uses the platform-specific character for representing
141 * characters entered into password fields.
143 * @param aOutString the output string. When this function returns,
144 * aOutString will contain aLength password characters.
145 * @param aLength the number of password characters that aOutString should
146 * contain.
148 static nsresult FillBufWithPWChars(nsAString *aOutString, PRInt32 aLength);
150 protected:
152 // nsTextEditRules implementation methods
153 nsresult WillInsertText( PRInt32 aAction,
154 nsISelection *aSelection,
155 PRBool *aCancel,
156 PRBool *aHandled,
157 const nsAString *inString,
158 nsAString *outString,
159 PRInt32 aMaxLength);
160 nsresult DidInsertText(nsISelection *aSelection, nsresult aResult);
161 nsresult GetTopEnclosingPre(nsIDOMNode *aNode, nsIDOMNode** aOutPreNode);
163 nsresult WillInsertBreak(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
164 nsresult DidInsertBreak(nsISelection *aSelection, nsresult aResult);
166 nsresult WillInsert(nsISelection *aSelection, PRBool *aCancel);
167 nsresult DidInsert(nsISelection *aSelection, nsresult aResult);
169 nsresult WillDeleteSelection(nsISelection *aSelection,
170 nsIEditor::EDirection aCollapsedAction,
171 PRBool *aCancel,
172 PRBool *aHandled);
173 nsresult DidDeleteSelection(nsISelection *aSelection,
174 nsIEditor::EDirection aCollapsedAction,
175 nsresult aResult);
177 nsresult WillSetTextProperty(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
178 nsresult DidSetTextProperty(nsISelection *aSelection, nsresult aResult);
180 nsresult WillRemoveTextProperty(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
181 nsresult DidRemoveTextProperty(nsISelection *aSelection, nsresult aResult);
183 nsresult WillUndo(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
184 nsresult DidUndo(nsISelection *aSelection, nsresult aResult);
186 nsresult WillRedo(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
187 nsresult DidRedo(nsISelection *aSelection, nsresult aResult);
189 /** called prior to nsIEditor::OutputToString
190 * @param aSelection
191 * @param aInFormat the format requested for the output, a MIME type
192 * @param aOutText the string to use for output, if aCancel is set to true
193 * @param aOutCancel if set to PR_TRUE, the caller should cancel the operation
194 * and use aOutText as the result.
196 nsresult WillOutputText(nsISelection *aSelection,
197 const nsAString *aInFormat,
198 nsAString *aOutText,
199 PRBool *aOutCancel,
200 PRBool *aHandled);
202 nsresult DidOutputText(nsISelection *aSelection, nsresult aResult);
205 // helper functions
207 /** replaces newllines with breaks, if needed. acts on doc portion in aRange */
208 nsresult ReplaceNewlines(nsIDOMRange *aRange);
210 /** creates a trailing break in the text doc if there is not one already */
211 nsresult CreateTrailingBRIfNeeded();
213 /** creates a bogus text node if the document has no editable content */
214 nsresult CreateBogusNodeIfNeeded(nsISelection *aSelection);
216 /** returns a truncated insertion string if insertion would place us
217 over aMaxLength */
218 nsresult TruncateInsertionIfNeeded(nsISelection *aSelection,
219 const nsAString *aInString,
220 nsAString *aOutString,
221 PRInt32 aMaxLength);
223 /** Remove IME composition text from password buffer */
224 nsresult RemoveIMETextFromPWBuf(PRUint32 &aStart, nsAString *aIMEString);
226 nsresult CreateMozBR(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outBRNode);
228 nsresult CheckBidiLevelForDeletion(nsISelection *aSelection,
229 nsIDOMNode *aSelNode,
230 PRInt32 aSelOffset,
231 nsIEditor::EDirection aAction,
232 PRBool *aCancel);
234 nsresult HideLastPWInput();
236 PRBool IsPasswordEditor() const
238 return mEditor ? mEditor->IsPasswordEditor() : PR_FALSE;
240 PRBool IsSingleLineEditor() const
242 return mEditor ? mEditor->IsSingleLineEditor() : PR_FALSE;
244 PRBool IsPlaintextEditor() const
246 return mEditor ? mEditor->IsPlaintextEditor() : PR_FALSE;
248 PRBool IsReadonly() const
250 return mEditor ? mEditor->IsReadonly() : PR_FALSE;
252 PRBool IsDisabled() const
254 return mEditor ? mEditor->IsDisabled() : PR_FALSE;
256 PRBool IsMailEditor() const
258 return mEditor ? mEditor->IsMailEditor() : PR_FALSE;
260 PRBool DontEchoPassword() const
262 return mEditor ? mEditor->DontEchoPassword() : PR_FALSE;
265 // data members
266 nsPlaintextEditor *mEditor; // note that we do not refcount the editor
267 nsString mPasswordText; // a buffer we use to store the real value of password editors
268 nsString mPasswordIMEText; // a buffer we use to track the IME composition string
269 PRUint32 mPasswordIMEIndex;
270 nsCOMPtr<nsIDOMNode> mBogusNode; // magic node acts as placeholder in empty doc
271 nsCOMPtr<nsIDOMNode> mCachedSelectionNode; // cached selected node
272 PRInt32 mCachedSelectionOffset; // cached selected offset
273 PRUint32 mActionNesting;
274 PRPackedBool mLockRulesSniffing;
275 PRPackedBool mDidExplicitlySetInterline;
276 PRPackedBool mDeleteBidiImmediately; // in bidirectional text, delete
277 // characters not visually
278 // adjacent to the caret without
279 // moving the caret first.
280 PRInt32 mTheAction; // the top level editor action
281 nsCOMPtr<nsITimer> mTimer;
282 PRUint32 mLastStart, mLastLength;
284 // friends
285 friend class nsAutoLockRulesSniffing;
291 class nsTextRulesInfo : public nsRulesInfo
293 public:
295 nsTextRulesInfo(int aAction) :
296 nsRulesInfo(aAction),
297 inString(0),
298 outString(0),
299 outputFormat(0),
300 maxLength(-1),
301 collapsedAction(nsIEditor::eNext),
302 bOrdered(PR_FALSE),
303 entireList(PR_FALSE),
304 bulletType(0),
305 alignType(0),
306 blockType(0),
307 insertElement(0)
310 virtual ~nsTextRulesInfo() {}
312 // kInsertText
313 const nsAString *inString;
314 nsAString *outString;
315 const nsAString *outputFormat;
316 PRInt32 maxLength;
318 // kDeleteSelection
319 nsIEditor::EDirection collapsedAction;
321 // kMakeList
322 PRBool bOrdered;
323 PRBool entireList;
324 const nsAString *bulletType;
326 // kAlign
327 const nsAString *alignType;
329 // kMakeBasicBlock
330 const nsAString *blockType;
332 // kInsertElement
333 const nsIDOMElement* insertElement;
337 /***************************************************************************
338 * stack based helper class for StartOperation()/EndOperation() sandwich.
339 * this class sets a bool letting us know to ignore any rules sniffing
340 * that tries to occur reentrantly.
342 class nsAutoLockRulesSniffing
344 public:
346 nsAutoLockRulesSniffing(nsTextEditRules *rules) : mRules(rules)
347 {if (mRules) mRules->mLockRulesSniffing = PR_TRUE;}
348 ~nsAutoLockRulesSniffing()
349 {if (mRules) mRules->mLockRulesSniffing = PR_FALSE;}
351 protected:
352 nsTextEditRules *mRules;
357 /***************************************************************************
358 * stack based helper class for turning on/off the edit listener.
360 class nsAutoLockListener
362 public:
364 nsAutoLockListener(PRPackedBool *enabled) : mEnabled(enabled)
365 {if (mEnabled) { mOldState=*mEnabled; *mEnabled = PR_FALSE;}}
366 ~nsAutoLockListener()
367 {if (mEnabled) *mEnabled = mOldState;}
369 protected:
370 PRPackedBool *mEnabled;
371 PRPackedBool mOldState;
375 nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult);
377 #endif //nsTextEditRules_h__