diff --git a/mozilla/content/base/src/nsDocument.cpp b/mozilla/content/base/src/nsDocument.cpp index aeea3835dbd..56c59d20062 100644 --- a/mozilla/content/base/src/nsDocument.cpp +++ b/mozilla/content/base/src/nsDocument.cpp @@ -798,6 +798,12 @@ nsresult nsDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } + if (aIID.Equals(nsCOMTypeInfo::GetIID())) { + nsISupportsWeakReference* tmp = this; + *aInstancePtr = (void*) tmp; + NS_ADDREF_THIS(); + return NS_OK; + } static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); if (aIID.Equals(kISupportsIID)) { nsIDocument* tmp = this; diff --git a/mozilla/content/base/src/nsDocument.h b/mozilla/content/base/src/nsDocument.h index 518b7214f39..ab549013c45 100644 --- a/mozilla/content/base/src/nsDocument.h +++ b/mozilla/content/base/src/nsDocument.h @@ -19,6 +19,7 @@ #define nsDocument_h___ #include "nsIDocument.h" +#include "nsWeakReference.h" #include "nsVoidArray.h" #include "nsIDOMDocument.h" #include "nsIDOMNSDocument.h" @@ -105,7 +106,8 @@ class nsDocument : public nsIDocument, public nsIDiskDocument, public nsIScriptObjectOwner, public nsIDOMEventCapturer, - public nsIJSScriptObject + public nsIJSScriptObject, + public nsSupportsWeakReference { public: NS_DECL_ISUPPORTS diff --git a/mozilla/editor/base/IMETextTxn.cpp b/mozilla/editor/base/IMETextTxn.cpp index 61d60160d96..c3b488329a7 100644 --- a/mozilla/editor/base/IMETextTxn.cpp +++ b/mozilla/editor/base/IMETextTxn.cpp @@ -43,21 +43,21 @@ IMETextTxn::IMETextTxn() IMETextTxn::~IMETextTxn() { - mRangeList = do_QueryInterface(nsnull); + mRangeList = do_QueryInterface(nsnull); } -NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement, - PRUint32 aOffset, - PRUint32 aReplaceLength, - nsIPrivateTextRangeList* aTextRangeList, - const nsString &aStringToInsert, - nsIPresShell *aPresShell) +NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + PRUint32 aReplaceLength, + nsIPrivateTextRangeList *aTextRangeList, + const nsString &aStringToInsert, + nsWeakPtr aPresShellWeak) { mElement = do_QueryInterface(aElement); mOffset = aOffset; mReplaceLength = aReplaceLength; mStringToInsert = aStringToInsert; - mPresShell = aPresShell; + mPresShellWeak = aPresShellWeak; mRangeList = do_QueryInterface(aTextRangeList); mFixed = PR_FALSE; return NS_OK; @@ -70,22 +70,25 @@ NS_IMETHODIMP IMETextTxn::Do(void) printf("Do IME Text element = %p\n", mElement.get()); #endif - // advance caret: This requires the presentation shell to get the selection. - nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - NS_ASSERTION(selection,"Could not get selection in IMEtextTxn::Do\n"); - if (NS_SUCCEEDED(result) && selection) { - if (mReplaceLength==0) { - result = mElement->InsertData(mOffset,mStringToInsert); - } else { - result = mElement->ReplaceData(mOffset,mReplaceLength,mStringToInsert); - } - if (NS_SUCCEEDED(result)) { - result = CollapseTextSelection(); - } - } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; - return result; + // advance caret: This requires the presentation shell to get the selection. + nsCOMPtr selection; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + NS_ASSERTION(selection,"Could not get selection in IMEtextTxn::Do\n"); + if (NS_SUCCEEDED(result) && selection) { + if (mReplaceLength==0) { + result = mElement->InsertData(mOffset,mStringToInsert); + } else { + result = mElement->ReplaceData(mOffset,mReplaceLength,mStringToInsert); + } + if (NS_SUCCEEDED(result)) { + result = CollapseTextSelection(); + } + } + + return result; } NS_IMETHODIMP IMETextTxn::Undo(void) @@ -94,13 +97,16 @@ NS_IMETHODIMP IMETextTxn::Undo(void) printf("Undo IME Text element = %p\n", mElement.get()); #endif + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result; PRUint32 length = mStringToInsert.Length(); result = mElement->DeleteData(mOffset, length); if (NS_SUCCEEDED(result)) { // set the selection to the insertion point where the string was removed nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { result = selection->Collapse(mElement, mOffset); NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert."); @@ -111,67 +117,67 @@ NS_IMETHODIMP IMETextTxn::Undo(void) NS_IMETHODIMP IMETextTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction) { - nsresult result; + nsresult result; #ifdef DEBUG_TAGUE printf("Merge IME Text element = %p\n", mElement.get()); #endif - // - // check to make sure we have valid return pointers - // - if ((nsnull==aDidMerge) && (nsnull==aTransaction)) - { - return NS_OK; - } + // + // check to make sure we have valid return pointers + // + if ((nsnull==aDidMerge) && (nsnull==aTransaction)) + { + return NS_OK; + } - // - // check to make sure we aren't fixed, if we are then nothing get's absorbed - // - if (mFixed) { - *aDidMerge = PR_FALSE; - return NS_OK; - } + // + // check to make sure we aren't fixed, if we are then nothing get's absorbed + // + if (mFixed) { + *aDidMerge = PR_FALSE; + return NS_OK; + } - // - // if aTransaction is another IMETextTxn then absorbe it - // - IMETextTxn* otherTxn = nsnull; - result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn); - if (otherTxn && result==NS_OK) - { - // - // we absorbe the next IME transaction by adopting it's insert string as our own - // - nsIPrivateTextRangeList* newTextRangeList; - otherTxn->GetData(mStringToInsert,&newTextRangeList); - mRangeList = do_QueryInterface(newTextRangeList); - *aDidMerge = PR_TRUE; + // + // if aTransaction is another IMETextTxn then absorbe it + // + IMETextTxn* otherTxn = nsnull; + result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn); + if (otherTxn && result==NS_OK) + { + // + // we absorbe the next IME transaction by adopting it's insert string as our own + // + nsIPrivateTextRangeList* newTextRangeList; + otherTxn->GetData(mStringToInsert,&newTextRangeList); + mRangeList = do_QueryInterface(newTextRangeList); + *aDidMerge = PR_TRUE; #ifdef DEBUG_TAGUE - printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction); + printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction); #endif - NS_RELEASE(otherTxn); - return NS_OK; - } + NS_RELEASE(otherTxn); + return NS_OK; + } - // - // second possible case is that we have a commit transaction - // - IMECommitTxn* commitTxn = nsnull; - result = aTransaction->QueryInterface(IMECommitTxn::GetCID(),(void**)&commitTxn); - if (commitTxn && result==NS_OK) - { - (void)CollapseTextSelectionOnCommit(); - mFixed = PR_TRUE; - *aDidMerge = PR_TRUE; // absorbe the commit transaction + // + // second possible case is that we have a commit transaction + // + IMECommitTxn* commitTxn = nsnull; + result = aTransaction->QueryInterface(IMECommitTxn::GetCID(),(void**)&commitTxn); + if (commitTxn && result==NS_OK) + { + (void)CollapseTextSelectionOnCommit(); + mFixed = PR_TRUE; + *aDidMerge = PR_TRUE; // absorbe the commit transaction #ifdef DEBUG_TAGUE - printf("IMETextTxn assimilated IMECommitTxn%p\n", aTransaction); + printf("IMETextTxn assimilated IMECommitTxn%p\n", aTransaction); #endif - NS_RELEASE(commitTxn); - return NS_OK; - } + NS_RELEASE(commitTxn); + return NS_OK; + } - *aDidMerge = PR_FALSE; - return NS_OK; + *aDidMerge = PR_FALSE; + return NS_OK; } NS_IMETHODIMP IMETextTxn::Write(nsIOutputStream *aOutputStream) @@ -226,84 +232,88 @@ NS_IMETHODIMP IMETextTxn::GetData(nsString& aResult,nsIPrivateTextRangeList** aT NS_IMETHODIMP IMETextTxn::CollapseTextSelection(void) { - nsresult result; - PRBool haveSelectedRange, haveCaretPosition; - PRUint16 textRangeListLength,selectionStart,selectionEnd, - textRangeType, caretPosition, i; - nsIPrivateTextRange* textRange; + nsresult result; + PRBool haveSelectedRange, haveCaretPosition; + PRUint16 textRangeListLength,selectionStart,selectionEnd, + textRangeType, caretPosition, i; + nsIPrivateTextRange* textRange; - haveSelectedRange = PR_FALSE; - haveCaretPosition = PR_FALSE; - + haveSelectedRange = PR_FALSE; + haveCaretPosition = PR_FALSE; + #ifdef DEBUG_tague - PRUint16 listlen,start,stop,type; - nsIPrivateTextRange* rangePtr; - result = mRangeList->GetLength(&listlen); - printf("nsIPrivateTextRangeList[%p]\n",mRangeList); - for (i=0;iItem(i,&rangePtr); - rangePtr->GetRangeStart(&start); - rangePtr->GetRangeEnd(&stop); - rangePtr->GetRangeType(&type); - printf("range[%d] start=%d end=%d type=",i,start,stop,type); - if (type==nsIPrivateTextRange::TEXTRANGE_RAWINPUT) printf("TEXTRANGE_RAWINPUT\n"); - if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT) printf("TEXTRANGE_SELECTEDRAWTEXT\n"); - if (type==nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT) printf("TEXTRANGE_CONVERTEDTEXT\n"); - if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) printf("TEXTRANGE_SELECTEDCONVERTEDTEXT\n"); - } + PRUint16 listlen,start,stop,type; + nsIPrivateTextRange* rangePtr; + result = mRangeList->GetLength(&listlen); + printf("nsIPrivateTextRangeList[%p]\n",mRangeList); + for (i=0;iItem(i,&rangePtr); + rangePtr->GetRangeStart(&start); + rangePtr->GetRangeEnd(&stop); + rangePtr->GetRangeType(&type); + printf("range[%d] start=%d end=%d type=",i,start,stop,type); + if (type==nsIPrivateTextRange::TEXTRANGE_RAWINPUT) printf("TEXTRANGE_RAWINPUT\n"); + if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT) printf("TEXTRANGE_SELECTEDRAWTEXT\n"); + if (type==nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT) printf("TEXTRANGE_CONVERTEDTEXT\n"); + if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) printf("TEXTRANGE_SELECTEDCONVERTEDTEXT\n"); + } #endif - - // - // run through the text range list - // - result = mRangeList->GetLength(&textRangeListLength); - if (NS_SUCCEEDED(result)) - { - for(i=0;iItem(i,&textRange); - if (NS_SUCCEEDED(result)) - { - result = textRange->GetRangeType(&textRangeType); - if (textRangeType==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) - { - haveSelectedRange = PR_TRUE; - textRange->GetRangeStart(&selectionStart); - textRange->GetRangeEnd(&selectionEnd); - } - if (textRangeType==nsIPrivateTextRange::TEXTRANGE_CARETPOSITION) - { - haveCaretPosition = PR_TRUE; - textRange->GetRangeStart(&caretPosition); - } - } - } - } + + // + // run through the text range list + // + result = mRangeList->GetLength(&textRangeListLength); + if (NS_SUCCEEDED(result)) + { + for(i=0;iItem(i,&textRange); + if (NS_SUCCEEDED(result)) + { + result = textRange->GetRangeType(&textRangeType); + if (textRangeType==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) + { + haveSelectedRange = PR_TRUE; + textRange->GetRangeStart(&selectionStart); + textRange->GetRangeEnd(&selectionEnd); + } + if (textRangeType==nsIPrivateTextRange::TEXTRANGE_CARETPOSITION) + { + haveCaretPosition = PR_TRUE; + textRange->GetRangeStart(&caretPosition); + } + } + } + } - nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_SUCCEEDED(result) && selection){ - if (haveSelectedRange) { - result = selection->Collapse(mElement,mOffset+selectionStart); - result = selection->Extend(mElement,mOffset+selectionEnd); - } else { - if (haveCaretPosition) - result = selection->Collapse(mElement,mOffset+caretPosition); - else - result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); - } - } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr selection; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_SUCCEEDED(result) && selection){ + if (haveSelectedRange) { + result = selection->Collapse(mElement,mOffset+selectionStart); + result = selection->Extend(mElement,mOffset+selectionEnd); + } else { + if (haveCaretPosition) + result = selection->Collapse(mElement,mOffset+caretPosition); + else + result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); + } + } - return result; + return result; } NS_IMETHODIMP IMETextTxn::CollapseTextSelectionOnCommit(void) { - nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_SUCCEEDED(result) && selection){ - result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); - } + nsCOMPtr selection; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_SUCCEEDED(result) && selection){ + result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); + } - return result; + return result; } diff --git a/mozilla/editor/base/IMETextTxn.h b/mozilla/editor/base/IMETextTxn.h index 6882505237b..9e4a70dd167 100644 --- a/mozilla/editor/base/IMETextTxn.h +++ b/mozilla/editor/base/IMETextTxn.h @@ -23,6 +23,7 @@ #include "nsIDOMCharacterData.h" #include "nsIPrivateTextRange.h" #include "nsCOMPtr.h" +#include "nsWeakPtr.h" // {D4D25721-2813-11d3-9EA3-0060089FE59B} #define IME_TEXT_TXN_CID \ @@ -61,7 +62,7 @@ public: PRUint32 aReplaceLength, nsIPrivateTextRangeList* aTextRangeList, const nsString& aString, - nsIPresShell* aPresShell); + nsWeakPtr aPresShell); private: @@ -112,7 +113,7 @@ protected: nsCOMPtr mRangeList; /** the presentation shell, which we'll need to get the selection */ - nsIPresShell* mPresShell; + nsWeakPtr mPresShellWeak; // use a weak reference PRBool mFixed; diff --git a/mozilla/editor/base/InsertTextTxn.cpp b/mozilla/editor/base/InsertTextTxn.cpp index 5f3a9442a31..70e3a539f31 100644 --- a/mozilla/editor/base/InsertTextTxn.cpp +++ b/mozilla/editor/base/InsertTextTxn.cpp @@ -52,7 +52,7 @@ InsertTextTxn::~InsertTextTxn() NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement, PRUint32 aOffset, const nsString &aStringToInsert, - nsIPresShell *aPresShell) + nsWeakPtr aPresShellWeak) { #if 0 //def DEBUG_cmanske nsString text; @@ -62,30 +62,32 @@ NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement, printf("\n"); #endif - NS_ASSERTION(aElement&&aPresShell, "bad args"); - if (!aElement || !aPresShell) return NS_ERROR_NULL_POINTER; + NS_ASSERTION(aElement && aPresShellWeak, "bad args"); + if (!aElement || !aPresShellWeak) return NS_ERROR_NULL_POINTER; mElement = do_QueryInterface(aElement); mOffset = aOffset; mStringToInsert = aStringToInsert; - mPresShell = aPresShell; + mPresShellWeak = aPresShellWeak; return NS_OK; } NS_IMETHODIMP InsertTextTxn::Do(void) { if (gNoisy) { printf("Do Insert Text element = %p\n", mElement.get()); } - NS_ASSERTION(mElement && mPresShell, "bad state"); - if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + NS_ASSERTION(mElement && mPresShellWeak, "bad state"); + if (!mElement || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; // advance caret: This requires the presentation shell to get the selection. nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_FAILED(result)) return result; - if (!selection) return NS_ERROR_NULL_POINTER; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_FAILED(result)) return result; + if (!selection) return NS_ERROR_NULL_POINTER; result = mElement->InsertData(mOffset, mStringToInsert); if (NS_SUCCEEDED(result)) - { + { result = selection->Collapse(mElement, mOffset+mStringToInsert.Length()); NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert."); } @@ -95,8 +97,10 @@ NS_IMETHODIMP InsertTextTxn::Do(void) NS_IMETHODIMP InsertTextTxn::Undo(void) { if (gNoisy) { printf("Undo Insert Text element = %p\n", mElement.get()); } - NS_ASSERTION(mElement && mPresShell, "bad state"); - if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + NS_ASSERTION(mElement && mPresShellWeak, "bad state"); + if (!mElement || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; nsresult result; PRUint32 length = mStringToInsert.Length(); @@ -104,9 +108,9 @@ NS_IMETHODIMP InsertTextTxn::Undo(void) if (NS_SUCCEEDED(result)) { // set the selection to the insertion point where the string was removed nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_FAILED(result)) return result; - if (!selection) return NS_ERROR_NULL_POINTER; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_FAILED(result)) return result; + if (!selection) return NS_ERROR_NULL_POINTER; result = selection->Collapse(mElement, mOffset); NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert."); } diff --git a/mozilla/editor/base/InsertTextTxn.h b/mozilla/editor/base/InsertTextTxn.h index e34272405f7..72995eb7372 100644 --- a/mozilla/editor/base/InsertTextTxn.h +++ b/mozilla/editor/base/InsertTextTxn.h @@ -22,6 +22,7 @@ #include "EditTxn.h" #include "nsIDOMCharacterData.h" #include "nsCOMPtr.h" +#include "nsWeakPtr.h" #define INSERT_TEXT_TXN_CID \ {/* 93276f00-ab2c-11d2-8f4b-006008159b0c*/ \ @@ -55,7 +56,7 @@ public: NS_IMETHOD Init(nsIDOMCharacterData *aElement, PRUint32 aOffset, const nsString& aString, - nsIPresShell* aPresShell); + nsWeakPtr aPresShellWeak); private: @@ -101,7 +102,7 @@ protected: nsString mStringToInsert; /** the presentation shell, which we'll need to get the selection */ - nsIPresShell* mPresShell; + nsWeakPtr mPresShellWeak; // weak reference to the nsIPresShell friend class TransactionFactory; diff --git a/mozilla/editor/base/nsEditor.cpp b/mozilla/editor/base/nsEditor.cpp index 607cb18d299..b88c1d1eb1b 100644 --- a/mozilla/editor/base/nsEditor.cpp +++ b/mozilla/editor/base/nsEditor.cpp @@ -142,12 +142,12 @@ PRInt32 nsEditor::gInstanceCount = 0; //class implementations are in order they are declared in nsEditor.h nsEditor::nsEditor() -: mPresShell(nsnull) +: mPresShellWeak(nsnull) , mViewManager(nsnull) , mUpdateCount(0) , mActionListeners(nsnull) , mDocDirtyState(-1) -, mDoc(nsnull) +, mDocWeak(nsnull) , mPrefs(nsnull) { //initialize member variables here @@ -232,12 +232,14 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) return NS_ERROR_NULL_POINTER; mFlags = aFlags; - mDoc = aDoc; - mPresShell = aPresShell; // we don't addref the pres shell + mDocWeak = getter_AddRefs( NS_GetWeakReference(aDoc) ); // weak reference to doc + mPresShellWeak = getter_AddRefs( NS_GetWeakReference(aPresShell) ); // weak reference to pres shell + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; // disable links nsCOMPtr context; - mPresShell->GetPresContext(getter_AddRefs(context)); + ps->GetPresContext(getter_AddRefs(context)); if (!context) return NS_ERROR_NULL_POINTER; context->SetLinkHandler(0); @@ -251,11 +253,11 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) if (NS_FAILED(result)) { return result; } if (!mEditProperty) {return NS_ERROR_NULL_POINTER;} - mPresShell->GetViewManager(&mViewManager); + ps->GetViewManager(&mViewManager); if (!mViewManager) {return NS_ERROR_NULL_POINTER;} mViewManager->Release(); //we want a weak link - mPresShell->SetDisplayNonTextSelection(PR_TRUE);//we want to see all the selection reflected to user + ps->SetDisplayNonTextSelection(PR_TRUE);//we want to see all the selection reflected to user mUpdateCount=0; InsertTextTxn::ClassInit(); @@ -269,11 +271,11 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) /* Show the caret */ // XXX: I suppose it's legal to fail to get a caret, so don't propogate the // result of GetCaret - nsCOMPtr caret; - if (NS_SUCCEEDED(mPresShell->GetCaret(getter_AddRefs(caret))) && caret) + nsCOMPtr caret; + if (NS_SUCCEEDED(ps->GetCaret(getter_AddRefs(caret))) && caret) { - caret->SetCaretVisible(PR_TRUE); - caret->SetCaretReadOnly(PR_FALSE); + caret->SetCaretVisible(PR_TRUE); + caret->SetCaretReadOnly(PR_FALSE); } // NOTE: We don't fail if we can't get prefs or string bundles @@ -317,7 +319,7 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) // Set the selection to the beginning: BeginningOfDocument(); - NS_POSTCONDITION(mDoc && mPresShell, "bad state"); + NS_POSTCONDITION(mDocWeak && mPresShellWeak, "bad state"); return NS_OK; } @@ -339,10 +341,11 @@ nsEditor::GetDocument(nsIDOMDocument **aDoc) if (!aDoc) return NS_ERROR_NULL_POINTER; *aDoc = nsnull; // init out param - NS_PRECONDITION(mDoc, "bad state, null mDoc"); - if (!mDoc) - return NS_ERROR_NOT_INITIALIZED; - return mDoc->QueryInterface(nsIDOMDocument::GetIID(), (void **)aDoc); + NS_PRECONDITION(mDocWeak, "bad state, mDocWeak weak pointer not initialized"); + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + return doc->QueryInterface(nsIDOMDocument::GetIID(), (void **)aDoc); } @@ -352,10 +355,11 @@ nsEditor::GetPresShell(nsIPresShell **aPS) if (!aPS) return NS_ERROR_NULL_POINTER; *aPS = nsnull; // init out param - NS_PRECONDITION(mPresShell, "bad state, null mPresShell"); - if (!mPresShell) - return NS_ERROR_NOT_INITIALIZED; - return mPresShell->QueryInterface(nsIPresShell::GetIID(), (void **)aPS); + NS_PRECONDITION(mPresShellWeak, "bad state, null mPresShellWeak"); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + return ps->QueryInterface(nsIPresShell::GetIID(), (void **)aPS); } @@ -365,7 +369,10 @@ nsEditor::GetSelection(nsIDOMSelection **aSelection) if (!aSelection) return NS_ERROR_NULL_POINTER; *aSelection = nsnull; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, aSelection); // does an addref + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, aSelection); // does an addref return result; } @@ -373,8 +380,8 @@ static NS_DEFINE_IID(kCFileWidgetCID, NS_FILEWIDGET_CID); NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy) { - nsresult rv = NS_OK; - + nsresult rv = NS_OK; + // get the document nsCOMPtr doc; rv = GetDocument(getter_AddRefs(doc)); @@ -389,29 +396,29 @@ NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy) // dialog. // find out if the doc already has a fileSpec associated with it. - nsFileSpec docFileSpec; + nsFileSpec docFileSpec; PRBool mustShowFileDialog = saveAs || (diskDoc->GetFileSpec(docFileSpec) == NS_ERROR_NOT_INITIALIZED); PRBool replacing = !saveAs; if (mustShowFileDialog) { - nsCOMPtr fileWidget; + nsCOMPtr fileWidget; rv = nsComponentManager::CreateInstance(kCFileWidgetCID, nsnull, nsIFileWidget::GetIID(), getter_AddRefs(fileWidget)); if (NS_SUCCEEDED(rv) && fileWidget) { - nsAutoString promptString("Save this document as:"); // XXX i18n, l10n - nsFileDlgResults dialogResult; - dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec); - if (dialogResult == nsFileDlgResults_Cancel) - return NS_OK; - - replacing = (dialogResult == nsFileDlgResults_Replace); - } - else - { - NS_ASSERTION(0, "Failed to get file widget"); - return rv; - } + nsAutoString promptString("Save this document as:"); // XXX i18n, l10n + nsFileDlgResults dialogResult; + dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec); + if (dialogResult == nsFileDlgResults_Cancel) + return NS_OK; + + replacing = (dialogResult == nsFileDlgResults_Replace); + } + else + { + NS_ASSERTION(0, "Failed to get file widget"); + return rv; + } } nsAutoString useDocCharset(""); @@ -520,7 +527,7 @@ nsEditor::Undo(PRUint32 aCount) if (NS_SUCCEEDED(result)) result = DoAfterUndoTransaction(); - + if (NS_FAILED(result)) break; } @@ -622,10 +629,13 @@ nsEditor::EndTransaction() // XXX: the rule system should tell us which node to select all on (ie, the root, or the body) NS_IMETHODIMP nsEditor::SelectAll() { - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { result = SelectEntireDocument(selection); @@ -635,15 +645,20 @@ NS_IMETHODIMP nsEditor::SelectAll() NS_IMETHODIMP nsEditor::BeginningOfDocument() { - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { nsCOMPtr nodeList; nsAutoString bodyTag = "body"; - result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if ((NS_SUCCEEDED(result)) && nodeList) { PRUint32 count; @@ -668,15 +683,20 @@ NS_IMETHODIMP nsEditor::BeginningOfDocument() NS_IMETHODIMP nsEditor::EndOfDocument() { - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { nsCOMPtr nodeList; nsAutoString bodyTag = "body"; - result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if ((NS_SUCCEEDED(result)) && nodeList) { PRUint32 count; @@ -741,10 +761,10 @@ nsEditor::GetDocumentCharacterSet(PRUnichar** characterSet) { presShell->GetDocument(getter_AddRefs(doc)); if (doc ) { - rv = doc->GetDocumentCharacterSet(character_set); - if (NS_SUCCEEDED(rv)) *characterSet=character_set.ToNewUnicode(); - return rv; - } + rv = doc->GetDocumentCharacterSet(character_set); + if (NS_SUCCEEDED(rv)) *characterSet=character_set.ToNewUnicode(); + return rv; + } } return rv; @@ -766,8 +786,8 @@ nsEditor::SetDocumentCharacterSet(const PRUnichar* characterSet) { presShell->GetDocument(getter_AddRefs(doc)); if (doc ) { - return doc->SetDocumentCharacterSet(character_set); - } + return doc->SetDocumentCharacterSet(character_set); + } } return rv; @@ -1140,7 +1160,9 @@ nsEditor::DebugDumpContent() const nsCOMPtrcontent; nsCOMPtrnodeList; nsAutoString bodyTag = "body"; - mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if (nodeList) { PRUint32 count; @@ -1179,97 +1201,103 @@ NS_IMETHODIMP nsEditor::BeginComposition(void) { #ifdef DEBUG_tague - printf("nsEditor::StartComposition\n"); + printf("nsEditor::StartComposition\n"); #endif - nsresult result; - PRInt32 offset; - nsCOMPtr selection; - nsCOMPtr nodeAsText; - - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if ((NS_SUCCEEDED(result)) && selection) - { - result = NS_ERROR_UNEXPECTED; - nsCOMPtr enumerator; + nsresult result; + PRInt32 offset; + nsCOMPtr selection; + nsCOMPtr nodeAsText; + + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if ((NS_SUCCEEDED(result)) && selection) + { + result = NS_ERROR_UNEXPECTED; + nsCOMPtr enumerator; result = selection->GetEnumerator(getter_AddRefs(enumerator)); if (NS_SUCCEEDED(result) && enumerator) - { - enumerator->First(); - nsCOMPtr currentItem; + { + enumerator->First(); + nsCOMPtr currentItem; result = enumerator->CurrentItem(getter_AddRefs(currentItem)); - if ((NS_SUCCEEDED(result)) && (currentItem)) - { - result = NS_ERROR_UNEXPECTED; - nsCOMPtr range(do_QueryInterface(currentItem)); - if (range) - { - nsCOMPtr node; - result = range->GetStartParent(getter_AddRefs(node)); - if ((NS_SUCCEEDED(result)) && (node)) - { - nodeAsText = do_QueryInterface(node); - range->GetStartOffset(&offset); - if (!nodeAsText) { - result = NS_ERROR_EDITOR_NO_TEXTNODE; - } - } - } - } - else - { - result = NS_ERROR_EDITOR_NO_SELECTION; - } - } - } + if ((NS_SUCCEEDED(result)) && (currentItem)) + { + result = NS_ERROR_UNEXPECTED; + nsCOMPtr range(do_QueryInterface(currentItem)); + if (range) + { + nsCOMPtr node; + result = range->GetStartParent(getter_AddRefs(node)); + if ((NS_SUCCEEDED(result)) && (node)) + { + nodeAsText = do_QueryInterface(node); + range->GetStartOffset(&offset); + if (!nodeAsText) { + result = NS_ERROR_EDITOR_NO_TEXTNODE; + } + } + } + } + else + { + result = NS_ERROR_EDITOR_NO_SELECTION; + } + } + } - if (NS_SUCCEEDED(result) && nodeAsText) - { - // - // store the information needed to construct IME transactions for this composition - // - mIMETextNode = nodeAsText; - mIMETextOffset = offset; - mIMEBufferLength = 0; - } + if (NS_SUCCEEDED(result) && nodeAsText) + { + // + // store the information needed to construct IME transactions for this composition + // + mIMETextNode = nodeAsText; + mIMETextOffset = offset; + mIMEBufferLength = 0; + } - return result; + return result; } NS_IMETHODIMP nsEditor::EndComposition(void) { - nsresult result; - IMECommitTxn *commitTxn; - - // - // create the commit transaction..we can do it directly from the transaction mgr - // + nsresult result; + IMECommitTxn *commitTxn; + + // + // create the commit transaction..we can do it directly from the transaction mgr + // result = TransactionFactory::GetNewTransaction(IMECommitTxn::GetCID(), (EditTxn**)&commitTxn); - if (NS_SUCCEEDED(result) && commitTxn!=nsnull) - { - commitTxn->Init(); - result = Do(commitTxn); - } + if (NS_SUCCEEDED(result) && commitTxn!=nsnull) + { + commitTxn->Init(); + result = Do(commitTxn); + } - /* reset the data we need to construct a transaction */ - mIMETextNode = do_QueryInterface(nsnull); - mIMETextOffset = 0; - mIMEBufferLength = 0; + /* reset the data we need to construct a transaction */ + mIMETextNode = do_QueryInterface(nsnull); + mIMETextOffset = 0; + mIMEBufferLength = 0; - return result; + return result; } NS_IMETHODIMP nsEditor::SetCompositionString(const nsString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply) { - nsCOMPtr caretP; - nsresult result = SetInputMethodText(aCompositionString,aTextRangeList); - mIMEBufferLength = aCompositionString.Length(); + nsCOMPtr caretP; + nsresult result = SetInputMethodText(aCompositionString,aTextRangeList); + mIMEBufferLength = aCompositionString.Length(); - mPresShell->GetCaret(getter_AddRefs(caretP)); - caretP->GetWindowRelativeCoordinates(aReply->mCursorPosition,aReply->mCursorIsCollapsed); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + ps->GetCaret(getter_AddRefs(caretP)); + caretP->GetWindowRelativeCoordinates(aReply->mCursorPosition,aReply->mCursorIsCollapsed); - return result; + return result; } #ifdef XP_MAC @@ -1292,14 +1320,16 @@ nsEditor::GetBodyElement(nsIDOMElement **aBodyElement) *aBodyElement = 0; - NS_PRECONDITION(mDoc, "bad state, null mDoc"); - if (!mDoc) + NS_PRECONDITION(mDocWeak, "bad state, null mDocWeak"); + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; nsCOMPtrnodeList; nsString bodyTag = "body"; - result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if (NS_FAILED(result)) return result; @@ -1474,7 +1504,7 @@ NS_IMETHODIMP nsEditor::InsertTextImpl(const nsString& aStringToInsert) newTextNode->SetData(placeholderText); selection->Collapse(newNode, 0); selection->Extend(newNode, 1); - result = InsertTextImpl(aStringToInsert); // this really recurses, right? + result = InsertTextImpl(aStringToInsert); // this really recurses, right? } } } @@ -1591,7 +1621,7 @@ NS_IMETHODIMP nsEditor::NotifyDocumentListeners(TDocumentListenerNotification aNotificationType) { if (!mDocStateListeners) - return NS_OK; // maybe there just aren't any. + return NS_OK; // maybe there just aren't any. PRUint32 numListeners; nsresult rv = mDocStateListeners->Count(&numListeners); @@ -1677,7 +1707,10 @@ NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsString & aStringToInsert, else { nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if ((NS_SUCCEEDED(result)) && selection) { #if 0 @@ -1734,7 +1767,7 @@ NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsString & aStringToInsert, { result = TransactionFactory::GetNewTransaction(InsertTextTxn::GetCID(), (EditTxn **)aTxn); if (nsnull!=*aTxn) { - result = (*aTxn)->Init(nodeAsText, offset, aStringToInsert, mPresShell); + result = (*aTxn)->Init(nodeAsText, offset, aStringToInsert, mPresShellWeak); } else { result = NS_ERROR_OUT_OF_MEMORY; @@ -1760,13 +1793,15 @@ NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsString & aStringToInsert, NS_IMETHODIMP nsEditor::DoInitialInsert(const nsString & aStringToInsert) { - if (!mDoc) { + if (!mDocWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtrnodeList; nsAutoString bodyTag = "body"; - nsresult result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsresult result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if ((NS_SUCCEEDED(result)) && nodeList) { PRUint32 count; @@ -2758,9 +2793,11 @@ nsEditor::CountEditableChildren(nsIDOMNode *aNode, PRUint32 &outCount) NS_IMETHODIMP nsEditor::IncDocModCount(PRInt32 inNumMods) { - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr diskDoc = do_QueryInterface(mDoc); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr diskDoc = do_QueryInterface(doc); if (diskDoc) diskDoc->IncrementModCount(inNumMods); @@ -2771,9 +2808,11 @@ NS_IMETHODIMP nsEditor::IncDocModCount(PRInt32 inNumMods) NS_IMETHODIMP nsEditor::GetDocModCount(PRInt32 &outModCount) { - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr diskDoc = do_QueryInterface(mDoc); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr diskDoc = do_QueryInterface(doc); if (diskDoc) diskDoc->GetModCount(&outModCount); @@ -2783,9 +2822,11 @@ NS_IMETHODIMP nsEditor::GetDocModCount(PRInt32 &outModCount) NS_IMETHODIMP nsEditor::ResetDocModCount() { - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr diskDoc = do_QueryInterface(mDoc); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr diskDoc = do_QueryInterface(doc); if (diskDoc) diskDoc->ResetModCount(); @@ -2914,110 +2955,112 @@ nsEditor::GetFirstTextNode(nsIDOMNode *aNode, nsIDOMNode **aRetNode) NS_IMETHODIMP nsEditor::SetInputMethodText(const nsString& aStringToInsert, nsIPrivateTextRangeList *aTextRangeList) { - IMETextTxn *txn; - - nsresult result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&txn); // insert at the current selection - if ((NS_SUCCEEDED(result)) && txn) { - BeginUpdateViewBatch(); - result = Do(txn); - EndUpdateViewBatch(); - } - else if (NS_ERROR_EDITOR_NO_SELECTION==result) { - result = DoInitialInputMethodInsert(aStringToInsert,aTextRangeList); - } - else if (NS_ERROR_EDITOR_NO_TEXTNODE==result) - { - BeginTransaction(); - nsCOMPtr selection; - result = GetSelection(getter_AddRefs(selection)); - if ((NS_SUCCEEDED(result)) && selection) - { - nsCOMPtr selectedNode; - PRInt32 offset; - result = selection->GetAnchorNode(getter_AddRefs(selectedNode)); - if (NS_SUCCEEDED(result) && NS_SUCCEEDED(selection->GetAnchorOffset(&offset)) && selectedNode) - { - nsCOMPtr newNode; - result = CreateNode(GetTextNodeTag(), selectedNode, offset+1,getter_AddRefs(newNode)); - if (NS_SUCCEEDED(result) && newNode) - { - nsCOMPtrnewTextNode; - newTextNode = do_QueryInterface(newNode); - if (newTextNode) - { - nsAutoString placeholderText(" "); - newTextNode->SetData(placeholderText); - selection->Collapse(newNode, 0); - selection->Extend(newNode, 1); - result = SetInputMethodText(aStringToInsert,aTextRangeList); - } - } - } - } - - EndTransaction(); - } + IMETextTxn *txn; + + nsresult result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&txn); // insert at the current selection + if ((NS_SUCCEEDED(result)) && txn) { + BeginUpdateViewBatch(); + result = Do(txn); + EndUpdateViewBatch(); + } + else if (NS_ERROR_EDITOR_NO_SELECTION==result) { + result = DoInitialInputMethodInsert(aStringToInsert,aTextRangeList); + } + else if (NS_ERROR_EDITOR_NO_TEXTNODE==result) + { + BeginTransaction(); + nsCOMPtr selection; + result = GetSelection(getter_AddRefs(selection)); + if ((NS_SUCCEEDED(result)) && selection) + { + nsCOMPtr selectedNode; + PRInt32 offset; + result = selection->GetAnchorNode(getter_AddRefs(selectedNode)); + if (NS_SUCCEEDED(result) && NS_SUCCEEDED(selection->GetAnchorOffset(&offset)) && selectedNode) + { + nsCOMPtr newNode; + result = CreateNode(GetTextNodeTag(), selectedNode, offset+1,getter_AddRefs(newNode)); + if (NS_SUCCEEDED(result) && newNode) + { + nsCOMPtrnewTextNode; + newTextNode = do_QueryInterface(newNode); + if (newTextNode) + { + nsAutoString placeholderText(" "); + newTextNode->SetData(placeholderText); + selection->Collapse(newNode, 0); + selection->Extend(newNode, 1); + result = SetInputMethodText(aStringToInsert,aTextRangeList); + } + } + } + } + + EndTransaction(); + } - return result; + return result; } NS_IMETHODIMP nsEditor::DoInitialInputMethodInsert(const nsString & aStringToInsert, nsIPrivateTextRangeList* aTextRangeList) { - if (!mDoc) { - return NS_ERROR_NOT_INITIALIZED; - } + if (!mDocWeak) { + return NS_ERROR_NOT_INITIALIZED; + } - nsCOMPtrnodeList; - nsAutoString bodyTag = "body"; - nsresult result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); - if ((NS_SUCCEEDED(result)) && nodeList) - { - PRUint32 count; - nodeList->GetLength(&count); - NS_ASSERTION(1==count, "there is not exactly 1 body in the document!"); - nsCOMPtrnode; - result = nodeList->Item(0, getter_AddRefs(node)); - if ((NS_SUCCEEDED(result)) && node) - { // now we've got the body tag. - // create transaction to insert the text node, - // and create a transaction to insert the text - CreateElementTxn *txn; - result = CreateTxnForCreateElement(GetTextNodeTag(), node, 0, &txn); - if ((NS_SUCCEEDED(result)) && txn) - { - result = Do(txn); - if (NS_SUCCEEDED(result)) - { - nsCOMPtrnewNode; - txn->GetNewNode(getter_AddRefs(newNode)); - if ((NS_SUCCEEDED(result)) && newNode) - { - nsCOMPtrnewTextNode; - newTextNode = do_QueryInterface(newNode); - if (newTextNode) - { - mIMETextNode = newTextNode; - mIMETextOffset = 0; - mIMEBufferLength = 0; + nsCOMPtrnodeList; + nsAutoString bodyTag = "body"; + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsresult result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + if ((NS_SUCCEEDED(result)) && nodeList) + { + PRUint32 count; + nodeList->GetLength(&count); + NS_ASSERTION(1==count, "there is not exactly 1 body in the document!"); + nsCOMPtrnode; + result = nodeList->Item(0, getter_AddRefs(node)); + if ((NS_SUCCEEDED(result)) && node) + { // now we've got the body tag. + // create transaction to insert the text node, + // and create a transaction to insert the text + CreateElementTxn *txn; + result = CreateTxnForCreateElement(GetTextNodeTag(), node, 0, &txn); + if ((NS_SUCCEEDED(result)) && txn) + { + result = Do(txn); + if (NS_SUCCEEDED(result)) + { + nsCOMPtrnewNode; + txn->GetNewNode(getter_AddRefs(newNode)); + if ((NS_SUCCEEDED(result)) && newNode) + { + nsCOMPtrnewTextNode; + newTextNode = do_QueryInterface(newNode); + if (newTextNode) + { + mIMETextNode = newTextNode; + mIMETextOffset = 0; + mIMEBufferLength = 0; - IMETextTxn *IMETxn; - result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&IMETxn); - if (NS_SUCCEEDED(result)) { - result = Do(IMETxn); - } - } - else { - result = NS_ERROR_UNEXPECTED; - } - } - } - } - } - } + IMETextTxn *IMETxn; + result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&IMETxn); + if (NS_SUCCEEDED(result)) { + result = Do(IMETxn); + } + } + else { + result = NS_ERROR_UNEXPECTED; + } + } + } + } + } + } - return result; + return result; } /////////////////////////////////////////////////////////////////////////// // GetTag: digs out the atom for the tag of this node @@ -3286,7 +3329,7 @@ nsEditor::NextNodeInBlock(nsIDOMNode *aNode, IterDirection aDir) while (NS_COMFALSE == iter->IsDone()) { - if (NS_FAILED(iter->CurrentNode(getter_AddRefs(content)))) return nullNode; + if (NS_FAILED(iter->CurrentNode(getter_AddRefs(content)))) return nullNode; // ignore nodes that aren't elements or text, or that are the block parent node = do_QueryInterface(content); if (node && IsTextOrElementNode(node) && (node != blockParent) && (node.get() != aNode)) @@ -3388,12 +3431,14 @@ nsEditor::IsPreformatted(nsIDOMNode *aNode, PRBool *aResult) if (!aResult || !content) return NS_ERROR_NULL_POINTER; - if (!mPresShell) return NS_ERROR_NOT_INITIALIZED; + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; - result = mPresShell->GetPrimaryFrameFor(content, &frame); + result = ps->GetPrimaryFrameFor(content, &frame); if (NS_FAILED(result)) return result; - result = mPresShell->GetStyleContextFor(frame, getter_AddRefs(styleContext)); + result = ps->GetStyleContextFor(frame, getter_AddRefs(styleContext)); if (NS_FAILED(result)) return result; styleText = (const nsStyleText*)styleContext->GetStyleData(eStyleStruct_Text); @@ -3760,7 +3805,7 @@ nsEditor::DoAfterDoTransaction(nsITransaction *aTxn) if (modCount < 0) modCount = -modCount; - rv = IncDocModCount(1); // don't count transient transactions + rv = IncDocModCount(1); // don't count transient transactions } return rv; @@ -3772,7 +3817,7 @@ nsEditor::DoAfterUndoTransaction() { nsresult rv = NS_OK; - rv = IncDocModCount(-1); // all undoable transactions are non-transient + rv = IncDocModCount(-1); // all undoable transactions are non-transient return rv; } @@ -3782,7 +3827,7 @@ nsEditor::DoAfterRedoTransaction() { nsresult rv = NS_OK; - rv = IncDocModCount(1); // all redoable transactions are non-transient + rv = IncDocModCount(1); // all redoable transactions are non-transient return rv; } @@ -3898,7 +3943,10 @@ NS_IMETHODIMP nsEditor::CreateAggregateTxnForDeleteSelection(nsIAtom *aTxnName, // Get current selection and setup txn to delete it, // but only if selection exists (is not a collapsed "caret" state) nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { PRBool collapsed; @@ -3918,22 +3966,22 @@ NS_IMETHODIMP nsEditor::CreateAggregateTxnForDeleteSelection(nsIAtom *aTxnName, NS_IMETHODIMP nsEditor::CreateTxnForIMEText(const nsString & aStringToInsert, - nsIPrivateTextRangeList* aTextRangeList, + nsIPrivateTextRangeList* aTextRangeList, IMETextTxn ** aTxn) { - nsresult result; + nsresult result; - if (mIMETextNode==nsnull) - BeginComposition(); + if (mIMETextNode==nsnull) + BeginComposition(); result = TransactionFactory::GetNewTransaction(IMETextTxn::GetCID(), (EditTxn **)aTxn); - if (nsnull!=*aTxn) { - result = (*aTxn)->Init(mIMETextNode,mIMETextOffset,mIMEBufferLength,aTextRangeList,aStringToInsert,mPresShell); - } - else { - result = NS_ERROR_OUT_OF_MEMORY; - } - return result; + if (nsnull!=*aTxn) { + result = (*aTxn)->Init(mIMETextNode,mIMETextOffset,mIMEBufferLength,aTextRangeList,aStringToInsert,mPresShellWeak); + } + else { + result = NS_ERROR_OUT_OF_MEMORY; + } + return result; } @@ -3976,7 +4024,10 @@ nsEditor::CreateTxnForDeleteSelection(nsIEditor::ESelectionCollapseDirection aAc nsresult result; nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if ((NS_SUCCEEDED(result)) && selection) { // Check whether the selection is collapsed and we should do nothing: diff --git a/mozilla/editor/base/nsEditor.h b/mozilla/editor/base/nsEditor.h index 2f8911e185b..51c7b934989 100644 --- a/mozilla/editor/base/nsEditor.h +++ b/mozilla/editor/base/nsEditor.h @@ -20,6 +20,7 @@ #define __editor_h__ #include "nsCOMPtr.h" +#include "nsWeakPtr.h" #include "prmon.h" #include "nsIEditor.h" @@ -584,7 +585,7 @@ protected: PRUint32 mFlags; // behavior flags. See nsIHTMLEditor.h for the flags we use. - nsIPresShell *mPresShell; + nsWeakPtr mPresShellWeak; // weak reference to the nsIPresShell nsIViewManager *mViewManager; PRUint32 mUpdateCount; nsCOMPtr mTxnMgr; @@ -604,10 +605,10 @@ protected: PRInt8 mDocDirtyState; // -1 = not initialized - nsIDOMDocument * mDoc; + nsWeakPtr mDocWeak; // weak reference to the nsIDOMDocument nsCOMPtr mDTD; // Services are not nsCOMPtr friendly - nsIPref* mPrefs; + nsIPref *mPrefs; static PRInt32 gInstanceCount; diff --git a/mozilla/editor/base/nsEditorEventListeners.cpp b/mozilla/editor/base/nsEditorEventListeners.cpp index a4379eea198..0ef4f21eafa 100644 --- a/mozilla/editor/base/nsEditorEventListeners.cpp +++ b/mozilla/editor/base/nsEditorEventListeners.cpp @@ -136,84 +136,84 @@ nsTextEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) // PRBool keyProcessed; // ProcessShortCutKeys(aKeyEvent, keyProcessed); // if (PR_FALSE==keyProcessed) - { - if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) && - NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) && - NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) && - NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) && - NS_SUCCEEDED(uiEvent->GetMetaKey(&metaKey))) - { - { - switch(keyCode) { - // case nsIDOMUIEvent::VK_BACK: - // mEditor->DeleteSelection(nsIEditor::eDeleteLeft); - // break; + { + if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) && + NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) && + NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) && + NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) && + NS_SUCCEEDED(uiEvent->GetMetaKey(&metaKey))) + { + { + switch(keyCode) { + // case nsIDOMUIEvent::VK_BACK: + // mEditor->DeleteSelection(nsIEditor::eDeleteLeft); + // break; - case nsIDOMUIEvent::VK_DELETE: - mEditor->DeleteSelection(nsIEditor::eDeleteNext); - break; + case nsIDOMUIEvent::VK_DELETE: + mEditor->DeleteSelection(nsIEditor::eDeleteNext); + break; - // case nsIDOMUIEvent::VK_RETURN: - //case nsIDOMUIEvent::VK_ENTER: // why does this not exist? - // Need to implement creation of either

or
nodes. - // mEditor->InsertBreak(); - // break; + // case nsIDOMUIEvent::VK_RETURN: + //case nsIDOMUIEvent::VK_ENTER: // why does this not exist? + // Need to implement creation of either

or
nodes. + // mEditor->InsertBreak(); + // break; - case nsIDOMUIEvent::VK_LEFT: - case nsIDOMUIEvent::VK_RIGHT: - case nsIDOMUIEvent::VK_UP: - case nsIDOMUIEvent::VK_DOWN: - // these have already been handled in nsRangeList. Why are we getting them - // again here (Mac)? In switch to avoid putting in bogus chars. + case nsIDOMUIEvent::VK_LEFT: + case nsIDOMUIEvent::VK_RIGHT: + case nsIDOMUIEvent::VK_UP: + case nsIDOMUIEvent::VK_DOWN: + // these have already been handled in nsRangeList. Why are we getting them + // again here (Mac)? In switch to avoid putting in bogus chars. - //return NS_OK to allow page scrolling. - return NS_OK; - break; + //return NS_OK to allow page scrolling. + return NS_OK; + break; - case nsIDOMUIEvent::VK_HOME: - case nsIDOMUIEvent::VK_END: - // who handles these? - #if DEBUG - printf("Key not handled\n"); - #endif - return NS_OK; - break; + case nsIDOMUIEvent::VK_HOME: + case nsIDOMUIEvent::VK_END: + // who handles these? + #if DEBUG + printf("Key not handled\n"); + #endif + return NS_OK; + break; - case nsIDOMUIEvent::VK_PAGE_UP: - case nsIDOMUIEvent::VK_PAGE_DOWN: - //return NS_OK to allow page scrolling. - return NS_OK; - break; + case nsIDOMUIEvent::VK_PAGE_UP: + case nsIDOMUIEvent::VK_PAGE_DOWN: + //return NS_OK to allow page scrolling. + return NS_OK; + break; - case nsIDOMUIEvent::VK_TAB: - { - PRUint32 flags=0; - mEditor->GetFlags(&flags); - if (! (flags & nsIHTMLEditor::eEditorSingleLineMask)) - { - if (metaKey || altKey) - return NS_OK; // don't consume - // else we insert the tab straight through - nsAutoString key; - key += keyCode; + case nsIDOMUIEvent::VK_TAB: + { + PRUint32 flags=0; + mEditor->GetFlags(&flags); + if (! (flags & nsIHTMLEditor::eEditorSingleLineMask)) + { + if (metaKey || altKey) + return NS_OK; // don't consume + // else we insert the tab straight through + nsAutoString key; + key += keyCode; - nsCOMPtr htmlEditor = do_QueryInterface(mEditor); - if (htmlEditor) - htmlEditor->InsertText(key); - return NS_ERROR_BASE; // this means "I handled the event, don't do default processing" - } - else { - return NS_OK; - } - break; - } + nsCOMPtr htmlEditor = do_QueryInterface(mEditor); + if (htmlEditor) + htmlEditor->InsertText(key); + return NS_ERROR_BASE; // this means "I handled the event, don't do default processing" + } + else { + return NS_OK; + } + break; + } - default: - return NS_OK; // this indicates that we have not handled the keyDown event in any way. - } - } - } - } + default: + return NS_OK; // this indicates that we have not handled the keyDown event in any way. + } + } + } + } return NS_ERROR_BASE; } @@ -229,70 +229,74 @@ nsTextEditorKeyListener::KeyUp(nsIDOMEvent* aKeyEvent) nsresult nsTextEditorKeyListener::KeyPress(nsIDOMEvent* aKeyEvent) { - nsAutoString key; - PRUint32 character; - PRUint32 keyCode; + nsAutoString key; + PRUint32 character; + PRUint32 keyCode; - nsCOMPtruiEvent; - uiEvent = do_QueryInterface(aKeyEvent); - if (!uiEvent) { - //non-key event passed to keydown. bad things. - return NS_OK; - } - // - // look at the keyCode if it is return or backspace, process it - // we handle these two special characters here because it makes windows integration - // eaiser - // + nsCOMPtruiEvent; + uiEvent = do_QueryInterface(aKeyEvent); + if (!uiEvent) + { + //non-key event passed to keydown. bad things. + return NS_OK; + } + // + // look at the keyCode if it is return or backspace, process it + // we handle these two special characters here because it makes windows integration + // eaiser + // PRBool keyProcessed; ProcessShortCutKeys(aKeyEvent, keyProcessed); if (PR_FALSE==keyProcessed) { PRUint32 flags; - PRBool ctrlKey, altKey, metaKey; - uiEvent->GetCtrlKey(&ctrlKey); - uiEvent->GetAltKey(&altKey); - uiEvent->GetMetaKey(&metaKey); + PRBool ctrlKey, altKey, metaKey; + uiEvent->GetCtrlKey(&ctrlKey); + uiEvent->GetAltKey(&altKey); + uiEvent->GetMetaKey(&metaKey); - if (metaKey) - return NS_OK; // don't consume + if (metaKey) + return NS_OK; // don't consume - nsCOMPtr htmlEditor = do_QueryInterface(mEditor); - if (!htmlEditor) return NS_ERROR_NO_INTERFACE; + nsCOMPtr htmlEditor = do_QueryInterface(mEditor); + if (!htmlEditor) return NS_ERROR_NO_INTERFACE; - if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode))) - { - if (nsIDOMUIEvent::VK_BACK==keyCode) { - mEditor->DeleteSelection(nsIEditor::eDeletePrevious); - return NS_ERROR_BASE; // consumed - } - if (nsIDOMUIEvent::VK_RETURN==keyCode) + if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode))) + { + if (nsIDOMUIEvent::VK_BACK==keyCode) + { + mEditor->DeleteSelection(nsIEditor::eDeletePrevious); + return NS_ERROR_BASE; // consumed + } + if (nsIDOMUIEvent::VK_RETURN==keyCode) { mEditor->GetFlags(&flags); if (!(flags & nsIHTMLEditor::eEditorSingleLineMask)) { - htmlEditor->InsertBreak(); - return NS_ERROR_BASE; // consumed + htmlEditor->InsertBreak(); + return NS_ERROR_BASE; // consumed } - else { + else + { return NS_OK; } - } - } - - if ((PR_FALSE==altKey) && (PR_FALSE==ctrlKey) && - (NS_SUCCEEDED(uiEvent->GetCharCode(&character)))) - { - if (nsIDOMUIEvent::VK_TAB==character) { - return NS_OK; // ignore tabs here, they're handled in keyDown if at all - } - key += character; - htmlEditor->InsertText(key); - } - } + } + } + + if ((PR_FALSE==altKey) && (PR_FALSE==ctrlKey) && + (NS_SUCCEEDED(uiEvent->GetCharCode(&character)))) + { + if (nsIDOMUIEvent::VK_TAB==character) + { + return NS_OK; // ignore tabs here, they're handled in keyDown if at all + } + key += character; + htmlEditor->InsertText(key); + } + } - return NS_ERROR_BASE; // consumed + return NS_ERROR_BASE; // consumed } @@ -321,8 +325,7 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) && NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) && NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) && - NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) - ) + NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) ) { if (PR_TRUE==ctrlKey) { aProcessed = PR_TRUE; @@ -385,11 +388,11 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr nsCOMPtr mailEditor = do_QueryInterface(mEditor); if (mailEditor) { - PRInt32 wrap; - if (NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - printf("Currently wrapping to %d\n", wrap); - else - printf("GetBodyWrapWidth returned an error\n"); + PRInt32 wrap; + if (NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + printf("Currently wrapping to %d\n", wrap); + else + printf("GetBodyWrapWidth returned an error\n"); } } break; @@ -398,24 +401,24 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // hard coded "Decrease wrap size" if (PR_TRUE==altKey) { - nsCOMPtr mailEditor = do_QueryInterface(mEditor); - if (mailEditor) - { - aProcessed=PR_TRUE; - PRInt32 wrap; - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("GetBodyWrapWidth returned an error\n"); - break; - } - mailEditor->SetBodyWrapWidth(wrap - 5); - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("Second GetBodyWrapWidth returned an error\n"); - break; - } - else printf("Now wrapping to %d\n", wrap); - } + nsCOMPtr mailEditor = do_QueryInterface(mEditor); + if (mailEditor) + { + aProcessed=PR_TRUE; + PRInt32 wrap; + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("GetBodyWrapWidth returned an error\n"); + break; + } + mailEditor->SetBodyWrapWidth(wrap - 5); + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("Second GetBodyWrapWidth returned an error\n"); + break; + } + else printf("Now wrapping to %d\n", wrap); + } } break; @@ -423,24 +426,24 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // hard coded "Increase wrap size" if (PR_TRUE==altKey) { - nsCOMPtr mailEditor = do_QueryInterface(mEditor); - if (mailEditor) - { - aProcessed=PR_TRUE; - PRInt32 wrap; - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("GetBodyWrapWidth returned an error\n"); - break; - } - mailEditor->SetBodyWrapWidth(wrap + 5); - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("Second GetBodyWrapWidth returned an error\n"); - break; - } - else printf("Now wrapping to %d\n", wrap); - } + nsCOMPtr mailEditor = do_QueryInterface(mEditor); + if (mailEditor) + { + aProcessed=PR_TRUE; + PRInt32 wrap; + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("GetBodyWrapWidth returned an error\n"); + break; + } + mailEditor->SetBodyWrapWidth(wrap + 5); + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("Second GetBodyWrapWidth returned an error\n"); + break; + } + else printf("Now wrapping to %d\n", wrap); + } } break; @@ -818,7 +821,7 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // the unit tests are only exposed through nsIEditor nsCOMPtr editor = do_QueryInterface(mEditor); if (editor) - editor->DebugUnitTests(&numTests, &numFailed); + editor->DebugUnitTests(&numTests, &numFailed); } } break; @@ -895,20 +898,20 @@ nsTextEditorMouseListener::HandleEvent(nsIDOMEvent* aEvent) nsresult IsNodeInSelection(nsIDOMNode *aInNode, nsIDOMSelection *aInSelection, PRBool &aOutIsInSel) { - aOutIsInSel = PR_FALSE; // init out-param - if (!aInNode || !aInSelection) { return NS_ERROR_NULL_POINTER; } + aOutIsInSel = PR_FALSE; // init out-param + if (!aInNode || !aInSelection) { return NS_ERROR_NULL_POINTER; } nsCOMPtriter; nsresult result = nsComponentManager::CreateInstance(kContentIteratorCID, nsnull, nsIContentIterator::GetIID(), - getter_AddRefs(iter)); - if (NS_FAILED(result)) { return result; } - if (!iter) { return NS_ERROR_OUT_OF_MEMORY; } + getter_AddRefs(iter)); + if (NS_FAILED(result)) { return result; } + if (!iter) { return NS_ERROR_OUT_OF_MEMORY; } - nsCOMPtr enumerator; + nsCOMPtr enumerator; result = aInSelection->GetEnumerator(getter_AddRefs(enumerator)); if (NS_FAILED(result)) { return result; } - if (!enumerator) { return NS_ERROR_OUT_OF_MEMORY; } + if (!enumerator) { return NS_ERROR_OUT_OF_MEMORY; } for (enumerator->First(); NS_OK!=enumerator->IsDone(); enumerator->Next()) { @@ -918,31 +921,31 @@ IsNodeInSelection(nsIDOMNode *aInNode, nsIDOMSelection *aInSelection, PRBool &aO { nsCOMPtr range( do_QueryInterface(currentItem) ); - iter->Init(range); - nsCOMPtr currentContent; - iter->CurrentNode(getter_AddRefs(currentContent)); - while (NS_COMFALSE == iter->IsDone()) - { - nsCOMPtrcurrentNode = do_QueryInterface(currentContent); - if (currentNode.get()==aInNode) - { - // if it's a start or end node, need to test whether the (x,y) - // of the event falls within the selection + iter->Init(range); + nsCOMPtr currentContent; + iter->CurrentNode(getter_AddRefs(currentContent)); + while (NS_COMFALSE == iter->IsDone()) + { + nsCOMPtrcurrentNode = do_QueryInterface(currentContent); + if (currentNode.get()==aInNode) + { + // if it's a start or end node, need to test whether the (x,y) + // of the event falls within the selection - // talk to Mike + // talk to Mike - aOutIsInSel = PR_TRUE; - return NS_OK; - } - /* do not check result here, and especially do not return the result code. - * we rely on iter->IsDone to tell us when the iteration is complete - */ - iter->Next(); - iter->CurrentNode(getter_AddRefs(currentContent)); - } - } - } - return NS_OK; + aOutIsInSel = PR_TRUE; + return NS_OK; + } + /* do not check result here, and especially do not return the result code. + * we rely on iter->IsDone to tell us when the iteration is complete + */ + iter->Next(); + iter->CurrentNode(getter_AddRefs(currentContent)); + } + } + } + return NS_OK; } @@ -959,136 +962,136 @@ nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent) return NS_OK; } - nsCOMPtr editor (do_QueryInterface(mEditor)); + nsCOMPtr editor (do_QueryInterface(mEditor)); if (!editor) { return NS_OK; } - // get the document - nsCOMPtrdomDoc; + // get the document + nsCOMPtrdomDoc; editor->GetDocument(getter_AddRefs(domDoc)); - if (!domDoc) { return NS_OK; } - nsCOMPtrdoc = do_QueryInterface(domDoc); - if (!doc) { return NS_OK; } + if (!domDoc) { return NS_OK; } + nsCOMPtrdoc = do_QueryInterface(domDoc); + if (!doc) { return NS_OK; } PRUint16 button = 0; uiEvent->GetButton(&button); - // left button click might be a drag-start - if (1==button) - { + // left button click might be a drag-start + if (1==button) + { #ifndef EXPERIMENTAL_DRAG_CODE - return NS_OK; + return NS_OK; #else - nsString XIFBuffer; - // get the DOM select here - nsCOMPtr sel; - editor->GetSelection(getter_AddRefs(sel)); + nsString XIFBuffer; + // get the DOM select here + nsCOMPtr sel; + editor->GetSelection(getter_AddRefs(sel)); - // convert the DOMselection to XIF - if (sel) - { - // if we are within the selection, start the drag + // convert the DOMselection to XIF + if (sel) + { + // if we are within the selection, start the drag - nsCOMPtr target; - nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target)); - if (NS_FAILED(rv) || !target) { return NS_OK; } - PRBool isInSel; - rv = IsNodeInSelection(target, sel, isInSel); - if (NS_FAILED(rv) || PR_FALSE==isInSel) { return NS_OK; } - doc->CreateXIF(XIFBuffer, sel); + nsCOMPtr target; + nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target)); + if (NS_FAILED(rv) || !target) { return NS_OK; } + PRBool isInSel; + rv = IsNodeInSelection(target, sel, isInSel); + if (NS_FAILED(rv) || PR_FALSE==isInSel) { return NS_OK; } + doc->CreateXIF(XIFBuffer, sel); - // Get the Clipboard - nsIClipboard* clipboard; - rv = nsServiceManager::GetService(kCClipboardCID, - nsIClipboard::GetIID(), - (nsISupports **)&clipboard); - if (NS_OK == rv) - { - // Create a data flavor to tell the transferable - // that it is about to receive XIF - nsAutoString flavor(kXIFMime); + // Get the Clipboard + nsIClipboard* clipboard; + rv = nsServiceManager::GetService(kCClipboardCID, + nsIClipboard::GetIID(), + (nsISupports **)&clipboard); + if (NS_OK == rv) + { + // Create a data flavor to tell the transferable + // that it is about to receive XIF + nsAutoString flavor(kXIFMime); - // Create a transferable for putting data on the Clipboard - nsCOMPtr trans; - rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull, - nsITransferable::GetIID(), - (void**) getter_AddRefs(trans)); - if (NS_OK == rv) { - // The data on the clipboard will be in "XIF" format - // so give the clipboard transferable a "XIFConverter" for - // converting from XIF to other formats - nsCOMPtr xifConverter; - rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull, - nsIFormatConverter::GetIID(), (void**) getter_AddRefs(xifConverter)); - if (NS_OK == rv) { - // Add the XIF DataFlavor to the transferable - // this tells the transferable that it can handle receiving the XIF format - trans->AddDataFlavor(&flavor); + // Create a transferable for putting data on the Clipboard + nsCOMPtr trans; + rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull, + nsITransferable::GetIID(), + (void**) getter_AddRefs(trans)); + if (NS_OK == rv) { + // The data on the clipboard will be in "XIF" format + // so give the clipboard transferable a "XIFConverter" for + // converting from XIF to other formats + nsCOMPtr xifConverter; + rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull, + nsIFormatConverter::GetIID(), (void**) getter_AddRefs(xifConverter)); + if (NS_OK == rv) { + // Add the XIF DataFlavor to the transferable + // this tells the transferable that it can handle receiving the XIF format + trans->AddDataFlavor(&flavor); - // Add the converter for going from XIF to other formats - trans->SetConverter(xifConverter); + // Add the converter for going from XIF to other formats + trans->SetConverter(xifConverter); - // Now add the XIF data to the transferable - // the transferable wants the number bytes for the data and since it is double byte - // we multiply by 2 - trans->SetTransferData(&flavor, XIFBuffer.ToNewUnicode(), XIFBuffer.Length()*2); + // Now add the XIF data to the transferable + // the transferable wants the number bytes for the data and since it is double byte + // we multiply by 2 + trans->SetTransferData(&flavor, XIFBuffer.ToNewUnicode(), XIFBuffer.Length()*2); - // Now invoke the drag session - nsIDragService* dragService; - nsresult rv = nsServiceManager::GetService(kCDragServiceCID, - nsIDragService::GetIID(), - (nsISupports **)&dragService); - if (NS_OK == rv) { - nsCOMPtr items; - NS_NewISupportsArray(getter_AddRefs(items)); - if ( items ) { - items->AppendElement(trans); - dragService->InvokeDragSession(items, nsnull, nsIDragService::DRAGDROP_ACTION_COPY | nsIDragService::DRAGDROP_ACTION_MOVE); - } - nsServiceManager::ReleaseService(kCDragServiceCID, dragService); - } - } - } - nsServiceManager::ReleaseService(kCClipboardCID, clipboard); - } - return NS_ERROR_BASE; // return that we've handled the event - } + // Now invoke the drag session + nsIDragService* dragService; + nsresult rv = nsServiceManager::GetService(kCDragServiceCID, + nsIDragService::GetIID(), + (nsISupports **)&dragService); + if (NS_OK == rv) { + nsCOMPtr items; + NS_NewISupportsArray(getter_AddRefs(items)); + if ( items ) { + items->AppendElement(trans); + dragService->InvokeDragSession(items, nsnull, nsIDragService::DRAGDROP_ACTION_COPY | nsIDragService::DRAGDROP_ACTION_MOVE); + } + nsServiceManager::ReleaseService(kCDragServiceCID, dragService); + } + } + } + nsServiceManager::ReleaseService(kCClipboardCID, clipboard); + } + return NS_ERROR_BASE; // return that we've handled the event + } #endif } // middle-mouse click (paste); else if (button == 2) - { + { // Set the selection to the point under the mouse cursor: - nsCOMPtr mouseEvent (do_QueryInterface(aMouseEvent)); + nsCOMPtr mouseEvent (do_QueryInterface(aMouseEvent)); - if (!mouseEvent) - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - nsCOMPtr parent; - if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent)))) - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - PRInt32 offset = 0; - if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset))) - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + if (!mouseEvent) + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + nsCOMPtr parent; + if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent)))) + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + PRInt32 offset = 0; + if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset))) + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - nsCOMPtr selection; - if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection)))) - (void)selection->Collapse(parent, offset); + nsCOMPtr selection; + if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection)))) + (void)selection->Collapse(parent, offset); - // If the ctrl key is pressed, we'll do paste as quotation. - // Would've used the alt key, but the kde wmgr treats alt-middle specially. - PRBool ctrlKey = PR_FALSE; - uiEvent->GetCtrlKey(&ctrlKey); + // If the ctrl key is pressed, we'll do paste as quotation. + // Would've used the alt key, but the kde wmgr treats alt-middle specially. + PRBool ctrlKey = PR_FALSE; + uiEvent->GetCtrlKey(&ctrlKey); - if (ctrlKey) - { - nsCOMPtr mailEditor = do_QueryInterface(mEditor); - if (mailEditor) - mailEditor->PasteAsQuotation(); - } - editor->Paste(); - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - } - return NS_OK; // did not process the event + if (ctrlKey) + { + nsCOMPtr mailEditor = do_QueryInterface(mEditor); + if (mailEditor) + mailEditor->PasteAsQuotation(); + } + editor->Paste(); + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + } + return NS_OK; // did not process the event } @@ -1113,7 +1116,7 @@ nsTextEditorMouseListener::MouseClick(nsIDOMEvent* aMouseEvent) nsresult nsTextEditorMouseListener::MouseDblClick(nsIDOMEvent* aMouseEvent) { - nsCOMPtr htmlEditor = do_QueryInterface(mEditor); + nsCOMPtr htmlEditor = do_QueryInterface(mEditor); if (htmlEditor) { nsCOMPtr selectedElement; @@ -1163,8 +1166,8 @@ NS_IMPL_RELEASE(nsTextEditorTextListener) nsTextEditorTextListener::nsTextEditorTextListener() -: mCommitText(PR_FALSE), - mInTransaction(PR_FALSE) +: mCommitText(PR_FALSE), + mInTransaction(PR_FALSE) { NS_INIT_REFCNT(); } @@ -1211,26 +1214,26 @@ nsTextEditorTextListener::HandleEvent(nsIDOMEvent* aEvent) nsresult nsTextEditorTextListener::HandleText(nsIDOMEvent* aTextEvent) { - nsString composedText; - nsresult result = NS_OK; - nsCOMPtr textEvent; - nsIPrivateTextRangeList *textRangeList; - nsTextEventReply *textEventReply; + nsString composedText; + nsresult result = NS_OK; + nsCOMPtr textEvent; + nsIPrivateTextRangeList *textRangeList; + nsTextEventReply *textEventReply; - textEvent = do_QueryInterface(aTextEvent); - if (!textEvent) { - //non-ui event passed in. bad things. - return NS_OK; - } + textEvent = do_QueryInterface(aTextEvent); + if (!textEvent) { + //non-ui event passed in. bad things. + return NS_OK; + } - textEvent->GetText(composedText); - textEvent->GetInputRange(&textRangeList); - textEvent->GetEventReply(&textEventReply); - textRangeList->AddRef(); - nsCOMPtr imeEditor = do_QueryInterface(mEditor, &result); - if (imeEditor) + textEvent->GetText(composedText); + textEvent->GetInputRange(&textRangeList); + textEvent->GetEventReply(&textEventReply); + textRangeList->AddRef(); + nsCOMPtr imeEditor = do_QueryInterface(mEditor, &result); + if (imeEditor) result = imeEditor->SetCompositionString(composedText,textRangeList,textEventReply); - return result; + return result; } /* @@ -1455,7 +1458,7 @@ nsTextEditorCompositionListener::HandleEvent(nsIDOMEvent* aEvent) void nsTextEditorCompositionListener::SetEditor(nsIEditor *aEditor) { nsCOMPtr imeEditor = do_QueryInterface(aEditor); - if (!imeEditor) return; // should return an error here! + if (!imeEditor) return; // should return an error here! // note that we don't hold an extra reference here. mEditor = imeEditor; @@ -1464,13 +1467,13 @@ void nsTextEditorCompositionListener::SetEditor(nsIEditor *aEditor) nsresult nsTextEditorCompositionListener::HandleStartComposition(nsIDOMEvent* aCompositionEvent) { - return mEditor->BeginComposition(); + return mEditor->BeginComposition(); } nsresult nsTextEditorCompositionListener::HandleEndComposition(nsIDOMEvent* aCompositionEvent) { - return mEditor->EndComposition(); + return mEditor->EndComposition(); } @@ -1515,14 +1518,14 @@ NS_NewEditorMouseListener(nsIDOMEventListener ** aInstancePtrResult, nsresult NS_NewEditorTextListener(nsIDOMEventListener** aInstancePtrResult, nsIEditor* aEditor) { - nsTextEditorTextListener* it = new nsTextEditorTextListener(); - if (nsnull==it) { - return NS_ERROR_OUT_OF_MEMORY; - } + nsTextEditorTextListener* it = new nsTextEditorTextListener(); + if (nsnull==it) { + return NS_ERROR_OUT_OF_MEMORY; + } - it->SetEditor(aEditor); + it->SetEditor(aEditor); - return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); + return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); } @@ -1544,11 +1547,11 @@ NS_NewEditorDragListener(nsIDOMEventListener ** aInstancePtrResult, nsresult NS_NewEditorCompositionListener(nsIDOMEventListener** aInstancePtrResult, nsIEditor* aEditor) { - nsTextEditorCompositionListener* it = new nsTextEditorCompositionListener(); - if (nsnull==it) { - return NS_ERROR_OUT_OF_MEMORY; - } - it->SetEditor(aEditor); + nsTextEditorCompositionListener* it = new nsTextEditorCompositionListener(); + if (nsnull==it) { + return NS_ERROR_OUT_OF_MEMORY; + } + it->SetEditor(aEditor); return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); } @@ -1561,7 +1564,7 @@ NS_NewEditorFocusListener(nsIDOMEventListener ** aInstancePtrResult, return NS_ERROR_OUT_OF_MEMORY; } it->SetEditor(aEditor); - return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); + return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); } @@ -1654,7 +1657,7 @@ nsTextEditorFocusListener::Focus(nsIDOMEvent* aEvent) ps->GetViewManager(getter_AddRefs(viewmgr)); if (viewmgr) { nsIView* view; - viewmgr->GetRootView(view); // views are not refCounted + viewmgr->GetRootView(view); // views are not refCounted if (view) { viewmgr->UpdateView(view,nsnull,NS_VMREFRESH_IMMEDIATE); } @@ -1698,7 +1701,7 @@ nsTextEditorFocusListener::Blur(nsIDOMEvent* aEvent) if (viewmgr) { nsIView* view; - viewmgr->GetRootView(view); // views are not refCounted + viewmgr->GetRootView(view); // views are not refCounted if (view) { viewmgr->UpdateView(view,nsnull,NS_VMREFRESH_IMMEDIATE); } diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index 8d873747dc9..515a9c19d7e 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -304,8 +304,8 @@ nsHTMLEditor::PostCreate() NS_IMETHODIMP nsHTMLEditor::InstallEventListeners() { - NS_ASSERTION(mDoc, "no document set on this editor"); - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + NS_ASSERTION(mDocWeak, "no document set on this editor"); + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; nsresult result; // get a key listener @@ -358,7 +358,9 @@ printf("nsTextEditor.cpp: failed to get TextEvent Listener\n"); // get the DOM event receiver nsCOMPtr erP; - result = mDoc->QueryInterface(nsIDOMEventReceiver::GetIID(), getter_AddRefs(erP)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->QueryInterface(nsIDOMEventReceiver::GetIID(), getter_AddRefs(erP)); if (NS_FAILED(result)) { HandleEventListenerError(); return result; @@ -2116,7 +2118,9 @@ nsHTMLEditor::CreateElementWithDefaults(const nsString& aTagName, nsIDOMElement* // go through the transaction system nsCOMPtrnewElement; - res = mDoc->CreateElement(realTagName, getter_AddRefs(newElement)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + res = doc->CreateElement(realTagName, getter_AddRefs(newElement)); if (NS_FAILED(res) || !newElement) return NS_ERROR_FAILURE; @@ -2205,7 +2209,7 @@ nsHTMLEditor::CreateElementWithDefaults(const nsString& aTagName, nsIDOMElement* // Set contents to the   character by concatanating the char code space += nbsp; // If we fail here, we return NS_OK anyway, since we have an OK cell node - nsresult result = mDoc->CreateTextNode(space, getter_AddRefs(newTextNode)); + nsresult result = doc->CreateTextNode(space, getter_AddRefs(newTextNode)); if (NS_FAILED(result)) return result; if (!newTextNode) return NS_ERROR_NULL_POINTER; @@ -2326,7 +2330,7 @@ DELETE_ANCHOR: NS_IMETHODIMP nsHTMLEditor::SetBackgroundColor(const nsString& aColor) { // nsresult result; - NS_PRECONDITION(mDoc, "Missing Editor DOM Document"); + NS_PRECONDITION(mDocWeak, "Missing Editor DOM Document"); // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level // For initial testing, just set the background on the BODY tag (the document's background) @@ -2353,7 +2357,7 @@ NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsString& aAttribute, const n nsresult res; // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level - NS_ASSERTION(mDoc, "Missing Editor DOM Document"); + NS_ASSERTION(mDocWeak, "Missing Editor DOM Document"); // Set the background color attribute on the body tag nsCOMPtr bodyElement; @@ -2523,7 +2527,10 @@ NS_IMETHODIMP nsHTMLEditor::ApplyStyleSheet(const nsString& aURL) if (NS_SUCCEEDED(rv)) { nsCOMPtr document; - rv = mPresShell->GetDocument(getter_AddRefs(document)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + rv = ps->GetDocument(getter_AddRefs(document)); if (NS_SUCCEEDED(rv)) { if (document) { @@ -2592,11 +2599,14 @@ nsHTMLEditor::GetBodyStyleContext(nsIStyleContext** aStyleContext) nsCOMPtr content = do_QueryInterface(body); nsIFrame *frame; - res = mPresShell->GetPrimaryFrameFor(content, &frame); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + res = ps->GetPrimaryFrameFor(content, &frame); if (NS_FAILED(res)) return res; nsCOMPtr styleContext; - return mPresShell->GetStyleContextFor(frame, aStyleContext); + return ps->GetStyleContextFor(frame, aStyleContext); } // @@ -2967,7 +2977,10 @@ nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList) NS_IMETHODIMP nsHTMLEditor::Cut() { nsCOMPtr selection; - nsresult res = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult res = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (!NS_SUCCEEDED(res)) return res; @@ -2985,7 +2998,10 @@ NS_IMETHODIMP nsHTMLEditor::Copy() { //printf("nsEditor::Copy\n"); - return mPresShell->DoCopy(); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + return ps->DoCopy(); } NS_IMETHODIMP nsHTMLEditor::Paste() @@ -3303,22 +3319,24 @@ void nsHTMLEditor::ApplyStyleSheetToPresShellDocument(nsICSSStyleSheet* aSheet, NS_IMETHODIMP nsHTMLEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject) { nsresult result = NS_ERROR_FAILURE; // we return an error unless we get the index - if( mPresShell != nsnull ) - { - if ((nsnull!=aNode)) - { // get the content interface - nsCOMPtr nodeAsContent( do_QueryInterface(aNode) ); - if (nodeAsContent) - { // get the frame from the content interface - //Note: frames are not ref counted, so don't use an nsCOMPtr - *aLayoutObject = nsnull; - result = mPresShell->GetLayoutObjectFor(nodeAsContent, aLayoutObject); - } - } - else { - result = NS_ERROR_NULL_POINTER; + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + + if ((nsnull!=aNode)) + { // get the content interface + nsCOMPtr nodeAsContent( do_QueryInterface(aNode) ); + if (nodeAsContent) + { // get the frame from the content interface + //Note: frames are not ref counted, so don't use an nsCOMPtr + *aLayoutObject = nsnull; + result = ps->GetLayoutObjectFor(nodeAsContent, aLayoutObject); } } + else { + result = NS_ERROR_NULL_POINTER; + } + return result; } diff --git a/mozilla/editor/libeditor/base/IMETextTxn.cpp b/mozilla/editor/libeditor/base/IMETextTxn.cpp index 61d60160d96..c3b488329a7 100644 --- a/mozilla/editor/libeditor/base/IMETextTxn.cpp +++ b/mozilla/editor/libeditor/base/IMETextTxn.cpp @@ -43,21 +43,21 @@ IMETextTxn::IMETextTxn() IMETextTxn::~IMETextTxn() { - mRangeList = do_QueryInterface(nsnull); + mRangeList = do_QueryInterface(nsnull); } -NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement, - PRUint32 aOffset, - PRUint32 aReplaceLength, - nsIPrivateTextRangeList* aTextRangeList, - const nsString &aStringToInsert, - nsIPresShell *aPresShell) +NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + PRUint32 aReplaceLength, + nsIPrivateTextRangeList *aTextRangeList, + const nsString &aStringToInsert, + nsWeakPtr aPresShellWeak) { mElement = do_QueryInterface(aElement); mOffset = aOffset; mReplaceLength = aReplaceLength; mStringToInsert = aStringToInsert; - mPresShell = aPresShell; + mPresShellWeak = aPresShellWeak; mRangeList = do_QueryInterface(aTextRangeList); mFixed = PR_FALSE; return NS_OK; @@ -70,22 +70,25 @@ NS_IMETHODIMP IMETextTxn::Do(void) printf("Do IME Text element = %p\n", mElement.get()); #endif - // advance caret: This requires the presentation shell to get the selection. - nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - NS_ASSERTION(selection,"Could not get selection in IMEtextTxn::Do\n"); - if (NS_SUCCEEDED(result) && selection) { - if (mReplaceLength==0) { - result = mElement->InsertData(mOffset,mStringToInsert); - } else { - result = mElement->ReplaceData(mOffset,mReplaceLength,mStringToInsert); - } - if (NS_SUCCEEDED(result)) { - result = CollapseTextSelection(); - } - } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; - return result; + // advance caret: This requires the presentation shell to get the selection. + nsCOMPtr selection; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + NS_ASSERTION(selection,"Could not get selection in IMEtextTxn::Do\n"); + if (NS_SUCCEEDED(result) && selection) { + if (mReplaceLength==0) { + result = mElement->InsertData(mOffset,mStringToInsert); + } else { + result = mElement->ReplaceData(mOffset,mReplaceLength,mStringToInsert); + } + if (NS_SUCCEEDED(result)) { + result = CollapseTextSelection(); + } + } + + return result; } NS_IMETHODIMP IMETextTxn::Undo(void) @@ -94,13 +97,16 @@ NS_IMETHODIMP IMETextTxn::Undo(void) printf("Undo IME Text element = %p\n", mElement.get()); #endif + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result; PRUint32 length = mStringToInsert.Length(); result = mElement->DeleteData(mOffset, length); if (NS_SUCCEEDED(result)) { // set the selection to the insertion point where the string was removed nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { result = selection->Collapse(mElement, mOffset); NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert."); @@ -111,67 +117,67 @@ NS_IMETHODIMP IMETextTxn::Undo(void) NS_IMETHODIMP IMETextTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction) { - nsresult result; + nsresult result; #ifdef DEBUG_TAGUE printf("Merge IME Text element = %p\n", mElement.get()); #endif - // - // check to make sure we have valid return pointers - // - if ((nsnull==aDidMerge) && (nsnull==aTransaction)) - { - return NS_OK; - } + // + // check to make sure we have valid return pointers + // + if ((nsnull==aDidMerge) && (nsnull==aTransaction)) + { + return NS_OK; + } - // - // check to make sure we aren't fixed, if we are then nothing get's absorbed - // - if (mFixed) { - *aDidMerge = PR_FALSE; - return NS_OK; - } + // + // check to make sure we aren't fixed, if we are then nothing get's absorbed + // + if (mFixed) { + *aDidMerge = PR_FALSE; + return NS_OK; + } - // - // if aTransaction is another IMETextTxn then absorbe it - // - IMETextTxn* otherTxn = nsnull; - result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn); - if (otherTxn && result==NS_OK) - { - // - // we absorbe the next IME transaction by adopting it's insert string as our own - // - nsIPrivateTextRangeList* newTextRangeList; - otherTxn->GetData(mStringToInsert,&newTextRangeList); - mRangeList = do_QueryInterface(newTextRangeList); - *aDidMerge = PR_TRUE; + // + // if aTransaction is another IMETextTxn then absorbe it + // + IMETextTxn* otherTxn = nsnull; + result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn); + if (otherTxn && result==NS_OK) + { + // + // we absorbe the next IME transaction by adopting it's insert string as our own + // + nsIPrivateTextRangeList* newTextRangeList; + otherTxn->GetData(mStringToInsert,&newTextRangeList); + mRangeList = do_QueryInterface(newTextRangeList); + *aDidMerge = PR_TRUE; #ifdef DEBUG_TAGUE - printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction); + printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction); #endif - NS_RELEASE(otherTxn); - return NS_OK; - } + NS_RELEASE(otherTxn); + return NS_OK; + } - // - // second possible case is that we have a commit transaction - // - IMECommitTxn* commitTxn = nsnull; - result = aTransaction->QueryInterface(IMECommitTxn::GetCID(),(void**)&commitTxn); - if (commitTxn && result==NS_OK) - { - (void)CollapseTextSelectionOnCommit(); - mFixed = PR_TRUE; - *aDidMerge = PR_TRUE; // absorbe the commit transaction + // + // second possible case is that we have a commit transaction + // + IMECommitTxn* commitTxn = nsnull; + result = aTransaction->QueryInterface(IMECommitTxn::GetCID(),(void**)&commitTxn); + if (commitTxn && result==NS_OK) + { + (void)CollapseTextSelectionOnCommit(); + mFixed = PR_TRUE; + *aDidMerge = PR_TRUE; // absorbe the commit transaction #ifdef DEBUG_TAGUE - printf("IMETextTxn assimilated IMECommitTxn%p\n", aTransaction); + printf("IMETextTxn assimilated IMECommitTxn%p\n", aTransaction); #endif - NS_RELEASE(commitTxn); - return NS_OK; - } + NS_RELEASE(commitTxn); + return NS_OK; + } - *aDidMerge = PR_FALSE; - return NS_OK; + *aDidMerge = PR_FALSE; + return NS_OK; } NS_IMETHODIMP IMETextTxn::Write(nsIOutputStream *aOutputStream) @@ -226,84 +232,88 @@ NS_IMETHODIMP IMETextTxn::GetData(nsString& aResult,nsIPrivateTextRangeList** aT NS_IMETHODIMP IMETextTxn::CollapseTextSelection(void) { - nsresult result; - PRBool haveSelectedRange, haveCaretPosition; - PRUint16 textRangeListLength,selectionStart,selectionEnd, - textRangeType, caretPosition, i; - nsIPrivateTextRange* textRange; + nsresult result; + PRBool haveSelectedRange, haveCaretPosition; + PRUint16 textRangeListLength,selectionStart,selectionEnd, + textRangeType, caretPosition, i; + nsIPrivateTextRange* textRange; - haveSelectedRange = PR_FALSE; - haveCaretPosition = PR_FALSE; - + haveSelectedRange = PR_FALSE; + haveCaretPosition = PR_FALSE; + #ifdef DEBUG_tague - PRUint16 listlen,start,stop,type; - nsIPrivateTextRange* rangePtr; - result = mRangeList->GetLength(&listlen); - printf("nsIPrivateTextRangeList[%p]\n",mRangeList); - for (i=0;iItem(i,&rangePtr); - rangePtr->GetRangeStart(&start); - rangePtr->GetRangeEnd(&stop); - rangePtr->GetRangeType(&type); - printf("range[%d] start=%d end=%d type=",i,start,stop,type); - if (type==nsIPrivateTextRange::TEXTRANGE_RAWINPUT) printf("TEXTRANGE_RAWINPUT\n"); - if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT) printf("TEXTRANGE_SELECTEDRAWTEXT\n"); - if (type==nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT) printf("TEXTRANGE_CONVERTEDTEXT\n"); - if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) printf("TEXTRANGE_SELECTEDCONVERTEDTEXT\n"); - } + PRUint16 listlen,start,stop,type; + nsIPrivateTextRange* rangePtr; + result = mRangeList->GetLength(&listlen); + printf("nsIPrivateTextRangeList[%p]\n",mRangeList); + for (i=0;iItem(i,&rangePtr); + rangePtr->GetRangeStart(&start); + rangePtr->GetRangeEnd(&stop); + rangePtr->GetRangeType(&type); + printf("range[%d] start=%d end=%d type=",i,start,stop,type); + if (type==nsIPrivateTextRange::TEXTRANGE_RAWINPUT) printf("TEXTRANGE_RAWINPUT\n"); + if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT) printf("TEXTRANGE_SELECTEDRAWTEXT\n"); + if (type==nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT) printf("TEXTRANGE_CONVERTEDTEXT\n"); + if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) printf("TEXTRANGE_SELECTEDCONVERTEDTEXT\n"); + } #endif - - // - // run through the text range list - // - result = mRangeList->GetLength(&textRangeListLength); - if (NS_SUCCEEDED(result)) - { - for(i=0;iItem(i,&textRange); - if (NS_SUCCEEDED(result)) - { - result = textRange->GetRangeType(&textRangeType); - if (textRangeType==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) - { - haveSelectedRange = PR_TRUE; - textRange->GetRangeStart(&selectionStart); - textRange->GetRangeEnd(&selectionEnd); - } - if (textRangeType==nsIPrivateTextRange::TEXTRANGE_CARETPOSITION) - { - haveCaretPosition = PR_TRUE; - textRange->GetRangeStart(&caretPosition); - } - } - } - } + + // + // run through the text range list + // + result = mRangeList->GetLength(&textRangeListLength); + if (NS_SUCCEEDED(result)) + { + for(i=0;iItem(i,&textRange); + if (NS_SUCCEEDED(result)) + { + result = textRange->GetRangeType(&textRangeType); + if (textRangeType==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT) + { + haveSelectedRange = PR_TRUE; + textRange->GetRangeStart(&selectionStart); + textRange->GetRangeEnd(&selectionEnd); + } + if (textRangeType==nsIPrivateTextRange::TEXTRANGE_CARETPOSITION) + { + haveCaretPosition = PR_TRUE; + textRange->GetRangeStart(&caretPosition); + } + } + } + } - nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_SUCCEEDED(result) && selection){ - if (haveSelectedRange) { - result = selection->Collapse(mElement,mOffset+selectionStart); - result = selection->Extend(mElement,mOffset+selectionEnd); - } else { - if (haveCaretPosition) - result = selection->Collapse(mElement,mOffset+caretPosition); - else - result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); - } - } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr selection; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_SUCCEEDED(result) && selection){ + if (haveSelectedRange) { + result = selection->Collapse(mElement,mOffset+selectionStart); + result = selection->Extend(mElement,mOffset+selectionEnd); + } else { + if (haveCaretPosition) + result = selection->Collapse(mElement,mOffset+caretPosition); + else + result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); + } + } - return result; + return result; } NS_IMETHODIMP IMETextTxn::CollapseTextSelectionOnCommit(void) { - nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_SUCCEEDED(result) && selection){ - result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); - } + nsCOMPtr selection; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_SUCCEEDED(result) && selection){ + result = selection->Collapse(mElement,mOffset+mStringToInsert.Length()); + } - return result; + return result; } diff --git a/mozilla/editor/libeditor/base/IMETextTxn.h b/mozilla/editor/libeditor/base/IMETextTxn.h index 6882505237b..9e4a70dd167 100644 --- a/mozilla/editor/libeditor/base/IMETextTxn.h +++ b/mozilla/editor/libeditor/base/IMETextTxn.h @@ -23,6 +23,7 @@ #include "nsIDOMCharacterData.h" #include "nsIPrivateTextRange.h" #include "nsCOMPtr.h" +#include "nsWeakPtr.h" // {D4D25721-2813-11d3-9EA3-0060089FE59B} #define IME_TEXT_TXN_CID \ @@ -61,7 +62,7 @@ public: PRUint32 aReplaceLength, nsIPrivateTextRangeList* aTextRangeList, const nsString& aString, - nsIPresShell* aPresShell); + nsWeakPtr aPresShell); private: @@ -112,7 +113,7 @@ protected: nsCOMPtr mRangeList; /** the presentation shell, which we'll need to get the selection */ - nsIPresShell* mPresShell; + nsWeakPtr mPresShellWeak; // use a weak reference PRBool mFixed; diff --git a/mozilla/editor/libeditor/base/InsertTextTxn.cpp b/mozilla/editor/libeditor/base/InsertTextTxn.cpp index 5f3a9442a31..70e3a539f31 100644 --- a/mozilla/editor/libeditor/base/InsertTextTxn.cpp +++ b/mozilla/editor/libeditor/base/InsertTextTxn.cpp @@ -52,7 +52,7 @@ InsertTextTxn::~InsertTextTxn() NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement, PRUint32 aOffset, const nsString &aStringToInsert, - nsIPresShell *aPresShell) + nsWeakPtr aPresShellWeak) { #if 0 //def DEBUG_cmanske nsString text; @@ -62,30 +62,32 @@ NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement, printf("\n"); #endif - NS_ASSERTION(aElement&&aPresShell, "bad args"); - if (!aElement || !aPresShell) return NS_ERROR_NULL_POINTER; + NS_ASSERTION(aElement && aPresShellWeak, "bad args"); + if (!aElement || !aPresShellWeak) return NS_ERROR_NULL_POINTER; mElement = do_QueryInterface(aElement); mOffset = aOffset; mStringToInsert = aStringToInsert; - mPresShell = aPresShell; + mPresShellWeak = aPresShellWeak; return NS_OK; } NS_IMETHODIMP InsertTextTxn::Do(void) { if (gNoisy) { printf("Do Insert Text element = %p\n", mElement.get()); } - NS_ASSERTION(mElement && mPresShell, "bad state"); - if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + NS_ASSERTION(mElement && mPresShellWeak, "bad state"); + if (!mElement || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; // advance caret: This requires the presentation shell to get the selection. nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_FAILED(result)) return result; - if (!selection) return NS_ERROR_NULL_POINTER; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_FAILED(result)) return result; + if (!selection) return NS_ERROR_NULL_POINTER; result = mElement->InsertData(mOffset, mStringToInsert); if (NS_SUCCEEDED(result)) - { + { result = selection->Collapse(mElement, mOffset+mStringToInsert.Length()); NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert."); } @@ -95,8 +97,10 @@ NS_IMETHODIMP InsertTextTxn::Do(void) NS_IMETHODIMP InsertTextTxn::Undo(void) { if (gNoisy) { printf("Undo Insert Text element = %p\n", mElement.get()); } - NS_ASSERTION(mElement && mPresShell, "bad state"); - if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + NS_ASSERTION(mElement && mPresShellWeak, "bad state"); + if (!mElement || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; nsresult result; PRUint32 length = mStringToInsert.Length(); @@ -104,9 +108,9 @@ NS_IMETHODIMP InsertTextTxn::Undo(void) if (NS_SUCCEEDED(result)) { // set the selection to the insertion point where the string was removed nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if (NS_FAILED(result)) return result; - if (!selection) return NS_ERROR_NULL_POINTER; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (NS_FAILED(result)) return result; + if (!selection) return NS_ERROR_NULL_POINTER; result = selection->Collapse(mElement, mOffset); NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert."); } diff --git a/mozilla/editor/libeditor/base/InsertTextTxn.h b/mozilla/editor/libeditor/base/InsertTextTxn.h index e34272405f7..72995eb7372 100644 --- a/mozilla/editor/libeditor/base/InsertTextTxn.h +++ b/mozilla/editor/libeditor/base/InsertTextTxn.h @@ -22,6 +22,7 @@ #include "EditTxn.h" #include "nsIDOMCharacterData.h" #include "nsCOMPtr.h" +#include "nsWeakPtr.h" #define INSERT_TEXT_TXN_CID \ {/* 93276f00-ab2c-11d2-8f4b-006008159b0c*/ \ @@ -55,7 +56,7 @@ public: NS_IMETHOD Init(nsIDOMCharacterData *aElement, PRUint32 aOffset, const nsString& aString, - nsIPresShell* aPresShell); + nsWeakPtr aPresShellWeak); private: @@ -101,7 +102,7 @@ protected: nsString mStringToInsert; /** the presentation shell, which we'll need to get the selection */ - nsIPresShell* mPresShell; + nsWeakPtr mPresShellWeak; // weak reference to the nsIPresShell friend class TransactionFactory; diff --git a/mozilla/editor/libeditor/base/nsEditor.cpp b/mozilla/editor/libeditor/base/nsEditor.cpp index 607cb18d299..b88c1d1eb1b 100644 --- a/mozilla/editor/libeditor/base/nsEditor.cpp +++ b/mozilla/editor/libeditor/base/nsEditor.cpp @@ -142,12 +142,12 @@ PRInt32 nsEditor::gInstanceCount = 0; //class implementations are in order they are declared in nsEditor.h nsEditor::nsEditor() -: mPresShell(nsnull) +: mPresShellWeak(nsnull) , mViewManager(nsnull) , mUpdateCount(0) , mActionListeners(nsnull) , mDocDirtyState(-1) -, mDoc(nsnull) +, mDocWeak(nsnull) , mPrefs(nsnull) { //initialize member variables here @@ -232,12 +232,14 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) return NS_ERROR_NULL_POINTER; mFlags = aFlags; - mDoc = aDoc; - mPresShell = aPresShell; // we don't addref the pres shell + mDocWeak = getter_AddRefs( NS_GetWeakReference(aDoc) ); // weak reference to doc + mPresShellWeak = getter_AddRefs( NS_GetWeakReference(aPresShell) ); // weak reference to pres shell + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; // disable links nsCOMPtr context; - mPresShell->GetPresContext(getter_AddRefs(context)); + ps->GetPresContext(getter_AddRefs(context)); if (!context) return NS_ERROR_NULL_POINTER; context->SetLinkHandler(0); @@ -251,11 +253,11 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) if (NS_FAILED(result)) { return result; } if (!mEditProperty) {return NS_ERROR_NULL_POINTER;} - mPresShell->GetViewManager(&mViewManager); + ps->GetViewManager(&mViewManager); if (!mViewManager) {return NS_ERROR_NULL_POINTER;} mViewManager->Release(); //we want a weak link - mPresShell->SetDisplayNonTextSelection(PR_TRUE);//we want to see all the selection reflected to user + ps->SetDisplayNonTextSelection(PR_TRUE);//we want to see all the selection reflected to user mUpdateCount=0; InsertTextTxn::ClassInit(); @@ -269,11 +271,11 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) /* Show the caret */ // XXX: I suppose it's legal to fail to get a caret, so don't propogate the // result of GetCaret - nsCOMPtr caret; - if (NS_SUCCEEDED(mPresShell->GetCaret(getter_AddRefs(caret))) && caret) + nsCOMPtr caret; + if (NS_SUCCEEDED(ps->GetCaret(getter_AddRefs(caret))) && caret) { - caret->SetCaretVisible(PR_TRUE); - caret->SetCaretReadOnly(PR_FALSE); + caret->SetCaretVisible(PR_TRUE); + caret->SetCaretReadOnly(PR_FALSE); } // NOTE: We don't fail if we can't get prefs or string bundles @@ -317,7 +319,7 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags) // Set the selection to the beginning: BeginningOfDocument(); - NS_POSTCONDITION(mDoc && mPresShell, "bad state"); + NS_POSTCONDITION(mDocWeak && mPresShellWeak, "bad state"); return NS_OK; } @@ -339,10 +341,11 @@ nsEditor::GetDocument(nsIDOMDocument **aDoc) if (!aDoc) return NS_ERROR_NULL_POINTER; *aDoc = nsnull; // init out param - NS_PRECONDITION(mDoc, "bad state, null mDoc"); - if (!mDoc) - return NS_ERROR_NOT_INITIALIZED; - return mDoc->QueryInterface(nsIDOMDocument::GetIID(), (void **)aDoc); + NS_PRECONDITION(mDocWeak, "bad state, mDocWeak weak pointer not initialized"); + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + return doc->QueryInterface(nsIDOMDocument::GetIID(), (void **)aDoc); } @@ -352,10 +355,11 @@ nsEditor::GetPresShell(nsIPresShell **aPS) if (!aPS) return NS_ERROR_NULL_POINTER; *aPS = nsnull; // init out param - NS_PRECONDITION(mPresShell, "bad state, null mPresShell"); - if (!mPresShell) - return NS_ERROR_NOT_INITIALIZED; - return mPresShell->QueryInterface(nsIPresShell::GetIID(), (void **)aPS); + NS_PRECONDITION(mPresShellWeak, "bad state, null mPresShellWeak"); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + return ps->QueryInterface(nsIPresShell::GetIID(), (void **)aPS); } @@ -365,7 +369,10 @@ nsEditor::GetSelection(nsIDOMSelection **aSelection) if (!aSelection) return NS_ERROR_NULL_POINTER; *aSelection = nsnull; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, aSelection); // does an addref + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, aSelection); // does an addref return result; } @@ -373,8 +380,8 @@ static NS_DEFINE_IID(kCFileWidgetCID, NS_FILEWIDGET_CID); NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy) { - nsresult rv = NS_OK; - + nsresult rv = NS_OK; + // get the document nsCOMPtr doc; rv = GetDocument(getter_AddRefs(doc)); @@ -389,29 +396,29 @@ NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy) // dialog. // find out if the doc already has a fileSpec associated with it. - nsFileSpec docFileSpec; + nsFileSpec docFileSpec; PRBool mustShowFileDialog = saveAs || (diskDoc->GetFileSpec(docFileSpec) == NS_ERROR_NOT_INITIALIZED); PRBool replacing = !saveAs; if (mustShowFileDialog) { - nsCOMPtr fileWidget; + nsCOMPtr fileWidget; rv = nsComponentManager::CreateInstance(kCFileWidgetCID, nsnull, nsIFileWidget::GetIID(), getter_AddRefs(fileWidget)); if (NS_SUCCEEDED(rv) && fileWidget) { - nsAutoString promptString("Save this document as:"); // XXX i18n, l10n - nsFileDlgResults dialogResult; - dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec); - if (dialogResult == nsFileDlgResults_Cancel) - return NS_OK; - - replacing = (dialogResult == nsFileDlgResults_Replace); - } - else - { - NS_ASSERTION(0, "Failed to get file widget"); - return rv; - } + nsAutoString promptString("Save this document as:"); // XXX i18n, l10n + nsFileDlgResults dialogResult; + dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec); + if (dialogResult == nsFileDlgResults_Cancel) + return NS_OK; + + replacing = (dialogResult == nsFileDlgResults_Replace); + } + else + { + NS_ASSERTION(0, "Failed to get file widget"); + return rv; + } } nsAutoString useDocCharset(""); @@ -520,7 +527,7 @@ nsEditor::Undo(PRUint32 aCount) if (NS_SUCCEEDED(result)) result = DoAfterUndoTransaction(); - + if (NS_FAILED(result)) break; } @@ -622,10 +629,13 @@ nsEditor::EndTransaction() // XXX: the rule system should tell us which node to select all on (ie, the root, or the body) NS_IMETHODIMP nsEditor::SelectAll() { - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { result = SelectEntireDocument(selection); @@ -635,15 +645,20 @@ NS_IMETHODIMP nsEditor::SelectAll() NS_IMETHODIMP nsEditor::BeginningOfDocument() { - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { nsCOMPtr nodeList; nsAutoString bodyTag = "body"; - result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if ((NS_SUCCEEDED(result)) && nodeList) { PRUint32 count; @@ -668,15 +683,20 @@ NS_IMETHODIMP nsEditor::BeginningOfDocument() NS_IMETHODIMP nsEditor::EndOfDocument() { - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } + if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; - nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { nsCOMPtr nodeList; nsAutoString bodyTag = "body"; - result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if ((NS_SUCCEEDED(result)) && nodeList) { PRUint32 count; @@ -741,10 +761,10 @@ nsEditor::GetDocumentCharacterSet(PRUnichar** characterSet) { presShell->GetDocument(getter_AddRefs(doc)); if (doc ) { - rv = doc->GetDocumentCharacterSet(character_set); - if (NS_SUCCEEDED(rv)) *characterSet=character_set.ToNewUnicode(); - return rv; - } + rv = doc->GetDocumentCharacterSet(character_set); + if (NS_SUCCEEDED(rv)) *characterSet=character_set.ToNewUnicode(); + return rv; + } } return rv; @@ -766,8 +786,8 @@ nsEditor::SetDocumentCharacterSet(const PRUnichar* characterSet) { presShell->GetDocument(getter_AddRefs(doc)); if (doc ) { - return doc->SetDocumentCharacterSet(character_set); - } + return doc->SetDocumentCharacterSet(character_set); + } } return rv; @@ -1140,7 +1160,9 @@ nsEditor::DebugDumpContent() const nsCOMPtrcontent; nsCOMPtrnodeList; nsAutoString bodyTag = "body"; - mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if (nodeList) { PRUint32 count; @@ -1179,97 +1201,103 @@ NS_IMETHODIMP nsEditor::BeginComposition(void) { #ifdef DEBUG_tague - printf("nsEditor::StartComposition\n"); + printf("nsEditor::StartComposition\n"); #endif - nsresult result; - PRInt32 offset; - nsCOMPtr selection; - nsCOMPtr nodeAsText; - - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); - if ((NS_SUCCEEDED(result)) && selection) - { - result = NS_ERROR_UNEXPECTED; - nsCOMPtr enumerator; + nsresult result; + PRInt32 offset; + nsCOMPtr selection; + nsCOMPtr nodeAsText; + + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if ((NS_SUCCEEDED(result)) && selection) + { + result = NS_ERROR_UNEXPECTED; + nsCOMPtr enumerator; result = selection->GetEnumerator(getter_AddRefs(enumerator)); if (NS_SUCCEEDED(result) && enumerator) - { - enumerator->First(); - nsCOMPtr currentItem; + { + enumerator->First(); + nsCOMPtr currentItem; result = enumerator->CurrentItem(getter_AddRefs(currentItem)); - if ((NS_SUCCEEDED(result)) && (currentItem)) - { - result = NS_ERROR_UNEXPECTED; - nsCOMPtr range(do_QueryInterface(currentItem)); - if (range) - { - nsCOMPtr node; - result = range->GetStartParent(getter_AddRefs(node)); - if ((NS_SUCCEEDED(result)) && (node)) - { - nodeAsText = do_QueryInterface(node); - range->GetStartOffset(&offset); - if (!nodeAsText) { - result = NS_ERROR_EDITOR_NO_TEXTNODE; - } - } - } - } - else - { - result = NS_ERROR_EDITOR_NO_SELECTION; - } - } - } + if ((NS_SUCCEEDED(result)) && (currentItem)) + { + result = NS_ERROR_UNEXPECTED; + nsCOMPtr range(do_QueryInterface(currentItem)); + if (range) + { + nsCOMPtr node; + result = range->GetStartParent(getter_AddRefs(node)); + if ((NS_SUCCEEDED(result)) && (node)) + { + nodeAsText = do_QueryInterface(node); + range->GetStartOffset(&offset); + if (!nodeAsText) { + result = NS_ERROR_EDITOR_NO_TEXTNODE; + } + } + } + } + else + { + result = NS_ERROR_EDITOR_NO_SELECTION; + } + } + } - if (NS_SUCCEEDED(result) && nodeAsText) - { - // - // store the information needed to construct IME transactions for this composition - // - mIMETextNode = nodeAsText; - mIMETextOffset = offset; - mIMEBufferLength = 0; - } + if (NS_SUCCEEDED(result) && nodeAsText) + { + // + // store the information needed to construct IME transactions for this composition + // + mIMETextNode = nodeAsText; + mIMETextOffset = offset; + mIMEBufferLength = 0; + } - return result; + return result; } NS_IMETHODIMP nsEditor::EndComposition(void) { - nsresult result; - IMECommitTxn *commitTxn; - - // - // create the commit transaction..we can do it directly from the transaction mgr - // + nsresult result; + IMECommitTxn *commitTxn; + + // + // create the commit transaction..we can do it directly from the transaction mgr + // result = TransactionFactory::GetNewTransaction(IMECommitTxn::GetCID(), (EditTxn**)&commitTxn); - if (NS_SUCCEEDED(result) && commitTxn!=nsnull) - { - commitTxn->Init(); - result = Do(commitTxn); - } + if (NS_SUCCEEDED(result) && commitTxn!=nsnull) + { + commitTxn->Init(); + result = Do(commitTxn); + } - /* reset the data we need to construct a transaction */ - mIMETextNode = do_QueryInterface(nsnull); - mIMETextOffset = 0; - mIMEBufferLength = 0; + /* reset the data we need to construct a transaction */ + mIMETextNode = do_QueryInterface(nsnull); + mIMETextOffset = 0; + mIMEBufferLength = 0; - return result; + return result; } NS_IMETHODIMP nsEditor::SetCompositionString(const nsString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply) { - nsCOMPtr caretP; - nsresult result = SetInputMethodText(aCompositionString,aTextRangeList); - mIMEBufferLength = aCompositionString.Length(); + nsCOMPtr caretP; + nsresult result = SetInputMethodText(aCompositionString,aTextRangeList); + mIMEBufferLength = aCompositionString.Length(); - mPresShell->GetCaret(getter_AddRefs(caretP)); - caretP->GetWindowRelativeCoordinates(aReply->mCursorPosition,aReply->mCursorIsCollapsed); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + ps->GetCaret(getter_AddRefs(caretP)); + caretP->GetWindowRelativeCoordinates(aReply->mCursorPosition,aReply->mCursorIsCollapsed); - return result; + return result; } #ifdef XP_MAC @@ -1292,14 +1320,16 @@ nsEditor::GetBodyElement(nsIDOMElement **aBodyElement) *aBodyElement = 0; - NS_PRECONDITION(mDoc, "bad state, null mDoc"); - if (!mDoc) + NS_PRECONDITION(mDocWeak, "bad state, null mDocWeak"); + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; nsCOMPtrnodeList; nsString bodyTag = "body"; - result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if (NS_FAILED(result)) return result; @@ -1474,7 +1504,7 @@ NS_IMETHODIMP nsEditor::InsertTextImpl(const nsString& aStringToInsert) newTextNode->SetData(placeholderText); selection->Collapse(newNode, 0); selection->Extend(newNode, 1); - result = InsertTextImpl(aStringToInsert); // this really recurses, right? + result = InsertTextImpl(aStringToInsert); // this really recurses, right? } } } @@ -1591,7 +1621,7 @@ NS_IMETHODIMP nsEditor::NotifyDocumentListeners(TDocumentListenerNotification aNotificationType) { if (!mDocStateListeners) - return NS_OK; // maybe there just aren't any. + return NS_OK; // maybe there just aren't any. PRUint32 numListeners; nsresult rv = mDocStateListeners->Count(&numListeners); @@ -1677,7 +1707,10 @@ NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsString & aStringToInsert, else { nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if ((NS_SUCCEEDED(result)) && selection) { #if 0 @@ -1734,7 +1767,7 @@ NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsString & aStringToInsert, { result = TransactionFactory::GetNewTransaction(InsertTextTxn::GetCID(), (EditTxn **)aTxn); if (nsnull!=*aTxn) { - result = (*aTxn)->Init(nodeAsText, offset, aStringToInsert, mPresShell); + result = (*aTxn)->Init(nodeAsText, offset, aStringToInsert, mPresShellWeak); } else { result = NS_ERROR_OUT_OF_MEMORY; @@ -1760,13 +1793,15 @@ NS_IMETHODIMP nsEditor::CreateTxnForInsertText(const nsString & aStringToInsert, NS_IMETHODIMP nsEditor::DoInitialInsert(const nsString & aStringToInsert) { - if (!mDoc) { + if (!mDocWeak) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtrnodeList; nsAutoString bodyTag = "body"; - nsresult result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsresult result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); if ((NS_SUCCEEDED(result)) && nodeList) { PRUint32 count; @@ -2758,9 +2793,11 @@ nsEditor::CountEditableChildren(nsIDOMNode *aNode, PRUint32 &outCount) NS_IMETHODIMP nsEditor::IncDocModCount(PRInt32 inNumMods) { - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr diskDoc = do_QueryInterface(mDoc); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr diskDoc = do_QueryInterface(doc); if (diskDoc) diskDoc->IncrementModCount(inNumMods); @@ -2771,9 +2808,11 @@ NS_IMETHODIMP nsEditor::IncDocModCount(PRInt32 inNumMods) NS_IMETHODIMP nsEditor::GetDocModCount(PRInt32 &outModCount) { - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr diskDoc = do_QueryInterface(mDoc); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr diskDoc = do_QueryInterface(doc); if (diskDoc) diskDoc->GetModCount(&outModCount); @@ -2783,9 +2822,11 @@ NS_IMETHODIMP nsEditor::GetDocModCount(PRInt32 &outModCount) NS_IMETHODIMP nsEditor::ResetDocModCount() { - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr diskDoc = do_QueryInterface(mDoc); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr diskDoc = do_QueryInterface(doc); if (diskDoc) diskDoc->ResetModCount(); @@ -2914,110 +2955,112 @@ nsEditor::GetFirstTextNode(nsIDOMNode *aNode, nsIDOMNode **aRetNode) NS_IMETHODIMP nsEditor::SetInputMethodText(const nsString& aStringToInsert, nsIPrivateTextRangeList *aTextRangeList) { - IMETextTxn *txn; - - nsresult result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&txn); // insert at the current selection - if ((NS_SUCCEEDED(result)) && txn) { - BeginUpdateViewBatch(); - result = Do(txn); - EndUpdateViewBatch(); - } - else if (NS_ERROR_EDITOR_NO_SELECTION==result) { - result = DoInitialInputMethodInsert(aStringToInsert,aTextRangeList); - } - else if (NS_ERROR_EDITOR_NO_TEXTNODE==result) - { - BeginTransaction(); - nsCOMPtr selection; - result = GetSelection(getter_AddRefs(selection)); - if ((NS_SUCCEEDED(result)) && selection) - { - nsCOMPtr selectedNode; - PRInt32 offset; - result = selection->GetAnchorNode(getter_AddRefs(selectedNode)); - if (NS_SUCCEEDED(result) && NS_SUCCEEDED(selection->GetAnchorOffset(&offset)) && selectedNode) - { - nsCOMPtr newNode; - result = CreateNode(GetTextNodeTag(), selectedNode, offset+1,getter_AddRefs(newNode)); - if (NS_SUCCEEDED(result) && newNode) - { - nsCOMPtrnewTextNode; - newTextNode = do_QueryInterface(newNode); - if (newTextNode) - { - nsAutoString placeholderText(" "); - newTextNode->SetData(placeholderText); - selection->Collapse(newNode, 0); - selection->Extend(newNode, 1); - result = SetInputMethodText(aStringToInsert,aTextRangeList); - } - } - } - } - - EndTransaction(); - } + IMETextTxn *txn; + + nsresult result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&txn); // insert at the current selection + if ((NS_SUCCEEDED(result)) && txn) { + BeginUpdateViewBatch(); + result = Do(txn); + EndUpdateViewBatch(); + } + else if (NS_ERROR_EDITOR_NO_SELECTION==result) { + result = DoInitialInputMethodInsert(aStringToInsert,aTextRangeList); + } + else if (NS_ERROR_EDITOR_NO_TEXTNODE==result) + { + BeginTransaction(); + nsCOMPtr selection; + result = GetSelection(getter_AddRefs(selection)); + if ((NS_SUCCEEDED(result)) && selection) + { + nsCOMPtr selectedNode; + PRInt32 offset; + result = selection->GetAnchorNode(getter_AddRefs(selectedNode)); + if (NS_SUCCEEDED(result) && NS_SUCCEEDED(selection->GetAnchorOffset(&offset)) && selectedNode) + { + nsCOMPtr newNode; + result = CreateNode(GetTextNodeTag(), selectedNode, offset+1,getter_AddRefs(newNode)); + if (NS_SUCCEEDED(result) && newNode) + { + nsCOMPtrnewTextNode; + newTextNode = do_QueryInterface(newNode); + if (newTextNode) + { + nsAutoString placeholderText(" "); + newTextNode->SetData(placeholderText); + selection->Collapse(newNode, 0); + selection->Extend(newNode, 1); + result = SetInputMethodText(aStringToInsert,aTextRangeList); + } + } + } + } + + EndTransaction(); + } - return result; + return result; } NS_IMETHODIMP nsEditor::DoInitialInputMethodInsert(const nsString & aStringToInsert, nsIPrivateTextRangeList* aTextRangeList) { - if (!mDoc) { - return NS_ERROR_NOT_INITIALIZED; - } + if (!mDocWeak) { + return NS_ERROR_NOT_INITIALIZED; + } - nsCOMPtrnodeList; - nsAutoString bodyTag = "body"; - nsresult result = mDoc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); - if ((NS_SUCCEEDED(result)) && nodeList) - { - PRUint32 count; - nodeList->GetLength(&count); - NS_ASSERTION(1==count, "there is not exactly 1 body in the document!"); - nsCOMPtrnode; - result = nodeList->Item(0, getter_AddRefs(node)); - if ((NS_SUCCEEDED(result)) && node) - { // now we've got the body tag. - // create transaction to insert the text node, - // and create a transaction to insert the text - CreateElementTxn *txn; - result = CreateTxnForCreateElement(GetTextNodeTag(), node, 0, &txn); - if ((NS_SUCCEEDED(result)) && txn) - { - result = Do(txn); - if (NS_SUCCEEDED(result)) - { - nsCOMPtrnewNode; - txn->GetNewNode(getter_AddRefs(newNode)); - if ((NS_SUCCEEDED(result)) && newNode) - { - nsCOMPtrnewTextNode; - newTextNode = do_QueryInterface(newNode); - if (newTextNode) - { - mIMETextNode = newTextNode; - mIMETextOffset = 0; - mIMEBufferLength = 0; + nsCOMPtrnodeList; + nsAutoString bodyTag = "body"; + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + nsresult result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList)); + if ((NS_SUCCEEDED(result)) && nodeList) + { + PRUint32 count; + nodeList->GetLength(&count); + NS_ASSERTION(1==count, "there is not exactly 1 body in the document!"); + nsCOMPtrnode; + result = nodeList->Item(0, getter_AddRefs(node)); + if ((NS_SUCCEEDED(result)) && node) + { // now we've got the body tag. + // create transaction to insert the text node, + // and create a transaction to insert the text + CreateElementTxn *txn; + result = CreateTxnForCreateElement(GetTextNodeTag(), node, 0, &txn); + if ((NS_SUCCEEDED(result)) && txn) + { + result = Do(txn); + if (NS_SUCCEEDED(result)) + { + nsCOMPtrnewNode; + txn->GetNewNode(getter_AddRefs(newNode)); + if ((NS_SUCCEEDED(result)) && newNode) + { + nsCOMPtrnewTextNode; + newTextNode = do_QueryInterface(newNode); + if (newTextNode) + { + mIMETextNode = newTextNode; + mIMETextOffset = 0; + mIMEBufferLength = 0; - IMETextTxn *IMETxn; - result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&IMETxn); - if (NS_SUCCEEDED(result)) { - result = Do(IMETxn); - } - } - else { - result = NS_ERROR_UNEXPECTED; - } - } - } - } - } - } + IMETextTxn *IMETxn; + result = CreateTxnForIMEText(aStringToInsert,aTextRangeList,&IMETxn); + if (NS_SUCCEEDED(result)) { + result = Do(IMETxn); + } + } + else { + result = NS_ERROR_UNEXPECTED; + } + } + } + } + } + } - return result; + return result; } /////////////////////////////////////////////////////////////////////////// // GetTag: digs out the atom for the tag of this node @@ -3286,7 +3329,7 @@ nsEditor::NextNodeInBlock(nsIDOMNode *aNode, IterDirection aDir) while (NS_COMFALSE == iter->IsDone()) { - if (NS_FAILED(iter->CurrentNode(getter_AddRefs(content)))) return nullNode; + if (NS_FAILED(iter->CurrentNode(getter_AddRefs(content)))) return nullNode; // ignore nodes that aren't elements or text, or that are the block parent node = do_QueryInterface(content); if (node && IsTextOrElementNode(node) && (node != blockParent) && (node.get() != aNode)) @@ -3388,12 +3431,14 @@ nsEditor::IsPreformatted(nsIDOMNode *aNode, PRBool *aResult) if (!aResult || !content) return NS_ERROR_NULL_POINTER; - if (!mPresShell) return NS_ERROR_NOT_INITIALIZED; + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; - result = mPresShell->GetPrimaryFrameFor(content, &frame); + result = ps->GetPrimaryFrameFor(content, &frame); if (NS_FAILED(result)) return result; - result = mPresShell->GetStyleContextFor(frame, getter_AddRefs(styleContext)); + result = ps->GetStyleContextFor(frame, getter_AddRefs(styleContext)); if (NS_FAILED(result)) return result; styleText = (const nsStyleText*)styleContext->GetStyleData(eStyleStruct_Text); @@ -3760,7 +3805,7 @@ nsEditor::DoAfterDoTransaction(nsITransaction *aTxn) if (modCount < 0) modCount = -modCount; - rv = IncDocModCount(1); // don't count transient transactions + rv = IncDocModCount(1); // don't count transient transactions } return rv; @@ -3772,7 +3817,7 @@ nsEditor::DoAfterUndoTransaction() { nsresult rv = NS_OK; - rv = IncDocModCount(-1); // all undoable transactions are non-transient + rv = IncDocModCount(-1); // all undoable transactions are non-transient return rv; } @@ -3782,7 +3827,7 @@ nsEditor::DoAfterRedoTransaction() { nsresult rv = NS_OK; - rv = IncDocModCount(1); // all redoable transactions are non-transient + rv = IncDocModCount(1); // all redoable transactions are non-transient return rv; } @@ -3898,7 +3943,10 @@ NS_IMETHODIMP nsEditor::CreateAggregateTxnForDeleteSelection(nsIAtom *aTxnName, // Get current selection and setup txn to delete it, // but only if selection exists (is not a collapsed "caret" state) nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (NS_SUCCEEDED(result) && selection) { PRBool collapsed; @@ -3918,22 +3966,22 @@ NS_IMETHODIMP nsEditor::CreateAggregateTxnForDeleteSelection(nsIAtom *aTxnName, NS_IMETHODIMP nsEditor::CreateTxnForIMEText(const nsString & aStringToInsert, - nsIPrivateTextRangeList* aTextRangeList, + nsIPrivateTextRangeList* aTextRangeList, IMETextTxn ** aTxn) { - nsresult result; + nsresult result; - if (mIMETextNode==nsnull) - BeginComposition(); + if (mIMETextNode==nsnull) + BeginComposition(); result = TransactionFactory::GetNewTransaction(IMETextTxn::GetCID(), (EditTxn **)aTxn); - if (nsnull!=*aTxn) { - result = (*aTxn)->Init(mIMETextNode,mIMETextOffset,mIMEBufferLength,aTextRangeList,aStringToInsert,mPresShell); - } - else { - result = NS_ERROR_OUT_OF_MEMORY; - } - return result; + if (nsnull!=*aTxn) { + result = (*aTxn)->Init(mIMETextNode,mIMETextOffset,mIMEBufferLength,aTextRangeList,aStringToInsert,mPresShellWeak); + } + else { + result = NS_ERROR_OUT_OF_MEMORY; + } + return result; } @@ -3976,7 +4024,10 @@ nsEditor::CreateTxnForDeleteSelection(nsIEditor::ESelectionCollapseDirection aAc nsresult result; nsCOMPtr selection; - result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + result = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if ((NS_SUCCEEDED(result)) && selection) { // Check whether the selection is collapsed and we should do nothing: diff --git a/mozilla/editor/libeditor/base/nsEditor.h b/mozilla/editor/libeditor/base/nsEditor.h index 2f8911e185b..51c7b934989 100644 --- a/mozilla/editor/libeditor/base/nsEditor.h +++ b/mozilla/editor/libeditor/base/nsEditor.h @@ -20,6 +20,7 @@ #define __editor_h__ #include "nsCOMPtr.h" +#include "nsWeakPtr.h" #include "prmon.h" #include "nsIEditor.h" @@ -584,7 +585,7 @@ protected: PRUint32 mFlags; // behavior flags. See nsIHTMLEditor.h for the flags we use. - nsIPresShell *mPresShell; + nsWeakPtr mPresShellWeak; // weak reference to the nsIPresShell nsIViewManager *mViewManager; PRUint32 mUpdateCount; nsCOMPtr mTxnMgr; @@ -604,10 +605,10 @@ protected: PRInt8 mDocDirtyState; // -1 = not initialized - nsIDOMDocument * mDoc; + nsWeakPtr mDocWeak; // weak reference to the nsIDOMDocument nsCOMPtr mDTD; // Services are not nsCOMPtr friendly - nsIPref* mPrefs; + nsIPref *mPrefs; static PRInt32 gInstanceCount; diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index 8d873747dc9..515a9c19d7e 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -304,8 +304,8 @@ nsHTMLEditor::PostCreate() NS_IMETHODIMP nsHTMLEditor::InstallEventListeners() { - NS_ASSERTION(mDoc, "no document set on this editor"); - if (!mDoc) return NS_ERROR_NOT_INITIALIZED; + NS_ASSERTION(mDocWeak, "no document set on this editor"); + if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED; nsresult result; // get a key listener @@ -358,7 +358,9 @@ printf("nsTextEditor.cpp: failed to get TextEvent Listener\n"); // get the DOM event receiver nsCOMPtr erP; - result = mDoc->QueryInterface(nsIDOMEventReceiver::GetIID(), getter_AddRefs(erP)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + result = doc->QueryInterface(nsIDOMEventReceiver::GetIID(), getter_AddRefs(erP)); if (NS_FAILED(result)) { HandleEventListenerError(); return result; @@ -2116,7 +2118,9 @@ nsHTMLEditor::CreateElementWithDefaults(const nsString& aTagName, nsIDOMElement* // go through the transaction system nsCOMPtrnewElement; - res = mDoc->CreateElement(realTagName, getter_AddRefs(newElement)); + nsCOMPtr doc = do_QueryReferent(mDocWeak); + if (!doc) return NS_ERROR_NOT_INITIALIZED; + res = doc->CreateElement(realTagName, getter_AddRefs(newElement)); if (NS_FAILED(res) || !newElement) return NS_ERROR_FAILURE; @@ -2205,7 +2209,7 @@ nsHTMLEditor::CreateElementWithDefaults(const nsString& aTagName, nsIDOMElement* // Set contents to the   character by concatanating the char code space += nbsp; // If we fail here, we return NS_OK anyway, since we have an OK cell node - nsresult result = mDoc->CreateTextNode(space, getter_AddRefs(newTextNode)); + nsresult result = doc->CreateTextNode(space, getter_AddRefs(newTextNode)); if (NS_FAILED(result)) return result; if (!newTextNode) return NS_ERROR_NULL_POINTER; @@ -2326,7 +2330,7 @@ DELETE_ANCHOR: NS_IMETHODIMP nsHTMLEditor::SetBackgroundColor(const nsString& aColor) { // nsresult result; - NS_PRECONDITION(mDoc, "Missing Editor DOM Document"); + NS_PRECONDITION(mDocWeak, "Missing Editor DOM Document"); // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level // For initial testing, just set the background on the BODY tag (the document's background) @@ -2353,7 +2357,7 @@ NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsString& aAttribute, const n nsresult res; // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level - NS_ASSERTION(mDoc, "Missing Editor DOM Document"); + NS_ASSERTION(mDocWeak, "Missing Editor DOM Document"); // Set the background color attribute on the body tag nsCOMPtr bodyElement; @@ -2523,7 +2527,10 @@ NS_IMETHODIMP nsHTMLEditor::ApplyStyleSheet(const nsString& aURL) if (NS_SUCCEEDED(rv)) { nsCOMPtr document; - rv = mPresShell->GetDocument(getter_AddRefs(document)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + rv = ps->GetDocument(getter_AddRefs(document)); if (NS_SUCCEEDED(rv)) { if (document) { @@ -2592,11 +2599,14 @@ nsHTMLEditor::GetBodyStyleContext(nsIStyleContext** aStyleContext) nsCOMPtr content = do_QueryInterface(body); nsIFrame *frame; - res = mPresShell->GetPrimaryFrameFor(content, &frame); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + res = ps->GetPrimaryFrameFor(content, &frame); if (NS_FAILED(res)) return res; nsCOMPtr styleContext; - return mPresShell->GetStyleContextFor(frame, aStyleContext); + return ps->GetStyleContextFor(frame, aStyleContext); } // @@ -2967,7 +2977,10 @@ nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList) NS_IMETHODIMP nsHTMLEditor::Cut() { nsCOMPtr selection; - nsresult res = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + nsresult res = ps->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (!NS_SUCCEEDED(res)) return res; @@ -2985,7 +2998,10 @@ NS_IMETHODIMP nsHTMLEditor::Copy() { //printf("nsEditor::Copy\n"); - return mPresShell->DoCopy(); + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + return ps->DoCopy(); } NS_IMETHODIMP nsHTMLEditor::Paste() @@ -3303,22 +3319,24 @@ void nsHTMLEditor::ApplyStyleSheetToPresShellDocument(nsICSSStyleSheet* aSheet, NS_IMETHODIMP nsHTMLEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject) { nsresult result = NS_ERROR_FAILURE; // we return an error unless we get the index - if( mPresShell != nsnull ) - { - if ((nsnull!=aNode)) - { // get the content interface - nsCOMPtr nodeAsContent( do_QueryInterface(aNode) ); - if (nodeAsContent) - { // get the frame from the content interface - //Note: frames are not ref counted, so don't use an nsCOMPtr - *aLayoutObject = nsnull; - result = mPresShell->GetLayoutObjectFor(nodeAsContent, aLayoutObject); - } - } - else { - result = NS_ERROR_NULL_POINTER; + if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + nsCOMPtr ps = do_QueryReferent(mPresShellWeak); + if (!ps) return NS_ERROR_NOT_INITIALIZED; + + if ((nsnull!=aNode)) + { // get the content interface + nsCOMPtr nodeAsContent( do_QueryInterface(aNode) ); + if (nodeAsContent) + { // get the frame from the content interface + //Note: frames are not ref counted, so don't use an nsCOMPtr + *aLayoutObject = nsnull; + result = ps->GetLayoutObjectFor(nodeAsContent, aLayoutObject); } } + else { + result = NS_ERROR_NULL_POINTER; + } + return result; } diff --git a/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp b/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp index a4379eea198..0ef4f21eafa 100644 --- a/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp +++ b/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp @@ -136,84 +136,84 @@ nsTextEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) // PRBool keyProcessed; // ProcessShortCutKeys(aKeyEvent, keyProcessed); // if (PR_FALSE==keyProcessed) - { - if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) && - NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) && - NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) && - NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) && - NS_SUCCEEDED(uiEvent->GetMetaKey(&metaKey))) - { - { - switch(keyCode) { - // case nsIDOMUIEvent::VK_BACK: - // mEditor->DeleteSelection(nsIEditor::eDeleteLeft); - // break; + { + if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) && + NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) && + NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) && + NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) && + NS_SUCCEEDED(uiEvent->GetMetaKey(&metaKey))) + { + { + switch(keyCode) { + // case nsIDOMUIEvent::VK_BACK: + // mEditor->DeleteSelection(nsIEditor::eDeleteLeft); + // break; - case nsIDOMUIEvent::VK_DELETE: - mEditor->DeleteSelection(nsIEditor::eDeleteNext); - break; + case nsIDOMUIEvent::VK_DELETE: + mEditor->DeleteSelection(nsIEditor::eDeleteNext); + break; - // case nsIDOMUIEvent::VK_RETURN: - //case nsIDOMUIEvent::VK_ENTER: // why does this not exist? - // Need to implement creation of either

or
nodes. - // mEditor->InsertBreak(); - // break; + // case nsIDOMUIEvent::VK_RETURN: + //case nsIDOMUIEvent::VK_ENTER: // why does this not exist? + // Need to implement creation of either

or
nodes. + // mEditor->InsertBreak(); + // break; - case nsIDOMUIEvent::VK_LEFT: - case nsIDOMUIEvent::VK_RIGHT: - case nsIDOMUIEvent::VK_UP: - case nsIDOMUIEvent::VK_DOWN: - // these have already been handled in nsRangeList. Why are we getting them - // again here (Mac)? In switch to avoid putting in bogus chars. + case nsIDOMUIEvent::VK_LEFT: + case nsIDOMUIEvent::VK_RIGHT: + case nsIDOMUIEvent::VK_UP: + case nsIDOMUIEvent::VK_DOWN: + // these have already been handled in nsRangeList. Why are we getting them + // again here (Mac)? In switch to avoid putting in bogus chars. - //return NS_OK to allow page scrolling. - return NS_OK; - break; + //return NS_OK to allow page scrolling. + return NS_OK; + break; - case nsIDOMUIEvent::VK_HOME: - case nsIDOMUIEvent::VK_END: - // who handles these? - #if DEBUG - printf("Key not handled\n"); - #endif - return NS_OK; - break; + case nsIDOMUIEvent::VK_HOME: + case nsIDOMUIEvent::VK_END: + // who handles these? + #if DEBUG + printf("Key not handled\n"); + #endif + return NS_OK; + break; - case nsIDOMUIEvent::VK_PAGE_UP: - case nsIDOMUIEvent::VK_PAGE_DOWN: - //return NS_OK to allow page scrolling. - return NS_OK; - break; + case nsIDOMUIEvent::VK_PAGE_UP: + case nsIDOMUIEvent::VK_PAGE_DOWN: + //return NS_OK to allow page scrolling. + return NS_OK; + break; - case nsIDOMUIEvent::VK_TAB: - { - PRUint32 flags=0; - mEditor->GetFlags(&flags); - if (! (flags & nsIHTMLEditor::eEditorSingleLineMask)) - { - if (metaKey || altKey) - return NS_OK; // don't consume - // else we insert the tab straight through - nsAutoString key; - key += keyCode; + case nsIDOMUIEvent::VK_TAB: + { + PRUint32 flags=0; + mEditor->GetFlags(&flags); + if (! (flags & nsIHTMLEditor::eEditorSingleLineMask)) + { + if (metaKey || altKey) + return NS_OK; // don't consume + // else we insert the tab straight through + nsAutoString key; + key += keyCode; - nsCOMPtr htmlEditor = do_QueryInterface(mEditor); - if (htmlEditor) - htmlEditor->InsertText(key); - return NS_ERROR_BASE; // this means "I handled the event, don't do default processing" - } - else { - return NS_OK; - } - break; - } + nsCOMPtr htmlEditor = do_QueryInterface(mEditor); + if (htmlEditor) + htmlEditor->InsertText(key); + return NS_ERROR_BASE; // this means "I handled the event, don't do default processing" + } + else { + return NS_OK; + } + break; + } - default: - return NS_OK; // this indicates that we have not handled the keyDown event in any way. - } - } - } - } + default: + return NS_OK; // this indicates that we have not handled the keyDown event in any way. + } + } + } + } return NS_ERROR_BASE; } @@ -229,70 +229,74 @@ nsTextEditorKeyListener::KeyUp(nsIDOMEvent* aKeyEvent) nsresult nsTextEditorKeyListener::KeyPress(nsIDOMEvent* aKeyEvent) { - nsAutoString key; - PRUint32 character; - PRUint32 keyCode; + nsAutoString key; + PRUint32 character; + PRUint32 keyCode; - nsCOMPtruiEvent; - uiEvent = do_QueryInterface(aKeyEvent); - if (!uiEvent) { - //non-key event passed to keydown. bad things. - return NS_OK; - } - // - // look at the keyCode if it is return or backspace, process it - // we handle these two special characters here because it makes windows integration - // eaiser - // + nsCOMPtruiEvent; + uiEvent = do_QueryInterface(aKeyEvent); + if (!uiEvent) + { + //non-key event passed to keydown. bad things. + return NS_OK; + } + // + // look at the keyCode if it is return or backspace, process it + // we handle these two special characters here because it makes windows integration + // eaiser + // PRBool keyProcessed; ProcessShortCutKeys(aKeyEvent, keyProcessed); if (PR_FALSE==keyProcessed) { PRUint32 flags; - PRBool ctrlKey, altKey, metaKey; - uiEvent->GetCtrlKey(&ctrlKey); - uiEvent->GetAltKey(&altKey); - uiEvent->GetMetaKey(&metaKey); + PRBool ctrlKey, altKey, metaKey; + uiEvent->GetCtrlKey(&ctrlKey); + uiEvent->GetAltKey(&altKey); + uiEvent->GetMetaKey(&metaKey); - if (metaKey) - return NS_OK; // don't consume + if (metaKey) + return NS_OK; // don't consume - nsCOMPtr htmlEditor = do_QueryInterface(mEditor); - if (!htmlEditor) return NS_ERROR_NO_INTERFACE; + nsCOMPtr htmlEditor = do_QueryInterface(mEditor); + if (!htmlEditor) return NS_ERROR_NO_INTERFACE; - if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode))) - { - if (nsIDOMUIEvent::VK_BACK==keyCode) { - mEditor->DeleteSelection(nsIEditor::eDeletePrevious); - return NS_ERROR_BASE; // consumed - } - if (nsIDOMUIEvent::VK_RETURN==keyCode) + if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode))) + { + if (nsIDOMUIEvent::VK_BACK==keyCode) + { + mEditor->DeleteSelection(nsIEditor::eDeletePrevious); + return NS_ERROR_BASE; // consumed + } + if (nsIDOMUIEvent::VK_RETURN==keyCode) { mEditor->GetFlags(&flags); if (!(flags & nsIHTMLEditor::eEditorSingleLineMask)) { - htmlEditor->InsertBreak(); - return NS_ERROR_BASE; // consumed + htmlEditor->InsertBreak(); + return NS_ERROR_BASE; // consumed } - else { + else + { return NS_OK; } - } - } - - if ((PR_FALSE==altKey) && (PR_FALSE==ctrlKey) && - (NS_SUCCEEDED(uiEvent->GetCharCode(&character)))) - { - if (nsIDOMUIEvent::VK_TAB==character) { - return NS_OK; // ignore tabs here, they're handled in keyDown if at all - } - key += character; - htmlEditor->InsertText(key); - } - } + } + } + + if ((PR_FALSE==altKey) && (PR_FALSE==ctrlKey) && + (NS_SUCCEEDED(uiEvent->GetCharCode(&character)))) + { + if (nsIDOMUIEvent::VK_TAB==character) + { + return NS_OK; // ignore tabs here, they're handled in keyDown if at all + } + key += character; + htmlEditor->InsertText(key); + } + } - return NS_ERROR_BASE; // consumed + return NS_ERROR_BASE; // consumed } @@ -321,8 +325,7 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) && NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) && NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) && - NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) - ) + NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) ) { if (PR_TRUE==ctrlKey) { aProcessed = PR_TRUE; @@ -385,11 +388,11 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr nsCOMPtr mailEditor = do_QueryInterface(mEditor); if (mailEditor) { - PRInt32 wrap; - if (NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - printf("Currently wrapping to %d\n", wrap); - else - printf("GetBodyWrapWidth returned an error\n"); + PRInt32 wrap; + if (NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + printf("Currently wrapping to %d\n", wrap); + else + printf("GetBodyWrapWidth returned an error\n"); } } break; @@ -398,24 +401,24 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // hard coded "Decrease wrap size" if (PR_TRUE==altKey) { - nsCOMPtr mailEditor = do_QueryInterface(mEditor); - if (mailEditor) - { - aProcessed=PR_TRUE; - PRInt32 wrap; - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("GetBodyWrapWidth returned an error\n"); - break; - } - mailEditor->SetBodyWrapWidth(wrap - 5); - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("Second GetBodyWrapWidth returned an error\n"); - break; - } - else printf("Now wrapping to %d\n", wrap); - } + nsCOMPtr mailEditor = do_QueryInterface(mEditor); + if (mailEditor) + { + aProcessed=PR_TRUE; + PRInt32 wrap; + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("GetBodyWrapWidth returned an error\n"); + break; + } + mailEditor->SetBodyWrapWidth(wrap - 5); + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("Second GetBodyWrapWidth returned an error\n"); + break; + } + else printf("Now wrapping to %d\n", wrap); + } } break; @@ -423,24 +426,24 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // hard coded "Increase wrap size" if (PR_TRUE==altKey) { - nsCOMPtr mailEditor = do_QueryInterface(mEditor); - if (mailEditor) - { - aProcessed=PR_TRUE; - PRInt32 wrap; - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("GetBodyWrapWidth returned an error\n"); - break; - } - mailEditor->SetBodyWrapWidth(wrap + 5); - if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) - { - printf("Second GetBodyWrapWidth returned an error\n"); - break; - } - else printf("Now wrapping to %d\n", wrap); - } + nsCOMPtr mailEditor = do_QueryInterface(mEditor); + if (mailEditor) + { + aProcessed=PR_TRUE; + PRInt32 wrap; + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("GetBodyWrapWidth returned an error\n"); + break; + } + mailEditor->SetBodyWrapWidth(wrap + 5); + if (!NS_SUCCEEDED(mailEditor->GetBodyWrapWidth(&wrap))) + { + printf("Second GetBodyWrapWidth returned an error\n"); + break; + } + else printf("Now wrapping to %d\n", wrap); + } } break; @@ -818,7 +821,7 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr // the unit tests are only exposed through nsIEditor nsCOMPtr editor = do_QueryInterface(mEditor); if (editor) - editor->DebugUnitTests(&numTests, &numFailed); + editor->DebugUnitTests(&numTests, &numFailed); } } break; @@ -895,20 +898,20 @@ nsTextEditorMouseListener::HandleEvent(nsIDOMEvent* aEvent) nsresult IsNodeInSelection(nsIDOMNode *aInNode, nsIDOMSelection *aInSelection, PRBool &aOutIsInSel) { - aOutIsInSel = PR_FALSE; // init out-param - if (!aInNode || !aInSelection) { return NS_ERROR_NULL_POINTER; } + aOutIsInSel = PR_FALSE; // init out-param + if (!aInNode || !aInSelection) { return NS_ERROR_NULL_POINTER; } nsCOMPtriter; nsresult result = nsComponentManager::CreateInstance(kContentIteratorCID, nsnull, nsIContentIterator::GetIID(), - getter_AddRefs(iter)); - if (NS_FAILED(result)) { return result; } - if (!iter) { return NS_ERROR_OUT_OF_MEMORY; } + getter_AddRefs(iter)); + if (NS_FAILED(result)) { return result; } + if (!iter) { return NS_ERROR_OUT_OF_MEMORY; } - nsCOMPtr enumerator; + nsCOMPtr enumerator; result = aInSelection->GetEnumerator(getter_AddRefs(enumerator)); if (NS_FAILED(result)) { return result; } - if (!enumerator) { return NS_ERROR_OUT_OF_MEMORY; } + if (!enumerator) { return NS_ERROR_OUT_OF_MEMORY; } for (enumerator->First(); NS_OK!=enumerator->IsDone(); enumerator->Next()) { @@ -918,31 +921,31 @@ IsNodeInSelection(nsIDOMNode *aInNode, nsIDOMSelection *aInSelection, PRBool &aO { nsCOMPtr range( do_QueryInterface(currentItem) ); - iter->Init(range); - nsCOMPtr currentContent; - iter->CurrentNode(getter_AddRefs(currentContent)); - while (NS_COMFALSE == iter->IsDone()) - { - nsCOMPtrcurrentNode = do_QueryInterface(currentContent); - if (currentNode.get()==aInNode) - { - // if it's a start or end node, need to test whether the (x,y) - // of the event falls within the selection + iter->Init(range); + nsCOMPtr currentContent; + iter->CurrentNode(getter_AddRefs(currentContent)); + while (NS_COMFALSE == iter->IsDone()) + { + nsCOMPtrcurrentNode = do_QueryInterface(currentContent); + if (currentNode.get()==aInNode) + { + // if it's a start or end node, need to test whether the (x,y) + // of the event falls within the selection - // talk to Mike + // talk to Mike - aOutIsInSel = PR_TRUE; - return NS_OK; - } - /* do not check result here, and especially do not return the result code. - * we rely on iter->IsDone to tell us when the iteration is complete - */ - iter->Next(); - iter->CurrentNode(getter_AddRefs(currentContent)); - } - } - } - return NS_OK; + aOutIsInSel = PR_TRUE; + return NS_OK; + } + /* do not check result here, and especially do not return the result code. + * we rely on iter->IsDone to tell us when the iteration is complete + */ + iter->Next(); + iter->CurrentNode(getter_AddRefs(currentContent)); + } + } + } + return NS_OK; } @@ -959,136 +962,136 @@ nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent) return NS_OK; } - nsCOMPtr editor (do_QueryInterface(mEditor)); + nsCOMPtr editor (do_QueryInterface(mEditor)); if (!editor) { return NS_OK; } - // get the document - nsCOMPtrdomDoc; + // get the document + nsCOMPtrdomDoc; editor->GetDocument(getter_AddRefs(domDoc)); - if (!domDoc) { return NS_OK; } - nsCOMPtrdoc = do_QueryInterface(domDoc); - if (!doc) { return NS_OK; } + if (!domDoc) { return NS_OK; } + nsCOMPtrdoc = do_QueryInterface(domDoc); + if (!doc) { return NS_OK; } PRUint16 button = 0; uiEvent->GetButton(&button); - // left button click might be a drag-start - if (1==button) - { + // left button click might be a drag-start + if (1==button) + { #ifndef EXPERIMENTAL_DRAG_CODE - return NS_OK; + return NS_OK; #else - nsString XIFBuffer; - // get the DOM select here - nsCOMPtr sel; - editor->GetSelection(getter_AddRefs(sel)); + nsString XIFBuffer; + // get the DOM select here + nsCOMPtr sel; + editor->GetSelection(getter_AddRefs(sel)); - // convert the DOMselection to XIF - if (sel) - { - // if we are within the selection, start the drag + // convert the DOMselection to XIF + if (sel) + { + // if we are within the selection, start the drag - nsCOMPtr target; - nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target)); - if (NS_FAILED(rv) || !target) { return NS_OK; } - PRBool isInSel; - rv = IsNodeInSelection(target, sel, isInSel); - if (NS_FAILED(rv) || PR_FALSE==isInSel) { return NS_OK; } - doc->CreateXIF(XIFBuffer, sel); + nsCOMPtr target; + nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target)); + if (NS_FAILED(rv) || !target) { return NS_OK; } + PRBool isInSel; + rv = IsNodeInSelection(target, sel, isInSel); + if (NS_FAILED(rv) || PR_FALSE==isInSel) { return NS_OK; } + doc->CreateXIF(XIFBuffer, sel); - // Get the Clipboard - nsIClipboard* clipboard; - rv = nsServiceManager::GetService(kCClipboardCID, - nsIClipboard::GetIID(), - (nsISupports **)&clipboard); - if (NS_OK == rv) - { - // Create a data flavor to tell the transferable - // that it is about to receive XIF - nsAutoString flavor(kXIFMime); + // Get the Clipboard + nsIClipboard* clipboard; + rv = nsServiceManager::GetService(kCClipboardCID, + nsIClipboard::GetIID(), + (nsISupports **)&clipboard); + if (NS_OK == rv) + { + // Create a data flavor to tell the transferable + // that it is about to receive XIF + nsAutoString flavor(kXIFMime); - // Create a transferable for putting data on the Clipboard - nsCOMPtr trans; - rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull, - nsITransferable::GetIID(), - (void**) getter_AddRefs(trans)); - if (NS_OK == rv) { - // The data on the clipboard will be in "XIF" format - // so give the clipboard transferable a "XIFConverter" for - // converting from XIF to other formats - nsCOMPtr xifConverter; - rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull, - nsIFormatConverter::GetIID(), (void**) getter_AddRefs(xifConverter)); - if (NS_OK == rv) { - // Add the XIF DataFlavor to the transferable - // this tells the transferable that it can handle receiving the XIF format - trans->AddDataFlavor(&flavor); + // Create a transferable for putting data on the Clipboard + nsCOMPtr trans; + rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull, + nsITransferable::GetIID(), + (void**) getter_AddRefs(trans)); + if (NS_OK == rv) { + // The data on the clipboard will be in "XIF" format + // so give the clipboard transferable a "XIFConverter" for + // converting from XIF to other formats + nsCOMPtr xifConverter; + rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull, + nsIFormatConverter::GetIID(), (void**) getter_AddRefs(xifConverter)); + if (NS_OK == rv) { + // Add the XIF DataFlavor to the transferable + // this tells the transferable that it can handle receiving the XIF format + trans->AddDataFlavor(&flavor); - // Add the converter for going from XIF to other formats - trans->SetConverter(xifConverter); + // Add the converter for going from XIF to other formats + trans->SetConverter(xifConverter); - // Now add the XIF data to the transferable - // the transferable wants the number bytes for the data and since it is double byte - // we multiply by 2 - trans->SetTransferData(&flavor, XIFBuffer.ToNewUnicode(), XIFBuffer.Length()*2); + // Now add the XIF data to the transferable + // the transferable wants the number bytes for the data and since it is double byte + // we multiply by 2 + trans->SetTransferData(&flavor, XIFBuffer.ToNewUnicode(), XIFBuffer.Length()*2); - // Now invoke the drag session - nsIDragService* dragService; - nsresult rv = nsServiceManager::GetService(kCDragServiceCID, - nsIDragService::GetIID(), - (nsISupports **)&dragService); - if (NS_OK == rv) { - nsCOMPtr items; - NS_NewISupportsArray(getter_AddRefs(items)); - if ( items ) { - items->AppendElement(trans); - dragService->InvokeDragSession(items, nsnull, nsIDragService::DRAGDROP_ACTION_COPY | nsIDragService::DRAGDROP_ACTION_MOVE); - } - nsServiceManager::ReleaseService(kCDragServiceCID, dragService); - } - } - } - nsServiceManager::ReleaseService(kCClipboardCID, clipboard); - } - return NS_ERROR_BASE; // return that we've handled the event - } + // Now invoke the drag session + nsIDragService* dragService; + nsresult rv = nsServiceManager::GetService(kCDragServiceCID, + nsIDragService::GetIID(), + (nsISupports **)&dragService); + if (NS_OK == rv) { + nsCOMPtr items; + NS_NewISupportsArray(getter_AddRefs(items)); + if ( items ) { + items->AppendElement(trans); + dragService->InvokeDragSession(items, nsnull, nsIDragService::DRAGDROP_ACTION_COPY | nsIDragService::DRAGDROP_ACTION_MOVE); + } + nsServiceManager::ReleaseService(kCDragServiceCID, dragService); + } + } + } + nsServiceManager::ReleaseService(kCClipboardCID, clipboard); + } + return NS_ERROR_BASE; // return that we've handled the event + } #endif } // middle-mouse click (paste); else if (button == 2) - { + { // Set the selection to the point under the mouse cursor: - nsCOMPtr mouseEvent (do_QueryInterface(aMouseEvent)); + nsCOMPtr mouseEvent (do_QueryInterface(aMouseEvent)); - if (!mouseEvent) - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - nsCOMPtr parent; - if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent)))) - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - PRInt32 offset = 0; - if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset))) - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + if (!mouseEvent) + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + nsCOMPtr parent; + if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent)))) + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + PRInt32 offset = 0; + if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset))) + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - nsCOMPtr selection; - if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection)))) - (void)selection->Collapse(parent, offset); + nsCOMPtr selection; + if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection)))) + (void)selection->Collapse(parent, offset); - // If the ctrl key is pressed, we'll do paste as quotation. - // Would've used the alt key, but the kde wmgr treats alt-middle specially. - PRBool ctrlKey = PR_FALSE; - uiEvent->GetCtrlKey(&ctrlKey); + // If the ctrl key is pressed, we'll do paste as quotation. + // Would've used the alt key, but the kde wmgr treats alt-middle specially. + PRBool ctrlKey = PR_FALSE; + uiEvent->GetCtrlKey(&ctrlKey); - if (ctrlKey) - { - nsCOMPtr mailEditor = do_QueryInterface(mEditor); - if (mailEditor) - mailEditor->PasteAsQuotation(); - } - editor->Paste(); - return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". - } - return NS_OK; // did not process the event + if (ctrlKey) + { + nsCOMPtr mailEditor = do_QueryInterface(mEditor); + if (mailEditor) + mailEditor->PasteAsQuotation(); + } + editor->Paste(); + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". + } + return NS_OK; // did not process the event } @@ -1113,7 +1116,7 @@ nsTextEditorMouseListener::MouseClick(nsIDOMEvent* aMouseEvent) nsresult nsTextEditorMouseListener::MouseDblClick(nsIDOMEvent* aMouseEvent) { - nsCOMPtr htmlEditor = do_QueryInterface(mEditor); + nsCOMPtr htmlEditor = do_QueryInterface(mEditor); if (htmlEditor) { nsCOMPtr selectedElement; @@ -1163,8 +1166,8 @@ NS_IMPL_RELEASE(nsTextEditorTextListener) nsTextEditorTextListener::nsTextEditorTextListener() -: mCommitText(PR_FALSE), - mInTransaction(PR_FALSE) +: mCommitText(PR_FALSE), + mInTransaction(PR_FALSE) { NS_INIT_REFCNT(); } @@ -1211,26 +1214,26 @@ nsTextEditorTextListener::HandleEvent(nsIDOMEvent* aEvent) nsresult nsTextEditorTextListener::HandleText(nsIDOMEvent* aTextEvent) { - nsString composedText; - nsresult result = NS_OK; - nsCOMPtr textEvent; - nsIPrivateTextRangeList *textRangeList; - nsTextEventReply *textEventReply; + nsString composedText; + nsresult result = NS_OK; + nsCOMPtr textEvent; + nsIPrivateTextRangeList *textRangeList; + nsTextEventReply *textEventReply; - textEvent = do_QueryInterface(aTextEvent); - if (!textEvent) { - //non-ui event passed in. bad things. - return NS_OK; - } + textEvent = do_QueryInterface(aTextEvent); + if (!textEvent) { + //non-ui event passed in. bad things. + return NS_OK; + } - textEvent->GetText(composedText); - textEvent->GetInputRange(&textRangeList); - textEvent->GetEventReply(&textEventReply); - textRangeList->AddRef(); - nsCOMPtr imeEditor = do_QueryInterface(mEditor, &result); - if (imeEditor) + textEvent->GetText(composedText); + textEvent->GetInputRange(&textRangeList); + textEvent->GetEventReply(&textEventReply); + textRangeList->AddRef(); + nsCOMPtr imeEditor = do_QueryInterface(mEditor, &result); + if (imeEditor) result = imeEditor->SetCompositionString(composedText,textRangeList,textEventReply); - return result; + return result; } /* @@ -1455,7 +1458,7 @@ nsTextEditorCompositionListener::HandleEvent(nsIDOMEvent* aEvent) void nsTextEditorCompositionListener::SetEditor(nsIEditor *aEditor) { nsCOMPtr imeEditor = do_QueryInterface(aEditor); - if (!imeEditor) return; // should return an error here! + if (!imeEditor) return; // should return an error here! // note that we don't hold an extra reference here. mEditor = imeEditor; @@ -1464,13 +1467,13 @@ void nsTextEditorCompositionListener::SetEditor(nsIEditor *aEditor) nsresult nsTextEditorCompositionListener::HandleStartComposition(nsIDOMEvent* aCompositionEvent) { - return mEditor->BeginComposition(); + return mEditor->BeginComposition(); } nsresult nsTextEditorCompositionListener::HandleEndComposition(nsIDOMEvent* aCompositionEvent) { - return mEditor->EndComposition(); + return mEditor->EndComposition(); } @@ -1515,14 +1518,14 @@ NS_NewEditorMouseListener(nsIDOMEventListener ** aInstancePtrResult, nsresult NS_NewEditorTextListener(nsIDOMEventListener** aInstancePtrResult, nsIEditor* aEditor) { - nsTextEditorTextListener* it = new nsTextEditorTextListener(); - if (nsnull==it) { - return NS_ERROR_OUT_OF_MEMORY; - } + nsTextEditorTextListener* it = new nsTextEditorTextListener(); + if (nsnull==it) { + return NS_ERROR_OUT_OF_MEMORY; + } - it->SetEditor(aEditor); + it->SetEditor(aEditor); - return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); + return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); } @@ -1544,11 +1547,11 @@ NS_NewEditorDragListener(nsIDOMEventListener ** aInstancePtrResult, nsresult NS_NewEditorCompositionListener(nsIDOMEventListener** aInstancePtrResult, nsIEditor* aEditor) { - nsTextEditorCompositionListener* it = new nsTextEditorCompositionListener(); - if (nsnull==it) { - return NS_ERROR_OUT_OF_MEMORY; - } - it->SetEditor(aEditor); + nsTextEditorCompositionListener* it = new nsTextEditorCompositionListener(); + if (nsnull==it) { + return NS_ERROR_OUT_OF_MEMORY; + } + it->SetEditor(aEditor); return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); } @@ -1561,7 +1564,7 @@ NS_NewEditorFocusListener(nsIDOMEventListener ** aInstancePtrResult, return NS_ERROR_OUT_OF_MEMORY; } it->SetEditor(aEditor); - return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); + return it->QueryInterface(nsIDOMEventListener::GetIID(), (void **) aInstancePtrResult); } @@ -1654,7 +1657,7 @@ nsTextEditorFocusListener::Focus(nsIDOMEvent* aEvent) ps->GetViewManager(getter_AddRefs(viewmgr)); if (viewmgr) { nsIView* view; - viewmgr->GetRootView(view); // views are not refCounted + viewmgr->GetRootView(view); // views are not refCounted if (view) { viewmgr->UpdateView(view,nsnull,NS_VMREFRESH_IMMEDIATE); } @@ -1698,7 +1701,7 @@ nsTextEditorFocusListener::Blur(nsIDOMEvent* aEvent) if (viewmgr) { nsIView* view; - viewmgr->GetRootView(view); // views are not refCounted + viewmgr->GetRootView(view); // views are not refCounted if (view) { viewmgr->UpdateView(view,nsnull,NS_VMREFRESH_IMMEDIATE); } diff --git a/mozilla/layout/base/nsPresShell.cpp b/mozilla/layout/base/nsPresShell.cpp index acc50d0422c..d637f2782bc 100644 --- a/mozilla/layout/base/nsPresShell.cpp +++ b/mozilla/layout/base/nsPresShell.cpp @@ -47,6 +47,7 @@ #include "nsIDOMElement.h" #include "nsHTMLAtoms.h" #include "nsCOMPtr.h" +#include "nsWeakReference.h" #include "nsICaret.h" #include "nsIDOMHTMLDocument.h" #include "nsIXMLDocument.h" @@ -110,7 +111,7 @@ static NS_DEFINE_IID(kIWebShellIID, NS_IWEB_SHELL_IID); class PresShell : public nsIPresShell, public nsIViewObserver, private nsIDocumentObserver, public nsIFocusTracker, - public nsIDOMSelectionListener + public nsIDOMSelectionListener, public nsSupportsWeakReference { public: PresShell(); @@ -432,6 +433,12 @@ PresShell::QueryInterface(const nsIID& aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } + if (aIID.Equals(nsCOMTypeInfo::GetIID())) { + nsISupportsWeakReference* tmp = this; + *aInstancePtr = (void*) tmp; + NS_ADDREF_THIS(); + return NS_OK; + } if (aIID.Equals(kISupportsIID)) { nsIPresShell* tmp = this; nsISupports* tmp2 = tmp; diff --git a/mozilla/layout/base/src/nsDocument.cpp b/mozilla/layout/base/src/nsDocument.cpp index aeea3835dbd..56c59d20062 100644 --- a/mozilla/layout/base/src/nsDocument.cpp +++ b/mozilla/layout/base/src/nsDocument.cpp @@ -798,6 +798,12 @@ nsresult nsDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } + if (aIID.Equals(nsCOMTypeInfo::GetIID())) { + nsISupportsWeakReference* tmp = this; + *aInstancePtr = (void*) tmp; + NS_ADDREF_THIS(); + return NS_OK; + } static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); if (aIID.Equals(kISupportsIID)) { nsIDocument* tmp = this; diff --git a/mozilla/layout/base/src/nsDocument.h b/mozilla/layout/base/src/nsDocument.h index 518b7214f39..ab549013c45 100644 --- a/mozilla/layout/base/src/nsDocument.h +++ b/mozilla/layout/base/src/nsDocument.h @@ -19,6 +19,7 @@ #define nsDocument_h___ #include "nsIDocument.h" +#include "nsWeakReference.h" #include "nsVoidArray.h" #include "nsIDOMDocument.h" #include "nsIDOMNSDocument.h" @@ -105,7 +106,8 @@ class nsDocument : public nsIDocument, public nsIDiskDocument, public nsIScriptObjectOwner, public nsIDOMEventCapturer, - public nsIJSScriptObject + public nsIJSScriptObject, + public nsSupportsWeakReference { public: NS_DECL_ISUPPORTS diff --git a/mozilla/layout/html/base/src/nsPresShell.cpp b/mozilla/layout/html/base/src/nsPresShell.cpp index acc50d0422c..d637f2782bc 100644 --- a/mozilla/layout/html/base/src/nsPresShell.cpp +++ b/mozilla/layout/html/base/src/nsPresShell.cpp @@ -47,6 +47,7 @@ #include "nsIDOMElement.h" #include "nsHTMLAtoms.h" #include "nsCOMPtr.h" +#include "nsWeakReference.h" #include "nsICaret.h" #include "nsIDOMHTMLDocument.h" #include "nsIXMLDocument.h" @@ -110,7 +111,7 @@ static NS_DEFINE_IID(kIWebShellIID, NS_IWEB_SHELL_IID); class PresShell : public nsIPresShell, public nsIViewObserver, private nsIDocumentObserver, public nsIFocusTracker, - public nsIDOMSelectionListener + public nsIDOMSelectionListener, public nsSupportsWeakReference { public: PresShell(); @@ -432,6 +433,12 @@ PresShell::QueryInterface(const nsIID& aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } + if (aIID.Equals(nsCOMTypeInfo::GetIID())) { + nsISupportsWeakReference* tmp = this; + *aInstancePtr = (void*) tmp; + NS_ADDREF_THIS(); + return NS_OK; + } if (aIID.Equals(kISupportsIID)) { nsIPresShell* tmp = this; nsISupports* tmp2 = tmp;