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