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.

243 lines
11KB

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