1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: undo/redo capable command processing framework
4 // Author: Julian Smart (extracted from docview.h by VZ)
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_CMDPROC_H_
13 #define _WX_CMDPROC_H_
16 #include "wx/object.h"
19 class WXDLLIMPEXP_FWD_CORE wxMenu
;
21 // ----------------------------------------------------------------------------
22 // wxCommand: a single command capable of performing itself
23 // ----------------------------------------------------------------------------
25 class WXDLLIMPEXP_CORE wxCommand
: public wxObject
28 wxCommand(bool canUndoIt
= false, const wxString
& name
= wxEmptyString
);
29 virtual ~wxCommand(){}
31 // Override this to perform a command
32 virtual bool Do() = 0;
34 // Override this to undo a command
35 virtual bool Undo() = 0;
37 virtual bool CanUndo() const { return m_canUndo
; }
38 virtual wxString
GetName() const { return m_commandName
; }
42 wxString m_commandName
;
45 DECLARE_CLASS(wxCommand
)
48 // ----------------------------------------------------------------------------
49 // wxCommandProcessor: wxCommand manager
50 // ----------------------------------------------------------------------------
52 class WXDLLIMPEXP_CORE wxCommandProcessor
: public wxObject
55 // if max number of commands is -1, it is unlimited
56 wxCommandProcessor(int maxCommands
= -1);
57 virtual ~wxCommandProcessor();
59 // Pass a command to the processor. The processor calls Do(); if
60 // successful, is appended to the command history unless storeIt is false.
61 virtual bool Submit(wxCommand
*command
, bool storeIt
= true);
63 // just store the command without executing it
64 virtual void Store(wxCommand
*command
);
68 virtual bool CanUndo() const;
69 virtual bool CanRedo() const;
71 // Initialises the current command and menu strings.
72 virtual void Initialize();
74 // Sets the Undo/Redo menu strings for the current menu.
75 virtual void SetMenuStrings();
77 // Gets the current Undo menu label.
78 wxString
GetUndoMenuLabel() const;
80 // Gets the current Undo menu label.
81 wxString
GetRedoMenuLabel() const;
84 // Call this to manage an edit menu.
85 void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
86 wxMenu
*GetEditMenu() const { return m_commandEditMenu
; }
89 // command list access
90 wxList
& GetCommands() { return m_commands
; }
91 const wxList
& GetCommands() const { return m_commands
; }
92 wxCommand
*GetCurrentCommand() const
94 return (wxCommand
*)(m_currentCommand
? m_currentCommand
->GetData() : NULL
);
96 int GetMaxCommands() const { return m_maxNoCommands
; }
97 virtual void ClearCommands();
99 // Has the current project been changed?
100 virtual bool IsDirty() const
102 return m_currentCommand
&& (m_lastSavedCommand
!= m_currentCommand
);
105 // Mark the current command as the one where the last save took place
108 m_lastSavedCommand
= m_currentCommand
;
112 // By default, the accelerators are "\tCtrl+Z" and "\tCtrl+Y"
113 const wxString
& GetUndoAccelerator() const { return m_undoAccelerator
; }
114 const wxString
& GetRedoAccelerator() const { return m_redoAccelerator
; }
116 void SetUndoAccelerator(const wxString
& accel
) { m_undoAccelerator
= accel
; }
117 void SetRedoAccelerator(const wxString
& accel
) { m_redoAccelerator
= accel
; }
120 // for further flexibility, command processor doesn't call wxCommand::Do()
121 // and Undo() directly but uses these functions which can be overridden in
123 virtual bool DoCommand(wxCommand
& cmd
);
124 virtual bool UndoCommand(wxCommand
& cmd
);
128 wxList::compatibility_iterator m_currentCommand
,
132 wxMenu
* m_commandEditMenu
;
133 #endif // wxUSE_MENUS
135 wxString m_undoAccelerator
;
136 wxString m_redoAccelerator
;
139 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
140 wxDECLARE_NO_COPY_CLASS(wxCommandProcessor
);
143 #endif // _WX_CMDPROC_H_