making nsPresShell and nsDocument support weak references; using weak references to docs and pres shells within the editor
git-svn-id: svn://10.0.0.236/trunk@44483 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
@@ -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<nsIDOMSelection> 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<nsIPresShell> 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<nsIDOMSelection> 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<nsIPresShell> 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<nsIDOMSelection> 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;i<listlen;i++) {
|
||||
(void)mRangeList->Item(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;i<listlen;i++) {
|
||||
(void)mRangeList->Item(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;i<textRangeListLength;i++) {
|
||||
result = mRangeList->Item(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;i<textRangeListLength;i++) {
|
||||
result = mRangeList->Item(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<nsIDOMSelection> 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<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
||||
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
||||
nsCOMPtr<nsIDOMSelection> 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<nsIDOMSelection> selection;
|
||||
nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
|
||||
if (NS_SUCCEEDED(result) && selection){
|
||||
result = selection->Collapse(mElement,mOffset+mStringToInsert.Length());
|
||||
}
|
||||
nsCOMPtr<nsIDOMSelection> selection;
|
||||
nsCOMPtr<nsIPresShell> 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user