]>
Commit | Line | Data |
---|---|---|
70050c82 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/cmdproc.cpp | |
3 | // Purpose: wxCommand and wxCommandProcessor classes | |
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) wxWindows team | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "cmdproc.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/intl.h" | |
33 | #include "wx/string.h" | |
34 | #include "wx/menu.h" | |
35 | #endif //WX_PRECOMP | |
36 | ||
37 | #include "wx/cmdproc.h" | |
38 | ||
39 | // ============================================================================ | |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
43 | IMPLEMENT_CLASS(wxCommand, wxObject) | |
44 | IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor, wxObject) | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // wxCommand | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | wxCommand::wxCommand(bool canUndoIt, const wxString& name) | |
51 | { | |
52 | m_canUndo = canUndoIt; | |
53 | m_commandName = name; | |
54 | } | |
55 | ||
56 | wxCommand::~wxCommand() | |
57 | { | |
58 | } | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // Command processor | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | wxCommandProcessor::wxCommandProcessor(int maxCommands) | |
65 | { | |
66 | m_maxNoCommands = maxCommands; | |
70050c82 VZ |
67 | #if wxUSE_MENUS |
68 | m_commandEditMenu = (wxMenu *) NULL; | |
69 | #endif // wxUSE_MENUS | |
6aa3ea88 JS |
70 | m_undoAccelerator = wxT("\tCtrl+Z"); |
71 | m_redoAccelerator = wxT("\tCtrl+Y"); | |
70050c82 VZ |
72 | } |
73 | ||
74 | wxCommandProcessor::~wxCommandProcessor() | |
75 | { | |
76 | ClearCommands(); | |
77 | } | |
78 | ||
79 | bool wxCommandProcessor::DoCommand(wxCommand& cmd) | |
80 | { | |
81 | return cmd.Do(); | |
82 | } | |
83 | ||
84 | bool wxCommandProcessor::UndoCommand(wxCommand& cmd) | |
85 | { | |
86 | return cmd.Undo(); | |
87 | } | |
88 | ||
89 | // Pass a command to the processor. The processor calls Do(); | |
90 | // if successful, is appended to the command history unless | |
91 | // storeIt is FALSE. | |
92 | bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt) | |
93 | { | |
94 | wxCHECK_MSG( command, FALSE, _T("no command in wxCommandProcessor::Submit") ); | |
95 | ||
96 | if ( !DoCommand(*command) ) | |
ca1b5af1 VZ |
97 | { |
98 | // the user code expects the command to be deleted anyhow | |
99 | delete command; | |
100 | ||
70050c82 | 101 | return FALSE; |
ca1b5af1 | 102 | } |
70050c82 VZ |
103 | |
104 | if ( storeIt ) | |
105 | Store(command); | |
106 | ||
107 | return TRUE; | |
108 | } | |
109 | ||
110 | void wxCommandProcessor::Store(wxCommand *command) | |
111 | { | |
112 | wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") ); | |
113 | ||
b1d4dd7a | 114 | if ( (int)m_commands.GetCount() == m_maxNoCommands ) |
70050c82 | 115 | { |
222ed1d6 | 116 | wxList::compatibility_iterator firstNode = m_commands.GetFirst(); |
b1d4dd7a | 117 | wxCommand *firstCommand = (wxCommand *)firstNode->GetData(); |
70050c82 | 118 | delete firstCommand; |
222ed1d6 | 119 | m_commands.Erase(firstNode); |
70050c82 VZ |
120 | } |
121 | ||
122 | // Correct a bug: we must chop off the current 'branch' | |
123 | // so that we're at the end of the command list. | |
124 | if (!m_currentCommand) | |
125 | ClearCommands(); | |
126 | else | |
127 | { | |
222ed1d6 | 128 | wxList::compatibility_iterator node = m_currentCommand->GetNext(); |
70050c82 VZ |
129 | while (node) |
130 | { | |
222ed1d6 | 131 | wxList::compatibility_iterator next = node->GetNext(); |
b1d4dd7a | 132 | delete (wxCommand *)node->GetData(); |
222ed1d6 | 133 | m_commands.Erase(node); |
70050c82 VZ |
134 | node = next; |
135 | } | |
136 | } | |
137 | ||
138 | m_commands.Append(command); | |
b1d4dd7a | 139 | m_currentCommand = m_commands.GetLast(); |
70050c82 VZ |
140 | SetMenuStrings(); |
141 | } | |
142 | ||
143 | bool wxCommandProcessor::Undo() | |
144 | { | |
145 | wxCommand *command = GetCurrentCommand(); | |
146 | if ( command && command->CanUndo() ) | |
147 | { | |
148 | if ( UndoCommand(*command) ) | |
149 | { | |
b1d4dd7a | 150 | m_currentCommand = m_currentCommand->GetPrevious(); |
70050c82 VZ |
151 | SetMenuStrings(); |
152 | return TRUE; | |
153 | } | |
154 | } | |
155 | ||
156 | return FALSE; | |
157 | } | |
158 | ||
159 | bool wxCommandProcessor::Redo() | |
160 | { | |
161 | wxCommand *redoCommand = (wxCommand *) NULL; | |
4ed40684 VZ |
162 | wxList::compatibility_iterator redoNode |
163 | #if !wxUSE_STL | |
164 | = NULL // just to avoid warnings | |
165 | #endif // !wxUSE_STL | |
166 | ; | |
17271c79 VZ |
167 | |
168 | if ( m_currentCommand ) | |
70050c82 | 169 | { |
17271c79 | 170 | // is there anything to redo? |
b1d4dd7a | 171 | if ( m_currentCommand->GetNext() ) |
17271c79 | 172 | { |
b1d4dd7a RL |
173 | redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData(); |
174 | redoNode = m_currentCommand->GetNext(); | |
17271c79 | 175 | } |
70050c82 | 176 | } |
17271c79 | 177 | else // no current command, redo the first one |
70050c82 | 178 | { |
b1d4dd7a | 179 | if (m_commands.GetCount() > 0) |
70050c82 | 180 | { |
b1d4dd7a RL |
181 | redoCommand = (wxCommand *)m_commands.GetFirst()->GetData(); |
182 | redoNode = m_commands.GetFirst(); | |
70050c82 VZ |
183 | } |
184 | } | |
185 | ||
186 | if (redoCommand) | |
187 | { | |
188 | bool success = DoCommand(*redoCommand); | |
189 | if (success) | |
190 | { | |
191 | m_currentCommand = redoNode; | |
192 | SetMenuStrings(); | |
193 | return TRUE; | |
194 | } | |
195 | } | |
196 | return FALSE; | |
197 | } | |
198 | ||
199 | bool wxCommandProcessor::CanUndo() const | |
200 | { | |
201 | wxCommand *command = GetCurrentCommand(); | |
202 | ||
203 | return command && command->CanUndo(); | |
204 | } | |
205 | ||
206 | bool wxCommandProcessor::CanRedo() const | |
207 | { | |
222ed1d6 | 208 | if (m_currentCommand && !m_currentCommand->GetNext()) |
70050c82 VZ |
209 | return FALSE; |
210 | ||
222ed1d6 | 211 | if (m_currentCommand && m_currentCommand->GetNext()) |
70050c82 VZ |
212 | return TRUE; |
213 | ||
222ed1d6 | 214 | if (!m_currentCommand && (m_commands.GetCount() > 0)) |
70050c82 VZ |
215 | return TRUE; |
216 | ||
217 | return FALSE; | |
218 | } | |
219 | ||
220 | void wxCommandProcessor::Initialize() | |
221 | { | |
b1d4dd7a | 222 | m_currentCommand = m_commands.GetLast(); |
70050c82 VZ |
223 | SetMenuStrings(); |
224 | } | |
225 | ||
226 | void wxCommandProcessor::SetMenuStrings() | |
227 | { | |
228 | #if wxUSE_MENUS | |
229 | if (m_commandEditMenu) | |
230 | { | |
d863ed83 JS |
231 | wxString undoLabel = GetUndoMenuLabel(); |
232 | wxString redoLabel = GetRedoMenuLabel(); | |
233 | ||
234 | m_commandEditMenu->SetLabel(wxID_UNDO, undoLabel); | |
235 | m_commandEditMenu->Enable(wxID_UNDO, CanUndo()); | |
236 | ||
237 | m_commandEditMenu->SetLabel(wxID_REDO, redoLabel); | |
238 | m_commandEditMenu->Enable(wxID_REDO, CanRedo()); | |
239 | } | |
240 | #endif // wxUSE_MENUS | |
241 | } | |
242 | ||
243 | // Gets the current Undo menu label. | |
244 | wxString wxCommandProcessor::GetUndoMenuLabel() const | |
245 | { | |
246 | wxString buf; | |
247 | if (m_currentCommand) | |
248 | { | |
b1d4dd7a | 249 | wxCommand *command = (wxCommand *)m_currentCommand->GetData(); |
d863ed83 JS |
250 | wxString commandName(command->GetName()); |
251 | if (commandName == wxT("")) commandName = _("Unnamed command"); | |
252 | bool canUndo = command->CanUndo(); | |
253 | if (canUndo) | |
254 | buf = wxString(_("&Undo ")) + commandName + m_undoAccelerator; | |
255 | else | |
256 | buf = wxString(_("Can't &Undo ")) + commandName + m_undoAccelerator; | |
257 | } | |
258 | else | |
259 | { | |
260 | buf = _("&Undo") + m_undoAccelerator; | |
261 | } | |
262 | ||
263 | return buf; | |
264 | } | |
265 | ||
266 | // Gets the current Undo menu label. | |
267 | wxString wxCommandProcessor::GetRedoMenuLabel() const | |
268 | { | |
269 | wxString buf; | |
270 | if (m_currentCommand) | |
271 | { | |
272 | // We can redo, if we're not at the end of the history. | |
b1d4dd7a | 273 | if (m_currentCommand->GetNext()) |
70050c82 | 274 | { |
b1d4dd7a | 275 | wxCommand *redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData(); |
d863ed83 JS |
276 | wxString redoCommandName(redoCommand->GetName()); |
277 | if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command"); | |
278 | buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator; | |
70050c82 VZ |
279 | } |
280 | else | |
281 | { | |
d863ed83 | 282 | buf = _("&Redo") + m_redoAccelerator; |
70050c82 VZ |
283 | } |
284 | } | |
d863ed83 JS |
285 | else |
286 | { | |
b1d4dd7a | 287 | if (m_commands.GetCount() == 0) |
d863ed83 JS |
288 | { |
289 | buf = _("&Redo") + m_redoAccelerator; | |
290 | } | |
291 | else | |
292 | { | |
293 | // currentCommand is NULL but there are commands: this means that | |
294 | // we've undone to the start of the list, but can redo the first. | |
b1d4dd7a | 295 | wxCommand *redoCommand = (wxCommand *)m_commands.GetFirst()->GetData(); |
d863ed83 JS |
296 | wxString redoCommandName(redoCommand->GetName()); |
297 | if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command"); | |
298 | buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator; | |
299 | } | |
300 | } | |
301 | return buf; | |
70050c82 VZ |
302 | } |
303 | ||
304 | void wxCommandProcessor::ClearCommands() | |
305 | { | |
222ed1d6 | 306 | wxList::compatibility_iterator node = m_commands.GetFirst(); |
70050c82 VZ |
307 | while (node) |
308 | { | |
b1d4dd7a | 309 | wxCommand *command = (wxCommand *)node->GetData(); |
70050c82 | 310 | delete command; |
222ed1d6 | 311 | m_commands.Erase(node); |
b1d4dd7a | 312 | node = m_commands.GetFirst(); |
70050c82 | 313 | } |
222ed1d6 | 314 | m_currentCommand = wxList::compatibility_iterator(); |
70050c82 VZ |
315 | } |
316 | ||
317 |