]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdproc.h
a74a3df5bf5eadc1178b100d5459d0dab07a82cf
[wxWidgets.git] / include / wx / cmdproc.h
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 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_CMDPROC_H_
13 #define _WX_CMDPROC_H_
14
15 #include "wx/defs.h"
16 #include "wx/object.h"
17 #include "wx/list.h"
18
19 class WXDLLEXPORT wxMenu;
20
21 // ----------------------------------------------------------------------------
22 // wxCommand: a single command capable of performing itself
23 // ----------------------------------------------------------------------------
24
25 class WXDLLEXPORT wxCommand : public wxObject
26 {
27 public:
28 wxCommand(bool canUndoIt = false, const wxString& name = wxEmptyString);
29 ~wxCommand(){}
30
31 // Override this to perform a command
32 virtual bool Do() = 0;
33
34 // Override this to undo a command
35 virtual bool Undo() = 0;
36
37 virtual bool CanUndo() const { return m_canUndo; }
38 virtual wxString GetName() const { return m_commandName; }
39
40 protected:
41 bool m_canUndo;
42 wxString m_commandName;
43
44 private:
45 DECLARE_CLASS(wxCommand)
46 };
47
48 // ----------------------------------------------------------------------------
49 // wxCommandProcessor: wxCommand manager
50 // ----------------------------------------------------------------------------
51
52 class WXDLLEXPORT wxCommandProcessor : public wxObject
53 {
54 public:
55 // if max number of commands is -1, it is unlimited
56 wxCommandProcessor(int maxCommands = -1);
57 virtual ~wxCommandProcessor();
58
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);
62
63 // just store the command without executing it
64 virtual void Store(wxCommand *command);
65
66 virtual bool Undo();
67 virtual bool Redo();
68 virtual bool CanUndo() const;
69 virtual bool CanRedo() const;
70
71 // Initialises the current command and menu strings.
72 virtual void Initialize();
73
74 // Sets the Undo/Redo menu strings for the current menu.
75 virtual void SetMenuStrings();
76
77 // Gets the current Undo menu label.
78 wxString GetUndoMenuLabel() const;
79
80 // Gets the current Undo menu label.
81 wxString GetRedoMenuLabel() const;
82
83 #if wxUSE_MENUS
84 // Call this to manage an edit menu.
85 void SetEditMenu(wxMenu *menu) { m_commandEditMenu = menu; }
86 wxMenu *GetEditMenu() const { return m_commandEditMenu; }
87 #endif // wxUSE_MENUS
88
89 // command list access
90 wxList& GetCommands() const { return (wxList&) m_commands; }
91 wxCommand *GetCurrentCommand() const
92 {
93 return (wxCommand *)(m_currentCommand ? m_currentCommand->GetData() : NULL);
94 }
95 int GetMaxCommands() const { return m_maxNoCommands; }
96 virtual void ClearCommands();
97
98 // Has the current project been changed?
99 virtual bool IsDirty() const
100 {
101 return m_currentCommand && (m_lastSavedCommand != m_currentCommand);
102 }
103
104 // Mark the current command as the one where the last save took place
105 void MarkAsSaved()
106 {
107 m_lastSavedCommand = m_currentCommand;
108 }
109
110
111 // By default, the accelerators are "\tCtrl+Z" and "\tCtrl+Y"
112 const wxString& GetUndoAccelerator() const { return m_undoAccelerator; }
113 const wxString& GetRedoAccelerator() const { return m_redoAccelerator; }
114
115 void SetUndoAccelerator(const wxString& accel) { m_undoAccelerator = accel; }
116 void SetRedoAccelerator(const wxString& accel) { m_redoAccelerator = accel; }
117
118 protected:
119 // for further flexibility, command processor doesn't call wxCommand::Do()
120 // and Undo() directly but uses these functions which can be overridden in
121 // the derived class
122 virtual bool DoCommand(wxCommand& cmd);
123 virtual bool UndoCommand(wxCommand& cmd);
124
125 int m_maxNoCommands;
126 wxList m_commands;
127 wxList::compatibility_iterator m_currentCommand,
128 m_lastSavedCommand;
129
130 #if wxUSE_MENUS
131 wxMenu* m_commandEditMenu;
132 #endif // wxUSE_MENUS
133
134 wxString m_undoAccelerator;
135 wxString m_redoAccelerator;
136
137 private:
138 DECLARE_DYNAMIC_CLASS(wxCommandProcessor)
139 DECLARE_NO_COPY_CLASS(wxCommandProcessor)
140 };
141
142 #endif // _WX_CMDPROC_H_
143