]> git.saurik.com Git - wxWidgets.git/blame - src/common/cmdproc.cpp
Added wxPaintDCEx class, to handle the case where an HDC
[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;
70050c82
VZ
67#if wxUSE_MENUS
68 m_commandEditMenu = (wxMenu *) NULL;
69#endif // wxUSE_MENUS
6aa3ea88
JS
70 m_undoAccelerator = wxT("\tCtrl+Z");
71 m_redoAccelerator = wxT("\tCtrl+Y");
084db46c
MB
72#if !wxUSE_STL
73 m_currentCommand = NULL;
74#endif
70050c82
VZ
75}
76
77wxCommandProcessor::~wxCommandProcessor()
78{
79 ClearCommands();
80}
81
82bool wxCommandProcessor::DoCommand(wxCommand& cmd)
83{
84 return cmd.Do();
85}
86
87bool wxCommandProcessor::UndoCommand(wxCommand& cmd)
88{
89 return cmd.Undo();
90}
91
92// Pass a command to the processor. The processor calls Do();
93// if successful, is appended to the command history unless
94// storeIt is FALSE.
95bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
96{
97 wxCHECK_MSG( command, FALSE, _T("no command in wxCommandProcessor::Submit") );
98
99 if ( !DoCommand(*command) )
ca1b5af1
VZ
100 {
101 // the user code expects the command to be deleted anyhow
102 delete command;
103
70050c82 104 return FALSE;
ca1b5af1 105 }
70050c82
VZ
106
107 if ( storeIt )
108 Store(command);
109
110 return TRUE;
111}
112
113void wxCommandProcessor::Store(wxCommand *command)
114{
115 wxCHECK_RET( command, _T("no command in wxCommandProcessor::Store") );
116
b1d4dd7a 117 if ( (int)m_commands.GetCount() == m_maxNoCommands )
70050c82 118 {
222ed1d6 119 wxList::compatibility_iterator firstNode = m_commands.GetFirst();
b1d4dd7a 120 wxCommand *firstCommand = (wxCommand *)firstNode->GetData();
70050c82 121 delete firstCommand;
222ed1d6 122 m_commands.Erase(firstNode);
70050c82
VZ
123 }
124
125 // Correct a bug: we must chop off the current 'branch'
126 // so that we're at the end of the command list.
127 if (!m_currentCommand)
128 ClearCommands();
129 else
130 {
222ed1d6 131 wxList::compatibility_iterator node = m_currentCommand->GetNext();
70050c82
VZ
132 while (node)
133 {
222ed1d6 134 wxList::compatibility_iterator next = node->GetNext();
b1d4dd7a 135 delete (wxCommand *)node->GetData();
222ed1d6 136 m_commands.Erase(node);
70050c82
VZ
137 node = next;
138 }
139 }
140
141 m_commands.Append(command);
b1d4dd7a 142 m_currentCommand = m_commands.GetLast();
70050c82
VZ
143 SetMenuStrings();
144}
145
146bool wxCommandProcessor::Undo()
147{
148 wxCommand *command = GetCurrentCommand();
149 if ( command && command->CanUndo() )
150 {
151 if ( UndoCommand(*command) )
152 {
b1d4dd7a 153 m_currentCommand = m_currentCommand->GetPrevious();
70050c82
VZ
154 SetMenuStrings();
155 return TRUE;
156 }
157 }
158
159 return FALSE;
160}
161
162bool wxCommandProcessor::Redo()
163{
164 wxCommand *redoCommand = (wxCommand *) NULL;
4ed40684
VZ
165 wxList::compatibility_iterator redoNode
166#if !wxUSE_STL
167 = NULL // just to avoid warnings
168#endif // !wxUSE_STL
169 ;
17271c79
VZ
170
171 if ( m_currentCommand )
70050c82 172 {
17271c79 173 // is there anything to redo?
b1d4dd7a 174 if ( m_currentCommand->GetNext() )
17271c79 175 {
b1d4dd7a
RL
176 redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
177 redoNode = m_currentCommand->GetNext();
17271c79 178 }
70050c82 179 }
17271c79 180 else // no current command, redo the first one
70050c82 181 {
b1d4dd7a 182 if (m_commands.GetCount() > 0)
70050c82 183 {
b1d4dd7a
RL
184 redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
185 redoNode = m_commands.GetFirst();
70050c82
VZ
186 }
187 }
188
189 if (redoCommand)
190 {
191 bool success = DoCommand(*redoCommand);
192 if (success)
193 {
194 m_currentCommand = redoNode;
195 SetMenuStrings();
196 return TRUE;
197 }
198 }
199 return FALSE;
200}
201
202bool wxCommandProcessor::CanUndo() const
203{
204 wxCommand *command = GetCurrentCommand();
205
206 return command && command->CanUndo();
207}
208
209bool wxCommandProcessor::CanRedo() const
210{
222ed1d6 211 if (m_currentCommand && !m_currentCommand->GetNext())
70050c82
VZ
212 return FALSE;
213
222ed1d6 214 if (m_currentCommand && m_currentCommand->GetNext())
70050c82
VZ
215 return TRUE;
216
222ed1d6 217 if (!m_currentCommand && (m_commands.GetCount() > 0))
70050c82
VZ
218 return TRUE;
219
220 return FALSE;
221}
222
223void wxCommandProcessor::Initialize()
224{
b1d4dd7a 225 m_currentCommand = m_commands.GetLast();
70050c82
VZ
226 SetMenuStrings();
227}
228
229void wxCommandProcessor::SetMenuStrings()
230{
231#if wxUSE_MENUS
232 if (m_commandEditMenu)
233 {
d863ed83
JS
234 wxString undoLabel = GetUndoMenuLabel();
235 wxString redoLabel = GetRedoMenuLabel();
236
237 m_commandEditMenu->SetLabel(wxID_UNDO, undoLabel);
238 m_commandEditMenu->Enable(wxID_UNDO, CanUndo());
239
240 m_commandEditMenu->SetLabel(wxID_REDO, redoLabel);
241 m_commandEditMenu->Enable(wxID_REDO, CanRedo());
242 }
243#endif // wxUSE_MENUS
244}
245
246// Gets the current Undo menu label.
247wxString wxCommandProcessor::GetUndoMenuLabel() const
248{
249 wxString buf;
250 if (m_currentCommand)
251 {
b1d4dd7a 252 wxCommand *command = (wxCommand *)m_currentCommand->GetData();
d863ed83
JS
253 wxString commandName(command->GetName());
254 if (commandName == wxT("")) commandName = _("Unnamed command");
255 bool canUndo = command->CanUndo();
256 if (canUndo)
257 buf = wxString(_("&Undo ")) + commandName + m_undoAccelerator;
258 else
259 buf = wxString(_("Can't &Undo ")) + commandName + m_undoAccelerator;
260 }
261 else
262 {
263 buf = _("&Undo") + m_undoAccelerator;
264 }
265
266 return buf;
267}
268
269// Gets the current Undo menu label.
270wxString wxCommandProcessor::GetRedoMenuLabel() const
271{
272 wxString buf;
273 if (m_currentCommand)
274 {
275 // We can redo, if we're not at the end of the history.
b1d4dd7a 276 if (m_currentCommand->GetNext())
70050c82 277 {
b1d4dd7a 278 wxCommand *redoCommand = (wxCommand *)m_currentCommand->GetNext()->GetData();
d863ed83
JS
279 wxString redoCommandName(redoCommand->GetName());
280 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
281 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
70050c82
VZ
282 }
283 else
284 {
d863ed83 285 buf = _("&Redo") + m_redoAccelerator;
70050c82
VZ
286 }
287 }
d863ed83
JS
288 else
289 {
b1d4dd7a 290 if (m_commands.GetCount() == 0)
d863ed83
JS
291 {
292 buf = _("&Redo") + m_redoAccelerator;
293 }
294 else
295 {
296 // currentCommand is NULL but there are commands: this means that
297 // we've undone to the start of the list, but can redo the first.
b1d4dd7a 298 wxCommand *redoCommand = (wxCommand *)m_commands.GetFirst()->GetData();
d863ed83
JS
299 wxString redoCommandName(redoCommand->GetName());
300 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
301 buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
302 }
303 }
304 return buf;
70050c82
VZ
305}
306
307void wxCommandProcessor::ClearCommands()
308{
222ed1d6 309 wxList::compatibility_iterator node = m_commands.GetFirst();
70050c82
VZ
310 while (node)
311 {
b1d4dd7a 312 wxCommand *command = (wxCommand *)node->GetData();
70050c82 313 delete command;
222ed1d6 314 m_commands.Erase(node);
b1d4dd7a 315 node = m_commands.GetFirst();
70050c82 316 }
222ed1d6 317 m_currentCommand = wxList::compatibility_iterator();
70050c82
VZ
318}
319
320