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