]>
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
;
71 m_undoAccelerator
= wxT("\tCtrl+Z");
72 m_redoAccelerator
= wxT("\tCtrl+Y");
75 wxCommandProcessor::~wxCommandProcessor()
80 bool wxCommandProcessor::DoCommand(wxCommand
& cmd
)
85 bool wxCommandProcessor::UndoCommand(wxCommand
& cmd
)
90 // Pass a command to the processor. The processor calls Do();
91 // if successful, is appended to the command history unless
93 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
95 wxCHECK_MSG( command
, FALSE
, _T("no command in wxCommandProcessor::Submit") );
97 if ( !DoCommand(*command
) )
99 // the user code expects the command to be deleted anyhow
111 void wxCommandProcessor::Store(wxCommand
*command
)
113 wxCHECK_RET( command
, _T("no command in wxCommandProcessor::Store") );
115 if (m_commands
.Number() == m_maxNoCommands
)
117 wxNode
*firstNode
= m_commands
.First();
118 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
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 wxNode
*node
= m_currentCommand
->Next();
132 wxNode
*next
= node
->Next();
133 delete (wxCommand
*)node
->Data();
139 m_commands
.Append(command
);
140 m_currentCommand
= m_commands
.Last();
144 bool wxCommandProcessor::Undo()
146 wxCommand
*command
= GetCurrentCommand();
147 if ( command
&& command
->CanUndo() )
149 if ( UndoCommand(*command
) )
151 m_currentCommand
= m_currentCommand
->Previous();
160 bool wxCommandProcessor::Redo()
162 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
163 wxNode
*redoNode
= (wxNode
*) NULL
;
165 if ( m_currentCommand
)
167 // is there anything to redo?
168 if ( m_currentCommand
->Next() )
170 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
171 redoNode
= m_currentCommand
->Next();
174 else // no current command, redo the first one
176 if (m_commands
.Number() > 0)
178 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
179 redoNode
= m_commands
.First();
185 bool success
= DoCommand(*redoCommand
);
188 m_currentCommand
= redoNode
;
196 bool wxCommandProcessor::CanUndo() const
198 wxCommand
*command
= GetCurrentCommand();
200 return command
&& command
->CanUndo();
203 bool wxCommandProcessor::CanRedo() const
205 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
208 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
211 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
217 void wxCommandProcessor::Initialize()
219 m_currentCommand
= m_commands
.Last();
223 static void wxSetMenuLabel(wxMenu
* menu
, int id
, const wxString
& label
)
225 menu
->SetLabel(id
, label
);
228 void wxCommandProcessor::SetMenuStrings()
231 if (m_commandEditMenu
)
234 if (m_currentCommand
)
236 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
237 wxString
commandName(command
->GetName());
238 if (commandName
== wxT("")) commandName
= _("Unnamed command");
239 bool canUndo
= command
->CanUndo();
241 buf
= wxString(_("&Undo ")) + commandName
+ m_undoAccelerator
;
243 buf
= wxString(_("Can't &Undo ")) + commandName
+ m_undoAccelerator
;
245 wxSetMenuLabel(m_commandEditMenu
, wxID_UNDO
, buf
);
247 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
249 // We can redo, if we're not at the end of the history.
250 if (m_currentCommand
->Next())
252 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
253 wxString
redoCommandName(redoCommand
->GetName());
254 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
255 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
256 wxSetMenuLabel(m_commandEditMenu
, wxID_REDO
, buf
);
257 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
261 wxSetMenuLabel(m_commandEditMenu
, wxID_REDO
, _("&Redo") + m_redoAccelerator
);
262 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
267 wxSetMenuLabel(m_commandEditMenu
, wxID_UNDO
, _("&Undo") + m_undoAccelerator
);
268 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
270 if (m_commands
.Number() == 0)
272 wxSetMenuLabel(m_commandEditMenu
, wxID_REDO
, _("&Redo") + m_redoAccelerator
);
273 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
277 // currentCommand is NULL but there are commands: this means that
278 // we've undone to the start of the list, but can redo the first.
279 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
280 wxString
redoCommandName(redoCommand
->GetName());
281 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
282 buf
= wxString(_("&Redo ")) + redoCommandName
+ m_redoAccelerator
;
283 wxSetMenuLabel(m_commandEditMenu
, wxID_REDO
, buf
);
284 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
288 #endif // wxUSE_MENUS
291 void wxCommandProcessor::ClearCommands()
293 wxNode
*node
= m_commands
.First();
296 wxCommand
*command
= (wxCommand
*)node
->Data();
299 node
= m_commands
.First();
301 m_currentCommand
= (wxNode
*) NULL
;