1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: exec sample demonstrates wxExecute and related functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "exec.cpp"
22 #pragma interface "exec.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWidgets headers
45 #include "wx/msgdlg.h"
46 #include "wx/textdlg.h"
47 #include "wx/filedlg.h"
48 #include "wx/choicdlg.h"
50 #include "wx/button.h"
51 #include "wx/textctrl.h"
52 #include "wx/listbox.h"
57 #include "wx/txtstrm.h"
58 #include "wx/numdlg.h"
60 #include "wx/process.h"
62 #include "wx/mimetype.h"
68 // ----------------------------------------------------------------------------
69 // the usual application and main frame classes
70 // ----------------------------------------------------------------------------
72 // Define a new application type, each program should derive a class from wxApp
73 class MyApp
: public wxApp
76 // override base class virtuals
77 // ----------------------------
79 // this one is called on application startup and is a good place for the app
80 // initialization (doing it here and not in the ctor allows to have an error
81 // return: if OnInit() returns false, the application terminates)
82 virtual bool OnInit();
85 // Define an array of process pointers used by MyFrame
87 WX_DEFINE_ARRAY_PTR(MyPipedProcess
*, MyProcessesArray
);
89 // Define a new frame type: this is going to be our main frame
90 class MyFrame
: public wxFrame
94 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
96 // event handlers (these functions should _not_ be virtual)
97 void OnQuit(wxCommandEvent
& event
);
99 void OnKill(wxCommandEvent
& event
);
101 void OnClear(wxCommandEvent
& event
);
103 void OnSyncExec(wxCommandEvent
& event
);
104 void OnAsyncExec(wxCommandEvent
& event
);
105 void OnShell(wxCommandEvent
& event
);
106 void OnExecWithRedirect(wxCommandEvent
& event
);
107 void OnExecWithPipe(wxCommandEvent
& event
);
109 void OnPOpen(wxCommandEvent
& event
);
111 void OnFileExec(wxCommandEvent
& event
);
113 void OnAbout(wxCommandEvent
& event
);
115 // polling output of async processes
116 void OnTimer(wxTimerEvent
& event
);
117 void OnIdle(wxIdleEvent
& event
);
119 // for MyPipedProcess
120 void OnProcessTerminated(MyPipedProcess
*process
);
121 wxListBox
*GetLogListBox() const { return m_lbox
; }
124 void ShowOutput(const wxString
& cmd
,
125 const wxArrayString
& output
,
126 const wxString
& title
);
128 void DoAsyncExec(const wxString
& cmd
);
130 void AddAsyncProcess(MyPipedProcess
*process
)
132 if ( m_running
.IsEmpty() )
134 // we want to start getting the timer events to ensure that a
135 // steady stream of idle events comes in -- otherwise we
136 // wouldn't be able to poll the child process input
137 m_timerIdleWakeUp
.Start(100);
139 //else: the timer is already running
141 m_running
.Add(process
);
144 void RemoveAsyncProcess(MyPipedProcess
*process
)
146 m_running
.Remove(process
);
148 if ( m_running
.IsEmpty() )
150 // we don't need to get idle events all the time any more
151 m_timerIdleWakeUp
.Stop();
155 // the PID of the last process we launched asynchronously
158 // last command we executed
162 void OnDDEExec(wxCommandEvent
& event
);
163 void OnDDERequest(wxCommandEvent
& event
);
167 // last params of a DDE transaction
171 #endif // __WINDOWS__
175 MyProcessesArray m_running
;
177 // the idle event wake up timer
178 wxTimer m_timerIdleWakeUp
;
180 // any class wishing to process wxWidgets events must use this macro
181 DECLARE_EVENT_TABLE()
184 // ----------------------------------------------------------------------------
185 // MyPipeFrame: allows the user to communicate with the child process
186 // ----------------------------------------------------------------------------
188 class MyPipeFrame
: public wxFrame
191 MyPipeFrame(wxFrame
*parent
,
196 void OnTextEnter(wxCommandEvent
& WXUNUSED(event
)) { DoSend(); }
197 void OnBtnSend(wxCommandEvent
& WXUNUSED(event
)) { DoSend(); }
198 void OnBtnGet(wxCommandEvent
& WXUNUSED(event
)) { DoGet(); }
200 void OnClose(wxCloseEvent
& event
);
202 void OnProcessTerm(wxProcessEvent
& event
);
206 m_out
.WriteString(m_textIn
->GetValue() + _T('\n'));
215 wxProcess
*m_process
;
217 wxTextInputStream m_in
;
218 wxTextOutputStream m_out
;
220 wxTextCtrl
*m_textIn
,
223 DECLARE_EVENT_TABLE()
226 // ----------------------------------------------------------------------------
227 // wxProcess-derived classes
228 // ----------------------------------------------------------------------------
230 // This is the handler for process termination events
231 class MyProcess
: public wxProcess
234 MyProcess(MyFrame
*parent
, const wxString
& cmd
)
235 : wxProcess(parent
), m_cmd(cmd
)
240 // instead of overriding this virtual function we might as well process the
241 // event from it in the frame class - this might be more convenient in some
243 virtual void OnTerminate(int pid
, int status
);
250 // A specialization of MyProcess for redirecting the output
251 class MyPipedProcess
: public MyProcess
254 MyPipedProcess(MyFrame
*parent
, const wxString
& cmd
)
255 : MyProcess(parent
, cmd
)
260 virtual void OnTerminate(int pid
, int status
);
262 virtual bool HasInput();
265 // A version of MyPipedProcess which also sends input to the stdin of the
267 class MyPipedProcess2
: public MyPipedProcess
270 MyPipedProcess2(MyFrame
*parent
, const wxString
& cmd
, const wxString
& input
)
271 : MyPipedProcess(parent
, cmd
), m_input(input
)
275 virtual bool HasInput();
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 // IDs for the controls and the menu commands
304 Exec_Btn_Send
= 1000,
308 static const wxChar
*DIALOG_TITLE
= _T("Exec sample");
310 // ----------------------------------------------------------------------------
311 // event tables and other macros for wxWidgets
312 // ----------------------------------------------------------------------------
314 // the event tables connect the wxWidgets events with the functions (event
315 // handlers) which process them. It can be also done at run-time, but for the
316 // simple menu events like this the static method is much simpler.
317 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
318 EVT_MENU(Exec_Quit
, MyFrame::OnQuit
)
319 EVT_MENU(Exec_Kill
, MyFrame::OnKill
)
320 EVT_MENU(Exec_ClearLog
, MyFrame::OnClear
)
322 EVT_MENU(Exec_SyncExec
, MyFrame::OnSyncExec
)
323 EVT_MENU(Exec_AsyncExec
, MyFrame::OnAsyncExec
)
324 EVT_MENU(Exec_Shell
, MyFrame::OnShell
)
325 EVT_MENU(Exec_Redirect
, MyFrame::OnExecWithRedirect
)
326 EVT_MENU(Exec_Pipe
, MyFrame::OnExecWithPipe
)
328 EVT_MENU(Exec_POpen
, MyFrame::OnPOpen
)
330 EVT_MENU(Exec_OpenFile
, MyFrame::OnFileExec
)
333 EVT_MENU(Exec_DDEExec
, MyFrame::OnDDEExec
)
334 EVT_MENU(Exec_DDERequest
, MyFrame::OnDDERequest
)
335 #endif // __WINDOWS__
337 EVT_MENU(Exec_About
, MyFrame::OnAbout
)
339 EVT_IDLE(MyFrame::OnIdle
)
341 EVT_TIMER(wxID_ANY
, MyFrame::OnTimer
)
344 BEGIN_EVENT_TABLE(MyPipeFrame
, wxFrame
)
345 EVT_BUTTON(Exec_Btn_Send
, MyPipeFrame::OnBtnSend
)
346 EVT_BUTTON(Exec_Btn_Get
, MyPipeFrame::OnBtnGet
)
348 EVT_TEXT_ENTER(wxID_ANY
, MyPipeFrame::OnTextEnter
)
350 EVT_CLOSE(MyPipeFrame::OnClose
)
352 EVT_END_PROCESS(wxID_ANY
, MyPipeFrame::OnProcessTerm
)
355 // Create a new application object: this macro will allow wxWidgets to create
356 // the application object during program execution (it's better than using a
357 // static object for many reasons) and also declares the accessor function
358 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
362 // ============================================================================
364 // ============================================================================
366 // ----------------------------------------------------------------------------
367 // the application class
368 // ----------------------------------------------------------------------------
370 // `Main program' equivalent: the program execution "starts" here
373 // Create the main application window
374 MyFrame
*frame
= new MyFrame(_T("Exec wxWidgets sample"),
375 wxDefaultPosition
, wxSize(500, 140));
377 // Show it and tell the application that it's our main window
381 // success: wxApp::OnRun() will be called which will enter the main message
382 // loop and the application will run. If we returned false here, the
383 // application would exit immediately.
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
392 #pragma warning(disable: 4355) // this used in base member initializer list
396 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
397 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
),
398 m_timerIdleWakeUp(this)
403 // we need this in order to allow the about menu relocation, since ABOUT is
404 // not the default id of the about menu
405 wxApp::s_macAboutMenuItemId
= Exec_About
;
409 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
410 menuFile
->Append(Exec_Kill
, _T("&Kill process...\tCtrl-K"),
411 _T("Kill a process by PID"));
412 menuFile
->AppendSeparator();
413 menuFile
->Append(Exec_ClearLog
, _T("&Clear log\tCtrl-C"),
414 _T("Clear the log window"));
415 menuFile
->AppendSeparator();
416 menuFile
->Append(Exec_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
418 wxMenu
*execMenu
= new wxMenu
;
419 execMenu
->Append(Exec_SyncExec
, _T("Sync &execution...\tCtrl-E"),
420 _T("Launch a program and return when it terminates"));
421 execMenu
->Append(Exec_AsyncExec
, _T("&Async execution...\tCtrl-A"),
422 _T("Launch a program and return immediately"));
423 execMenu
->Append(Exec_Shell
, _T("Execute &shell command...\tCtrl-S"),
424 _T("Launch a shell and execute a command in it"));
425 execMenu
->AppendSeparator();
426 execMenu
->Append(Exec_Redirect
, _T("Capture command &output...\tCtrl-O"),
427 _T("Launch a program and capture its output"));
428 execMenu
->Append(Exec_Pipe
, _T("&Pipe through command..."),
429 _T("Pipe a string through a filter"));
430 execMenu
->Append(Exec_POpen
, _T("&Open a pipe to a command...\tCtrl-P"),
431 _T("Open a pipe to and from another program"));
433 execMenu
->AppendSeparator();
434 execMenu
->Append(Exec_OpenFile
, _T("Open &file...\tCtrl-F"),
435 _T("Launch the command to open this kind of files"));
437 execMenu
->AppendSeparator();
438 execMenu
->Append(Exec_DDEExec
, _T("Execute command via &DDE...\tCtrl-D"));
439 execMenu
->Append(Exec_DDERequest
, _T("Send DDE &request...\tCtrl-R"));
442 wxMenu
*helpMenu
= new wxMenu(_T(""), wxMENU_TEAROFF
);
443 helpMenu
->Append(Exec_About
, _T("&About...\tF1"), _T("Show about dialog"));
445 // now append the freshly created menu to the menu bar...
446 wxMenuBar
*menuBar
= new wxMenuBar();
447 menuBar
->Append(menuFile
, _T("&File"));
448 menuBar
->Append(execMenu
, _T("&Exec"));
449 menuBar
->Append(helpMenu
, _T("&Help"));
451 // ... and attach this menu bar to the frame
454 // create the listbox in which we will show misc messages as they come
455 m_lbox
= new wxListBox(this, wxID_ANY
);
456 wxFont
font(12, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
,
457 wxFONTWEIGHT_NORMAL
);
459 m_lbox
->SetFont(font
);
462 // create a status bar just for fun (by default with 1 pane only)
464 SetStatusText(_T("Welcome to wxWidgets exec sample!"));
465 #endif // wxUSE_STATUSBAR
468 // ----------------------------------------------------------------------------
469 // event handlers: file and help menu
470 // ----------------------------------------------------------------------------
472 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
474 // true is to force the frame to close
478 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
483 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
485 wxMessageBox(_T("Exec wxWidgets Sample\n© 2000-2002 Vadim Zeitlin"),
486 _T("About Exec"), wxOK
| wxICON_INFORMATION
, this);
489 void MyFrame::OnKill(wxCommandEvent
& WXUNUSED(event
))
491 long pid
= wxGetNumberFromUser(_T("Please specify the process to kill"),
495 // we need the full unsigned int range
504 static const wxString signalNames
[] =
506 _T("Just test (SIGNONE)"),
507 _T("Hangup (SIGHUP)"),
508 _T("Interrupt (SIGINT)"),
509 _T("Quit (SIGQUIT)"),
510 _T("Illegal instruction (SIGILL)"),
511 _T("Trap (SIGTRAP)"),
512 _T("Abort (SIGABRT)"),
513 _T("Emulated trap (SIGEMT)"),
514 _T("FP exception (SIGFPE)"),
515 _T("Kill (SIGKILL)"),
517 _T("Segment violation (SIGSEGV)"),
518 _T("System (SIGSYS)"),
519 _T("Broken pipe (SIGPIPE)"),
520 _T("Alarm (SIGALRM)"),
521 _T("Terminate (SIGTERM)"),
524 int sig
= wxGetSingleChoiceIndex(_T("How to kill the process?"),
526 WXSIZEOF(signalNames
), signalNames
,
531 wxFAIL_MSG( _T("unexpected return value") );
559 if ( wxProcess::Exists(pid
) )
560 wxLogStatus(_T("Process %ld is running."), pid
);
562 wxLogStatus(_T("No process with pid = %ld."), pid
);
566 wxKillError rc
= wxProcess::Kill(pid
, (wxSignal
)sig
);
567 if ( rc
== wxKILL_OK
)
569 wxLogStatus(_T("Process %ld killed with signal %d."), pid
, sig
);
573 static const wxChar
*errorText
[] =
576 _T("signal not supported"),
577 _T("permission denied"),
578 _T("no such process"),
579 _T("unspecified error"),
582 wxLogStatus(_T("Failed to kill process %ld with signal %d: %s"),
583 pid
, sig
, errorText
[rc
]);
588 // ----------------------------------------------------------------------------
589 // event handlers: exec menu
590 // ----------------------------------------------------------------------------
592 void MyFrame::DoAsyncExec(const wxString
& cmd
)
594 wxProcess
*process
= new MyProcess(this, cmd
);
595 m_pidLast
= wxExecute(cmd
, wxEXEC_ASYNC
, process
);
598 wxLogError( _T("Execution of '%s' failed."), cmd
.c_str() );
604 wxLogStatus( _T("Process %ld (%s) launched."),
605 m_pidLast
, cmd
.c_str() );
611 void MyFrame::OnSyncExec(wxCommandEvent
& WXUNUSED(event
))
613 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
620 wxLogStatus( _T("'%s' is running please wait..."), cmd
.c_str() );
622 int code
= wxExecute(cmd
, wxEXEC_SYNC
);
624 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
630 void MyFrame::OnAsyncExec(wxCommandEvent
& WXUNUSED(event
))
632 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
642 void MyFrame::OnShell(wxCommandEvent
& WXUNUSED(event
))
644 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
651 int code
= wxShell(cmd
);
652 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
657 void MyFrame::OnExecWithRedirect(wxCommandEvent
& WXUNUSED(event
))
659 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
667 switch ( wxMessageBox(_T("Execute it synchronously?"),
669 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
, this) )
685 wxArrayString output
, errors
;
686 int code
= wxExecute(cmd
, output
, errors
);
687 wxLogStatus(_T("command '%s' terminated with exit code %d."),
692 ShowOutput(cmd
, output
, _T("Output"));
693 ShowOutput(cmd
, errors
, _T("Errors"));
698 MyPipedProcess
*process
= new MyPipedProcess(this, cmd
);
699 if ( !wxExecute(cmd
, wxEXEC_ASYNC
, process
) )
701 wxLogError(_T("Execution of '%s' failed."), cmd
.c_str());
707 AddAsyncProcess(process
);
714 void MyFrame::OnExecWithPipe(wxCommandEvent
& WXUNUSED(event
))
717 m_cmdLast
= _T("tr [a-z] [A-Z]");
719 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
726 wxString input
= wxGetTextFromUser(_T("Enter the string to send to it: "),
731 // always execute the filter asynchronously
732 MyPipedProcess2
*process
= new MyPipedProcess2(this, cmd
, input
);
733 long pid
= wxExecute(cmd
, wxEXEC_ASYNC
, process
);
736 wxLogStatus( _T("Process %ld (%s) launched."), pid
, cmd
.c_str() );
738 AddAsyncProcess(process
);
742 wxLogError(_T("Execution of '%s' failed."), cmd
.c_str());
750 void MyFrame::OnPOpen(wxCommandEvent
& WXUNUSED(event
))
752 wxString cmd
= wxGetTextFromUser(_T("Enter the command to launch: "),
758 wxProcess
*process
= wxProcess::Open(cmd
);
761 wxLogError(_T("Failed to launch the command."));
765 wxOutputStream
*out
= process
->GetOutputStream();
768 wxLogError(_T("Failed to connect to child stdin"));
772 wxInputStream
*in
= process
->GetInputStream();
775 wxLogError(_T("Failed to connect to child stdout"));
779 new MyPipeFrame(this, cmd
, process
);
782 void MyFrame::OnFileExec(wxCommandEvent
& WXUNUSED(event
))
784 static wxString s_filename
;
786 wxString filename
= wxLoadFileSelector(_T(""), _T(""), s_filename
);
790 s_filename
= filename
;
792 wxString ext
= filename
.AfterFirst(_T('.'));
793 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
796 wxLogError(_T("Impossible to determine the file type for extension '%s'"),
802 bool ok
= ft
->GetOpenCommand(&cmd
,
803 wxFileType::MessageParameters(filename
, _T("")));
807 wxLogError(_T("Impossible to find out how to open files of extension '%s'"),
815 // ----------------------------------------------------------------------------
817 // ----------------------------------------------------------------------------
821 bool MyFrame::GetDDEServer()
823 wxString server
= wxGetTextFromUser(_T("Server to connect to:"),
824 DIALOG_TITLE
, m_server
);
830 wxString topic
= wxGetTextFromUser(_T("DDE topic:"), DIALOG_TITLE
, m_topic
);
836 wxString cmd
= wxGetTextFromUser(_T("DDE command:"), DIALOG_TITLE
, m_cmdDde
);
845 void MyFrame::OnDDEExec(wxCommandEvent
& WXUNUSED(event
))
847 if ( !GetDDEServer() )
851 wxConnectionBase
*conn
= client
.MakeConnection(_T(""), m_server
, m_topic
);
854 wxLogError(_T("Failed to connect to the DDE server '%s'."),
859 if ( !conn
->Execute(m_cmdDde
) )
861 wxLogError(_T("Failed to execute command '%s' via DDE."),
866 wxLogStatus(_T("Successfully executed DDE command"));
871 void MyFrame::OnDDERequest(wxCommandEvent
& WXUNUSED(event
))
873 if ( !GetDDEServer() )
877 wxConnectionBase
*conn
= client
.MakeConnection(_T(""), m_server
, m_topic
);
880 wxLogError(_T("Failed to connect to the DDE server '%s'."),
885 if ( !conn
->Request(m_cmdDde
) )
887 wxLogError(_T("Failed to send request '%s' via DDE."),
892 wxLogStatus(_T("Successfully sent DDE request."));
897 #endif // __WINDOWS__
899 // ----------------------------------------------------------------------------
901 // ----------------------------------------------------------------------------
904 void MyFrame::OnIdle(wxIdleEvent
& event
)
906 size_t count
= m_running
.GetCount();
907 for ( size_t n
= 0; n
< count
; n
++ )
909 if ( m_running
[n
]->HasInput() )
916 void MyFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
921 void MyFrame::OnProcessTerminated(MyPipedProcess
*process
)
923 RemoveAsyncProcess(process
);
927 void MyFrame::ShowOutput(const wxString
& cmd
,
928 const wxArrayString
& output
,
929 const wxString
& title
)
931 size_t count
= output
.GetCount();
935 m_lbox
->Append(wxString::Format(_T("--- %s of '%s' ---"),
936 title
.c_str(), cmd
.c_str()));
938 for ( size_t n
= 0; n
< count
; n
++ )
940 m_lbox
->Append(output
[n
]);
943 m_lbox
->Append(wxString::Format(_T("--- End of %s ---"),
944 title
.Lower().c_str()));
947 // ----------------------------------------------------------------------------
949 // ----------------------------------------------------------------------------
951 void MyProcess::OnTerminate(int pid
, int status
)
953 wxLogStatus(m_parent
, _T("Process %u ('%s') terminated with exit code %d."),
954 pid
, m_cmd
.c_str(), status
);
956 // we're not needed any more
960 // ----------------------------------------------------------------------------
962 // ----------------------------------------------------------------------------
964 bool MyPipedProcess::HasInput()
966 bool hasInput
= false;
968 if ( IsInputAvailable() )
970 wxTextInputStream
tis(*GetInputStream());
972 // this assumes that the output is always line buffered
974 msg
<< m_cmd
<< _T(" (stdout): ") << tis
.ReadLine();
976 m_parent
->GetLogListBox()->Append(msg
);
981 if ( IsErrorAvailable() )
983 wxTextInputStream
tis(*GetErrorStream());
985 // this assumes that the output is always line buffered
987 msg
<< m_cmd
<< _T(" (stderr): ") << tis
.ReadLine();
989 m_parent
->GetLogListBox()->Append(msg
);
997 void MyPipedProcess::OnTerminate(int pid
, int status
)
999 // show the rest of the output
1000 while ( HasInput() )
1003 m_parent
->OnProcessTerminated(this);
1005 MyProcess::OnTerminate(pid
, status
);
1008 // ----------------------------------------------------------------------------
1010 // ----------------------------------------------------------------------------
1012 bool MyPipedProcess2::HasInput()
1016 wxTextOutputStream
os(*GetOutputStream());
1017 os
.WriteString(m_input
);
1022 // call us once again - may be we'll have output
1026 return MyPipedProcess::HasInput();
1029 // ============================================================================
1030 // MyPipeFrame implementation
1031 // ============================================================================
1033 MyPipeFrame::MyPipeFrame(wxFrame
*parent
,
1034 const wxString
& cmd
,
1036 : wxFrame(parent
, wxID_ANY
, cmd
),
1038 // in a real program we'd check that the streams are !NULL here
1039 m_in(*process
->GetInputStream()),
1040 m_out(*process
->GetOutputStream())
1042 m_process
->SetNextHandler(this);
1044 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
1046 m_textIn
= new wxTextCtrl(panel
, wxID_ANY
, _T(""),
1047 wxDefaultPosition
, wxDefaultSize
,
1048 wxTE_PROCESS_ENTER
);
1049 m_textOut
= new wxTextCtrl(panel
, wxID_ANY
, _T(""));
1050 m_textOut
->SetEditable(false);
1052 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
1053 sizerTop
->Add(m_textIn
, 0, wxGROW
| wxALL
, 5);
1055 wxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
1056 sizerBtns
->Add(new wxButton(panel
, Exec_Btn_Send
, _T("&Send")), 0,
1058 sizerBtns
->Add(new wxButton(panel
, Exec_Btn_Get
, _T("&Get")), 0,
1061 sizerTop
->Add(sizerBtns
, 0, wxCENTRE
| wxALL
, 5);
1062 sizerTop
->Add(m_textOut
, 0, wxGROW
| wxALL
, 5);
1064 panel
->SetSizer(sizerTop
);
1065 sizerTop
->Fit(this);
1070 void MyPipeFrame::DoGet()
1072 // we don't have any way to be notified when any input appears on the
1073 // stream so we have to poll it :-(
1075 // NB: this really must be done because otherwise the other program might
1076 // not have enough time to receive or process our data and we'd read
1078 while ( !m_process
->IsInputAvailable() && m_process
->IsInputOpened() )
1081 m_textOut
->SetValue(m_in
.ReadLine());
1084 void MyPipeFrame::OnClose(wxCloseEvent
& event
)
1088 // we're not interested in getting the process termination notification
1089 // if we are closing it ourselves
1090 wxProcess
*process
= m_process
;
1092 process
->SetNextHandler(NULL
);
1094 process
->CloseOutput();
1100 void MyPipeFrame::OnProcessTerm(wxProcessEvent
& WXUNUSED(event
))
1105 wxLogWarning(_T("The other process has terminated, closing"));