1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "InsertTextTxn.h"
39 : #include "nsIDOMCharacterData.h"
40 : #include "nsISelection.h"
41 : #include "EditAggregateTxn.h"
42 :
43 : #ifdef NS_DEBUG
44 : static bool gNoisy = false;
45 : #endif
46 :
47 0 : InsertTextTxn::InsertTextTxn()
48 0 : : EditTxn()
49 : {
50 0 : }
51 :
52 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(InsertTextTxn)
53 :
54 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(InsertTextTxn, EditTxn)
55 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mElement)
56 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
57 :
58 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(InsertTextTxn, EditTxn)
59 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mElement)
60 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
61 :
62 0 : NS_IMPL_ADDREF_INHERITED(InsertTextTxn, EditTxn)
63 0 : NS_IMPL_RELEASE_INHERITED(InsertTextTxn, EditTxn)
64 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertTextTxn)
65 0 : if (aIID.Equals(InsertTextTxn::GetCID())) {
66 0 : *aInstancePtr = (void*)(InsertTextTxn*)this;
67 0 : NS_ADDREF_THIS();
68 0 : return NS_OK;
69 : } else
70 0 : NS_INTERFACE_MAP_END_INHERITING(EditTxn)
71 :
72 0 : NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
73 : PRUint32 aOffset,
74 : const nsAString &aStringToInsert,
75 : nsIEditor *aEditor)
76 : {
77 : #if 0
78 : nsAutoString text;
79 : aElement->GetData(text);
80 : printf("InsertTextTxn: Offset to insert at = %d. Text of the node to insert into:\n", aOffset);
81 : wprintf(text.get());
82 : printf("\n");
83 : #endif
84 :
85 0 : NS_ASSERTION(aElement && aEditor, "bad args");
86 0 : NS_ENSURE_TRUE(aElement && aEditor, NS_ERROR_NULL_POINTER);
87 :
88 0 : mElement = do_QueryInterface(aElement);
89 0 : mOffset = aOffset;
90 0 : mStringToInsert = aStringToInsert;
91 0 : mEditor = aEditor;
92 0 : return NS_OK;
93 : }
94 :
95 0 : NS_IMETHODIMP InsertTextTxn::DoTransaction(void)
96 : {
97 : #ifdef NS_DEBUG
98 0 : if (gNoisy)
99 : {
100 : printf("Do Insert Text element = %p\n",
101 0 : static_cast<void*>(mElement.get()));
102 : }
103 : #endif
104 :
105 0 : NS_ASSERTION(mElement && mEditor, "bad state");
106 0 : if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
107 :
108 0 : nsresult result = mElement->InsertData(mOffset, mStringToInsert);
109 0 : NS_ENSURE_SUCCESS(result, result);
110 :
111 : // only set selection to insertion point if editor gives permission
112 : bool bAdjustSelection;
113 0 : mEditor->ShouldTxnSetSelection(&bAdjustSelection);
114 0 : if (bAdjustSelection)
115 : {
116 0 : nsCOMPtr<nsISelection> selection;
117 0 : result = mEditor->GetSelection(getter_AddRefs(selection));
118 0 : NS_ENSURE_SUCCESS(result, result);
119 0 : NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
120 0 : result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
121 0 : NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
122 : }
123 : else
124 : {
125 : // do nothing - dom range gravity will adjust selection
126 : }
127 :
128 0 : return result;
129 : }
130 :
131 0 : NS_IMETHODIMP InsertTextTxn::UndoTransaction(void)
132 : {
133 : #ifdef NS_DEBUG
134 0 : if (gNoisy)
135 : {
136 : printf("Undo Insert Text element = %p\n",
137 0 : static_cast<void*>(mElement.get()));
138 : }
139 : #endif
140 :
141 0 : NS_ASSERTION(mElement && mEditor, "bad state");
142 0 : if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
143 :
144 0 : PRUint32 length = mStringToInsert.Length();
145 0 : return mElement->DeleteData(mOffset, length);
146 : }
147 :
148 0 : NS_IMETHODIMP InsertTextTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge)
149 : {
150 : // set out param default value
151 0 : if (aDidMerge)
152 0 : *aDidMerge = false;
153 0 : nsresult result = NS_OK;
154 0 : if (aDidMerge && aTransaction)
155 : {
156 : // if aTransaction is a InsertTextTxn, and if the selection hasn't changed,
157 : // then absorb it
158 0 : InsertTextTxn *otherInsTxn = nsnull;
159 0 : aTransaction->QueryInterface(InsertTextTxn::GetCID(), (void **)&otherInsTxn);
160 0 : if (otherInsTxn)
161 : {
162 0 : if (IsSequentialInsert(otherInsTxn))
163 : {
164 0 : nsAutoString otherData;
165 0 : otherInsTxn->GetData(otherData);
166 0 : mStringToInsert += otherData;
167 0 : *aDidMerge = true;
168 : #ifdef NS_DEBUG
169 0 : if (gNoisy)
170 : {
171 : printf("InsertTextTxn assimilated %p\n",
172 0 : static_cast<void*>(aTransaction));
173 : }
174 : #endif
175 : }
176 0 : NS_RELEASE(otherInsTxn);
177 : }
178 : }
179 0 : return result;
180 : }
181 :
182 0 : NS_IMETHODIMP InsertTextTxn::GetTxnDescription(nsAString& aString)
183 : {
184 0 : aString.AssignLiteral("InsertTextTxn: ");
185 0 : aString += mStringToInsert;
186 0 : return NS_OK;
187 : }
188 :
189 : /* ============ protected methods ================== */
190 :
191 0 : NS_IMETHODIMP InsertTextTxn::GetData(nsString& aResult)
192 : {
193 0 : aResult = mStringToInsert;
194 0 : return NS_OK;
195 : }
196 :
197 0 : bool InsertTextTxn::IsSequentialInsert(InsertTextTxn *aOtherTxn)
198 : {
199 0 : NS_ASSERTION(aOtherTxn, "null param");
200 0 : if (aOtherTxn && aOtherTxn->mElement == mElement)
201 : {
202 : // here, we need to compare offsets.
203 0 : PRInt32 length = mStringToInsert.Length();
204 0 : if (aOtherTxn->mOffset == (mOffset + length))
205 0 : return true;
206 : }
207 0 : return false;
208 4392 : }
|