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");
74 wxCommandProcessor::~wxCommandProcessor()
79 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
84 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
89 // Pass a command to the processor. The processor calls Do();
90 // if successful, is appended to the command history unless
92 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
94 wxCHECK_MSG( command
, FALSE
, _T("no command in wxCommandProcessor::Submit") );
96 if ( !DoCommand(*command
) )
98 // the user code expects the command to be deleted anyhow
110 void wxCommandProcessor::Store(wxCommand
*command
)
112 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
114 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
116 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
117 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
119 m_commands
.Erase(firstNode
);
122 // Correct a bug: we must chop off the current 'branch'
123 // so that we're at the end of the command list.
124 if (!m_currentCommand
)
128 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
131 wxList::compatibility_iterator next
= node
->GetNext();
132 delete (wxCommand
*)node
->GetData();
133 m_commands
.Erase(node
);
138 m_commands
.Append(command
);
139 m_currentCommand
= m_commands
.GetLast();
143 bool wxCommandProcessor::Undo()
145 wxCommand
*command
= GetCurrentCommand();
146 if ( command
&& command
->CanUndo() )
148 if ( UndoCommand(*command
) )
150 m_currentCommand
= m_currentCommand
->GetPrevious();
159 bool wxCommandProcessor::Redo()
161 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
162 wxList::compatibility_iterator redoNode
164 = NULL
// just to avoid warnings
168 if ( m_currentCommand
)
170 // is there anything to redo?
171 if ( m_currentCommand
->GetNext() )
173 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
174 redoNode
= m_currentCommand
->GetNext();
177 else // no current command, redo the first one
179 if (m_commands
.GetCount() > 0)
181 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
182 redoNode
= m_commands
.GetFirst();
188 bool success
= DoCommand(*redoCommand
);
191 m_currentCommand
= redoNode
;
199 bool wxCommandProcessor::CanUndo() const
201 wxCommand
*command
= GetCurrentCommand();
203 return command
&& command
->CanUndo();
206 bool wxCommandProcessor::CanRedo() const
208 if (m_currentCommand
&& !m_currentCommand
->GetNext())
211 if (m_currentCommand
&& m_currentCommand
->GetNext())
214 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
220 void wxCommandProcessor::Initialize()
222 m_currentCommand
= m_commands
.GetLast();
226 void wxCommandProcessor::SetMenuStrings()
229 if (m_commandEditMenu
)
231 wxString undoLabel
= GetUndoMenuLabel();
232 wxString redoLabel
= GetRedoMenuLabel();
234 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
235 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
237 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
238 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
240 #endif // wxUSE_MENUS
243 // Gets the current Undo menu label.
244 wxString
wxCommandProcessor::GetUndoMenuLabel() const
247 if (m_currentCommand
)
249 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
250 wxString
commandName(command
->GetName());
251 if (commandName
== wxT("")) commandName
= _("Unnamed command");
252 bool canUndo
= command
->CanUndo();
254 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
256 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
260 buf
= _("&Undo") + m_undoAccelerator
;
266 // Gets the current Undo menu label.
267 wxString
wxCommandProcessor::GetRedoMenuLabel() const
270 if (m_currentCommand
)
272 // We can redo, if we're not at the end of the history.
273 if (m_currentCommand
->GetNext())
275 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
276 wxString
redoCommandName(redoCommand
->GetName());
277 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
278 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
282 buf
= _("&Redo") + m_redoAccelerator
;
287 if (m_commands
.GetCount() == 0)
289 buf
= _("&Redo") + m_redoAccelerator
;
293 // currentCommand is NULL but there are commands: this means that
294 // we've undone to the start of the list, but can redo the first.
295 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
296 wxString
redoCommandName(redoCommand
->GetName());
297 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
298 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
304 void wxCommandProcessor::ClearCommands()
306 wxList::compatibility_iterator node
= m_commands
.GetFirst();
309 wxCommand
*command
= (wxCommand
*)node
->GetData();
311 m_commands
.Erase(node
);
312 node
= m_commands
.GetFirst();
314 m_currentCommand
= wxList::compatibility_iterator();