]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsexc.cpp
wxHtmlFilterHTML adds fake <meta> tag so that the parser knows charset
[wxWidgets.git] / src / msw / utilsexc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/utilsexec.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/utils.h"
33 #include "wx/app.h"
34 #include "wx/intl.h"
35 #endif
36
37 #include "wx/log.h"
38
39 #ifdef __WIN32__
40 #include "wx/process.h"
41 #endif
42
43 #include "wx/msw/private.h"
44
45 #include <ctype.h>
46
47 #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__)
48 #include <direct.h>
49 #ifndef __MWERKS__
50 #include <dos.h>
51 #endif
52 #endif
53
54 #if defined(__GNUWIN32__) && !defined(__TWIN32__)
55 #include <sys/unistd.h>
56 #include <sys/stat.h>
57 #endif
58
59 #if defined(__WIN32__) && !defined(__WXWINE__)
60 #include <io.h>
61
62 #ifndef __GNUWIN32__
63 #include <shellapi.h>
64 #endif
65 #endif
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #ifndef __WATCOMC__
71 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
72 #include <errno.h>
73 #endif
74 #endif
75 #include <stdarg.h>
76
77 // ----------------------------------------------------------------------------
78 // constants
79 // ----------------------------------------------------------------------------
80
81 // this message is sent when the process we're waiting for terminates
82 #define wxWM_PROC_TERMINATED (WM_USER + 10000)
83
84 // ----------------------------------------------------------------------------
85 // this module globals
86 // ----------------------------------------------------------------------------
87
88 // we need to create a hidden window to receive the process termination
89 // notifications and for this we need a (Win) class name for it which we will
90 // register the first time it's needed
91 static const wxChar *gs_classForHiddenWindow = NULL;
92
93 // ----------------------------------------------------------------------------
94 // private types
95 // ----------------------------------------------------------------------------
96
97 // structure describing the process we're being waiting for
98 struct wxExecuteData
99 {
100 public:
101 ~wxExecuteData()
102 {
103 #ifndef __WIN16__
104 if ( !::CloseHandle(hProcess) )
105 {
106 wxLogLastError("CloseHandle(hProcess)");
107 }
108 #endif
109 }
110
111 HWND hWnd; // window to send wxWM_PROC_TERMINATED to
112 HANDLE hProcess; // handle of the process
113 DWORD dwProcessId; // pid of the process
114 wxProcess *handler;
115 DWORD dwExitCode; // the exit code of the process
116 bool state; // set to FALSE when the process finishes
117 };
118
119 // ============================================================================
120 // implementation
121 // ============================================================================
122
123 #ifdef __WIN32__
124 static DWORD wxExecuteThread(wxExecuteData *data)
125 {
126 WaitForSingleObject(data->hProcess, INFINITE);
127
128 // get the exit code
129 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
130 {
131 wxLogLastError("GetExitCodeProcess");
132 }
133
134 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
135 wxT("process should have terminated") );
136
137 // send a message indicating process termination to the window
138 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
139
140 return 0;
141 }
142
143 // window procedure of a hidden window which is created just to receive
144 // the notification message when a process exits
145 LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
146 WPARAM wParam, LPARAM lParam)
147 {
148 if ( message == wxWM_PROC_TERMINATED )
149 {
150 DestroyWindow(hWnd); // we don't need it any more
151
152 wxExecuteData *data = (wxExecuteData *)lParam;
153 if ( data->handler )
154 {
155 data->handler->OnTerminate((int)data->dwProcessId,
156 (int)data->dwExitCode);
157 }
158
159 if ( data->state )
160 {
161 // we're executing synchronously, tell the waiting thread
162 // that the process finished
163 data->state = 0;
164 }
165 else
166 {
167 // asynchronous execution - we should do the clean up
168 delete data;
169 }
170
171 return 0;
172 }
173 else
174 {
175 return DefWindowProc(hWnd, message, wParam, lParam);
176 }
177 }
178 #endif
179
180 long wxExecute(const wxString& command, bool sync, wxProcess *handler)
181 {
182 wxCHECK_MSG( !!command, 0, wxT("empty command in wxExecute") );
183
184 #if defined(__WIN32__) && !defined(__TWIN32__)
185 // the old code is disabled because we really need a process handle
186 // if we want to execute it asynchronously or even just get its
187 // return code and for this we must use CreateProcess() and not
188 // ShellExecute()
189 #if 0
190 // isolate command and arguments
191 wxString commandName;
192 bool insideQuotes = FALSE;
193 const char *pc;
194 for ( pc = command.c_str(); *pc != '\0'; pc++ )
195 {
196 switch ( *pc )
197 {
198 case ' ':
199 case '\t':
200 if ( !insideQuotes )
201 break;
202 // fall through
203
204 case '"':
205 insideQuotes = !insideQuotes;
206 // fall through
207
208 default:
209 commandName += *pc;
210 continue; // skip the next break
211 }
212
213 // only reached for space not inside quotes
214 break;
215 }
216
217 wxString commandArgs = pc;
218
219 wxWindow *winTop = wxTheApp->GetTopWindow();
220 HWND hwndTop = (HWND)(winTop ? winTop->GetHWND() : 0);
221
222 HANDLE result;
223 #ifdef __GNUWIN32__
224 result = ShellExecute(hwndTop,
225 (const wchar_t)"open",
226 (const wchar_t)commandName,
227 (const wchar_t)commandArgs,
228 (const wchar_t)NULL,
229 SW_SHOWNORMAL);
230 #else // !GNUWIN32
231 result = ShellExecute(hwndTop, "open", commandName,
232 commandArgs, NULL, SW_SHOWNORMAL);
233 #endif // GNUWIN32
234
235 if ( ((long)result) <= 32 )
236 wxLogSysError(_("Can't execute command '%s'"), command.c_str());
237
238 return result;
239 #else // 1
240 // create the process
241 STARTUPINFO si;
242 wxZeroMemory(si);
243
244 si.cb = sizeof(si);
245
246 PROCESS_INFORMATION pi;
247
248 if ( ::CreateProcess(
249 NULL, // application name (use only cmd line)
250 (wxChar *)command.c_str(), // full command line
251 NULL, // security attributes: defaults for both
252 NULL, // the process and its main thread
253 FALSE, // don't inherit handles
254 CREATE_DEFAULT_ERROR_MODE |
255 CREATE_SUSPENDED, // flags
256 NULL, // environment (use the same)
257 NULL, // current directory (use the same)
258 &si, // startup info (unused here)
259 &pi // process info
260 ) == 0 )
261 {
262 wxLogSysError(_("Execution of command '%s' failed"), command.c_str());
263
264 return 0;
265 }
266
267 // register the class for the hidden window used for the notifications
268 if ( !gs_classForHiddenWindow )
269 {
270 gs_classForHiddenWindow = _T("wxHiddenWindow");
271
272 WNDCLASS wndclass;
273 wxZeroMemory(wndclass);
274 wndclass.lpfnWndProc = (WNDPROC)wxExecuteWindowCbk;
275 wndclass.hInstance = wxGetInstance();
276 wndclass.lpszClassName = gs_classForHiddenWindow;
277
278 if ( !::RegisterClass(&wndclass) )
279 {
280 wxLogLastError("RegisterClass(hidden window)");
281 }
282 }
283
284 // create a hidden window to receive notification about process
285 // termination
286 HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL,
287 WS_OVERLAPPEDWINDOW,
288 0, 0, 0, 0, NULL,
289 (HMENU)NULL, wxGetInstance(), 0);
290 wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") );
291
292 // Alloc data
293 wxExecuteData *data = new wxExecuteData;
294 data->hProcess = pi.hProcess;
295 data->dwProcessId = pi.dwProcessId;
296 data->hWnd = hwnd;
297 data->state = sync;
298 if ( sync )
299 {
300 wxASSERT_MSG( !handler, wxT("wxProcess param ignored for sync execution") );
301
302 data->handler = NULL;
303 }
304 else
305 {
306 // may be NULL or not
307 data->handler = handler;
308 }
309
310 DWORD tid;
311 HANDLE hThread = ::CreateThread(NULL,
312 0,
313 (LPTHREAD_START_ROUTINE)wxExecuteThread,
314 (void *)data,
315 0,
316 &tid);
317
318 // resume process we created now - whether the thread creation succeeded or
319 // not
320 if ( ::ResumeThread(pi.hThread) == (DWORD)-1 )
321 {
322 // ignore it - what can we do?
323 wxLogLastError("ResumeThread in wxExecute");
324 }
325
326 // close unneeded handle
327 if ( !::CloseHandle(pi.hThread) )
328 wxLogLastError("CloseHandle(hThread)");
329
330 if ( !hThread )
331 {
332 wxLogLastError("CreateThread in wxExecute");
333
334 DestroyWindow(hwnd);
335 delete data;
336
337 // the process still started up successfully...
338 return pi.dwProcessId;
339 }
340
341 if ( !sync )
342 {
343 // clean up will be done when the process terminates
344
345 // return the pid
346 return pi.dwProcessId;
347 }
348
349 // waiting until command executed (disable everything while doing it)
350 #if wxUSE_GUI
351 wxBeginBusyCursor();
352 wxEnableTopLevelWindows(FALSE);
353 #endif // wxUSE_GUI
354
355 while ( data->state )
356 wxYield();
357
358 #if wxUSE_GUI
359 wxEnableTopLevelWindows(TRUE);
360 wxEndBusyCursor();
361 #endif // wxUSE_GUI
362
363 DWORD dwExitCode = data->dwExitCode;
364 delete data;
365
366 // return the exit code
367 return dwExitCode;
368 #endif // 0/1
369 #else // Win16
370 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
371 if (instanceID < 32) return(0);
372
373 if (sync) {
374 int running;
375 do {
376 wxYield();
377 running = GetModuleUsage((HINSTANCE)instanceID);
378 } while (running);
379 }
380
381 return(instanceID);
382 #endif // Win16/32
383 }
384
385 long wxExecute(char **argv, bool sync, wxProcess *handler)
386 {
387 wxString command;
388
389 while ( *argv != NULL )
390 {
391 command << *argv++ << ' ';
392 }
393
394 command.RemoveLast();
395
396 return wxExecute(command, sync, handler);
397 }
398
399 // ----------------------------------------------------------------------------
400 // Metafile helpers
401 // ----------------------------------------------------------------------------
402
403 extern void PixelToHIMETRIC(LONG *x, LONG *y)
404 {
405 ScreenHDC hdcRef;
406
407 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
408 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
409 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
410 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
411
412 *x *= (iWidthMM * 100);
413 *x /= iWidthPels;
414 *y *= (iHeightMM * 100);
415 *y /= iHeightPels;
416 }
417
418 extern void HIMETRICToPixel(LONG *x, LONG *y)
419 {
420 ScreenHDC hdcRef;
421
422 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
423 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
424 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
425 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
426
427 *x *= iWidthPels;
428 *x /= (iWidthMM * 100);
429 *y *= iHeightPels;
430 *y /= (iHeightMM * 100);
431 }