Update contrib.
2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "EditorPlainTextCommands.h"
20 #include "AssertFileAndLine.h"
22 #include <txtclipboard.h>
24 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
25 #include "txtfmlyr_internal.h"
27 using namespace UndoSystem;
29 /////////////////////////
31 // Internal Commands //
33 /////////////////////////
35 TUid CEditorCommand::FamilyUid() const { return TUid::Uid(KUndoDllUid); }
37 CEditorCommandInsertTextAndFormat*
38 CEditorCommand::CastToCEditorCommandInsertTextAndFormat() { return 0; }
39 CEditorCommandDeleteText*
40 CEditorCommand::CastToCEditorCommandDeleteText() { return 0; }
41 CEditorCommandInsertPlainText*
42 CEditorCommand::CastToCEditorCommandInsertPlainText() { return 0; }
43 CEditorCommandDeletePlainText*
44 CEditorCommand::CastToCEditorCommandDeletePlainText() { return 0; }
45 CEditorCommandDeleteCharFormat*
46 CEditorCommand::CastToCEditorCommandDeleteCharFormat() { return 0; }
47 CEditorCommandDeleteParFormat*
48 CEditorCommand::CastToCEditorCommandDeleteParFormat() { return 0; }
50 /////////////////////////////////
52 // Command coalesce function //
54 /////////////////////////////////
56 CCommand* UndoSystem::CoalesceL(CCommand* aLeft, CCommand* aRight)
63 CleanupStack::PushL(aRight);
64 CBatchCommand* batch = aLeft->Batch();
67 batch->PrepareToPushL(aRight);
69 CleanupStack::Pop(aRight);
72 ASSERT(aLeft->Single());
73 batch = CBatchCommand::NewL();
74 CleanupStack::PushL(batch);
75 batch->PrepareToPushL(aLeft);
77 batch->PrepareToPushL(aRight);
79 CleanupStack::Pop(batch);
80 CleanupStack::Pop(aRight);
85 * Command for deleting plain text.
88 * @since App-frameworks6.1
90 NONSHARABLE_CLASS(CEditorCommandDeletePlainText) : public CEditorCommand
92 TEditorDeletePlainTextImpl iImpl;
94 CEditorCommandDeletePlainText*
95 CastToCEditorCommandDeletePlainText() { return this; }
97 CEditorCommandDeletePlainText(MUnifiedEditor& aTarget,
98 TInt aPos, TInt aLength)
99 : iImpl(aTarget, aPos, aLength) {}
101 static CEditorCommandDeletePlainText* NewL(TInt aPos,
102 TInt aLength, MUnifiedEditor& aTarget)
104 return new(ELeave) CEditorCommandDeletePlainText(aTarget, aPos, aLength);
107 CCommand* CreateInverseL() const;
109 TInt ExecuteL() const
111 return iImpl.ExecuteL();
114 // This command can be coalesced with others of the same type
115 TBool CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const
117 return iImpl.CanAdd(aPos, aLength, aTarget);
120 void Add(TInt aLength)
127 * Command for inserting text in a specified style and format.
130 * @since App-frameworks6.1
132 NONSHARABLE_CLASS(CEditorCommandInsertPlainText) : public CEditorCommand
134 TEditorInsertPlainTextImpl iImpl;
136 CEditorCommandInsertPlainText*
137 CastToCEditorCommandInsertPlainText() { return this; }
138 CEditorCommandInsertPlainText(MUnifiedEditor& aTarget, TInt aPos, const TDesC& aText)
139 : iImpl(aTarget, aPos, aText) {}
141 static CEditorCommandInsertPlainText* NewL(TInt aPos, const TDesC& aText,
142 MUnifiedEditor& aTarget)
144 return new(ELeave) CEditorCommandInsertPlainText(aTarget, aPos, aText);
147 * Gets the length specified, but this might entail the use of more than
148 * one CCommand object.
150 static CCommand* NewBatchL(TInt aPos, TInt aLength, MUnifiedEditor& aTarget);
152 CCommand* CreateInverseL() const;
154 TInt ExecuteL() const
156 return iImpl.ExecuteL();
159 TBool CanAdd(TInt aPos, const TDesC& aText, MUnifiedEditor& aTarget) const
161 return iImpl.CanAdd(aPos, aText, aTarget);
164 void Add(TInt aPos, const TDesC& aText)
166 iImpl.Add(aPos, aText);
170 CCommand* CEditorCommandInsertPlainText::NewBatchL(TInt aPos,
171 TInt aLength, MUnifiedEditor& aTarget)
173 CCommand* command = 0;
174 TInt end = aPos + aLength;
178 aTarget.GetText(end - aLength, textSegment);
179 TInt segLength = textSegment.Length();
180 if (aLength < segLength)
182 if (KMaxCharsInSingleCommand < segLength)
183 segLength = KMaxCharsInSingleCommand;
184 CleanupStack::PushL(command);
185 // coverity[double_free]
186 command = CoalesceL(command, CEditorCommandInsertPlainText::NewL(aPos,
187 textSegment.Left(segLength), aTarget));
189 aLength -= segLength;
194 CCommand* CEditorCommandInsertPlainText::CreateInverseL() const
196 return CEditorCommandDeletePlainText::NewL(iImpl.Pos(), iImpl.Text().Length(),
201 CCommand* CEditorCommandDeletePlainText::CreateInverseL() const
203 return CEditorCommandInsertPlainText::NewBatchL(iImpl.Pos(), iImpl.Length(),
207 //////////////////////////////////////////
209 // CEditorCommandInsertPlainTextProto //
211 //////////////////////////////////////////
213 void CEditorCommandInsertPlainTextProto::Set(TInt aPos, const TDesC& aText)
219 UndoSystem::CCommand* CEditorCommandInsertPlainTextProto::CreateInverseL() const
221 return CEditorCommandDeletePlainText::NewL(iPos, iText->Length(), iTarget);
224 TInt CEditorCommandInsertPlainTextProto::ExecuteL() const
226 iTarget.InsertTextL(iPos, *iText);
230 TBool CEditorCommandInsertPlainTextProto::PrepareToAddInverseToLastL(
231 CSingleCommand& aLastCommand) const
233 if (aLastCommand.FamilyUid() != TUid::Uid(KUndoDllUid))
235 CEditorCommandDeletePlainText* last =
236 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeletePlainText();
239 return last->CanAdd(iPos, iText->Length(), iTarget);
242 void CEditorCommandInsertPlainTextProto::AddInverseToLast(CSingleCommand& aLastCommand) const
244 ASSERT(aLastCommand.FamilyUid() == TUid::Uid(KUndoDllUid));
245 CEditorCommandDeletePlainText* last =
246 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeletePlainText();
248 last->Add(iText->Length());
251 //////////////////////////////////////////
253 // CEditorCommandDeletePlainTextProto //
255 //////////////////////////////////////////
257 void CEditorCommandDeletePlainTextProto::Set(TInt aPos, TInt aLength)
263 CCommand* CEditorCommandDeletePlainTextProto::CreateInverseL() const
265 return CEditorCommandInsertPlainText::NewBatchL(iPos, iLength, iTarget);
268 TInt CEditorCommandDeletePlainTextProto::ExecuteL() const
270 iTarget.DeleteTextL(iPos, iLength);
274 TBool CEditorCommandDeletePlainTextProto::PrepareToAddInverseToLastL(
275 CSingleCommand& aLastCommand) const
277 if (iDeletedText.MaxLength() < iLength)
279 if (aLastCommand.FamilyUid() != TUid::Uid(KUndoDllUid))
281 CEditorCommandInsertPlainText* last =
282 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandInsertPlainText();
285 TBool result = EFalse;
287 iTarget.GetText(iPos, textSegment);
288 if (iLength <= textSegment.Length())
290 result = last->CanAdd(iPos, textSegment.Left(iLength), iTarget);
292 iDeletedText = textSegment.Left(iLength);
297 void CEditorCommandDeletePlainTextProto::AddInverseToLast(CSingleCommand& aLastCommand) const
299 ASSERT(aLastCommand.FamilyUid() == TUid::Uid(KUndoDllUid));
300 CEditorCommandInsertPlainText* last =
301 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandInsertPlainText();
303 last->Add(iPos, iDeletedText);
306 //////////////////////////////////
308 // TEditorInsertPlainTextImpl //
310 //////////////////////////////////
312 TInt TEditorInsertPlainTextImpl::ExecuteL(const TDesC* aStyle,
313 const TTmCharFormatLayer* aChar,
314 const RTmParFormatLayer* aPar) const
316 iTarget.InsertTextL(iPos, iText, aStyle, aChar, aPar);
320 TInt TEditorInsertPlainTextImpl::ExecuteL() const
322 return ExecuteL(0, 0, 0);
325 TInt TEditorInsertPlainTextImpl::CanAdd(TInt aPos, const TDesC& aText, MUnifiedEditor& aTarget) const
327 if (&aTarget != &iTarget)
329 TInt length = aText.Length();
330 if (KMaxCharsInSingleCommand <= iText.Length() + length)
332 return aPos <= iPos && iPos <= aPos + length;
335 void TEditorInsertPlainTextImpl::Add(TInt aPos, const TDesC& aText)
337 TPtrC left = aText.Left(iPos - aPos);
338 TPtrC right = aText.Mid(iPos - aPos);
339 iText.Insert(0, left);
344 //////////////////////////////////
346 // TEditorDeletePlainTextImpl //
348 //////////////////////////////////
350 TInt TEditorDeletePlainTextImpl::ExecuteL() const
352 iTarget.DeleteTextL(iPos, iLength);
356 TBool TEditorDeletePlainTextImpl::CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const
358 return &aTarget == &iTarget && iPos <= aPos && aPos <= iLength + iPos
359 && aLength + iLength <= KMaxCharsInSingleCommand;
362 void TEditorDeletePlainTextImpl::Add(TInt aLength)
367 /////////////////////////////
369 // TEditorPasteProtoImpl //
371 /////////////////////////////
373 void TEditorPasteProtoImpl::Set(const CStreamStore& aStore,
374 const CStreamDictionary& aDict,
378 iStreamDictionary = &aDict;
382 void TEditorPasteProtoImpl::OpenPlainTextStreamLC(RStoreReadStream& aStream) const
384 TStreamId plainTextStream = iStreamDictionary->At(KClipboardUidTypePlainText);
385 if (plainTextStream == KNullStreamId)
386 User::Leave(KErrNotSupported); // don't know how to undo this
387 aStream.OpenLC(*iStore, plainTextStream);
390 TInt TEditorPasteProtoImpl::LengthL() const
392 RStoreReadStream stream;
393 OpenPlainTextStreamLC(stream);
394 TInt length = stream.ReadInt32L();
395 CleanupStack::PopAndDestroy();
399 TInt TEditorPasteProtoImpl::ExecuteL() const
401 MUnifiedEditor::MClipboardSupport* ci = iTarget.ClipboardSupport();
403 ci->PasteFromStoreL(*iStore, *iStreamDictionary, iPos);
407 /////////////////////////////////////////
409 // CEditorCommandPastePlainTextProto //
411 /////////////////////////////////////////
413 void CEditorCommandPastePlainTextProto::Set(const CStreamStore& aStore,
414 const CStreamDictionary& aStreamDictionary,
417 iImpl.Set(aStore, aStreamDictionary, aPos);
420 UndoSystem::CCommand* CEditorCommandPastePlainTextProto::CreateInverseL() const
422 return CEditorCommandDeletePlainText::NewL(iImpl.Pos(), iImpl.LengthL(), iImpl.Target());
425 TInt CEditorCommandPastePlainTextProto::ExecuteL() const
427 return iImpl.ExecuteL();