From 7fd16575ef30b3a080e021a7c74a3bcf93fd4bf3 Mon Sep 17 00:00:00 2001 From: "kin%netscape.com" Date: Fri, 23 Apr 1999 16:52:28 +0000 Subject: [PATCH] Misc changes to the nsITextServicesDocument interface. git-svn-id: svn://10.0.0.236/trunk@28867 18797224-902f-48f8-a5cc-f745e15eee43 --- .../txtsvc/nsITextServicesDocument.h | 118 ++++++++-- .../txtsvc/nsTextServicesDocument.cpp | 206 +++++++++++++----- .../libeditor/txtsvc/nsTextServicesDocument.h | 10 +- .../txtsvc/public/nsITextServicesDocument.h | 118 ++++++++-- .../txtsvc/src/nsTextServicesDocument.cpp | 206 +++++++++++++----- .../txtsvc/src/nsTextServicesDocument.h | 10 +- 6 files changed, 512 insertions(+), 156 deletions(-) diff --git a/mozilla/editor/libeditor/txtsvc/nsITextServicesDocument.h b/mozilla/editor/libeditor/txtsvc/nsITextServicesDocument.h index 8e117c8d2fb..dd29cd0ed62 100644 --- a/mozilla/editor/libeditor/txtsvc/nsITextServicesDocument.h +++ b/mozilla/editor/libeditor/txtsvc/nsITextServicesDocument.h @@ -37,79 +37,157 @@ TextServicesDocument interface to outside world /** - * A text services specific interface. - *

- * It's implemented by an object that executes some behavior that must be - * tracked by the transaction manager. + * The nsITextServicesDocument presents the document in as a + * bunch of flattened text blocks. Each text block can be retrieved + * as an nsString (array of characters). */ class nsITextServicesDocument : public nsISupports{ public: static const nsIID& GetIID() { static nsIID iid = NS_ITEXTSERVICESDOCUMENT_IID; return iid; } - /** - * - */ - NS_IMETHOD Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) = 0; + typedef enum { eDSNormal=0, eDSUndlerline } TSDDisplayStyle; /** - * + * Initailizes the text services document to use a particular + * DOM document. + * @param aDOMDocument is the document to use. It is AddRef'd + * by this method. + * @param aPresShell is the presentation shell to use when + * setting the selection. It is AddRef'd by this method. */ - NS_IMETHOD SetEditor(nsIEditor *aEditor) = 0; + NS_IMETHOD InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) = 0; /** - * + * Initializes the text services document to use a particular + * editor. The text services document will use the DOM document + * and presentation shell used by the editor. + * @param aEditor is the editor to use. The editor is AddRef'd + * by this method. + */ + NS_IMETHOD InitWithEditor(nsIEditor *aEditor) = 0; + + /** + * Returns true if the document can be modified with calls + * to DeleteSelection() and InsertText(). + * @param aCanEdit is true if the document can be modified, + * false if it can't. + */ + NS_IMETHOD CanEdit(PRBool *aCanEdit) = 0; + + /** + * Returns the text in the current text block. + * @param aStr will contain the text. */ NS_IMETHOD GetCurrentTextBlock(nsString *aStr) = 0; /** - * + * Tells the document to point to the first text block + * in the document. This method does not adjust the current + * cursor position or selection. */ NS_IMETHOD FirstBlock() = 0; /** - * + * Tells the document to point to the last text block in the + * document. This method does not adjust the current cursor + * position or selection. */ NS_IMETHOD LastBlock() = 0; /** - * + * Tells the document to point to the first text block that + * contains the current selection or caret. + * @param aSelectionOffset will contain the offset into the + * string returned by GetCurrentTextBlock() where the selection + * begins. + * @param aLength will contain the number of characters that are + * selected in the string. + */ + + NS_IMETHOD FirstSelectedBlock(PRInt32 *aSelectionOffset, PRInt32 *aSelectionLength) = 0; + + /** + * Tells the document to point to the last text block that + * contains the current selection or caret. + * @param aSelectionOffset will contain the offset into the + * string returned by GetCurrentTextBlock() where the selection + * begins. + * @param aLength will contain the number of characters that are + * selected in the string. + */ + + NS_IMETHOD LastSelectedBlock(PRInt32 *aSelectionOffset, PRInt32 *aSelectionLength) = 0; + + /** + * Tells the document to point to the text block before + * the current one. This method will return NS_OK, even + * if there is no previous block. Callers should call IsDone() + * to check if we have gone beyond the first text block in + * the document. */ NS_IMETHOD PrevBlock() = 0; /** - * + * Tells the document to point to the text block after + * the current one. This method will return NS_OK, even + * if there is no next block. Callers should call IsDone() + * to check if we have gone beyond the last text block + * in the document. */ NS_IMETHOD NextBlock() = 0; /** - * + * IsDone() will always set aIsDone == PR_FALSE unless + * the document contains no text, PrevBlock() was called + * while the document was already pointing to the first + * text block in the document, or NextBlock() was called + * while the document was already pointing to the last + * text block in the document. + * @param aIsDone will contain the result. */ - NS_IMETHOD IsDone() = 0; + NS_IMETHOD IsDone(PRBool *aIsDone) = 0; /** - * + * SetSelection() allows the caller to set the selection + * based on an offset into the string returned by + * GetCurrentTextBlock(). A length of zero places the cursor + * at that offset. A positive non-zero length "n" selects + * n characters in the string. + * @param aOffset offset into string returned by GetCurrentTextBlock(). + * @param aLength number characters selected. */ NS_IMETHOD SetSelection(PRInt32 aOffset, PRInt32 aLength) = 0; /** - * + * Deletes the text selected by SetSelection(). Calling + * DeleteSelection with nothing selected, or with a collapsed + * selection (cursor) does nothing and returns NS_OK. */ NS_IMETHOD DeleteSelection() = 0; /** - * + * Inserts the given text at the current cursor position. + * If there is a selection, it will be deleted before the + * text is inserted. */ NS_IMETHOD InsertText(const nsString *aText) = 0; + + /** + * Sets the display style for the text selected by SetSelection(). + * @param aStyle is the style to apply to the selected text. + */ + + NS_IMETHOD SetDisplayStyle(TSDDisplayStyle aStyle) = 0; }; #endif // nsITextServicesDocument_h__ diff --git a/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.cpp b/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.cpp index dd1d7d4dcad..984c60a2b2a 100644 --- a/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.cpp +++ b/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.cpp @@ -235,7 +235,7 @@ nsTextServicesDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) } NS_IMETHODIMP -nsTextServicesDocument::Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) +nsTextServicesDocument::InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) { nsresult result = NS_OK; @@ -257,7 +257,15 @@ nsTextServicesDocument::Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresSh mPresShell = do_QueryInterface(aPresShell); mDOMDocument = do_QueryInterface(aDOMDocument); - InitContentIterator(); + result = InitContentIterator(); + + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } + + result = FirstBlock(); UNLOCK_DOC(this); @@ -265,7 +273,7 @@ nsTextServicesDocument::Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresSh } NS_IMETHODIMP -nsTextServicesDocument::SetEditor(nsIEditor *aEditor) +nsTextServicesDocument::InitWithEditor(nsIEditor *aEditor) { nsresult result = NS_OK; nsCOMPtr presShell; @@ -315,7 +323,22 @@ nsTextServicesDocument::SetEditor(nsIEditor *aEditor) if (!mDOMDocument) { mDOMDocument = doc; - InitContentIterator(); + + result = InitContentIterator(); + + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } + + result = FirstBlock(); + + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } } mEditor = do_QueryInterface(aEditor); @@ -339,6 +362,17 @@ nsTextServicesDocument::SetEditor(nsIEditor *aEditor) return result; } +NS_IMETHODIMP +nsTextServicesDocument::CanEdit(PRBool *aCanEdit) +{ + if (!aCanEdit) + return NS_ERROR_NULL_POINTER; + + *aCanEdit = (mEditor) ? PR_TRUE : PR_FALSE; + + return NS_OK; +} + NS_IMETHODIMP nsTextServicesDocument::GetCurrentTextBlock(nsString *aStr) { @@ -436,39 +470,51 @@ nsTextServicesDocument::GetCurrentTextBlock(nsString *aStr) NS_IMETHODIMP nsTextServicesDocument::PrevBlock() { - nsresult result; + nsresult result = NS_OK; if (!mIterator) return NS_ERROR_FAILURE; LOCK_DOC(this); - if (mIteratorStatus == nsTextServicesDocument::eValid || mIteratorStatus == nsTextServicesDocument::eNext) + switch (mIteratorStatus) { - result = FirstTextNodeInPrevBlock(); + case nsTextServicesDocument::eValid: + case nsTextServicesDocument::eNext: - if (NS_FAILED(result)) - { - UNLOCK_DOC(this); - return result; - } + result = FirstTextNodeInPrevBlock(); - // XXX: Check to make sure the iterator is pointing - // to a valid text block, and set mIteratorStatus - // accordingly! - } - else if (mIteratorStatus == nsTextServicesDocument::ePrev) - { - // The iterator already points to the previous - // block, so don't do anything. + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } - mIteratorStatus = nsTextServicesDocument::eValid; + // XXX: Check to make sure the iterator is pointing + // to a valid text block, and set mIteratorStatus + // accordingly! + + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + case nsTextServicesDocument::ePrev: + + // The iterator already points to the previous + // block, so don't do anything. + + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + default: + + mIteratorStatus = nsTextServicesDocument::eInvalid; + break; } // Keep track of prev and next blocks, just in case // the text service blows away the current block. - if (eValid) + if (mIteratorStatus == nsTextServicesDocument::eValid) { result = FindFirstTextNodeInPrevBlock(getter_AddRefs(mPrevTextBlock)); result = FindFirstTextNodeInNextBlock(getter_AddRefs(mNextTextBlock)); @@ -482,39 +528,49 @@ nsTextServicesDocument::PrevBlock() NS_IMETHODIMP nsTextServicesDocument::NextBlock() { - nsresult result; + nsresult result = NS_OK; if (!mIterator) return NS_ERROR_FAILURE; LOCK_DOC(this); - if (mIteratorStatus == nsTextServicesDocument::eValid) + switch (mIteratorStatus) { - result = FirstTextNodeInNextBlock(); + case nsTextServicesDocument::eValid: - if (NS_FAILED(result)) - { - UNLOCK_DOC(this); - return result; - } + result = FirstTextNodeInNextBlock(); - // XXX: check if the iterator is pointing to a text node, - // then set mIteratorStatus. - } - else if (mIteratorStatus == nsTextServicesDocument::eNext) - { - // The iterator already points to the next block, - // so don't do anything to it! + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } - mIteratorStatus = nsTextServicesDocument::eValid; - } - else if (mIteratorStatus == nsTextServicesDocument::ePrev) - { - // If the iterator is pointing to the previous block, - // we know that there is no next text block! + // XXX: check if the iterator is pointing to a text node, + // then set mIteratorStatus. - mIteratorStatus = nsTextServicesDocument::eInvalid; + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + case nsTextServicesDocument::eNext: + + // The iterator already points to the next block, + // so don't do anything to it! + + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + case nsTextServicesDocument::ePrev: + + // If the iterator is pointing to the previous block, + // we know that there is no next text block! Just + // fall through to the default case! + + default: + + mIteratorStatus = nsTextServicesDocument::eInvalid; + break; } // Keep track of prev and next blocks, just in case @@ -533,14 +589,36 @@ nsTextServicesDocument::NextBlock() } NS_IMETHODIMP -nsTextServicesDocument::IsDone() +nsTextServicesDocument::IsDone(PRBool *aIsDone) { + nsresult result = NS_OK; + if (!mIterator) return NS_ERROR_FAILURE; + if (!aIsDone) + return NS_ERROR_NULL_POINTER; + + *aIsDone = PR_FALSE; + LOCK_DOC(this); - nsresult result = mIterator->IsDone(); + // XXX: Take into account the iterator status! + + if (mIteratorStatus == eInvalid && !mPrevTextBlock && !mNextTextBlock) + { + // We have an empty document! + *aIsDone = PR_TRUE; + } + else + { + result = mIterator->IsDone(); + + if (result != NS_COMFALSE) + *aIsDone = PR_TRUE; + else + result = NS_OK; + } UNLOCK_DOC(this); @@ -552,7 +630,7 @@ nsTextServicesDocument::SetSelection(PRInt32 aOffset, PRInt32 aLength) { nsresult result = NS_OK; - if ((!mPresShell && !mEditor) || aOffset < 0 || aLength < 0) + if (!mPresShell || aOffset < 0 || aLength < 0) return NS_ERROR_FAILURE; LOCK_DOC(this); @@ -696,9 +774,9 @@ nsTextServicesDocument::DeleteSelection() LOCK_DOC(this); //**** KDEBUG **** - printf("\n---- Before Delete\n"); - printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); - PrintOffsetTable(); + // printf("\n---- Before Delete\n"); + // printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); + // PrintOffsetTable(); //**** KDEBUG **** PRInt32 i, selLength; @@ -888,9 +966,9 @@ nsTextServicesDocument::DeleteSelection() result = RemoveInvalidOffsetEntries(); //**** KDEBUG **** - printf("\n---- After Delete\n"); - printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); - PrintOffsetTable(); + // printf("\n---- After Delete\n"); + // printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); + // PrintOffsetTable(); //**** KDEBUG **** UNLOCK_DOC(this); @@ -1147,6 +1225,12 @@ nsTextServicesDocument::InsertText(const nsString *aText) return result; } +NS_IMETHODIMP +nsTextServicesDocument::SetDisplayStyle(TSDDisplayStyle aStyle) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + nsresult nsTextServicesDocument::InsertNode(nsIDOMNode *aNode, nsIDOMNode *aParent, @@ -1755,7 +1839,7 @@ nsTextServicesDocument::SelectionIsValid() return(mSelStartIndex >= 0); } -nsresult +NS_IMETHODIMP nsTextServicesDocument::FirstBlock() { nsresult result; @@ -1811,7 +1895,7 @@ nsTextServicesDocument::FirstBlock() return result; } -nsresult +NS_IMETHODIMP nsTextServicesDocument::LastBlock() { nsresult result; @@ -1875,6 +1959,18 @@ nsTextServicesDocument::LastBlock() return result; } +NS_IMETHODIMP +nsTextServicesDocument::FirstSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsTextServicesDocument::LastSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + nsresult nsTextServicesDocument::FirstTextNodeInCurrentBlock() { diff --git a/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.h b/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.h index b8cb8399368..39aee01fc5b 100644 --- a/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.h +++ b/mozilla/editor/libeditor/txtsvc/nsTextServicesDocument.h @@ -97,19 +97,23 @@ public: NS_DECL_ISUPPORTS /* nsITextServicesDocumentInternal method implementations. */ - NS_IMETHOD Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell); - NS_IMETHOD SetEditor(nsIEditor *aEditor); + NS_IMETHOD InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell); + NS_IMETHOD InitWithEditor(nsIEditor *aEditor); /* nsITextServicesDocument method implementations. */ + NS_IMETHOD CanEdit(PRBool *aCanEdit); NS_IMETHOD GetCurrentTextBlock(nsString *aStr); NS_IMETHOD FirstBlock(); NS_IMETHOD LastBlock(); + NS_IMETHOD FirstSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength); + NS_IMETHOD LastSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength); NS_IMETHOD PrevBlock(); NS_IMETHOD NextBlock(); - NS_IMETHOD IsDone(); + NS_IMETHOD IsDone(PRBool *aIsDone); NS_IMETHOD SetSelection(PRInt32 aOffset, PRInt32 aLength); NS_IMETHOD DeleteSelection(); NS_IMETHOD InsertText(const nsString *aText); + NS_IMETHOD SetDisplayStyle(TSDDisplayStyle aStyle); /* nsIEditActionListener method implementations. */ nsresult InsertNode(nsIDOMNode * aNode, diff --git a/mozilla/editor/txtsvc/public/nsITextServicesDocument.h b/mozilla/editor/txtsvc/public/nsITextServicesDocument.h index 8e117c8d2fb..dd29cd0ed62 100644 --- a/mozilla/editor/txtsvc/public/nsITextServicesDocument.h +++ b/mozilla/editor/txtsvc/public/nsITextServicesDocument.h @@ -37,79 +37,157 @@ TextServicesDocument interface to outside world /** - * A text services specific interface. - *

- * It's implemented by an object that executes some behavior that must be - * tracked by the transaction manager. + * The nsITextServicesDocument presents the document in as a + * bunch of flattened text blocks. Each text block can be retrieved + * as an nsString (array of characters). */ class nsITextServicesDocument : public nsISupports{ public: static const nsIID& GetIID() { static nsIID iid = NS_ITEXTSERVICESDOCUMENT_IID; return iid; } - /** - * - */ - NS_IMETHOD Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) = 0; + typedef enum { eDSNormal=0, eDSUndlerline } TSDDisplayStyle; /** - * + * Initailizes the text services document to use a particular + * DOM document. + * @param aDOMDocument is the document to use. It is AddRef'd + * by this method. + * @param aPresShell is the presentation shell to use when + * setting the selection. It is AddRef'd by this method. */ - NS_IMETHOD SetEditor(nsIEditor *aEditor) = 0; + NS_IMETHOD InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) = 0; /** - * + * Initializes the text services document to use a particular + * editor. The text services document will use the DOM document + * and presentation shell used by the editor. + * @param aEditor is the editor to use. The editor is AddRef'd + * by this method. + */ + NS_IMETHOD InitWithEditor(nsIEditor *aEditor) = 0; + + /** + * Returns true if the document can be modified with calls + * to DeleteSelection() and InsertText(). + * @param aCanEdit is true if the document can be modified, + * false if it can't. + */ + NS_IMETHOD CanEdit(PRBool *aCanEdit) = 0; + + /** + * Returns the text in the current text block. + * @param aStr will contain the text. */ NS_IMETHOD GetCurrentTextBlock(nsString *aStr) = 0; /** - * + * Tells the document to point to the first text block + * in the document. This method does not adjust the current + * cursor position or selection. */ NS_IMETHOD FirstBlock() = 0; /** - * + * Tells the document to point to the last text block in the + * document. This method does not adjust the current cursor + * position or selection. */ NS_IMETHOD LastBlock() = 0; /** - * + * Tells the document to point to the first text block that + * contains the current selection or caret. + * @param aSelectionOffset will contain the offset into the + * string returned by GetCurrentTextBlock() where the selection + * begins. + * @param aLength will contain the number of characters that are + * selected in the string. + */ + + NS_IMETHOD FirstSelectedBlock(PRInt32 *aSelectionOffset, PRInt32 *aSelectionLength) = 0; + + /** + * Tells the document to point to the last text block that + * contains the current selection or caret. + * @param aSelectionOffset will contain the offset into the + * string returned by GetCurrentTextBlock() where the selection + * begins. + * @param aLength will contain the number of characters that are + * selected in the string. + */ + + NS_IMETHOD LastSelectedBlock(PRInt32 *aSelectionOffset, PRInt32 *aSelectionLength) = 0; + + /** + * Tells the document to point to the text block before + * the current one. This method will return NS_OK, even + * if there is no previous block. Callers should call IsDone() + * to check if we have gone beyond the first text block in + * the document. */ NS_IMETHOD PrevBlock() = 0; /** - * + * Tells the document to point to the text block after + * the current one. This method will return NS_OK, even + * if there is no next block. Callers should call IsDone() + * to check if we have gone beyond the last text block + * in the document. */ NS_IMETHOD NextBlock() = 0; /** - * + * IsDone() will always set aIsDone == PR_FALSE unless + * the document contains no text, PrevBlock() was called + * while the document was already pointing to the first + * text block in the document, or NextBlock() was called + * while the document was already pointing to the last + * text block in the document. + * @param aIsDone will contain the result. */ - NS_IMETHOD IsDone() = 0; + NS_IMETHOD IsDone(PRBool *aIsDone) = 0; /** - * + * SetSelection() allows the caller to set the selection + * based on an offset into the string returned by + * GetCurrentTextBlock(). A length of zero places the cursor + * at that offset. A positive non-zero length "n" selects + * n characters in the string. + * @param aOffset offset into string returned by GetCurrentTextBlock(). + * @param aLength number characters selected. */ NS_IMETHOD SetSelection(PRInt32 aOffset, PRInt32 aLength) = 0; /** - * + * Deletes the text selected by SetSelection(). Calling + * DeleteSelection with nothing selected, or with a collapsed + * selection (cursor) does nothing and returns NS_OK. */ NS_IMETHOD DeleteSelection() = 0; /** - * + * Inserts the given text at the current cursor position. + * If there is a selection, it will be deleted before the + * text is inserted. */ NS_IMETHOD InsertText(const nsString *aText) = 0; + + /** + * Sets the display style for the text selected by SetSelection(). + * @param aStyle is the style to apply to the selected text. + */ + + NS_IMETHOD SetDisplayStyle(TSDDisplayStyle aStyle) = 0; }; #endif // nsITextServicesDocument_h__ diff --git a/mozilla/editor/txtsvc/src/nsTextServicesDocument.cpp b/mozilla/editor/txtsvc/src/nsTextServicesDocument.cpp index dd1d7d4dcad..984c60a2b2a 100644 --- a/mozilla/editor/txtsvc/src/nsTextServicesDocument.cpp +++ b/mozilla/editor/txtsvc/src/nsTextServicesDocument.cpp @@ -235,7 +235,7 @@ nsTextServicesDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) } NS_IMETHODIMP -nsTextServicesDocument::Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) +nsTextServicesDocument::InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell) { nsresult result = NS_OK; @@ -257,7 +257,15 @@ nsTextServicesDocument::Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresSh mPresShell = do_QueryInterface(aPresShell); mDOMDocument = do_QueryInterface(aDOMDocument); - InitContentIterator(); + result = InitContentIterator(); + + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } + + result = FirstBlock(); UNLOCK_DOC(this); @@ -265,7 +273,7 @@ nsTextServicesDocument::Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresSh } NS_IMETHODIMP -nsTextServicesDocument::SetEditor(nsIEditor *aEditor) +nsTextServicesDocument::InitWithEditor(nsIEditor *aEditor) { nsresult result = NS_OK; nsCOMPtr presShell; @@ -315,7 +323,22 @@ nsTextServicesDocument::SetEditor(nsIEditor *aEditor) if (!mDOMDocument) { mDOMDocument = doc; - InitContentIterator(); + + result = InitContentIterator(); + + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } + + result = FirstBlock(); + + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } } mEditor = do_QueryInterface(aEditor); @@ -339,6 +362,17 @@ nsTextServicesDocument::SetEditor(nsIEditor *aEditor) return result; } +NS_IMETHODIMP +nsTextServicesDocument::CanEdit(PRBool *aCanEdit) +{ + if (!aCanEdit) + return NS_ERROR_NULL_POINTER; + + *aCanEdit = (mEditor) ? PR_TRUE : PR_FALSE; + + return NS_OK; +} + NS_IMETHODIMP nsTextServicesDocument::GetCurrentTextBlock(nsString *aStr) { @@ -436,39 +470,51 @@ nsTextServicesDocument::GetCurrentTextBlock(nsString *aStr) NS_IMETHODIMP nsTextServicesDocument::PrevBlock() { - nsresult result; + nsresult result = NS_OK; if (!mIterator) return NS_ERROR_FAILURE; LOCK_DOC(this); - if (mIteratorStatus == nsTextServicesDocument::eValid || mIteratorStatus == nsTextServicesDocument::eNext) + switch (mIteratorStatus) { - result = FirstTextNodeInPrevBlock(); + case nsTextServicesDocument::eValid: + case nsTextServicesDocument::eNext: - if (NS_FAILED(result)) - { - UNLOCK_DOC(this); - return result; - } + result = FirstTextNodeInPrevBlock(); - // XXX: Check to make sure the iterator is pointing - // to a valid text block, and set mIteratorStatus - // accordingly! - } - else if (mIteratorStatus == nsTextServicesDocument::ePrev) - { - // The iterator already points to the previous - // block, so don't do anything. + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } - mIteratorStatus = nsTextServicesDocument::eValid; + // XXX: Check to make sure the iterator is pointing + // to a valid text block, and set mIteratorStatus + // accordingly! + + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + case nsTextServicesDocument::ePrev: + + // The iterator already points to the previous + // block, so don't do anything. + + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + default: + + mIteratorStatus = nsTextServicesDocument::eInvalid; + break; } // Keep track of prev and next blocks, just in case // the text service blows away the current block. - if (eValid) + if (mIteratorStatus == nsTextServicesDocument::eValid) { result = FindFirstTextNodeInPrevBlock(getter_AddRefs(mPrevTextBlock)); result = FindFirstTextNodeInNextBlock(getter_AddRefs(mNextTextBlock)); @@ -482,39 +528,49 @@ nsTextServicesDocument::PrevBlock() NS_IMETHODIMP nsTextServicesDocument::NextBlock() { - nsresult result; + nsresult result = NS_OK; if (!mIterator) return NS_ERROR_FAILURE; LOCK_DOC(this); - if (mIteratorStatus == nsTextServicesDocument::eValid) + switch (mIteratorStatus) { - result = FirstTextNodeInNextBlock(); + case nsTextServicesDocument::eValid: - if (NS_FAILED(result)) - { - UNLOCK_DOC(this); - return result; - } + result = FirstTextNodeInNextBlock(); - // XXX: check if the iterator is pointing to a text node, - // then set mIteratorStatus. - } - else if (mIteratorStatus == nsTextServicesDocument::eNext) - { - // The iterator already points to the next block, - // so don't do anything to it! + if (NS_FAILED(result)) + { + UNLOCK_DOC(this); + return result; + } - mIteratorStatus = nsTextServicesDocument::eValid; - } - else if (mIteratorStatus == nsTextServicesDocument::ePrev) - { - // If the iterator is pointing to the previous block, - // we know that there is no next text block! + // XXX: check if the iterator is pointing to a text node, + // then set mIteratorStatus. - mIteratorStatus = nsTextServicesDocument::eInvalid; + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + case nsTextServicesDocument::eNext: + + // The iterator already points to the next block, + // so don't do anything to it! + + mIteratorStatus = nsTextServicesDocument::eValid; + break; + + case nsTextServicesDocument::ePrev: + + // If the iterator is pointing to the previous block, + // we know that there is no next text block! Just + // fall through to the default case! + + default: + + mIteratorStatus = nsTextServicesDocument::eInvalid; + break; } // Keep track of prev and next blocks, just in case @@ -533,14 +589,36 @@ nsTextServicesDocument::NextBlock() } NS_IMETHODIMP -nsTextServicesDocument::IsDone() +nsTextServicesDocument::IsDone(PRBool *aIsDone) { + nsresult result = NS_OK; + if (!mIterator) return NS_ERROR_FAILURE; + if (!aIsDone) + return NS_ERROR_NULL_POINTER; + + *aIsDone = PR_FALSE; + LOCK_DOC(this); - nsresult result = mIterator->IsDone(); + // XXX: Take into account the iterator status! + + if (mIteratorStatus == eInvalid && !mPrevTextBlock && !mNextTextBlock) + { + // We have an empty document! + *aIsDone = PR_TRUE; + } + else + { + result = mIterator->IsDone(); + + if (result != NS_COMFALSE) + *aIsDone = PR_TRUE; + else + result = NS_OK; + } UNLOCK_DOC(this); @@ -552,7 +630,7 @@ nsTextServicesDocument::SetSelection(PRInt32 aOffset, PRInt32 aLength) { nsresult result = NS_OK; - if ((!mPresShell && !mEditor) || aOffset < 0 || aLength < 0) + if (!mPresShell || aOffset < 0 || aLength < 0) return NS_ERROR_FAILURE; LOCK_DOC(this); @@ -696,9 +774,9 @@ nsTextServicesDocument::DeleteSelection() LOCK_DOC(this); //**** KDEBUG **** - printf("\n---- Before Delete\n"); - printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); - PrintOffsetTable(); + // printf("\n---- Before Delete\n"); + // printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); + // PrintOffsetTable(); //**** KDEBUG **** PRInt32 i, selLength; @@ -888,9 +966,9 @@ nsTextServicesDocument::DeleteSelection() result = RemoveInvalidOffsetEntries(); //**** KDEBUG **** - printf("\n---- After Delete\n"); - printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); - PrintOffsetTable(); + // printf("\n---- After Delete\n"); + // printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset); + // PrintOffsetTable(); //**** KDEBUG **** UNLOCK_DOC(this); @@ -1147,6 +1225,12 @@ nsTextServicesDocument::InsertText(const nsString *aText) return result; } +NS_IMETHODIMP +nsTextServicesDocument::SetDisplayStyle(TSDDisplayStyle aStyle) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + nsresult nsTextServicesDocument::InsertNode(nsIDOMNode *aNode, nsIDOMNode *aParent, @@ -1755,7 +1839,7 @@ nsTextServicesDocument::SelectionIsValid() return(mSelStartIndex >= 0); } -nsresult +NS_IMETHODIMP nsTextServicesDocument::FirstBlock() { nsresult result; @@ -1811,7 +1895,7 @@ nsTextServicesDocument::FirstBlock() return result; } -nsresult +NS_IMETHODIMP nsTextServicesDocument::LastBlock() { nsresult result; @@ -1875,6 +1959,18 @@ nsTextServicesDocument::LastBlock() return result; } +NS_IMETHODIMP +nsTextServicesDocument::FirstSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsTextServicesDocument::LastSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + nsresult nsTextServicesDocument::FirstTextNodeInCurrentBlock() { diff --git a/mozilla/editor/txtsvc/src/nsTextServicesDocument.h b/mozilla/editor/txtsvc/src/nsTextServicesDocument.h index b8cb8399368..39aee01fc5b 100644 --- a/mozilla/editor/txtsvc/src/nsTextServicesDocument.h +++ b/mozilla/editor/txtsvc/src/nsTextServicesDocument.h @@ -97,19 +97,23 @@ public: NS_DECL_ISUPPORTS /* nsITextServicesDocumentInternal method implementations. */ - NS_IMETHOD Init(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell); - NS_IMETHOD SetEditor(nsIEditor *aEditor); + NS_IMETHOD InitWithDocument(nsIDOMDocument *aDOMDocument, nsIPresShell *aPresShell); + NS_IMETHOD InitWithEditor(nsIEditor *aEditor); /* nsITextServicesDocument method implementations. */ + NS_IMETHOD CanEdit(PRBool *aCanEdit); NS_IMETHOD GetCurrentTextBlock(nsString *aStr); NS_IMETHOD FirstBlock(); NS_IMETHOD LastBlock(); + NS_IMETHOD FirstSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength); + NS_IMETHOD LastSelectedBlock(PRInt32 *aSelOffset, PRInt32 *aSelLength); NS_IMETHOD PrevBlock(); NS_IMETHOD NextBlock(); - NS_IMETHOD IsDone(); + NS_IMETHOD IsDone(PRBool *aIsDone); NS_IMETHOD SetSelection(PRInt32 aOffset, PRInt32 aLength); NS_IMETHOD DeleteSelection(); NS_IMETHOD InsertText(const nsString *aText); + NS_IMETHOD SetDisplayStyle(TSDDisplayStyle aStyle); /* nsIEditActionListener method implementations. */ nsresult InsertNode(nsIDOMNode * aNode,