]>
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 | // ULARGE_INTEGER is a union of a 64 bit value and a struct containing | |
502 | // two 32 bit fields which may be or may be not named - try to make it | |
503 | // compile in all cases | |
504 | #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT) | |
505 | #define UL(ul) ul.u | |
506 | #else // anon union | |
507 | #define UL(ul) ul | |
508 | #endif | |
509 | if ( pTotal ) | |
510 | { | |
511 | *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart); | |
512 | } | |
513 | ||
514 | if ( pFree ) | |
515 | { | |
516 | *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart); | |
517 | } | |
518 | } | |
519 | else | |
520 | #endif // Win32 | |
521 | { | |
522 | // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx() | |
523 | // should be used instead - but if it's not available, fall back on | |
524 | // GetDiskFreeSpace() nevertheless... | |
525 | ||
526 | DWORD lSectorsPerCluster, | |
527 | lBytesPerSector, | |
528 | lNumberOfFreeClusters, | |
529 | lTotalNumberOfClusters; | |
530 | ||
531 | // FIXME: this is wrong, we should extract the root drive from path | |
532 | // instead, but this is the job for wxFileName... | |
533 | if ( !::GetDiskFreeSpace(path, | |
534 | &lSectorsPerCluster, | |
535 | &lBytesPerSector, | |
536 | &lNumberOfFreeClusters, | |
537 | &lTotalNumberOfClusters) ) | |
538 | { | |
539 | wxLogLastError(_T("GetDiskFreeSpace")); | |
540 | ||
541 | return FALSE; | |
542 | } | |
543 | ||
544 | wxLongLong lBytesPerCluster = lSectorsPerCluster; | |
545 | lBytesPerCluster *= lBytesPerSector; | |
546 | ||
547 | if ( pTotal ) | |
548 | { | |
549 | *pTotal = lBytesPerCluster; | |
550 | *pTotal *= lTotalNumberOfClusters; | |
551 | } | |
552 | ||
553 | if ( pFree ) | |
554 | { | |
555 | *pFree = lBytesPerCluster; | |
556 | *pFree *= lNumberOfFreeClusters; | |
557 | } | |
558 | } | |
559 | ||
560 | return TRUE; | |
561 | } | |
562 | ||
563 | // ---------------------------------------------------------------------------- | |
564 | // env vars | |
565 | // ---------------------------------------------------------------------------- | |
566 | ||
567 | bool wxGetEnv(const wxString& var, wxString *value) | |
568 | { | |
569 | #ifdef __WIN16__ | |
570 | const wxChar* ret = wxGetenv(var); | |
571 | if (ret) | |
572 | { | |
573 | *value = ret; | |
574 | return TRUE; | |
575 | } | |
576 | else | |
577 | return FALSE; | |
578 | #else | |
579 | // first get the size of the buffer | |
580 | DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0); | |
581 | if ( !dwRet ) | |
582 | { | |
583 | // this means that there is no such variable | |
584 | return FALSE; | |
585 | } | |
586 | ||
587 | if ( value ) | |
588 | { | |
589 | (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet); | |
590 | value->UngetWriteBuf(); | |
591 | } | |
592 | ||
593 | return TRUE; | |
594 | #endif | |
595 | } | |
596 | ||
597 | bool wxSetEnv(const wxString& var, const wxChar *value) | |
598 | { | |
599 | // some compilers have putenv() or _putenv() or _wputenv() but it's better | |
600 | // to always use Win32 function directly instead of dealing with them | |
601 | #if defined(__WIN32__) | |
602 | if ( !::SetEnvironmentVariable(var, value) ) | |
603 | { | |
604 | wxLogLastError(_T("SetEnvironmentVariable")); | |
605 | ||
606 | return FALSE; | |
607 | } | |
608 | ||
609 | return TRUE; | |
610 | #else // no way to set env vars | |
611 | return FALSE; | |
612 | #endif | |
613 | } | |
614 | ||
615 | // ---------------------------------------------------------------------------- | |
616 | // process management | |
617 | // ---------------------------------------------------------------------------- | |
618 | ||
619 | #ifdef __WIN32__ | |
620 | ||
621 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() | |
622 | struct wxFindByPidParams | |
623 | { | |
624 | wxFindByPidParams() { hwnd = 0; pid = 0; } | |
625 | ||
626 | // the HWND used to return the result | |
627 | HWND hwnd; | |
628 | ||
629 | // the PID we're looking from | |
630 | DWORD pid; | |
631 | }; | |
632 | ||
633 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
634 | // level) window belonging to the given process | |
635 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
636 | { | |
637 | DWORD pid; | |
638 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
639 | ||
640 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
641 | if ( pid == params->pid ) | |
642 | { | |
643 | // remember the window we found | |
644 | params->hwnd = hwnd; | |
645 | ||
646 | // return FALSE to stop the enumeration | |
647 | return FALSE; | |
648 | } | |
649 | ||
650 | // continue enumeration | |
651 | return TRUE; | |
652 | } | |
653 | ||
654 | #endif // __WIN32__ | |
655 | ||
656 | int wxKill(long pid, wxSignal sig, wxKillError *krc) | |
657 | { | |
658 | #ifdef __WIN32__ | |
659 | // get the process handle to operate on | |
660 | HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | | |
661 | PROCESS_TERMINATE | | |
662 | PROCESS_QUERY_INFORMATION, | |
663 | FALSE, // not inheritable | |
664 | (DWORD)pid); | |
665 | if ( hProcess == NULL ) | |
666 | { | |
667 | if ( krc ) | |
668 | { | |
669 | if ( ::GetLastError() == ERROR_ACCESS_DENIED ) | |
670 | { | |
671 | *krc = wxKILL_ACCESS_DENIED; | |
672 | } | |
673 | else | |
674 | { | |
675 | *krc = wxKILL_NO_PROCESS; | |
676 | } | |
677 | } | |
678 | ||
679 | return -1; | |
680 | } | |
681 | ||
682 | bool ok = TRUE; | |
683 | switch ( sig ) | |
684 | { | |
685 | case wxSIGKILL: | |
686 | // kill the process forcefully returning -1 as error code | |
687 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
688 | { | |
689 | wxLogSysError(_("Failed to kill process %d"), pid); | |
690 | ||
691 | if ( krc ) | |
692 | { | |
693 | // this is not supposed to happen if we could open the | |
694 | // process | |
695 | *krc = wxKILL_ERROR; | |
696 | } | |
697 | ||
698 | ok = FALSE; | |
699 | } | |
700 | break; | |
701 | ||
702 | case wxSIGNONE: | |
703 | // do nothing, we just want to test for process existence | |
704 | break; | |
705 | ||
706 | default: | |
707 | // any other signal means "terminate" | |
708 | { | |
709 | wxFindByPidParams params; | |
710 | params.pid = (DWORD)pid; | |
711 | ||
712 | // EnumWindows() has nice semantics: it returns 0 if it found | |
713 | // something or if an error occured and non zero if it | |
714 | // enumerated all the window | |
715 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
716 | { | |
717 | // did we find any window? | |
718 | if ( params.hwnd ) | |
719 | { | |
720 | // tell the app to close | |
721 | // | |
722 | // NB: this is the harshest way, the app won't have | |
723 | // opportunity to save any files, for example, but | |
724 | // this is probably what we want here. If not we | |
725 | // can also use SendMesageTimeout(WM_CLOSE) | |
726 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
727 | { | |
728 | wxLogLastError(_T("PostMessage(WM_QUIT)")); | |
729 | } | |
730 | } | |
731 | else // it was an error then | |
732 | { | |
733 | wxLogLastError(_T("EnumWindows")); | |
734 | ||
735 | ok = FALSE; | |
736 | } | |
737 | } | |
738 | else // no windows for this PID | |
739 | { | |
740 | if ( krc ) | |
741 | { | |
742 | *krc = wxKILL_ERROR; | |
743 | } | |
744 | ||
745 | ok = FALSE; | |
746 | } | |
747 | } | |
748 | } | |
749 | ||
750 | // the return code | |
751 | DWORD rc; | |
752 | ||
753 | if ( ok ) | |
754 | { | |
755 | // as we wait for a short time, we can use just WaitForSingleObject() | |
756 | // and not MsgWaitForMultipleObjects() | |
757 | switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) ) | |
758 | { | |
759 | case WAIT_OBJECT_0: | |
760 | // process terminated | |
761 | if ( !::GetExitCodeProcess(hProcess, &rc) ) | |
762 | { | |
763 | wxLogLastError(_T("GetExitCodeProcess")); | |
764 | } | |
765 | break; | |
766 | ||
767 | default: | |
768 | wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") ); | |
769 | // fall through | |
770 | ||
771 | case WAIT_FAILED: | |
772 | wxLogLastError(_T("WaitForSingleObject")); | |
773 | // fall through | |
774 | ||
775 | case WAIT_TIMEOUT: | |
776 | if ( krc ) | |
777 | { | |
778 | *krc = wxKILL_ERROR; | |
779 | } | |
780 | ||
781 | rc = STILL_ACTIVE; | |
782 | break; | |
783 | } | |
784 | } | |
785 | else // !ok | |
786 | { | |
787 | // just to suppress the warnings about uninitialized variable | |
788 | rc = 0; | |
789 | } | |
790 | ||
791 | ::CloseHandle(hProcess); | |
792 | ||
793 | // the return code is the same as from Unix kill(): 0 if killed | |
794 | // successfully or -1 on error | |
795 | if ( sig == wxSIGNONE ) | |
796 | { | |
797 | if ( ok && rc == STILL_ACTIVE ) | |
798 | { | |
799 | // there is such process => success | |
800 | return 0; | |
801 | } | |
802 | } | |
803 | else // not SIGNONE | |
804 | { | |
805 | if ( ok && rc != STILL_ACTIVE ) | |
806 | { | |
807 | // killed => success | |
808 | return 0; | |
809 | } | |
810 | } | |
811 | #else // Win15 | |
812 | wxFAIL_MSG( _T("not implemented") ); | |
813 | #endif // Win32/Win16 | |
814 | ||
815 | // error | |
816 | return -1; | |
817 | } | |
818 | ||
819 | // Execute a program in an Interactive Shell | |
820 | bool wxShell(const wxString& command) | |
821 | { | |
822 | wxChar *shell = wxGetenv(wxT("COMSPEC")); | |
823 | if ( !shell ) | |
824 | shell = wxT("\\COMMAND.COM"); | |
825 | ||
826 | wxString cmd; | |
827 | if ( !command ) | |
828 | { | |
829 | // just the shell | |
830 | cmd = shell; | |
831 | } | |
832 | else | |
833 | { | |
834 | // pass the command to execute to the command processor | |
835 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
836 | } | |
837 | ||
838 | return wxExecute(cmd, TRUE /* sync */) != 0; | |
839 | } | |
840 | ||
841 | // ---------------------------------------------------------------------------- | |
842 | // misc | |
843 | // ---------------------------------------------------------------------------- | |
844 | ||
845 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
846 | long wxGetFreeMemory() | |
847 | { | |
848 | #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__) | |
849 | MEMORYSTATUS memStatus; | |
850 | memStatus.dwLength = sizeof(MEMORYSTATUS); | |
851 | GlobalMemoryStatus(&memStatus); | |
852 | return memStatus.dwAvailPhys; | |
853 | #else | |
854 | return (long)GetFreeSpace(0); | |
855 | #endif | |
856 | } | |
857 | ||
858 | // Emit a beeeeeep | |
859 | void wxBell() | |
860 | { | |
861 | ::MessageBeep((UINT)-1); // default sound | |
862 | } | |
863 | ||
864 | wxString wxGetOsDescription() | |
865 | { | |
866 | #ifdef __WIN32__ | |
867 | wxString str; | |
868 | ||
869 | OSVERSIONINFO info; | |
870 | wxZeroMemory(info); | |
871 | ||
872 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
873 | if ( ::GetVersionEx(&info) ) | |
874 | { | |
875 | switch ( info.dwPlatformId ) | |
876 | { | |
877 | case VER_PLATFORM_WIN32s: | |
878 | str = _("Win32s on Windows 3.1"); | |
879 | break; | |
880 | ||
881 | case VER_PLATFORM_WIN32_WINDOWS: | |
882 | str.Printf(_("Windows 9%c"), | |
883 | info.dwMinorVersion == 0 ? _T('5') : _T('8')); | |
884 | if ( !wxIsEmpty(info.szCSDVersion) ) | |
885 | { | |
886 | str << _T(" (") << info.szCSDVersion << _T(')'); | |
887 | } | |
888 | break; | |
889 | ||
890 | case VER_PLATFORM_WIN32_NT: | |
891 | str.Printf(_T("Windows NT %lu.%lu (build %lu"), | |
892 | info.dwMajorVersion, | |
893 | info.dwMinorVersion, | |
894 | info.dwBuildNumber); | |
895 | if ( !wxIsEmpty(info.szCSDVersion) ) | |
896 | { | |
897 | str << _T(", ") << info.szCSDVersion; | |
898 | } | |
899 | str << _T(')'); | |
900 | break; | |
901 | } | |
902 | } | |
903 | else | |
904 | { | |
905 | wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen | |
906 | } | |
907 | ||
908 | return str; | |
909 | #else // Win16 | |
910 | return _("Windows 3.1"); | |
911 | #endif // Win32/16 | |
912 | } | |
913 | ||
914 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
915 | { | |
916 | #if defined(__WIN32__) && !defined(__SC__) | |
917 | OSVERSIONINFO info; | |
918 | wxZeroMemory(info); | |
919 | ||
920 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
921 | if ( ::GetVersionEx(&info) ) | |
922 | { | |
923 | if (majorVsn) | |
924 | *majorVsn = info.dwMajorVersion; | |
925 | if (minorVsn) | |
926 | *minorVsn = info.dwMinorVersion; | |
927 | ||
928 | switch ( info.dwPlatformId ) | |
929 | { | |
930 | case VER_PLATFORM_WIN32s: | |
931 | return wxWIN32S; | |
932 | ||
933 | case VER_PLATFORM_WIN32_WINDOWS: | |
934 | return wxWIN95; | |
935 | ||
936 | case VER_PLATFORM_WIN32_NT: | |
937 | return wxWINDOWS_NT; | |
938 | } | |
939 | } | |
940 | ||
941 | return wxWINDOWS; // error if we get here, return generic value | |
942 | #else // Win16 | |
943 | int retValue = wxWINDOWS; | |
944 | #ifdef __WINDOWS_386__ | |
945 | retValue = wxWIN386; | |
946 | #else | |
947 | #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS | |
948 | extern HANDLE g_hPenWin; | |
949 | retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS; | |
950 | #endif | |
951 | #endif | |
952 | ||
953 | if (majorVsn) | |
954 | *majorVsn = 3; | |
955 | if (minorVsn) | |
956 | *minorVsn = 1; | |
957 | ||
958 | return retValue; | |
959 | #endif | |
960 | } | |
961 | ||
962 | // ---------------------------------------------------------------------------- | |
963 | // sleep functions | |
964 | // ---------------------------------------------------------------------------- | |
965 | ||
966 | #if wxUSE_GUI | |
967 | ||
968 | #if wxUSE_TIMER | |
969 | ||
970 | // Sleep for nSecs seconds. Attempt a Windows implementation using timers. | |
971 | static bool gs_inTimer = FALSE; | |
972 | ||
973 | class wxSleepTimer : public wxTimer | |
974 | { | |
975 | public: | |
976 | virtual void Notify() | |
977 | { | |
978 | gs_inTimer = FALSE; | |
979 | Stop(); | |
980 | } | |
981 | }; | |
982 | ||
983 | static wxTimer *wxTheSleepTimer = NULL; | |
984 | ||
985 | void wxUsleep(unsigned long milliseconds) | |
986 | { | |
987 | #ifdef __WIN32__ | |
988 | ::Sleep(milliseconds); | |
989 | #else // !Win32 | |
990 | if (gs_inTimer) | |
991 | return; | |
992 | ||
993 | wxTheSleepTimer = new wxSleepTimer; | |
994 | gs_inTimer = TRUE; | |
995 | wxTheSleepTimer->Start(milliseconds); | |
996 | while (gs_inTimer) | |
997 | { | |
998 | if (wxTheApp->Pending()) | |
999 | wxTheApp->Dispatch(); | |
1000 | } | |
1001 | delete wxTheSleepTimer; | |
1002 | wxTheSleepTimer = NULL; | |
1003 | #endif // Win32/!Win32 | |
1004 | } | |
1005 | ||
1006 | void wxSleep(int nSecs) | |
1007 | { | |
1008 | if (gs_inTimer) | |
1009 | return; | |
1010 | ||
1011 | wxTheSleepTimer = new wxSleepTimer; | |
1012 | gs_inTimer = TRUE; | |
1013 | wxTheSleepTimer->Start(nSecs*1000); | |
1014 | while (gs_inTimer) | |
1015 | { | |
1016 | if (wxTheApp->Pending()) | |
1017 | wxTheApp->Dispatch(); | |
1018 | } | |
1019 | delete wxTheSleepTimer; | |
1020 | wxTheSleepTimer = NULL; | |
1021 | } | |
1022 | ||
1023 | // Consume all events until no more left | |
1024 | void wxFlushEvents() | |
1025 | { | |
1026 | // wxYield(); | |
1027 | } | |
1028 | ||
1029 | #endif // wxUSE_TIMER | |
1030 | ||
1031 | #elif defined(__WIN32__) // wxUSE_GUI | |
1032 | ||
1033 | void wxUsleep(unsigned long milliseconds) | |
1034 | { | |
1035 | ::Sleep(milliseconds); | |
1036 | } | |
1037 | ||
1038 | void wxSleep(int nSecs) | |
1039 | { | |
1040 | wxUsleep(1000*nSecs); | |
1041 | } | |
1042 | ||
1043 | #endif // wxUSE_GUI/!wxUSE_GUI | |
1044 | #endif // __WXMICROWIN__ | |
1045 | ||
1046 | // ---------------------------------------------------------------------------- | |
1047 | // deprecated (in favour of wxLog) log functions | |
1048 | // ---------------------------------------------------------------------------- | |
1049 | ||
1050 | #if wxUSE_GUI | |
1051 | ||
1052 | // Output a debug mess., in a system dependent fashion. | |
1053 | #ifndef __WXMICROWIN__ | |
1054 | void wxDebugMsg(const wxChar *fmt ...) | |
1055 | { | |
1056 | va_list ap; | |
1057 | static wxChar buffer[512]; | |
1058 | ||
1059 | if (!wxTheApp->GetWantDebugOutput()) | |
1060 | return ; | |
1061 | ||
1062 | va_start(ap, fmt); | |
1063 | ||
1064 | wvsprintf(buffer,fmt,ap) ; | |
1065 | OutputDebugString((LPCTSTR)buffer) ; | |
1066 | ||
1067 | va_end(ap); | |
1068 | } | |
1069 | ||
1070 | // Non-fatal error: pop up message box and (possibly) continue | |
1071 | void wxError(const wxString& msg, const wxString& title) | |
1072 | { | |
1073 | wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg); | |
1074 | if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title, | |
1075 | MB_ICONSTOP | MB_YESNO) == IDNO) | |
1076 | wxExit(); | |
1077 | } | |
1078 | ||
1079 | // Fatal error: pop up message box and abort | |
1080 | void wxFatalError(const wxString& msg, const wxString& title) | |
1081 | { | |
1082 | wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg); | |
1083 | FatalAppExit(0, (LPCTSTR)wxBuffer); | |
1084 | } | |
1085 | #endif // __WXMICROWIN__ | |
1086 | ||
1087 | // ---------------------------------------------------------------------------- | |
1088 | // functions to work with .INI files | |
1089 | // ---------------------------------------------------------------------------- | |
1090 | ||
1091 | // Reading and writing resources (eg WIN.INI, .Xdefaults) | |
1092 | #if wxUSE_RESOURCES | |
1093 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) | |
1094 | { | |
1095 | if (file != wxT("")) | |
1096 | // Don't know what the correct cast should be, but it doesn't | |
1097 | // compile in BC++/16-bit without this cast. | |
1098 | #if !defined(__WIN32__) | |
1099 | return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0); | |
1100 | #else | |
1101 | return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0); | |
1102 | #endif | |
1103 | else | |
1104 | return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0); | |
1105 | } | |
1106 | ||
1107 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
1108 | { | |
1109 | wxString buf; | |
1110 | buf.Printf(wxT("%.4f"), value); | |
1111 | ||
1112 | return wxWriteResource(section, entry, buf, file); | |
1113 | } | |
1114 | ||
1115 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
1116 | { | |
1117 | wxString buf; | |
1118 | buf.Printf(wxT("%ld"), value); | |
1119 | ||
1120 | return wxWriteResource(section, entry, buf, file); | |
1121 | } | |
1122 | ||
1123 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
1124 | { | |
1125 | wxString buf; | |
1126 | buf.Printf(wxT("%d"), value); | |
1127 | ||
1128 | return wxWriteResource(section, entry, buf, file); | |
1129 | } | |
1130 | ||
1131 | bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file) | |
1132 | { | |
1133 | static const wxChar defunkt[] = wxT("$$default"); | |
1134 | if (file != wxT("")) | |
1135 | { | |
1136 | int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1137 | (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file); | |
1138 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1139 | return FALSE; | |
1140 | } | |
1141 | else | |
1142 | { | |
1143 | int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1144 | (LPTSTR)wxBuffer, 1000); | |
1145 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1146 | return FALSE; | |
1147 | } | |
1148 | if (*value) delete[] (*value); | |
1149 | *value = copystring(wxBuffer); | |
1150 | return TRUE; | |
1151 | } | |
1152 | ||
1153 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
1154 | { | |
1155 | wxChar *s = NULL; | |
1156 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1157 | if (succ) | |
1158 | { | |
1159 | *value = (float)wxStrtod(s, NULL); | |
1160 | delete[] s; | |
1161 | return TRUE; | |
1162 | } | |
1163 | else return FALSE; | |
1164 | } | |
1165 | ||
1166 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
1167 | { | |
1168 | wxChar *s = NULL; | |
1169 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1170 | if (succ) | |
1171 | { | |
1172 | *value = wxStrtol(s, NULL, 10); | |
1173 | delete[] s; | |
1174 | return TRUE; | |
1175 | } | |
1176 | else return FALSE; | |
1177 | } | |
1178 | ||
1179 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
1180 | { | |
1181 | wxChar *s = NULL; | |
1182 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1183 | if (succ) | |
1184 | { | |
1185 | *value = (int)wxStrtol(s, NULL, 10); | |
1186 | delete[] s; | |
1187 | return TRUE; | |
1188 | } | |
1189 | else return FALSE; | |
1190 | } | |
1191 | #endif // wxUSE_RESOURCES | |
1192 | ||
1193 | // --------------------------------------------------------------------------- | |
1194 | // helper functions for showing a "busy" cursor | |
1195 | // --------------------------------------------------------------------------- | |
1196 | ||
1197 | static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor | |
1198 | static HCURSOR gs_wxBusyCursorOld = 0; // old cursor | |
1199 | static int gs_wxBusyCursorCount = 0; | |
1200 | ||
1201 | extern HCURSOR wxGetCurrentBusyCursor() | |
1202 | { | |
1203 | return gs_wxBusyCursor; | |
1204 | } | |
1205 | ||
1206 | // Set the cursor to the busy cursor for all windows | |
1207 | void wxBeginBusyCursor(wxCursor *cursor) | |
1208 | { | |
1209 | if ( gs_wxBusyCursorCount++ == 0 ) | |
1210 | { | |
1211 | gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR(); | |
1212 | #ifndef __WXMICROWIN__ | |
1213 | gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor); | |
1214 | #endif | |
1215 | } | |
1216 | //else: nothing to do, already set | |
1217 | } | |
1218 | ||
1219 | // Restore cursor to normal | |
1220 | void wxEndBusyCursor() | |
1221 | { | |
1222 | wxCHECK_RET( gs_wxBusyCursorCount > 0, | |
1223 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); | |
1224 | ||
1225 | if ( --gs_wxBusyCursorCount == 0 ) | |
1226 | { | |
1227 | #ifndef __WXMICROWIN__ | |
1228 | ::SetCursor(gs_wxBusyCursorOld); | |
1229 | #endif | |
1230 | gs_wxBusyCursorOld = 0; | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | // TRUE if we're between the above two calls | |
1235 | bool wxIsBusy() | |
1236 | { | |
1237 | return gs_wxBusyCursorCount > 0; | |
1238 | } | |
1239 | ||
1240 | // Check whether this window wants to process messages, e.g. Stop button | |
1241 | // in long calculations. | |
1242 | bool wxCheckForInterrupt(wxWindow *wnd) | |
1243 | { | |
1244 | wxCHECK( wnd, FALSE ); | |
1245 | ||
1246 | MSG msg; | |
1247 | while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) ) | |
1248 | { | |
1249 | ::TranslateMessage(&msg); | |
1250 | ::DispatchMessage(&msg); | |
1251 | } | |
1252 | ||
1253 | return TRUE; | |
1254 | } | |
1255 | ||
1256 | // MSW only: get user-defined resource from the .res file. | |
1257 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
1258 | ||
1259 | #ifndef __WXMICROWIN__ | |
1260 | wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType) | |
1261 | { | |
1262 | HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType); | |
1263 | if ( hResource == 0 ) | |
1264 | return NULL; | |
1265 | ||
1266 | HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); | |
1267 | if ( hData == 0 ) | |
1268 | return NULL; | |
1269 | ||
1270 | wxChar *theText = (wxChar *)::LockResource(hData); | |
1271 | if ( !theText ) | |
1272 | return NULL; | |
1273 | ||
1274 | // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). | |
1275 | // so we need to find the length of the resource. | |
1276 | int len = ::SizeofResource(wxGetInstance(), hResource); | |
1277 | wxChar *s = new wxChar[len+1]; | |
1278 | wxStrncpy(s,theText,len); | |
1279 | s[len]=0; | |
1280 | ||
1281 | // wxChar *s = copystring(theText); | |
1282 | ||
1283 | // Obsolete in WIN32 | |
1284 | #ifndef __WIN32__ | |
1285 | UnlockResource(hData); | |
1286 | #endif | |
1287 | ||
1288 | // No need?? | |
1289 | // GlobalFree(hData); | |
1290 | ||
1291 | return s; | |
1292 | } | |
1293 | #endif // __WXMICROWIN__ | |
1294 | ||
1295 | // ---------------------------------------------------------------------------- | |
1296 | // get display info | |
1297 | // ---------------------------------------------------------------------------- | |
1298 | ||
1299 | // See also the wxGetMousePosition in window.cpp | |
1300 | // Deprecated: use wxPoint wxGetMousePosition() instead | |
1301 | void wxGetMousePosition( int* x, int* y ) | |
1302 | { | |
1303 | POINT pt; | |
1304 | GetCursorPos( & pt ); | |
1305 | if ( x ) *x = pt.x; | |
1306 | if ( y ) *y = pt.y; | |
1307 | }; | |
1308 | ||
1309 | // Return TRUE if we have a colour display | |
1310 | bool wxColourDisplay() | |
1311 | { | |
1312 | #ifdef __WXMICROWIN__ | |
1313 | // MICROWIN_TODO | |
1314 | return TRUE; | |
1315 | #else | |
1316 | // this function is called from wxDC ctor so it is called a *lot* of times | |
1317 | // hence we optimize it a bit but doign the check only once | |
1318 | // | |
1319 | // this should be MT safe as only the GUI thread (holding the GUI mutex) | |
1320 | // can call us | |
1321 | static int s_isColour = -1; | |
1322 | ||
1323 | if ( s_isColour == -1 ) | |
1324 | { | |
1325 | ScreenHDC dc; | |
1326 | int noCols = ::GetDeviceCaps(dc, NUMCOLORS); | |
1327 | ||
1328 | s_isColour = (noCols == -1) || (noCols > 2); | |
1329 | } | |
1330 | ||
1331 | return s_isColour != 0; | |
1332 | #endif | |
1333 | } | |
1334 | ||
1335 | // Returns depth of screen | |
1336 | int wxDisplayDepth() | |
1337 | { | |
1338 | ScreenHDC dc; | |
1339 | return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL); | |
1340 | } | |
1341 | ||
1342 | // Get size of display | |
1343 | void wxDisplaySize(int *width, int *height) | |
1344 | { | |
1345 | #ifdef __WXMICROWIN__ | |
1346 | RECT rect; | |
1347 | HWND hWnd = GetDesktopWindow(); | |
1348 | ::GetWindowRect(hWnd, & rect); | |
1349 | ||
1350 | if ( width ) | |
1351 | *width = rect.right - rect.left; | |
1352 | if ( height ) | |
1353 | *height = rect.bottom - rect.top; | |
1354 | #else // !__WXMICROWIN__ | |
1355 | ScreenHDC dc; | |
1356 | ||
1357 | if ( width ) | |
1358 | *width = ::GetDeviceCaps(dc, HORZRES); | |
1359 | if ( height ) | |
1360 | *height = ::GetDeviceCaps(dc, VERTRES); | |
1361 | #endif // __WXMICROWIN__/!__WXMICROWIN__ | |
1362 | } | |
1363 | ||
1364 | void wxDisplaySizeMM(int *width, int *height) | |
1365 | { | |
1366 | #ifdef __WXMICROWIN__ | |
1367 | // MICROWIN_TODO | |
1368 | if ( width ) | |
1369 | *width = 0; | |
1370 | if ( height ) | |
1371 | *height = 0; | |
1372 | #else | |
1373 | ScreenHDC dc; | |
1374 | ||
1375 | if ( width ) | |
1376 | *width = ::GetDeviceCaps(dc, HORZSIZE); | |
1377 | if ( height ) | |
1378 | *height = ::GetDeviceCaps(dc, VERTSIZE); | |
1379 | #endif | |
1380 | } | |
1381 | ||
1382 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) | |
1383 | { | |
1384 | #if defined(__WIN16__) || defined(__WXMICROWIN__) | |
1385 | *x = 0; *y = 0; | |
1386 | wxDisplaySize(width, height); | |
1387 | #else | |
1388 | // Determine the desktop dimensions minus the taskbar and any other | |
1389 | // special decorations... | |
1390 | RECT r; | |
1391 | ||
1392 | SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); | |
1393 | if (x) *x = r.left; | |
1394 | if (y) *y = r.top; | |
1395 | if (width) *width = r.right - r.left; | |
1396 | if (height) *height = r.bottom - r.top; | |
1397 | #endif | |
1398 | } | |
1399 | ||
1400 | // --------------------------------------------------------------------------- | |
1401 | // window information functions | |
1402 | // --------------------------------------------------------------------------- | |
1403 | ||
1404 | wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd) | |
1405 | { | |
1406 | wxString str; | |
1407 | ||
1408 | if ( hWnd ) | |
1409 | { | |
1410 | int len = GetWindowTextLength((HWND)hWnd) + 1; | |
1411 | ::GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len); | |
1412 | str.UngetWriteBuf(); | |
1413 | } | |
1414 | ||
1415 | return str; | |
1416 | } | |
1417 | ||
1418 | wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd) | |
1419 | { | |
1420 | wxString str; | |
1421 | ||
1422 | // MICROWIN_TODO | |
1423 | #ifndef __WXMICROWIN__ | |
1424 | if ( hWnd ) | |
1425 | { | |
1426 | int len = 256; // some starting value | |
1427 | ||
1428 | for ( ;; ) | |
1429 | { | |
1430 | int count = ::GetClassName((HWND)hWnd, str.GetWriteBuf(len), len); | |
1431 | ||
1432 | str.UngetWriteBuf(); | |
1433 | if ( count == len ) | |
1434 | { | |
1435 | // the class name might have been truncated, retry with larger | |
1436 | // buffer | |
1437 | len *= 2; | |
1438 | } | |
1439 | else | |
1440 | { | |
1441 | break; | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | #endif // !__WXMICROWIN__ | |
1446 | ||
1447 | return str; | |
1448 | } | |
1449 | ||
1450 | WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd) | |
1451 | { | |
1452 | #ifndef __WIN32__ | |
1453 | return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID); | |
1454 | #else // Win32 | |
1455 | return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID); | |
1456 | #endif // Win16/32 | |
1457 | } | |
1458 | ||
1459 | #endif // wxUSE_GUI | |
1460 | ||
1461 | #if wxUSE_GUI | |
1462 | ||
1463 | // ---------------------------------------------------------------------------- | |
1464 | // Metafile helpers | |
1465 | // ---------------------------------------------------------------------------- | |
1466 | ||
1467 | extern void PixelToHIMETRIC(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 *= (iWidthMM * 100); | |
1477 | *x /= iWidthPels; | |
1478 | *y *= (iHeightMM * 100); | |
1479 | *y /= iHeightPels; | |
1480 | } | |
1481 | ||
1482 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
1483 | { | |
1484 | ScreenHDC hdcRef; | |
1485 | ||
1486 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1487 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1488 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1489 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1490 | ||
1491 | *x *= iWidthPels; | |
1492 | *x /= (iWidthMM * 100); | |
1493 | *y *= iHeightPels; | |
1494 | *y /= (iHeightMM * 100); | |
1495 | } | |
1496 | ||
1497 | #endif // wxUSE_GUI | |
1498 | ||
1499 | #ifdef __WXMICROWIN__ | |
1500 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
1501 | { | |
1502 | // MICROWIN_TODO | |
1503 | if (majorVsn) *majorVsn = 0; | |
1504 | if (minorVsn) *minorVsn = 0; | |
1505 | return wxUNIX; | |
1506 | } | |
1507 | #endif // __WXMICROWIN__ | |
1508 | ||
1509 | // ---------------------------------------------------------------------------- | |
1510 | // Win32 codepage conversion functions | |
1511 | // ---------------------------------------------------------------------------- | |
1512 | ||
1513 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) | |
1514 | ||
1515 | // wxGetNativeFontEncoding() doesn't exist neither in wxBase nor in wxUniv | |
1516 | #if wxUSE_GUI && !defined(__WXUNIVERSAL__) | |
1517 | ||
1518 | #include "wx/fontmap.h" | |
1519 | ||
1520 | // VZ: the new version of wxCharsetToCodepage() is more politically correct | |
1521 | // and should work on other Windows versions as well but the old version is | |
1522 | // still needed for !wxUSE_FONTMAP || !wxUSE_GUI case | |
1523 | ||
1524 | extern long wxEncodingToCodepage(wxFontEncoding encoding) | |
1525 | { | |
1526 | // translate encoding into the Windows CHARSET | |
1527 | wxNativeEncodingInfo natveEncInfo; | |
1528 | if ( !wxGetNativeFontEncoding(encoding, &natveEncInfo) ) | |
1529 | return -1; | |
1530 | ||
1531 | // translate CHARSET to code page | |
1532 | CHARSETINFO csetInfo; | |
1533 | if ( !::TranslateCharsetInfo((DWORD *)(DWORD)natveEncInfo.charset, | |
1534 | &csetInfo, | |
1535 | TCI_SRCCHARSET) ) | |
1536 | { | |
1537 | wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)")); | |
1538 | ||
1539 | return -1; | |
1540 | } | |
1541 | ||
1542 | return csetInfo.ciACP; | |
1543 | } | |
1544 | ||
1545 | #if wxUSE_FONTMAP | |
1546 | ||
1547 | extern long wxCharsetToCodepage(const wxChar *name) | |
1548 | { | |
1549 | // first get the font encoding for this charset | |
1550 | if ( !name ) | |
1551 | return -1; | |
1552 | ||
1553 | wxFontEncoding enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); | |
1554 | if ( enc == wxFONTENCODING_SYSTEM ) | |
1555 | return -1; | |
1556 | ||
1557 | // the use the helper function | |
1558 | return wxEncodingToCodepage(enc); | |
1559 | } | |
1560 | ||
1561 | #endif // wxUSE_FONTMAP | |
1562 | ||
1563 | #endif // wxUSE_GUI | |
1564 | ||
1565 | // include old wxCharsetToCodepage() by OK if needed | |
1566 | #if !wxUSE_GUI || !wxUSE_FONTMAP | |
1567 | ||
1568 | #include "wx/msw/registry.h" | |
1569 | ||
1570 | // this should work if Internet Exploiter is installed | |
1571 | extern long wxCharsetToCodepage(const wxChar *name) | |
1572 | { | |
1573 | if (!name) | |
1574 | return GetACP(); | |
1575 | ||
1576 | long CP=-1; | |
1577 | ||
1578 | wxString cn(name); | |
1579 | do { | |
1580 | wxString path(wxT("MIME\\Database\\Charset\\")); | |
1581 | path += cn; | |
1582 | wxRegKey key(wxRegKey::HKCR, path); | |
1583 | ||
1584 | if (!key.Exists()) break; | |
1585 | ||
1586 | // two cases: either there's an AliasForCharset string, | |
1587 | // or there are Codepage and InternetEncoding dwords. | |
1588 | // The InternetEncoding gives us the actual encoding, | |
1589 | // the Codepage just says which Windows character set to | |
1590 | // use when displaying the data. | |
1591 | if (key.HasValue(wxT("InternetEncoding")) && | |
1592 | key.QueryValue(wxT("InternetEncoding"), &CP)) break; | |
1593 | ||
1594 | // no encoding, see if it's an alias | |
1595 | if (!key.HasValue(wxT("AliasForCharset")) || | |
1596 | !key.QueryValue(wxT("AliasForCharset"), cn)) break; | |
1597 | } while (1); | |
1598 | ||
1599 | return CP; | |
1600 | } | |
1601 | ||
1602 | #endif // !wxUSE_GUI || !wxUSE_FONTMAP | |
1603 | ||
1604 | #endif // Win32 | |
1605 |