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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/string.h"
34 #include "wx/cmdproc.h"
36 // ============================================================================
38 // ============================================================================
40 IMPLEMENT_CLASS(wxCommand
, wxObject
)
41 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
49 m_canUndo
= canUndoIt
;
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
59 m_maxNoCommands
= maxCommands
;
61 m_commandEditMenu
= NULL
;
63 m_undoAccelerator
= '\t' + wxAcceleratorEntry(wxACCEL_CTRL
, 'Z').ToString();
64 m_redoAccelerator
= '\t' + wxAcceleratorEntry(wxACCEL_CTRL
, 'Y').ToString();
67 m_currentCommand
= wxList::compatibility_iterator();
70 wxCommandProcessor::~wxCommandProcessor()
75 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
80 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
85 // Pass a command to the processor. The processor calls Do();
86 // if successful, is appended to the command history unless
88 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
90 wxCHECK_MSG( command
, false, wxT("no command in wxCommandProcessor::Submit") );
92 if ( !DoCommand(*command
) )
94 // the user code expects the command to be deleted anyhow
108 void wxCommandProcessor::Store(wxCommand
*command
)
110 wxCHECK_RET( command
, wxT("no command in wxCommandProcessor::Store") );
112 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
114 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
115 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
117 m_commands
.Erase(firstNode
);
119 // Make sure m_lastSavedCommand won't point to freed memory
120 if ( m_lastSavedCommand
== firstNode
)
121 m_lastSavedCommand
= wxList::compatibility_iterator();
124 // Correct a bug: we must chop off the current 'branch'
125 // so that we're at the end of the command list.
126 if (!m_currentCommand
)
130 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
133 wxList::compatibility_iterator next
= node
->GetNext();
134 delete (wxCommand
*)node
->GetData();
135 m_commands
.Erase(node
);
137 // Make sure m_lastSavedCommand won't point to freed memory
138 if ( m_lastSavedCommand
== node
)
139 m_lastSavedCommand
= wxList::compatibility_iterator();
145 m_commands
.Append(command
);
146 m_currentCommand
= m_commands
.GetLast();
150 bool wxCommandProcessor::Undo()
152 wxCommand
*command
= GetCurrentCommand();
153 if ( command
&& command
->CanUndo() )
155 if ( UndoCommand(*command
) )
157 m_currentCommand
= m_currentCommand
->GetPrevious();
166 bool wxCommandProcessor::Redo()
168 wxCommand
*redoCommand
= NULL
;
169 wxList::compatibility_iterator redoNode
171 = NULL
// just to avoid warnings
175 if ( m_currentCommand
)
177 // is there anything to redo?
178 if ( m_currentCommand
->GetNext() )
180 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
181 redoNode
= m_currentCommand
->GetNext();
184 else // no current command, redo the first one
186 if (m_commands
.GetCount() > 0)
188 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
189 redoNode
= m_commands
.GetFirst();
195 bool success
= DoCommand(*redoCommand
);
198 m_currentCommand
= redoNode
;
206 bool wxCommandProcessor::CanUndo() const
208 wxCommand
*command
= GetCurrentCommand();
210 return command
&& command
->CanUndo();
213 bool wxCommandProcessor::CanRedo() const
215 if (m_currentCommand
&& !m_currentCommand
->GetNext())
218 if (m_currentCommand
&& m_currentCommand
->GetNext())
221 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
227 void wxCommandProcessor::Initialize()
229 m_currentCommand
= m_commands
.GetLast();
233 void wxCommandProcessor::SetMenuStrings()
236 if (m_commandEditMenu
)
238 wxString undoLabel
= GetUndoMenuLabel();
239 wxString redoLabel
= GetRedoMenuLabel();
241 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
242 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
244 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
245 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
247 #endif // wxUSE_MENUS
250 // Gets the current Undo menu label.
251 wxString
wxCommandProcessor::GetUndoMenuLabel() const
254 if (m_currentCommand
)
256 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
257 wxString
commandName(command
->GetName());
258 if (commandName
.empty()) commandName
= _("Unnamed command");
259 bool canUndo
= command
->CanUndo();
261 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
263 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
267 buf
= _("&Undo") + m_undoAccelerator
;
273 // Gets the current Undo menu label.
274 wxString
wxCommandProcessor::GetRedoMenuLabel() const
277 if (m_currentCommand
)
279 // We can redo, if we're not at the end of the history.
280 if (m_currentCommand
->GetNext())
282 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
283 wxString
redoCommandName(redoCommand
->GetName());
284 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
285 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
289 buf
= _("&Redo") + m_redoAccelerator
;
294 if (m_commands
.GetCount() == 0)
296 buf
= _("&Redo") + m_redoAccelerator
;
300 // currentCommand is NULL but there are commands: this means that
301 // we've undone to the start of the list, but can redo the first.
302 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
303 wxString
redoCommandName(redoCommand
->GetName());
304 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
305 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
311 void wxCommandProcessor::ClearCommands()
313 wxList::compatibility_iterator node
= m_commands
.GetFirst();
316 wxCommand
*command
= (wxCommand
*)node
->GetData();
318 m_commands
.Erase(node
);
319 node
= m_commands
.GetFirst();
322 m_currentCommand
= wxList::compatibility_iterator();
323 m_lastSavedCommand
= wxList::compatibility_iterator();