]> git.saurik.com Git - wxWidgets.git/blame - src/common/cmdproc.cpp
Fixed the drawing of the HRules so they don't try to iterate over all
[wxWidgets.git] / src / common / cmdproc.cpp
CommitLineData
70050c82
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/cmdproc.cpp
3// Purpose: wxCommand and wxCommandProcessor classes
4// Author: Julian Smart (extracted from docview.h by VZ)
5// Modified by:
6// Created: 05.11.00
7// RCS-ID: $Id$
8// Copyright: (c) wxWindows team
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "cmdproc.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/string.h"
34 #include "wx/menu.h"
35#endif //WX_PRECOMP
36
37#include "wx/cmdproc.h"
38
39// ============================================================================
40// implementation
41// ============================================================================
42
43IMPLEMENT_CLASS(wxCommand, wxObject)
44IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor, wxObject)
45
46// ----------------------------------------------------------------------------
47// wxCommand
48// ----------------------------------------------------------------------------
49
50wxCommand::wxCommand(bool canUndoIt, const wxString& name)
51{
52 m_canUndo = canUndoIt;
53 m_commandName = name;
54}
55
56wxCommand::~wxCommand()
57{
58}
59
60// ----------------------------------------------------------------------------
61// Command processor
62// ----------------------------------------------------------------------------
63
64wxCommandProcessor::wxCommandProcessor(int maxCommands)
65{
66 m_maxNoCommands = maxCommands;
67 m_currentCommand = (wxNode *) NULL;
68#if wxUSE_MENUS
69 m_commandEditMenu = (wxMenu *) NULL;
70#endif // wxUSE_MENUS
71}
72
73wxCommandProcessor::~wxCommandProcessor()
74{
75 ClearCommands();
76}
77
78bool wxCommandProcessor::DoCommand(wxCommand& cmd)
79{
80 return cmd.Do();
81}
82
83bool wxCommandProcessor::UndoCommand(wxCommand& cmd)
84{
85 return cmd.Undo();
86}
87
88// Pass a command to the processor. The processor calls Do();
89// if successful, is appended to the command history unless
90// storeIt is FALSE.
91bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
92{
93 wxCHECK_MSG( command, FALSE, _T("no command in wxCommandProcessor::Submit") );
94
95 if ( !DoCommand(*command) )
96 return FALSE;
97
98 if ( storeIt )
99 Store(command);
100
101 return TRUE;
102}
103
104void wxCommandProcessor::Store(wxCommand *command)
105{
106 wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") );
107
108 if (m_commands.Number() == m_maxNoCommands)
109 {
110 wxNode *firstNode = m_commands.First();
111 wxCommand *firstCommand = (wxCommand *)firstNode->Data();
112 delete firstCommand;
113 delete firstNode;
114 }
115
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)
119 ClearCommands();
120 else
121 {
122 wxNode *node = m_currentCommand->Next();
123 while (node)
124 {
125 wxNode *next = node->Next();
126 delete (wxCommand *)node->Data();
127 delete node;
128 node = next;
129 }
130 }
131
132 m_commands.Append(command);
133 m_currentCommand = m_commands.Last();
134 SetMenuStrings();
135}
136
137bool wxCommandProcessor::Undo()
138{
139 wxCommand *command = GetCurrentCommand();
140 if ( command && command->CanUndo() )
141 {
142 if ( UndoCommand(*command) )
143 {
144 m_currentCommand = m_currentCommand->Previous();
145 SetMenuStrings();
146 return TRUE;
147 }
148 }
149
150 return FALSE;
151}
152
153bool wxCommandProcessor::Redo()
154{
155 wxCommand *redoCommand = (wxCommand *) NULL;
156 wxNode *redoNode = (wxNode *) NULL;
157 if (m_currentCommand && m_currentCommand->Next())
158 {
159 redoCommand = (wxCommand *)m_currentCommand->Next()->Data();
160 redoNode = m_currentCommand->Next();
161 }
162 else
163 {
164 if (m_commands.Number() > 0)
165 {
166 redoCommand = (wxCommand *)m_commands.First()->Data();
167 redoNode = m_commands.First();
168 }
169 }
170
171 if (redoCommand)
172 {
173 bool success = DoCommand(*redoCommand);
174 if (success)
175 {
176 m_currentCommand = redoNode;
177 SetMenuStrings();
178 return TRUE;
179 }
180 }
181 return FALSE;
182}
183
184bool wxCommandProcessor::CanUndo() const
185{
186 wxCommand *command = GetCurrentCommand();
187
188 return command && command->CanUndo();
189}
190
191bool wxCommandProcessor::CanRedo() const
192{
193 if ((m_currentCommand != (wxNode*) NULL) && (m_currentCommand->Next() == (wxNode*) NULL))
194 return FALSE;
195
196 if ((m_currentCommand != (wxNode*) NULL) && (m_currentCommand->Next() != (wxNode*) NULL))
197 return TRUE;
198
199 if ((m_currentCommand == (wxNode*) NULL) && (m_commands.Number() > 0))
200 return TRUE;
201
202 return FALSE;
203}
204
205void wxCommandProcessor::Initialize()
206{
207 m_currentCommand = m_commands.Last();
208 SetMenuStrings();
209}
210
211void wxCommandProcessor::SetMenuStrings()
212{
213#if wxUSE_MENUS
214 if (m_commandEditMenu)
215 {
216 wxString buf;
217 if (m_currentCommand)
218 {
219 wxCommand *command = (wxCommand *)m_currentCommand->Data();
220 wxString commandName(command->GetName());
221 if (commandName == wxT("")) commandName = _("Unnamed command");
222 bool canUndo = command->CanUndo();
223 if (canUndo)
224 buf = wxString(_("&Undo ")) + commandName;
225 else
226 buf = wxString(_("Can't &Undo ")) + commandName;
227
228 m_commandEditMenu->SetLabel(wxID_UNDO, buf);
229 m_commandEditMenu->Enable(wxID_UNDO, canUndo);
230
231 // We can redo, if we're not at the end of the history.
232 if (m_currentCommand->Next())
233 {
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);
240 }
241 else
242 {
243 m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
244 m_commandEditMenu->Enable(wxID_REDO, FALSE);
245 }
246 }
247 else
248 {
249 m_commandEditMenu->SetLabel(wxID_UNDO, _("&Undo"));
250 m_commandEditMenu->Enable(wxID_UNDO, FALSE);
251
252 if (m_commands.Number() == 0)
253 {
254 m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
255 m_commandEditMenu->Enable(wxID_REDO, FALSE);
256 }
257 else
258 {
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);
267 }
268 }
269 }
270#endif // wxUSE_MENUS
271}
272
273void wxCommandProcessor::ClearCommands()
274{
275 wxNode *node = m_commands.First();
276 while (node)
277 {
278 wxCommand *command = (wxCommand *)node->Data();
279 delete command;
280 delete node;
281 node = m_commands.First();
282 }
283 m_currentCommand = (wxNode *) NULL;
284}
285
286