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