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" wxWindows 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"
59 #include "wx/process.h"
61 #include "wx/mimetype.h"
67 // ----------------------------------------------------------------------------
68 // the usual application and main frame classes
69 // ----------------------------------------------------------------------------
71 // Define a new application type, each program should derive a class from wxApp
72 class MyApp
: public wxApp
75 // override base class virtuals
76 // ----------------------------
78 // this one is called on application startup and is a good place for the app
79 // initialization (doing it here and not in the ctor allows to have an error
80 // return: if OnInit() returns false, the application terminates)
81 virtual bool OnInit();
84 // Define an array of process pointers used by MyFrame
86 WX_DEFINE_ARRAY(MyPipedProcess
*, MyProcessesArray
);
88 // Define a new frame type: this is going to be our main frame
89 class MyFrame
: public wxFrame
93 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
95 // event handlers (these functions should _not_ be virtual)
96 void OnQuit(wxCommandEvent
& event
);
98 void OnKill(wxCommandEvent
& event
);
100 void OnClear(wxCommandEvent
& event
);
102 void OnSyncExec(wxCommandEvent
& event
);
103 void OnAsyncExec(wxCommandEvent
& event
);
104 void OnShell(wxCommandEvent
& event
);
105 void OnExecWithRedirect(wxCommandEvent
& event
);
106 void OnExecWithPipe(wxCommandEvent
& event
);
108 void OnPOpen(wxCommandEvent
& event
);
110 void OnFileExec(wxCommandEvent
& event
);
112 void OnAbout(wxCommandEvent
& event
);
114 // polling output of async processes
115 void OnTimer(wxTimerEvent
& event
);
116 void OnIdle(wxIdleEvent
& event
);
118 // for MyPipedProcess
119 void OnProcessTerminated(MyPipedProcess
*process
);
120 wxListBox
*GetLogListBox() const { return m_lbox
; }
123 void ShowOutput(const wxString
& cmd
,
124 const wxArrayString
& output
,
125 const wxString
& title
);
127 void DoAsyncExec(const wxString
& cmd
);
129 void AddAsyncProcess(MyPipedProcess
*process
)
131 if ( m_running
.IsEmpty() )
133 // we want to start getting the timer events to ensure that a
134 // steady stream of idle events comes in -- otherwise we
135 // wouldn't be able to poll the child process input
136 m_timerIdleWakeUp
.Start(100);
138 //else: the timer is already running
140 m_running
.Add(process
);
143 void RemoveAsyncProcess(MyPipedProcess
*process
)
145 m_running
.Remove(process
);
147 if ( m_running
.IsEmpty() )
149 // we don't need to get idle events all the time any more
150 m_timerIdleWakeUp
.Stop();
154 // the PID of the last process we launched asynchronously
157 // last command we executed
161 void OnDDEExec(wxCommandEvent
& event
);
162 void OnDDERequest(wxCommandEvent
& event
);
166 // last params of a DDE transaction
170 #endif // __WINDOWS__
174 MyProcessesArray m_running
;
176 // the idle event wake up timer
177 wxTimer m_timerIdleWakeUp
;
179 // any class wishing to process wxWindows events must use this macro
180 DECLARE_EVENT_TABLE()
183 // ----------------------------------------------------------------------------
184 // MyPipeFrame: allows the user to communicate with the child process
185 // ----------------------------------------------------------------------------
187 class MyPipeFrame
: public wxFrame
190 MyPipeFrame(wxFrame
*parent
,
195 void OnTextEnter(wxCommandEvent
& event
) { DoSend(); }
196 void OnBtnSend(wxCommandEvent
& event
) { DoSend(); }
197 void OnBtnGet(wxCommandEvent
& event
) { DoGet(); }
199 void OnClose(wxCloseEvent
& event
);
201 void OnProcessTerm(wxProcessEvent
& event
);
205 m_out
.WriteString(m_textIn
->GetValue() + '\n');
214 wxProcess
*m_process
;
216 wxTextInputStream m_in
;
217 wxTextOutputStream m_out
;
219 wxTextCtrl
*m_textIn
,
222 DECLARE_EVENT_TABLE()
225 // ----------------------------------------------------------------------------
226 // wxProcess-derived classes
227 // ----------------------------------------------------------------------------
229 // This is the handler for process termination events
230 class MyProcess
: public wxProcess
233 MyProcess(MyFrame
*parent
, const wxString
& cmd
)
234 : wxProcess(parent
), m_cmd(cmd
)
239 // instead of overriding this virtual function we might as well process the
240 // event from it in the frame class - this might be more convenient in some
242 virtual void OnTerminate(int pid
, int status
);
249 // A specialization of MyProcess for redirecting the output
250 class MyPipedProcess
: public MyProcess
253 MyPipedProcess(MyFrame
*parent
, const wxString
& cmd
)
254 : MyProcess(parent
, cmd
)
259 virtual void OnTerminate(int pid
, int status
);
261 virtual bool HasInput();
264 // A version of MyPipedProcess which also sends input to the stdin of the
266 class MyPipedProcess2
: public MyPipedProcess
269 MyPipedProcess2(MyFrame
*parent
, const wxString
& cmd
, const wxString
& input
)
270 : MyPipedProcess(parent
, cmd
), m_input(input
)
274 virtual bool HasInput();
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 // IDs for the controls and the menu commands
303 Exec_Btn_Send
= 1000,
307 static const wxChar
*DIALOG_TITLE
= _T("Exec sample");
309 // ----------------------------------------------------------------------------
310 // event tables and other macros for wxWindows
311 // ----------------------------------------------------------------------------
313 // the event tables connect the wxWindows events with the functions (event
314 // handlers) which process them. It can be also done at run-time, but for the
315 // simple menu events like this the static method is much simpler.
316 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
317 EVT_MENU(Exec_Quit
, MyFrame::OnQuit
)
318 EVT_MENU(Exec_Kill
, MyFrame::OnKill
)
319 EVT_MENU(Exec_ClearLog
, MyFrame::OnClear
)
321 EVT_MENU(Exec_SyncExec
, MyFrame::OnSyncExec
)
322 EVT_MENU(Exec_AsyncExec
, MyFrame::OnAsyncExec
)
323 EVT_MENU(Exec_Shell
, MyFrame::OnShell
)
324 EVT_MENU(Exec_Redirect
, MyFrame::OnExecWithRedirect
)
325 EVT_MENU(Exec_Pipe
, MyFrame::OnExecWithPipe
)
327 EVT_MENU(Exec_POpen
, MyFrame::OnPOpen
)
329 EVT_MENU(Exec_OpenFile
, MyFrame::OnFileExec
)
332 EVT_MENU(Exec_DDEExec
, MyFrame::OnDDEExec
)
333 EVT_MENU(Exec_DDERequest
, MyFrame::OnDDERequest
)
334 #endif // __WINDOWS__
336 EVT_MENU(Exec_About
, MyFrame::OnAbout
)
338 EVT_IDLE(MyFrame::OnIdle
)
340 EVT_TIMER(-1, MyFrame::OnTimer
)
343 BEGIN_EVENT_TABLE(MyPipeFrame
, wxFrame
)
344 EVT_BUTTON(Exec_Btn_Send
, MyPipeFrame::OnBtnSend
)
345 EVT_BUTTON(Exec_Btn_Get
, MyPipeFrame::OnBtnGet
)
347 EVT_TEXT_ENTER(-1, MyPipeFrame::OnTextEnter
)
349 EVT_CLOSE(MyPipeFrame::OnClose
)
351 EVT_END_PROCESS(-1, MyPipeFrame::OnProcessTerm
)
354 // Create a new application object: this macro will allow wxWindows to create
355 // the application object during program execution (it's better than using a
356 // static object for many reasons) and also declares the accessor function
357 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
361 // ============================================================================
363 // ============================================================================
365 // ----------------------------------------------------------------------------
366 // the application class
367 // ----------------------------------------------------------------------------
369 // `Main program' equivalent: the program execution "starts" here
372 // Create the main application window
373 MyFrame
*frame
= new MyFrame(_T("Exec wxWindows sample"),
374 wxDefaultPosition
, wxSize(500, 140));
376 // Show it and tell the application that it's our main window
380 // success: wxApp::OnRun() will be called which will enter the main message
381 // loop and the application will run. If we returned FALSE here, the
382 // application would exit immediately.
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
391 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
392 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
),
393 m_timerIdleWakeUp(this)
398 // we need this in order to allow the about menu relocation, since ABOUT is
399 // not the default id of the about menu
400 wxApp::s_macAboutMenuItemId
= Exec_About
;
404 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
405 menuFile
->Append(Exec_Kill
, _T("&Kill process...\tCtrl-K"),
406 _T("Kill a process by PID"));
407 menuFile
->AppendSeparator();
408 menuFile
->Append(Exec_ClearLog
, _T("&Clear log\tCtrl-C"),
409 _T("Clear the log window"));
410 menuFile
->AppendSeparator();
411 menuFile
->Append(Exec_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
413 wxMenu
*execMenu
= new wxMenu
;
414 execMenu
->Append(Exec_SyncExec
, _T("Sync &execution...\tCtrl-E"),
415 _T("Launch a program and return when it terminates"));
416 execMenu
->Append(Exec_AsyncExec
, _T("&Async execution...\tCtrl-A"),
417 _T("Launch a program and return immediately"));
418 execMenu
->Append(Exec_Shell
, _T("Execute &shell command...\tCtrl-S"),
419 _T("Launch a shell and execute a command in it"));
420 execMenu
->AppendSeparator();
421 execMenu
->Append(Exec_Redirect
, _T("Capture command &output...\tCtrl-O"),
422 _T("Launch a program and capture its output"));
423 execMenu
->Append(Exec_Pipe
, _T("&Pipe through command..."),
424 _T("Pipe a string through a filter"));
425 execMenu
->Append(Exec_POpen
, _T("&Open a pipe to a command...\tCtrl-P"),
426 _T("Open a pipe to and from another program"));
428 execMenu
->AppendSeparator();
429 execMenu
->Append(Exec_OpenFile
, _T("Open &file...\tCtrl-F"),
430 _T("Launch the command to open this kind of files"));
432 execMenu
->AppendSeparator();
433 execMenu
->Append(Exec_DDEExec
, _T("Execute command via &DDE...\tCtrl-D"));
434 execMenu
->Append(Exec_DDERequest
, _T("Send DDE &request...\tCtrl-R"));
437 wxMenu
*helpMenu
= new wxMenu(_T(""), wxMENU_TEAROFF
);
438 helpMenu
->Append(Exec_About
, _T("&About...\tF1"), _T("Show about dialog"));
440 // now append the freshly created menu to the menu bar...
441 wxMenuBar
*menuBar
= new wxMenuBar();
442 menuBar
->Append(menuFile
, _T("&File"));
443 menuBar
->Append(execMenu
, _T("&Exec"));
444 menuBar
->Append(helpMenu
, _T("&Help"));
446 // ... and attach this menu bar to the frame
449 // create the listbox in which we will show misc messages as they come
450 m_lbox
= new wxListBox(this, -1);
451 wxFont
font(12, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
,
452 wxFONTWEIGHT_NORMAL
);
454 m_lbox
->SetFont(font
);
457 // create a status bar just for fun (by default with 1 pane only)
459 SetStatusText(_T("Welcome to wxWindows exec sample!"));
460 #endif // wxUSE_STATUSBAR
463 // ----------------------------------------------------------------------------
464 // event handlers: file and help menu
465 // ----------------------------------------------------------------------------
467 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
469 // TRUE is to force the frame to close
473 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
478 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
480 wxMessageBox(_T("Exec wxWindows Sample\n© 2000-2002 Vadim Zeitlin"),
481 _T("About Exec"), wxOK
| wxICON_INFORMATION
, this);
484 void MyFrame::OnKill(wxCommandEvent
& WXUNUSED(event
))
486 long pid
= wxGetNumberFromUser(_T("Please specify the process to kill"),
490 // we need the full unsigned int range
499 static const wxString signalNames
[] =
501 _T("Just test (SIGNONE)"),
502 _T("Hangup (SIGHUP)"),
503 _T("Interrupt (SIGINT)"),
504 _T("Quit (SIGQUIT)"),
505 _T("Illegal instruction (SIGILL)"),
506 _T("Trap (SIGTRAP)"),
507 _T("Abort (SIGABRT)"),
508 _T("Emulated trap (SIGEMT)"),
509 _T("FP exception (SIGFPE)"),
510 _T("Kill (SIGKILL)"),
512 _T("Segment violation (SIGSEGV)"),
513 _T("System (SIGSYS)"),
514 _T("Broken pipe (SIGPIPE)"),
515 _T("Alarm (SIGALRM)"),
516 _T("Terminate (SIGTERM)"),
519 int sig
= wxGetSingleChoiceIndex(_T("How to kill the process?"),
521 WXSIZEOF(signalNames
), signalNames
,
526 wxFAIL_MSG( _T("unexpected return value") );
554 if ( wxProcess::Exists(pid
) )
555 wxLogStatus(_T("Process %ld is running."), pid
);
557 wxLogStatus(_T("No process with pid = %ld."), pid
);
561 wxKillError rc
= wxProcess::Kill(pid
, (wxSignal
)sig
);
562 if ( rc
== wxKILL_OK
)
564 wxLogStatus(_T("Process %ld killed with signal %d."), pid
, sig
);
568 static const wxChar
*errorText
[] =
571 _T("signal not supported"),
572 _T("permission denied"),
573 _T("no such process"),
574 _T("unspecified error"),
577 wxLogStatus(_T("Failed to kill process %ld with signal %d: %s"),
578 pid
, sig
, errorText
[rc
]);
583 // ----------------------------------------------------------------------------
584 // event handlers: exec menu
585 // ----------------------------------------------------------------------------
587 void MyFrame::DoAsyncExec(const wxString
& cmd
)
589 wxProcess
*process
= new MyProcess(this, cmd
);
590 m_pidLast
= wxExecute(cmd
, wxEXEC_ASYNC
, process
);
593 wxLogError( _T("Execution of '%s' failed."), cmd
.c_str() );
599 wxLogStatus( _T("Process %ld (%s) launched."),
600 m_pidLast
, cmd
.c_str() );
606 void MyFrame::OnSyncExec(wxCommandEvent
& WXUNUSED(event
))
608 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
615 wxLogStatus( _T("'%s' is running please wait..."), cmd
.c_str() );
617 int code
= wxExecute(cmd
, wxEXEC_SYNC
);
619 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
625 void MyFrame::OnAsyncExec(wxCommandEvent
& WXUNUSED(event
))
627 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
637 void MyFrame::OnShell(wxCommandEvent
& WXUNUSED(event
))
639 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
646 int code
= wxShell(cmd
);
647 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
652 void MyFrame::OnExecWithRedirect(wxCommandEvent
& WXUNUSED(event
))
654 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
662 switch ( wxMessageBox(_T("Execute it synchronously?"),
664 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
, this) )
680 wxArrayString output
, errors
;
681 int code
= wxExecute(cmd
, output
, errors
);
682 wxLogStatus(_T("command '%s' terminated with exit code %d."),
687 ShowOutput(cmd
, output
, _T("Output"));
688 ShowOutput(cmd
, errors
, _T("Errors"));
693 MyPipedProcess
*process
= new MyPipedProcess(this, cmd
);
694 if ( !wxExecute(cmd
, wxEXEC_ASYNC
, process
) )
696 wxLogError(_T("Execution of '%s' failed."), cmd
.c_str());
702 AddAsyncProcess(process
);
709 void MyFrame::OnExecWithPipe(wxCommandEvent
& WXUNUSED(event
))
712 m_cmdLast
= _T("tr [a-z] [A-Z]");
714 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
721 wxString input
= wxGetTextFromUser(_T("Enter the string to send to it: "),
726 // always execute the filter asynchronously
727 MyPipedProcess2
*process
= new MyPipedProcess2(this, cmd
, input
);
728 long pid
= wxExecute(cmd
, wxEXEC_ASYNC
, process
);
731 wxLogStatus( _T("Process %ld (%s) launched."), pid
, cmd
.c_str() );
733 AddAsyncProcess(process
);
737 wxLogError(_T("Execution of '%s' failed."), cmd
.c_str());
745 void MyFrame::OnPOpen(wxCommandEvent
& event
)
747 wxString cmd
= wxGetTextFromUser(_T("Enter the command to launch: "),
753 wxProcess
*process
= wxProcess::Open(cmd
);
756 wxLogError(_T("Failed to launch the command."));
760 wxOutputStream
*out
= process
->GetOutputStream();
763 wxLogError(_T("Failed to connect to child stdin"));
767 wxInputStream
*in
= process
->GetInputStream();
770 wxLogError(_T("Failed to connect to child stdout"));
774 new MyPipeFrame(this, cmd
, process
);
777 void MyFrame::OnFileExec(wxCommandEvent
& event
)
779 static wxString s_filename
;
781 wxString filename
= wxLoadFileSelector(_T(""), _T(""), s_filename
);
785 s_filename
= filename
;
787 wxString ext
= filename
.AfterFirst(_T('.'));
788 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
791 wxLogError(_T("Impossible to determine the file type for extension '%s'"),
797 bool ok
= ft
->GetOpenCommand(&cmd
,
798 wxFileType::MessageParameters(filename
, _T("")));
802 wxLogError(_T("Impossible to find out how to open files of extension '%s'"),
810 // ----------------------------------------------------------------------------
812 // ----------------------------------------------------------------------------
816 bool MyFrame::GetDDEServer()
818 wxString server
= wxGetTextFromUser(_T("Server to connect to:"),
819 DIALOG_TITLE
, m_server
);
825 wxString topic
= wxGetTextFromUser(_T("DDE topic:"), DIALOG_TITLE
, m_topic
);
831 wxString cmd
= wxGetTextFromUser(_T("DDE command:"), DIALOG_TITLE
, m_cmdDde
);
840 void MyFrame::OnDDEExec(wxCommandEvent
& WXUNUSED(event
))
842 if ( !GetDDEServer() )
846 wxConnectionBase
*conn
= client
.MakeConnection("", m_server
, m_topic
);
849 wxLogError(_T("Failed to connect to the DDE server '%s'."),
854 if ( !conn
->Execute(m_cmdDde
) )
856 wxLogError(_T("Failed to execute command '%s' via DDE."),
861 wxLogStatus(_T("Successfully executed DDE command"));
866 void MyFrame::OnDDERequest(wxCommandEvent
& WXUNUSED(event
))
868 if ( !GetDDEServer() )
872 wxConnectionBase
*conn
= client
.MakeConnection("", m_server
, m_topic
);
875 wxLogError(_T("Failed to connect to the DDE server '%s'."),
880 if ( !conn
->Request(m_cmdDde
) )
882 wxLogError(_T("Failed to send request '%s' via DDE."),
887 wxLogStatus(_T("Successfully sent DDE request."));
892 #endif // __WINDOWS__
894 // ----------------------------------------------------------------------------
896 // ----------------------------------------------------------------------------
899 void MyFrame::OnIdle(wxIdleEvent
& event
)
901 size_t count
= m_running
.GetCount();
902 for ( size_t n
= 0; n
< count
; n
++ )
904 if ( m_running
[n
]->HasInput() )
911 void MyFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
916 void MyFrame::OnProcessTerminated(MyPipedProcess
*process
)
918 RemoveAsyncProcess(process
);
922 void MyFrame::ShowOutput(const wxString
& cmd
,
923 const wxArrayString
& output
,
924 const wxString
& title
)
926 size_t count
= output
.GetCount();
930 m_lbox
->Append(wxString::Format(_T("--- %s of '%s' ---"),
931 title
.c_str(), cmd
.c_str()));
933 for ( size_t n
= 0; n
< count
; n
++ )
935 m_lbox
->Append(output
[n
]);
938 m_lbox
->Append(wxString::Format(_T("--- End of %s ---"),
939 title
.Lower().c_str()));
942 // ----------------------------------------------------------------------------
944 // ----------------------------------------------------------------------------
946 void MyProcess::OnTerminate(int pid
, int status
)
948 wxLogStatus(m_parent
, _T("Process %u ('%s') terminated with exit code %d."),
949 pid
, m_cmd
.c_str(), status
);
951 // we're not needed any more
955 // ----------------------------------------------------------------------------
957 // ----------------------------------------------------------------------------
959 bool MyPipedProcess::HasInput()
961 bool hasInput
= FALSE
;
963 if ( IsInputAvailable() )
965 wxTextInputStream
tis(*GetInputStream());
967 // this assumes that the output is always line buffered
969 msg
<< m_cmd
<< _T(" (stdout): ") << tis
.ReadLine();
971 m_parent
->GetLogListBox()->Append(msg
);
976 if ( IsErrorAvailable() )
978 wxTextInputStream
tis(*GetErrorStream());
980 // this assumes that the output is always line buffered
982 msg
<< m_cmd
<< _T(" (stderr): ") << tis
.ReadLine();
984 m_parent
->GetLogListBox()->Append(msg
);
992 void MyPipedProcess::OnTerminate(int pid
, int status
)
994 // show the rest of the output
998 m_parent
->OnProcessTerminated(this);
1000 MyProcess::OnTerminate(pid
, status
);
1003 // ----------------------------------------------------------------------------
1005 // ----------------------------------------------------------------------------
1007 bool MyPipedProcess2::HasInput()
1011 wxTextOutputStream
os(*GetOutputStream());
1012 os
.WriteString(m_input
);
1017 // call us once again - may be we'll have output
1021 return MyPipedProcess::HasInput();
1024 // ============================================================================
1025 // MyPipeFrame implementation
1026 // ============================================================================
1028 MyPipeFrame::MyPipeFrame(wxFrame
*parent
,
1029 const wxString
& cmd
,
1031 : wxFrame(parent
, -1, cmd
),
1033 // in a real program we'd check that the streams are !NULL here
1034 m_in(*process
->GetInputStream()),
1035 m_out(*process
->GetOutputStream())
1037 m_process
->SetNextHandler(this);
1039 wxPanel
*panel
= new wxPanel(this, -1);
1041 m_textIn
= new wxTextCtrl(panel
, -1, _T(""),
1042 wxDefaultPosition
, wxDefaultSize
,
1043 wxTE_PROCESS_ENTER
);
1044 m_textOut
= new wxTextCtrl(panel
, -1, _T(""));
1045 m_textOut
->SetEditable(FALSE
);
1047 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
1048 sizerTop
->Add(m_textIn
, 0, wxGROW
| wxALL
, 5);
1050 wxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
1051 sizerBtns
->Add(new wxButton(panel
, Exec_Btn_Send
, _T("&Send")), 0,
1053 sizerBtns
->Add(new wxButton(panel
, Exec_Btn_Get
, _T("&Get")), 0,
1056 sizerTop
->Add(sizerBtns
, 0, wxCENTRE
| wxALL
, 5);
1057 sizerTop
->Add(m_textOut
, 0, wxGROW
| wxALL
, 5);
1059 panel
->SetSizer(sizerTop
);
1060 sizerTop
->Fit(this);
1065 void MyPipeFrame::DoGet()
1067 // we don't have any way to be notified when any input appears on the
1068 // stream so we have to poll it :-(
1070 // NB: this really must be done because otherwise the other program might
1071 // not have enough time to receive or process our data and we'd read
1073 while ( !m_process
->IsInputAvailable() && m_process
->IsInputOpened() )
1076 m_textOut
->SetValue(m_in
.ReadLine());
1079 void MyPipeFrame::OnClose(wxCloseEvent
& event
)
1083 // we're not interested in getting the process termination notification
1084 // if we are closing it ourselves
1085 wxProcess
*process
= m_process
;
1087 process
->SetNextHandler(NULL
);
1089 process
->CloseOutput();
1095 void MyPipeFrame::OnProcessTerm(wxProcessEvent
& event
)
1100 wxLogWarning(_T("The other process has terminated, closing"));