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.

233 lines
10KB

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