1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998-1999
20
* the Initial Developer. All Rights Reserved.
23
* Pierre Phaneuf <pp@ludusdesign.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#include "IMETextTxn.h"
41
#include "nsIDOMCharacterData.h"
42
#include "nsIDOMRange.h"
43
#include "nsIPrivateTextRange.h"
44
#include "nsISelection.h"
45
#include "nsISelectionPrivate.h"
46
#include "nsISelectionController.h"
47
#include "nsComponentManagerUtils.h"
49
// #define DEBUG_IMETXN
51
nsIAtom *IMETextTxn::gIMETextTxnName = nsnull;
53
nsresult IMETextTxn::ClassInit()
56
gIMETextTxnName = NS_NewAtom("NS_IMETextTxn");
60
nsresult IMETextTxn::ClassShutdown()
62
NS_IF_RELEASE(gIMETextTxnName);
66
IMETextTxn::IMETextTxn()
71
IMETextTxn::~IMETextTxn()
73
mRangeList = do_QueryInterface(nsnull);
76
NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement,
78
PRUint32 aReplaceLength,
79
nsIPrivateTextRangeList *aTextRangeList,
80
const nsAString &aStringToInsert,
81
nsWeakPtr aSelConWeak)
83
NS_ASSERTION(aElement, "illegal value- null ptr- aElement");
84
NS_ASSERTION(aTextRangeList, "illegal value- null ptr - aTextRangeList");
85
if (!aElement || !aTextRangeList)
86
return NS_ERROR_NULL_POINTER;
87
mElement = do_QueryInterface(aElement);
89
mReplaceLength = aReplaceLength;
90
mStringToInsert = aStringToInsert;
91
mSelConWeak = aSelConWeak;
92
mRangeList = do_QueryInterface(aTextRangeList);
97
NS_IMETHODIMP IMETextTxn::DoTransaction(void)
101
printf("Do IME Text element = %p replace = %d len = %d\n", mElement.get(), mReplaceLength, mStringToInsert.Length());
104
nsCOMPtr<nsISelectionController> selCon = do_QueryReferent(mSelConWeak);
105
if (!selCon) return NS_ERROR_NOT_INITIALIZED;
107
// advance caret: This requires the presentation shell to get the selection.
109
if (mReplaceLength == 0) {
110
result = mElement->InsertData(mOffset, mStringToInsert);
112
result = mElement->ReplaceData(mOffset, mReplaceLength, mStringToInsert);
114
if (NS_SUCCEEDED(result)) {
115
result = CollapseTextSelection();
121
NS_IMETHODIMP IMETextTxn::UndoTransaction(void)
124
printf("Undo IME Text element = %p\n", mElement.get());
127
nsCOMPtr<nsISelectionController> selCon = do_QueryReferent(mSelConWeak);
128
if (!selCon) return NS_ERROR_NOT_INITIALIZED;
130
nsresult result = mElement->DeleteData(mOffset, mStringToInsert.Length());
131
if (NS_SUCCEEDED(result))
132
{ // set the selection to the insertion point where the string was removed
133
nsCOMPtr<nsISelection> selection;
134
result = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
135
if (NS_SUCCEEDED(result) && selection) {
136
result = selection->Collapse(mElement, mOffset);
137
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert.");
143
NS_IMETHODIMP IMETextTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
145
NS_ASSERTION(aDidMerge, "illegal vaule- null ptr- aDidMerge");
146
NS_ASSERTION(aTransaction, "illegal vaule- null ptr- aTransaction");
147
if (!aDidMerge || !aTransaction)
148
return NS_ERROR_NULL_POINTER;
151
printf("Merge IME Text element = %p\n", mElement.get());
155
// check to make sure we aren't fixed, if we are then nothing get's absorbed
158
*aDidMerge = PR_FALSE;
163
// if aTransaction is another IMETextTxn then absorb it
165
IMETextTxn* otherTxn = nsnull;
166
nsresult result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn);
167
if (otherTxn && NS_SUCCEEDED(result))
170
// we absorb the next IME transaction by adopting its insert string as our own
172
nsIPrivateTextRangeList* newTextRangeList;
173
otherTxn->GetData(mStringToInsert,&newTextRangeList);
174
mRangeList = do_QueryInterface(newTextRangeList);
175
*aDidMerge = PR_TRUE;
177
printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction);
179
NS_RELEASE(otherTxn);
183
*aDidMerge = PR_FALSE;
187
NS_IMETHODIMP IMETextTxn::MarkFixed(void)
193
NS_IMETHODIMP IMETextTxn::GetTxnDescription(nsAString& aString)
195
aString.Assign(NS_LITERAL_STRING("IMETextTxn: "));
196
aString += mStringToInsert;
200
/* ============= nsISupports implementation ====================== */
203
IMETextTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr)
205
if (nsnull == aInstancePtr) {
206
return NS_ERROR_NULL_POINTER;
208
if (aIID.Equals(IMETextTxn::GetCID())) {
209
*aInstancePtr = (void*)(IMETextTxn*)this;
213
return (EditTxn::QueryInterface(aIID, aInstancePtr));
216
/* ============ protected methods ================== */
217
static SelectionType TextRangeToSelection(int aTextRangeType)
219
switch(aTextRangeType)
221
case nsIPrivateTextRange::TEXTRANGE_RAWINPUT:
222
return nsISelectionController::SELECTION_IME_RAWINPUT;
223
case nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT:
224
return nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT;
225
case nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT:
226
return nsISelectionController::SELECTION_IME_CONVERTEDTEXT;
227
case nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT:
228
return nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT;
229
case nsIPrivateTextRange::TEXTRANGE_CARETPOSITION:
231
return nsISelectionController::SELECTION_NORMAL;
235
NS_IMETHODIMP IMETextTxn::GetData(nsString& aResult,nsIPrivateTextRangeList** aTextRangeList)
237
NS_ASSERTION(aTextRangeList, "illegal value- null ptr- aTextRangeList");
239
return NS_ERROR_NULL_POINTER;
240
aResult = mStringToInsert;
241
*aTextRangeList = mRangeList;
245
static SelectionType sel[4]=
247
nsISelectionController::SELECTION_IME_RAWINPUT,
248
nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT,
249
nsISelectionController::SELECTION_IME_CONVERTEDTEXT,
250
nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT
253
NS_IMETHODIMP IMETextTxn::CollapseTextSelection(void)
259
PRUint16 listlen,start,stop,type;
260
result = mRangeList->GetLength(&listlen);
261
printf("nsIPrivateTextRangeList[%p]\n",mRangeList);
262
nsIPrivateTextRange* rangePtr;
263
for (i=0;i<listlen;i++) {
264
(void)mRangeList->Item(i,&rangePtr);
265
rangePtr->GetRangeStart(&start);
266
rangePtr->GetRangeEnd(&stop);
267
rangePtr->GetRangeType(&type);
268
printf("range[%d] start=%d end=%d type=",i,start,stop,type);
269
if (type==nsIPrivateTextRange::TEXTRANGE_RAWINPUT)
270
printf("TEXTRANGE_RAWINPUT\n");
271
else if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDRAWTEXT)
272
printf("TEXTRANGE_SELECTEDRAWTEXT\n");
273
else if (type==nsIPrivateTextRange::TEXTRANGE_CONVERTEDTEXT)
274
printf("TEXTRANGE_CONVERTEDTEXT\n");
275
else if (type==nsIPrivateTextRange::TEXTRANGE_SELECTEDCONVERTEDTEXT)
276
printf("TEXTRANGE_SELECTEDCONVERTEDTEXT\n");
277
else if (type==nsIPrivateTextRange::TEXTRANGE_CARETPOSITION)
278
printf("TEXTRANGE_CARETPOSITION\n");
279
else printf("unknown constant\n");
284
// run through the text range list, if any
286
nsCOMPtr<nsISelectionController> selCon = do_QueryReferent(mSelConWeak);
287
if (!selCon) return NS_ERROR_NOT_INITIALIZED;
289
PRUint16 textRangeListLength,selectionStart,selectionEnd,
292
result = mRangeList->GetLength(&textRangeListLength);
293
if(NS_FAILED(result))
295
nsCOMPtr<nsISelection> selection;
296
result = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
297
if(NS_SUCCEEDED(result))
299
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
300
result = selPriv->StartBatchChanges();
301
if (NS_SUCCEEDED(result))
303
nsCOMPtr<nsISelection> imeSel;
304
for(PRInt8 selIdx = 0; selIdx < 4;selIdx++)
306
result = selCon->GetSelection(sel[selIdx], getter_AddRefs(imeSel));
307
if (NS_SUCCEEDED(result))
309
result = imeSel->RemoveAllRanges();
310
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot ClearSelection");
311
// we just ignore the result and clean up the next one here
315
nsIPrivateTextRange* textRange;
316
PRBool setCaret=PR_FALSE;
317
for(i=0;i<textRangeListLength;i++)
319
result = mRangeList->Item(i,&textRange);
320
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get item");
321
if(NS_FAILED(result))
324
result = textRange->GetRangeType(&textRangeType);
325
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get range type");
326
if(NS_FAILED(result))
329
result = textRange->GetRangeStart(&selectionStart);
330
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get range start");
331
if(NS_FAILED(result))
333
result = textRange->GetRangeEnd(&selectionEnd);
334
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get range end");
335
if(NS_FAILED(result))
338
if(nsIPrivateTextRange::TEXTRANGE_CARETPOSITION == textRangeType)
341
result = selection->Collapse(mElement,
342
mOffset+selectionStart);
343
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot Collapse");
344
if(NS_SUCCEEDED(result))
347
// NS_ASSERTION(selectionStart != selectionEnd, "end == start");
348
if(selectionStart == selectionEnd)
351
result= selCon->GetSelection(TextRangeToSelection(textRangeType),
352
getter_AddRefs(imeSel));
353
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot get selction");
354
if(NS_FAILED(result))
357
nsCOMPtr<nsIDOMRange> newRange = do_CreateInstance(
358
"@mozilla.org/content/range;1", &result);
359
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot create new nsIDOMRange");
360
if(NS_FAILED(result))
363
newRange->SetStart(mElement,mOffset+selectionStart);
364
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot SetStart");
365
if(NS_FAILED(result))
368
newRange->SetEnd(mElement,mOffset+selectionEnd);
369
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot SetEnd");
370
if(NS_FAILED(result))
373
imeSel->AddRange(newRange);
374
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot AddRange");
375
if(NS_FAILED(result))
379
} // for textRangeListLength
382
result = selection->Collapse(mElement,mOffset+mStringToInsert.Length());
383
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot Collapse");
385
result = selPriv->EndBatchChanges();
386
NS_ASSERTION(NS_SUCCEEDED(result), "Cannot EndBatchChanges");
387
} // if StartBatchChanges