Back to index

lightning-sunbird  0.9+nobinonly
nsTextServicesDocument.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 nsTextServicesDocument_h__
00039 #define nsTextServicesDocument_h__
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsIPresShell.h"
00043 #include "nsIDOMDocument.h"
00044 #include "nsIDOMRange.h"
00045 #include "nsIContent.h"
00046 #include "nsIContentIterator.h"
00047 #include "nsIEditor.h"
00048 #include "nsIEditActionListener.h"
00049 #include "nsITextServicesDocument.h"
00050 #include "nsVoidArray.h"
00051 #include "nsTSDNotifier.h"
00052 #include "nsISelectionController.h"
00053 #include "nsITextServicesFilter.h"
00054 #include "nsWeakReference.h"
00055 
00056 class nsIWordBreaker;
00057 class nsIRangeUtils;
00058 
00062 class nsTextServicesDocument : public nsITextServicesDocument
00063 {
00064 private:
00065   static nsIAtom *sAAtom;
00066   static nsIAtom *sAddressAtom;
00067   static nsIAtom *sBigAtom;
00068   static nsIAtom *sBlinkAtom;
00069   static nsIAtom *sBAtom;
00070   static nsIAtom *sCiteAtom;
00071   static nsIAtom *sCodeAtom;
00072   static nsIAtom *sDfnAtom;
00073   static nsIAtom *sEmAtom;
00074   static nsIAtom *sFontAtom;
00075   static nsIAtom *sIAtom;
00076   static nsIAtom *sKbdAtom;
00077   static nsIAtom *sKeygenAtom;
00078   static nsIAtom *sNobrAtom;
00079   static nsIAtom *sSAtom;
00080   static nsIAtom *sSampAtom;
00081   static nsIAtom *sSmallAtom;
00082   static nsIAtom *sSpacerAtom;
00083   static nsIAtom *sSpanAtom;      
00084   static nsIAtom *sStrikeAtom;
00085   static nsIAtom *sStrongAtom;
00086   static nsIAtom *sSubAtom;
00087   static nsIAtom *sSupAtom;
00088   static nsIAtom *sTtAtom;
00089   static nsIAtom *sUAtom;
00090   static nsIAtom *sVarAtom;
00091   static nsIAtom *sWbrAtom;
00092 
00093   typedef enum { eIsDone=0,        // No iterator (I), or itertor doesn't point to anything valid.
00094                  eValid,           // I points to first text node (TN) in current block (CB).
00095                  ePrev,            // No TN in CB, I points to first TN in prev block.
00096                  eNext             // No TN in CB, I points to first TN in next block.
00097   } TSDIteratorStatus;
00098 
00099   nsCOMPtr<nsIDOMDocument>        mDOMDocument;
00100   nsCOMPtr<nsISelectionController>mSelCon;
00101   nsWeakPtr                       mEditor;  // avoid a cycle with the spell checker and editor
00102   nsCOMPtr<nsIContentIterator>    mIterator;
00103   TSDIteratorStatus               mIteratorStatus;
00104   nsCOMPtr<nsIContent>            mPrevTextBlock;
00105   nsCOMPtr<nsIContent>            mNextTextBlock;
00106   nsCOMPtr<nsIEditActionListener> mNotifier;
00107   nsVoidArray                     mOffsetTable;
00108 
00109   PRInt32                         mSelStartIndex;
00110   PRInt32                         mSelStartOffset;
00111   PRInt32                         mSelEndIndex;
00112   PRInt32                         mSelEndOffset;
00113 
00114   nsCOMPtr<nsIDOMRange>           mExtent;
00115 
00116   nsCOMPtr<nsITextServicesFilter> mTxtSvcFilter;
00117 
00118   static nsIRangeUtils* sRangeHelper;
00119 
00120 public:
00121 
00124   nsTextServicesDocument();
00125 
00128   virtual ~nsTextServicesDocument();
00129 
00132   static void RegisterAtoms();
00133 
00136   static void Shutdown();
00137 
00138   /* Macro for AddRef(), Release(), and QueryInterface() */
00139   NS_DECL_ISUPPORTS
00140 
00141   /* nsITextServicesDocument method implementations. */
00142   NS_IMETHOD InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell);
00143   NS_IMETHOD InitWithEditor(nsIEditor *aEditor);
00144   NS_IMETHOD GetDocument(nsIDOMDocument **aDoc);
00145   NS_IMETHOD SetExtent(nsIDOMRange* aDOMRange);
00146   NS_IMETHOD GetExtent(nsIDOMRange** aDOMRange);
00147   NS_IMETHOD ExpandRangeToWordBoundaries(nsIDOMRange *aRange);
00148   NS_IMETHOD SetFilter(nsITextServicesFilter *aFilter);
00149   NS_IMETHOD CanEdit(PRBool *aCanEdit);
00150   NS_IMETHOD GetCurrentTextBlock(nsString *aStr);
00151   NS_IMETHOD FirstBlock();
00152   NS_IMETHOD LastBlock();
00153   NS_IMETHOD FirstSelectedBlock(TSDBlockSelectionStatus *aSelStatus, PRInt32 *aSelOffset, PRInt32 *aSelLength);
00154   NS_IMETHOD LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, PRInt32 *aSelOffset, PRInt32 *aSelLength);
00155   NS_IMETHOD PrevBlock();
00156   NS_IMETHOD NextBlock();
00157   NS_IMETHOD IsDone(PRBool *aIsDone);
00158   NS_IMETHOD SetSelection(PRInt32 aOffset, PRInt32 aLength);
00159   NS_IMETHOD ScrollSelectionIntoView();
00160   NS_IMETHOD DeleteSelection();
00161   NS_IMETHOD InsertText(const nsString *aText);
00162   NS_IMETHOD SetDisplayStyle(TSDDisplayStyle aStyle);
00163   NS_IMETHOD GetDOMRangeFor(PRInt32 aOffset, PRInt32 aLength, nsIDOMRange** aRange);
00164 
00165   /* nsIEditActionListener method implementations. */
00166   nsresult InsertNode(nsIDOMNode * aNode,
00167                       nsIDOMNode * aParent,
00168                       PRInt32      aPosition);
00169   nsresult DeleteNode(nsIDOMNode * aChild);
00170   nsresult SplitNode(nsIDOMNode * aExistingRightNode,
00171                      PRInt32      aOffset,
00172                      nsIDOMNode * aNewLeftNode);
00173   nsresult JoinNodes(nsIDOMNode  *aLeftNode,
00174                      nsIDOMNode  *aRightNode,
00175                      nsIDOMNode  *aParent);
00176 
00177   /* Helper functions */
00178   static nsresult ComparePoints(nsIDOMNode *aParent1, PRInt32 aOffset1, nsIDOMNode *aParent2, PRInt32 aOffset2, PRInt32 *aResult);
00179   static nsresult GetRangeEndPoints(nsIDOMRange *aRange, nsIDOMNode **aParent1, PRInt32 *aOffset1, nsIDOMNode **aParent2, PRInt32 *aOffset2);
00180   static nsresult CreateRange(nsIDOMNode *aStartParent, PRInt32 aStartOffset, nsIDOMNode *aEndParent, PRInt32 aEndOffset, nsIDOMRange **aRange);
00181 
00182 private:
00183 
00184   /* nsTextServicesDocument private methods. */
00185 
00186   nsresult CreateContentIterator(nsIDOMRange *aRange, nsIContentIterator **aIterator);
00187 
00188   nsresult GetDocumentContentRootNode(nsIDOMNode **aNode);
00189   nsresult CreateDocumentContentRange(nsIDOMRange **aRange);
00190   nsresult CreateDocumentContentRootToNodeOffsetRange(nsIDOMNode *aParent, PRInt32 aOffset, PRBool aToStart, nsIDOMRange **aRange);
00191   nsresult CreateDocumentContentIterator(nsIContentIterator **aIterator);
00192 
00193   nsresult AdjustContentIterator();
00194 
00195   static nsresult FirstTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus);
00196   static nsresult LastTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus);
00197 
00198   static nsresult FirstTextNodeInCurrentBlock(nsIContentIterator *aIterator);
00199   static nsresult FirstTextNodeInPrevBlock(nsIContentIterator *aIterator);
00200   static nsresult FirstTextNodeInNextBlock(nsIContentIterator *aIterator);
00201 
00202   nsresult GetFirstTextNodeInPrevBlock(nsIContent **aContent);
00203   nsresult GetFirstTextNodeInNextBlock(nsIContent **aContent);
00204 
00205   static PRBool IsBlockNode(nsIContent *aContent);
00206   static PRBool IsTextNode(nsIContent *aContent);
00207   static PRBool IsTextNode(nsIDOMNode *aNode);
00208 
00209   static PRBool DidSkip(nsIContentIterator* aFilteredIter);
00210   static void   ClearDidSkip(nsIContentIterator* aFilteredIter);
00211 
00212   static PRBool HasSameBlockNodeParent(nsIContent *aContent1, nsIContent *aContent2);
00213 
00214   nsresult SetSelectionInternal(PRInt32 aOffset, PRInt32 aLength, PRBool aDoUpdate);
00215   nsresult GetSelection(TSDBlockSelectionStatus *aSelStatus, PRInt32 *aSelOffset, PRInt32 *aSelLength);
00216   nsresult GetCollapsedSelection(TSDBlockSelectionStatus *aSelStatus, PRInt32 *aSelOffset, PRInt32 *aSelLength);
00217   nsresult GetUncollapsedSelection(TSDBlockSelectionStatus *aSelStatus, PRInt32 *aSelOffset, PRInt32 *aSelLength);
00218 
00219   PRBool SelectionIsCollapsed();
00220   PRBool SelectionIsValid();
00221 
00222   static nsresult CreateOffsetTable(nsVoidArray *aOffsetTable,
00223                              nsIContentIterator *aIterator,
00224                              TSDIteratorStatus *aIteratorStatus,
00225                              nsIDOMRange *aIterRange,
00226                              nsString *aStr);
00227   static nsresult ClearOffsetTable(nsVoidArray *aOffsetTable);
00228 
00229   static nsresult NodeHasOffsetEntry(nsVoidArray *aOffsetTable,
00230                                      nsIDOMNode *aNode,
00231                                      PRBool *aHasEntry,
00232                                      PRInt32 *aEntryIndex);
00233 
00234   nsresult RemoveInvalidOffsetEntries();
00235   nsresult SplitOffsetEntry(PRInt32 aTableIndex, PRInt32 aOffsetIntoEntry);
00236 
00237   static nsresult GetWordBreaker(nsIWordBreaker **aWordBreaker);
00238   static nsresult FindWordBounds(nsVoidArray *offsetTable, nsString *blockStr,
00239                                  nsIWordBreaker *aWordBreaker,
00240                                  nsIDOMNode *aNode, PRInt32 aNodeOffset,
00241                                  nsIDOMNode **aWordStartNode,
00242                                  PRInt32 *aWordStartOffset,
00243                                  nsIDOMNode **aWordEndNode,
00244                                  PRInt32 *aWordEndOffset);
00245 
00246 #ifdef DEBUG_kin
00247   void PrintOffsetTable();
00248   void PrintContentNode(nsIContent *aContent);
00249 #endif
00250 };
00251 
00252 #endif // nsTextServicesDocument_h__