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