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