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) wxWindows team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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 wxCommand::~wxCommand()
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
66 m_maxNoCommands
= maxCommands
;
68 m_commandEditMenu
= (wxMenu
*) NULL
;
70 m_undoAccelerator
= wxT("\tCtrl+Z");
71 m_redoAccelerator
= wxT("\tCtrl+Y");
73 m_currentCommand
= NULL
;
77 wxCommandProcessor::~wxCommandProcessor()
82 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
87 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
92 // Pass a command to the processor. The processor calls Do();
93 // if successful, is appended to the command history unless
95 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
97 wxCHECK_MSG( command
, FALSE
, _T("no command in wxCommandProcessor::Submit") );
99 if ( !DoCommand(*command
) )
101 // the user code expects the command to be deleted anyhow
113 void wxCommandProcessor::Store(wxCommand
*command
)
115 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
117 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
119 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
120 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
122 m_commands
.Erase(firstNode
);
125 // Correct a bug: we must chop off the current 'branch'
126 // so that we're at the end of the command list.
127 if (!m_currentCommand
)
131 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
134 wxList::compatibility_iterator next
= node
->GetNext();
135 delete (wxCommand
*)node
->GetData();
136 m_commands
.Erase(node
);
141 m_commands
.Append(command
);
142 m_currentCommand
= m_commands
.GetLast();
146 bool wxCommandProcessor::Undo()
148 wxCommand
*command
= GetCurrentCommand();
149 if ( command
&& command
->CanUndo() )
151 if ( UndoCommand(*command
) )
153 m_currentCommand
= m_currentCommand
->GetPrevious();
162 bool wxCommandProcessor::Redo()
164 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
165 wxList::compatibility_iterator redoNode
167 = NULL
// just to avoid warnings
171 if ( m_currentCommand
)
173 // is there anything to redo?
174 if ( m_currentCommand
->GetNext() )
176 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
177 redoNode
= m_currentCommand
->GetNext();
180 else // no current command, redo the first one
182 if (m_commands
.GetCount() > 0)
184 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
185 redoNode
= m_commands
.GetFirst();
191 bool success
= DoCommand(*redoCommand
);
194 m_currentCommand
= redoNode
;
202 bool wxCommandProcessor::CanUndo() const
204 wxCommand
*command
= GetCurrentCommand();
206 return command
&& command
->CanUndo();
209 bool wxCommandProcessor::CanRedo() const
211 if (m_currentCommand
&& !m_currentCommand
->GetNext())
214 if (m_currentCommand
&& m_currentCommand
->GetNext())
217 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
223 void wxCommandProcessor::Initialize()
225 m_currentCommand
= m_commands
.GetLast();
229 void wxCommandProcessor::SetMenuStrings()
232 if (m_commandEditMenu
)
234 wxString undoLabel
= GetUndoMenuLabel();
235 wxString redoLabel
= GetRedoMenuLabel();
237 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
238 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
240 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
241 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
243 #endif // wxUSE_MENUS
246 // Gets the current Undo menu label.
247 wxString
wxCommandProcessor::GetUndoMenuLabel() const
250 if (m_currentCommand
)
252 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
253 wxString
commandName(command
->GetName());
254 if (commandName
== wxT("")) commandName
= _("Unnamed command");
255 bool canUndo
= command
->CanUndo();
257 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
259 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
263 buf
= _("&Undo") + m_undoAccelerator
;
269 // Gets the current Undo menu label.
270 wxString
wxCommandProcessor::GetRedoMenuLabel() const
273 if (m_currentCommand
)
275 // We can redo, if we're not at the end of the history.
276 if (m_currentCommand
->GetNext())
278 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
279 wxString
redoCommandName(redoCommand
->GetName());
280 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
281 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
285 buf
= _("&Redo") + m_redoAccelerator
;
290 if (m_commands
.GetCount() == 0)
292 buf
= _("&Redo") + m_redoAccelerator
;
296 // currentCommand is NULL but there are commands: this means that
297 // we've undone to the start of the list, but can redo the first.
298 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
299 wxString
redoCommandName(redoCommand
->GetName());
300 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
301 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
307 void wxCommandProcessor::ClearCommands()
309 wxList::compatibility_iterator node
= m_commands
.GetFirst();
312 wxCommand
*command
= (wxCommand
*)node
->GetData();
314 m_commands
.Erase(node
);
315 node
= m_commands
.GetFirst();
317 m_currentCommand
= wxList::compatibility_iterator();