]>
Commit | Line | Data |
---|---|---|
69c33c6c VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: exec.cpp | |
3 | // Purpose: exec sample demonstrates wxExecute and related functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 15.01.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
788233da | 20 | #if defined(__GNUG__) && !defined(__APPLE__) |
11fdee42 WS |
21 | #pragma implementation |
22 | #pragma interface | |
69c33c6c VZ |
23 | #endif |
24 | ||
25 | // For compilers that support precompilation, includes "wx/wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 33 | // need because it includes almost all "standard" wxWidgets headers |
69c33c6c VZ |
34 | #ifndef WX_PRECOMP |
35 | #include "wx/app.h" | |
788233da | 36 | #include "wx/log.h" |
69c33c6c | 37 | #include "wx/frame.h" |
92a2a7eb VZ |
38 | #include "wx/panel.h" |
39 | ||
40 | #include "wx/timer.h" | |
eb671557 | 41 | |
69c33c6c | 42 | #include "wx/utils.h" |
a8f0faf3 | 43 | #include "wx/menu.h" |
eb671557 | 44 | |
a8f0faf3 GRG |
45 | #include "wx/msgdlg.h" |
46 | #include "wx/textdlg.h" | |
8ce88dfa | 47 | #include "wx/filedlg.h" |
50567b69 | 48 | #include "wx/choicdlg.h" |
eb671557 VZ |
49 | |
50 | #include "wx/button.h" | |
51 | #include "wx/textctrl.h" | |
52 | #include "wx/listbox.h" | |
53 | ||
54 | #include "wx/sizer.h" | |
69c33c6c VZ |
55 | #endif |
56 | ||
d8e41d42 | 57 | #include "wx/txtstrm.h" |
e4f3eb42 | 58 | #include "wx/numdlg.h" |
1a278e7b | 59 | #include "wx/ffile.h" |
d8e41d42 | 60 | |
69c33c6c VZ |
61 | #include "wx/process.h" |
62 | ||
6ba63600 VZ |
63 | #include "wx/mimetype.h" |
64 | ||
d93c719a VZ |
65 | #ifdef __WINDOWS__ |
66 | #include "wx/dde.h" | |
67 | #endif // __WINDOWS__ | |
68 | ||
69c33c6c | 69 | // ---------------------------------------------------------------------------- |
eb671557 | 70 | // the usual application and main frame classes |
69c33c6c VZ |
71 | // ---------------------------------------------------------------------------- |
72 | ||
73 | // Define a new application type, each program should derive a class from wxApp | |
74 | class MyApp : public wxApp | |
75 | { | |
76 | public: | |
77 | // override base class virtuals | |
78 | // ---------------------------- | |
79 | ||
80 | // this one is called on application startup and is a good place for the app | |
81 | // initialization (doing it here and not in the ctor allows to have an error | |
82 | // return: if OnInit() returns false, the application terminates) | |
83 | virtual bool OnInit(); | |
84 | }; | |
85 | ||
cd6ce4a9 VZ |
86 | // Define an array of process pointers used by MyFrame |
87 | class MyPipedProcess; | |
33eab530 | 88 | WX_DEFINE_ARRAY_PTR(MyPipedProcess *, MyProcessesArray); |
cd6ce4a9 | 89 | |
69c33c6c VZ |
90 | // Define a new frame type: this is going to be our main frame |
91 | class MyFrame : public wxFrame | |
92 | { | |
93 | public: | |
94 | // ctor(s) | |
95 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
96 | ||
97 | // event handlers (these functions should _not_ be virtual) | |
98 | void OnQuit(wxCommandEvent& event); | |
99 | ||
50567b69 VZ |
100 | void OnKill(wxCommandEvent& event); |
101 | ||
d8e41d42 VZ |
102 | void OnClear(wxCommandEvent& event); |
103 | ||
69c33c6c VZ |
104 | void OnSyncExec(wxCommandEvent& event); |
105 | void OnAsyncExec(wxCommandEvent& event); | |
106 | void OnShell(wxCommandEvent& event); | |
d8e41d42 | 107 | void OnExecWithRedirect(wxCommandEvent& event); |
f6bcfd97 BP |
108 | void OnExecWithPipe(wxCommandEvent& event); |
109 | ||
eb671557 VZ |
110 | void OnPOpen(wxCommandEvent& event); |
111 | ||
6ba63600 VZ |
112 | void OnFileExec(wxCommandEvent& event); |
113 | ||
69c33c6c VZ |
114 | void OnAbout(wxCommandEvent& event); |
115 | ||
cd6ce4a9 | 116 | // polling output of async processes |
92a2a7eb | 117 | void OnTimer(wxTimerEvent& event); |
cd6ce4a9 VZ |
118 | void OnIdle(wxIdleEvent& event); |
119 | ||
d8e41d42 | 120 | // for MyPipedProcess |
f6bcfd97 | 121 | void OnProcessTerminated(MyPipedProcess *process); |
d8e41d42 VZ |
122 | wxListBox *GetLogListBox() const { return m_lbox; } |
123 | ||
69c33c6c | 124 | private: |
f6bcfd97 BP |
125 | void ShowOutput(const wxString& cmd, |
126 | const wxArrayString& output, | |
127 | const wxString& title); | |
128 | ||
6ba63600 VZ |
129 | void DoAsyncExec(const wxString& cmd); |
130 | ||
92a2a7eb VZ |
131 | void AddAsyncProcess(MyPipedProcess *process) |
132 | { | |
133 | if ( m_running.IsEmpty() ) | |
134 | { | |
135 | // we want to start getting the timer events to ensure that a | |
136 | // steady stream of idle events comes in -- otherwise we | |
137 | // wouldn't be able to poll the child process input | |
138 | m_timerIdleWakeUp.Start(100); | |
139 | } | |
140 | //else: the timer is already running | |
141 | ||
142 | m_running.Add(process); | |
143 | } | |
144 | ||
145 | void RemoveAsyncProcess(MyPipedProcess *process) | |
146 | { | |
147 | m_running.Remove(process); | |
148 | ||
149 | if ( m_running.IsEmpty() ) | |
150 | { | |
151 | // we don't need to get idle events all the time any more | |
152 | m_timerIdleWakeUp.Stop(); | |
153 | } | |
154 | } | |
155 | ||
50567b69 | 156 | // the PID of the last process we launched asynchronously |
aec18ff7 | 157 | long m_pidLast; |
50567b69 | 158 | |
ca289436 | 159 | // last command we executed |
69c33c6c VZ |
160 | wxString m_cmdLast; |
161 | ||
ca289436 VZ |
162 | #ifdef __WINDOWS__ |
163 | void OnDDEExec(wxCommandEvent& event); | |
164 | void OnDDERequest(wxCommandEvent& event); | |
165 | ||
166 | bool GetDDEServer(); | |
167 | ||
168 | // last params of a DDE transaction | |
169 | wxString m_server, | |
170 | m_topic, | |
171 | m_cmdDde; | |
172 | #endif // __WINDOWS__ | |
173 | ||
d8e41d42 VZ |
174 | wxListBox *m_lbox; |
175 | ||
cd6ce4a9 VZ |
176 | MyProcessesArray m_running; |
177 | ||
92a2a7eb VZ |
178 | // the idle event wake up timer |
179 | wxTimer m_timerIdleWakeUp; | |
180 | ||
be5a51fb | 181 | // any class wishing to process wxWidgets events must use this macro |
69c33c6c VZ |
182 | DECLARE_EVENT_TABLE() |
183 | }; | |
184 | ||
eb671557 VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // MyPipeFrame: allows the user to communicate with the child process | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | class MyPipeFrame : public wxFrame | |
190 | { | |
191 | public: | |
192 | MyPipeFrame(wxFrame *parent, | |
193 | const wxString& cmd, | |
194 | wxProcess *process); | |
195 | ||
196 | protected: | |
a0b08655 VZ |
197 | void OnTextEnter(wxCommandEvent& WXUNUSED(event)) { DoSend(); } |
198 | void OnBtnSend(wxCommandEvent& WXUNUSED(event)) { DoSend(); } | |
1a278e7b | 199 | void OnBtnSendFile(wxCommandEvent& WXUNUSED(event)); |
a0b08655 | 200 | void OnBtnGet(wxCommandEvent& WXUNUSED(event)) { DoGet(); } |
1a278e7b | 201 | void OnBtnClose(wxCommandEvent& WXUNUSED(event)) { DoClose(); } |
eb671557 VZ |
202 | |
203 | void OnClose(wxCloseEvent& event); | |
204 | ||
7ca528cb VZ |
205 | void OnProcessTerm(wxProcessEvent& event); |
206 | ||
207 | void DoSend() | |
208 | { | |
1a278e7b VZ |
209 | wxString s(m_textOut->GetValue()); |
210 | s += _T('\n'); | |
211 | m_out.Write(s.c_str(), s.length()); | |
212 | m_textOut->Clear(); | |
7ca528cb VZ |
213 | |
214 | DoGet(); | |
215 | } | |
216 | ||
eb671557 | 217 | void DoGet(); |
1a278e7b | 218 | void DoClose(); |
eb671557 VZ |
219 | |
220 | private: | |
1a278e7b VZ |
221 | void DoGetFromStream(wxTextCtrl *text, wxInputStream& in); |
222 | void DisableInput(); | |
223 | void DisableOutput(); | |
224 | ||
225 | ||
eb671557 VZ |
226 | wxProcess *m_process; |
227 | ||
1a278e7b VZ |
228 | wxOutputStream &m_out; |
229 | wxInputStream &m_in, | |
230 | &m_err; | |
eb671557 | 231 | |
1a278e7b VZ |
232 | wxTextCtrl *m_textOut, |
233 | *m_textIn, | |
234 | *m_textErr; | |
eb671557 VZ |
235 | |
236 | DECLARE_EVENT_TABLE() | |
237 | }; | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // wxProcess-derived classes | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
69c33c6c VZ |
243 | // This is the handler for process termination events |
244 | class MyProcess : public wxProcess | |
245 | { | |
246 | public: | |
d8e41d42 | 247 | MyProcess(MyFrame *parent, const wxString& cmd) |
69c33c6c VZ |
248 | : wxProcess(parent), m_cmd(cmd) |
249 | { | |
250 | m_parent = parent; | |
251 | } | |
252 | ||
253 | // instead of overriding this virtual function we might as well process the | |
254 | // event from it in the frame class - this might be more convenient in some | |
255 | // cases | |
256 | virtual void OnTerminate(int pid, int status); | |
257 | ||
d8e41d42 VZ |
258 | protected: |
259 | MyFrame *m_parent; | |
69c33c6c VZ |
260 | wxString m_cmd; |
261 | }; | |
262 | ||
d8e41d42 VZ |
263 | // A specialization of MyProcess for redirecting the output |
264 | class MyPipedProcess : public MyProcess | |
265 | { | |
266 | public: | |
267 | MyPipedProcess(MyFrame *parent, const wxString& cmd) | |
268 | : MyProcess(parent, cmd) | |
269 | { | |
cd6ce4a9 | 270 | Redirect(); |
d8e41d42 VZ |
271 | } |
272 | ||
273 | virtual void OnTerminate(int pid, int status); | |
cd6ce4a9 | 274 | |
f6bcfd97 BP |
275 | virtual bool HasInput(); |
276 | }; | |
277 | ||
278 | // A version of MyPipedProcess which also sends input to the stdin of the | |
279 | // child process | |
280 | class MyPipedProcess2 : public MyPipedProcess | |
281 | { | |
282 | public: | |
283 | MyPipedProcess2(MyFrame *parent, const wxString& cmd, const wxString& input) | |
284 | : MyPipedProcess(parent, cmd), m_input(input) | |
285 | { | |
286 | } | |
287 | ||
288 | virtual bool HasInput(); | |
289 | ||
290 | private: | |
291 | wxString m_input; | |
d8e41d42 VZ |
292 | }; |
293 | ||
69c33c6c VZ |
294 | // ---------------------------------------------------------------------------- |
295 | // constants | |
296 | // ---------------------------------------------------------------------------- | |
297 | ||
298 | // IDs for the controls and the menu commands | |
299 | enum | |
300 | { | |
301 | // menu items | |
302 | Exec_Quit = 100, | |
50567b69 | 303 | Exec_Kill, |
d8e41d42 | 304 | Exec_ClearLog, |
69c33c6c VZ |
305 | Exec_SyncExec = 200, |
306 | Exec_AsyncExec, | |
307 | Exec_Shell, | |
eb671557 | 308 | Exec_POpen, |
6ba63600 | 309 | Exec_OpenFile, |
d93c719a | 310 | Exec_DDEExec, |
ca289436 | 311 | Exec_DDERequest, |
d8e41d42 | 312 | Exec_Redirect, |
f6bcfd97 | 313 | Exec_Pipe, |
eb671557 VZ |
314 | Exec_About = 300, |
315 | ||
316 | // control ids | |
317 | Exec_Btn_Send = 1000, | |
1a278e7b VZ |
318 | Exec_Btn_SendFile, |
319 | Exec_Btn_Get, | |
320 | Exec_Btn_Close | |
69c33c6c VZ |
321 | }; |
322 | ||
d93c719a VZ |
323 | static const wxChar *DIALOG_TITLE = _T("Exec sample"); |
324 | ||
69c33c6c | 325 | // ---------------------------------------------------------------------------- |
be5a51fb | 326 | // event tables and other macros for wxWidgets |
69c33c6c VZ |
327 | // ---------------------------------------------------------------------------- |
328 | ||
be5a51fb | 329 | // the event tables connect the wxWidgets events with the functions (event |
69c33c6c VZ |
330 | // handlers) which process them. It can be also done at run-time, but for the |
331 | // simple menu events like this the static method is much simpler. | |
332 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
333 | EVT_MENU(Exec_Quit, MyFrame::OnQuit) | |
50567b69 | 334 | EVT_MENU(Exec_Kill, MyFrame::OnKill) |
d8e41d42 | 335 | EVT_MENU(Exec_ClearLog, MyFrame::OnClear) |
69c33c6c VZ |
336 | |
337 | EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec) | |
338 | EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec) | |
339 | EVT_MENU(Exec_Shell, MyFrame::OnShell) | |
d8e41d42 | 340 | EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect) |
f6bcfd97 BP |
341 | EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe) |
342 | ||
eb671557 VZ |
343 | EVT_MENU(Exec_POpen, MyFrame::OnPOpen) |
344 | ||
6ba63600 VZ |
345 | EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec) |
346 | ||
5af4b77f | 347 | #ifdef __WINDOWS__ |
d93c719a | 348 | EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec) |
ca289436 | 349 | EVT_MENU(Exec_DDERequest, MyFrame::OnDDERequest) |
5af4b77f | 350 | #endif // __WINDOWS__ |
eb671557 | 351 | |
69c33c6c | 352 | EVT_MENU(Exec_About, MyFrame::OnAbout) |
cd6ce4a9 VZ |
353 | |
354 | EVT_IDLE(MyFrame::OnIdle) | |
92a2a7eb | 355 | |
07850a49 | 356 | EVT_TIMER(wxID_ANY, MyFrame::OnTimer) |
69c33c6c VZ |
357 | END_EVENT_TABLE() |
358 | ||
eb671557 VZ |
359 | BEGIN_EVENT_TABLE(MyPipeFrame, wxFrame) |
360 | EVT_BUTTON(Exec_Btn_Send, MyPipeFrame::OnBtnSend) | |
1a278e7b | 361 | EVT_BUTTON(Exec_Btn_SendFile, MyPipeFrame::OnBtnSendFile) |
eb671557 | 362 | EVT_BUTTON(Exec_Btn_Get, MyPipeFrame::OnBtnGet) |
1a278e7b | 363 | EVT_BUTTON(Exec_Btn_Close, MyPipeFrame::OnBtnClose) |
eb671557 | 364 | |
07850a49 | 365 | EVT_TEXT_ENTER(wxID_ANY, MyPipeFrame::OnTextEnter) |
eb671557 VZ |
366 | |
367 | EVT_CLOSE(MyPipeFrame::OnClose) | |
7ca528cb | 368 | |
07850a49 | 369 | EVT_END_PROCESS(wxID_ANY, MyPipeFrame::OnProcessTerm) |
eb671557 VZ |
370 | END_EVENT_TABLE() |
371 | ||
be5a51fb | 372 | // Create a new application object: this macro will allow wxWidgets to create |
69c33c6c VZ |
373 | // the application object during program execution (it's better than using a |
374 | // static object for many reasons) and also declares the accessor function | |
375 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
376 | // not wxApp) | |
377 | IMPLEMENT_APP(MyApp) | |
378 | ||
379 | // ============================================================================ | |
380 | // implementation | |
381 | // ============================================================================ | |
382 | ||
383 | // ---------------------------------------------------------------------------- | |
384 | // the application class | |
385 | // ---------------------------------------------------------------------------- | |
386 | ||
387 | // `Main program' equivalent: the program execution "starts" here | |
388 | bool MyApp::OnInit() | |
389 | { | |
390 | // Create the main application window | |
be5a51fb | 391 | MyFrame *frame = new MyFrame(_T("Exec wxWidgets sample"), |
69c33c6c VZ |
392 | wxDefaultPosition, wxSize(500, 140)); |
393 | ||
394 | // Show it and tell the application that it's our main window | |
07850a49 | 395 | frame->Show(true); |
69c33c6c VZ |
396 | SetTopWindow(frame); |
397 | ||
398 | // success: wxApp::OnRun() will be called which will enter the main message | |
07850a49 | 399 | // loop and the application will run. If we returned false here, the |
69c33c6c | 400 | // application would exit immediately. |
07850a49 | 401 | return true; |
69c33c6c VZ |
402 | } |
403 | ||
404 | // ---------------------------------------------------------------------------- | |
405 | // main frame | |
406 | // ---------------------------------------------------------------------------- | |
407 | ||
2b5f62a0 VZ |
408 | #ifdef __VISUALC__ |
409 | #pragma warning(disable: 4355) // this used in base member initializer list | |
410 | #endif | |
411 | ||
69c33c6c VZ |
412 | // frame constructor |
413 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
07850a49 | 414 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), |
92a2a7eb | 415 | m_timerIdleWakeUp(this) |
69c33c6c | 416 | { |
50567b69 VZ |
417 | m_pidLast = 0; |
418 | ||
69c33c6c VZ |
419 | #ifdef __WXMAC__ |
420 | // we need this in order to allow the about menu relocation, since ABOUT is | |
421 | // not the default id of the about menu | |
422 | wxApp::s_macAboutMenuItemId = Exec_About; | |
423 | #endif | |
424 | ||
69c33c6c | 425 | // create a menu bar |
71307412 | 426 | wxMenu *menuFile = new wxMenu(wxEmptyString, wxMENU_TEAROFF); |
50567b69 VZ |
427 | menuFile->Append(Exec_Kill, _T("&Kill process...\tCtrl-K"), |
428 | _T("Kill a process by PID")); | |
429 | menuFile->AppendSeparator(); | |
d8e41d42 VZ |
430 | menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-C"), |
431 | _T("Clear the log window")); | |
432 | menuFile->AppendSeparator(); | |
69c33c6c VZ |
433 | menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
434 | ||
435 | wxMenu *execMenu = new wxMenu; | |
436 | execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"), | |
437 | _T("Launch a program and return when it terminates")); | |
438 | execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"), | |
439 | _T("Launch a program and return immediately")); | |
440 | execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"), | |
441 | _T("Launch a shell and execute a command in it")); | |
f6bcfd97 | 442 | execMenu->AppendSeparator(); |
d8e41d42 VZ |
443 | execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"), |
444 | _T("Launch a program and capture its output")); | |
eb671557 | 445 | execMenu->Append(Exec_Pipe, _T("&Pipe through command..."), |
f6bcfd97 | 446 | _T("Pipe a string through a filter")); |
eb671557 VZ |
447 | execMenu->Append(Exec_POpen, _T("&Open a pipe to a command...\tCtrl-P"), |
448 | _T("Open a pipe to and from another program")); | |
69c33c6c | 449 | |
6ba63600 VZ |
450 | execMenu->AppendSeparator(); |
451 | execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"), | |
452 | _T("Launch the command to open this kind of files")); | |
d93c719a VZ |
453 | #ifdef __WINDOWS__ |
454 | execMenu->AppendSeparator(); | |
455 | execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D")); | |
ca289436 | 456 | execMenu->Append(Exec_DDERequest, _T("Send DDE &request...\tCtrl-R")); |
d93c719a VZ |
457 | #endif |
458 | ||
71307412 | 459 | wxMenu *helpMenu = new wxMenu(wxEmptyString, wxMENU_TEAROFF); |
69c33c6c VZ |
460 | helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog")); |
461 | ||
462 | // now append the freshly created menu to the menu bar... | |
463 | wxMenuBar *menuBar = new wxMenuBar(); | |
464 | menuBar->Append(menuFile, _T("&File")); | |
465 | menuBar->Append(execMenu, _T("&Exec")); | |
466 | menuBar->Append(helpMenu, _T("&Help")); | |
467 | ||
468 | // ... and attach this menu bar to the frame | |
469 | SetMenuBar(menuBar); | |
470 | ||
9121bed2 | 471 | // create the listbox in which we will show misc messages as they come |
07850a49 | 472 | m_lbox = new wxListBox(this, wxID_ANY); |
8ce88dfa VZ |
473 | wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, |
474 | wxFONTWEIGHT_NORMAL); | |
475 | if ( font.Ok() ) | |
476 | m_lbox->SetFont(font); | |
9121bed2 | 477 | |
69c33c6c VZ |
478 | #if wxUSE_STATUSBAR |
479 | // create a status bar just for fun (by default with 1 pane only) | |
e680a378 | 480 | CreateStatusBar(); |
be5a51fb | 481 | SetStatusText(_T("Welcome to wxWidgets exec sample!")); |
69c33c6c VZ |
482 | #endif // wxUSE_STATUSBAR |
483 | } | |
484 | ||
50567b69 VZ |
485 | // ---------------------------------------------------------------------------- |
486 | // event handlers: file and help menu | |
487 | // ---------------------------------------------------------------------------- | |
69c33c6c VZ |
488 | |
489 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
490 | { | |
07850a49 WS |
491 | // true is to force the frame to close |
492 | Close(true); | |
69c33c6c VZ |
493 | } |
494 | ||
d8e41d42 VZ |
495 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
496 | { | |
497 | m_lbox->Clear(); | |
498 | } | |
499 | ||
69c33c6c VZ |
500 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
501 | { | |
749bfe9a | 502 | wxMessageBox(_T("Exec wxWidgets Sample\n(c) 2000-2002 Vadim Zeitlin"), |
69c33c6c VZ |
503 | _T("About Exec"), wxOK | wxICON_INFORMATION, this); |
504 | } | |
505 | ||
50567b69 VZ |
506 | void MyFrame::OnKill(wxCommandEvent& WXUNUSED(event)) |
507 | { | |
508 | long pid = wxGetNumberFromUser(_T("Please specify the process to kill"), | |
509 | _T("Enter PID:"), | |
510 | _T("Exec question"), | |
511 | m_pidLast, | |
9c05a3ca VZ |
512 | // we need the full unsigned int range |
513 | -INT_MAX, INT_MAX, | |
50567b69 VZ |
514 | this); |
515 | if ( pid == -1 ) | |
516 | { | |
517 | // cancelled | |
518 | return; | |
519 | } | |
520 | ||
521 | static const wxString signalNames[] = | |
522 | { | |
523 | _T("Just test (SIGNONE)"), | |
524 | _T("Hangup (SIGHUP)"), | |
525 | _T("Interrupt (SIGINT)"), | |
526 | _T("Quit (SIGQUIT)"), | |
527 | _T("Illegal instruction (SIGILL)"), | |
528 | _T("Trap (SIGTRAP)"), | |
529 | _T("Abort (SIGABRT)"), | |
530 | _T("Emulated trap (SIGEMT)"), | |
531 | _T("FP exception (SIGFPE)"), | |
532 | _T("Kill (SIGKILL)"), | |
533 | _T("Bus (SIGBUS)"), | |
534 | _T("Segment violation (SIGSEGV)"), | |
535 | _T("System (SIGSYS)"), | |
536 | _T("Broken pipe (SIGPIPE)"), | |
537 | _T("Alarm (SIGALRM)"), | |
538 | _T("Terminate (SIGTERM)"), | |
539 | }; | |
540 | ||
541 | int sig = wxGetSingleChoiceIndex(_T("How to kill the process?"), | |
542 | _T("Exec question"), | |
543 | WXSIZEOF(signalNames), signalNames, | |
544 | this); | |
545 | switch ( sig ) | |
546 | { | |
547 | default: | |
548 | wxFAIL_MSG( _T("unexpected return value") ); | |
549 | // fall through | |
550 | ||
551 | case -1: | |
552 | // cancelled | |
553 | return; | |
554 | ||
555 | case wxSIGNONE: | |
556 | case wxSIGHUP: | |
557 | case wxSIGINT: | |
558 | case wxSIGQUIT: | |
559 | case wxSIGILL: | |
560 | case wxSIGTRAP: | |
561 | case wxSIGABRT: | |
562 | case wxSIGEMT: | |
563 | case wxSIGFPE: | |
564 | case wxSIGKILL: | |
565 | case wxSIGBUS: | |
566 | case wxSIGSEGV: | |
567 | case wxSIGSYS: | |
568 | case wxSIGPIPE: | |
569 | case wxSIGALRM: | |
570 | case wxSIGTERM: | |
571 | break; | |
572 | } | |
573 | ||
574 | if ( sig == 0 ) | |
575 | { | |
576 | if ( wxProcess::Exists(pid) ) | |
aec18ff7 | 577 | wxLogStatus(_T("Process %ld is running."), pid); |
50567b69 | 578 | else |
aec18ff7 | 579 | wxLogStatus(_T("No process with pid = %ld."), pid); |
50567b69 VZ |
580 | } |
581 | else // not SIGNONE | |
582 | { | |
583 | wxKillError rc = wxProcess::Kill(pid, (wxSignal)sig); | |
584 | if ( rc == wxKILL_OK ) | |
585 | { | |
aec18ff7 | 586 | wxLogStatus(_T("Process %ld killed with signal %d."), pid, sig); |
50567b69 VZ |
587 | } |
588 | else | |
589 | { | |
590 | static const wxChar *errorText[] = | |
591 | { | |
592 | _T(""), // no error | |
593 | _T("signal not supported"), | |
594 | _T("permission denied"), | |
595 | _T("no such process"), | |
596 | _T("unspecified error"), | |
597 | }; | |
598 | ||
aec18ff7 | 599 | wxLogStatus(_T("Failed to kill process %ld with signal %d: %s"), |
50567b69 VZ |
600 | pid, sig, errorText[rc]); |
601 | } | |
602 | } | |
603 | } | |
604 | ||
605 | // ---------------------------------------------------------------------------- | |
606 | // event handlers: exec menu | |
607 | // ---------------------------------------------------------------------------- | |
608 | ||
6ba63600 VZ |
609 | void MyFrame::DoAsyncExec(const wxString& cmd) |
610 | { | |
611 | wxProcess *process = new MyProcess(this, cmd); | |
4c1ef422 | 612 | m_pidLast = wxExecute(cmd, wxEXEC_ASYNC, process); |
50567b69 | 613 | if ( !m_pidLast ) |
6ba63600 | 614 | { |
aec18ff7 | 615 | wxLogError( _T("Execution of '%s' failed."), cmd.c_str() ); |
6ba63600 VZ |
616 | |
617 | delete process; | |
618 | } | |
619 | else | |
620 | { | |
aec18ff7 MB |
621 | wxLogStatus( _T("Process %ld (%s) launched."), |
622 | m_pidLast, cmd.c_str() ); | |
6ba63600 VZ |
623 | |
624 | m_cmdLast = cmd; | |
625 | } | |
626 | } | |
627 | ||
69c33c6c VZ |
628 | void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event)) |
629 | { | |
630 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
d93c719a | 631 | DIALOG_TITLE, |
69c33c6c VZ |
632 | m_cmdLast); |
633 | ||
634 | if ( !cmd ) | |
635 | return; | |
636 | ||
aec18ff7 | 637 | wxLogStatus( _T("'%s' is running please wait..."), cmd.c_str() ); |
d31b7b68 | 638 | |
4c1ef422 | 639 | int code = wxExecute(cmd, wxEXEC_SYNC); |
d31b7b68 | 640 | |
69c33c6c | 641 | wxLogStatus(_T("Process '%s' terminated with exit code %d."), |
aec18ff7 MB |
642 | cmd.c_str(), code); |
643 | ||
69c33c6c VZ |
644 | m_cmdLast = cmd; |
645 | } | |
646 | ||
647 | void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event)) | |
648 | { | |
649 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
d93c719a | 650 | DIALOG_TITLE, |
69c33c6c VZ |
651 | m_cmdLast); |
652 | ||
653 | if ( !cmd ) | |
654 | return; | |
655 | ||
6ba63600 | 656 | DoAsyncExec(cmd); |
69c33c6c VZ |
657 | } |
658 | ||
659 | void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event)) | |
660 | { | |
661 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
d93c719a | 662 | DIALOG_TITLE, |
69c33c6c VZ |
663 | m_cmdLast); |
664 | ||
665 | if ( !cmd ) | |
666 | return; | |
667 | ||
668 | int code = wxShell(cmd); | |
669 | wxLogStatus(_T("Shell command '%s' terminated with exit code %d."), | |
670 | cmd.c_str(), code); | |
671 | m_cmdLast = cmd; | |
672 | } | |
673 | ||
d8e41d42 VZ |
674 | void MyFrame::OnExecWithRedirect(wxCommandEvent& WXUNUSED(event)) |
675 | { | |
676 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
677 | DIALOG_TITLE, | |
678 | m_cmdLast); | |
679 | ||
680 | if ( !cmd ) | |
681 | return; | |
682 | ||
cd6ce4a9 VZ |
683 | bool sync; |
684 | switch ( wxMessageBox(_T("Execute it synchronously?"), | |
685 | _T("Exec question"), | |
686 | wxYES_NO | wxCANCEL | wxICON_QUESTION, this) ) | |
d8e41d42 | 687 | { |
cd6ce4a9 | 688 | case wxYES: |
07850a49 | 689 | sync = true; |
cd6ce4a9 | 690 | break; |
d8e41d42 | 691 | |
cd6ce4a9 | 692 | case wxNO: |
07850a49 | 693 | sync = false; |
cd6ce4a9 VZ |
694 | break; |
695 | ||
696 | default: | |
697 | return; | |
d8e41d42 | 698 | } |
cd6ce4a9 VZ |
699 | |
700 | if ( sync ) | |
d8e41d42 | 701 | { |
f6bcfd97 BP |
702 | wxArrayString output, errors; |
703 | int code = wxExecute(cmd, output, errors); | |
cd6ce4a9 VZ |
704 | wxLogStatus(_T("command '%s' terminated with exit code %d."), |
705 | cmd.c_str(), code); | |
706 | ||
707 | if ( code != -1 ) | |
708 | { | |
f6bcfd97 BP |
709 | ShowOutput(cmd, output, _T("Output")); |
710 | ShowOutput(cmd, errors, _T("Errors")); | |
cd6ce4a9 VZ |
711 | } |
712 | } | |
713 | else // async exec | |
714 | { | |
715 | MyPipedProcess *process = new MyPipedProcess(this, cmd); | |
4c1ef422 | 716 | if ( !wxExecute(cmd, wxEXEC_ASYNC, process) ) |
cd6ce4a9 VZ |
717 | { |
718 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
719 | ||
720 | delete process; | |
721 | } | |
722 | else | |
723 | { | |
92a2a7eb | 724 | AddAsyncProcess(process); |
cd6ce4a9 | 725 | } |
d8e41d42 | 726 | } |
cd6ce4a9 VZ |
727 | |
728 | m_cmdLast = cmd; | |
d8e41d42 VZ |
729 | } |
730 | ||
f6bcfd97 BP |
731 | void MyFrame::OnExecWithPipe(wxCommandEvent& WXUNUSED(event)) |
732 | { | |
733 | if ( !m_cmdLast ) | |
734 | m_cmdLast = _T("tr [a-z] [A-Z]"); | |
735 | ||
736 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
737 | DIALOG_TITLE, | |
738 | m_cmdLast); | |
739 | ||
740 | if ( !cmd ) | |
741 | return; | |
742 | ||
743 | wxString input = wxGetTextFromUser(_T("Enter the string to send to it: "), | |
744 | DIALOG_TITLE); | |
745 | if ( !input ) | |
746 | return; | |
747 | ||
748 | // always execute the filter asynchronously | |
749 | MyPipedProcess2 *process = new MyPipedProcess2(this, cmd, input); | |
aec18ff7 | 750 | long pid = wxExecute(cmd, wxEXEC_ASYNC, process); |
f6bcfd97 BP |
751 | if ( pid ) |
752 | { | |
aec18ff7 | 753 | wxLogStatus( _T("Process %ld (%s) launched."), pid, cmd.c_str() ); |
f6bcfd97 | 754 | |
92a2a7eb | 755 | AddAsyncProcess(process); |
f6bcfd97 BP |
756 | } |
757 | else | |
758 | { | |
759 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
760 | ||
761 | delete process; | |
762 | } | |
763 | ||
764 | m_cmdLast = cmd; | |
765 | } | |
766 | ||
87728739 | 767 | void MyFrame::OnPOpen(wxCommandEvent& WXUNUSED(event)) |
eb671557 VZ |
768 | { |
769 | wxString cmd = wxGetTextFromUser(_T("Enter the command to launch: "), | |
770 | DIALOG_TITLE, | |
771 | m_cmdLast); | |
772 | if ( cmd.empty() ) | |
773 | return; | |
774 | ||
775 | wxProcess *process = wxProcess::Open(cmd); | |
776 | if ( !process ) | |
777 | { | |
778 | wxLogError(_T("Failed to launch the command.")); | |
779 | return; | |
780 | } | |
781 | ||
782 | wxOutputStream *out = process->GetOutputStream(); | |
783 | if ( !out ) | |
784 | { | |
785 | wxLogError(_T("Failed to connect to child stdin")); | |
786 | return; | |
787 | } | |
788 | ||
789 | wxInputStream *in = process->GetInputStream(); | |
790 | if ( !in ) | |
791 | { | |
792 | wxLogError(_T("Failed to connect to child stdout")); | |
793 | return; | |
794 | } | |
795 | ||
796 | new MyPipeFrame(this, cmd, process); | |
797 | } | |
798 | ||
87728739 | 799 | void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event)) |
6ba63600 VZ |
800 | { |
801 | static wxString s_filename; | |
802 | ||
71307412 WS |
803 | wxString filename; |
804 | ||
805 | #if wxUSE_FILEDLG | |
806 | filename = wxLoadFileSelector(wxEmptyString, wxEmptyString, s_filename); | |
807 | #endif // wxUSE_FILEDLG | |
808 | ||
809 | if ( filename.empty() ) | |
6ba63600 VZ |
810 | return; |
811 | ||
812 | s_filename = filename; | |
813 | ||
814 | wxString ext = filename.AfterFirst(_T('.')); | |
815 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); | |
816 | if ( !ft ) | |
817 | { | |
818 | wxLogError(_T("Impossible to determine the file type for extension '%s'"), | |
819 | ext.c_str()); | |
820 | return; | |
821 | } | |
822 | ||
823 | wxString cmd; | |
824 | bool ok = ft->GetOpenCommand(&cmd, | |
71307412 | 825 | wxFileType::MessageParameters(filename)); |
6ba63600 VZ |
826 | delete ft; |
827 | if ( !ok ) | |
828 | { | |
829 | wxLogError(_T("Impossible to find out how to open files of extension '%s'"), | |
830 | ext.c_str()); | |
831 | return; | |
832 | } | |
833 | ||
834 | DoAsyncExec(cmd); | |
835 | } | |
836 | ||
50567b69 VZ |
837 | // ---------------------------------------------------------------------------- |
838 | // DDE stuff | |
839 | // ---------------------------------------------------------------------------- | |
840 | ||
d93c719a | 841 | #ifdef __WINDOWS__ |
ca289436 VZ |
842 | |
843 | bool MyFrame::GetDDEServer() | |
844 | { | |
d93c719a | 845 | wxString server = wxGetTextFromUser(_T("Server to connect to:"), |
ca289436 | 846 | DIALOG_TITLE, m_server); |
d93c719a | 847 | if ( !server ) |
07850a49 | 848 | return false; |
ca289436 VZ |
849 | |
850 | m_server = server; | |
d93c719a | 851 | |
ca289436 | 852 | wxString topic = wxGetTextFromUser(_T("DDE topic:"), DIALOG_TITLE, m_topic); |
d93c719a | 853 | if ( !topic ) |
07850a49 | 854 | return false; |
d93c719a | 855 | |
ca289436 VZ |
856 | m_topic = topic; |
857 | ||
858 | wxString cmd = wxGetTextFromUser(_T("DDE command:"), DIALOG_TITLE, m_cmdDde); | |
d93c719a | 859 | if ( !cmd ) |
07850a49 | 860 | return false; |
ca289436 VZ |
861 | |
862 | m_cmdDde = cmd; | |
863 | ||
07850a49 | 864 | return true; |
ca289436 VZ |
865 | } |
866 | ||
867 | void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event)) | |
868 | { | |
869 | if ( !GetDDEServer() ) | |
d93c719a VZ |
870 | return; |
871 | ||
872 | wxDDEClient client; | |
71307412 | 873 | wxConnectionBase *conn = client.MakeConnection(wxEmptyString, m_server, m_topic); |
d93c719a VZ |
874 | if ( !conn ) |
875 | { | |
876 | wxLogError(_T("Failed to connect to the DDE server '%s'."), | |
ca289436 | 877 | m_server.c_str()); |
d93c719a VZ |
878 | } |
879 | else | |
880 | { | |
ca289436 | 881 | if ( !conn->Execute(m_cmdDde) ) |
d93c719a VZ |
882 | { |
883 | wxLogError(_T("Failed to execute command '%s' via DDE."), | |
ca289436 | 884 | m_cmdDde.c_str()); |
d93c719a VZ |
885 | } |
886 | else | |
887 | { | |
888 | wxLogStatus(_T("Successfully executed DDE command")); | |
889 | } | |
890 | } | |
d93c719a VZ |
891 | } |
892 | ||
ca289436 VZ |
893 | void MyFrame::OnDDERequest(wxCommandEvent& WXUNUSED(event)) |
894 | { | |
895 | if ( !GetDDEServer() ) | |
896 | return; | |
897 | ||
898 | wxDDEClient client; | |
71307412 | 899 | wxConnectionBase *conn = client.MakeConnection(wxEmptyString, m_server, m_topic); |
ca289436 VZ |
900 | if ( !conn ) |
901 | { | |
902 | wxLogError(_T("Failed to connect to the DDE server '%s'."), | |
903 | m_server.c_str()); | |
904 | } | |
905 | else | |
906 | { | |
907 | if ( !conn->Request(m_cmdDde) ) | |
908 | { | |
909 | wxLogError(_T("Failed to send request '%s' via DDE."), | |
910 | m_cmdDde.c_str()); | |
911 | } | |
912 | else | |
913 | { | |
914 | wxLogStatus(_T("Successfully sent DDE request.")); | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | #endif // __WINDOWS__ | |
920 | ||
50567b69 VZ |
921 | // ---------------------------------------------------------------------------- |
922 | // various helpers | |
923 | // ---------------------------------------------------------------------------- | |
924 | ||
cd6ce4a9 VZ |
925 | // input polling |
926 | void MyFrame::OnIdle(wxIdleEvent& event) | |
927 | { | |
928 | size_t count = m_running.GetCount(); | |
929 | for ( size_t n = 0; n < count; n++ ) | |
930 | { | |
931 | if ( m_running[n]->HasInput() ) | |
932 | { | |
933 | event.RequestMore(); | |
934 | } | |
935 | } | |
936 | } | |
937 | ||
92a2a7eb VZ |
938 | void MyFrame::OnTimer(wxTimerEvent& WXUNUSED(event)) |
939 | { | |
940 | wxWakeUpIdle(); | |
941 | } | |
942 | ||
f6bcfd97 BP |
943 | void MyFrame::OnProcessTerminated(MyPipedProcess *process) |
944 | { | |
92a2a7eb | 945 | RemoveAsyncProcess(process); |
f6bcfd97 BP |
946 | } |
947 | ||
948 | ||
949 | void MyFrame::ShowOutput(const wxString& cmd, | |
950 | const wxArrayString& output, | |
951 | const wxString& title) | |
952 | { | |
953 | size_t count = output.GetCount(); | |
954 | if ( !count ) | |
955 | return; | |
956 | ||
957 | m_lbox->Append(wxString::Format(_T("--- %s of '%s' ---"), | |
958 | title.c_str(), cmd.c_str())); | |
959 | ||
960 | for ( size_t n = 0; n < count; n++ ) | |
961 | { | |
962 | m_lbox->Append(output[n]); | |
963 | } | |
964 | ||
eb671557 VZ |
965 | m_lbox->Append(wxString::Format(_T("--- End of %s ---"), |
966 | title.Lower().c_str())); | |
f6bcfd97 BP |
967 | } |
968 | ||
69c33c6c VZ |
969 | // ---------------------------------------------------------------------------- |
970 | // MyProcess | |
971 | // ---------------------------------------------------------------------------- | |
972 | ||
973 | void MyProcess::OnTerminate(int pid, int status) | |
974 | { | |
975 | wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."), | |
976 | pid, m_cmd.c_str(), status); | |
977 | ||
978 | // we're not needed any more | |
979 | delete this; | |
980 | } | |
d8e41d42 | 981 | |
cd6ce4a9 VZ |
982 | // ---------------------------------------------------------------------------- |
983 | // MyPipedProcess | |
984 | // ---------------------------------------------------------------------------- | |
985 | ||
986 | bool MyPipedProcess::HasInput() | |
d8e41d42 | 987 | { |
07850a49 | 988 | bool hasInput = false; |
f6bcfd97 | 989 | |
92a2a7eb | 990 | if ( IsInputAvailable() ) |
cd6ce4a9 | 991 | { |
92a2a7eb | 992 | wxTextInputStream tis(*GetInputStream()); |
cd6ce4a9 VZ |
993 | |
994 | // this assumes that the output is always line buffered | |
995 | wxString msg; | |
f6bcfd97 | 996 | msg << m_cmd << _T(" (stdout): ") << tis.ReadLine(); |
d8e41d42 | 997 | |
cd6ce4a9 VZ |
998 | m_parent->GetLogListBox()->Append(msg); |
999 | ||
07850a49 | 1000 | hasInput = true; |
cd6ce4a9 | 1001 | } |
f6bcfd97 | 1002 | |
92a2a7eb | 1003 | if ( IsErrorAvailable() ) |
d8e41d42 | 1004 | { |
92a2a7eb | 1005 | wxTextInputStream tis(*GetErrorStream()); |
f6bcfd97 BP |
1006 | |
1007 | // this assumes that the output is always line buffered | |
1008 | wxString msg; | |
1009 | msg << m_cmd << _T(" (stderr): ") << tis.ReadLine(); | |
1010 | ||
1011 | m_parent->GetLogListBox()->Append(msg); | |
1012 | ||
07850a49 | 1013 | hasInput = true; |
d8e41d42 | 1014 | } |
f6bcfd97 BP |
1015 | |
1016 | return hasInput; | |
cd6ce4a9 VZ |
1017 | } |
1018 | ||
1019 | void MyPipedProcess::OnTerminate(int pid, int status) | |
1020 | { | |
1021 | // show the rest of the output | |
1022 | while ( HasInput() ) | |
1023 | ; | |
1024 | ||
1025 | m_parent->OnProcessTerminated(this); | |
d8e41d42 VZ |
1026 | |
1027 | MyProcess::OnTerminate(pid, status); | |
1028 | } | |
f6bcfd97 BP |
1029 | |
1030 | // ---------------------------------------------------------------------------- | |
1031 | // MyPipedProcess2 | |
1032 | // ---------------------------------------------------------------------------- | |
1033 | ||
1034 | bool MyPipedProcess2::HasInput() | |
1035 | { | |
11fdee42 | 1036 | if ( !m_input.empty() ) |
f6bcfd97 BP |
1037 | { |
1038 | wxTextOutputStream os(*GetOutputStream()); | |
1039 | os.WriteString(m_input); | |
1040 | ||
1041 | CloseOutput(); | |
1042 | m_input.clear(); | |
1043 | ||
1044 | // call us once again - may be we'll have output | |
07850a49 | 1045 | return true; |
f6bcfd97 BP |
1046 | } |
1047 | ||
1048 | return MyPipedProcess::HasInput(); | |
1049 | } | |
eb671557 VZ |
1050 | |
1051 | // ============================================================================ | |
1052 | // MyPipeFrame implementation | |
1053 | // ============================================================================ | |
1054 | ||
1055 | MyPipeFrame::MyPipeFrame(wxFrame *parent, | |
1056 | const wxString& cmd, | |
1057 | wxProcess *process) | |
07850a49 | 1058 | : wxFrame(parent, wxID_ANY, cmd), |
eb671557 VZ |
1059 | m_process(process), |
1060 | // in a real program we'd check that the streams are !NULL here | |
f6def1fa | 1061 | m_out(*process->GetOutputStream()), |
eb671557 | 1062 | m_in(*process->GetInputStream()), |
f6def1fa | 1063 | m_err(*process->GetErrorStream()) |
eb671557 | 1064 | { |
7ca528cb VZ |
1065 | m_process->SetNextHandler(this); |
1066 | ||
07850a49 | 1067 | wxPanel *panel = new wxPanel(this, wxID_ANY); |
7ca528cb | 1068 | |
71307412 | 1069 | m_textOut = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, |
eb671557 VZ |
1070 | wxDefaultPosition, wxDefaultSize, |
1071 | wxTE_PROCESS_ENTER); | |
71307412 | 1072 | m_textIn = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, |
1a278e7b VZ |
1073 | wxDefaultPosition, wxDefaultSize, |
1074 | wxTE_MULTILINE | wxTE_RICH); | |
1075 | m_textIn->SetEditable(false); | |
71307412 | 1076 | m_textErr = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, |
1a278e7b VZ |
1077 | wxDefaultPosition, wxDefaultSize, |
1078 | wxTE_MULTILINE | wxTE_RICH); | |
1079 | m_textErr->SetEditable(false); | |
eb671557 VZ |
1080 | |
1081 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
1a278e7b | 1082 | sizerTop->Add(m_textOut, 0, wxGROW | wxALL, 5); |
eb671557 VZ |
1083 | |
1084 | wxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL); | |
1a278e7b VZ |
1085 | sizerBtns-> |
1086 | Add(new wxButton(panel, Exec_Btn_Send, _T("&Send")), 0, wxALL, 5); | |
1087 | sizerBtns-> | |
1088 | Add(new wxButton(panel, Exec_Btn_SendFile, _T("&File...")), 0, wxALL, 5); | |
1089 | sizerBtns-> | |
1090 | Add(new wxButton(panel, Exec_Btn_Get, _T("&Get")), 0, wxALL, 5); | |
1091 | sizerBtns-> | |
1092 | Add(new wxButton(panel, Exec_Btn_Close, _T("&Close")), 0, wxALL, 5); | |
eb671557 VZ |
1093 | |
1094 | sizerTop->Add(sizerBtns, 0, wxCENTRE | wxALL, 5); | |
1a278e7b VZ |
1095 | sizerTop->Add(m_textIn, 1, wxGROW | wxALL, 5); |
1096 | sizerTop->Add(m_textErr, 1, wxGROW | wxALL, 5); | |
eb671557 | 1097 | |
7ca528cb | 1098 | panel->SetSizer(sizerTop); |
eb671557 VZ |
1099 | sizerTop->Fit(this); |
1100 | ||
1101 | Show(); | |
1102 | } | |
1103 | ||
1a278e7b VZ |
1104 | void MyPipeFrame::OnBtnSendFile(wxCommandEvent& WXUNUSED(event)) |
1105 | { | |
71307412 | 1106 | #if wxUSE_FILEDLG |
1a278e7b VZ |
1107 | wxFileDialog filedlg(this, _T("Select file to send")); |
1108 | if ( filedlg.ShowModal() != wxID_OK ) | |
1109 | return; | |
1110 | ||
1111 | wxFFile file(filedlg.GetFilename(), _T("r")); | |
1112 | wxString data; | |
1113 | if ( !file.IsOpened() || !file.ReadAll(&data) ) | |
1114 | return; | |
1115 | ||
1116 | // can't write the entire string at once, this risk overflowing the pipe | |
1117 | // and we would dead lock | |
1118 | size_t len = data.length(); | |
1119 | const wxChar *pc = data.c_str(); | |
1120 | while ( len ) | |
1121 | { | |
1122 | const size_t CHUNK_SIZE = 4096; | |
42d0aa30 | 1123 | m_out.Write(pc, len > CHUNK_SIZE ? CHUNK_SIZE : len); |
1a278e7b | 1124 | |
42d0aa30 VZ |
1125 | // note that not all data could have been written as we don't block on |
1126 | // the write end of the pipe | |
1127 | const size_t lenChunk = m_out.LastWrite(); | |
1a278e7b VZ |
1128 | |
1129 | pc += lenChunk; | |
1130 | len -= lenChunk; | |
1131 | ||
1132 | DoGet(); | |
1133 | } | |
71307412 | 1134 | #endif // wxUSE_FILEDLG |
1a278e7b VZ |
1135 | } |
1136 | ||
eb671557 VZ |
1137 | void MyPipeFrame::DoGet() |
1138 | { | |
7ca528cb VZ |
1139 | // we don't have any way to be notified when any input appears on the |
1140 | // stream so we have to poll it :-( | |
1a278e7b VZ |
1141 | DoGetFromStream(m_textIn, m_in); |
1142 | DoGetFromStream(m_textErr, m_err); | |
1143 | } | |
1144 | ||
1145 | void MyPipeFrame::DoGetFromStream(wxTextCtrl *text, wxInputStream& in) | |
1146 | { | |
1147 | while ( in.CanRead() ) | |
1148 | { | |
1149 | wxChar buffer[4096]; | |
1150 | buffer[in.Read(buffer, WXSIZEOF(buffer) - 1).LastRead()] = _T('\0'); | |
7ca528cb | 1151 | |
1a278e7b VZ |
1152 | text->AppendText(buffer); |
1153 | } | |
1154 | } | |
1155 | ||
1156 | void MyPipeFrame::DoClose() | |
1157 | { | |
1158 | m_process->CloseOutput(); | |
1159 | ||
1160 | DisableInput(); | |
1161 | } | |
1162 | ||
1163 | void MyPipeFrame::DisableInput() | |
1164 | { | |
1165 | m_textOut->SetEditable(false); | |
1166 | FindWindow(Exec_Btn_Send)->Disable(); | |
1167 | FindWindow(Exec_Btn_SendFile)->Disable(); | |
1168 | FindWindow(Exec_Btn_Close)->Disable(); | |
1169 | } | |
1170 | ||
1171 | void MyPipeFrame::DisableOutput() | |
1172 | { | |
1173 | FindWindow(Exec_Btn_Get)->Disable(); | |
eb671557 VZ |
1174 | } |
1175 | ||
1176 | void MyPipeFrame::OnClose(wxCloseEvent& event) | |
1177 | { | |
7ca528cb VZ |
1178 | if ( m_process ) |
1179 | { | |
1180 | // we're not interested in getting the process termination notification | |
1181 | // if we are closing it ourselves | |
1182 | wxProcess *process = m_process; | |
1183 | m_process = NULL; | |
1184 | process->SetNextHandler(NULL); | |
1185 | ||
1186 | process->CloseOutput(); | |
1187 | } | |
eb671557 VZ |
1188 | |
1189 | event.Skip(); | |
1190 | } | |
1191 | ||
87728739 | 1192 | void MyPipeFrame::OnProcessTerm(wxProcessEvent& WXUNUSED(event)) |
7ca528cb | 1193 | { |
1a278e7b VZ |
1194 | DoGet(); |
1195 | ||
7ca528cb VZ |
1196 | delete m_process; |
1197 | m_process = NULL; | |
1198 | ||
1199 | wxLogWarning(_T("The other process has terminated, closing")); | |
1200 | ||
1a278e7b VZ |
1201 | DisableInput(); |
1202 | DisableOutput(); | |
7ca528cb | 1203 | } |