]> git.saurik.com Git - wxWidgets.git/blame - src/common/cmdproc.cpp
Changes to the XRC library:
[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
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
90// storeIt is FALSE.
91bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
92{
93 wxCHECK_MSG( command, FALSE, _T("no command in wxCommandProcessor::Submit") );
94
95 if ( !DoCommand(*command) )
96 return FALSE;
97
98 if ( storeIt )
99 Store(command);
100
101 return TRUE;
102}
103
104void wxCommandProcessor::Store(wxCommand *command)
105{
106 wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") );
107
108 if (m_commands.Number() == m_maxNoCommands)
109 {
110 wxNode *firstNode = m_commands.First();
111 wxCommand *firstCommand = (wxCommand *)firstNode->Data();
112 delete firstCommand;
113 delete firstNode;
114 }
115
116 // Correct a bug: we must chop off the current 'branch'
117 // so that we're at the end of the command list.
118 if (!m_currentCommand)
119 ClearCommands();
120 else
121 {
122 wxNode *node = m_currentCommand->Next();
123 while (node)
124 {
125 wxNode *next = node->Next();
126 delete (wxCommand *)node->Data();
127 delete node;
128 node = next;
129 }
130 }
131
132 m_commands.Append(command);
133 m_currentCommand = m_commands.Last();
134 SetMenuStrings();
135}
136
137bool wxCommandProcessor::Undo()
138{
139 wxCommand *command = GetCurrentCommand();
140 if ( command && command->CanUndo() )
141 {
142 if ( UndoCommand(*command) )
143 {
144 m_currentCommand = m_currentCommand->Previous();
145 SetMenuStrings();
146 return TRUE;
147 }
148 }
149
150 return FALSE;
151}
152
153bool wxCommandProcessor::Redo()
154{
155 wxCommand *redoCommand = (wxCommand *) NULL;
156 wxNode *redoNode = (wxNode *) NULL;
17271c79
VZ
157
158 if ( m_currentCommand )
70050c82 159 {
17271c79
VZ
160 // is there anything to redo?
161 if ( m_currentCommand->Next() )
162 {
163 redoCommand = (wxCommand *)m_currentCommand->Next()->Data();
164 redoNode = m_currentCommand->Next();
165 }
70050c82 166 }
17271c79 167 else // no current command, redo the first one
70050c82
VZ
168 {
169 if (m_commands.Number() > 0)
170 {
171 redoCommand = (wxCommand *)m_commands.First()->Data();
172 redoNode = m_commands.First();
173 }
174 }
175
176 if (redoCommand)
177 {
178 bool success = DoCommand(*redoCommand);
179 if (success)
180 {
181 m_currentCommand = redoNode;
182 SetMenuStrings();
183 return TRUE;
184 }
185 }
186 return FALSE;
187}
188
189bool wxCommandProcessor::CanUndo() const
190{
191 wxCommand *command = GetCurrentCommand();
192
193 return command && command->CanUndo();
194}
195
196bool wxCommandProcessor::CanRedo() const
197{
198 if ((m_currentCommand != (wxNode*) NULL) && (m_currentCommand->Next() == (wxNode*) NULL))
199 return FALSE;
200
201 if ((m_currentCommand != (wxNode*) NULL) && (m_currentCommand->Next() != (wxNode*) NULL))
202 return TRUE;
203
204 if ((m_currentCommand == (wxNode*) NULL) && (m_commands.Number() > 0))
205 return TRUE;
206
207 return FALSE;
208}
209
210void wxCommandProcessor::Initialize()
211{
212 m_currentCommand = m_commands.Last();
213 SetMenuStrings();
214}
215
216void wxCommandProcessor::SetMenuStrings()
217{
218#if wxUSE_MENUS
219 if (m_commandEditMenu)
220 {
221 wxString buf;
222 if (m_currentCommand)
223 {
224 wxCommand *command = (wxCommand *)m_currentCommand->Data();
225 wxString commandName(command->GetName());
226 if (commandName == wxT("")) commandName = _("Unnamed command");
227 bool canUndo = command->CanUndo();
228 if (canUndo)
229 buf = wxString(_("&Undo ")) + commandName;
230 else
231 buf = wxString(_("Can't &Undo ")) + commandName;
232
233 m_commandEditMenu->SetLabel(wxID_UNDO, buf);
234 m_commandEditMenu->Enable(wxID_UNDO, canUndo);
235
236 // We can redo, if we're not at the end of the history.
237 if (m_currentCommand->Next())
238 {
239 wxCommand *redoCommand = (wxCommand *)m_currentCommand->Next()->Data();
240 wxString redoCommandName(redoCommand->GetName());
241 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
242 buf = wxString(_("&Redo ")) + redoCommandName;
243 m_commandEditMenu->SetLabel(wxID_REDO, buf);
244 m_commandEditMenu->Enable(wxID_REDO, TRUE);
245 }
246 else
247 {
248 m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
249 m_commandEditMenu->Enable(wxID_REDO, FALSE);
250 }
251 }
252 else
253 {
254 m_commandEditMenu->SetLabel(wxID_UNDO, _("&Undo"));
255 m_commandEditMenu->Enable(wxID_UNDO, FALSE);
256
257 if (m_commands.Number() == 0)
258 {
259 m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
260 m_commandEditMenu->Enable(wxID_REDO, FALSE);
261 }
262 else
263 {
264 // currentCommand is NULL but there are commands: this means that
265 // we've undone to the start of the list, but can redo the first.
266 wxCommand *redoCommand = (wxCommand *)m_commands.First()->Data();
267 wxString redoCommandName(redoCommand->GetName());
268 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
269 buf = wxString(_("&Redo ")) + redoCommandName;
270 m_commandEditMenu->SetLabel(wxID_REDO, buf);
271 m_commandEditMenu->Enable(wxID_REDO, TRUE);
272 }
273 }
274 }
275#endif // wxUSE_MENUS
276}
277
278void wxCommandProcessor::ClearCommands()
279{
280 wxNode *node = m_commands.First();
281 while (node)
282 {
283 wxCommand *command = (wxCommand *)node->Data();
284 delete command;
285 delete node;
286 node = m_commands.First();
287 }
288 m_currentCommand = (wxNode *) NULL;
289}
290
291