]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8caa4ed1 | 2 | // Name: msw/utils.cpp |
2bda0e17 KB |
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 | |
743e0a66 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
b568d04f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
a3b46648 | 21 | // #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now. |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
a4f96412 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
a4f96412 VZ |
32 | #include "wx/utils.h" |
33 | #include "wx/app.h" | |
34 | #include "wx/cursor.h" | |
1f0500b3 VZ |
35 | #include "wx/intl.h" |
36 | #include "wx/log.h" | |
743e0a66 | 37 | #endif //WX_PRECOMP |
2bda0e17 | 38 | |
26993c4f GRG |
39 | // In some mingws there is a missing extern "C" int the winsock header, |
40 | // so we put it here just to be safe. Note that this must appear _before_ | |
ec5d7799 | 41 | // #include "wx/msw/private.h" which itself includes <windows.h>, as this |
26993c4f GRG |
42 | // one in turn includes <winsock.h> unless we define WIN32_LEAN_AND_MEAN. |
43 | // | |
04ef50df | 44 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) |
26993c4f GRG |
45 | extern "C" { |
46 | #include <winsock.h> // we use socket functions in wxGetFullHostName() | |
47 | } | |
48 | #endif | |
49 | ||
b568d04f VZ |
50 | #include "wx/msw/private.h" // includes <windows.h> |
51 | ||
2bda0e17 KB |
52 | #include "wx/timer.h" |
53 | ||
04ef50df | 54 | #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) |
a4f96412 | 55 | #include <direct.h> |
ce3ed50d | 56 | |
a4f96412 VZ |
57 | #ifndef __MWERKS__ |
58 | #include <dos.h> | |
59 | #endif | |
743e0a66 | 60 | #endif //GNUWIN32 |
2bda0e17 | 61 | |
57c208c5 | 62 | #if defined(__GNUWIN32__) && !defined(__TWIN32__) |
a4f96412 VZ |
63 | #include <sys/unistd.h> |
64 | #include <sys/stat.h> | |
743e0a66 VZ |
65 | #endif //GNUWIN32 |
66 | ||
2bda0e17 KB |
67 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs |
68 | // this (3.1 I believe) and how to test for it. | |
69 | // If this works for Borland 4.0 as well, then no worries. | |
a4f96412 | 70 | #include <dir.h> |
2bda0e17 KB |
71 | #endif |
72 | ||
a4f96412 VZ |
73 | // VZ: there is some code using NetXXX() functions to get the full user name: |
74 | // I don't think it's a good idea because they don't work under Win95 and | |
75 | // seem to return the same as wxGetUserId() under NT. If you really want | |
76 | // to use them, just #define USE_NET_API | |
77 | #undef USE_NET_API | |
78 | ||
79 | #ifdef USE_NET_API | |
80 | #include <lm.h> | |
81 | #endif // USE_NET_API | |
82 | ||
04ef50df | 83 | #if defined(__WIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__) |
a4f96412 | 84 | #include <io.h> |
2bda0e17 | 85 | |
a4f96412 VZ |
86 | #ifndef __GNUWIN32__ |
87 | #include <shellapi.h> | |
88 | #endif | |
2bda0e17 KB |
89 | #endif |
90 | ||
2bda0e17 | 91 | #ifndef __WATCOMC__ |
3f4a0c5b VZ |
92 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
93 | #include <errno.h> | |
94 | #endif | |
2bda0e17 | 95 | #endif |
2bda0e17 | 96 | |
c030b70f | 97 | //// BEGIN for console support: VC++ only |
631f1bfe | 98 | #ifdef __VISUALC__ |
c030b70f | 99 | |
3f4a0c5b | 100 | #include "wx/msw/msvcrt.h" |
c030b70f | 101 | |
3f4a0c5b | 102 | #include <fcntl.h> |
c030b70f | 103 | |
3f4a0c5b | 104 | #include "wx/ioswrap.h" |
c030b70f | 105 | |
1e6feb95 | 106 | #include "wx/ioswrap.h" |
c030b70f JS |
107 | |
108 | /* Need to undef new if including crtdbg.h */ | |
109 | # ifdef new | |
110 | # undef new | |
111 | # endif | |
112 | ||
1e6d9499 | 113 | #ifndef __WIN16__ |
c030b70f | 114 | # include <crtdbg.h> |
1e6d9499 | 115 | #endif |
c030b70f JS |
116 | |
117 | # if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
f6bcfd97 | 118 | # define new new(__TFILE__,__LINE__) |
c030b70f JS |
119 | # endif |
120 | ||
631f1bfe JS |
121 | #endif |
122 | // __VISUALC__ | |
c030b70f JS |
123 | /// END for console support |
124 | ||
b568d04f VZ |
125 | // ---------------------------------------------------------------------------- |
126 | // constants | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
2bda0e17 | 129 | // In the WIN.INI file |
223d09f6 | 130 | static const wxChar WX_SECTION[] = wxT("wxWindows"); |
b568d04f VZ |
131 | static const wxChar eUSERNAME[] = wxT("UserName"); |
132 | ||
133 | // these are only used under Win16 | |
04ef50df | 134 | #if !defined(__WIN32__) && !defined(__WXMICROWIN__) |
223d09f6 KB |
135 | static const wxChar eHOSTNAME[] = wxT("HostName"); |
136 | static const wxChar eUSERID[] = wxT("UserId"); | |
b568d04f VZ |
137 | #endif // !Win32 |
138 | ||
04ef50df JS |
139 | #ifndef __WXMICROWIN__ |
140 | ||
b568d04f VZ |
141 | // ============================================================================ |
142 | // implementation | |
143 | // ============================================================================ | |
2bda0e17 | 144 | |
b568d04f VZ |
145 | // ---------------------------------------------------------------------------- |
146 | // get host name and related | |
147 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 148 | |
1f0500b3 | 149 | // Get hostname only (without domain name) |
837e5743 | 150 | bool wxGetHostName(wxChar *buf, int maxSize) |
2bda0e17 | 151 | { |
04ef50df | 152 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) |
b568d04f VZ |
153 | DWORD nSize = maxSize; |
154 | if ( !::GetComputerName(buf, &nSize) ) | |
155 | { | |
f6bcfd97 | 156 | wxLogLastError(wxT("GetComputerName")); |
b568d04f VZ |
157 | |
158 | return FALSE; | |
159 | } | |
160 | ||
161 | return TRUE; | |
2bda0e17 | 162 | #else |
b568d04f VZ |
163 | wxChar *sysname; |
164 | const wxChar *default_host = wxT("noname"); | |
165 | ||
166 | if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) { | |
167 | GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1); | |
168 | } else | |
169 | wxStrncpy(buf, sysname, maxSize - 1); | |
170 | buf[maxSize] = wxT('\0'); | |
171 | return *buf ? TRUE : FALSE; | |
2bda0e17 KB |
172 | #endif |
173 | } | |
174 | ||
1f0500b3 | 175 | // get full hostname (with domain name if possible) |
b568d04f VZ |
176 | bool wxGetFullHostName(wxChar *buf, int maxSize) |
177 | { | |
04ef50df | 178 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) |
1f0500b3 VZ |
179 | // TODO should use GetComputerNameEx() when available |
180 | WSADATA wsa; | |
181 | if ( WSAStartup(MAKEWORD(1, 1), &wsa) == 0 ) | |
182 | { | |
183 | wxString host; | |
184 | char bufA[256]; | |
185 | if ( gethostname(bufA, WXSIZEOF(bufA)) == 0 ) | |
186 | { | |
187 | // gethostname() won't usually include the DNS domain name, for | |
188 | // this we need to work a bit more | |
189 | if ( !strchr(bufA, '.') ) | |
190 | { | |
191 | struct hostent *pHostEnt = gethostbyname(bufA); | |
192 | ||
193 | if ( pHostEnt ) | |
194 | { | |
195 | // Windows will use DNS internally now | |
196 | pHostEnt = gethostbyaddr(pHostEnt->h_addr, 4, PF_INET); | |
197 | } | |
198 | ||
199 | if ( pHostEnt ) | |
200 | { | |
201 | host = pHostEnt->h_name; | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | WSACleanup(); | |
207 | ||
208 | if ( !!host ) | |
209 | { | |
210 | wxStrncpy(buf, host, maxSize); | |
211 | ||
212 | return TRUE; | |
213 | } | |
214 | } | |
215 | #endif // Win32 | |
216 | ||
b568d04f VZ |
217 | return wxGetHostName(buf, maxSize); |
218 | } | |
219 | ||
2bda0e17 | 220 | // Get user ID e.g. jacs |
837e5743 | 221 | bool wxGetUserId(wxChar *buf, int maxSize) |
2bda0e17 | 222 | { |
04ef50df | 223 | #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) |
0a144271 VZ |
224 | DWORD nSize = maxSize; |
225 | if ( ::GetUserName(buf, &nSize) == 0 ) | |
226 | { | |
a4f96412 | 227 | // actually, it does happen on Win9x if the user didn't log on |
223d09f6 | 228 | DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize); |
a4f96412 VZ |
229 | if ( res == 0 ) |
230 | { | |
231 | // not found | |
232 | return FALSE; | |
233 | } | |
0a144271 VZ |
234 | } |
235 | ||
236 | return TRUE; | |
0a144271 | 237 | #else // Win16 or Win32s |
b568d04f VZ |
238 | wxChar *user; |
239 | const wxChar *default_id = wxT("anonymous"); | |
2bda0e17 | 240 | |
b568d04f VZ |
241 | // Can't assume we have NIS (PC-NFS) or some other ID daemon |
242 | // So we ... | |
243 | if ( (user = wxGetenv(wxT("USER"))) == NULL && | |
244 | (user = wxGetenv(wxT("LOGNAME"))) == NULL ) | |
245 | { | |
246 | // Use wxWindows configuration data (comming soon) | |
247 | GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1); | |
248 | } | |
249 | else | |
250 | { | |
251 | wxStrncpy(buf, user, maxSize - 1); | |
252 | } | |
a4f96412 | 253 | |
b568d04f | 254 | return *buf ? TRUE : FALSE; |
2bda0e17 KB |
255 | #endif |
256 | } | |
257 | ||
258 | // Get user name e.g. Julian Smart | |
837e5743 | 259 | bool wxGetUserName(wxChar *buf, int maxSize) |
2bda0e17 | 260 | { |
0a144271 | 261 | #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__) |
b568d04f VZ |
262 | extern HANDLE g_hPenWin; // PenWindows Running? |
263 | if (g_hPenWin) | |
264 | { | |
265 | // PenWindows Does have a user concept! | |
266 | // Get the current owner of the recognizer | |
267 | GetPrivateProfileString("Current", "User", default_name, wxBuffer, maxSize - 1, "PENWIN.INI"); | |
268 | strncpy(buf, wxBuffer, maxSize - 1); | |
269 | } | |
270 | else | |
2bda0e17 | 271 | #endif |
b568d04f | 272 | { |
a4f96412 | 273 | #ifdef USE_NET_API |
b568d04f VZ |
274 | CHAR szUserName[256]; |
275 | if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) ) | |
276 | return FALSE; | |
a4f96412 | 277 | |
b568d04f VZ |
278 | // TODO how to get the domain name? |
279 | CHAR *szDomain = ""; | |
a4f96412 | 280 | |
b568d04f VZ |
281 | // the code is based on the MSDN example (also see KB article Q119670) |
282 | WCHAR wszUserName[256]; // Unicode user name | |
283 | WCHAR wszDomain[256]; | |
284 | LPBYTE ComputerName; | |
a4f96412 | 285 | |
b568d04f | 286 | USER_INFO_2 *ui2; // User structure |
a4f96412 | 287 | |
b568d04f VZ |
288 | // Convert ANSI user name and domain to Unicode |
289 | MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1, | |
290 | wszUserName, WXSIZEOF(wszUserName) ); | |
291 | MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1, | |
292 | wszDomain, WXSIZEOF(wszDomain) ); | |
a4f96412 | 293 | |
b568d04f VZ |
294 | // Get the computer name of a DC for the domain. |
295 | if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success ) | |
296 | { | |
297 | wxLogError(wxT("Can not find domain controller")); | |
a4f96412 | 298 | |
b568d04f VZ |
299 | goto error; |
300 | } | |
a4f96412 | 301 | |
b568d04f VZ |
302 | // Look up the user on the DC |
303 | NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName, | |
304 | (LPWSTR)&wszUserName, | |
305 | 2, // level - we want USER_INFO_2 | |
306 | (LPBYTE *) &ui2 ); | |
307 | switch ( status ) | |
308 | { | |
309 | case NERR_Success: | |
310 | // ok | |
311 | break; | |
a4f96412 | 312 | |
b568d04f VZ |
313 | case NERR_InvalidComputer: |
314 | wxLogError(wxT("Invalid domain controller name.")); | |
a4f96412 | 315 | |
b568d04f | 316 | goto error; |
a4f96412 | 317 | |
b568d04f VZ |
318 | case NERR_UserNotFound: |
319 | wxLogError(wxT("Invalid user name '%s'."), szUserName); | |
a4f96412 | 320 | |
b568d04f | 321 | goto error; |
a4f96412 | 322 | |
b568d04f VZ |
323 | default: |
324 | wxLogSysError(wxT("Can't get information about user")); | |
a4f96412 | 325 | |
b568d04f VZ |
326 | goto error; |
327 | } | |
a4f96412 | 328 | |
b568d04f VZ |
329 | // Convert the Unicode full name to ANSI |
330 | WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1, | |
331 | buf, maxSize, NULL, NULL ); | |
a4f96412 | 332 | |
b568d04f | 333 | return TRUE; |
a4f96412 VZ |
334 | |
335 | error: | |
b568d04f | 336 | wxLogError(wxT("Couldn't look up full user name.")); |
a4f96412 | 337 | |
b568d04f | 338 | return FALSE; |
a4f96412 | 339 | #else // !USE_NET_API |
b568d04f VZ |
340 | // Could use NIS, MS-Mail or other site specific programs |
341 | // Use wxWindows configuration data | |
342 | bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxT(""), buf, maxSize - 1) != 0; | |
343 | if ( !ok ) | |
344 | { | |
345 | ok = wxGetUserId(buf, maxSize); | |
346 | } | |
0a144271 | 347 | |
b568d04f VZ |
348 | if ( !ok ) |
349 | { | |
350 | wxStrncpy(buf, wxT("Unknown User"), maxSize); | |
351 | } | |
a4f96412 | 352 | #endif // Win32/16 |
b568d04f | 353 | } |
0a144271 | 354 | |
b568d04f | 355 | return TRUE; |
2bda0e17 KB |
356 | } |
357 | ||
b568d04f | 358 | const wxChar* wxGetHomeDir(wxString *pstr) |
2bda0e17 | 359 | { |
b568d04f | 360 | wxString& strDir = *pstr; |
2bda0e17 | 361 | |
b568d04f VZ |
362 | #if defined(__UNIX__) && !defined(__TWIN32__) |
363 | const wxChar *szHome = wxGetenv("HOME"); | |
364 | if ( szHome == NULL ) { | |
365 | // we're homeless... | |
366 | wxLogWarning(_("can't find user's HOME, using current directory.")); | |
367 | strDir = wxT("."); | |
368 | } | |
369 | else | |
370 | strDir = szHome; | |
2bda0e17 | 371 | |
b568d04f VZ |
372 | // add a trailing slash if needed |
373 | if ( strDir.Last() != wxT('/') ) | |
374 | strDir << wxT('/'); | |
375 | #else // Windows | |
376 | #ifdef __WIN32__ | |
377 | const wxChar *szHome = wxGetenv(wxT("HOMEDRIVE")); | |
378 | if ( szHome != NULL ) | |
379 | strDir << szHome; | |
380 | szHome = wxGetenv(wxT("HOMEPATH")); | |
381 | if ( szHome != NULL ) { | |
382 | strDir << szHome; | |
2bda0e17 | 383 | |
b568d04f VZ |
384 | // the idea is that under NT these variables have default values |
385 | // of "%systemdrive%:" and "\\". As we don't want to create our | |
386 | // config files in the root directory of the system drive, we will | |
387 | // create it in our program's dir. However, if the user took care | |
388 | // to set HOMEPATH to something other than "\\", we suppose that he | |
389 | // knows what he is doing and use the supplied value. | |
390 | if ( wxStrcmp(szHome, wxT("\\")) != 0 ) | |
391 | return strDir.c_str(); | |
392 | } | |
2bda0e17 | 393 | |
b568d04f VZ |
394 | #else // Win16 |
395 | // Win16 has no idea about home, so use the working directory instead | |
396 | #endif // WIN16/32 | |
2bda0e17 | 397 | |
b568d04f VZ |
398 | // 260 was taken from windef.h |
399 | #ifndef MAX_PATH | |
400 | #define MAX_PATH 260 | |
401 | #endif | |
2bda0e17 | 402 | |
b568d04f VZ |
403 | wxString strPath; |
404 | ::GetModuleFileName(::GetModuleHandle(NULL), | |
405 | strPath.GetWriteBuf(MAX_PATH), MAX_PATH); | |
406 | strPath.UngetWriteBuf(); | |
2bda0e17 | 407 | |
b568d04f VZ |
408 | // extract the dir name |
409 | wxSplitPath(strPath, &strDir, NULL, NULL); | |
58a33cb4 | 410 | |
b568d04f VZ |
411 | #endif // UNIX/Win |
412 | ||
413 | return strDir.c_str(); | |
afb74891 VZ |
414 | } |
415 | ||
33ac7e6f | 416 | wxChar *wxGetUserHome(const wxString& WXUNUSED(user)) |
2bda0e17 | 417 | { |
b568d04f VZ |
418 | // VZ: the old code here never worked for user != "" anyhow! Moreover, it |
419 | // returned sometimes a malloc()'d pointer, sometimes a pointer to a | |
420 | // static buffer and sometimes I don't even know what. | |
421 | static wxString s_home; | |
2bda0e17 | 422 | |
b568d04f | 423 | return (wxChar *)wxGetHomeDir(&s_home); |
2bda0e17 KB |
424 | } |
425 | ||
b568d04f | 426 | bool wxDirExists(const wxString& dir) |
2bda0e17 | 427 | { |
04ef50df JS |
428 | #ifdef __WXMICROWIN__ |
429 | return wxPathExist(dir); | |
430 | #elif defined(__WIN32__) | |
cf1eeea3 | 431 | DWORD attribs = GetFileAttributes(dir); |
3897b707 | 432 | return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY)); |
b568d04f VZ |
433 | #else // Win16 |
434 | #ifdef __BORLANDC__ | |
435 | struct ffblk fileInfo; | |
436 | #else | |
437 | struct find_t fileInfo; | |
438 | #endif | |
b568d04f VZ |
439 | // In Borland findfirst has a different argument |
440 | // ordering from _dos_findfirst. But _dos_findfirst | |
441 | // _should_ be ok in both MS and Borland... why not? | |
442 | #ifdef __BORLANDC__ | |
443 | return (findfirst(dir, &fileInfo, _A_SUBDIR) == 0 && | |
444 | (fileInfo.ff_attrib & _A_SUBDIR) != 0); | |
445 | #else | |
446 | return (_dos_findfirst(dir, _A_SUBDIR, &fileInfo) == 0) && | |
447 | ((fileInfo.attrib & _A_SUBDIR) != 0); | |
448 | #endif | |
449 | #endif // Win32/16 | |
450 | } | |
2bda0e17 | 451 | |
308978f6 VZ |
452 | // ---------------------------------------------------------------------------- |
453 | // env vars | |
454 | // ---------------------------------------------------------------------------- | |
455 | ||
456 | bool wxGetEnv(const wxString& var, wxString *value) | |
457 | { | |
788722ac JS |
458 | #ifdef __WIN16__ |
459 | const wxChar* ret = wxGetenv(var); | |
460 | if (ret) | |
461 | { | |
462 | *value = ret; | |
463 | return TRUE; | |
464 | } | |
465 | else | |
466 | return FALSE; | |
467 | #else | |
308978f6 VZ |
468 | // first get the size of the buffer |
469 | DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0); | |
470 | if ( !dwRet ) | |
471 | { | |
472 | // this means that there is no such variable | |
473 | return FALSE; | |
474 | } | |
475 | ||
476 | if ( value ) | |
477 | { | |
478 | (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet); | |
479 | value->UngetWriteBuf(); | |
480 | } | |
481 | ||
482 | return TRUE; | |
788722ac | 483 | #endif |
308978f6 VZ |
484 | } |
485 | ||
1fb45475 VZ |
486 | bool wxSetEnv(const wxString& var, const wxChar *value) |
487 | { | |
488 | // some compilers have putenv() or _putenv() or _wputenv() but it's better | |
489 | // to always use Win32 function directly instead of dealing with them | |
490 | #if defined(__WIN32__) | |
491 | if ( !::SetEnvironmentVariable(var, value) ) | |
492 | { | |
493 | wxLogLastError(_T("SetEnvironmentVariable")); | |
494 | ||
495 | return FALSE; | |
496 | } | |
497 | ||
498 | return TRUE; | |
499 | #else // no way to set env vars | |
500 | return FALSE; | |
501 | #endif | |
502 | } | |
503 | ||
b568d04f VZ |
504 | // ---------------------------------------------------------------------------- |
505 | // process management | |
506 | // ---------------------------------------------------------------------------- | |
507 | ||
50567b69 VZ |
508 | #ifdef __WIN32__ |
509 | ||
510 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() | |
511 | struct wxFindByPidParams | |
b568d04f | 512 | { |
50567b69 VZ |
513 | wxFindByPidParams() { hwnd = 0; pid = 0; } |
514 | ||
515 | // the HWND used to return the result | |
516 | HWND hwnd; | |
517 | ||
518 | // the PID we're looking from | |
519 | DWORD pid; | |
520 | }; | |
521 | ||
522 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
523 | // level) window belonging to the given process | |
524 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
525 | { | |
526 | DWORD pid; | |
527 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
528 | ||
529 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
530 | if ( pid == params->pid ) | |
531 | { | |
532 | // remember the window we found | |
533 | params->hwnd = hwnd; | |
534 | ||
535 | // return FALSE to stop the enumeration | |
536 | return FALSE; | |
537 | } | |
538 | ||
539 | // continue enumeration | |
540 | return TRUE; | |
541 | } | |
e949bf13 | 542 | |
50567b69 VZ |
543 | #endif // __WIN32__ |
544 | ||
545 | int wxKill(long pid, wxSignal sig, wxKillError *krc) | |
546 | { | |
547 | #ifdef __WIN32__ | |
548 | // get the process handle to operate on | |
549 | HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | | |
550 | PROCESS_TERMINATE | | |
551 | PROCESS_QUERY_INFORMATION, | |
552 | FALSE, // not inheritable | |
553 | (DWORD)pid); | |
554 | if ( hProcess == NULL ) | |
e949bf13 | 555 | { |
50567b69 | 556 | if ( krc ) |
e949bf13 | 557 | { |
50567b69 | 558 | if ( ::GetLastError() == ERROR_ACCESS_DENIED ) |
e949bf13 | 559 | { |
50567b69 VZ |
560 | *krc = wxKILL_ACCESS_DENIED; |
561 | } | |
562 | else | |
563 | { | |
564 | *krc = wxKILL_NO_PROCESS; | |
565 | } | |
566 | } | |
567 | ||
568 | return -1; | |
569 | } | |
570 | ||
571 | bool ok = TRUE; | |
572 | switch ( sig ) | |
573 | { | |
574 | case wxSIGKILL: | |
575 | // kill the process forcefully returning -1 as error code | |
576 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
577 | { | |
578 | wxLogSysError(_("Failed to kill process %d"), pid); | |
579 | ||
580 | if ( krc ) | |
e949bf13 | 581 | { |
50567b69 VZ |
582 | // this is not supposed to happen if we could open the |
583 | // process | |
584 | *krc = wxKILL_ERROR; | |
e949bf13 | 585 | } |
50567b69 VZ |
586 | |
587 | ok = FALSE; | |
e949bf13 | 588 | } |
50567b69 VZ |
589 | break; |
590 | ||
591 | case wxSIGNONE: | |
592 | // do nothing, we just want to test for process existence | |
593 | break; | |
594 | ||
595 | default: | |
596 | // any other signal means "terminate" | |
597 | { | |
598 | wxFindByPidParams params; | |
599 | params.pid = (DWORD)pid; | |
600 | ||
601 | // EnumWindows() has nice semantics: it returns 0 if it found | |
602 | // something or if an error occured and non zero if it | |
603 | // enumerated all the window | |
604 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
605 | { | |
606 | // did we find any window? | |
607 | if ( params.hwnd ) | |
608 | { | |
609 | // tell the app to close | |
610 | // | |
611 | // NB: this is the harshest way, the app won't have | |
612 | // opportunity to save any files, for example, but | |
613 | // this is probably what we want here. If not we | |
614 | // can also use SendMesageTimeout(WM_CLOSE) | |
615 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
616 | { | |
617 | wxLogLastError(_T("PostMessage(WM_QUIT)")); | |
618 | } | |
619 | } | |
620 | else // it was an error then | |
621 | { | |
622 | wxLogLastError(_T("EnumWindows")); | |
623 | ||
624 | ok = FALSE; | |
625 | } | |
626 | } | |
627 | else // no windows for this PID | |
628 | { | |
629 | if ( krc ) | |
630 | { | |
631 | *krc = wxKILL_ERROR; | |
632 | } | |
633 | ||
634 | ok = FALSE; | |
635 | } | |
636 | } | |
637 | } | |
638 | ||
639 | // the return code | |
640 | DWORD rc; | |
641 | ||
642 | if ( ok ) | |
643 | { | |
644 | // as we wait for a short time, we can use just WaitForSingleObject() | |
645 | // and not MsgWaitForMultipleObjects() | |
646 | switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) ) | |
647 | { | |
648 | case WAIT_OBJECT_0: | |
649 | // process terminated | |
650 | if ( !::GetExitCodeProcess(hProcess, &rc) ) | |
651 | { | |
652 | wxLogLastError(_T("GetExitCodeProcess")); | |
653 | } | |
654 | break; | |
655 | ||
656 | default: | |
657 | wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") ); | |
658 | // fall through | |
659 | ||
660 | case WAIT_FAILED: | |
661 | wxLogLastError(_T("WaitForSingleObject")); | |
662 | // fall through | |
663 | ||
664 | case WAIT_TIMEOUT: | |
665 | if ( krc ) | |
666 | { | |
667 | *krc = wxKILL_ERROR; | |
668 | } | |
669 | ||
670 | rc = STILL_ACTIVE; | |
671 | break; | |
e949bf13 JS |
672 | } |
673 | } | |
50567b69 VZ |
674 | else // !ok |
675 | { | |
676 | // just to suppress the warnings about uninitialized variable | |
677 | rc = 0; | |
678 | } | |
e949bf13 | 679 | |
50567b69 | 680 | ::CloseHandle(hProcess); |
b568d04f | 681 | |
50567b69 VZ |
682 | // the return code is the same as from Unix kill(): 0 if killed |
683 | // successfully or -1 on error | |
684 | if ( sig == wxSIGNONE ) | |
e949bf13 | 685 | { |
50567b69 VZ |
686 | if ( ok && rc == STILL_ACTIVE ) |
687 | { | |
688 | // there is such process => success | |
e949bf13 | 689 | return 0; |
50567b69 | 690 | } |
e949bf13 | 691 | } |
50567b69 | 692 | else // not SIGNONE |
e949bf13 | 693 | { |
50567b69 VZ |
694 | if ( ok && rc != STILL_ACTIVE ) |
695 | { | |
696 | // killed => success | |
e949bf13 | 697 | return 0; |
50567b69 | 698 | } |
e949bf13 | 699 | } |
50567b69 VZ |
700 | #else // Win15 |
701 | wxFAIL_MSG( _T("not implemented") ); | |
702 | #endif // Win32/Win16 | |
703 | ||
704 | // error | |
705 | return -1; | |
2bda0e17 KB |
706 | } |
707 | ||
b568d04f VZ |
708 | // Execute a program in an Interactive Shell |
709 | bool wxShell(const wxString& command) | |
2bda0e17 | 710 | { |
b568d04f VZ |
711 | wxChar *shell = wxGetenv(wxT("COMSPEC")); |
712 | if ( !shell ) | |
713 | shell = wxT("\\COMMAND.COM"); | |
714 | ||
715 | wxString cmd; | |
716 | if ( !command ) | |
717 | { | |
718 | // just the shell | |
719 | cmd = shell; | |
720 | } | |
721 | else | |
722 | { | |
723 | // pass the command to execute to the command processor | |
724 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
725 | } | |
726 | ||
0d7ea902 | 727 | return wxExecute(cmd, TRUE /* sync */) != 0; |
2bda0e17 KB |
728 | } |
729 | ||
b568d04f VZ |
730 | // ---------------------------------------------------------------------------- |
731 | // misc | |
732 | // ---------------------------------------------------------------------------- | |
733 | ||
734 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
735 | long wxGetFreeMemory() | |
2bda0e17 | 736 | { |
b568d04f VZ |
737 | #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__) |
738 | MEMORYSTATUS memStatus; | |
739 | memStatus.dwLength = sizeof(MEMORYSTATUS); | |
740 | GlobalMemoryStatus(&memStatus); | |
741 | return memStatus.dwAvailPhys; | |
742 | #else | |
743 | return (long)GetFreeSpace(0); | |
744 | #endif | |
2bda0e17 KB |
745 | } |
746 | ||
747 | // Emit a beeeeeep | |
634903fd | 748 | void wxBell() |
2bda0e17 | 749 | { |
b568d04f | 750 | ::MessageBeep((UINT)-1); // default sound |
2bda0e17 KB |
751 | } |
752 | ||
bdc72a22 | 753 | wxString wxGetOsDescription() |
2bda0e17 | 754 | { |
bdc72a22 VZ |
755 | #ifdef __WIN32__ |
756 | wxString str; | |
757 | ||
758 | OSVERSIONINFO info; | |
759 | wxZeroMemory(info); | |
760 | ||
761 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
762 | if ( ::GetVersionEx(&info) ) | |
763 | { | |
764 | switch ( info.dwPlatformId ) | |
765 | { | |
766 | case VER_PLATFORM_WIN32s: | |
767 | str = _("Win32s on Windows 3.1"); | |
768 | break; | |
769 | ||
770 | case VER_PLATFORM_WIN32_WINDOWS: | |
771 | str.Printf(_("Windows 9%c"), | |
a46a73a6 | 772 | info.dwMinorVersion == 0 ? _T('5') : _T('8')); |
bdc72a22 VZ |
773 | if ( !wxIsEmpty(info.szCSDVersion) ) |
774 | { | |
775 | str << _T(" (") << info.szCSDVersion << _T(')'); | |
776 | } | |
777 | break; | |
778 | ||
779 | case VER_PLATFORM_WIN32_NT: | |
780 | str.Printf(_T("Windows NT %lu.%lu (build %lu"), | |
781 | info.dwMajorVersion, | |
782 | info.dwMinorVersion, | |
783 | info.dwBuildNumber); | |
784 | if ( !wxIsEmpty(info.szCSDVersion) ) | |
785 | { | |
786 | str << _T(", ") << info.szCSDVersion; | |
787 | } | |
788 | str << _T(')'); | |
789 | break; | |
790 | } | |
791 | } | |
792 | else | |
793 | { | |
794 | wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen | |
795 | } | |
796 | ||
797 | return str; | |
798 | #else // Win16 | |
799 | return _("Windows 3.1"); | |
800 | #endif // Win32/16 | |
801 | } | |
6f65e337 | 802 | |
bdc72a22 VZ |
803 | int wxGetOsVersion(int *majorVsn, int *minorVsn) |
804 | { | |
2432b92d | 805 | #if defined(__WIN32__) && !defined(__SC__) |
bdc72a22 VZ |
806 | OSVERSIONINFO info; |
807 | wxZeroMemory(info); | |
808 | ||
809 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
810 | if ( ::GetVersionEx(&info) ) | |
811 | { | |
812 | if (majorVsn) | |
813 | *majorVsn = info.dwMajorVersion; | |
814 | if (minorVsn) | |
815 | *minorVsn = info.dwMinorVersion; | |
ec5d7799 | 816 | |
bdc72a22 VZ |
817 | switch ( info.dwPlatformId ) |
818 | { | |
819 | case VER_PLATFORM_WIN32s: | |
820 | return wxWIN32S; | |
821 | ||
822 | case VER_PLATFORM_WIN32_WINDOWS: | |
823 | return wxWIN95; | |
824 | ||
825 | case VER_PLATFORM_WIN32_NT: | |
826 | return wxWINDOWS_NT; | |
827 | } | |
828 | } | |
829 | ||
830 | return wxWINDOWS; // error if we get here, return generic value | |
831 | #else // Win16 | |
832 | int retValue = wxWINDOWS; | |
833 | #ifdef __WINDOWS_386__ | |
834 | retValue = wxWIN386; | |
835 | #else | |
836 | #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS | |
837 | extern HANDLE g_hPenWin; | |
838 | retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS; | |
839 | #endif | |
840 | #endif | |
841 | ||
842 | if (majorVsn) | |
843 | *majorVsn = 3; | |
844 | if (minorVsn) | |
845 | *minorVsn = 1; | |
846 | ||
847 | return retValue; | |
6f65e337 | 848 | #endif |
2bda0e17 KB |
849 | } |
850 | ||
b568d04f VZ |
851 | // ---------------------------------------------------------------------------- |
852 | // sleep functions | |
853 | // ---------------------------------------------------------------------------- | |
854 | ||
855 | #if wxUSE_GUI | |
856 | ||
1e6feb95 VZ |
857 | #if wxUSE_TIMER |
858 | ||
b568d04f VZ |
859 | // Sleep for nSecs seconds. Attempt a Windows implementation using timers. |
860 | static bool gs_inTimer = FALSE; | |
861 | ||
1e6feb95 | 862 | class wxSleepTimer : public wxTimer |
b568d04f VZ |
863 | { |
864 | public: | |
865 | virtual void Notify() | |
866 | { | |
867 | gs_inTimer = FALSE; | |
868 | Stop(); | |
869 | } | |
870 | }; | |
871 | ||
872 | static wxTimer *wxTheSleepTimer = NULL; | |
873 | ||
874 | void wxUsleep(unsigned long milliseconds) | |
875 | { | |
876 | #ifdef __WIN32__ | |
877 | ::Sleep(milliseconds); | |
1e6feb95 | 878 | #else // !Win32 |
b568d04f VZ |
879 | if (gs_inTimer) |
880 | return; | |
881 | ||
882 | wxTheSleepTimer = new wxSleepTimer; | |
883 | gs_inTimer = TRUE; | |
884 | wxTheSleepTimer->Start(milliseconds); | |
885 | while (gs_inTimer) | |
886 | { | |
887 | if (wxTheApp->Pending()) | |
888 | wxTheApp->Dispatch(); | |
889 | } | |
890 | delete wxTheSleepTimer; | |
891 | wxTheSleepTimer = NULL; | |
1e6feb95 | 892 | #endif // Win32/!Win32 |
b568d04f VZ |
893 | } |
894 | ||
895 | void wxSleep(int nSecs) | |
896 | { | |
897 | if (gs_inTimer) | |
898 | return; | |
899 | ||
900 | wxTheSleepTimer = new wxSleepTimer; | |
901 | gs_inTimer = TRUE; | |
902 | wxTheSleepTimer->Start(nSecs*1000); | |
903 | while (gs_inTimer) | |
904 | { | |
905 | if (wxTheApp->Pending()) | |
906 | wxTheApp->Dispatch(); | |
907 | } | |
908 | delete wxTheSleepTimer; | |
909 | wxTheSleepTimer = NULL; | |
910 | } | |
911 | ||
912 | // Consume all events until no more left | |
913 | void wxFlushEvents() | |
914 | { | |
915 | // wxYield(); | |
916 | } | |
917 | ||
1e6feb95 VZ |
918 | #endif // wxUSE_TIMER |
919 | ||
b568d04f VZ |
920 | #elif defined(__WIN32__) // wxUSE_GUI |
921 | ||
922 | void wxUsleep(unsigned long milliseconds) | |
923 | { | |
924 | ::Sleep(milliseconds); | |
925 | } | |
926 | ||
927 | void wxSleep(int nSecs) | |
928 | { | |
929 | wxUsleep(1000*nSecs); | |
930 | } | |
931 | ||
932 | #endif // wxUSE_GUI/!wxUSE_GUI | |
04ef50df JS |
933 | #endif |
934 | // __WXMICROWIN__ | |
b568d04f VZ |
935 | |
936 | // ---------------------------------------------------------------------------- | |
937 | // deprecated (in favour of wxLog) log functions | |
938 | // ---------------------------------------------------------------------------- | |
939 | ||
940 | #if wxUSE_GUI | |
941 | ||
942 | // Output a debug mess., in a system dependent fashion. | |
04ef50df | 943 | #ifndef __WXMICROWIN__ |
b568d04f VZ |
944 | void wxDebugMsg(const wxChar *fmt ...) |
945 | { | |
946 | va_list ap; | |
947 | static wxChar buffer[512]; | |
948 | ||
949 | if (!wxTheApp->GetWantDebugOutput()) | |
950 | return ; | |
951 | ||
952 | va_start(ap, fmt); | |
953 | ||
954 | wvsprintf(buffer,fmt,ap) ; | |
955 | OutputDebugString((LPCTSTR)buffer) ; | |
956 | ||
957 | va_end(ap); | |
958 | } | |
959 | ||
960 | // Non-fatal error: pop up message box and (possibly) continue | |
961 | void wxError(const wxString& msg, const wxString& title) | |
962 | { | |
963 | wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg); | |
964 | if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title, | |
965 | MB_ICONSTOP | MB_YESNO) == IDNO) | |
966 | wxExit(); | |
967 | } | |
968 | ||
969 | // Fatal error: pop up message box and abort | |
970 | void wxFatalError(const wxString& msg, const wxString& title) | |
971 | { | |
972 | wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg); | |
973 | FatalAppExit(0, (LPCTSTR)wxBuffer); | |
974 | } | |
04ef50df | 975 | #endif // __WXMICROWIN__ |
b568d04f VZ |
976 | |
977 | // ---------------------------------------------------------------------------- | |
978 | // functions to work with .INI files | |
979 | // ---------------------------------------------------------------------------- | |
980 | ||
2bda0e17 | 981 | // Reading and writing resources (eg WIN.INI, .Xdefaults) |
47d67540 | 982 | #if wxUSE_RESOURCES |
2bda0e17 KB |
983 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) |
984 | { | |
223d09f6 | 985 | if (file != wxT("")) |
8fb3a512 JS |
986 | // Don't know what the correct cast should be, but it doesn't |
987 | // compile in BC++/16-bit without this cast. | |
988 | #if !defined(__WIN32__) | |
989 | return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0); | |
990 | #else | |
837e5743 | 991 | return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0); |
8fb3a512 | 992 | #endif |
2bda0e17 | 993 | else |
837e5743 | 994 | return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0); |
2bda0e17 KB |
995 | } |
996 | ||
997 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
998 | { | |
b568d04f VZ |
999 | wxString buf; |
1000 | buf.Printf(wxT("%.4f"), value); | |
1001 | ||
1002 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1003 | } |
1004 | ||
1005 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
1006 | { | |
b568d04f VZ |
1007 | wxString buf; |
1008 | buf.Printf(wxT("%ld"), value); | |
1009 | ||
1010 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1011 | } |
1012 | ||
1013 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
1014 | { | |
b568d04f VZ |
1015 | wxString buf; |
1016 | buf.Printf(wxT("%d"), value); | |
1017 | ||
1018 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1019 | } |
1020 | ||
837e5743 | 1021 | bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file) |
2bda0e17 | 1022 | { |
b568d04f VZ |
1023 | static const wxChar defunkt[] = wxT("$$default"); |
1024 | if (file != wxT("")) | |
1025 | { | |
1026 | int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1027 | (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file); | |
1028 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1029 | return FALSE; | |
1030 | } | |
1031 | else | |
1032 | { | |
1033 | int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1034 | (LPTSTR)wxBuffer, 1000); | |
1035 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1036 | return FALSE; | |
2bda0e17 | 1037 | } |
b568d04f VZ |
1038 | if (*value) delete[] (*value); |
1039 | *value = copystring(wxBuffer); | |
1040 | return TRUE; | |
1041 | } | |
2bda0e17 KB |
1042 | |
1043 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
1044 | { | |
b568d04f VZ |
1045 | wxChar *s = NULL; |
1046 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1047 | if (succ) | |
1048 | { | |
1049 | *value = (float)wxStrtod(s, NULL); | |
1050 | delete[] s; | |
1051 | return TRUE; | |
1052 | } | |
1053 | else return FALSE; | |
2bda0e17 KB |
1054 | } |
1055 | ||
1056 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
1057 | { | |
b568d04f VZ |
1058 | wxChar *s = NULL; |
1059 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1060 | if (succ) | |
1061 | { | |
1062 | *value = wxStrtol(s, NULL, 10); | |
1063 | delete[] s; | |
1064 | return TRUE; | |
1065 | } | |
1066 | else return FALSE; | |
2bda0e17 KB |
1067 | } |
1068 | ||
1069 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
1070 | { | |
b568d04f VZ |
1071 | wxChar *s = NULL; |
1072 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1073 | if (succ) | |
1074 | { | |
1075 | *value = (int)wxStrtol(s, NULL, 10); | |
1076 | delete[] s; | |
1077 | return TRUE; | |
1078 | } | |
1079 | else return FALSE; | |
2bda0e17 | 1080 | } |
47d67540 | 1081 | #endif // wxUSE_RESOURCES |
2bda0e17 | 1082 | |
634903fd | 1083 | // --------------------------------------------------------------------------- |
25889d3c | 1084 | // helper functions for showing a "busy" cursor |
634903fd VZ |
1085 | // --------------------------------------------------------------------------- |
1086 | ||
bfbd6dc1 VZ |
1087 | static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor |
1088 | static HCURSOR gs_wxBusyCursorOld = 0; // old cursor | |
634903fd | 1089 | static int gs_wxBusyCursorCount = 0; |
2bda0e17 | 1090 | |
bfbd6dc1 VZ |
1091 | extern HCURSOR wxGetCurrentBusyCursor() |
1092 | { | |
1093 | return gs_wxBusyCursor; | |
1094 | } | |
1095 | ||
2bda0e17 KB |
1096 | // Set the cursor to the busy cursor for all windows |
1097 | void wxBeginBusyCursor(wxCursor *cursor) | |
1098 | { | |
634903fd VZ |
1099 | if ( gs_wxBusyCursorCount++ == 0 ) |
1100 | { | |
1101 | gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR(); | |
04ef50df | 1102 | #ifndef __WXMICROWIN__ |
634903fd | 1103 | gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor); |
04ef50df | 1104 | #endif |
634903fd VZ |
1105 | } |
1106 | //else: nothing to do, already set | |
2bda0e17 KB |
1107 | } |
1108 | ||
1109 | // Restore cursor to normal | |
634903fd | 1110 | void wxEndBusyCursor() |
2bda0e17 | 1111 | { |
634903fd | 1112 | wxCHECK_RET( gs_wxBusyCursorCount > 0, |
223d09f6 | 1113 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); |
b07135ba | 1114 | |
634903fd VZ |
1115 | if ( --gs_wxBusyCursorCount == 0 ) |
1116 | { | |
04ef50df | 1117 | #ifndef __WXMICROWIN__ |
634903fd | 1118 | ::SetCursor(gs_wxBusyCursorOld); |
04ef50df | 1119 | #endif |
634903fd VZ |
1120 | gs_wxBusyCursorOld = 0; |
1121 | } | |
2bda0e17 KB |
1122 | } |
1123 | ||
1124 | // TRUE if we're between the above two calls | |
634903fd | 1125 | bool wxIsBusy() |
2bda0e17 | 1126 | { |
634903fd | 1127 | return (gs_wxBusyCursorCount > 0); |
b07135ba | 1128 | } |
2bda0e17 | 1129 | |
2bda0e17 KB |
1130 | // Check whether this window wants to process messages, e.g. Stop button |
1131 | // in long calculations. | |
1132 | bool wxCheckForInterrupt(wxWindow *wnd) | |
1133 | { | |
b568d04f VZ |
1134 | wxCHECK( wnd, FALSE ); |
1135 | ||
743e0a66 | 1136 | MSG msg; |
b568d04f VZ |
1137 | while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) ) |
1138 | { | |
1139 | ::TranslateMessage(&msg); | |
1140 | ::DispatchMessage(&msg); | |
743e0a66 | 1141 | } |
634903fd | 1142 | |
b568d04f | 1143 | return TRUE; |
2bda0e17 KB |
1144 | } |
1145 | ||
1146 | // MSW only: get user-defined resource from the .res file. | |
1147 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
1148 | ||
04ef50df | 1149 | #ifndef __WXMICROWIN__ |
837e5743 | 1150 | wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType) |
2bda0e17 | 1151 | { |
b568d04f VZ |
1152 | HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType); |
1153 | if ( hResource == 0 ) | |
1154 | return NULL; | |
1155 | ||
1156 | HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); | |
1157 | if ( hData == 0 ) | |
1158 | return NULL; | |
2bda0e17 | 1159 | |
b568d04f VZ |
1160 | wxChar *theText = (wxChar *)::LockResource(hData); |
1161 | if ( !theText ) | |
1162 | return NULL; | |
b07135ba | 1163 | |
8caa4ed1 JS |
1164 | // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). |
1165 | // so we need to find the length of the resource. | |
1166 | int len = ::SizeofResource(wxGetInstance(), hResource); | |
1167 | wxChar *s = new wxChar[len+1]; | |
1168 | wxStrncpy(s,theText,len); | |
1169 | s[len]=0; | |
1170 | ||
1171 | // wxChar *s = copystring(theText); | |
2bda0e17 | 1172 | |
b568d04f | 1173 | // Obsolete in WIN32 |
2bda0e17 | 1174 | #ifndef __WIN32__ |
b568d04f | 1175 | UnlockResource(hData); |
2bda0e17 KB |
1176 | #endif |
1177 | ||
b568d04f VZ |
1178 | // No need?? |
1179 | // GlobalFree(hData); | |
2bda0e17 | 1180 | |
b568d04f | 1181 | return s; |
2bda0e17 | 1182 | } |
04ef50df | 1183 | #endif |
b568d04f VZ |
1184 | |
1185 | // ---------------------------------------------------------------------------- | |
1186 | // get display info | |
1187 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1188 | |
d3db92b2 JS |
1189 | // See also the wxGetMousePosition in window.cpp |
1190 | // Deprecated: use wxPoint wxGetMousePosition() instead | |
2bda0e17 KB |
1191 | void wxGetMousePosition( int* x, int* y ) |
1192 | { | |
b568d04f VZ |
1193 | POINT pt; |
1194 | GetCursorPos( & pt ); | |
1195 | if ( x ) *x = pt.x; | |
1196 | if ( y ) *y = pt.y; | |
2bda0e17 KB |
1197 | }; |
1198 | ||
1199 | // Return TRUE if we have a colour display | |
634903fd | 1200 | bool wxColourDisplay() |
2bda0e17 | 1201 | { |
04ef50df JS |
1202 | #ifdef __WXMICROWIN__ |
1203 | // MICROWIN_TODO | |
1204 | return TRUE; | |
1205 | #else | |
807a903e VZ |
1206 | // this function is called from wxDC ctor so it is called a *lot* of times |
1207 | // hence we optimize it a bit but doign the check only once | |
1208 | // | |
1209 | // this should be MT safe as only the GUI thread (holding the GUI mutex) | |
1210 | // can call us | |
1211 | static int s_isColour = -1; | |
1212 | ||
1213 | if ( s_isColour == -1 ) | |
1214 | { | |
1215 | ScreenHDC dc; | |
1216 | int noCols = ::GetDeviceCaps(dc, NUMCOLORS); | |
1217 | ||
1218 | s_isColour = (noCols == -1) || (noCols > 2); | |
1219 | } | |
b568d04f | 1220 | |
807a903e | 1221 | return s_isColour != 0; |
04ef50df | 1222 | #endif |
2bda0e17 KB |
1223 | } |
1224 | ||
1225 | // Returns depth of screen | |
634903fd | 1226 | int wxDisplayDepth() |
2bda0e17 | 1227 | { |
b568d04f VZ |
1228 | ScreenHDC dc; |
1229 | return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL); | |
2bda0e17 KB |
1230 | } |
1231 | ||
1232 | // Get size of display | |
1233 | void wxDisplaySize(int *width, int *height) | |
1234 | { | |
04ef50df JS |
1235 | #ifdef __WXMICROWIN__ |
1236 | // MICROWIN_TODO | |
1237 | *width = 0; * height = 0; | |
1238 | #else | |
b568d04f | 1239 | ScreenHDC dc; |
3f4a0c5b | 1240 | |
b568d04f VZ |
1241 | if ( width ) *width = GetDeviceCaps(dc, HORZRES); |
1242 | if ( height ) *height = GetDeviceCaps(dc, VERTRES); | |
04ef50df | 1243 | #endif |
7f555861 JS |
1244 | } |
1245 | ||
904a68b6 RL |
1246 | void wxDisplaySizeMM(int *width, int *height) |
1247 | { | |
04ef50df JS |
1248 | #ifdef __WXMICROWIN__ |
1249 | // MICROWIN_TODO | |
1250 | *width = 0; * height = 0; | |
1251 | #else | |
904a68b6 RL |
1252 | ScreenHDC dc; |
1253 | ||
1254 | if ( width ) *width = GetDeviceCaps(dc, HORZSIZE); | |
1255 | if ( height ) *height = GetDeviceCaps(dc, VERTSIZE); | |
04ef50df | 1256 | #endif |
904a68b6 RL |
1257 | } |
1258 | ||
ec5d7799 RD |
1259 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
1260 | { | |
04ef50df | 1261 | #if defined(__WIN16__) || defined(__WXMICROWIN__) |
788722ac JS |
1262 | *x = 0; *y = 0; |
1263 | wxDisplaySize(width, height); | |
1264 | #else | |
ec5d7799 RD |
1265 | // Determine the desktop dimensions minus the taskbar and any other |
1266 | // special decorations... | |
1267 | RECT r; | |
788722ac | 1268 | |
ec5d7799 RD |
1269 | SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); |
1270 | if (x) *x = r.left; | |
1271 | if (y) *y = r.top; | |
1272 | if (width) *width = r.right - r.left; | |
1273 | if (height) *height = r.bottom - r.top; | |
788722ac | 1274 | #endif |
ec5d7799 RD |
1275 | } |
1276 | ||
904a68b6 | 1277 | |
cc2b7472 VZ |
1278 | // --------------------------------------------------------------------------- |
1279 | // window information functions | |
1280 | // --------------------------------------------------------------------------- | |
1281 | ||
1c4a764c VZ |
1282 | wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd) |
1283 | { | |
1284 | wxString str; | |
1285 | int len = GetWindowTextLength((HWND)hWnd) + 1; | |
1286 | GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len); | |
1287 | str.UngetWriteBuf(); | |
1288 | ||
1289 | return str; | |
1290 | } | |
1291 | ||
cc2b7472 VZ |
1292 | wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd) |
1293 | { | |
04ef50df JS |
1294 | #ifdef __WXMICROWIN__ |
1295 | // MICROWIN_TODO | |
1296 | return wxEmptyString; | |
1297 | #else | |
cc2b7472 VZ |
1298 | wxString str; |
1299 | ||
1300 | int len = 256; // some starting value | |
1301 | ||
1302 | for ( ;; ) | |
1303 | { | |
1304 | // as we've #undefined GetClassName we must now manually choose the | |
1305 | // right function to call | |
1306 | int count = | |
1307 | ||
1308 | #ifndef __WIN32__ | |
1309 | GetClassName | |
1310 | #else // Win32 | |
1311 | #ifdef UNICODE | |
1312 | GetClassNameW | |
1313 | #else // !Unicode | |
1314 | #ifdef __TWIN32__ | |
1315 | GetClassName | |
1316 | #else // !Twin32 | |
1317 | GetClassNameA | |
1318 | #endif // Twin32/!Twin32 | |
1319 | #endif // Unicode/ANSI | |
1320 | #endif // Win16/32 | |
1321 | ((HWND)hWnd, str.GetWriteBuf(len), len); | |
1322 | ||
1323 | str.UngetWriteBuf(); | |
1324 | if ( count == len ) | |
1325 | { | |
1326 | // the class name might have been truncated, retry with larger | |
1327 | // buffer | |
1328 | len *= 2; | |
1329 | } | |
1330 | else | |
1331 | { | |
1332 | break; | |
1333 | } | |
1334 | } | |
1335 | ||
1336 | return str; | |
04ef50df | 1337 | #endif |
cc2b7472 VZ |
1338 | } |
1339 | ||
42e69d6b | 1340 | WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd) |
cc2b7472 VZ |
1341 | { |
1342 | #ifndef __WIN32__ | |
33ac7e6f | 1343 | return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID); |
cc2b7472 | 1344 | #else // Win32 |
33ac7e6f | 1345 | return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID); |
cc2b7472 VZ |
1346 | #endif // Win16/32 |
1347 | } | |
1348 | ||
f6bcfd97 BP |
1349 | #endif // wxUSE_GUI |
1350 | ||
b0c89ddf JS |
1351 | #if wxUSE_GUI |
1352 | ||
1353 | // ---------------------------------------------------------------------------- | |
1354 | // Metafile helpers | |
1355 | // ---------------------------------------------------------------------------- | |
1356 | ||
1357 | extern void PixelToHIMETRIC(LONG *x, LONG *y) | |
1358 | { | |
1359 | ScreenHDC hdcRef; | |
1360 | ||
1361 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1362 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1363 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1364 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1365 | ||
1366 | *x *= (iWidthMM * 100); | |
1367 | *x /= iWidthPels; | |
1368 | *y *= (iHeightMM * 100); | |
1369 | *y /= iHeightPels; | |
1370 | } | |
1371 | ||
1372 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
1373 | { | |
1374 | ScreenHDC hdcRef; | |
1375 | ||
1376 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1377 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1378 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1379 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1380 | ||
1381 | *x *= iWidthPels; | |
1382 | *x /= (iWidthMM * 100); | |
1383 | *y *= iHeightPels; | |
1384 | *y /= (iHeightMM * 100); | |
1385 | } | |
1386 | ||
1387 | #endif // wxUSE_GUI | |
1388 | ||
7f555861 JS |
1389 | #if 0 |
1390 | //------------------------------------------------------------------------ | |
1391 | // wild character routines | |
1392 | //------------------------------------------------------------------------ | |
1393 | ||
1394 | bool wxIsWild( const wxString& pattern ) | |
1395 | { | |
1396 | wxString tmp = pattern; | |
1397 | char *pat = WXSTRINGCAST(tmp); | |
1398 | while (*pat) { | |
3f4a0c5b VZ |
1399 | switch (*pat++) { |
1400 | case '?': case '*': case '[': case '{': | |
1401 | return TRUE; | |
1402 | case '\\': | |
1403 | if (!*pat++) | |
1404 | return FALSE; | |
1405 | } | |
7f555861 JS |
1406 | } |
1407 | return FALSE; | |
1408 | }; | |
1409 | ||
1410 | ||
1411 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) | |
1412 | { | |
1413 | wxString tmp1 = pat; | |
1414 | char *pattern = WXSTRINGCAST(tmp1); | |
1415 | wxString tmp2 = text; | |
1416 | char *str = WXSTRINGCAST(tmp2); | |
1417 | char c; | |
1418 | char *cp; | |
1419 | bool done = FALSE, ret_code, ok; | |
1420 | // Below is for vi fans | |
1421 | const char OB = '{', CB = '}'; | |
1422 | ||
1423 | // dot_special means '.' only matches '.' | |
1424 | if (dot_special && *str == '.' && *pattern != *str) | |
3f4a0c5b | 1425 | return FALSE; |
7f555861 JS |
1426 | |
1427 | while ((*pattern != '\0') && (!done) | |
1428 | && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) { | |
3f4a0c5b VZ |
1429 | switch (*pattern) { |
1430 | case '\\': | |
1431 | pattern++; | |
1432 | if (*pattern != '\0') | |
1433 | pattern++; | |
1434 | break; | |
1435 | case '*': | |
1436 | pattern++; | |
1437 | ret_code = FALSE; | |
1438 | while ((*str!='\0') | |
1439 | && (!(ret_code=wxMatchWild(pattern, str++, FALSE)))) | |
1440 | /*loop*/; | |
1441 | if (ret_code) { | |
1442 | while (*str != '\0') | |
1443 | str++; | |
1444 | while (*pattern != '\0') | |
1445 | pattern++; | |
1446 | } | |
1447 | break; | |
1448 | case '[': | |
1449 | pattern++; | |
1450 | repeat: | |
1451 | if ((*pattern == '\0') || (*pattern == ']')) { | |
1452 | done = TRUE; | |
1453 | break; | |
1454 | } | |
1455 | if (*pattern == '\\') { | |
1456 | pattern++; | |
1457 | if (*pattern == '\0') { | |
1458 | done = TRUE; | |
1459 | break; | |
1460 | } | |
1461 | } | |
1462 | if (*(pattern + 1) == '-') { | |
1463 | c = *pattern; | |
1464 | pattern += 2; | |
1465 | if (*pattern == ']') { | |
1466 | done = TRUE; | |
1467 | break; | |
1468 | } | |
1469 | if (*pattern == '\\') { | |
1470 | pattern++; | |
1471 | if (*pattern == '\0') { | |
1472 | done = TRUE; | |
1473 | break; | |
1474 | } | |
1475 | } | |
1476 | if ((*str < c) || (*str > *pattern)) { | |
1477 | pattern++; | |
1478 | goto repeat; | |
1479 | } | |
1480 | } else if (*pattern != *str) { | |
1481 | pattern++; | |
1482 | goto repeat; | |
1483 | } | |
1484 | pattern++; | |
1485 | while ((*pattern != ']') && (*pattern != '\0')) { | |
1486 | if ((*pattern == '\\') && (*(pattern + 1) != '\0')) | |
1487 | pattern++; | |
1488 | pattern++; | |
1489 | } | |
1490 | if (*pattern != '\0') { | |
1491 | pattern++, str++; | |
1492 | } | |
1493 | break; | |
1494 | case '?': | |
1495 | pattern++; | |
1496 | str++; | |
1497 | break; | |
1498 | case OB: | |
1499 | pattern++; | |
1500 | while ((*pattern != CB) && (*pattern != '\0')) { | |
1501 | cp = str; | |
1502 | ok = TRUE; | |
1503 | while (ok && (*cp != '\0') && (*pattern != '\0') | |
1504 | && (*pattern != ',') && (*pattern != CB)) { | |
1505 | if (*pattern == '\\') | |
1506 | pattern++; | |
1507 | ok = (*pattern++ == *cp++); | |
1508 | } | |
1509 | if (*pattern == '\0') { | |
1510 | ok = FALSE; | |
1511 | done = TRUE; | |
1512 | break; | |
1513 | } else if (ok) { | |
1514 | str = cp; | |
1515 | while ((*pattern != CB) && (*pattern != '\0')) { | |
1516 | if (*++pattern == '\\') { | |
1517 | if (*++pattern == CB) | |
1518 | pattern++; | |
1519 | } | |
1520 | } | |
1521 | } else { | |
1522 | while (*pattern!=CB && *pattern!=',' && *pattern!='\0') { | |
1523 | if (*++pattern == '\\') { | |
7f555861 | 1524 | if (*++pattern == CB || *pattern == ',') |
3f4a0c5b VZ |
1525 | pattern++; |
1526 | } | |
1527 | } | |
1528 | } | |
1529 | if (*pattern != '\0') | |
1530 | pattern++; | |
1531 | } | |
1532 | break; | |
1533 | default: | |
1534 | if (*str == *pattern) { | |
1535 | str++, pattern++; | |
1536 | } else { | |
1537 | done = TRUE; | |
1538 | } | |
1539 | } | |
7f555861 JS |
1540 | } |
1541 | while (*pattern == '*') | |
3f4a0c5b | 1542 | pattern++; |
7f555861 JS |
1543 | return ((*str == '\0') && (*pattern == '\0')); |
1544 | }; | |
1545 | ||
f6bcfd97 | 1546 | #endif // 0 |
7f555861 | 1547 | |
cf45d1f4 | 1548 | #if 0 |
c030b70f JS |
1549 | |
1550 | // maximum mumber of lines the output console should have | |
1551 | static const WORD MAX_CONSOLE_LINES = 500; | |
1552 | ||
1553 | BOOL WINAPI MyConsoleHandler( DWORD dwCtrlType ) { // control signal type | |
3f4a0c5b VZ |
1554 | FreeConsole(); |
1555 | return TRUE; | |
c030b70f JS |
1556 | } |
1557 | ||
1558 | void wxRedirectIOToConsole() | |
1559 | { | |
1560 | int hConHandle; | |
1561 | long lStdHandle; | |
1562 | CONSOLE_SCREEN_BUFFER_INFO coninfo; | |
1563 | FILE *fp; | |
1564 | ||
1565 | // allocate a console for this app | |
1566 | AllocConsole(); | |
1567 | ||
1568 | // set the screen buffer to be big enough to let us scroll text | |
3f4a0c5b | 1569 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), |
c030b70f JS |
1570 | &coninfo); |
1571 | coninfo.dwSize.Y = MAX_CONSOLE_LINES; | |
3f4a0c5b | 1572 | SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), |
c030b70f JS |
1573 | coninfo.dwSize); |
1574 | ||
1575 | // redirect unbuffered STDOUT to the console | |
1576 | lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); | |
1577 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
1578 | if(hConHandle <= 0) return; | |
1579 | fp = _fdopen( hConHandle, "w" ); | |
1580 | *stdout = *fp; | |
1581 | setvbuf( stdout, NULL, _IONBF, 0 ); | |
1582 | ||
1583 | // redirect unbuffered STDIN to the console | |
1584 | lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); | |
1585 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
1586 | if(hConHandle <= 0) return; | |
1587 | fp = _fdopen( hConHandle, "r" ); | |
1588 | *stdin = *fp; | |
1589 | setvbuf( stdin, NULL, _IONBF, 0 ); | |
1590 | ||
1591 | // redirect unbuffered STDERR to the console | |
1592 | lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); | |
1593 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
1594 | if(hConHandle <= 0) return; | |
1595 | fp = _fdopen( hConHandle, "w" ); | |
1596 | *stderr = *fp; | |
1597 | setvbuf( stderr, NULL, _IONBF, 0 ); | |
3f4a0c5b VZ |
1598 | |
1599 | // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog | |
c030b70f JS |
1600 | // point to console as well |
1601 | ios::sync_with_stdio(); | |
1602 | ||
3f4a0c5b | 1603 | SetConsoleCtrlHandler(MyConsoleHandler, TRUE); |
c030b70f JS |
1604 | } |
1605 | #else | |
1606 | // Not supported | |
1607 | void wxRedirectIOToConsole() | |
1608 | { | |
1609 | } | |
1610 | #endif | |
1611 | ||
04ef50df JS |
1612 | #ifdef __WXMICROWIN__ |
1613 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
1614 | { | |
1615 | // MICROWIN_TODO | |
1616 | if (majorVsn) *majorVsn = 0; | |
1617 | if (minorVsn) *minorVsn = 0; | |
1618 | return wxUNIX; | |
1619 | } | |
1620 | #endif | |
c030b70f | 1621 |