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