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