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) wxWindows team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "cmdproc.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
37 #include "wx/cmdproc.h"
39 // ============================================================================
41 // ============================================================================
43 IMPLEMENT_CLASS(wxCommand
, wxObject
)
44 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
52 m_canUndo
= canUndoIt
;
56 wxCommand::~wxCommand()
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
66 m_maxNoCommands
= maxCommands
;
67 m_currentCommand
= (wxNode
*) NULL
;
69 m_commandEditMenu
= (wxMenu
*) NULL
;
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
, _T("no command in wxCommandProcessor::Submit") );
95 if ( !DoCommand(*command
) )
104 void wxCommandProcessor::Store(wxCommand
*command
)
106 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
108 if (m_commands
.Number() == m_maxNoCommands
)
110 wxNode
*firstNode
= m_commands
.First();
111 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
116 // Correct a bug: we must chop off the current 'branch'
117 // so that we're at the end of the command list.
118 if (!m_currentCommand
)
122 wxNode
*node
= m_currentCommand
->Next();
125 wxNode
*next
= node
->Next();
126 delete (wxCommand
*)node
->Data();
132 m_commands
.Append(command
);
133 m_currentCommand
= m_commands
.Last();
137 bool wxCommandProcessor::Undo()
139 wxCommand
*command
= GetCurrentCommand();
140 if ( command
&& command
->CanUndo() )
142 if ( UndoCommand(*command
) )
144 m_currentCommand
= m_currentCommand
->Previous();
153 bool wxCommandProcessor::Redo()
155 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
156 wxNode
*redoNode
= (wxNode
*) NULL
;
157 if (m_currentCommand
&& m_currentCommand
->Next())
159 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
160 redoNode
= m_currentCommand
->Next();
164 if (m_commands
.Number() > 0)
166 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
167 redoNode
= m_commands
.First();
173 bool success
= DoCommand(*redoCommand
);
176 m_currentCommand
= redoNode
;
184 bool wxCommandProcessor::CanUndo() const
186 wxCommand
*command
= GetCurrentCommand();
188 return command
&& command
->CanUndo();
191 bool wxCommandProcessor::CanRedo() const
193 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
196 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
199 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
205 void wxCommandProcessor::Initialize()
207 m_currentCommand
= m_commands
.Last();
211 void wxCommandProcessor::SetMenuStrings()
214 if (m_commandEditMenu
)
217 if (m_currentCommand
)
219 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
220 wxString
commandName(command
->GetName());
221 if (commandName
== wxT("")) commandName
= _("Unnamed command");
222 bool canUndo
= command
->CanUndo();
224 buf
= wxString(_("&Undo ")) + commandName
;
226 buf
= wxString(_("Can't &Undo ")) + commandName
;
228 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
229 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
231 // We can redo, if we're not at the end of the history.
232 if (m_currentCommand
->Next())
234 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
235 wxString
redoCommandName(redoCommand
->GetName());
236 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
237 buf
= wxString(_("&Redo ")) + redoCommandName
;
238 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
239 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
243 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
244 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
249 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
250 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
252 if (m_commands
.Number() == 0)
254 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
255 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
259 // currentCommand is NULL but there are commands: this means that
260 // we've undone to the start of the list, but can redo the first.
261 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
262 wxString
redoCommandName(redoCommand
->GetName());
263 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
264 buf
= wxString(_("&Redo ")) + redoCommandName
;
265 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
266 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
270 #endif // wxUSE_MENUS
273 void wxCommandProcessor::ClearCommands()
275 wxNode
*node
= m_commands
.First();
278 wxCommand
*command
= (wxCommand
*)node
->Data();
281 node
= m_commands
.First();
283 m_currentCommand
= (wxNode
*) NULL
;