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 // Correct a bug: we must chop off the current 'branch'
116 // so that we're at the end of the command list.
117 if (!m_currentCommand
)
121 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
124 wxList::compatibility_iterator next
= node
->GetNext();
126 // Make sure m_lastSavedCommand won't point to freed memory
127 if ( m_lastSavedCommand
&& m_lastSavedCommand
== node
)
128 m_lastSavedCommand
= wxList::compatibility_iterator();
130 delete (wxCommand
*)node
->GetData();
131 m_commands
.Erase(node
);
137 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
139 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
141 // Make sure m_lastSavedCommand won't point to freed memory
142 if ( m_lastSavedCommand
&& m_lastSavedCommand
== firstNode
)
143 m_lastSavedCommand
= wxList::compatibility_iterator();
145 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
147 m_commands
.Erase(firstNode
);
150 m_commands
.Append(command
);
151 m_currentCommand
= m_commands
.GetLast();
155 bool wxCommandProcessor::Undo()
157 wxCommand
*command
= GetCurrentCommand();
158 if ( command
&& command
->CanUndo() )
160 if ( UndoCommand(*command
) )
162 m_currentCommand
= m_currentCommand
->GetPrevious();
171 bool wxCommandProcessor::Redo()
173 wxCommand
*redoCommand
= NULL
;
174 wxList::compatibility_iterator redoNode
175 #if !wxUSE_STD_CONTAINERS
176 = NULL
// just to avoid warnings
177 #endif // !wxUSE_STD_CONTAINERS
180 if ( m_currentCommand
)
182 // is there anything to redo?
183 if ( m_currentCommand
->GetNext() )
185 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
186 redoNode
= m_currentCommand
->GetNext();
189 else // no current command, redo the first one
191 if (m_commands
.GetCount() > 0)
193 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
194 redoNode
= m_commands
.GetFirst();
200 bool success
= DoCommand(*redoCommand
);
203 m_currentCommand
= redoNode
;
211 bool wxCommandProcessor::CanUndo() const
213 wxCommand
*command
= GetCurrentCommand();
215 return command
&& command
->CanUndo();
218 bool wxCommandProcessor::CanRedo() const
220 if (m_currentCommand
&& !m_currentCommand
->GetNext())
223 if (m_currentCommand
&& m_currentCommand
->GetNext())
226 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
232 void wxCommandProcessor::Initialize()
234 m_currentCommand
= m_commands
.GetLast();
238 void wxCommandProcessor::SetMenuStrings()
241 if (m_commandEditMenu
)
243 wxString undoLabel
= GetUndoMenuLabel();
244 wxString redoLabel
= GetRedoMenuLabel();
246 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
247 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
249 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
250 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
252 #endif // wxUSE_MENUS
255 // Gets the current Undo menu label.
256 wxString
wxCommandProcessor::GetUndoMenuLabel() const
259 if (m_currentCommand
)
261 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
262 wxString
commandName(command
->GetName());
263 if (commandName
.empty()) commandName
= _("Unnamed command");
264 bool canUndo
= command
->CanUndo();
266 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
268 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
272 buf
= _("&Undo") + m_undoAccelerator
;
278 // Gets the current Undo menu label.
279 wxString
wxCommandProcessor::GetRedoMenuLabel() const
282 if (m_currentCommand
)
284 // We can redo, if we're not at the end of the history.
285 if (m_currentCommand
->GetNext())
287 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
288 wxString
redoCommandName(redoCommand
->GetName());
289 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
290 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
294 buf
= _("&Redo") + m_redoAccelerator
;
299 if (m_commands
.GetCount() == 0)
301 buf
= _("&Redo") + m_redoAccelerator
;
305 // currentCommand is NULL but there are commands: this means that
306 // we've undone to the start of the list, but can redo the first.
307 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
308 wxString
redoCommandName(redoCommand
->GetName());
309 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
310 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
316 void wxCommandProcessor::ClearCommands()
318 wxList::compatibility_iterator node
= m_commands
.GetFirst();
321 wxCommand
*command
= (wxCommand
*)node
->GetData();
323 m_commands
.Erase(node
);
324 node
= m_commands
.GetFirst();
327 m_currentCommand
= wxList::compatibility_iterator();
328 m_lastSavedCommand
= wxList::compatibility_iterator();