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