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 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
115 void wxCommandProcessor::Store(wxCommand
*command
)
117 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
119 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
121 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
122 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
124 m_commands
.Erase(firstNode
);
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
)
133 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
136 wxList::compatibility_iterator next
= node
->GetNext();
137 delete (wxCommand
*)node
->GetData();
138 m_commands
.Erase(node
);
143 m_commands
.Append(command
);
144 m_currentCommand
= m_commands
.GetLast();
148 bool wxCommandProcessor::Undo()
150 wxCommand
*command
= GetCurrentCommand();
151 if ( command
&& command
->CanUndo() )
153 if ( UndoCommand(*command
) )
155 m_currentCommand
= m_currentCommand
->GetPrevious();
164 bool wxCommandProcessor::Redo()
166 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
167 wxList::compatibility_iterator redoNode
169 = NULL
// just to avoid warnings
173 if ( m_currentCommand
)
175 // is there anything to redo?
176 if ( m_currentCommand
->GetNext() )
178 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
179 redoNode
= m_currentCommand
->GetNext();
182 else // no current command, redo the first one
184 if (m_commands
.GetCount() > 0)
186 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
187 redoNode
= m_commands
.GetFirst();
193 bool success
= DoCommand(*redoCommand
);
196 m_currentCommand
= redoNode
;
204 bool wxCommandProcessor::CanUndo() const
206 wxCommand
*command
= GetCurrentCommand();
208 return command
&& command
->CanUndo();
211 bool wxCommandProcessor::CanRedo() const
213 if (m_currentCommand
&& !m_currentCommand
->GetNext())
216 if (m_currentCommand
&& m_currentCommand
->GetNext())
219 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
225 void wxCommandProcessor::Initialize()
227 m_currentCommand
= m_commands
.GetLast();
231 void wxCommandProcessor::SetMenuStrings()
234 if (m_commandEditMenu
)
236 wxString undoLabel
= GetUndoMenuLabel();
237 wxString redoLabel
= GetRedoMenuLabel();
239 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
240 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
242 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
243 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
245 #endif // wxUSE_MENUS
248 // Gets the current Undo menu label.
249 wxString
wxCommandProcessor::GetUndoMenuLabel() const
252 if (m_currentCommand
)
254 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
255 wxString
commandName(command
->GetName());
256 if (commandName
== wxT("")) commandName
= _("Unnamed command");
257 bool canUndo
= command
->CanUndo();
259 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
261 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
265 buf
= _("&Undo") + m_undoAccelerator
;
271 // Gets the current Undo menu label.
272 wxString
wxCommandProcessor::GetRedoMenuLabel() const
275 if (m_currentCommand
)
277 // We can redo, if we're not at the end of the history.
278 if (m_currentCommand
->GetNext())
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
;
287 buf
= _("&Redo") + m_redoAccelerator
;
292 if (m_commands
.GetCount() == 0)
294 buf
= _("&Redo") + m_redoAccelerator
;
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
;
309 void wxCommandProcessor::ClearCommands()
311 wxList::compatibility_iterator node
= m_commands
.GetFirst();
314 wxCommand
*command
= (wxCommand
*)node
->GetData();
316 m_commands
.Erase(node
);
317 node
= m_commands
.GetFirst();
319 m_currentCommand
= wxList::compatibility_iterator();