cmdproc.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cmdproc.h
  3. // Purpose: undo/redo capable command processing framework
  4. // Author: Julian Smart (extracted from docview.h by VZ)
  5. // Modified by:
  6. // Created: 05.11.00
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CMDPROC_H_
  11. #define _WX_CMDPROC_H_
  12. #include "wx/defs.h"
  13. #include "wx/object.h"
  14. #include "wx/list.h"
  15. class WXDLLIMPEXP_FWD_CORE wxMenu;
  16. // ----------------------------------------------------------------------------
  17. // wxCommand: a single command capable of performing itself
  18. // ----------------------------------------------------------------------------
  19. class WXDLLIMPEXP_CORE wxCommand : public wxObject
  20. {
  21. public:
  22. wxCommand(bool canUndoIt = false, const wxString& name = wxEmptyString);
  23. virtual ~wxCommand(){}
  24. // Override this to perform a command
  25. virtual bool Do() = 0;
  26. // Override this to undo a command
  27. virtual bool Undo() = 0;
  28. virtual bool CanUndo() const { return m_canUndo; }
  29. virtual wxString GetName() const { return m_commandName; }
  30. protected:
  31. bool m_canUndo;
  32. wxString m_commandName;
  33. private:
  34. DECLARE_CLASS(wxCommand)
  35. };
  36. // ----------------------------------------------------------------------------
  37. // wxCommandProcessor: wxCommand manager
  38. // ----------------------------------------------------------------------------
  39. class WXDLLIMPEXP_CORE wxCommandProcessor : public wxObject
  40. {
  41. public:
  42. // if max number of commands is -1, it is unlimited
  43. wxCommandProcessor(int maxCommands = -1);
  44. virtual ~wxCommandProcessor();
  45. // Pass a command to the processor. The processor calls Do(); if
  46. // successful, is appended to the command history unless storeIt is false.
  47. virtual bool Submit(wxCommand *command, bool storeIt = true);
  48. // just store the command without executing it
  49. virtual void Store(wxCommand *command);
  50. virtual bool Undo();
  51. virtual bool Redo();
  52. virtual bool CanUndo() const;
  53. virtual bool CanRedo() const;
  54. // Initialises the current command and menu strings.
  55. virtual void Initialize();
  56. // Sets the Undo/Redo menu strings for the current menu.
  57. virtual void SetMenuStrings();
  58. // Gets the current Undo menu label.
  59. wxString GetUndoMenuLabel() const;
  60. // Gets the current Undo menu label.
  61. wxString GetRedoMenuLabel() const;
  62. #if wxUSE_MENUS
  63. // Call this to manage an edit menu.
  64. void SetEditMenu(wxMenu *menu) { m_commandEditMenu = menu; }
  65. wxMenu *GetEditMenu() const { return m_commandEditMenu; }
  66. #endif // wxUSE_MENUS
  67. // command list access
  68. wxList& GetCommands() { return m_commands; }
  69. const wxList& GetCommands() const { return m_commands; }
  70. wxCommand *GetCurrentCommand() const
  71. {
  72. return (wxCommand *)(m_currentCommand ? m_currentCommand->GetData() : NULL);
  73. }
  74. int GetMaxCommands() const { return m_maxNoCommands; }
  75. virtual void ClearCommands();
  76. // Has the current project been changed?
  77. virtual bool IsDirty() const;
  78. // Mark the current command as the one where the last save took place
  79. void MarkAsSaved()
  80. {
  81. m_lastSavedCommand = m_currentCommand;
  82. }
  83. // By default, the accelerators are "\tCtrl+Z" and "\tCtrl+Y"
  84. const wxString& GetUndoAccelerator() const { return m_undoAccelerator; }
  85. const wxString& GetRedoAccelerator() const { return m_redoAccelerator; }
  86. void SetUndoAccelerator(const wxString& accel) { m_undoAccelerator = accel; }
  87. void SetRedoAccelerator(const wxString& accel) { m_redoAccelerator = accel; }
  88. protected:
  89. // for further flexibility, command processor doesn't call wxCommand::Do()
  90. // and Undo() directly but uses these functions which can be overridden in
  91. // the derived class
  92. virtual bool DoCommand(wxCommand& cmd);
  93. virtual bool UndoCommand(wxCommand& cmd);
  94. int m_maxNoCommands;
  95. wxList m_commands;
  96. wxList::compatibility_iterator m_currentCommand,
  97. m_lastSavedCommand;
  98. #if wxUSE_MENUS
  99. wxMenu* m_commandEditMenu;
  100. #endif // wxUSE_MENUS
  101. wxString m_undoAccelerator;
  102. wxString m_redoAccelerator;
  103. private:
  104. DECLARE_DYNAMIC_CLASS(wxCommandProcessor)
  105. wxDECLARE_NO_COPY_CLASS(wxCommandProcessor);
  106. };
  107. #endif // _WX_CMDPROC_H_