Audio plugin host https://kx.studio/carla
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.

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