textrendering/textformatting/undo/UndoSystemImpl.h
changeset 32 8b9155204a54
parent 0 1fb32624e06b
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef UNDOSYSTEMIMPL_H_
       
    20 #define UNDOSYSTEMIMPL_H_
       
    21 
       
    22 #include <e32base.h>
       
    23 #include "UndoSystem.h"
       
    24 
       
    25 namespace UndoSystem
       
    26 {
       
    27 class CCommand;
       
    28 class CSingleCommand;
       
    29 class CBatchCommand;
       
    30 
       
    31 /**
       
    32  * Undo panic codes
       
    33  *
       
    34  * @internalComponent
       
    35  */
       
    36 enum TPanicCode
       
    37 	{
       
    38 	/**
       
    39 	 * CSingleCommand::PrepareToAddInverseToLast() overridden to return ETrue, but
       
    40 	 * CSingleCommand::AddInverseToLast() not overridden.
       
    41 	 */
       
    42 	KAddToLastOnlyHalfImplemented = 1,
       
    43 	/**
       
    44 	 * CCommandStack or CSingleCommandStack::Pop() called on an empty stack
       
    45 	 */
       
    46 	KCommandStackUnderflow,
       
    47 	/**
       
    48 	 * CCommandStack or CSingleCommandStack::Push() called without adequate
       
    49 	 * space being reserved via PrepareToPushL()
       
    50 	 */
       
    51 	KCommandStackPushNotPrepared,
       
    52 	/**
       
    53 	 * No shared undo system passed when expected
       
    54 	 */
       
    55 	KEditorUndoNoCommandManager
       
    56 	};
       
    57 
       
    58 /**
       
    59  * Panic from Undo DLL
       
    60  *
       
    61  * @internalComponent
       
    62  */
       
    63 void Panic(TPanicCode aCode);
       
    64 
       
    65 /**
       
    66  * Stack of commands. A bookmark is maintianed so that we can tell if the stack
       
    67  * is in the same state it was at a previous time.
       
    68  *
       
    69  * @internalComponent
       
    70  * @since App-frameworks6.1
       
    71  */
       
    72 NONSHARABLE_CLASS(CCommandStack) : public CBase
       
    73 	{
       
    74 public:
       
    75 	static CCommandStack* NewL();
       
    76 	~CCommandStack();
       
    77 	CCommandStack();
       
    78 	void ConstructL();
       
    79 
       
    80 	CCommand* Top() const;
       
    81 	CCommand* Pop();
       
    82 	/**
       
    83 	 * Allows aNumberOfItems Push()s to be done before the next
       
    84 	 * call to another non-const member function apart from Push().
       
    85 	 * aNumberOfItems must be non-negative.
       
    86 	 */
       
    87 	void PrepareToPushL(TInt aNumberOfItems);
       
    88 	void Push(CCommand* aCommand);
       
    89 	void PruneTo(TInt aNumberOfItems);
       
    90 	TInt Count() const;
       
    91 	/**
       
    92 	 * Removes all elements from the stack and deletes the bookmark.
       
    93 	 */
       
    94 	void Reset();
       
    95 	/**
       
    96 	 * Adds aStack to the top of this CCommandStack, emptying it completely.
       
    97 	 * Enough items must have been reserved with PrepareToPushL(TInt).
       
    98 	 */
       
    99 	void Concatenate(CCommandStack& aStack);
       
   100 	/**
       
   101 	 * Sets the current position in the stack as the location of the bookmark.
       
   102 	 * The bookmark will be lost if the bookmarked object is removed from the
       
   103 	 * stack. Bookmark begins set at the zero position.
       
   104 	 */
       
   105 	void SetBookmark();
       
   106 	/**
       
   107 	 * Returns true iff we are at the current bookmark position.
       
   108 	 */
       
   109 	TBool IsAtBookmark() const;
       
   110 
       
   111 private:
       
   112 	CArrayFix<CCommand*>* iStack;
       
   113 	/**
       
   114 	 * One-past-top element in the array. Less than Count() after a call
       
   115 	 * to PrepareToPushL() with non-zero argument.
       
   116 	 */
       
   117 	TInt iEnd;
       
   118 	/**
       
   119 	 * The bookmark into the stack. We are at the bookmark when iEnd ==
       
   120 	 * iBookmark. No bookmark is represented by iBookmark < 0.
       
   121 	 */
       
   122 	TInt iBookmark;
       
   123 	};
       
   124 
       
   125 /**
       
   126  * Stack of single commands
       
   127  *
       
   128  * @internalComponent
       
   129  * @since App-frameworks6.1
       
   130  */
       
   131 class CSingleCommandStack : public CBase
       
   132 	{
       
   133 public:
       
   134 	static CSingleCommandStack* NewL();
       
   135 
       
   136 	inline CSingleCommand* Top() const;
       
   137 	inline CSingleCommand* Pop();
       
   138 	inline void PrepareToPushL(TInt aNumberOfItems);
       
   139 	inline void Push(CSingleCommand* aCommand);
       
   140 	inline void PruneTo(TInt aNumberOfItems);
       
   141 	inline TInt Count() const;
       
   142 	inline void Reset();
       
   143 	inline void Concatenate(CSingleCommandStack& aStack);
       
   144 
       
   145 private:
       
   146 	CSingleCommandStack() {}
       
   147 
       
   148 	CCommandStack iStack;
       
   149 	};
       
   150 
       
   151 /**
       
   152  * A stack of commands supporting batching and a flag for whether a batch
       
   153  * has had its undo option waived. A bookmark is also supported, so that we can
       
   154  * tell whether the history is in the same state as it was at a previous time.
       
   155  *
       
   156  * @internalComponent
       
   157  * @since App-frameworks6.1
       
   158  */
       
   159 NONSHARABLE_CLASS(CCommandHistory) : public CBase
       
   160 	{
       
   161 public:
       
   162 	~CCommandHistory();
       
   163 	static CCommandHistory* NewL();
       
   164 
       
   165 	/**
       
   166 	 * Returns the command at the top of the stack.
       
   167 	 */
       
   168 	CCommand* Top() const;
       
   169 	/**
       
   170 	 * Returns the single command at the top of the stack.
       
   171 	 */
       
   172 	CSingleCommand* TopSingleCommand() const;
       
   173 	/**
       
   174 	 * Allocates enough resources for one call to AddCommand(CCommand* aCommand).
       
   175 	 */
       
   176 	void PrepareToAddCommandL(CCommand* aCommand);
       
   177 	/**
       
   178 	 * Adds the command to the top of the stack. PrepareToAddCommandL() must
       
   179 	 * have been called successfully since the last call to AddCommand or NewL.
       
   180 	 */
       
   181 	void AddCommand(CCommand*);
       
   182 	/**
       
   183 	 * Returns ETrue iff SetUndoWaived() has been called during this batch. If
       
   184 	 * there is no batch currently open, this function will always return
       
   185 	 * EFalse.
       
   186 	 */
       
   187 	TBool UndoHasBeenWaived() const;
       
   188 	/**
       
   189 	 * Sets a flag to indicated that undo is not required for this current
       
   190 	 * batch. This function has no effect if there is no batch currently open.
       
   191 	 *
       
   192 	 * @see UndoHasBeenWaived
       
   193 	 */
       
   194 	void SetUndoWaived();
       
   195 	/**
       
   196 	 * Sets the maximum height of the stack to aMaxItems. If it is exceeded,
       
   197 	 * the stack is truncated to aPrunedItems.
       
   198 	 */
       
   199 	void SetMaxItems(TInt aMaxItems);
       
   200 	/**
       
   201 	 * Returns ETrue iff the stack has no elements.
       
   202 	 */
       
   203 	TBool IsEmpty() const { return Top()? EFalse : ETrue; }
       
   204 	/**
       
   205 	 * Begins a new batch. All commands added subsequently will be exeuted all
       
   206 	 * at once. Close the batch with CleanupStack::PopAndDestroy();
       
   207 	 */
       
   208 	void BeginBatchLC();
       
   209 	/**
       
   210 	 * Returns ETrue iff there is a batch currently open.
       
   211 	 */
       
   212 	TBool IsWithinBatch() const;
       
   213 	/**
       
   214 	 * Removes and returns the top element. There must be no batch currently
       
   215 	 * open before a call to Pop().
       
   216 	 */
       
   217 	CCommand* Pop();
       
   218 	/**
       
   219 	 * Removes all elements from the stack. If a batch is currently open, any
       
   220 	 * commands added subsequently will not be added to it. Deletes the
       
   221 	 * bookmark.
       
   222 	 */
       
   223 	void Reset();
       
   224 	/**
       
   225 	 * Removes and destroys the top element iff it is an empty batch. Must not
       
   226 	 * be called if a batch is currently open.
       
   227 	 */
       
   228 	void Clean();
       
   229 	/**
       
   230 	 * Sets the bookmark to the current position.
       
   231 	 */
       
   232 	void SetBookmark();
       
   233 	/**
       
   234 	 * Returns true iff the bookmark is at the current position.
       
   235 	 */
       
   236 	TBool IsAtBookmark();
       
   237 
       
   238 private:
       
   239 
       
   240 	CCommandHistory();
       
   241 	void ConstructL();
       
   242 	void Prune();
       
   243 
       
   244 	static void CloseBatch(void* aCCommandHistory);
       
   245 	static void DownBatchLevel(void* aCCommandHistory);
       
   246 
       
   247 	CCommandStack* iStack;
       
   248 	CBatchCommand* iCurrent;	// either == 0 or points to the most
       
   249 								// recent CBatchCommand within iStack
       
   250 
       
   251 	TInt iMaxItems;
       
   252 
       
   253 	TBool iBatchUndoHasBeenWaived;
       
   254 	};
       
   255 
       
   256 //
       
   257 // CSingleCommandStack inlines
       
   258 //
       
   259 CSingleCommand* CSingleCommandStack::Top() const
       
   260 	{ return static_cast<CSingleCommand*>(iStack.Top()); }
       
   261 
       
   262 CSingleCommand* CSingleCommandStack::Pop()
       
   263 	{ return static_cast<CSingleCommand*>(iStack.Pop()); }
       
   264 
       
   265 void CSingleCommandStack::PrepareToPushL(TInt aNumberOfItems)
       
   266 	{ iStack.PrepareToPushL(aNumberOfItems); }
       
   267 
       
   268 void CSingleCommandStack::Push(CSingleCommand* aCommand)
       
   269 	{ iStack.Push(aCommand); }
       
   270 
       
   271 void CSingleCommandStack::PruneTo(TInt aNumberOfItems)
       
   272 	{ iStack.PruneTo(aNumberOfItems); }
       
   273 
       
   274 TInt CSingleCommandStack::Count() const
       
   275 	{ return iStack.Count(); }
       
   276 
       
   277 void CSingleCommandStack::Reset()
       
   278 	{ iStack.Reset(); }
       
   279 
       
   280 void CSingleCommandStack::Concatenate(CSingleCommandStack& aStack)
       
   281 	{ iStack.Concatenate(aStack.iStack); }
       
   282 
       
   283 }
       
   284 
       
   285 #endif
       
   286