The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
9.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_UNDOMANAGER_JUCEHEADER__
  19. #define __JUCE_UNDOMANAGER_JUCEHEADER__
  20. #include "juce_UndoableAction.h"
  21. //==============================================================================
  22. /**
  23. Manages a list of undo/redo commands.
  24. An UndoManager object keeps a list of past actions and can use these actions
  25. to move backwards and forwards through an undo history.
  26. To use it, create subclasses of UndoableAction which perform all the
  27. actions you need, then when you need to actually perform an action, create one
  28. and pass it to the UndoManager's perform() method.
  29. The manager also uses the concept of 'transactions' to group the actions
  30. together - all actions performed between calls to beginNewTransaction() are
  31. grouped together and are all undone/redone as a group.
  32. The UndoManager is a ChangeBroadcaster, so listeners can register to be told
  33. when actions are performed or undone.
  34. @see UndoableAction
  35. */
  36. class JUCE_API UndoManager : public ChangeBroadcaster
  37. {
  38. public:
  39. //==============================================================================
  40. /** Creates an UndoManager.
  41. @param maxNumberOfUnitsToKeep each UndoableAction object returns a value
  42. to indicate how much storage it takes up
  43. (UndoableAction::getSizeInUnits()), so this
  44. lets you specify the maximum total number of
  45. units that the undomanager is allowed to
  46. keep in memory before letting the older actions
  47. drop off the end of the list.
  48. @param minimumTransactionsToKeep this specifies the minimum number of transactions
  49. that will be kept, even if this involves exceeding
  50. the amount of space specified in maxNumberOfUnitsToKeep
  51. */
  52. UndoManager (int maxNumberOfUnitsToKeep = 30000,
  53. int minimumTransactionsToKeep = 30);
  54. /** Destructor. */
  55. ~UndoManager();
  56. //==============================================================================
  57. /** Deletes all stored actions in the list. */
  58. void clearUndoHistory();
  59. /** Returns the current amount of space to use for storing UndoableAction objects.
  60. @see setMaxNumberOfStoredUnits
  61. */
  62. int getNumberOfUnitsTakenUpByStoredCommands() const;
  63. /** Sets the amount of space that can be used for storing UndoableAction objects.
  64. @param maxNumberOfUnitsToKeep each UndoableAction object returns a value
  65. to indicate how much storage it takes up
  66. (UndoableAction::getSizeInUnits()), so this
  67. lets you specify the maximum total number of
  68. units that the undomanager is allowed to
  69. keep in memory before letting the older actions
  70. drop off the end of the list.
  71. @param minimumTransactionsToKeep this specifies the minimum number of transactions
  72. that will be kept, even if this involves exceeding
  73. the amount of space specified in maxNumberOfUnitsToKeep
  74. @see getNumberOfUnitsTakenUpByStoredCommands
  75. */
  76. void setMaxNumberOfStoredUnits (int maxNumberOfUnitsToKeep,
  77. int minimumTransactionsToKeep);
  78. //==============================================================================
  79. /** Performs an action and adds it to the undo history list.
  80. @param action the action to perform - this will be deleted by the UndoManager
  81. when no longer needed
  82. @param actionName if this string is non-empty, the current transaction will be
  83. given this name; if it's empty, the current transaction name will
  84. be left unchanged. See setCurrentTransactionName()
  85. @returns true if the command succeeds - see UndoableAction::perform
  86. @see beginNewTransaction
  87. */
  88. bool perform (UndoableAction* action,
  89. const String& actionName = String::empty);
  90. /** Starts a new group of actions that together will be treated as a single transaction.
  91. All actions that are passed to the perform() method between calls to this
  92. method are grouped together and undone/redone together by a single call to
  93. undo() or redo().
  94. @param actionName a description of the transaction that is about to be
  95. performed
  96. */
  97. void beginNewTransaction (const String& actionName = String::empty);
  98. /** Changes the name stored for the current transaction.
  99. Each transaction is given a name when the beginNewTransaction() method is
  100. called, but this can be used to change that name without starting a new
  101. transaction.
  102. */
  103. void setCurrentTransactionName (const String& newName);
  104. //==============================================================================
  105. /** Returns true if there's at least one action in the list to undo.
  106. @see getUndoDescription, undo, canRedo
  107. */
  108. bool canUndo() const;
  109. /** Returns the description of the transaction that would be next to get undone.
  110. The description returned is the one that was passed into beginNewTransaction
  111. before the set of actions was performed.
  112. @see undo
  113. */
  114. String getUndoDescription() const;
  115. /** Tries to roll-back the last transaction.
  116. @returns true if the transaction can be undone, and false if it fails, or
  117. if there aren't any transactions to undo
  118. */
  119. bool undo();
  120. /** Tries to roll-back any actions that were added to the current transaction.
  121. This will perform an undo() only if there are some actions in the undo list
  122. that were added after the last call to beginNewTransaction().
  123. This is useful because it lets you call beginNewTransaction(), then
  124. perform an operation which may or may not actually perform some actions, and
  125. then call this method to get rid of any actions that might have been done
  126. without it rolling back the previous transaction if nothing was actually
  127. done.
  128. @returns true if any actions were undone.
  129. */
  130. bool undoCurrentTransactionOnly();
  131. /** Returns a list of the UndoableAction objects that have been performed during the
  132. transaction that is currently open.
  133. Effectively, this is the list of actions that would be undone if undoCurrentTransactionOnly()
  134. were to be called now.
  135. The first item in the list is the earliest action performed.
  136. */
  137. void getActionsInCurrentTransaction (Array <const UndoableAction*>& actionsFound) const;
  138. /** Returns the number of UndoableAction objects that have been performed during the
  139. transaction that is currently open.
  140. @see getActionsInCurrentTransaction
  141. */
  142. int getNumActionsInCurrentTransaction() const;
  143. //==============================================================================
  144. /** Returns true if there's at least one action in the list to redo.
  145. @see getRedoDescription, redo, canUndo
  146. */
  147. bool canRedo() const;
  148. /** Returns the description of the transaction that would be next to get redone.
  149. The description returned is the one that was passed into beginNewTransaction
  150. before the set of actions was performed.
  151. @see redo
  152. */
  153. String getRedoDescription() const;
  154. /** Tries to redo the last transaction that was undone.
  155. @returns true if the transaction can be redone, and false if it fails, or
  156. if there aren't any transactions to redo
  157. */
  158. bool redo();
  159. private:
  160. //==============================================================================
  161. OwnedArray <OwnedArray <UndoableAction> > transactions;
  162. StringArray transactionNames;
  163. String currentTransactionName;
  164. int totalUnitsStored, maxNumUnitsToKeep, minimumTransactionsToKeep, nextIndex;
  165. bool newTransaction, reentrancyCheck;
  166. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UndoManager);
  167. };
  168. #endif // __JUCE_UNDOMANAGER_JUCEHEADER__