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