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