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