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