Include wx/choicdlg.h, wx/textdlg.h and wx/filedlg.h according to precompiled headers...
[wxWidgets.git] / src / common / process.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: process.cpp
3 // Purpose: Process termination classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
6 // Created: 24/06/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/process.h"
28
29 // ----------------------------------------------------------------------------
30 // event tables and such
31 // ----------------------------------------------------------------------------
32
33 DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
34
35 IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
36 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
37
38 // ============================================================================
39 // wxProcess implementation
40 // ============================================================================
41
42 // ----------------------------------------------------------------------------
43 // wxProcess creation
44 // ----------------------------------------------------------------------------
45
46 void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
47 {
48 if ( parent )
49 SetNextHandler(parent);
50
51 m_id = id;
52 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
53
54 #if wxUSE_STREAMS
55 m_inputStream = NULL;
56 m_errorStream = NULL;
57 m_outputStream = NULL;
58 #endif // wxUSE_STREAMS
59 }
60
61 /* static */
62 wxProcess *wxProcess::Open(const wxString& cmd, int flags)
63 {
64 wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." ));
65 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
66 if ( !wxExecute(cmd, flags, process) )
67 {
68 // couldn't launch the process
69 delete process;
70 return NULL;
71 }
72
73 return process;
74 }
75
76 // ----------------------------------------------------------------------------
77 // wxProcess termination
78 // ----------------------------------------------------------------------------
79
80 wxProcess::~wxProcess()
81 {
82 #if wxUSE_STREAMS
83 delete m_inputStream;
84 delete m_errorStream;
85 delete m_outputStream;
86 #endif // wxUSE_STREAMS
87 }
88
89 void wxProcess::OnTerminate(int pid, int status)
90 {
91 wxProcessEvent event(m_id, pid, status);
92
93 if ( !ProcessEvent(event) )
94 delete this;
95 //else: the object which processed the event is responsible for deleting
96 // us!
97 }
98
99 void wxProcess::Detach()
100 {
101 SetNextHandler(NULL);
102 }
103
104 // ----------------------------------------------------------------------------
105 // process IO redirection
106 // ----------------------------------------------------------------------------
107
108 #if wxUSE_STREAMS
109
110 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
111 wxOutputStream *outputStream,
112 wxInputStream *errorStream)
113 {
114 m_inputStream = inputSstream;
115 m_errorStream = errorStream;
116 m_outputStream = outputStream;
117 }
118
119 bool wxProcess::IsInputOpened() const
120 {
121 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
122 }
123
124 bool wxProcess::IsInputAvailable() const
125 {
126 return m_inputStream && m_inputStream->CanRead();
127 }
128
129 bool wxProcess::IsErrorAvailable() const
130 {
131 return m_errorStream && m_errorStream->CanRead();
132 }
133
134 #endif // wxUSE_STREAMS
135
136 // ----------------------------------------------------------------------------
137 // process killing
138 // ----------------------------------------------------------------------------
139
140 /* static */
141 wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
142 {
143 wxKillError rc;
144 (void)wxKill(pid, sig, &rc, flags);
145
146 return rc;
147 }
148
149 /* static */
150 bool wxProcess::Exists(int pid)
151 {
152 switch ( Kill(pid, wxSIGNONE) )
153 {
154 case wxKILL_OK:
155 case wxKILL_ACCESS_DENIED:
156 return true;
157
158 default:
159 case wxKILL_ERROR:
160 case wxKILL_BAD_SIGNAL:
161 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
162 // fall through
163
164 case wxKILL_NO_PROCESS:
165 return false;
166 }
167 }
168