OpenASIP  2.0
ConsoleWindow.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ConsoleWindow.cc
26  *
27  * Implementation of ConsoleWindow class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2005 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <wx/textctrl.h>
34 #include <wx/valtext.h>
35 #include "ConsoleWindow.hh"
36 #include "ProximLineReader.hh"
37 #include "WxConversion.hh"
38 #include "Proxim.hh"
40 
41 BEGIN_EVENT_TABLE(ConsoleWindow, ProximSimulatorWindow)
42  EVT_TEXT_ENTER(ID_INPUT, ConsoleWindow::textEntered)
49 
50 BEGIN_EVENT_TABLE(ConsoleWindow::ConsoleWindowInputCtrl, wxTextCtrl)
51  EVT_KEY_DOWN(ConsoleWindowInputCtrl::onKeyDown)
53 
54 
55 //-----------------------------------------------------------------------------
56 //
57 // ConsoleWindowInputCtrl
58 //
59 //-----------------------------------------------------------------------------
60 
61 /**
62  * The Constructor.
63  *
64  * @param console Pointer to the parent console of the input widget.
65  */
67  ConsoleWindow* console) :
68  wxTextCtrl(console, ID_INPUT, _T(""), wxDefaultPosition,
69  wxSize(500,30), wxTE_PROCESS_ENTER),
70  console_(console) {
71 
72 }
73 
74 
75 /**
76  * An event handler for the input control key events.
77  *
78  * Events are passed to the parent console's event handler.
79  *
80  * @param event Key press event to handle.
81  */
82 void
84  console_->onInputKey(event);
85 }
86 
87 
88 //-----------------------------------------------------------------------------
89 //
90 // ConsoleWindow
91 //
92 //-----------------------------------------------------------------------------
93 
94 
95 /**
96  * The Constructor.
97  *
98  * @param parent Parent window of the console window.
99  * @param id Window identifier.
100  */
102  ProximMainFrame* parent,
103  wxWindowID id) :
104  ProximSimulatorWindow(parent, id),
105  outTextCtrl_(NULL),
106  inTextCtrl_(NULL),
107  historyIterator_(-1) {
108 
109  createContents();
110  lineReader_ = &wxGetApp().simulation()->lineReader();
111 }
112 
113 
114 /**
115  * The Destructor.
116  */
118 }
119 
120 /**
121  * Called when the simulator program, machine and memory models are deleted.
122  */
123 void
125  // Do nothing.
126 }
127 
128 
129 /**
130  * Event handler for the user input to the text input widget.
131  *
132  * The text is passed to the linereader as input.
133  */
134 void
135 ConsoleWindow::textEntered(wxCommandEvent&) {
136  std::string command = WxConversion::toString(inTextCtrl_->GetLineText(0));
137  lineReader_->input(command);
138  historyIterator_ = -1;
139  inTextCtrl_->Clear();
140 }
141 
142 
143 
144 /**
145  * Appends text to the output window.
146  *
147  * @param text Text to append to the output window.
148  */
149 void
150 ConsoleWindow::write(std::string text) {
151 
152  if (text == "") {
153  return;
154  }
155  wxString output = WxConversion::toWxString(text);
156  outTextCtrl_->AppendText(output);
157 }
158 
159 
160 /**
161  * Creates the window widgets.
162  */
163 void
165 
166  // output widget
167  outTextCtrl_ = new wxTextCtrl(
168  this, ID_OUTPUT, _T(""), wxDefaultPosition, wxDefaultSize,
169  wxTE_MULTILINE | wxTE_READONLY);
170 
171  // input widget
173 
174  // Sizer responsible for the window layout.
175  wxFlexGridSizer* sizer = new wxFlexGridSizer(1, 0, 0);
176  sizer->AddGrowableCol(0);
177  sizer->AddGrowableRow(0);
178 
179  sizer->Add(outTextCtrl_, 0, wxGROW | wxALL, 5);
180  sizer->Add(inTextCtrl_, 0, wxGROW | wxALIGN_CENTER_VERTICAL | wxALL, 5);
181  SetSizer(sizer);
182  sizer->SetSizeHints(this);
183 }
184 
185 
186 /**
187  * Handles simulator text output events.
188  *
189  * The event data contains the simulator interpreter output, which is appended
190  * to the text output widget.
191  *
192  * @param event Simulator interpreter text output event.
193  */
194 void
196  // Append text to the output widget.
197  std::string text = event.data();
198  outTextCtrl_->AppendText(WxConversion::toWxString(text));
199  outTextCtrl_->ShowPosition(outTextCtrl_->GetLastPosition());
200 }
201 
202 
203 /**
204  * Handles simulator error events.
205  *
206  * The error text is printed in the console.
207  *
208  * @param event Simulator interpreter command event.
209  */
210 void
212  std::string error = event.data();
213  wxTextAttr oldStyle = outTextCtrl_->GetDefaultStyle();
214  outTextCtrl_->SetDefaultStyle(wxTextAttr(*wxRED));
215  outTextCtrl_->AppendText(WxConversion::toWxString("\n" + error + "\n"));
216  outTextCtrl_->SetDefaultStyle(oldStyle);
217 }
218 
219 
220 /**
221  * Handles special keypresses in the input widget.
222  *
223  * @param event Keypress event to handle.
224  */
225 void
226 ConsoleWindow::onInputKey(wxKeyEvent& event) {
227 
228  if (event.GetKeyCode() == WXK_UP) {
229  // Up key. Browses the command history backward.
233  return;
234  }
235  } else if (event.GetKeyCode() == WXK_DOWN) {
236  // Down key. Browses the command history forward.
237  if (historyIterator_ < 0) {
238  return;
239  }
241  } else {
242  // Key with no special fucntion. The key event is skipped.
243  event.Skip();
244  return;
245  }
246 
247  // If the command history is browsed back to top, the
248  // input widget is cleared.
249  if (historyIterator_ < 0) {
250  inTextCtrl_->Clear();
251  return;
252  }
253 
254  // Command browsed from the command history is set as the input
255  // widget value.
256  wxString command = WxConversion::toWxString(
258 
259  inTextCtrl_->SetValue(command);
260  inTextCtrl_->SetInsertionPointEnd();
261 }
262 
263 
264 
265 /**
266  * Clears text from the output text widget.
267  */
268 void
270  outTextCtrl_->Clear();
271 }
272 
273 /**
274  * An event handler for the event of the simulator to start processing
275  * a command.
276  *
277  * The Console window must stay enabled when the simulator is busy, so this
278  * method is overloaded to not to lock the window.
279  */
280 void
282  // Do nothing.
283 }
284 
285 /**
286  * An event handler for the event of the simulator to complete command
287  * processing.
288  *
289  * The Console window is not locked during simulator processing, so there's
290  * no need to do anything.
291  */
292 void
294  // Do nothing.
295 }
ProximMainFrame
Definition: ProximMainFrame.hh:58
ConsoleWindow::~ConsoleWindow
virtual ~ConsoleWindow()
Definition: ConsoleWindow.cc:117
ConsoleWindow::reset
virtual void reset()
Definition: ConsoleWindow.cc:124
WxConversion::toWxString
static wxString toWxString(const std::string &source)
ConsoleWindow::onSimulatorBusy
virtual void onSimulatorBusy(SimulatorEvent &event)
Definition: ConsoleWindow.cc:281
EVT_SIMULATOR_RUNTIME_ERROR
#define EVT_SIMULATOR_RUNTIME_ERROR(id, fn)
Definition: SimulatorEvent.hh:169
ConsoleWindow::lineReader_
ProximLineReader * lineReader_
Linereader to send the user input to.
Definition: ConsoleWindow.hh:73
ConsoleWindow::clear
void clear()
Definition: ConsoleWindow.cc:269
ConsoleWindow::ID_OUTPUT
@ ID_OUTPUT
Definition: ConsoleWindow.hh:80
ProximLineReader.hh
ProximSimulationThread.hh
Proxim.hh
ProximLineReader::input
void input(std::string command)
Definition: ProximLineReader.cc:131
ConsoleWindow::ConsoleWindowInputCtrl::console_
ConsoleWindow * console_
Parent console of the widget.
Definition: ConsoleWindow.hh:97
ConsoleWindow::onSimulatorDone
virtual void onSimulatorDone(SimulatorEvent &event)
Definition: ConsoleWindow.cc:293
ProximSimulatorWindow
Definition: ProximSimulatorWindow.hh:47
EVT_SIMULATOR_COMMAND
#define EVT_SIMULATOR_COMMAND(id, fn)
Definition: SimulatorEvent.hh:120
EVT_SIMULATOR_OUTPUT
#define EVT_SIMULATOR_OUTPUT(id, fn)
Definition: SimulatorEvent.hh:113
LineReader::inputsInHistory
virtual std::size_t inputsInHistory() const
Definition: LineReader.cc:150
ConsoleWindow.hh
ConsoleWindow::onInputKey
void onInputKey(wxKeyEvent &event)
Definition: ConsoleWindow.cc:226
EVT_SIMULATOR_ERROR
#define EVT_SIMULATOR_ERROR(id, fn)
Definition: SimulatorEvent.hh:134
LineReader::inputHistoryEntry
virtual std::string inputHistoryEntry(std::size_t age) const
Definition: LineReader.cc:165
ConsoleWindow::onSimulatorOutput
void onSimulatorOutput(const SimulatorEvent &event)
Definition: ConsoleWindow.cc:195
ConsoleWindow::historyIterator_
int historyIterator_
Stores the command history iterator, used for command history browsing.
Definition: ConsoleWindow.hh:75
ConsoleWindow::inTextCtrl_
wxTextCtrl * inTextCtrl_
Input text control.
Definition: ConsoleWindow.hh:71
ConsoleWindow::ConsoleWindowInputCtrl
Definition: ConsoleWindow.hh:90
ConsoleWindow::outTextCtrl_
wxTextCtrl * outTextCtrl_
Output text control.
Definition: ConsoleWindow.hh:69
ConsoleWindow::textEntered
void textEntered(wxCommandEvent &event)
Definition: ConsoleWindow.cc:135
EVT_SIMULATOR_RUNTIME_WARNING
#define EVT_SIMULATOR_RUNTIME_WARNING(id, fn)
Definition: SimulatorEvent.hh:176
ConsoleWindow::onError
void onError(const SimulatorEvent &event)
Definition: ConsoleWindow.cc:211
ConsoleWindow::ConsoleWindowInputCtrl::onKeyDown
void onKeyDown(wxKeyEvent &event)
Definition: ConsoleWindow.cc:83
WxConversion.hh
SimulatorEvent
Definition: SimulatorEvent.hh:42
WxConversion::toString
static std::string toString(const wxString &source)
ConsoleWindow::write
void write(std::string text)
Definition: ConsoleWindow.cc:150
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
ConsoleWindow::ConsoleWindow
ConsoleWindow(ProximMainFrame *parent, wxWindowID id)
Definition: ConsoleWindow.cc:101
ConsoleWindow::createContents
void createContents()
Definition: ConsoleWindow.cc:164
ConsoleWindow
Definition: ConsoleWindow.hh:51