]> git.saurik.com Git - wxWidgets.git/blame - src/common/cmdproc.cpp
added wxUmaskChanger class and wxCHANGE_UMASK macro and use them instead of duplicati...
[wxWidgets.git] / src / common / cmdproc.cpp
CommitLineData
70050c82
VZ
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$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
70050c82
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
70050c82
VZ
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
43IMPLEMENT_CLASS(wxCommand, wxObject)
44IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor, wxObject)
45
46// ----------------------------------------------------------------------------
47// wxCommand
48// ----------------------------------------------------------------------------
49
50wxCommand::wxCommand(bool canUndoIt, const wxString& name)
51{
52 m_canUndo = canUndoIt;
53 m_commandName = name;
54}
55
56wxCommand::~wxCommand()
57{
58}
59
60// ----------------------------------------------------------------------------
61// Command processor
62// ----------------------------------------------------------------------------
63
64wxCommandProcessor::wxCommandProcessor(int maxCommands)
65{
66 m_maxNoCommands = maxCommands;
70050c82
VZ
67#if wxUSE_MENUS
68 m_commandEditMenu = (wxMenu *) NULL;
69#endif // wxUSE_MENUS
6aa3ea88
JS
70 m_undoAccelerator = wxT("\tCtrl+Z");
71 m_redoAccelerator = wxT("\tCtrl+Y");
084db46c
MB
72#if !wxUSE_STL
73 m_currentCommand = NULL;
74#endif
70050c82
VZ
75}
76
77wxCommandProcessor::~wxCommandProcessor()
78{
79 ClearCommands();
80}
81
82bool wxCommandProcessor::DoCommand(wxCommand& cmd)
83{
84 return cmd.Do();
85}
86
87bool 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
c9d59ee7 94// storeIt is false.
70050c82
VZ
95bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
96{
c9d59ee7 97 wxCHECK_MSG( command, false, _T("no command in wxCommandProcessor::Submit") );
70050c82
VZ
98
99 if ( !DoCommand(*command) )
ca1b5af1
VZ
100 {
101 // the user code expects the command to be deleted anyhow
102 delete command;
103
c9d59ee7 104 return false;
ca1b5af1 105 }
70050c82
VZ
106
107 if ( storeIt )
108 Store(command);
f530fa27
VZ
109 else
110 delete command;
70050c82 111
c9d59ee7 112 return true;
70050c82
VZ
113}
114
115void wxCommandProcessor::Store(wxCommand *command)
116{
117 wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") );
118
b1d4dd7a 119 if ( (int)m_commands.GetCount() == m_maxNoCommands )
70050c82 120 {
222ed1d6 121 wxList::compatibility_iterator firstNode = m_commands.GetFirst();
b1d4dd7a 122 wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
70050c82 123 delete firstCommand;
222ed1d6 124 m_commands.Erase(firstNode);
70050c82
VZ
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 {
222ed1d6 133 wxList::compatibility_iterator node = m_currentCommand->GetNext();
70050c82
VZ
134 while (node)
135 {
222ed1d6 136 wxList::compatibility_iterator next = node->GetNext();
b1d4dd7a 137 delete (wxCommand *)node->GetData();
222ed1d6 138 m_commands.Erase(node);
70050c82
VZ
139 node = next;
140 }
141 }
142
143 m_commands.Append(command);
b1d4dd7a 144 m_currentCommand = m_commands.GetLast();
70050c82
VZ
145 SetMenuStrings();
146}
147
148bool wxCommandProcessor::Undo()
149{
150 wxCommand *command = GetCurrentCommand();
151 if ( command && command->CanUndo() )
152 {
153 if ( UndoCommand(*command) )
154 {
b1d4dd7a 155 m_currentCommand = m_currentCommand->GetPrevious();
70050c82 156 SetMenuStrings();
c9d59ee7 157 return true;
70050c82
VZ
158 }
159 }
160
c9d59ee7 161 return false;
70050c82
VZ
162}
163
164bool wxCommandProcessor::Redo()
165{
166 wxCommand *redoCommand = (wxCommand *) NULL;
4ed40684
VZ
167 wxList::compatibility_iterator redoNode
168#if !wxUSE_STL
169 = NULL // just to avoid warnings
170#endif // !wxUSE_STL
171 ;
17271c79
VZ
172
173 if ( m_currentCommand )
70050c82 174 {
17271c79 175 // is there anything to redo?
b1d4dd7a 176 if ( m_currentCommand->GetNext() )
17271c79 177 {
b1d4dd7a
RL
178 redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
179 redoNode = m_currentCommand->GetNext();
17271c79 180 }
70050c82 181 }
17271c79 182 else // no current command, redo the first one
70050c82 183 {
b1d4dd7a 184 if (m_commands.GetCount() > 0)
70050c82 185 {
b1d4dd7a
RL
186 redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
187 redoNode = m_commands.GetFirst();
70050c82
VZ
188 }
189 }
190
191 if (redoCommand)
192 {
193 bool success = DoCommand(*redoCommand);
194 if (success)
195 {
196 m_currentCommand = redoNode;
197 SetMenuStrings();
c9d59ee7 198 return true;
70050c82
VZ
199 }
200 }
c9d59ee7 201 return false;
70050c82
VZ
202}
203
204bool wxCommandProcessor::CanUndo() const
205{
206 wxCommand *command = GetCurrentCommand();
207
208 return command && command->CanUndo();
209}
210
211bool wxCommandProcessor::CanRedo() const
212{
222ed1d6 213 if (m_currentCommand && !m_currentCommand->GetNext())
c9d59ee7 214 return false;
70050c82 215
222ed1d6 216 if (m_currentCommand && m_currentCommand->GetNext())
c9d59ee7 217 return true;
70050c82 218
222ed1d6 219 if (!m_currentCommand && (m_commands.GetCount() > 0))
c9d59ee7 220 return true;
70050c82 221
c9d59ee7 222 return false;
70050c82
VZ
223}
224
225void wxCommandProcessor::Initialize()
226{
b1d4dd7a 227 m_currentCommand = m_commands.GetLast();
70050c82
VZ
228 SetMenuStrings();
229}
230
231void wxCommandProcessor::SetMenuStrings()
232{
233#if wxUSE_MENUS
234 if (m_commandEditMenu)
235 {
d863ed83
JS
236 wxString undoLabel = GetUndoMenuLabel();
237 wxString redoLabel = GetRedoMenuLabel();
c9d59ee7 238
d863ed83
JS
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.
249wxString wxCommandProcessor::GetUndoMenuLabel() const
250{
251 wxString buf;
252 if (m_currentCommand)
253 {
b1d4dd7a 254 wxCommand *command = (wxCommand *)m_currentCommand->GetData();
d863ed83
JS
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 }
c9d59ee7 267
d863ed83
JS
268 return buf;
269}
270
271// Gets the current Undo menu label.
272wxString 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.
b1d4dd7a 278 if (m_currentCommand->GetNext())
70050c82 279 {
b1d4dd7a 280 wxCommand *redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
d863ed83
JS
281 wxString redoCommandName(redoCommand->GetName());
282 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
283 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
70050c82
VZ
284 }
285 else
286 {
d863ed83 287 buf = _("&Redo") + m_redoAccelerator;
70050c82
VZ
288 }
289 }
d863ed83
JS
290 else
291 {
b1d4dd7a 292 if (m_commands.GetCount() == 0)
d863ed83
JS
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.
b1d4dd7a 300 wxCommand *redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
d863ed83
JS
301 wxString redoCommandName(redoCommand->GetName());
302 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
303 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
304 }
305 }
306 return buf;
70050c82
VZ
307}
308
309void wxCommandProcessor::ClearCommands()
310{
222ed1d6 311 wxList::compatibility_iterator node = m_commands.GetFirst();
70050c82
VZ
312 while (node)
313 {
b1d4dd7a 314 wxCommand *command = (wxCommand *)node->GetData();
70050c82 315 delete command;
222ed1d6 316 m_commands.Erase(node);
b1d4dd7a 317 node = m_commands.GetFirst();
70050c82 318 }
222ed1d6 319 m_currentCommand = wxList::compatibility_iterator();
70050c82
VZ
320}
321
322