]>
git.saurik.com Git - wxWidgets.git/blob - src/common/cmdproc.cpp
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
) )
97 // the user code expects the command to be deleted anyhow
109 void wxCommandProcessor::Store(wxCommand
*command
)
111 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
113 if (m_commands
.Number() == m_maxNoCommands
)
115 wxNode
*firstNode
= m_commands
.First();
116 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
121 // Correct a bug: we must chop off the current 'branch'
122 // so that we're at the end of the command list.
123 if (!m_currentCommand
)
127 wxNode
*node
= m_currentCommand
->Next();
130 wxNode
*next
= node
->Next();
131 delete (wxCommand
*)node
->Data();
137 m_commands
.Append(command
);
138 m_currentCommand
= m_commands
.Last();
142 bool wxCommandProcessor::Undo()
144 wxCommand
*command
= GetCurrentCommand();
145 if ( command
&& command
->CanUndo() )
147 if ( UndoCommand(*command
) )
149 m_currentCommand
= m_currentCommand
->Previous();
158 bool wxCommandProcessor::Redo()
160 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
161 wxNode
*redoNode
= (wxNode
*) NULL
;
163 if ( m_currentCommand
)
165 // is there anything to redo?
166 if ( m_currentCommand
->Next() )
168 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
169 redoNode
= m_currentCommand
->Next();
172 else // no current command, redo the first one
174 if (m_commands
.Number() > 0)
176 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
177 redoNode
= m_commands
.First();
183 bool success
= DoCommand(*redoCommand
);
186 m_currentCommand
= redoNode
;
194 bool wxCommandProcessor::CanUndo() const
196 wxCommand
*command
= GetCurrentCommand();
198 return command
&& command
->CanUndo();
201 bool wxCommandProcessor::CanRedo() const
203 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
206 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
209 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
215 void wxCommandProcessor::Initialize()
217 m_currentCommand
= m_commands
.Last();
221 void wxCommandProcessor::SetMenuStrings()
224 if (m_commandEditMenu
)
227 if (m_currentCommand
)
229 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
230 wxString
commandName(command
->GetName());
231 if (commandName
== wxT("")) commandName
= _("Unnamed command");
232 bool canUndo
= command
->CanUndo();
234 buf
= wxString(_("&Undo ")) + commandName
;
236 buf
= wxString(_("Can't &Undo ")) + commandName
;
238 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
239 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
241 // We can redo, if we're not at the end of the history.
242 if (m_currentCommand
->Next())
244 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
245 wxString
redoCommandName(redoCommand
->GetName());
246 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
247 buf
= wxString(_("&Redo ")) + redoCommandName
;
248 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
249 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
253 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
254 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
259 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
260 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
262 if (m_commands
.Number() == 0)
264 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
265 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
269 // currentCommand is NULL but there are commands: this means that
270 // we've undone to the start of the list, but can redo the first.
271 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
272 wxString
redoCommandName(redoCommand
->GetName());
273 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
274 buf
= wxString(_("&Redo ")) + redoCommandName
;
275 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
276 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
280 #endif // wxUSE_MENUS
283 void wxCommandProcessor::ClearCommands()
285 wxNode
*node
= m_commands
.First();
288 wxCommand
*command
= (wxCommand
*)node
->Data();
291 node
= m_commands
.First();
293 m_currentCommand
= (wxNode
*) NULL
;