cmdproc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: cmdproc.h
  3. // Purpose: interface of wxCommandProcessor and wxCommand
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxCommand
  9. wxCommand is a base class for modelling an application command, which is an
  10. action usually performed by selecting a menu item, pressing a toolbar
  11. button or any other means provided by the application to change the data or
  12. view.
  13. @library{wxcore}
  14. @category{docview}
  15. @see @ref overview_docview_wxcommand
  16. */
  17. class wxCommand : public wxObject
  18. {
  19. public:
  20. /**
  21. Constructor. wxCommand is an abstract class, so you will need to derive
  22. a new class and call this constructor from your own constructor.
  23. @param canUndo
  24. Tells the command processor whether this command is undo-able. You
  25. can achieve the same functionality by overriding the CanUndo()
  26. member function (if for example the criteria for undoability is
  27. context-dependent).
  28. @param name
  29. Must be supplied for the command processor to display the command
  30. name in the application's edit menu.
  31. */
  32. wxCommand(bool canUndo = false, const wxString& name = wxEmptyString);
  33. /**
  34. Destructor.
  35. */
  36. virtual ~wxCommand();
  37. /**
  38. Returns @true if the command can be undone, @false otherwise.
  39. */
  40. virtual bool CanUndo() const;
  41. /**
  42. Override this member function to execute the appropriate action when
  43. called.
  44. @return @true to indicate that the action has taken place, @false
  45. otherwise. Returning @false will indicate to the command
  46. processor that the action is not undoable and should not be
  47. added to the command history.
  48. */
  49. virtual bool Do() = 0;
  50. /**
  51. Returns the command name.
  52. */
  53. virtual wxString GetName() const;
  54. /**
  55. Override this member function to un-execute a previous Do.
  56. How you implement this command is totally application dependent, but
  57. typical strategies include:
  58. - Perform an inverse operation on the last modified piece of data in
  59. the document. When redone, a copy of data stored in command is pasted
  60. back or some operation reapplied. This relies on the fact that you
  61. know the ordering of Undos; the user can never Undo at an arbitrary
  62. position in the command history.
  63. - Restore the entire document state (perhaps using document
  64. transacting). Potentially very inefficient, but possibly easier to
  65. code if the user interface and data are complex, and an "inverse
  66. execute" operation is hard to write. The docview sample uses the
  67. first method, to remove or restore segments in the drawing.
  68. @return @true to indicate that the action has taken place, @false
  69. otherwise. Returning @false will indicate to the command
  70. processor that the action is not redoable and no change should
  71. be made to the command history.
  72. */
  73. virtual bool Undo() = 0;
  74. };
  75. /**
  76. @class wxCommandProcessor
  77. wxCommandProcessor is a class that maintains a history of wxCommands, with
  78. undo/redo functionality built-in. Derive a new class from this if you want
  79. different behaviour.
  80. @library{wxcore}
  81. @category{docview}
  82. @see @ref overview_docview_wxcommandproc, wxCommand
  83. */
  84. class wxCommandProcessor : public wxObject
  85. {
  86. public:
  87. /**
  88. Constructor.
  89. @param maxCommands
  90. May be set to a positive integer to limit the number of commands
  91. stored to it, otherwise (and by default) the list of commands can
  92. grow arbitrarily.
  93. */
  94. wxCommandProcessor(int maxCommands = -1);
  95. /**
  96. Destructor.
  97. */
  98. virtual ~wxCommandProcessor();
  99. /**
  100. Returns @true if the currently-active command can be undone, @false
  101. otherwise.
  102. */
  103. virtual bool CanUndo() const;
  104. /**
  105. Returns @true if the currently-active command can be redone, @false
  106. otherwise.
  107. */
  108. virtual bool CanRedo() const;
  109. /**
  110. Deletes all commands in the list and sets the current command pointer
  111. to @NULL.
  112. */
  113. virtual void ClearCommands();
  114. /**
  115. Returns the list of commands.
  116. */
  117. wxList& GetCommands();
  118. /**
  119. Returns the current command.
  120. */
  121. wxCommand *GetCurrentCommand() const;
  122. /**
  123. Returns the edit menu associated with the command processor.
  124. */
  125. wxMenu* GetEditMenu() const;
  126. /**
  127. Returns the maximum number of commands that the command processor
  128. stores.
  129. */
  130. int GetMaxCommands() const;
  131. /**
  132. Returns the string that will be appended to the Redo menu item.
  133. */
  134. const wxString& GetRedoAccelerator() const;
  135. /**
  136. Returns the string that will be shown for the redo menu item.
  137. */
  138. wxString GetRedoMenuLabel() const;
  139. /**
  140. Returns the string that will be appended to the Undo menu item.
  141. */
  142. const wxString& GetUndoAccelerator() const;
  143. /**
  144. Returns the string that will be shown for the undo menu item.
  145. */
  146. wxString GetUndoMenuLabel() const;
  147. /**
  148. Initializes the command processor, setting the current command to the
  149. last in the list (if any), and updating the edit menu (if one has been
  150. specified).
  151. */
  152. virtual void Initialize();
  153. /**
  154. Returns a boolean value that indicates if changes have been made since
  155. the last save operation. This only works if MarkAsSaved() is called
  156. whenever the project is saved.
  157. */
  158. virtual bool IsDirty() const;
  159. /**
  160. You must call this method whenever the project is saved if you plan to
  161. use IsDirty().
  162. */
  163. void MarkAsSaved();
  164. /**
  165. Executes (redoes) the current command (the command that has just been
  166. undone if any).
  167. */
  168. virtual bool Redo();
  169. /**
  170. Tells the command processor to update the Undo and Redo items on this
  171. menu as appropriate. Set this to @NULL if the menu is about to be
  172. destroyed and command operations may still be performed, or the command
  173. processor may try to access an invalid pointer.
  174. */
  175. void SetEditMenu(wxMenu* menu);
  176. /**
  177. Sets the menu labels according to the currently set menu and the
  178. current command state.
  179. */
  180. virtual void SetMenuStrings();
  181. /**
  182. Sets the string that will be appended to the Redo menu item.
  183. */
  184. void SetRedoAccelerator(const wxString& accel);
  185. /**
  186. Sets the string that will be appended to the Undo menu item.
  187. */
  188. void SetUndoAccelerator(const wxString& accel);
  189. /**
  190. Submits a new command to the command processor.
  191. The command processor calls wxCommand::Do() to execute the command;
  192. if it succeeds, the command is stored in the history list, and the
  193. associated edit menu (if any) updated appropriately.
  194. If it fails, the command is deleted immediately. Once Submit() has been
  195. called, the passed command should not be deleted directly by the application.
  196. @param command
  197. The command to submit
  198. @param storeIt
  199. Indicates whether the successful command should be stored in the
  200. history list.
  201. */
  202. virtual bool Submit(wxCommand* command, bool storeIt = true);
  203. /**
  204. Just store the command without executing it. The command is stored in the
  205. history list, and the associated edit menu (if any) updated appropriately.
  206. */
  207. virtual void Store(wxCommand *command);
  208. /**
  209. Undoes the last command executed.
  210. */
  211. virtual bool Undo();
  212. };