]> git.saurik.com Git - wxWidgets.git/blob - src/common/cmdproc.cpp
toplevel background behaviour changed
[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) wxWidgets 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 // ----------------------------------------------------------------------------
57 // Command processor
58 // ----------------------------------------------------------------------------
59
60 wxCommandProcessor::wxCommandProcessor(int maxCommands)
61 {
62 m_maxNoCommands = maxCommands;
63 #if wxUSE_MENUS
64 m_commandEditMenu = (wxMenu *) NULL;
65 #endif // wxUSE_MENUS
66 m_undoAccelerator = wxT("\tCtrl+Z");
67 m_redoAccelerator = wxT("\tCtrl+Y");
68 #if !wxUSE_STL
69 m_currentCommand = NULL;
70 #endif
71 }
72
73 wxCommandProcessor::~wxCommandProcessor()
74 {
75 ClearCommands();
76 }
77
78 bool wxCommandProcessor::DoCommand(wxCommand& cmd)
79 {
80 return cmd.Do();
81 }
82
83 bool 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.
91 bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
92 {
93 wxCHECK_MSG( command, false, _T("no command in wxCommandProcessor::Submit") );
94
95 if ( !DoCommand(*command) )
96 {
97 // the user code expects the command to be deleted anyhow
98 delete command;
99
100 return false;
101 }
102
103 if ( storeIt )
104 Store(command);
105 else
106 delete command;
107
108 return true;
109 }
110
111 void wxCommandProcessor::Store(wxCommand *command)
112 {
113 wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") );
114
115 if ( (int)m_commands.GetCount() == m_maxNoCommands )
116 {
117 wxList::compatibility_iterator firstNode = m_commands.GetFirst();
118 wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
119 delete firstCommand;
120 m_commands.Erase(firstNode);
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 {
129 wxList::compatibility_iterator node = m_currentCommand->GetNext();
130 while (node)
131 {
132 wxList::compatibility_iterator next = node->GetNext();
133 delete (wxCommand *)node->GetData();
134 m_commands.Erase(node);
135 node = next;
136 }
137 }
138
139 m_commands.Append(command);
140 m_currentCommand = m_commands.GetLast();
141 SetMenuStrings();
142 }
143
144 bool wxCommandProcessor::Undo()
145 {
146 wxCommand *command = GetCurrentCommand();
147 if ( command && command->CanUndo() )
148 {
149 if ( UndoCommand(*command) )
150 {
151 m_currentCommand = m_currentCommand->GetPrevious();
152 SetMenuStrings();
153 return true;
154 }
155 }
156
157 return false;
158 }
159
160 bool wxCommandProcessor::Redo()
161 {
162 wxCommand *redoCommand = (wxCommand *) NULL;
163 wxList::compatibility_iterator redoNode
164 #if !wxUSE_STL
165 = NULL // just to avoid warnings
166 #endif // !wxUSE_STL
167 ;
168
169 if ( m_currentCommand )
170 {
171 // is there anything to redo?
172 if ( m_currentCommand->GetNext() )
173 {
174 redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
175 redoNode = m_currentCommand->GetNext();
176 }
177 }
178 else // no current command, redo the first one
179 {
180 if (m_commands.GetCount() > 0)
181 {
182 redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
183 redoNode = m_commands.GetFirst();
184 }
185 }
186
187 if (redoCommand)
188 {
189 bool success = DoCommand(*redoCommand);
190 if (success)
191 {
192 m_currentCommand = redoNode;
193 SetMenuStrings();
194 return true;
195 }
196 }
197 return false;
198 }
199
200 bool wxCommandProcessor::CanUndo() const
201 {
202 wxCommand *command = GetCurrentCommand();
203
204 return command && command->CanUndo();
205 }
206
207 bool wxCommandProcessor::CanRedo() const
208 {
209 if (m_currentCommand && !m_currentCommand->GetNext())
210 return false;
211
212 if (m_currentCommand && m_currentCommand->GetNext())
213 return true;
214
215 if (!m_currentCommand && (m_commands.GetCount() > 0))
216 return true;
217
218 return false;
219 }
220
221 void wxCommandProcessor::Initialize()
222 {
223 m_currentCommand = m_commands.GetLast();
224 SetMenuStrings();
225 }
226
227 void wxCommandProcessor::SetMenuStrings()
228 {
229 #if wxUSE_MENUS
230 if (m_commandEditMenu)
231 {
232 wxString undoLabel = GetUndoMenuLabel();
233 wxString redoLabel = GetRedoMenuLabel();
234
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.
245 wxString wxCommandProcessor::GetUndoMenuLabel() const
246 {
247 wxString buf;
248 if (m_currentCommand)
249 {
250 wxCommand *command = (wxCommand *)m_currentCommand->GetData();
251 wxString commandName(command->GetName());
252 if (commandName.empty()) 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 }
263
264 return buf;
265 }
266
267 // Gets the current Undo menu label.
268 wxString 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.
274 if (m_currentCommand->GetNext())
275 {
276 wxCommand *redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
277 wxString redoCommandName(redoCommand->GetName());
278 if (redoCommandName.empty()) redoCommandName = _("Unnamed command");
279 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
280 }
281 else
282 {
283 buf = _("&Redo") + m_redoAccelerator;
284 }
285 }
286 else
287 {
288 if (m_commands.GetCount() == 0)
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.
296 wxCommand *redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
297 wxString redoCommandName(redoCommand->GetName());
298 if (redoCommandName.empty()) redoCommandName = _("Unnamed command");
299 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
300 }
301 }
302 return buf;
303 }
304
305 void wxCommandProcessor::ClearCommands()
306 {
307 wxList::compatibility_iterator node = m_commands.GetFirst();
308 while (node)
309 {
310 wxCommand *command = (wxCommand *)node->GetData();
311 delete command;
312 m_commands.Erase(node);
313 node = m_commands.GetFirst();
314 }
315 m_currentCommand = wxList::compatibility_iterator();
316 }
317
318