]> git.saurik.com Git - wxWidgets.git/blame - src/common/cmdproc.cpp
wxMac uses wxStandardPathsCF to mean wxStandardPaths in its wxBase
[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$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
70050c82
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
70050c82
VZ
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
70050c82
VZ
56// ----------------------------------------------------------------------------
57// Command processor
58// ----------------------------------------------------------------------------
59
60wxCommandProcessor::wxCommandProcessor(int maxCommands)
61{
62 m_maxNoCommands = maxCommands;
70050c82
VZ
63#if wxUSE_MENUS
64 m_commandEditMenu = (wxMenu *) NULL;
65#endif // wxUSE_MENUS
6aa3ea88
JS
66 m_undoAccelerator = wxT("\tCtrl+Z");
67 m_redoAccelerator = wxT("\tCtrl+Y");
084db46c
MB
68#if !wxUSE_STL
69 m_currentCommand = NULL;
70#endif
70050c82
VZ
71}
72
73wxCommandProcessor::~wxCommandProcessor()
74{
75 ClearCommands();
76}
77
78bool wxCommandProcessor::DoCommand(wxCommand& cmd)
79{
80 return cmd.Do();
81}
82
83bool wxCommandProcessor::UndoCommand(wxCommand& cmd)
84{
85 return cmd.Undo();
86}
87
88// Pass a command to the processor. The processor calls Do();
89// if successful, is appended to the command history unless
c9d59ee7 90// storeIt is false.
70050c82
VZ
91bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
92{
c9d59ee7 93 wxCHECK_MSG( command, false, _T("no command in wxCommandProcessor::Submit") );
70050c82
VZ
94
95 if ( !DoCommand(*command) )
ca1b5af1
VZ
96 {
97 // the user code expects the command to be deleted anyhow
98 delete command;
99
c9d59ee7 100 return false;
ca1b5af1 101 }
70050c82
VZ
102
103 if ( storeIt )
104 Store(command);
f530fa27
VZ
105 else
106 delete command;
70050c82 107
c9d59ee7 108 return true;
70050c82
VZ
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 {
222ed1d6 117 wxList::compatibility_iterator firstNode = m_commands.GetFirst();
b1d4dd7a 118 wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
70050c82 119 delete firstCommand;
222ed1d6 120 m_commands.Erase(firstNode);
70050c82
VZ
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 {
222ed1d6 129 wxList::compatibility_iterator node = m_currentCommand->GetNext();
70050c82
VZ
130 while (node)
131 {
222ed1d6 132 wxList::compatibility_iterator next = node->GetNext();
b1d4dd7a 133 delete (wxCommand *)node->GetData();
222ed1d6 134 m_commands.Erase(node);
70050c82
VZ
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 152 SetMenuStrings();
c9d59ee7 153 return true;
70050c82
VZ
154 }
155 }
156
c9d59ee7 157 return false;
70050c82
VZ
158}
159
160bool wxCommandProcessor::Redo()
161{
162 wxCommand *redoCommand = (wxCommand *) NULL;
4ed40684
VZ
163 wxList::compatibility_iterator redoNode
164#if !wxUSE_STL
165 = NULL // just to avoid warnings
166#endif // !wxUSE_STL
167 ;
17271c79
VZ
168
169 if ( m_currentCommand )
70050c82 170 {
17271c79 171 // is there anything to redo?
b1d4dd7a 172 if ( m_currentCommand->GetNext() )
17271c79 173 {
b1d4dd7a
RL
174 redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
175 redoNode = m_currentCommand->GetNext();
17271c79 176 }
70050c82 177 }
17271c79 178 else // no current command, redo the first one
70050c82 179 {
b1d4dd7a 180 if (m_commands.GetCount() > 0)
70050c82 181 {
b1d4dd7a
RL
182 redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
183 redoNode = m_commands.GetFirst();
70050c82
VZ
184 }
185 }
186
187 if (redoCommand)
188 {
189 bool success = DoCommand(*redoCommand);
190 if (success)
191 {
192 m_currentCommand = redoNode;
193 SetMenuStrings();
c9d59ee7 194 return true;
70050c82
VZ
195 }
196 }
c9d59ee7 197 return false;
70050c82
VZ
198}
199
200bool wxCommandProcessor::CanUndo() const
201{
202 wxCommand *command = GetCurrentCommand();
203
204 return command && command->CanUndo();
205}
206
207bool wxCommandProcessor::CanRedo() const
208{
222ed1d6 209 if (m_currentCommand && !m_currentCommand->GetNext())
c9d59ee7 210 return false;
70050c82 211
222ed1d6 212 if (m_currentCommand && m_currentCommand->GetNext())
c9d59ee7 213 return true;
70050c82 214
222ed1d6 215 if (!m_currentCommand && (m_commands.GetCount() > 0))
c9d59ee7 216 return true;
70050c82 217
c9d59ee7 218 return false;
70050c82
VZ
219}
220
221void wxCommandProcessor::Initialize()
222{
b1d4dd7a 223 m_currentCommand = m_commands.GetLast();
70050c82
VZ
224 SetMenuStrings();
225}
226
227void wxCommandProcessor::SetMenuStrings()
228{
229#if wxUSE_MENUS
230 if (m_commandEditMenu)
231 {
d863ed83
JS
232 wxString undoLabel = GetUndoMenuLabel();
233 wxString redoLabel = GetRedoMenuLabel();
c9d59ee7 234
d863ed83
JS
235 m_commandEditMenu->SetLabel(wxID_UNDO, undoLabel);
236 m_commandEditMenu->Enable(wxID_UNDO, CanUndo());
237
238 m_commandEditMenu->SetLabel(wxID_REDO, redoLabel);
239 m_commandEditMenu->Enable(wxID_REDO, CanRedo());
240 }
241#endif // wxUSE_MENUS
242}
243
244// Gets the current Undo menu label.
245wxString wxCommandProcessor::GetUndoMenuLabel() const
246{
247 wxString buf;
248 if (m_currentCommand)
249 {
b1d4dd7a 250 wxCommand *command = (wxCommand *)m_currentCommand->GetData();
d863ed83
JS
251 wxString commandName(command->GetName());
252 if (commandName == wxT("")) commandName = _("Unnamed command");
253 bool canUndo = command->CanUndo();
254 if (canUndo)
255 buf = wxString(_("&Undo ")) + commandName + m_undoAccelerator;
256 else
257 buf = wxString(_("Can't &Undo ")) + commandName + m_undoAccelerator;
258 }
259 else
260 {
261 buf = _("&Undo") + m_undoAccelerator;
262 }
c9d59ee7 263
d863ed83
JS
264 return buf;
265}
266
267// Gets the current Undo menu label.
268wxString wxCommandProcessor::GetRedoMenuLabel() const
269{
270 wxString buf;
271 if (m_currentCommand)
272 {
273 // We can redo, if we're not at the end of the history.
b1d4dd7a 274 if (m_currentCommand->GetNext())
70050c82 275 {
b1d4dd7a 276 wxCommand *redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
d863ed83
JS
277 wxString redoCommandName(redoCommand->GetName());
278 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
279 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
70050c82
VZ
280 }
281 else
282 {
d863ed83 283 buf = _("&Redo") + m_redoAccelerator;
70050c82
VZ
284 }
285 }
d863ed83
JS
286 else
287 {
b1d4dd7a 288 if (m_commands.GetCount() == 0)
d863ed83
JS
289 {
290 buf = _("&Redo") + m_redoAccelerator;
291 }
292 else
293 {
294 // currentCommand is NULL but there are commands: this means that
295 // we've undone to the start of the list, but can redo the first.
b1d4dd7a 296 wxCommand *redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
d863ed83
JS
297 wxString redoCommandName(redoCommand->GetName());
298 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
299 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
300 }
301 }
302 return buf;
70050c82
VZ
303}
304
305void wxCommandProcessor::ClearCommands()
306{
222ed1d6 307 wxList::compatibility_iterator node = m_commands.GetFirst();
70050c82
VZ
308 while (node)
309 {
b1d4dd7a 310 wxCommand *command = (wxCommand *)node->GetData();
70050c82 311 delete command;
222ed1d6 312 m_commands.Erase(node);
b1d4dd7a 313 node = m_commands.GetFirst();
70050c82 314 }
222ed1d6 315 m_currentCommand = wxList::compatibility_iterator();
70050c82
VZ
316}
317
318