]> git.saurik.com Git - wxWidgets.git/blob - src/common/cmdproc.cpp
added wxUmaskChanger class and wxCHANGE_UMASK macro and use them instead of duplicati...
[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 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 else
110 delete command;
111
112 return true;
113 }
114
115 void wxCommandProcessor::Store(wxCommand *command)
116 {
117 wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") );
118
119 if ( (int)m_commands.GetCount() == m_maxNoCommands )
120 {
121 wxList::compatibility_iterator firstNode = m_commands.GetFirst();
122 wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
123 delete firstCommand;
124 m_commands.Erase(firstNode);
125 }
126
127 // Correct a bug: we must chop off the current 'branch'
128 // so that we're at the end of the command list.
129 if (!m_currentCommand)
130 ClearCommands();
131 else
132 {
133 wxList::compatibility_iterator node = m_currentCommand->GetNext();
134 while (node)
135 {
136 wxList::compatibility_iterator next = node->GetNext();
137 delete (wxCommand *)node->GetData();
138 m_commands.Erase(node);
139 node = next;
140 }
141 }
142
143 m_commands.Append(command);
144 m_currentCommand = m_commands.GetLast();
145 SetMenuStrings();
146 }
147
148 bool wxCommandProcessor::Undo()
149 {
150 wxCommand *command = GetCurrentCommand();
151 if ( command && command->CanUndo() )
152 {
153 if ( UndoCommand(*command) )
154 {
155 m_currentCommand = m_currentCommand->GetPrevious();
156 SetMenuStrings();
157 return true;
158 }
159 }
160
161 return false;
162 }
163
164 bool wxCommandProcessor::Redo()
165 {
166 wxCommand *redoCommand = (wxCommand *) NULL;
167 wxList::compatibility_iterator redoNode
168 #if !wxUSE_STL
169 = NULL // just to avoid warnings
170 #endif // !wxUSE_STL
171 ;
172
173 if ( m_currentCommand )
174 {
175 // is there anything to redo?
176 if ( m_currentCommand->GetNext() )
177 {
178 redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
179 redoNode = m_currentCommand->GetNext();
180 }
181 }
182 else // no current command, redo the first one
183 {
184 if (m_commands.GetCount() > 0)
185 {
186 redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
187 redoNode = m_commands.GetFirst();
188 }
189 }
190
191 if (redoCommand)
192 {
193 bool success = DoCommand(*redoCommand);
194 if (success)
195 {
196 m_currentCommand = redoNode;
197 SetMenuStrings();
198 return true;
199 }
200 }
201 return false;
202 }
203
204 bool wxCommandProcessor::CanUndo() const
205 {
206 wxCommand *command = GetCurrentCommand();
207
208 return command && command->CanUndo();
209 }
210
211 bool wxCommandProcessor::CanRedo() const
212 {
213 if (m_currentCommand && !m_currentCommand->GetNext())
214 return false;
215
216 if (m_currentCommand && m_currentCommand->GetNext())
217 return true;
218
219 if (!m_currentCommand && (m_commands.GetCount() > 0))
220 return true;
221
222 return false;
223 }
224
225 void wxCommandProcessor::Initialize()
226 {
227 m_currentCommand = m_commands.GetLast();
228 SetMenuStrings();
229 }
230
231 void wxCommandProcessor::SetMenuStrings()
232 {
233 #if wxUSE_MENUS
234 if (m_commandEditMenu)
235 {
236 wxString undoLabel = GetUndoMenuLabel();
237 wxString redoLabel = GetRedoMenuLabel();
238
239 m_commandEditMenu->SetLabel(wxID_UNDO, undoLabel);
240 m_commandEditMenu->Enable(wxID_UNDO, CanUndo());
241
242 m_commandEditMenu->SetLabel(wxID_REDO, redoLabel);
243 m_commandEditMenu->Enable(wxID_REDO, CanRedo());
244 }
245 #endif // wxUSE_MENUS
246 }
247
248 // Gets the current Undo menu label.
249 wxString wxCommandProcessor::GetUndoMenuLabel() const
250 {
251 wxString buf;
252 if (m_currentCommand)
253 {
254 wxCommand *command = (wxCommand *)m_currentCommand->GetData();
255 wxString commandName(command->GetName());
256 if (commandName == wxT("")) commandName = _("Unnamed command");
257 bool canUndo = command->CanUndo();
258 if (canUndo)
259 buf = wxString(_("&Undo ")) + commandName + m_undoAccelerator;
260 else
261 buf = wxString(_("Can't &Undo ")) + commandName + m_undoAccelerator;
262 }
263 else
264 {
265 buf = _("&Undo") + m_undoAccelerator;
266 }
267
268 return buf;
269 }
270
271 // Gets the current Undo menu label.
272 wxString wxCommandProcessor::GetRedoMenuLabel() const
273 {
274 wxString buf;
275 if (m_currentCommand)
276 {
277 // We can redo, if we're not at the end of the history.
278 if (m_currentCommand->GetNext())
279 {
280 wxCommand *redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
281 wxString redoCommandName(redoCommand->GetName());
282 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
283 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
284 }
285 else
286 {
287 buf = _("&Redo") + m_redoAccelerator;
288 }
289 }
290 else
291 {
292 if (m_commands.GetCount() == 0)
293 {
294 buf = _("&Redo") + m_redoAccelerator;
295 }
296 else
297 {
298 // currentCommand is NULL but there are commands: this means that
299 // we've undone to the start of the list, but can redo the first.
300 wxCommand *redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
301 wxString redoCommandName(redoCommand->GetName());
302 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
303 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
304 }
305 }
306 return buf;
307 }
308
309 void wxCommandProcessor::ClearCommands()
310 {
311 wxList::compatibility_iterator node = m_commands.GetFirst();
312 while (node)
313 {
314 wxCommand *command = (wxCommand *)node->GetData();
315 delete command;
316 m_commands.Erase(node);
317 node = m_commands.GetFirst();
318 }
319 m_currentCommand = wxList::compatibility_iterator();
320 }
321
322