1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cmdproc.cpp
3 // Purpose: wxCommand and wxCommandProcessor classes
4 // Author: Julian Smart (extracted from docview.h by VZ)
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "cmdproc.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
37 #include "wx/cmdproc.h"
39 // ============================================================================
41 // ============================================================================
43 IMPLEMENT_CLASS(wxCommand
, wxObject
)
44 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
52 m_canUndo
= canUndoIt
;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
62 m_maxNoCommands
= maxCommands
;
64 m_commandEditMenu
= (wxMenu
*) NULL
;
66 m_undoAccelerator
= wxT("\tCtrl+Z");
67 m_redoAccelerator
= wxT("\tCtrl+Y");
69 m_currentCommand
= NULL
;
73 wxCommandProcessor::~wxCommandProcessor()
78 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
83 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
88 // Pass a command to the processor. The processor calls Do();
89 // if successful, is appended to the command history unless
91 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
93 wxCHECK_MSG( command
, false, _T("no command in wxCommandProcessor::Submit") );
95 if ( !DoCommand(*command
) )
97 // the user code expects the command to be deleted anyhow
111 void wxCommandProcessor::Store(wxCommand
*command
)
113 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
115 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
117 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
118 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
120 m_commands
.Erase(firstNode
);
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
)
129 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
132 wxList::compatibility_iterator next
= node
->GetNext();
133 delete (wxCommand
*)node
->GetData();
134 m_commands
.Erase(node
);
139 m_commands
.Append(command
);
140 m_currentCommand
= m_commands
.GetLast();
144 bool wxCommandProcessor::Undo()
146 wxCommand
*command
= GetCurrentCommand();
147 if ( command
&& command
->CanUndo() )
149 if ( UndoCommand(*command
) )
151 m_currentCommand
= m_currentCommand
->GetPrevious();
160 bool wxCommandProcessor::Redo()
162 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
163 wxList::compatibility_iterator redoNode
165 = NULL
// just to avoid warnings
169 if ( m_currentCommand
)
171 // is there anything to redo?
172 if ( m_currentCommand
->GetNext() )
174 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
175 redoNode
= m_currentCommand
->GetNext();
178 else // no current command, redo the first one
180 if (m_commands
.GetCount() > 0)
182 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
183 redoNode
= m_commands
.GetFirst();
189 bool success
= DoCommand(*redoCommand
);
192 m_currentCommand
= redoNode
;
200 bool wxCommandProcessor::CanUndo() const
202 wxCommand
*command
= GetCurrentCommand();
204 return command
&& command
->CanUndo();
207 bool wxCommandProcessor::CanRedo() const
209 if (m_currentCommand
&& !m_currentCommand
->GetNext())
212 if (m_currentCommand
&& m_currentCommand
->GetNext())
215 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
221 void wxCommandProcessor::Initialize()
223 m_currentCommand
= m_commands
.GetLast();
227 void wxCommandProcessor::SetMenuStrings()
230 if (m_commandEditMenu
)
232 wxString undoLabel
= GetUndoMenuLabel();
233 wxString redoLabel
= GetRedoMenuLabel();
235 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
236 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
238 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
239 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
241 #endif // wxUSE_MENUS
244 // Gets the current Undo menu label.
245 wxString
wxCommandProcessor::GetUndoMenuLabel() const
248 if (m_currentCommand
)
250 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
251 wxString
commandName(command
->GetName());
252 if (commandName
== wxT("")) commandName
= _("Unnamed command");
253 bool canUndo
= command
->CanUndo();
255 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
257 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
261 buf
= _("&Undo") + m_undoAccelerator
;
267 // Gets the current Undo menu label.
268 wxString
wxCommandProcessor::GetRedoMenuLabel() const
271 if (m_currentCommand
)
273 // We can redo, if we're not at the end of the history.
274 if (m_currentCommand
->GetNext())
276 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
277 wxString
redoCommandName(redoCommand
->GetName());
278 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
279 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
283 buf
= _("&Redo") + m_redoAccelerator
;
288 if (m_commands
.GetCount() == 0)
290 buf
= _("&Redo") + m_redoAccelerator
;
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
== wxT("")) redoCommandName
= _("Unnamed command");
299 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
305 void wxCommandProcessor::ClearCommands()
307 wxList::compatibility_iterator node
= m_commands
.GetFirst();
310 wxCommand
*command
= (wxCommand
*)node
->GetData();
312 m_commands
.Erase(node
);
313 node
= m_commands
.GetFirst();
315 m_currentCommand
= wxList::compatibility_iterator();