juce_UndoableAction.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Used by the UndoManager class to store an action which can be done
  24. and undone.
  25. @see UndoManager
  26. @tags{DataStructures}
  27. */
  28. class JUCE_API UndoableAction
  29. {
  30. protected:
  31. /** Creates an action. */
  32. UndoableAction() = default;
  33. public:
  34. /** Destructor. */
  35. virtual ~UndoableAction() = default;
  36. //==============================================================================
  37. /** Overridden by a subclass to perform the action.
  38. This method is called by the UndoManager, and shouldn't be used directly by
  39. applications.
  40. Be careful not to make any calls in a perform() method that could call
  41. recursively back into the UndoManager::perform() method
  42. @returns true if the action could be performed.
  43. @see UndoManager::perform
  44. */
  45. virtual bool perform() = 0;
  46. /** Overridden by a subclass to undo the action.
  47. This method is called by the UndoManager, and shouldn't be used directly by
  48. applications.
  49. Be careful not to make any calls in an undo() method that could call
  50. recursively back into the UndoManager::perform() method
  51. @returns true if the action could be undone without any errors.
  52. @see UndoManager::perform
  53. */
  54. virtual bool undo() = 0;
  55. //==============================================================================
  56. /** Returns a value to indicate how much memory this object takes up.
  57. Because the UndoManager keeps a list of UndoableActions, this is used
  58. to work out how much space each one will take up, so that the UndoManager
  59. can work out how many to keep.
  60. The default value returned here is 10 - units are arbitrary and
  61. don't have to be accurate.
  62. @see UndoManager::getNumberOfUnitsTakenUpByStoredCommands,
  63. UndoManager::setMaxNumberOfStoredUnits
  64. */
  65. virtual int getSizeInUnits() { return 10; }
  66. /** Allows multiple actions to be coalesced into a single action object, to reduce storage space.
  67. If possible, this method should create and return a single action that does the same job as
  68. this one followed by the supplied action.
  69. If it's not possible to merge the two actions, the method should return a nullptr.
  70. */
  71. virtual UndoableAction* createCoalescedAction (UndoableAction* nextAction) { ignoreUnused (nextAction); return nullptr; }
  72. };
  73. } // namespace juce