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.

245 lines
11KB

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