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
;
65 m_undoAccelerator
= '\t' + wxAcceleratorEntry(wxACCEL_CTRL
, 'Z').ToString();
66 m_redoAccelerator
= '\t' + wxAcceleratorEntry(wxACCEL_CTRL
, 'Y').ToString();
70 m_currentCommand
= wxList::compatibility_iterator();
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, wxT("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
, wxT("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
);
122 // Make sure m_lastSavedCommand won't point to freed memory
123 if ( m_lastSavedCommand
== firstNode
)
124 m_lastSavedCommand
= wxList::compatibility_iterator();
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
);
140 // Make sure m_lastSavedCommand won't point to freed memory
141 if ( m_lastSavedCommand
== node
)
142 m_lastSavedCommand
= wxList::compatibility_iterator();
148 m_commands
.Append(command
);
149 m_currentCommand
= m_commands
.GetLast();
153 bool wxCommandProcessor::Undo()
155 wxCommand
*command
= GetCurrentCommand();
156 if ( command
&& command
->CanUndo() )
158 if ( UndoCommand(*command
) )
160 m_currentCommand
= m_currentCommand
->GetPrevious();
169 bool wxCommandProcessor::Redo()
171 wxCommand
*redoCommand
= NULL
;
172 wxList::compatibility_iterator redoNode
174 = NULL
// just to avoid warnings
178 if ( m_currentCommand
)
180 // is there anything to redo?
181 if ( m_currentCommand
->GetNext() )
183 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
184 redoNode
= m_currentCommand
->GetNext();
187 else // no current command, redo the first one
189 if (m_commands
.GetCount() > 0)
191 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
192 redoNode
= m_commands
.GetFirst();
198 bool success
= DoCommand(*redoCommand
);
201 m_currentCommand
= redoNode
;
209 bool wxCommandProcessor::CanUndo() const
211 wxCommand
*command
= GetCurrentCommand();
213 return command
&& command
->CanUndo();
216 bool wxCommandProcessor::CanRedo() const
218 if (m_currentCommand
&& !m_currentCommand
->GetNext())
221 if (m_currentCommand
&& m_currentCommand
->GetNext())
224 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
230 void wxCommandProcessor::Initialize()
232 m_currentCommand
= m_commands
.GetLast();
236 void wxCommandProcessor::SetMenuStrings()
239 if (m_commandEditMenu
)
241 wxString undoLabel
= GetUndoMenuLabel();
242 wxString redoLabel
= GetRedoMenuLabel();
244 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
245 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
247 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
248 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
250 #endif // wxUSE_MENUS
253 // Gets the current Undo menu label.
254 wxString
wxCommandProcessor::GetUndoMenuLabel() const
257 if (m_currentCommand
)
259 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
260 wxString
commandName(command
->GetName());
261 if (commandName
.empty()) commandName
= _("Unnamed command");
262 bool canUndo
= command
->CanUndo();
264 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
266 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
270 buf
= _("&Undo") + m_undoAccelerator
;
276 // Gets the current Undo menu label.
277 wxString
wxCommandProcessor::GetRedoMenuLabel() const
280 if (m_currentCommand
)
282 // We can redo, if we're not at the end of the history.
283 if (m_currentCommand
->GetNext())
285 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
286 wxString
redoCommandName(redoCommand
->GetName());
287 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
288 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
292 buf
= _("&Redo") + m_redoAccelerator
;
297 if (m_commands
.GetCount() == 0)
299 buf
= _("&Redo") + m_redoAccelerator
;
303 // currentCommand is NULL but there are commands: this means that
304 // we've undone to the start of the list, but can redo the first.
305 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
306 wxString
redoCommandName(redoCommand
->GetName());
307 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
308 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
314 void wxCommandProcessor::ClearCommands()
316 wxList::compatibility_iterator node
= m_commands
.GetFirst();
319 wxCommand
*command
= (wxCommand
*)node
->GetData();
321 m_commands
.Erase(node
);
322 node
= m_commands
.GetFirst();
325 m_currentCommand
= wxList::compatibility_iterator();
326 m_lastSavedCommand
= wxList::compatibility_iterator();