]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsexc.cpp
added some utils (tex2rtf, helpgen, makegen) to make system
[wxWidgets.git] / src / msw / utilsexc.cpp
CommitLineData
32592631 1/////////////////////////////////////////////////////////////////////////////
b568d04f 2// Name: msw/utilsexec.cpp
32592631
GL
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
3f4a0c5b 9// Licence: wxWindows license
32592631
GL
10/////////////////////////////////////////////////////////////////////////////
11
b568d04f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
32592631 20#ifdef __GNUG__
b568d04f 21 #pragma implementation
32592631
GL
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
b568d04f 28 #pragma hdrstop
32592631
GL
29#endif
30
31#ifndef WX_PRECOMP
b568d04f
VZ
32 #include "wx/utils.h"
33 #include "wx/app.h"
34 #include "wx/intl.h"
32592631
GL
35#endif
36
3e64d4e1 37#include "wx/log.h"
1044a386
JS
38
39#ifdef __WIN32__
b568d04f 40 #include "wx/process.h"
1044a386 41#endif
dbeac4bd 42
32592631 43#include "wx/msw/private.h"
dbeac4bd 44
32592631
GL
45#include <ctype.h>
46
5ea105e0 47#if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__)
b568d04f 48 #include <direct.h>
17dff81c 49#ifndef __MWERKS__
b568d04f 50 #include <dos.h>
32592631 51#endif
17dff81c 52#endif
32592631 53
b568d04f
VZ
54#if defined(__GNUWIN32__) && !defined(__TWIN32__)
55 #include <sys/unistd.h>
56 #include <sys/stat.h>
57c208c5 57#endif
32592631 58
5ea105e0 59#if defined(__WIN32__) && !defined(__WXWINE__)
32592631
GL
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__
3f4a0c5b
VZ
71 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
72 #include <errno.h>
73 #endif
32592631
GL
74#endif
75#include <stdarg.h>
76
b568d04f
VZ
77// ----------------------------------------------------------------------------
78// constants
79// ----------------------------------------------------------------------------
80
cb6827a8
VZ
81// this message is sent when the process we're waiting for terminates
82#define wxWM_PROC_TERMINATED (WM_USER + 10000)
32592631 83
b568d04f
VZ
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
91static const wxChar *gs_classForHiddenWindow = NULL;
92
93// ----------------------------------------------------------------------------
94// private types
95// ----------------------------------------------------------------------------
96
cb6827a8
VZ
97// structure describing the process we're being waiting for
98struct wxExecuteData
99{
100public:
101 ~wxExecuteData()
102 {
750b78ba 103#ifndef __WIN16__
cb6827a8
VZ
104 if ( !::CloseHandle(hProcess) )
105 {
106 wxLogLastError("CloseHandle(hProcess)");
107 }
750b78ba 108#endif
cb6827a8
VZ
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
32592631
GL
117};
118
b568d04f
VZ
119// ============================================================================
120// implementation
121// ============================================================================
5260b1c5
JS
122
123#ifdef __WIN32__
32592631
GL
124static DWORD wxExecuteThread(wxExecuteData *data)
125{
cb6827a8
VZ
126 WaitForSingleObject(data->hProcess, INFINITE);
127
128 // get the exit code
129 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
130 {
131 wxLogLastError("GetExitCodeProcess");
132 }
32592631 133
cb6827a8 134 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
223d09f6 135 wxT("process should have terminated") );
32592631 136
cb6827a8
VZ
137 // send a message indicating process termination to the window
138 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
e6045e08 139
cb6827a8 140 return 0;
32592631 141}
5260b1c5 142
cb6827a8
VZ
143// window procedure of a hidden window which is created just to receive
144// the notification message when a process exits
32592631
GL
145LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
146 WPARAM wParam, LPARAM lParam)
147{
cb6827a8
VZ
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 }
e6045e08 158
cb6827a8
VZ
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 }
cb6827a8 170
0d7ea902
VZ
171 return 0;
172 }
173 else
174 {
175 return DefWindowProc(hWnd, message, wParam, lParam);
176 }
32592631 177}
1044a386 178#endif
32592631 179
32592631
GL
180long wxExecute(const wxString& command, bool sync, wxProcess *handler)
181{
223d09f6 182 wxCHECK_MSG( !!command, 0, wxT("empty command in wxExecute") );
32592631 183
57c208c5 184#if defined(__WIN32__) && !defined(__TWIN32__)
cb6827a8
VZ
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;
32592631 223#ifdef __GNUWIN32__
cb6827a8
VZ
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
e6045e08 234
cb6827a8
VZ
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;
11c7d5b6 242 wxZeroMemory(si);
cf0b3979 243
cb6827a8
VZ
244 si.cb = sizeof(si);
245
246 PROCESS_INFORMATION pi;
247
248 if ( ::CreateProcess(
249 NULL, // application name (use only cmd line)
837e5743 250 (wxChar *)command.c_str(), // full command line
cb6827a8
VZ
251 NULL, // security attributes: defaults for both
252 NULL, // the process and its main thread
253 FALSE, // don't inherit handles
0d7ea902
VZ
254 CREATE_DEFAULT_ERROR_MODE |
255 CREATE_SUSPENDED, // flags
cb6827a8
VZ
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
0d7ea902 267 // register the class for the hidden window used for the notifications
b568d04f
VZ
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)");
b568d04f
VZ
281 }
282 }
283
cb6827a8
VZ
284 // create a hidden window to receive notification about process
285 // termination
b568d04f 286 HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL,
0d7ea902
VZ
287 WS_OVERLAPPEDWINDOW,
288 0, 0, 0, 0, NULL,
cb6827a8 289 (HMENU)NULL, wxGetInstance(), 0);
223d09f6 290 wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") );
e6045e08 291
cb6827a8
VZ
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;
e6045e08
VZ
298 if ( sync )
299 {
223d09f6 300 wxASSERT_MSG( !handler, wxT("wxProcess param ignored for sync execution") );
e6045e08
VZ
301
302 data->handler = NULL;
303 }
304 else
305 {
306 // may be NULL or not
307 data->handler = handler;
308 }
cb6827a8
VZ
309
310 DWORD tid;
311 HANDLE hThread = ::CreateThread(NULL,
312 0,
313 (LPTHREAD_START_ROUTINE)wxExecuteThread,
314 (void *)data,
315 0,
316 &tid);
317
0d7ea902
VZ
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
cb6827a8
VZ
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 }
e6045e08 340
cb6827a8
VZ
341 if ( !sync )
342 {
343 // clean up will be done when the process terminates
e6045e08
VZ
344
345 // return the pid
cb6827a8
VZ
346 return pi.dwProcessId;
347 }
e6045e08 348
0d7ea902
VZ
349 // waiting until command executed (disable everything while doing it)
350#if wxUSE_GUI
351 wxBeginBusyCursor();
352 wxEnableTopLevelWindows(FALSE);
353#endif // wxUSE_GUI
354
cb6827a8
VZ
355 while ( data->state )
356 wxYield();
e6045e08 357
0d7ea902
VZ
358#if wxUSE_GUI
359 wxEnableTopLevelWindows(TRUE);
360 wxEndBusyCursor();
361#endif // wxUSE_GUI
362
e6045e08 363 DWORD dwExitCode = data->dwExitCode;
cb6827a8
VZ
364 delete data;
365
e6045e08
VZ
366 // return the exit code
367 return dwExitCode;
cb6827a8
VZ
368#endif // 0/1
369#else // Win16
370 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
371 if (instanceID < 32) return(0);
e6045e08 372
cb6827a8
VZ
373 if (sync) {
374 int running;
375 do {
376 wxYield();
27a9bd48 377 running = GetModuleUsage((HINSTANCE)instanceID);
cb6827a8
VZ
378 } while (running);
379 }
380
381 return(instanceID);
382#endif // Win16/32
32592631 383}
c740f496
GL
384
385long wxExecute(char **argv, bool sync, wxProcess *handler)
386{
cb6827a8 387 wxString command;
e6045e08 388
cb6827a8
VZ
389 while ( *argv != NULL )
390 {
391 command << *argv++ << ' ';
392 }
393
394 command.RemoveLast();
395
396 return wxExecute(command, sync, handler);
c740f496 397}
006162a9 398
d9317fd4
VZ
399// ----------------------------------------------------------------------------
400// Metafile helpers
401// ----------------------------------------------------------------------------
402
403extern 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
418extern 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}