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