]>
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
) )
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
;
158 if ( m_currentCommand
)
160 // is there anything to redo?
161 if ( m_currentCommand
->Next() )
163 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
164 redoNode
= m_currentCommand
->Next();
167 else // no current command, redo the first one
169 if (m_commands
.Number() > 0)
171 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
172 redoNode
= m_commands
.First();
178 bool success
= DoCommand(*redoCommand
);
181 m_currentCommand
= redoNode
;
189 bool wxCommandProcessor::CanUndo() const
191 wxCommand
*command
= GetCurrentCommand();
193 return command
&& command
->CanUndo();
196 bool wxCommandProcessor::CanRedo() const
198 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
201 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
204 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
210 void wxCommandProcessor::Initialize()
212 m_currentCommand
= m_commands
.Last();
216 void wxCommandProcessor::SetMenuStrings()
219 if (m_commandEditMenu
)
222 if (m_currentCommand
)
224 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
225 wxString
commandName(command
->GetName());
226 if (commandName
== wxT("")) commandName
= _("Unnamed command");
227 bool canUndo
= command
->CanUndo();
229 buf
= wxString(_("&Undo ")) + commandName
;
231 buf
= wxString(_("Can't &Undo ")) + commandName
;
233 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
234 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
236 // We can redo, if we're not at the end of the history.
237 if (m_currentCommand
->Next())
239 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
240 wxString
redoCommandName(redoCommand
->GetName());
241 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
242 buf
= wxString(_("&Redo ")) + redoCommandName
;
243 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
244 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
248 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
249 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
254 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
255 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
257 if (m_commands
.Number() == 0)
259 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
260 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
264 // currentCommand is NULL but there are commands: this means that
265 // we've undone to the start of the list, but can redo the first.
266 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
267 wxString
redoCommandName(redoCommand
->GetName());
268 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
269 buf
= wxString(_("&Redo ")) + redoCommandName
;
270 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
271 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
275 #endif // wxUSE_MENUS
278 void wxCommandProcessor::ClearCommands()
280 wxNode
*node
= m_commands
.First();
283 wxCommand
*command
= (wxCommand
*)node
->Data();
286 node
= m_commands
.First();
288 m_currentCommand
= (wxNode
*) NULL
;