From 6a340adf28ca91278212b01c8028418e2fa5dd4f Mon Sep 17 00:00:00 2001 From: "mjudge%netscape.com" Date: Thu, 1 Jun 2000 02:38:13 +0000 Subject: [PATCH] fixing caret usages. hiding caret now is done by giving the stack destructor a caret * not a presshell --- editor/base/nsEditor.cpp | 8 +- editor/base/nsHTMLEditor.cpp | 141 +++++++++++++++---------- editor/base/nsHTMLEditor.h | 4 +- editor/libeditor/base/nsEditor.cpp | 8 +- editor/libeditor/html/nsHTMLEditor.cpp | 141 +++++++++++++++---------- editor/libeditor/html/nsHTMLEditor.h | 4 +- 6 files changed, 188 insertions(+), 118 deletions(-) diff --git a/editor/base/nsEditor.cpp b/editor/base/nsEditor.cpp index 5c442047db57..22b9f88dec6a 100644 --- a/editor/base/nsEditor.cpp +++ b/editor/base/nsEditor.cpp @@ -4991,7 +4991,13 @@ nsresult nsEditor::EndUpdateViewBatch() if (NS_FAILED(rv) || !selCon) return rv?rv:NS_ERROR_FAILURE; - StCaretHider caretHider(selCon); + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + nsCOMPtr caret; + if (ps) + rv = ps->GetCaret(getter_AddRefs(caret)); + if (NS_FAILED(rv) ||!caret) + return rv?rv:NS_ERROR_FAILURE; + StCaretHider caretHider(caret); nsCOMPtrselection; nsresult selectionResult = GetSelection(getter_AddRefs(selection)); diff --git a/editor/base/nsHTMLEditor.cpp b/editor/base/nsHTMLEditor.cpp index ed86caa042d1..11239993130a 100644 --- a/editor/base/nsHTMLEditor.cpp +++ b/editor/base/nsHTMLEditor.cpp @@ -308,40 +308,31 @@ nsHTMLEditor::~nsHTMLEditor() selection->RemoveSelectionListener(listener); } } - // Don't use getDocument here, because we have no way of knowing if - // Init() was ever called. So we need to get the document ourselves, - // if it exists. - if (mDocWeak) + nsCOMPtr erP; + result = GetDOMEventReceiver(getter_AddRefs(erP)); + if (NS_SUCCEEDED(result) && erP) { - nsCOMPtr doc = do_QueryReferent(mDocWeak); - if (doc) - { - nsCOMPtr erP = do_QueryInterface(doc, &result); - if (NS_SUCCEEDED(result) && erP) - { - if (mKeyListenerP) { - erP->RemoveEventListenerByIID(mKeyListenerP, NS_GET_IID(nsIDOMKeyListener)); - } - if (mMouseListenerP) { - erP->RemoveEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener)); - } - if (mTextListenerP) { - erP->RemoveEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener)); - } - if (mCompositionListenerP) { - erP->RemoveEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener)); - } - if (mFocusListenerP) { - erP->RemoveEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener)); - } - if (mDragListenerP) { - erP->RemoveEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener)); - } - } - else - NS_NOTREACHED("~nsTextEditor"); + if (mKeyListenerP) { + erP->RemoveEventListenerByIID(mKeyListenerP, NS_GET_IID(nsIDOMKeyListener)); + } + if (mMouseListenerP) { + erP->RemoveEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener)); + } + if (mTextListenerP) { + erP->RemoveEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener)); + } + if (mCompositionListenerP) { + erP->RemoveEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener)); + } + if (mFocusListenerP) { + erP->RemoveEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener)); + } + if (mDragListenerP) { + erP->RemoveEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener)); } } + else + NS_NOTREACHED("~nsTextEditor"); NS_IF_RELEASE(mTypeInState); } @@ -599,32 +590,9 @@ printf("nsTextEditor.cpp: failed to get TextEvent Listener\n"); return result; } - // get the DOM event receiver - nsCOMPtr domdoc; - nsEditor::GetDocument(getter_AddRefs(domdoc)); nsCOMPtr erP; - nsCOMPtr rootElement; - GetRootElement(getter_AddRefs(rootElement)); - //now hack to make sure we are not anonymous content if we are grab the parent of root element for our observer + result = GetDOMEventReceiver(getter_AddRefs(erP)); - nsCOMPtr content = do_QueryInterface(rootElement); - if (content) - { - nsCOMPtr parent; - if (NS_SUCCEEDED(content->GetParent(*getter_AddRefs(parent))) && parent) - { - PRInt32 index; - if (NS_FAILED(parent->IndexOf(content, index)) || index<0 ) - { - rootElement = do_QueryInterface(parent); //this will put listener on the form element basically - result = rootElement->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(erP)); - } - else - rootElement = 0;//let the event receiver work on the document instead of the root element - } - } - if (!rootElement && domdoc) - result = domdoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(erP)); //end hack if (NS_FAILED(result)) { HandleEventListenerError(); @@ -2189,6 +2157,64 @@ nsresult nsHTMLEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode, return result; } +nsresult +nsHTMLEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver) +{ + if (!aEventReceiver) + return NS_ERROR_NULL_POINTER; + + *aEventReceiver = 0; + + nsCOMPtr rootElement; + + nsresult result = GetRootElement(getter_AddRefs(rootElement)); + + if (NS_FAILED(result)) + return result; + + if (!rootElement) + return NS_ERROR_FAILURE; + + // Now hack to make sure we are not anonymous content. + // If we are grab the parent of root element for our observer. + + nsCOMPtr content = do_QueryInterface(rootElement); + + if (content) + { + nsCOMPtr parent; + if (NS_SUCCEEDED(content->GetParent(*getter_AddRefs(parent))) && parent) + { + PRInt32 index; + if (NS_FAILED(parent->IndexOf(content, index)) || index < 0 ) + { + rootElement = do_QueryInterface(parent); //this will put listener on the form element basically + result = rootElement->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), (void **)aEventReceiver); + } + else + rootElement = 0; // Let the event receiver work on the document instead of the root element + } + } + else + rootElement = 0; + + if (!rootElement && mDocWeak) + { + // Don't use getDocument here, because we have no way of knowing if + // Init() was ever called. So we need to get the document ourselves, + // if it exists. + + nsCOMPtr domdoc = do_QueryReferent(mDocWeak); + + if (!domdoc) + return NS_ERROR_FAILURE; + + result = domdoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), (void **)aEventReceiver); + } + + return result; +} + NS_IMETHODIMP nsHTMLEditor::SetCaretToDocumentStart() { @@ -3592,7 +3618,7 @@ nsHTMLEditor::GetSelectedElement(const nsString& aTagName, nsIDOMElement** aRetu res = enumerator->CurrentItem(getter_AddRefs(currentItem)); if ((NS_SUCCEEDED(res)) && currentItem) { - nsCOMPtr range( do_QueryInterface(currentItem) ); + nsCOMPtr currange( do_QueryInterface(currentItem) ); nsCOMPtr iter; res = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull, NS_GET_IID(nsIContentIterator), @@ -3600,7 +3626,7 @@ nsHTMLEditor::GetSelectedElement(const nsString& aTagName, nsIDOMElement** aRetu // XXX: ERROR_HANDLING XPCOM usage if ((NS_SUCCEEDED(res)) && iter) { - iter->Init(range); + iter->Init(currange); // loop through the content iterator for each content node nsCOMPtr content; while (NS_ENUMERATOR_FALSE == iter->IsDone()) @@ -5131,6 +5157,7 @@ NS_IMETHODIMP nsHTMLEditor::OutputToString(nsString& aOutputString, wc = (PRUint32)wrapColumn; } } + printf("Editor: getting output with wrapcol = %d (GetBodyWrapWidth returned %d\n", wc, wrapColumn); return selection->ToString(aFormatType, aFlags, wc, aOutputString); } } diff --git a/editor/base/nsHTMLEditor.h b/editor/base/nsHTMLEditor.h index da37e64dc4c9..48d3045f7a5d 100644 --- a/editor/base/nsHTMLEditor.h +++ b/editor/base/nsHTMLEditor.h @@ -42,6 +42,7 @@ class nsIDOMKeyEvent; class nsITransferable; +class nsIDOMEventReceiver; /** * The HTML editor implementation.
@@ -545,7 +546,8 @@ protected: nsresult GetFirstEditableChild( nsIDOMNode *aNode, nsCOMPtr *aOutFirstChild); nsresult GetLastEditableChild( nsIDOMNode *aNode, nsCOMPtr *aOutLastChild); - + nsresult GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver); + // Data members protected: diff --git a/editor/libeditor/base/nsEditor.cpp b/editor/libeditor/base/nsEditor.cpp index 5c442047db57..22b9f88dec6a 100644 --- a/editor/libeditor/base/nsEditor.cpp +++ b/editor/libeditor/base/nsEditor.cpp @@ -4991,7 +4991,13 @@ nsresult nsEditor::EndUpdateViewBatch() if (NS_FAILED(rv) || !selCon) return rv?rv:NS_ERROR_FAILURE; - StCaretHider caretHider(selCon); + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + nsCOMPtr caret; + if (ps) + rv = ps->GetCaret(getter_AddRefs(caret)); + if (NS_FAILED(rv) ||!caret) + return rv?rv:NS_ERROR_FAILURE; + StCaretHider caretHider(caret); nsCOMPtrselection; nsresult selectionResult = GetSelection(getter_AddRefs(selection)); diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp index ed86caa042d1..11239993130a 100644 --- a/editor/libeditor/html/nsHTMLEditor.cpp +++ b/editor/libeditor/html/nsHTMLEditor.cpp @@ -308,40 +308,31 @@ nsHTMLEditor::~nsHTMLEditor() selection->RemoveSelectionListener(listener); } } - // Don't use getDocument here, because we have no way of knowing if - // Init() was ever called. So we need to get the document ourselves, - // if it exists. - if (mDocWeak) + nsCOMPtr erP; + result = GetDOMEventReceiver(getter_AddRefs(erP)); + if (NS_SUCCEEDED(result) && erP) { - nsCOMPtr doc = do_QueryReferent(mDocWeak); - if (doc) - { - nsCOMPtr erP = do_QueryInterface(doc, &result); - if (NS_SUCCEEDED(result) && erP) - { - if (mKeyListenerP) { - erP->RemoveEventListenerByIID(mKeyListenerP, NS_GET_IID(nsIDOMKeyListener)); - } - if (mMouseListenerP) { - erP->RemoveEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener)); - } - if (mTextListenerP) { - erP->RemoveEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener)); - } - if (mCompositionListenerP) { - erP->RemoveEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener)); - } - if (mFocusListenerP) { - erP->RemoveEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener)); - } - if (mDragListenerP) { - erP->RemoveEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener)); - } - } - else - NS_NOTREACHED("~nsTextEditor"); + if (mKeyListenerP) { + erP->RemoveEventListenerByIID(mKeyListenerP, NS_GET_IID(nsIDOMKeyListener)); + } + if (mMouseListenerP) { + erP->RemoveEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener)); + } + if (mTextListenerP) { + erP->RemoveEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener)); + } + if (mCompositionListenerP) { + erP->RemoveEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener)); + } + if (mFocusListenerP) { + erP->RemoveEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener)); + } + if (mDragListenerP) { + erP->RemoveEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener)); } } + else + NS_NOTREACHED("~nsTextEditor"); NS_IF_RELEASE(mTypeInState); } @@ -599,32 +590,9 @@ printf("nsTextEditor.cpp: failed to get TextEvent Listener\n"); return result; } - // get the DOM event receiver - nsCOMPtr domdoc; - nsEditor::GetDocument(getter_AddRefs(domdoc)); nsCOMPtr erP; - nsCOMPtr rootElement; - GetRootElement(getter_AddRefs(rootElement)); - //now hack to make sure we are not anonymous content if we are grab the parent of root element for our observer + result = GetDOMEventReceiver(getter_AddRefs(erP)); - nsCOMPtr content = do_QueryInterface(rootElement); - if (content) - { - nsCOMPtr parent; - if (NS_SUCCEEDED(content->GetParent(*getter_AddRefs(parent))) && parent) - { - PRInt32 index; - if (NS_FAILED(parent->IndexOf(content, index)) || index<0 ) - { - rootElement = do_QueryInterface(parent); //this will put listener on the form element basically - result = rootElement->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(erP)); - } - else - rootElement = 0;//let the event receiver work on the document instead of the root element - } - } - if (!rootElement && domdoc) - result = domdoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(erP)); //end hack if (NS_FAILED(result)) { HandleEventListenerError(); @@ -2189,6 +2157,64 @@ nsresult nsHTMLEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode, return result; } +nsresult +nsHTMLEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver) +{ + if (!aEventReceiver) + return NS_ERROR_NULL_POINTER; + + *aEventReceiver = 0; + + nsCOMPtr rootElement; + + nsresult result = GetRootElement(getter_AddRefs(rootElement)); + + if (NS_FAILED(result)) + return result; + + if (!rootElement) + return NS_ERROR_FAILURE; + + // Now hack to make sure we are not anonymous content. + // If we are grab the parent of root element for our observer. + + nsCOMPtr content = do_QueryInterface(rootElement); + + if (content) + { + nsCOMPtr parent; + if (NS_SUCCEEDED(content->GetParent(*getter_AddRefs(parent))) && parent) + { + PRInt32 index; + if (NS_FAILED(parent->IndexOf(content, index)) || index < 0 ) + { + rootElement = do_QueryInterface(parent); //this will put listener on the form element basically + result = rootElement->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), (void **)aEventReceiver); + } + else + rootElement = 0; // Let the event receiver work on the document instead of the root element + } + } + else + rootElement = 0; + + if (!rootElement && mDocWeak) + { + // Don't use getDocument here, because we have no way of knowing if + // Init() was ever called. So we need to get the document ourselves, + // if it exists. + + nsCOMPtr domdoc = do_QueryReferent(mDocWeak); + + if (!domdoc) + return NS_ERROR_FAILURE; + + result = domdoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), (void **)aEventReceiver); + } + + return result; +} + NS_IMETHODIMP nsHTMLEditor::SetCaretToDocumentStart() { @@ -3592,7 +3618,7 @@ nsHTMLEditor::GetSelectedElement(const nsString& aTagName, nsIDOMElement** aRetu res = enumerator->CurrentItem(getter_AddRefs(currentItem)); if ((NS_SUCCEEDED(res)) && currentItem) { - nsCOMPtr range( do_QueryInterface(currentItem) ); + nsCOMPtr currange( do_QueryInterface(currentItem) ); nsCOMPtr iter; res = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull, NS_GET_IID(nsIContentIterator), @@ -3600,7 +3626,7 @@ nsHTMLEditor::GetSelectedElement(const nsString& aTagName, nsIDOMElement** aRetu // XXX: ERROR_HANDLING XPCOM usage if ((NS_SUCCEEDED(res)) && iter) { - iter->Init(range); + iter->Init(currange); // loop through the content iterator for each content node nsCOMPtr content; while (NS_ENUMERATOR_FALSE == iter->IsDone()) @@ -5131,6 +5157,7 @@ NS_IMETHODIMP nsHTMLEditor::OutputToString(nsString& aOutputString, wc = (PRUint32)wrapColumn; } } + printf("Editor: getting output with wrapcol = %d (GetBodyWrapWidth returned %d\n", wc, wrapColumn); return selection->ToString(aFormatType, aFlags, wc, aOutputString); } } diff --git a/editor/libeditor/html/nsHTMLEditor.h b/editor/libeditor/html/nsHTMLEditor.h index da37e64dc4c9..48d3045f7a5d 100644 --- a/editor/libeditor/html/nsHTMLEditor.h +++ b/editor/libeditor/html/nsHTMLEditor.h @@ -42,6 +42,7 @@ class nsIDOMKeyEvent; class nsITransferable; +class nsIDOMEventReceiver; /** * The HTML editor implementation.
@@ -545,7 +546,8 @@ protected: nsresult GetFirstEditableChild( nsIDOMNode *aNode, nsCOMPtr *aOutFirstChild); nsresult GetLastEditableChild( nsIDOMNode *aNode, nsCOMPtr *aOutLastChild); - + nsresult GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver); + // Data members protected: