]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsexc.cpp
drawing optimization fix
[wxWidgets.git] / src / msw / utilsexc.cpp
CommitLineData
32592631
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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#ifdef __GNUG__
cfe780fb 13#pragma implementation
32592631
GL
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/setup.h"
25#include "wx/utils.h"
26#include "wx/app.h"
27#endif
28
3e64d4e1 29#include "wx/log.h"
32592631
GL
30#include "wx/msw/private.h"
31#include <windows.h>
32
33#include <ctype.h>
34
35#ifndef __GNUWIN32__
36#include <direct.h>
37#include <dos.h>
38#endif
39
40#ifdef __GNUWIN32__
41#include <sys/unistd.h>
42#include <sys/stat.h>
32592631
GL
43#endif
44
45#ifdef __WIN32__
46#include <io.h>
47
48#ifndef __GNUWIN32__
49#include <shellapi.h>
50#endif
51#endif
52
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#ifndef __WATCOMC__
57#if !(defined(_MSC_VER) && (_MSC_VER > 800))
58#include <errno.h>
59#endif
60#endif
61#include <stdarg.h>
62
63#define wxEXECUTE_WIN_MESSAGE 10000
64
65struct wxExecuteData {
66 HWND window;
67 HINSTANCE process;
68 wxProcess *handler;
69 char state;
70};
71
5260b1c5
JS
72
73#ifdef __WIN32__
32592631
GL
74static DWORD wxExecuteThread(wxExecuteData *data)
75{
76 WaitForSingleObject(data->process, INFINITE);
77
78 // Send an implicit message to the window
79 SendMessage(data->window, wxEXECUTE_WIN_MESSAGE, 0, (LPARAM)data);
80
81 return 0;
82}
5260b1c5
JS
83#endif
84
32592631
GL
85
86LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
87 WPARAM wParam, LPARAM lParam)
88{
89 wxExecuteData *data = (wxExecuteData *)lParam;
90
91 if (message == wxEXECUTE_WIN_MESSAGE) {
92 DestroyWindow(hWnd);
93 if (data->handler)
94 data->handler->OnTerminate((int)data->process);
95
96 if (data->state)
97 data->state = 0;
98 else
99 delete data;
100 }
101 return 0;
102}
103
104extern char wxPanelClassName[];
105
106long wxExecute(const wxString& command, bool sync, wxProcess *handler)
107{
108 if (command == "")
109 return 0;
110
111#ifdef __WIN32__
112 char * cl;
113 char * argp;
114 int clen;
115 HINSTANCE result;
116 DWORD dresult;
117 HWND window;
118 wxExecuteData *data;
119 DWORD tid;
120
121 // copy the command line
122 clen = command.Length();
123 if (!clen) return -1;
124 cl = (char *) calloc( 1, 256);
125 if (!cl) return -1;
126 strcpy( cl, WXSTRINGCAST command);
127
128 // isolate command and arguments
129 argp = strchr( cl, ' ');
130 if (argp)
131 *argp++ = '\0';
132
133#ifdef __GNUWIN32__
cfe780fb
JS
134 result = ShellExecute((HWND) (wxTheApp->GetTopWindow() ? (HWND) wxTheApp->GetTopWindow()->GetHWND() : NULL),
135 (const wchar_t) "open", (const wchar_t) cl, (const wchar_t) argp,
32592631
GL
136 (const wchar_t) NULL, SW_SHOWNORMAL);
137#else
138 result = ShellExecute( (HWND) (wxTheApp->GetTopWindow() ? wxTheApp->GetTopWindow()->GetHWND() : NULL),
139 "open", cl, argp, NULL, SW_SHOWNORMAL);
140#endif
141
142 if (((long)result) <= 32) {
143 free(cl);
3e64d4e1
VZ
144
145 wxLogSysError(_("Can't execute command '%s'"), command.c_str());
32592631
GL
146 return 0;
147 }
148
149 // Alloc data
150 data = new wxExecuteData;
151
152 // Create window
153 window = CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
154 (HMENU) NULL, wxGetInstance(), 0);
155
156 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC) wxExecuteWindowCbk,
157 wxGetInstance());
158
159 SetWindowLong(window, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
160 SetWindowLong(window, GWL_USERDATA, (LONG) data);
161
162 data->process = result;
163 data->window = window;
164 data->state = sync;
165 data->handler = (sync) ? NULL : handler;
166
167 dresult = (DWORD)CreateThread(NULL, 0,
168 (LPTHREAD_START_ROUTINE)wxExecuteThread,
169 (void *)data, 0, &tid);
170 if (dresult == 0) {
171 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
172 DestroyWindow(window);
173 return (long)result;
174 }
175
176 if (!sync)
177 {
178 free(cl);
179 return (long)result;
180 }
181
182 // waiting until command executed
183 while (data->state)
184 wxYield();
185
186 free(cl);
187 return 0;
188#else
189 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
190 if (instanceID < 32) return(0);
191
192 if (sync) {
193 int running;
194 do {
195 wxYield();
196 running = GetModuleUsage((HANDLE)instanceID);
197 } while (running);
198 }
199 return(instanceID);
200#endif
201}
c740f496
GL
202
203long wxExecute(char **argv, bool sync, wxProcess *handler)
204{
205 wxString command = "";
206
207 while (*argv != NULL) {
208 command += *argv;
209 command += ' ';
210 argv++;
211 }
212 command.RemoveLast();
213 return wxExecute(command, sync, handler);
214}