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.

265 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  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. @tags{DataStructures}
  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() override;
  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 object will be deleted by
  81. the UndoManager when no longer needed
  82. @returns true if the command succeeds - see UndoableAction::perform
  83. @see beginNewTransaction
  84. */
  85. bool perform (UndoableAction* action);
  86. /** Performs an action and also gives it a name.
  87. @param action the action to perform - this object will be deleted by
  88. the UndoManager when no longer needed
  89. @param actionName if this string is non-empty, the current transaction will be
  90. given this name; if it's empty, the current transaction name will
  91. be left unchanged. See setCurrentTransactionName()
  92. @returns true if the command succeeds - see UndoableAction::perform
  93. @see beginNewTransaction
  94. */
  95. bool perform (UndoableAction* action, const String& actionName);
  96. /** Starts a new group of actions that together will be treated as a single transaction.
  97. All actions that are passed to the perform() method between calls to this
  98. method are grouped together and undone/redone together by a single call to
  99. undo() or redo().
  100. */
  101. void beginNewTransaction();
  102. /** Starts a new group of actions that together will be treated as a single transaction.
  103. All actions that are passed to the perform() method between calls to this
  104. method are grouped together and undone/redone together by a single call to
  105. undo() or redo().
  106. @param actionName a description of the transaction that is about to be
  107. performed
  108. */
  109. void beginNewTransaction (const String& actionName);
  110. /** Changes the name stored for the current transaction.
  111. Each transaction is given a name when the beginNewTransaction() method is
  112. called, but this can be used to change that name without starting a new
  113. transaction.
  114. */
  115. void setCurrentTransactionName (const String& newName);
  116. /** Returns the name of the current transaction.
  117. @see setCurrentTransactionName
  118. */
  119. String getCurrentTransactionName() const;
  120. //==============================================================================
  121. /** Returns true if there's at least one action in the list to undo.
  122. @see getUndoDescription, undo, canRedo
  123. */
  124. bool canUndo() const;
  125. /** Tries to roll-back the last transaction.
  126. @returns true if the transaction can be undone, and false if it fails, or
  127. if there aren't any transactions to undo
  128. @see undoCurrentTransactionOnly
  129. */
  130. bool undo();
  131. /** Tries to roll-back any actions that were added to the current transaction.
  132. This will perform an undo() only if there are some actions in the undo list
  133. that were added after the last call to beginNewTransaction().
  134. This is useful because it lets you call beginNewTransaction(), then
  135. perform an operation which may or may not actually perform some actions, and
  136. then call this method to get rid of any actions that might have been done
  137. without it rolling back the previous transaction if nothing was actually
  138. done.
  139. @returns true if any actions were undone.
  140. */
  141. bool undoCurrentTransactionOnly();
  142. /** Returns the name of the transaction that will be rolled-back when undo() is called.
  143. @see undo, canUndo, getUndoDescriptions
  144. */
  145. String getUndoDescription() const;
  146. /** Returns the names of the sequence of transactions that will be performed if undo()
  147. is repeatedly called. Note that for transactions where no name was provided, the
  148. corresponding string will be empty.
  149. @see undo, canUndo, getUndoDescription
  150. */
  151. StringArray getUndoDescriptions() const;
  152. /** Returns the time to which the state would be restored if undo() was to be called.
  153. If an undo isn't currently possible, it'll return Time().
  154. */
  155. Time getTimeOfUndoTransaction() const;
  156. /** Returns a list of the UndoableAction objects that have been performed during the
  157. transaction that is currently open.
  158. Effectively, this is the list of actions that would be undone if undoCurrentTransactionOnly()
  159. were to be called now.
  160. The first item in the list is the earliest action performed.
  161. */
  162. void getActionsInCurrentTransaction (Array<const UndoableAction*>& actionsFound) const;
  163. /** Returns the number of UndoableAction objects that have been performed during the
  164. transaction that is currently open.
  165. @see getActionsInCurrentTransaction
  166. */
  167. int getNumActionsInCurrentTransaction() const;
  168. //==============================================================================
  169. /** Returns true if there's at least one action in the list to redo.
  170. @see getRedoDescription, redo, canUndo
  171. */
  172. bool canRedo() const;
  173. /** Tries to redo the last transaction that was undone.
  174. @returns true if the transaction can be redone, and false if it fails, or
  175. if there aren't any transactions to redo
  176. */
  177. bool redo();
  178. /** Returns the name of the transaction that will be redone when redo() is called.
  179. @see redo, canRedo, getRedoDescriptions
  180. */
  181. String getRedoDescription() const;
  182. /** Returns the names of the sequence of transactions that will be performed if redo()
  183. is repeatedly called. Note that for transactions where no name was provided, the
  184. corresponding string will be empty.
  185. @see redo, canRedo, getRedoDescription
  186. */
  187. StringArray getRedoDescriptions() const;
  188. /** Returns the time to which the state would be restored if redo() was to be called.
  189. If a redo isn't currently possible, it'll return Time::getCurrentTime().
  190. @see redo, canRedo
  191. */
  192. Time getTimeOfRedoTransaction() const;
  193. /** Returns true if the caller code is in the middle of an undo or redo action. */
  194. bool isPerformingUndoRedo() const;
  195. private:
  196. //==============================================================================
  197. struct ActionSet;
  198. OwnedArray<ActionSet> transactions, stashedFutureTransactions;
  199. String newTransactionName;
  200. int totalUnitsStored = 0, maxNumUnitsToKeep = 0, minimumTransactionsToKeep = 0, nextIndex = 0;
  201. bool newTransaction = true, isInsideUndoRedoCall = false;
  202. ActionSet* getCurrentSet() const;
  203. ActionSet* getNextSet() const;
  204. void moveFutureTransactionsToStash();
  205. void restoreStashedFutureTransactions();
  206. void dropOldTransactionsIfTooLarge();
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UndoManager)
  208. };
  209. } // namespace juce