1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cmdproc.cpp
3 // Purpose: wxCommand and wxCommandProcessor classes
4 // Author: Julian Smart (extracted from docview.h by VZ)
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/string.h"
33 #include "wx/cmdproc.h"
35 // ============================================================================
37 // ============================================================================
39 IMPLEMENT_CLASS(wxCommand
, wxObject
)
40 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
48 m_canUndo
= canUndoIt
;
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
58 m_maxNoCommands
= maxCommands
;
60 m_commandEditMenu
= NULL
;
64 m_undoAccelerator
= '\t' + wxAcceleratorEntry(wxACCEL_CTRL
, 'Z').ToString();
65 m_redoAccelerator
= '\t' + wxAcceleratorEntry(wxACCEL_CTRL
, 'Y').ToString();
69 m_currentCommand
= wxList::compatibility_iterator();
72 wxCommandProcessor::~wxCommandProcessor()
77 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
82 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
87 // Pass a command to the processor. The processor calls Do();
88 // if successful, is appended to the command history unless
90 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
92 wxCHECK_MSG( command
, false, wxT("no command in wxCommandProcessor::Submit") );
94 if ( !DoCommand(*command
) )
96 // the user code expects the command to be deleted anyhow
110 void wxCommandProcessor::Store(wxCommand
*command
)
112 wxCHECK_RET( command
, wxT("no command in wxCommandProcessor::Store") );
114 // Correct a bug: we must chop off the current 'branch'
115 // so that we're at the end of the command list.
116 if (!m_currentCommand
)
120 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
123 wxList::compatibility_iterator next
= node
->GetNext();
125 // Make sure m_lastSavedCommand won't point to freed memory
126 if ( m_lastSavedCommand
&& m_lastSavedCommand
== node
)
127 m_lastSavedCommand
= wxList::compatibility_iterator();
129 delete (wxCommand
*)node
->GetData();
130 m_commands
.Erase(node
);
136 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
138 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
140 // Make sure m_lastSavedCommand won't point to freed memory
141 if ( m_lastSavedCommand
&& m_lastSavedCommand
== firstNode
)
142 m_lastSavedCommand
= wxList::compatibility_iterator();
144 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
146 m_commands
.Erase(firstNode
);
149 m_commands
.Append(command
);
150 m_currentCommand
= m_commands
.GetLast();
154 bool wxCommandProcessor::Undo()
156 wxCommand
*command
= GetCurrentCommand();
157 if ( command
&& command
->CanUndo() )
159 if ( UndoCommand(*command
) )
161 m_currentCommand
= m_currentCommand
->GetPrevious();
170 bool wxCommandProcessor::Redo()
172 wxCommand
*redoCommand
= NULL
;
173 wxList::compatibility_iterator redoNode
174 #if !wxUSE_STD_CONTAINERS
175 = NULL
// just to avoid warnings
176 #endif // !wxUSE_STD_CONTAINERS
179 if ( m_currentCommand
)
181 // is there anything to redo?
182 if ( m_currentCommand
->GetNext() )
184 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
185 redoNode
= m_currentCommand
->GetNext();
188 else // no current command, redo the first one
190 if (m_commands
.GetCount() > 0)
192 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
193 redoNode
= m_commands
.GetFirst();
199 bool success
= DoCommand(*redoCommand
);
202 m_currentCommand
= redoNode
;
210 bool wxCommandProcessor::CanUndo() const
212 wxCommand
*command
= GetCurrentCommand();
214 return command
&& command
->CanUndo();
217 bool wxCommandProcessor::CanRedo() const
219 if (m_currentCommand
&& !m_currentCommand
->GetNext())
222 if (m_currentCommand
&& m_currentCommand
->GetNext())
225 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
231 void wxCommandProcessor::Initialize()
233 m_currentCommand
= m_commands
.GetLast();
237 void wxCommandProcessor::SetMenuStrings()
240 if (m_commandEditMenu
)
242 wxString undoLabel
= GetUndoMenuLabel();
243 wxString redoLabel
= GetRedoMenuLabel();
245 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
246 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
248 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
249 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
251 #endif // wxUSE_MENUS
254 // Gets the current Undo menu label.
255 wxString
wxCommandProcessor::GetUndoMenuLabel() const
258 if (m_currentCommand
)
260 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
261 wxString
commandName(command
->GetName());
262 if (commandName
.empty()) commandName
= _("Unnamed command");
263 bool canUndo
= command
->CanUndo();
265 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
267 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
271 buf
= _("&Undo") + m_undoAccelerator
;
277 // Gets the current Undo menu label.
278 wxString
wxCommandProcessor::GetRedoMenuLabel() const
281 if (m_currentCommand
)
283 // We can redo, if we're not at the end of the history.
284 if (m_currentCommand
->GetNext())
286 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
287 wxString
redoCommandName(redoCommand
->GetName());
288 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
289 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
293 buf
= _("&Redo") + m_redoAccelerator
;
298 if (m_commands
.GetCount() == 0)
300 buf
= _("&Redo") + m_redoAccelerator
;
304 // currentCommand is NULL but there are commands: this means that
305 // we've undone to the start of the list, but can redo the first.
306 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
307 wxString
redoCommandName(redoCommand
->GetName());
308 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
309 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
315 void wxCommandProcessor::ClearCommands()
317 wxList::compatibility_iterator node
= m_commands
.GetFirst();
320 wxCommand
*command
= (wxCommand
*)node
->GetData();
322 m_commands
.Erase(node
);
323 node
= m_commands
.GetFirst();
326 m_currentCommand
= wxList::compatibility_iterator();
327 m_lastSavedCommand
= wxList::compatibility_iterator();
330 bool wxCommandProcessor::IsDirty() const
332 if ( m_commands
.empty() )
334 // If we have never been modified, we can't be dirty.
338 if ( !m_lastSavedCommand
)
340 // If we have been modified but have never been saved, we're dirty.
344 if ( !m_currentCommand
)
346 // This only happens if all commands were undone after saving the
347 // document: we're dirty then.
351 // Finally if both iterators are valid, we may just compare them.
352 return m_currentCommand
!= m_lastSavedCommand
;