juce_UndoManager.h 11 KB

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