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 "EditorCommands.h"
20 #include "AssertFileAndLine.h"
24 using namespace UndoSystem;
26 inline TBool IsSurrogate(TText a) { return 0xD800 == (a & 0xF800); }
27 inline TBool IsHighSurrogate(TText a) { return 0xD800 == (a & 0xFC00); }
28 inline TBool IsLowSurrogate(TText a) { return 0xDC00 == (a & 0xFC00); }
29 inline TChar JoinSurrogates(TText aHigh, TText aLow)
31 return ((aHigh - 0xd7f7) << 10) + aLow;
34 // These roughly mirror the prototype commands, but rather than
35 // referencing externally-owned (and therefore temporary) objects,
36 // they own objects. Typically these are unique instances.
39 * Command for setting the base character and paragraph formats.
42 * @since App-frameworks6.1
44 NONSHARABLE_CLASS(CEditorCommandSetBaseFormat) : public CEditorCommand
46 MUnifiedEditor& iTarget;
47 RUniqueInstance<TTmCharFormat> iChar;
48 RUniqueInstance<RTmParFormat> iPar;
50 const TRepositories& iRepositories;
52 CEditorCommandSetBaseFormat(const TRepositories& aReps, MUnifiedEditor& aTarget);
54 ~CEditorCommandSetBaseFormat();
55 static CEditorCommandSetBaseFormat* NewL(const TRepositories& aReps,
56 const TTmCharFormat& aCharFormat, const RTmParFormat& aParFormat,
57 MUnifiedEditor& aTarget);
58 static CEditorCommandSetBaseFormat* NewL(const TRepositories& aReps,
59 MUnifiedEditor& aTarget);
61 UndoSystem::CCommand* CreateInverseL() const;
62 TInt ExecuteL() const;
66 * Command for renaming a style.
69 * @since App-frameworks6.1
71 NONSHARABLE_CLASS(CEditorCommandRenameStyle) : public CEditorCommand
73 MUnifiedEditor& iTarget;
74 RUniqueInstance<TDes> iOldName;
75 RUniqueInstance<TDes> iNewName;
77 const TRepositories& iRepositories;
79 CEditorCommandRenameStyle(const TRepositories& aReps, MUnifiedEditor& aTarget)
80 : iTarget(aTarget), iOldName(*aReps.iDes), iNewName(*aReps.iDes),
81 iRepositories(aReps) {}
83 ~CEditorCommandRenameStyle();
84 static CEditorCommandRenameStyle* NewL(const TRepositories& aReps,
85 const TDesC& aOldName, const TDesC& aNewName, MUnifiedEditor& aTarget);
87 CCommand* CreateInverseL() const;
88 TInt ExecuteL() const;
92 * Command for inserting a picture into the text.
95 * @since App-frameworks6.1
97 NONSHARABLE_CLASS(CEditorCommandInsertPicture) : public CEditorCommand
99 MUnifiedEditor& iTarget;
101 mutable TPictureHeader iPicture;
102 MPictureOwner* iPictureOwner;
103 const TRepositories& iRepositories;
105 CEditorCommandInsertPicture(const TRepositories& aReps, MUnifiedEditor& aTarget)
106 : iTarget(aTarget), iRepositories(aReps) {}
108 ~CEditorCommandInsertPicture();
109 static CEditorCommandInsertPicture* NewL(const TRepositories& aReps,
110 TInt aPos, MPictureOwner& aPictureOwner, MUnifiedEditor& aTarget);
111 void TakePictureOwnership(TPictureHeader&);
112 void ForgetOwner(MPictureOwner*);
114 CCommand* CreateInverseL() const;
115 TInt ExecuteL() const;
119 * Command for creating a new style.
122 * @since App-frameworks6.1
124 NONSHARABLE_CLASS(CEditorCommandCreateStyle) : public CEditorCommand
126 MUnifiedEditor& iTarget;
127 RUniqueInstance<TDes> iName;
128 RUniqueInstance<TDes> iNext;
129 RUniqueInstance<TTmCharFormat> iChar;
130 TTmCharFormatMask iCharMask;
131 RUniqueInstance<RTmParFormat> iPar;
132 TTmParFormatMask iParMask;
135 const TRepositories& iRepositories;
137 CEditorCommandCreateStyle(const TRepositories& aReps, MUnifiedEditor& aTarget)
138 : iTarget(aTarget), iName(*aReps.iDes), iNext(*aReps.iDes),
140 iPar(*aReps.iPar), iRepositories(aReps) {}
142 ~CEditorCommandCreateStyle();
143 static CEditorCommandCreateStyle* NewL(const TRepositories& aReps,
144 const TDesC& aName, const TDesC& aNext,
145 TTmCharFormat& aChar, TTmCharFormatMask aCharMask,
146 RTmParFormat& aPar, TTmParFormatMask aParMask,
147 TInt aLevel, MUnifiedEditor& aTarget);
148 static CEditorCommandCreateStyle* NewL(const TRepositories& aReps,
149 const TDesC& aName, MUnifiedEditor& aTarget);
150 static CCommand* NewBatchL(const TRepositories& aReps,
151 const TDesC& aName, MUnifiedEditor& aTarget);
153 CCommand* CreateInverseL() const;
154 TInt ExecuteL() const;
158 * Command for deleting a style, removing it from anywhere that it is applied.
161 * @since App-frameworks6.1
163 NONSHARABLE_CLASS(CEditorCommandDeleteStyle) : public CEditorCommand
165 MUnifiedEditor& iTarget;
166 RUniqueInstance<TDes> iName;
167 const TRepositories& iRepositories;
169 CEditorCommandDeleteStyle(const TRepositories& aReps, MUnifiedEditor& aTarget)
170 : iTarget(aTarget), iName(*aReps.iDes), iRepositories(aReps) {}
172 ~CEditorCommandDeleteStyle();
173 static CEditorCommandDeleteStyle* NewL(const TRepositories& aReps,
174 const TDesC& aName, MUnifiedEditor& aTarget);
176 CCommand* CreateInverseL() const;
177 TInt ExecuteL() const;
181 * Command for altering the attributes of a style.
184 * @since App-frameworks6.1
186 NONSHARABLE_CLASS(CEditorCommandAlterStyle) : public CEditorCommand
188 MUnifiedEditor& iTarget;
189 RUniqueInstance<TDes> iName;
190 RUniqueInstance<TDes> iNext;
191 RUniqueInstance<TTmCharFormat> iChar;
192 TTmCharFormatMask iCharMask;
193 RUniqueInstance<RTmParFormat> iPar;
194 TTmParFormatMask iParMask;
197 const TRepositories& iRepositories;
199 CEditorCommandAlterStyle(const TRepositories& aReps, MUnifiedEditor& aTarget)
200 : iTarget(aTarget), iName(*aReps.iDes), iNext(*aReps.iDes),
201 iChar(*aReps.iChar), iPar(*aReps.iPar), iRepositories(aReps) {}
203 ~CEditorCommandAlterStyle();
204 static CEditorCommandAlterStyle* NewL(const TRepositories& aReps,
205 const TDesC& aName, const TDesC& aNext,
206 TTmCharFormat& aChar, TTmCharFormatMask& aCharMask,
207 RTmParFormat& aPar, TTmParFormatMask& aParMask,
208 TInt aLevel, MUnifiedEditor& aTarget);
209 static CEditorCommandAlterStyle* NewL(const TRepositories& aReps,
210 const TDesC& aName, MUnifiedEditor& aTarget);
212 CCommand* CreateInverseL() const;
213 TInt ExecuteL() const;
217 * Command for applying an existing style to text.
220 * @since App-frameworks6.1
222 NONSHARABLE_CLASS(CEditorCommandSetStyle) : public CEditorCommand
224 MUnifiedEditor& iTarget;
225 RUniqueInstance<TDes> iName;
229 const TRepositories& iRepositories;
231 CEditorCommandSetStyle(const TRepositories& aReps, MUnifiedEditor& aTarget)
232 : iTarget(aTarget), iName(*aReps.iDes), iRepositories(aReps) {}
234 ~CEditorCommandSetStyle();
235 static CEditorCommandSetStyle* NewL(const TRepositories& aReps,
236 const TDesC& aName, TInt aPos, TInt aRunLength,
237 MUnifiedEditor& aTarget);
238 static CCommand* NewL(const TRepositories& aReps,
239 TInt aPos, TInt aRunLength,
240 MUnifiedEditor& aTarget);
242 CCommand* CreateInverseL() const;
243 TInt ExecuteL() const;
247 * Command for deleting text without pictures in it.
250 * @since App-frameworks6.1
252 NONSHARABLE_CLASS(CEditorCommandDeleteText) : public CEditorCommand
254 TEditorDeletePlainTextImpl iImpl;
255 const TRepositories& iRepositories; // only used in creating the inverse
257 CEditorCommandDeleteText*
258 CastToCEditorCommandDeleteText() { return this; }
260 CEditorCommandDeleteText(const TRepositories& aReps, MUnifiedEditor& aTarget,
261 TInt aPos, TInt aLength)
262 : iImpl(aTarget, aPos, aLength), iRepositories(aReps) {}
264 ~CEditorCommandDeleteText();
265 static CEditorCommandDeleteText* NewL(const TRepositories& aReps,
266 TInt aPos, TInt aLength, MUnifiedEditor& aTarget);
268 CCommand* CreateInverseL() const;
269 TInt ExecuteL() const;
271 // This command can be coalesced with others of the same type
272 TBool CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const;
273 void Add(TInt aLength);
277 * Command for inserting text in a specified style and format.
280 * @since App-frameworks6.1
282 NONSHARABLE_CLASS(CEditorCommandInsertTextAndFormat) : public CEditorCommand
284 TEditorInsertPlainTextImpl iImpl;
286 RUniqueInstance<TTmCharFormat> iChar;
287 TTmCharFormatMask iCharMask;
288 RUniqueInstance<RTmParFormat> iPar;
289 TTmParFormatMask iParMask;
290 RUniqueInstance<TDes> iStyle;
292 const TRepositories& iRepositories;
294 CEditorCommandInsertTextAndFormat*
295 CastToCEditorCommandInsertTextAndFormat() { return this; }
296 CEditorCommandInsertTextAndFormat(const TRepositories& aReps,
297 MUnifiedEditor& aTarget, TInt aPos, TDesC& aText)
298 : iImpl(aTarget, aPos, aText), iChar(*aReps.iChar), iPar(*aReps.iPar),
299 iStyle(*aReps.iDes), iRepositories(aReps) {}
301 struct RTextAndFormatParameters
306 TTmCharFormatLayer iChar;
307 RTmParFormatLayer iPar;
310 void SetL(TInt aPos, TInt aMaxLength, MUnifiedEditor& aTarget);
312 ~CEditorCommandInsertTextAndFormat();
313 static CEditorCommandInsertTextAndFormat* NewL(const TRepositories& aReps,
314 RTextAndFormatParameters& aParams, MUnifiedEditor& aTarget);
317 * Gets as much of a run as possible.
319 static CEditorCommandInsertTextAndFormat* NewL(const TRepositories& aReps,
320 TInt aPos, TInt& aRemaining, TInt aOriginalPos, MUnifiedEditor& aTarget);
323 * Gets as many runs as necessary and returns a batch if necessary.
325 static CCommand* NewBatchL(const TRepositories& aReps,
326 TInt aPos, TInt aLength, MUnifiedEditor& aTarget);
328 CCommand* CreateInverseL() const;
329 TInt ExecuteL() const;
331 // This command can be coalesced with others of the same type
332 TBool CanAdd(const RTextAndFormatParameters&, MUnifiedEditor& aTarget) const;
333 void Add(TInt aPos, const TDesC& aText);
337 * Command for deleting the specific character format over a run of text.
340 * @since App-frameworks6.1
342 NONSHARABLE_CLASS(CEditorCommandDeleteCharFormat) : public CEditorCommand
344 MUnifiedEditor& iTarget;
348 const TRepositories& iRepositories;
350 CEditorCommandDeleteCharFormat*
351 CastToCEditorCommandDeleteCharFormat() { return this; }
353 CEditorCommandDeleteCharFormat(const TRepositories& aReps, MUnifiedEditor& aTarget)
354 : iTarget(aTarget), iRepositories(aReps) {}
356 ~CEditorCommandDeleteCharFormat();
357 static CEditorCommandDeleteCharFormat* NewL(const TRepositories& aReps,
358 TInt aPos, TInt aLength,
359 MUnifiedEditor& aTarget);
361 CCommand* CreateInverseL() const;
362 TInt ExecuteL() const;
364 // This command can be coalesced with others of the same type
365 TBool CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const;
366 void Add(TInt aPos, TInt aLength);
370 * Command for deleting the specific paragraph format over a run of text.
373 * @since App-frameworks6.1
375 NONSHARABLE_CLASS(CEditorCommandDeleteParFormat) : public CEditorCommand
377 MUnifiedEditor& iTarget;
381 const TRepositories& iRepositories;
383 CEditorCommandDeleteParFormat*
384 CastToCEditorCommandDeleteParFormat() { return this; }
386 CEditorCommandDeleteParFormat(const TRepositories& aReps, MUnifiedEditor& aTarget)
387 : iTarget(aTarget), iRepositories(aReps) {}
389 ~CEditorCommandDeleteParFormat();
390 static CEditorCommandDeleteParFormat* NewL(const TRepositories& aReps,
391 TInt aPos, TInt aLength,
392 MUnifiedEditor& aTarget);
394 CCommand* CreateInverseL() const;
395 TInt ExecuteL() const;
397 // This command can be coalesced with others of the same type
398 TBool CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const;
399 void Add(TInt aPos, TInt aLength);
403 * Command for applying a character format to a run of text that has no
404 * existing specific character format.
407 * @since App-frameworks6.1
409 NONSHARABLE_CLASS(CEditorCommandSetCharFormat) : public CEditorCommand
411 MUnifiedEditor& iTarget;
415 RUniqueInstance<TTmCharFormat> iChar;
416 TTmCharFormatMask iCharMask;
418 const TRepositories& iRepositories;
420 CEditorCommandSetCharFormat(const TRepositories& aReps, MUnifiedEditor& aTarget)
421 : iTarget(aTarget), iChar(*aReps.iChar), iRepositories(aReps) {}
423 ~CEditorCommandSetCharFormat();
424 static CEditorCommandSetCharFormat* NewL(const TRepositories& aReps,
425 TInt aPos, TInt aLength,
426 const TTmCharFormat* aChar, TTmCharFormatMask aCharMask,
427 MUnifiedEditor& aTarget);
429 // get as much of a run as possible.
430 static CEditorCommandSetCharFormat* NewL(const TRepositories& aReps,
431 TInt aPos, TInt& aRemaining, MUnifiedEditor& aTarget);
433 // get as many runs as necessary and return a batch if necessary.
434 static CCommand* NewBatchL(const TRepositories& aReps,
435 TInt aPos, TInt aLength, MUnifiedEditor& aTarget);
437 TBool PrepareToAddInverseToLastL(CSingleCommand& aLastCommand) const;
438 void AddInverseToLast(CSingleCommand& aLastCommand) const;
439 CCommand* CreateInverseL() const;
440 TInt ExecuteL() const;
444 * Command for applying a paragraph format to a run of text that has no
445 * existing specific paragraph format.
448 * @since App-frameworks6.1
450 NONSHARABLE_CLASS(CEditorCommandSetParFormat) : public CEditorCommand
452 MUnifiedEditor& iTarget;
456 RUniqueInstance<RTmParFormat> iPar;
457 TTmParFormatMask iParMask;
459 const TRepositories& iRepositories;
461 CEditorCommandSetParFormat(const TRepositories& aReps, MUnifiedEditor& aTarget)
462 : iTarget(aTarget), iPar(*aReps.iPar), iRepositories(aReps) {}
464 ~CEditorCommandSetParFormat();
465 static CEditorCommandSetParFormat* NewL(const TRepositories& aReps,
466 TInt aPos, TInt aLength,
467 const RTmParFormat* aFormat, TTmParFormatMask aMask,
468 MUnifiedEditor& aTarget);
470 // get as much of a run as possible.
471 static CEditorCommandSetParFormat* NewL(const TRepositories& aReps,
472 TInt aPos, TInt& aRemaining, MUnifiedEditor& aTarget);
474 // get as many runs as necessary and return a batch if necessary.
475 static CCommand* NewBatchL(const TRepositories& aReps,
476 TInt aPos, TInt aLength, MUnifiedEditor& aTarget);
478 TBool PrepareToAddInverseToLastL(CSingleCommand& aLastCommand) const;
479 void AddInverseToLast(CSingleCommand& aLastCommand) const;
480 CCommand* CreateInverseL() const;
481 TInt ExecuteL() const;
485 * Command for deleting a picture from the text. The picture is assumed
486 * to be without significant formatting.
489 * @since App-frameworks6.1
491 NONSHARABLE_CLASS(CEditorCommandDeletePicture) : public CEditorCommand,
492 private MPictureOwner
494 MUnifiedEditor& iTarget;
496 const TRepositories& iRepositories;
498 * Will own picture after us.
500 mutable CEditorCommandInsertPicture* iPictureOwnerDelegate;
502 void ForgetDelegate();
503 CEditorCommandDeletePicture(const TRepositories& aReps, MUnifiedEditor& aTarget)
504 : iTarget(aTarget), iRepositories(aReps) {}
506 ~CEditorCommandDeletePicture();
507 static CEditorCommandDeletePicture* NewL(const TRepositories& aReps,
508 TInt aPos, MUnifiedEditor& aTarget);
510 UndoSystem::CCommand* CreateInverseL() const;
511 TInt ExecuteL() const;
514 ///////////////////////////////////
516 // CEditorCommandSetBaseFormat //
518 ///////////////////////////////////
520 CEditorCommandSetBaseFormat::CEditorCommandSetBaseFormat(
521 const TRepositories& aReps, MUnifiedEditor& aTarget)
522 : iTarget(aTarget), iChar(*aReps.iChar), iPar(*aReps.iPar),
527 CEditorCommandSetBaseFormat::~CEditorCommandSetBaseFormat()
533 CEditorCommandSetBaseFormat* CEditorCommandSetBaseFormat::NewL(
534 const TRepositories& aReps,
535 const TTmCharFormat& aCharFormat, const RTmParFormat& aParFormat,
536 MUnifiedEditor& aTarget)
538 CEditorCommandSetBaseFormat* r
539 = new(ELeave) CEditorCommandSetBaseFormat(aReps, aTarget);
540 CleanupStack::PushL(r);
541 r->iChar.TakeCopyL(&aCharFormat);
542 r->iPar.TakeCopyL(&aParFormat);
547 CEditorCommandSetBaseFormat* CEditorCommandSetBaseFormat::NewL(
548 const TRepositories& aReps, MUnifiedEditor& aTarget)
552 CleanupClosePushL(p);
553 aTarget.GetBaseFormatL(c, p);
554 CEditorCommandSetBaseFormat* r = NewL(aReps, c, p, aTarget);
555 CleanupStack::PopAndDestroy();
559 CCommand* CEditorCommandSetBaseFormat::CreateInverseL() const
561 return NewL(iRepositories, iTarget);
564 TInt CEditorCommandSetBaseFormat::ExecuteL() const
566 iTarget.SetBaseFormatL(*iChar.Peek(), *iPar.Peek());
570 /////////////////////////////////
572 // CEditorCommandRenameStyle //
574 /////////////////////////////////
576 CEditorCommandRenameStyle::~CEditorCommandRenameStyle()
582 CEditorCommandRenameStyle* CEditorCommandRenameStyle::NewL(
583 const TRepositories& aReps,
584 const TDesC& aOldName, const TDesC& aNewName, MUnifiedEditor& aTarget)
586 CEditorCommandRenameStyle* r =
587 new(ELeave) CEditorCommandRenameStyle(aReps, aTarget);
588 CleanupStack::PushL(r);
589 r->iOldName.TakeCopyL(&aOldName);
590 r->iNewName.TakeCopyL(&aNewName);
591 CleanupStack::Pop(r);
595 CCommand* CEditorCommandRenameStyle::CreateInverseL() const
597 return NewL(iRepositories, *iNewName.Peek(), *iOldName.Peek(), iTarget);
600 TInt CEditorCommandRenameStyle::ExecuteL() const
602 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
604 return si->RenameStyleL(*iOldName.Peek(), *iNewName.Peek());
607 ///////////////////////////////////
609 // CEditorCommandInsertPicture //
611 ///////////////////////////////////
613 CEditorCommandInsertPicture::~CEditorCommandInsertPicture()
616 iPictureOwner->ForgetDelegate();
617 iPicture.DeletePicture();
620 CEditorCommandInsertPicture* CEditorCommandInsertPicture::NewL(
621 const TRepositories& aReps,
622 TInt aPos, MPictureOwner& aPictureOwner, MUnifiedEditor& aTarget)
624 CEditorCommandInsertPicture* r = new(ELeave)
625 CEditorCommandInsertPicture(aReps, aTarget);
626 r->iPictureOwner = &aPictureOwner;
631 void CEditorCommandInsertPicture::TakePictureOwnership(TPictureHeader& aPic)
637 void CEditorCommandInsertPicture::ForgetOwner(MPictureOwner*
643 ASSERT(aOwner == iPictureOwner);
647 CCommand* CEditorCommandInsertPicture::CreateInverseL() const
649 return CEditorCommandDeletePicture::NewL(iRepositories, iPos, iTarget);
652 TInt CEditorCommandInsertPicture::ExecuteL() const
654 ASSERT(iPictureOwner == 0);
655 MUnifiedEditor::MPictureSupport* pi = iTarget.PictureSupport();
657 pi->InsertPictureL(iPos, iPicture);
658 iPicture.iPicture = 0;
659 iPicture.iPictureType = KNullUid;
664 ///////////////////////////////////
666 // CEditorCommandDeletePicture //
668 ///////////////////////////////////
670 CEditorCommandDeletePicture::~CEditorCommandDeletePicture()
672 if (iPictureOwnerDelegate)
673 iPictureOwnerDelegate->ForgetOwner(this);
676 void CEditorCommandDeletePicture::ForgetDelegate()
678 iPictureOwnerDelegate = 0;
681 CEditorCommandDeletePicture* CEditorCommandDeletePicture::NewL(
682 const TRepositories& aReps, TInt aPos, MUnifiedEditor& aTarget)
684 CEditorCommandDeletePicture* r =
685 new(ELeave) CEditorCommandDeletePicture(aReps, aTarget);
690 CCommand* CEditorCommandDeletePicture::CreateInverseL() const
692 CEditorCommandDeletePicture* nonConstThis =
693 const_cast<CEditorCommandDeletePicture*>(this); //yuck
694 CEditorCommandInsertPicture* inv =
695 CEditorCommandInsertPicture::NewL(iRepositories, iPos,
696 *nonConstThis, iTarget);
697 CleanupStack::PushL(inv);
699 if (iPictureOwnerDelegate)
700 iPictureOwnerDelegate->ForgetOwner(nonConstThis);
702 iPictureOwnerDelegate = inv;
703 CleanupStack::Pop(inv);
708 TInt CEditorCommandDeletePicture::ExecuteL() const
710 MUnifiedEditor::MPictureSupport* pi = iTarget.PictureSupport();
713 pi->Picture(iPos, pic);
714 pi->DropPictureL(iPos);
715 if (iPictureOwnerDelegate)
717 iPictureOwnerDelegate->TakePictureOwnership(pic);
718 iPictureOwnerDelegate = 0;
725 /////////////////////////////////
727 // CEditorCommandCreateStyle //
729 /////////////////////////////////
731 CEditorCommandCreateStyle* CEditorCommandCreateStyle::NewL(const TRepositories& aReps,
732 const TDesC& aName, const TDesC& aNext,
733 TTmCharFormat& aChar, TTmCharFormatMask aCharMask,
734 RTmParFormat& aPar, TTmParFormatMask aParMask,
735 TInt aLevel, MUnifiedEditor& aTarget)
737 CEditorCommandCreateStyle* p = new(ELeave) CEditorCommandCreateStyle(aReps, aTarget);
738 CleanupStack::PushL(p);
739 p->iName.TakeCopyL(&aName);
740 p->iNext.TakeCopyL(&aNext);
741 p->iChar.TakeCopyL(&aChar);
742 p->iCharMask = aCharMask;
743 p->iPar.TakeCopyL(&aPar);
744 p->iParMask = aParMask;
746 CleanupStack::Pop(p);
749 CEditorCommandCreateStyle* CEditorCommandCreateStyle::NewL(const TRepositories& aReps,
750 const TDesC& aName, MUnifiedEditor& aTarget)
753 MUnifiedEditor::MStyleSupport* si = aTarget.StyleSupport();
755 si->GetStyleByNameL(aName, style);
756 CleanupClosePushL(style);
757 CEditorCommandCreateStyle* p = NewL(aReps, aName, style.iNextStyleName,
758 style.iCharFormat.iFormat, style.iCharFormat.iMask,
759 style.iParFormat.iFormat, style.iParFormat.iMask,
760 style.iOutlineLevel, aTarget);
761 CleanupStack::PopAndDestroy();
765 CCommand* CEditorCommandCreateStyle::NewBatchL(const TRepositories& aReps,
767 MUnifiedEditor& aTarget)
769 CCommand* inverse = 0;
770 MUnifiedEditor::MStyleSupport* si = aTarget.StyleSupport();
773 TInt remainingLength = aTarget.DocumentLength();
774 TInt currentPosition = 0;
775 // got to be careful.. GetStyleL() can return KMaxTInt as the run length!
776 // luckily, any nonnegative TInt - KMaxTInt gives a negative TInt
777 while (0 < remainingLength)
781 si->GetStyle(currentPosition, name, runLength);
784 CleanupStack::PushL(inverse);
785 // coverity[double_free]
786 inverse = CoalesceL(inverse,
787 CEditorCommandSetStyle::NewL(aReps, aName,
788 currentPosition, runLength, aTarget));
791 currentPosition += runLength;
792 remainingLength -= runLength;
795 CleanupStack::PushL(inverse);
796 // coverity[double_free]
797 inverse = CoalesceL(inverse,
798 CEditorCommandCreateStyle::NewL(aReps, aName, aTarget));
803 CEditorCommandCreateStyle::~CEditorCommandCreateStyle()
811 CCommand* CEditorCommandCreateStyle::CreateInverseL() const
813 return CEditorCommandDeleteStyle::NewL(iRepositories, *iName.Peek(), iTarget);
816 TInt CEditorCommandCreateStyle::ExecuteL() const
819 CleanupClosePushL(style);
820 style.iName = *iName.Peek();
821 style.iNextStyleName = *iNext.Peek();
822 style.iCharFormat.iFormat = *iChar.Peek();
823 style.iCharFormat.iMask = iCharMask;
824 style.iParFormat.iFormat.CopyL( *iPar.Peek() );
825 style.iParFormat.iMask.iFlags = iParMask.iFlags;
826 style.iOutlineLevel = iLevel;
827 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
829 TInt err = si->CreateStyleL(style);
830 CleanupStack::PopAndDestroy();
834 /////////////////////////////////
836 // CEditorCommandDeleteStyle //
838 /////////////////////////////////
840 CEditorCommandDeleteStyle* CEditorCommandDeleteStyle::NewL(const TRepositories& aReps,
841 const TDesC& aName, MUnifiedEditor& aTarget)
843 CEditorCommandDeleteStyle* p = new(ELeave) CEditorCommandDeleteStyle(aReps, aTarget);
844 CleanupStack::PushL(p);
845 p->iName.TakeCopyL(&aName);
846 CleanupStack::Pop(p);
850 CEditorCommandDeleteStyle::~CEditorCommandDeleteStyle()
855 CCommand* CEditorCommandDeleteStyle::CreateInverseL() const
857 return CEditorCommandCreateStyle::NewBatchL(iRepositories, *iName.Peek(), iTarget);
860 TInt CEditorCommandDeleteStyle::ExecuteL() const
862 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
864 return si->DeleteStyleL(*iName.Peek());
867 ////////////////////////////////
869 // CEditorCommandAlterStyle //
871 ////////////////////////////////
873 CEditorCommandAlterStyle* CEditorCommandAlterStyle::NewL(const TRepositories& aReps,
874 const TDesC& aName, const TDesC& aNext,
875 TTmCharFormat& aChar, TTmCharFormatMask& aCharMask,
876 RTmParFormat& aPar, TTmParFormatMask& aParMask,
877 TInt aLevel, MUnifiedEditor& aTarget)
879 CEditorCommandAlterStyle* p = new(ELeave) CEditorCommandAlterStyle(aReps, aTarget);
880 CleanupStack::PushL(p);
881 p->iName.TakeCopyL(&aName);
882 p->iNext.TakeCopyL(&aNext);
883 p->iChar.TakeCopyL(&aChar);
884 p->iCharMask = aCharMask;
885 p->iPar.TakeCopyL(&aPar);
886 p->iParMask = aParMask;
888 CleanupStack::Pop(p);
892 CEditorCommandAlterStyle* CEditorCommandAlterStyle::NewL(const TRepositories& aReps,
893 const TDesC& aName, MUnifiedEditor& aTarget)
896 CleanupClosePushL(style);
897 MUnifiedEditor::MStyleSupport* si = aTarget.StyleSupport();
899 si->GetStyleByNameL(aName, style);
900 CEditorCommandAlterStyle* p = NewL(aReps, aName, style.iNextStyleName,
901 style.iCharFormat.iFormat, style.iCharFormat.iMask,
902 style.iParFormat.iFormat, style.iParFormat.iMask,
903 style.iOutlineLevel, aTarget);
904 CleanupStack::PopAndDestroy();
908 CEditorCommandAlterStyle::~CEditorCommandAlterStyle()
916 CCommand* CEditorCommandAlterStyle::CreateInverseL() const
918 return NewL(iRepositories, *iName.Peek(), iTarget);
921 TInt CEditorCommandAlterStyle::ExecuteL() const
924 CleanupClosePushL(style);
925 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
927 si->GetStyleByNameL(*iName.Peek(), style);
928 style.iCharFormat.iFormat = *iChar.Peek();
929 style.iCharFormat.iMask = iCharMask;
930 style.iParFormat.iFormat.CopyL( *iPar.Peek() );
931 style.iParFormat.iMask = iParMask;
932 style.iOutlineLevel = iLevel;
933 style.iName = *iName.Peek();
934 style.iNextStyleName = *iNext.Peek();
935 TInt err = si->ChangeStyleL(style);
936 CleanupStack::PopAndDestroy();
940 //////////////////////////////
942 // CEditorCommandSetStyle //
944 //////////////////////////////
946 CEditorCommandSetStyle::~CEditorCommandSetStyle()
951 CEditorCommandSetStyle* CEditorCommandSetStyle::NewL(const TRepositories& aReps,
952 const TDesC& aName, TInt aPos, TInt aRunLength, MUnifiedEditor& aTarget)
954 CEditorCommandSetStyle* r = new (ELeave) CEditorCommandSetStyle(aReps, aTarget);
955 CleanupStack::PushL(r);
956 r->iName.TakeCopyL(&aName);
958 r->iRunLength = aRunLength;
959 CleanupStack::Pop(r);
963 CCommand* CEditorCommandSetStyle::NewL(
964 const TRepositories& aReps, TInt aPos, TInt aRunLength, MUnifiedEditor& aTarget)
966 CCommand* inverse = 0;
967 MUnifiedEditor::MStyleSupport* si = aTarget.StyleSupport();
969 while (0 < aRunLength)
973 si->GetStyle(aPos, name, runLength);
974 if (aRunLength < runLength)
975 runLength = aRunLength;
976 CleanupStack::PushL(inverse);
977 // coverity[double_free]
978 inverse = CoalesceL(inverse, NewL(aReps, name, aPos, runLength, aTarget));
981 aRunLength -= runLength;
986 CCommand* CEditorCommandSetStyle::CreateInverseL() const
988 return NewL(iRepositories, iPos, iRunLength, iTarget);
991 TInt CEditorCommandSetStyle::ExecuteL() const
993 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
995 return si->SetStyleL(iPos, iRunLength, *iName.Peek());
998 ////////////////////////////////
1000 // CEditorCommandDeleteText //
1002 ////////////////////////////////
1004 CEditorCommandDeleteText::~CEditorCommandDeleteText() {}
1006 CEditorCommandDeleteText* CEditorCommandDeleteText::NewL(const TRepositories& aReps,
1007 TInt aPos, TInt aLength, MUnifiedEditor& aTarget)
1009 return new(ELeave) CEditorCommandDeleteText(aReps,
1010 aTarget, aPos, aLength);
1013 CCommand* CEditorCommandDeleteText::CreateInverseL() const
1015 return CEditorCommandInsertTextAndFormat::NewBatchL(
1016 iRepositories, iImpl.Pos(), iImpl.Length(), iImpl.Target());
1019 TInt CEditorCommandDeleteText::ExecuteL() const
1021 return iImpl.ExecuteL();
1024 TBool CEditorCommandDeleteText::CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const
1026 return iImpl.CanAdd(aPos, aLength, aTarget);
1029 void CEditorCommandDeleteText::Add(TInt aLength)
1034 /////////////////////////////////////////
1036 // CEditorCommandInsertTextAndFormat //
1038 /////////////////////////////////////////
1040 CEditorCommandInsertTextAndFormat::~CEditorCommandInsertTextAndFormat()
1047 CEditorCommandInsertTextAndFormat* CEditorCommandInsertTextAndFormat::NewL(
1048 const TRepositories& aReps,
1049 CEditorCommandInsertTextAndFormat::RTextAndFormatParameters& aParams,
1050 MUnifiedEditor& aTarget)
1052 ASSERT(aParams.iText.Length() <= KMaxCharsInSingleCommand);
1053 CEditorCommandInsertTextAndFormat* r = new(ELeave)
1054 CEditorCommandInsertTextAndFormat(aReps, aTarget, aParams.iPos, aParams.iText);
1055 CleanupStack::PushL(r);
1056 r->iChar.TakeCopyL(&aParams.iChar.iFormat);
1057 r->iCharMask = aParams.iChar.iMask;
1058 r->iPar.TakeCopyL(&aParams.iPar.iFormat);
1059 r->iParMask.iFlags = aParams.iPar.iMask.iFlags;
1060 r->iStyle.TakeCopyL(&aParams.iStyleName);
1061 CleanupStack::Pop(r);
1065 void CEditorCommandInsertTextAndFormat::RTextAndFormatParameters::Close()
1070 void CEditorCommandInsertTextAndFormat::RTextAndFormatParameters::SetL(
1071 TInt aPos, TInt aMaxLength, MUnifiedEditor& aTarget)
1074 aTarget.GetText(aPos, iText);
1076 TInt length = iText.Length();
1079 aTarget.GetCharFormat(aPos, MUnifiedEditor::ESpecific, iChar, charLength);
1082 aTarget.GetParFormatL(aPos, MUnifiedEditor::ESpecific, iPar, parLength);
1084 TInt styleLength = length;
1085 MUnifiedEditor::MStyleSupport* si = aTarget.StyleSupport();
1087 si->GetStyle(aPos, iStyleName, styleLength);
1089 iStyleName.Set(TPtrC());
1091 if (charLength < length)
1092 length = charLength;
1093 if (parLength < length)
1095 if (styleLength < length)
1096 length = styleLength;
1097 if (aMaxLength < length)
1098 length = aMaxLength;
1099 if (KMaxCharsInSingleCommand < length)
1100 length = KMaxCharsInSingleCommand;
1101 iText.Set(iText.Ptr(), length);
1104 CEditorCommandInsertTextAndFormat* CEditorCommandInsertTextAndFormat::NewL(
1105 const TRepositories& aReps, TInt aPos, TInt& aRemaining,
1106 TInt aOriginalPos, MUnifiedEditor& aTarget)
1108 RTextAndFormatParameters params;
1109 CleanupClosePushL(params);
1110 params.SetL(aPos, aRemaining, aTarget);
1112 params.iPos = aOriginalPos;
1113 CEditorCommandInsertTextAndFormat* p = NewL(aReps, params, aTarget);
1114 aRemaining -= params.iText.Length();
1116 CleanupStack::PopAndDestroy();
1121 CCommand* CEditorCommandInsertTextAndFormat::NewBatchL(const TRepositories& aReps,
1122 TInt aPos, TInt aLength, MUnifiedEditor& aTarget)
1124 CCommand* command = 0;
1125 TInt oldLength = aLength + 1;
1126 TInt end = aPos + aLength;
1127 // while there is still some length to go and there is still some
1129 while (0 < aLength && aLength < oldLength)
1131 oldLength = aLength;
1132 CleanupStack::PushL(command);
1133 // coverity[double_free]
1134 command = CoalesceL(command,
1135 NewL(aReps, end - aLength, aLength, aPos, aTarget));
1136 CleanupStack::Pop();
1142 CCommand* CEditorCommandInsertTextAndFormat::CreateInverseL() const
1144 return CEditorCommandDeleteText::NewL(iRepositories,
1145 iImpl.Pos(), iImpl.Text().Length(), iImpl.Target());
1148 TInt CEditorCommandInsertTextAndFormat::ExecuteL() const
1150 TTmCharFormatLayer charLayer;
1151 RTmParFormatLayer parLayer;
1153 TTmCharFormatLayer* pCharLayer = 0;
1154 RTmParFormatLayer* pParLayer = 0;
1158 charLayer.iFormat = *iChar.Peek();
1159 charLayer.iMask = iCharMask;
1160 pCharLayer = &charLayer;
1164 parLayer.iFormat.CopyL( *iPar.Peek() );
1165 parLayer.iMask = iParMask;
1166 pParLayer = &parLayer;
1167 CleanupClosePushL(parLayer);
1169 TInt result = iImpl.ExecuteL(iStyle.Peek(), pCharLayer, pParLayer);
1171 CleanupStack::PopAndDestroy();
1175 TBool CEditorCommandInsertTextAndFormat::CanAdd(
1176 const CEditorCommandInsertTextAndFormat::RTextAndFormatParameters& aParams,
1177 MUnifiedEditor& aTarget) const
1179 if (!iImpl.CanAdd(aParams.iPos, aParams.iText, aTarget))
1187 if (aParams.iStyleName != *iStyle.Peek())
1189 if (aParams.iPar.iMask.iFlags != iParMask.iFlags
1190 || aParams.iPar.iFormat != *iPar.Peek())
1192 if (iCharMask.iFlags != aParams.iChar.iMask.iFlags
1193 || aParams.iChar.iFormat != *iChar.Peek())
1195 // coalescence is not possible if the new text contains pictures.
1196 if (0 <= UndoSystem::FindPicture(aTarget, aParams.iPos, aParams.iText.Length()))
1201 void CEditorCommandInsertTextAndFormat::Add(TInt aPos, const TDesC& aText)
1203 iImpl.Add(aPos, aText);
1206 ///////////////////////////////////
1208 // CEditorCommandSetCharFormat //
1210 ///////////////////////////////////
1212 CEditorCommandSetCharFormat::~CEditorCommandSetCharFormat()
1217 CEditorCommandSetCharFormat* CEditorCommandSetCharFormat::NewL(
1218 const TRepositories& aReps, TInt aPos, TInt aLength,
1219 const TTmCharFormat* aChar, TTmCharFormatMask aCharMask,
1220 MUnifiedEditor& aTarget)
1222 CEditorCommandSetCharFormat* r = new(ELeave) CEditorCommandSetCharFormat(aReps, aTarget);
1223 CleanupStack::PushL(r);
1225 r->iLength = aLength;
1226 r->iChar.TakeCopyL(aChar);
1227 r->iCharMask= aCharMask;
1228 CleanupStack::Pop(r);
1232 // get as much of a run as possible.
1233 CEditorCommandSetCharFormat* CEditorCommandSetCharFormat::NewL(const TRepositories& aReps,
1234 TInt aPos, TInt& aRemaining, MUnifiedEditor& aTarget)
1236 TTmCharFormatLayer format;
1238 aTarget.GetCharFormat(aPos, MUnifiedEditor::ESpecific, format, length);
1239 if (aRemaining < length)
1240 length = aRemaining;
1241 CEditorCommandSetCharFormat* r =
1242 NewL(aReps, aPos, length, &format.iFormat, format.iMask, aTarget);
1243 aRemaining -= length;
1247 // get as many runs as necessary and return a batch if necessary.
1248 CCommand* CEditorCommandSetCharFormat::NewBatchL(const TRepositories& aReps,
1249 TInt aPos, TInt aLength, MUnifiedEditor& aTarget)
1251 CCommand* command = 0;
1252 TInt oldLength = aLength + 1;
1253 TInt end = aPos + aLength;
1254 while (0 < aLength && aLength < oldLength)
1256 oldLength = aLength;
1257 CleanupStack::PushL(command);
1258 // coverity[double_free]
1259 command = CoalesceL(command, NewL(aReps, end - aLength, aLength, aTarget));
1260 CleanupStack::Pop(); // command, as was before call to CoalesceL
1265 CCommand* CEditorCommandSetCharFormat::CreateInverseL() const
1267 return CEditorCommandDeleteCharFormat::NewL(iRepositories, iPos, iLength, iTarget);
1270 TInt CEditorCommandSetCharFormat::ExecuteL() const
1272 TTmCharFormatLayer layer;
1273 layer.iFormat = *iChar.Peek();
1274 layer.iMask = iCharMask;
1275 iTarget.SetCharFormatL(iPos, iLength, layer);
1279 TBool CEditorCommandSetCharFormat::PrepareToAddInverseToLastL(CSingleCommand& aLastCommand) const
1281 if (aLastCommand.FamilyUid() != TUid::Uid(KUndoDllUid))
1283 CEditorCommandDeleteCharFormat* last =
1284 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeleteCharFormat();
1287 return last->CanAdd(iPos, iLength, iTarget);
1290 void CEditorCommandSetCharFormat::AddInverseToLast(CSingleCommand& aLastCommand) const
1292 ASSERT(aLastCommand.FamilyUid() == TUid::Uid(KUndoDllUid));
1293 CEditorCommandDeleteCharFormat* last =
1294 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeleteCharFormat();
1296 last->Add(iPos, iLength);
1299 //////////////////////////////////
1301 // CEditorCommandSetParFormat //
1303 //////////////////////////////////
1305 CEditorCommandSetParFormat::~CEditorCommandSetParFormat()
1310 CEditorCommandSetParFormat* CEditorCommandSetParFormat::NewL(
1311 const TRepositories& aReps, TInt aPos, TInt aLength,
1312 const RTmParFormat* aFormat, TTmParFormatMask aMask,
1313 MUnifiedEditor& aTarget)
1315 CEditorCommandSetParFormat* r = new(ELeave) CEditorCommandSetParFormat(aReps, aTarget);
1316 CleanupStack::PushL(r);
1318 r->iLength = aLength;
1319 r->iPar.TakeCopyL(aFormat);
1321 CleanupStack::Pop(r);
1325 // get as much of a run as possible.
1326 CEditorCommandSetParFormat* CEditorCommandSetParFormat::NewL(const TRepositories& aReps,
1327 TInt aPos, TInt& aRemaining, MUnifiedEditor& aTarget)
1329 RTmParFormatLayer format;
1331 CleanupClosePushL(format);
1332 aTarget.GetParFormatL(aPos, MUnifiedEditor::ESpecific, format, length);
1333 if (aRemaining < length)
1334 length = aRemaining;
1335 CEditorCommandSetParFormat* r =
1336 NewL(aReps, aPos, length, &format.iFormat, format.iMask, aTarget);
1337 aRemaining -= length;
1338 CleanupStack::PopAndDestroy(); // format
1342 // get as many runs as necessary and return a batch if necessary.
1343 CCommand* CEditorCommandSetParFormat::NewBatchL(const TRepositories& aReps,
1344 TInt aPos, TInt aLength, MUnifiedEditor& aTarget)
1346 CCommand* command = 0;
1347 TInt oldLength = aLength + 1;
1348 TInt end = aPos + aLength;
1349 while (0 < aLength && aLength < oldLength)
1351 oldLength = aLength;
1352 CleanupStack::PushL(command);
1353 // coverity[double_free]
1354 command = CoalesceL(command, NewL(aReps, end - aLength, aLength, aTarget));
1355 CleanupStack::Pop(); // command, as was before call to CoalesceL
1360 CCommand* CEditorCommandSetParFormat::CreateInverseL() const
1362 return CEditorCommandDeleteParFormat::NewL(iRepositories, iPos, iLength, iTarget);
1365 TInt CEditorCommandSetParFormat::ExecuteL() const
1367 RTmParFormatLayer layer;
1368 CleanupClosePushL(layer);
1369 layer.iFormat.CopyL(*iPar.Peek());
1370 layer.iMask = iParMask;
1371 iTarget.SetParFormatL(iPos, iLength, layer);
1372 CleanupStack::PopAndDestroy();
1376 TBool CEditorCommandSetParFormat::PrepareToAddInverseToLastL(CSingleCommand& aLastCommand) const
1378 if (aLastCommand.FamilyUid() != TUid::Uid(KUndoDllUid))
1380 CEditorCommandDeleteParFormat* last =
1381 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeleteParFormat();
1384 return last->CanAdd(iPos, iLength, iTarget);
1387 void CEditorCommandSetParFormat::AddInverseToLast(CSingleCommand& aLastCommand) const
1389 ASSERT(aLastCommand.FamilyUid() == TUid::Uid(KUndoDllUid));
1390 CEditorCommandDeleteParFormat* last =
1391 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeleteParFormat();
1393 last->Add(iPos, iLength);
1396 //////////////////////////////////////
1398 // CEditorCommandDeleteCharFormat //
1400 //////////////////////////////////////
1402 CEditorCommandDeleteCharFormat::~CEditorCommandDeleteCharFormat() {}
1404 CEditorCommandDeleteCharFormat* CEditorCommandDeleteCharFormat::NewL(
1405 const TRepositories& aReps, TInt aPos, TInt aLength, MUnifiedEditor& aTarget)
1407 CEditorCommandDeleteCharFormat* r =
1408 new(ELeave) CEditorCommandDeleteCharFormat(aReps, aTarget);
1410 r->iLength = aLength;
1414 CCommand* CEditorCommandDeleteCharFormat::CreateInverseL() const
1416 return CEditorCommandSetCharFormat::NewBatchL(iRepositories, iPos, iLength, iTarget);
1419 TInt CEditorCommandDeleteCharFormat::ExecuteL() const
1421 iTarget.DeleteCharFormatL(iPos, iLength);
1425 TBool CEditorCommandDeleteCharFormat::CanAdd(TInt aPos, TInt aLength, MUnifiedEditor &aTarget) const
1427 return &aTarget == &iTarget
1428 && aPos <= iPos + iLength
1429 && iPos <= aPos + aLength? ETrue : EFalse;
1432 void CEditorCommandDeleteCharFormat::Add(TInt aPos, TInt aLength)
1434 TInt min = aPos < iPos? aPos : iPos;
1435 TInt max = iPos + iLength;
1436 TInt max2= aPos + aLength;
1438 iLength = (max < max2? max2 : max) - min;
1441 /////////////////////////////////////
1443 // CEditorCommandDeleteParFormat //
1445 /////////////////////////////////////
1447 CEditorCommandDeleteParFormat::~CEditorCommandDeleteParFormat() {}
1449 CEditorCommandDeleteParFormat* CEditorCommandDeleteParFormat::NewL(
1450 const TRepositories& aReps, TInt aPos, TInt aLength, MUnifiedEditor& aTarget)
1452 CEditorCommandDeleteParFormat* r =
1453 new(ELeave) CEditorCommandDeleteParFormat(aReps, aTarget);
1455 r->iLength = aLength;
1459 CCommand* CEditorCommandDeleteParFormat::CreateInverseL() const
1461 return CEditorCommandSetParFormat::NewBatchL(iRepositories, iPos, iLength, iTarget);
1464 TInt CEditorCommandDeleteParFormat::ExecuteL() const
1466 iTarget.DeleteParFormatL(iPos, iLength);
1470 TBool CEditorCommandDeleteParFormat::CanAdd(TInt aPos, TInt aLength, MUnifiedEditor& aTarget) const
1472 return &aTarget == &iTarget
1473 && aPos <= iPos + iLength
1474 && iPos <= aPos + aLength? ETrue : EFalse;
1477 void CEditorCommandDeleteParFormat::Add(TInt aPos, TInt aLength)
1479 TInt min = aPos < iPos? aPos : iPos;
1480 TInt max = iPos + iLength;
1481 TInt max2= aPos + aLength;
1483 iLength = (max < max2? max2 : max) - min;
1486 //////////////////////////
1488 // Command prototypes //
1490 //////////////////////////
1493 // Attribute setters
1495 void CEditorCommandCreateStyleProto::Set(const RTmStyle& aStyle)
1500 void CEditorCommandChangeStyleProto::Set(const RTmStyle& aStyle)
1505 void CEditorCommandSetStyleProto::Set(TInt aPos,
1514 void CEditorCommandDeleteStyleProto::Set(const TDesC& aName)
1519 void CEditorCommandSetCharFormatProto::Set(TInt aPos,
1521 const TTmCharFormatLayer& aFormat)
1528 void CEditorCommandSetParFormatProto::Set(TInt aPos,
1530 const RTmParFormatLayer& aFormat)
1537 void CEditorCommandInsertProto::Set(TInt aPos,
1539 const TDesC* aStyle,
1540 const TTmCharFormatLayer* aCharFormat,
1541 const RTmParFormatLayer* aParFormat)
1546 iCharFormat = aCharFormat;
1547 iParFormat = aParFormat;
1550 void CEditorCommandDeleteProto::Set(TInt aPos, TInt aLength)
1555 // adjust 'iPos' and 'iLength' to be surrogate aligned, if possible
1556 if (iPos >= 0 && iPos <= iTarget.DocumentLength())
1558 // check the character at aPos
1560 iTarget.GetText(iPos, dest);
1561 if (dest.Length() > 0 && IsLowSurrogate(dest[0]))
1563 // try to decrease aPos by 1
1566 iTarget.GetText(iPos-1, dest);
1567 if (dest.Length() > 0 && IsHighSurrogate(dest[0]))
1574 // do nothing, just delete the corrupt surrogate
1586 // check the character at aPos+aLength
1588 iTarget.GetText(iPos+iLength-1, dest);
1589 if (dest.Length() > 0 && IsHighSurrogate(dest[0]))
1591 if (iPos + (iLength - 1) <= iTarget.DocumentLength())
1593 // try to increase aLength by 1
1594 iTarget.GetText(iPos+iLength, dest);
1595 if (dest.Length() > 0 && IsLowSurrogate(dest[0]))
1612 void CEditorCommandDeleteCharFormatProto::Set(TInt aPos, TInt aLength)
1618 void CEditorCommandDeleteParFormatProto::Set(TInt aPos, TInt aLength)
1624 void CEditorCommandDeletePictureProto::Set(TInt aPos)
1629 void CEditorCommandDestroyPictureProto::Set(TInt aPos)
1631 if (iPictureOwnerDelegate)
1633 iPictureOwnerDelegate->ForgetOwner(this);
1634 iPictureOwnerDelegate = 0;
1639 void CEditorCommandInsertPictureProto::Set(TInt aPos, const TPictureHeader& aPicture)
1642 iPicture = &aPicture;
1645 void CEditorCommandRenameStyleProto::Set(const TDesC& aOldName, const TDesC& aNewName)
1647 iOldName = &aOldName;
1648 iNewName = &aNewName;
1651 void CEditorCommandSetBaseFormatProto::Set(
1652 const TTmCharFormat* aCharFormat, const RTmParFormat* aParFormat)
1654 iChar = aCharFormat;
1658 void CEditorCommandPasteProto::Set(const CStreamStore& aStore,
1659 const CStreamDictionary& aStreamDictionary, TInt aPos)
1661 iImpl.Set(aStore, aStreamDictionary, aPos);
1665 // execution and inversion
1668 // CEditorCommandCreateStyleProto
1669 TInt CEditorCommandCreateStyleProto::ExecuteL() const
1671 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
1673 return si->CreateStyleL(*iStyle);
1676 CCommand* CEditorCommandCreateStyleProto::CreateInverseL() const
1678 return CEditorCommandDeleteStyle::NewL(Repositories(), iStyle->iName, iTarget);
1682 // CEditorCommandChangeStyleProto
1683 TInt CEditorCommandChangeStyleProto::ExecuteL() const
1685 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
1687 return si->ChangeStyleL(*iStyle);
1690 CCommand* CEditorCommandChangeStyleProto::CreateInverseL() const
1692 return CEditorCommandAlterStyle::NewL(Repositories(), iStyle->iName, iTarget);
1695 // CEditorCommandSetStyleProto
1696 TInt CEditorCommandSetStyleProto::ExecuteL() const
1698 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
1700 return si->SetStyleL(iPos, iLength, *iName);
1703 CCommand* CEditorCommandSetStyleProto::CreateInverseL() const
1705 return CEditorCommandSetStyle::NewL(Repositories(), iPos, iLength, iTarget);
1708 // CEditorCommandDeleteStyleProto
1709 TInt CEditorCommandDeleteStyleProto::ExecuteL() const
1711 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
1713 return si->DeleteStyleL(*iName);
1716 CCommand* CEditorCommandDeleteStyleProto::CreateInverseL() const
1718 return CEditorCommandCreateStyle::NewBatchL(Repositories(), *iName, iTarget);
1721 // CEditorCommandSetCharFormatProto
1722 TInt CEditorCommandSetCharFormatProto::ExecuteL() const
1724 iTarget.SetCharFormatL(iPos, iLength, *iFormat);
1728 CCommand* CEditorCommandSetCharFormatProto::CreateInverseL() const
1730 // inverse is to remove formatting and re-apply the old
1732 CEditorCommandSetCharFormat::NewBatchL(Repositories(),
1733 iPos, iLength, iTarget);
1734 CleanupStack::PushL(inverse);
1735 // coverity[double_free]
1736 inverse = CoalesceL(inverse,
1737 CEditorCommandDeleteCharFormat::NewL(Repositories(), iPos, iLength, iTarget));
1738 CleanupStack::Pop();
1742 // CEditorCommandSetParFormatProto
1743 TInt CEditorCommandSetParFormatProto::ExecuteL() const
1745 iTarget.SetParFormatL(iPos, iLength, *iFormat);
1749 CCommand* CEditorCommandSetParFormatProto::CreateInverseL() const
1751 // inverse is to remove formatting and re-apply the old
1753 CEditorCommandSetParFormat::NewBatchL(Repositories(), iPos, iLength, iTarget);
1754 CleanupStack::PushL(inverse);
1755 // coverity[double_free]
1756 inverse = CoalesceL(inverse,
1757 CEditorCommandDeleteParFormat::NewL(Repositories(), iPos, iLength, iTarget));
1758 CleanupStack::Pop();
1762 // CEditorCommandInsertProto
1763 TInt CEditorCommandInsertProto::ExecuteL() const
1765 iTarget.InsertTextL(iPos, *iText, iStyle, iCharFormat, iParFormat);
1769 CCommand* CEditorCommandInsertProto::CreateInverseL() const
1771 return CEditorCommandDeleteText::NewL(Repositories(), iPos, iText->Length(), iTarget);
1774 TBool CEditorCommandInsertProto::PrepareToAddInverseToLastL(CSingleCommand& aLastCommand) const
1776 if (aLastCommand.FamilyUid() != TUid::Uid(KUndoDllUid))
1778 CEditorCommandDeleteText* last =
1779 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeleteText();
1782 return last->CanAdd(iPos, iText->Length(), iTarget);
1785 void CEditorCommandInsertProto::AddInverseToLast(CSingleCommand& aLastCommand) const
1787 ASSERT(aLastCommand.FamilyUid() == TUid::Uid(KUndoDllUid));
1788 CEditorCommandDeleteText* last =
1789 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandDeleteText();
1791 last->Add(iText->Length());
1794 // CEditorCommandDeleteProto
1795 TInt CEditorCommandDeleteProto::ExecuteL() const
1797 iTarget.DeleteTextL(iPos, iLength);
1801 CCommand* CEditorCommandDeleteProto::CreateInverseL() const
1803 return CEditorCommandInsertTextAndFormat::NewBatchL(Repositories(),
1804 iPos, iLength, iTarget);
1807 TBool CEditorCommandDeleteProto::PrepareToAddInverseToLastL(CSingleCommand& aLastCommand) const
1809 if (iDeletedText.MaxLength() < iLength)
1811 if (aLastCommand.FamilyUid() != TUid::Uid(KUndoDllUid))
1813 CEditorCommandInsertTextAndFormat* last =
1814 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandInsertTextAndFormat();
1817 CEditorCommandInsertTextAndFormat::RTextAndFormatParameters params;
1818 CleanupClosePushL(params);
1819 params.SetL(iPos, iLength, iTarget);
1820 TBool result = EFalse;
1821 if (params.iText.Length() == iLength)
1823 result = last->CanAdd(params, iTarget);
1825 iDeletedText = params.iText;
1827 CleanupStack::PopAndDestroy();
1831 void CEditorCommandDeleteProto::AddInverseToLast(CSingleCommand& aLastCommand) const
1833 ASSERT(aLastCommand.FamilyUid() == TUid::Uid(KUndoDllUid));
1834 CEditorCommandInsertTextAndFormat* last =
1835 static_cast<CEditorCommand&>(aLastCommand).CastToCEditorCommandInsertTextAndFormat();
1837 last->Add(iPos, iDeletedText);
1840 // CEditorCommandDeleteCharFormatProto
1841 TInt CEditorCommandDeleteCharFormatProto::ExecuteL() const
1843 iTarget.DeleteCharFormatL(iPos, iLength);
1847 CCommand* CEditorCommandDeleteCharFormatProto::CreateInverseL() const
1849 return CEditorCommandSetCharFormat::NewBatchL(Repositories(),
1850 iPos, iLength, iTarget);
1853 // CEditorCommandDeleteParFormatProto
1854 TInt CEditorCommandDeleteParFormatProto::ExecuteL() const
1856 iTarget.DeleteParFormatL(iPos, iLength);
1860 CCommand* CEditorCommandDeleteParFormatProto::CreateInverseL() const
1862 return CEditorCommandSetParFormat::NewBatchL(Repositories(),
1863 iPos, iLength, iTarget);
1866 // CEditorCommandDeletePictureProto
1867 TInt CEditorCommandDeletePictureProto::ExecuteL() const
1869 MUnifiedEditor::MPictureSupport* pi = iTarget.PictureSupport();
1871 pi->DropPictureL(iPos);
1875 // CEditorCommandInsertPictureProto
1876 TInt CEditorCommandInsertPictureProto::ExecuteL() const
1878 MUnifiedEditor::MPictureSupport* pi = iTarget.PictureSupport();
1880 pi->InsertPictureL(iPos, *iPicture);
1884 CCommand* CEditorCommandInsertPictureProto::CreateInverseL() const
1886 return CEditorCommandDeletePicture::NewL(Repositories(), iPos, iTarget);
1889 // CEditorCommandDestroyPictureProto
1890 void CEditorCommandDestroyPictureProto::ForgetDelegate()
1892 iPictureOwnerDelegate = 0;
1895 CEditorCommandDestroyPictureProto::~CEditorCommandDestroyPictureProto()
1897 if (iPictureOwnerDelegate)
1898 iPictureOwnerDelegate->ForgetOwner(this);
1901 CCommand* CEditorCommandDestroyPictureProto::CreateInverseL() const
1903 CEditorCommandDestroyPictureProto* nonConstThis =
1904 const_cast<CEditorCommandDestroyPictureProto*>(this);
1905 if (iPictureOwnerDelegate)
1907 iPictureOwnerDelegate->ForgetOwner(nonConstThis);
1908 iPictureOwnerDelegate = 0;
1911 CEditorCommandSetStyle::NewL(Repositories(), iPos, 1, iTarget);
1912 CleanupStack::PushL(style);
1913 CCommand* charFormat =
1914 CEditorCommandSetCharFormat::NewBatchL(Repositories(), iPos, 1, iTarget);
1915 CleanupStack::PushL(charFormat);
1916 CCommand* parFormat =
1917 CEditorCommandSetParFormat::NewBatchL(Repositories(), iPos, 1, iTarget);
1918 CleanupStack::PushL(parFormat);
1919 iPictureOwnerDelegate =
1920 CEditorCommandInsertPicture::NewL(Repositories(), iPos,
1921 *const_cast<CEditorCommandDestroyPictureProto*>(nonConstThis), iTarget);
1922 // coverity[double_free]
1923 CCommand* command = CoalesceL(parFormat, iPictureOwnerDelegate);
1924 CleanupStack::Pop(); // parFormat, no longer owned
1925 // coverity[double_free]
1926 command = CoalesceL(charFormat, command);
1927 CleanupStack::Pop(); // charFormat, no longer owned
1928 // coverity[double_free]
1929 command = CoalesceL(style, command);
1930 CleanupStack::Pop(); // style, no longer owned
1935 TInt CEditorCommandDestroyPictureProto::ExecuteL() const
1937 MUnifiedEditor::MPictureSupport* pi = iTarget.PictureSupport();
1940 pi->Picture(iPos, pic);
1941 pi->DropPictureL(iPos);
1942 if (iPictureOwnerDelegate)
1944 iPictureOwnerDelegate->TakePictureOwnership(pic);
1945 iPictureOwnerDelegate = 0;
1948 pic.DeletePicture();
1952 // CEditorCommandRenameStyleProto
1953 TInt CEditorCommandRenameStyleProto::ExecuteL() const
1955 MUnifiedEditor::MStyleSupport* si = iTarget.StyleSupport();
1957 return si->RenameStyleL(*iOldName, *iNewName);
1960 CCommand* CEditorCommandRenameStyleProto::CreateInverseL() const
1962 return CEditorCommandRenameStyle::NewL(Repositories(),
1963 *iNewName, *iOldName, iTarget);
1966 // CEditorCommandPasteProto
1967 TInt CEditorCommandPasteProto::ExecuteL() const
1969 return iImpl.ExecuteL();
1972 UndoSystem::CCommand* CEditorCommandPasteProto::CreateInverseL() const
1974 RStoreReadStream stream;
1975 iImpl.OpenPlainTextStreamLC(stream);
1976 TInt pos = iImpl.Pos();
1977 TInt length = stream.ReadInt32L();
1978 CCommand* command = 0;
1979 CBufSeg* buf = CBufSeg::NewL(200);
1980 CleanupStack::PushL(buf);
1984 User::Leave(KErrNotSupported); // don't know how to undo
1986 RBufWriteStream bufferStream;
1987 bufferStream.Open(*buf);
1988 TMemoryStreamUnicodeSink sink(bufferStream);
1990 e.ExpandL(sink, stream, length);
1991 bufferStream.CommitL();
1992 bufferStream.Close();
1994 TInt end = pos + length;
2000 TInt lengthSearched = 0;
2001 TInt pic = KErrNotFound;
2002 while ((pic = current.Locate(CEditableText::EPictureCharacter)) < 0
2003 && pos + lengthSearched < end)
2005 lengthSearched += current.Length();
2006 TPtr8 seg(buf->Ptr(bufferPos));
2007 bufferPos += seg.Length();
2008 current.Set(reinterpret_cast<const TText*>(seg.Ptr()),
2009 seg.Length() / sizeof(TText));
2011 TInt currentSearched = 0 <= pic? pic : current.Length();
2012 lengthSearched += currentSearched;
2016 // we have some text to delete
2017 CleanupStack::PushL(command);
2018 // coverity[double_free]
2019 command = CoalesceL(command,
2020 CEditorCommandDeleteText::NewL(Repositories(), pos, lengthSearched,
2022 CleanupStack::Pop();
2023 pos += lengthSearched;
2028 ASSERT(current.Length());
2029 // we have a picture to delete
2030 CleanupStack::PushL(command);
2031 command = CoalesceL(command,
2032 CEditorCommandDeletePicture::NewL(Repositories(), pos, iImpl.Target()));
2033 CleanupStack::Pop();
2034 currentSearched += 1;
2038 current.Set(current.Mid(currentSearched));
2041 CleanupStack::PopAndDestroy(buf);
2042 CleanupStack::PopAndDestroy(); // close stream
2047 // CEditorCommandSetBaseFormatProto
2048 TInt CEditorCommandSetBaseFormatProto::ExecuteL() const
2050 iTarget.SetBaseFormatL(*iChar, *iPar);
2054 CCommand* CEditorCommandSetBaseFormatProto::CreateInverseL() const
2056 return CEditorCommandSetBaseFormat::NewL(Repositories(),
2060 //////////////////////
2062 // free functions //
2064 //////////////////////
2066 TInt UndoSystem::FindPicture(const MUnifiedEditor& aTarget, TInt aPos, TInt aLength)
2068 const MUnifiedEditor::MPictureSupport* pi =
2069 const_cast<MUnifiedEditor&>(aTarget).PictureSupport();
2071 return KErrNotFound;
2075 aTarget.GetText(aPos, text);
2076 if (aLength < text.Length())
2077 text.Set(text.Ptr(), aLength);
2079 while (0 <= (result = text.Locate(CEditableText::EPictureCharacter)))
2082 pi->Picture(aPos + result, pic);
2083 if (pic.iPictureType != KNullUid)
2084 return aPos + result;
2088 TPtrC temp = text.Mid(result);
2091 aPos += text.Length();
2092 aLength -= text.Length();
2094 return KErrNotFound;