Back to index

lightning-sunbird  0.9+nobinonly
nsRange.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 #ifndef nsRange_h___
00038 #define nsRange_h___
00039 
00040 /*
00041  * nsRange.h: interface of the nsRange object.
00042  */
00043 
00044 #include "nsIDOMRange.h"
00045 #include "nsIRangeUtils.h"
00046 #include "nsIDOMNSRange.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsIDOMDocumentFragment.h"
00049 #include "nsIContent.h"
00050 #include "nsIDOMNode.h"
00051 #include "prmon.h"
00052 
00053 class nsVoidArray;
00054 
00055 // -------------------------------------------------------------------------------
00056 
00057 class nsRangeUtils : public nsIRangeUtils
00058 {
00059 public:
00060   NS_DECL_ISUPPORTS
00061 
00062   nsRangeUtils();
00063   virtual ~nsRangeUtils();
00064 
00065   // nsIRangeUtils interface
00066   NS_IMETHOD_(PRInt32) ComparePoints(nsIDOMNode* aParent1, PRInt32 aOffset1,
00067                                      nsIDOMNode* aParent2, PRInt32 aOffset2);
00068                                
00069   NS_IMETHOD_(PRBool) IsNodeIntersectsRange(nsIContent* aNode, nsIDOMRange* aRange);
00070   
00071   NS_IMETHOD CompareNodeToRange(nsIContent* aNode, 
00072                                 nsIDOMRange* aRange,
00073                                 PRBool *outNodeBefore,
00074                                 PRBool *outNodeAfter);
00075 };
00076 
00077 // -------------------------------------------------------------------------------
00078 
00079 class nsRange : public nsIDOMRange,
00080                 public nsIDOMNSRange
00081 {
00082 public:
00083   NS_DECL_ISUPPORTS
00084 
00085   nsRange();
00086   virtual ~nsRange();
00087 
00088   // nsIDOMRange interface
00089   NS_DECL_NSIDOMRANGE
00090 
00091 /*BEGIN nsIDOMNSRange interface implementations*/
00092   NS_IMETHOD    CreateContextualFragment(const nsAString& aFragment, 
00093                                          nsIDOMDocumentFragment** aReturn);
00094   NS_IMETHOD    IsPointInRange(nsIDOMNode* aParent, PRInt32 aOffset,
00095                                PRBool* aResult);
00096   NS_IMETHOD    ComparePoint(nsIDOMNode* aParent, PRInt32 aOffset,
00097                              PRInt16* aResult);
00098   NS_IMETHOD    IntersectsNode(nsIDOMNode* aNode, PRBool* aReturn);
00099   NS_IMETHOD    CompareNode(nsIDOMNode* aNode, PRUint16* aReturn);
00100   NS_IMETHOD    NSDetach();
00101 /*END nsIDOMNSRange interface implementations*/
00102   
00103   NS_IMETHOD    GetHasGeneratedBefore(PRBool *aBool);
00104   NS_IMETHOD    GetHasGeneratedAfter(PRBool *aBool);
00105   NS_IMETHOD    SetHasGeneratedBefore(PRBool aBool);
00106   NS_IMETHOD    SetHasGeneratedAfter(PRBool aBool);
00107   NS_IMETHOD    SetBeforeAndAfter(PRBool aBefore, PRBool aAfter);
00108 
00109   // nsRange interface extensions
00110   
00111   static NS_METHOD    OwnerGone(nsIContent* aParentNode);
00112   
00113   static NS_METHOD    OwnerChildInserted(nsIContent* aParentNode, PRInt32 aOffset);
00114   
00115   static NS_METHOD    OwnerChildRemoved(nsIContent* aParentNode, PRInt32 aOffset, nsIContent* aRemovedNode);
00116   
00117   static NS_METHOD    OwnerChildReplaced(nsIContent* aParentNode, PRInt32 aOffset, nsIContent* aReplacedNode);
00118   
00119   static nsresult TextOwnerChanged(nsIContent *aTextNode,
00120                                    nsVoidArray *aRangeList,
00121                                    PRInt32 aStartOffset,
00122                                    PRInt32 aEndOffset,
00123                                    PRInt32 aReplaceLength);
00124 
00125 protected:
00126 
00127   PRPackedBool mBeforeGenContent;
00128   PRPackedBool mAfterGenContent;
00129 
00130   PRPackedBool mIsPositioned;
00131   PRPackedBool mIsDetached;
00132 
00133   PRInt32      mStartOffset;
00134   PRInt32      mEndOffset;
00135 
00136   nsCOMPtr<nsIDOMNode> mStartParent;
00137   nsCOMPtr<nsIDOMNode> mEndParent;
00138 
00139   // no copy's or assigns
00140   nsRange(const nsRange&);
00141   nsRange& operator=(const nsRange&);
00142  
00143 public:
00144   // helper routines
00145   
00146   static PRInt32       IndexOf(nsIDOMNode* aNode);
00147   static nsresult      PopRanges(nsIDOMNode* aDestNode, PRInt32 aOffset, nsIContent* aSourceNode);
00148 
00149   static nsresult CloneParentsBetween(nsIDOMNode* aAncestor, 
00150                                       nsIDOMNode* aNode,
00151                                       nsIDOMNode** closestAncestor,
00152                                       nsIDOMNode** farthestAncestor);
00153 
00160   static PRInt32 ComparePoints(nsIDOMNode* aParent1, PRInt32 aOffset1,
00161                                nsIDOMNode* aParent2, PRInt32 aOffset2);
00162 
00166   static PRBool IsNodeIntersectsRange(nsIContent* aNode, nsIDOMRange* aRange);
00167 
00168 /******************************************************************************
00169  *  Utility routine to detect if a content node starts before a range and/or 
00170  *  ends after a range.  If neither it is contained inside the range.
00171  *  
00172  *  XXX - callers responsibility to ensure node in same doc as range!
00173  *
00174  *****************************************************************************/
00175   static nsresult CompareNodeToRange(nsIContent* aNode, nsIDOMRange* aRange,
00176                                      PRBool *outNodeBefore,
00177                                      PRBool *outNodeAfter);
00178 
00179 protected:
00180 
00181   // CollapseRangeAfterDelete() should only be called from DeleteContents()
00182   // or ExtractContents() since it makes certain assumptions about the state
00183   // of the range used. It's purpose is to collapse the range according to
00184   // the range spec after the removal of nodes within the range.
00185   static nsresult CollapseRangeAfterDelete(nsIDOMRange *aRange);
00186 
00187   static PRInt32  GetNodeLength(nsIDOMNode *aNode);
00188   
00189   nsresult      DoSetRange(nsIDOMNode* aStartN, PRInt32 aStartOffset,
00190                              nsIDOMNode* aEndN, PRInt32 aEndOffset);
00191 
00192   static PRBool IsIncreasing(nsIDOMNode* aStartN, PRInt32 aStartOff,
00193                              nsIDOMNode* aEndN, PRInt32 aEndOff);
00194   PRBool        IsDetached(){return mIsDetached;}
00195                        
00196   nsresult      ComparePointToRange(nsIDOMNode* aParent, PRInt32 aOffset, PRInt32* aResult);
00197   
00198   
00199   nsresult      AddToListOf(nsIDOMNode* aNode);
00200   
00201   void          RemoveFromListOf(nsIDOMNode* aNode);
00202  
00203   nsresult      ContentOwnsUs(nsIDOMNode* domNode);
00204 
00205   nsresult      GetIsPositioned(PRBool* aIsPositioned);
00206 };
00207 
00208 // Make a new nsIDOMRange object
00209 nsresult NS_NewRange(nsIDOMRange** aInstancePtrResult);
00210 
00211 // Make a new nsIRangeUtils object
00212 nsresult NS_NewRangeUtils(nsIRangeUtils** aInstancePtrResult);
00213 
00214 
00215 /*************************************************************************************
00216  * Utility routine to create a pair of dom points to represent 
00217  * the start and end locations of a single node.  Return false
00218  * if we dont' succeed.
00219  ************************************************************************************/
00220 PRBool GetNodeBracketPoints(nsIContent* aNode, 
00221                             nsCOMPtr<nsIDOMNode>* outParent,
00222                             PRInt32* outStartOffset,
00223                             PRInt32* outEndOffset);
00224 
00225 #endif /* nsRange_h___ */