Back to index

lightning-sunbird  0.9+nobinonly
nsTextEditRules.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsTextEditRules_h__
00039 #define nsTextEditRules_h__
00040 
00041 #include "nsCOMPtr.h"
00042 
00043 #include "nsPlaintextEditor.h"
00044 #include "nsIDOMNode.h"
00045 
00046 #include "nsEditRules.h"
00047 
00059 class nsTextEditRules : public nsIEditRules
00060 {
00061 public:
00062   NS_DECL_ISUPPORTS
00063   
00064               nsTextEditRules();
00065   virtual     ~nsTextEditRules();
00066 
00067   // nsIEditRules methods
00068   NS_IMETHOD Init(nsPlaintextEditor *aEditor, PRUint32 aFlags);
00069   NS_IMETHOD BeforeEdit(PRInt32 action, nsIEditor::EDirection aDirection);
00070   NS_IMETHOD AfterEdit(PRInt32 action, nsIEditor::EDirection aDirection);
00071   NS_IMETHOD WillDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, PRBool *aCancel, PRBool *aHandled);
00072   NS_IMETHOD DidDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, nsresult aResult);
00073   NS_IMETHOD GetFlags(PRUint32 *aFlags);
00074   NS_IMETHOD SetFlags(PRUint32 aFlags);
00075   NS_IMETHOD DocumentIsEmpty(PRBool *aDocumentIsEmpty);
00076 
00077   // nsTextEditRules action id's
00078   enum 
00079   {
00080     kDefault             = 0,
00081     // any editor that has a txn mgr
00082     kUndo                = 1000,
00083     kRedo                = 1001,
00084     // text actions
00085     kInsertText          = 2000,
00086     kInsertTextIME       = 2001,
00087     kDeleteSelection     = 2002,
00088     kSetTextProperty     = 2003,
00089     kRemoveTextProperty  = 2004,
00090     kOutputText          = 2005,
00091     // html only action
00092     kInsertBreak         = 3000,
00093     kMakeList            = 3001,
00094     kIndent              = 3002,
00095     kOutdent             = 3003,
00096     kAlign               = 3004,
00097     kMakeBasicBlock      = 3005,
00098     kRemoveList          = 3006,
00099     kMakeDefListItem     = 3007,
00100     kInsertElement       = 3008,
00101     kLoadHTML            = 3013,
00102     kSetAbsolutePosition = 3015,
00103     kRemoveAbsolutePosition = 3016,
00104     kDecreaseZIndex      = 3017,
00105     kIncreaseZIndex      = 3018
00106 
00107   };
00108   
00109 public:
00110   nsresult ResetIMETextPWBuf();
00111 
00112 protected:
00113 
00114   // nsTextEditRules implementation methods
00115   nsresult WillInsertText(  PRInt32          aAction,
00116                             nsISelection *aSelection, 
00117                             PRBool          *aCancel,
00118                             PRBool          *aHandled,
00119                             const nsAString *inString,
00120                             nsAString       *outString,
00121                             PRInt32          aMaxLength);
00122   nsresult DidInsertText(nsISelection *aSelection, nsresult aResult);
00123   nsresult GetTopEnclosingPre(nsIDOMNode *aNode, nsIDOMNode** aOutPreNode);
00124 
00125   nsresult WillInsertBreak(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
00126   nsresult DidInsertBreak(nsISelection *aSelection, nsresult aResult);
00127 
00128   nsresult WillInsert(nsISelection *aSelection, PRBool *aCancel);
00129   nsresult DidInsert(nsISelection *aSelection, nsresult aResult);
00130 
00131   nsresult WillDeleteSelection(nsISelection *aSelection, 
00132                                nsIEditor::EDirection aCollapsedAction, 
00133                                PRBool *aCancel,
00134                                PRBool *aHandled);
00135   nsresult DidDeleteSelection(nsISelection *aSelection, 
00136                               nsIEditor::EDirection aCollapsedAction, 
00137                               nsresult aResult);
00138 
00139   nsresult WillSetTextProperty(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
00140   nsresult DidSetTextProperty(nsISelection *aSelection, nsresult aResult);
00141 
00142   nsresult WillRemoveTextProperty(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
00143   nsresult DidRemoveTextProperty(nsISelection *aSelection, nsresult aResult);
00144 
00145   nsresult WillUndo(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
00146   nsresult DidUndo(nsISelection *aSelection, nsresult aResult);
00147 
00148   nsresult WillRedo(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
00149   nsresult DidRedo(nsISelection *aSelection, nsresult aResult);
00150 
00158   nsresult WillOutputText(nsISelection *aSelection,
00159                           const nsAString  *aInFormat,
00160                           nsAString *aOutText, 
00161                           PRBool   *aOutCancel, 
00162                           PRBool *aHandled);
00163 
00164   nsresult DidOutputText(nsISelection *aSelection, nsresult aResult);
00165 
00166 
00167   // helper functions
00168   
00170   nsresult ReplaceNewlines(nsIDOMRange *aRange);
00171   
00173   nsresult CreateTrailingBRIfNeeded();
00174   
00176   nsresult CreateBogusNodeIfNeeded(nsISelection *aSelection);
00177 
00180   nsresult TruncateInsertionIfNeeded(nsISelection             *aSelection, 
00181                                      const nsAString          *aInString,
00182                                      nsAString                *aOutString,
00183                                      PRInt32                   aMaxLength);
00184   
00187   nsresult EchoInsertionToPWBuff(PRInt32 aStart, PRInt32 aEnd, nsAString *aOutString);
00188 
00190   nsresult RemoveIMETextFromPWBuf(PRUint32 &aStart, nsAString *aIMEString);
00191 
00192   nsresult CreateMozBR(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outBRNode);
00193 
00194   nsresult CheckBidiLevelForDeletion(nsIDOMNode           *aSelNode, 
00195                                      PRInt32               aSelOffset, 
00196                                      nsIEditor::EDirection aAction,
00197                                      PRBool               *aCancel);
00198 
00199   nsIDOMNode *GetBody();
00200 
00201   // data members
00202   nsPlaintextEditor   *mEditor;        // note that we do not refcount the editor
00203   nsString             mPasswordText;  // a buffer we use to store the real value of password editors
00204   nsString             mPasswordIMEText;  // a buffer we use to track the IME composition string
00205   PRUint32             mPasswordIMEIndex;
00206   nsCOMPtr<nsIDOMNode> mBogusNode;     // magic node acts as placeholder in empty doc
00207   nsCOMPtr<nsIDOMNode> mBody;          // cached root node
00208   nsCOMPtr<nsIDOMNode> mCachedSelectionNode;    // cached selected node
00209   PRInt32              mCachedSelectionOffset;  // cached selected offset
00210   PRUint32             mFlags;
00211   PRUint32             mActionNesting;
00212   PRPackedBool         mLockRulesSniffing;
00213   PRPackedBool         mDidExplicitlySetInterline;
00214   PRInt32              mTheAction;     // the top level editor action
00215   // friends
00216   friend class nsAutoLockRulesSniffing;
00217 
00218 };
00219 
00220 
00221 
00222 class nsTextRulesInfo : public nsRulesInfo
00223 {
00224  public:
00225  
00226   nsTextRulesInfo(int aAction) : 
00227     nsRulesInfo(aAction),
00228     inString(0),
00229     outString(0),
00230     outputFormat(0),
00231     maxLength(-1),
00232     collapsedAction(nsIEditor::eNext),
00233     bOrdered(PR_FALSE),
00234     entireList(PR_FALSE),
00235     bulletType(0),
00236     alignType(0),
00237     blockType(0),
00238     insertElement(0)
00239     {};
00240 
00241   virtual ~nsTextRulesInfo() {};
00242   
00243   // kInsertText
00244   const nsAString *inString;
00245   nsAString *outString;
00246   const nsAString *outputFormat;
00247   PRInt32 maxLength;
00248   
00249   // kDeleteSelection
00250   nsIEditor::EDirection collapsedAction;
00251   
00252   // kMakeList
00253   PRBool bOrdered;
00254   PRBool entireList;
00255   const nsAString *bulletType;
00256 
00257   // kAlign
00258   const nsAString *alignType;
00259   
00260   // kMakeBasicBlock
00261   const nsAString *blockType;
00262   
00263   // kInsertElement
00264   const nsIDOMElement* insertElement;
00265 };
00266 
00267 
00268 /***************************************************************************
00269  * stack based helper class for StartOperation()/EndOperation() sandwich.
00270  * this class sets a bool letting us know to ignore any rules sniffing
00271  * that tries to occur reentrantly. 
00272  */
00273 class nsAutoLockRulesSniffing
00274 {
00275   public:
00276   
00277   nsAutoLockRulesSniffing(nsTextEditRules *rules) : mRules(rules) 
00278                  {if (mRules) mRules->mLockRulesSniffing = PR_TRUE;}
00279   ~nsAutoLockRulesSniffing() 
00280                  {if (mRules) mRules->mLockRulesSniffing = PR_FALSE;}
00281   
00282   protected:
00283   nsTextEditRules *mRules;
00284 };
00285 
00286 
00287 
00288 /***************************************************************************
00289  * stack based helper class for turning on/off the edit listener.
00290  */
00291 class nsAutoLockListener
00292 {
00293   public:
00294   
00295   nsAutoLockListener(PRPackedBool *enabled) : mEnabled(enabled)
00296                  {if (mEnabled) { mOldState=*mEnabled; *mEnabled = PR_FALSE;}}
00297   ~nsAutoLockListener() 
00298                  {if (mEnabled) *mEnabled = mOldState;}
00299   
00300   protected:
00301   PRPackedBool *mEnabled;
00302   PRPackedBool mOldState;
00303 };
00304 
00305 
00306 nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult);
00307 
00308 #endif //nsTextEditRules_h__
00309