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"
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
;
62 m_undoAccelerator
= wxT("\tCtrl+Z");
63 m_redoAccelerator
= wxT("\tCtrl+Y");
66 m_currentCommand
= wxList::compatibility_iterator();
69 wxCommandProcessor::~wxCommandProcessor()
74 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
79 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
84 // Pass a command to the processor. The processor calls Do();
85 // if successful, is appended to the command history unless
87 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
89 wxCHECK_MSG( command
, false, wxT("no command in wxCommandProcessor::Submit") );
91 if ( !DoCommand(*command
) )
93 // the user code expects the command to be deleted anyhow
107 void wxCommandProcessor::Store(wxCommand
*command
)
109 wxCHECK_RET( command
, wxT("no command in wxCommandProcessor::Store") );
111 if ( (int)m_commands
.GetCount() == m_maxNoCommands
)
113 wxList::compatibility_iterator firstNode
= m_commands
.GetFirst();
114 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->GetData();
116 m_commands
.Erase(firstNode
);
118 // Make sure m_lastSavedCommand won't point to freed memory
119 if ( m_lastSavedCommand
== firstNode
)
120 m_lastSavedCommand
= wxList::compatibility_iterator();
123 // Correct a bug: we must chop off the current 'branch'
124 // so that we're at the end of the command list.
125 if (!m_currentCommand
)
129 wxList::compatibility_iterator node
= m_currentCommand
->GetNext();
132 wxList::compatibility_iterator next
= node
->GetNext();
133 delete (wxCommand
*)node
->GetData();
134 m_commands
.Erase(node
);
136 // Make sure m_lastSavedCommand won't point to freed memory
137 if ( m_lastSavedCommand
== node
)
138 m_lastSavedCommand
= wxList::compatibility_iterator();
144 m_commands
.Append(command
);
145 m_currentCommand
= m_commands
.GetLast();
149 bool wxCommandProcessor::Undo()
151 wxCommand
*command
= GetCurrentCommand();
152 if ( command
&& command
->CanUndo() )
154 if ( UndoCommand(*command
) )
156 m_currentCommand
= m_currentCommand
->GetPrevious();
165 bool wxCommandProcessor::Redo()
167 wxCommand
*redoCommand
= NULL
;
168 wxList::compatibility_iterator redoNode
170 = NULL
// just to avoid warnings
174 if ( m_currentCommand
)
176 // is there anything to redo?
177 if ( m_currentCommand
->GetNext() )
179 redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
180 redoNode
= m_currentCommand
->GetNext();
183 else // no current command, redo the first one
185 if (m_commands
.GetCount() > 0)
187 redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
188 redoNode
= m_commands
.GetFirst();
194 bool success
= DoCommand(*redoCommand
);
197 m_currentCommand
= redoNode
;
205 bool wxCommandProcessor::CanUndo() const
207 wxCommand
*command
= GetCurrentCommand();
209 return command
&& command
->CanUndo();
212 bool wxCommandProcessor::CanRedo() const
214 if (m_currentCommand
&& !m_currentCommand
->GetNext())
217 if (m_currentCommand
&& m_currentCommand
->GetNext())
220 if (!m_currentCommand
&& (m_commands
.GetCount() > 0))
226 void wxCommandProcessor::Initialize()
228 m_currentCommand
= m_commands
.GetLast();
232 void wxCommandProcessor::SetMenuStrings()
235 if (m_commandEditMenu
)
237 wxString undoLabel
= GetUndoMenuLabel();
238 wxString redoLabel
= GetRedoMenuLabel();
240 m_commandEditMenu
->SetLabel(wxID_UNDO
, undoLabel
);
241 m_commandEditMenu
->Enable(wxID_UNDO
, CanUndo());
243 m_commandEditMenu
->SetLabel(wxID_REDO
, redoLabel
);
244 m_commandEditMenu
->Enable(wxID_REDO
, CanRedo());
246 #endif // wxUSE_MENUS
249 // Gets the current Undo menu label.
250 wxString
wxCommandProcessor::GetUndoMenuLabel() const
253 if (m_currentCommand
)
255 wxCommand
*command
= (wxCommand
*)m_currentCommand
->GetData();
256 wxString
commandName(command
->GetName());
257 if (commandName
.empty()) commandName
= _("Unnamed command");
258 bool canUndo
= command
->CanUndo();
260 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
262 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
266 buf
= _("&Undo") + m_undoAccelerator
;
272 // Gets the current Undo menu label.
273 wxString
wxCommandProcessor::GetRedoMenuLabel() const
276 if (m_currentCommand
)
278 // We can redo, if we're not at the end of the history.
279 if (m_currentCommand
->GetNext())
281 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->GetNext()->GetData();
282 wxString
redoCommandName(redoCommand
->GetName());
283 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
284 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
288 buf
= _("&Redo") + m_redoAccelerator
;
293 if (m_commands
.GetCount() == 0)
295 buf
= _("&Redo") + m_redoAccelerator
;
299 // currentCommand is NULL but there are commands: this means that
300 // we've undone to the start of the list, but can redo the first.
301 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.GetFirst()->GetData();
302 wxString
redoCommandName(redoCommand
->GetName());
303 if (redoCommandName
.empty()) redoCommandName
= _("Unnamed command");
304 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
310 void wxCommandProcessor::ClearCommands()
312 wxList::compatibility_iterator node
= m_commands
.GetFirst();
315 wxCommand
*command
= (wxCommand
*)node
->GetData();
317 m_commands
.Erase(node
);
318 node
= m_commands
.GetFirst();
321 m_currentCommand
= wxList::compatibility_iterator();
322 m_lastSavedCommand
= wxList::compatibility_iterator();