]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8caa4ed1 | 2 | // Name: msw/utils.cpp |
2bda0e17 KB |
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 | |
743e0a66 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
b568d04f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
a3b46648 | 21 | // #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now. |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
a4f96412 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
a4f96412 VZ |
32 | #include "wx/utils.h" |
33 | #include "wx/app.h" | |
1f0500b3 VZ |
34 | #include "wx/intl.h" |
35 | #include "wx/log.h" | |
3a922bb4 RL |
36 | #if wxUSE_GUI |
37 | #include "wx/cursor.h" | |
38 | #endif | |
743e0a66 | 39 | #endif //WX_PRECOMP |
2bda0e17 | 40 | |
b568d04f VZ |
41 | #include "wx/msw/private.h" // includes <windows.h> |
42 | ||
c1cb4153 VZ |
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 | ||
2bda0e17 KB |
49 | #include "wx/timer.h" |
50 | ||
04ef50df | 51 | #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) |
a4f96412 | 52 | #include <direct.h> |
ce3ed50d | 53 | |
a4f96412 VZ |
54 | #ifndef __MWERKS__ |
55 | #include <dos.h> | |
56 | #endif | |
743e0a66 | 57 | #endif //GNUWIN32 |
2bda0e17 | 58 | |
e02e8816 | 59 | #if defined(__CYGWIN__) && !defined(__TWIN32__) |
a4f96412 VZ |
60 | #include <sys/unistd.h> |
61 | #include <sys/stat.h> | |
3ffbc733 | 62 | #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path() |
743e0a66 VZ |
63 | #endif //GNUWIN32 |
64 | ||
2bda0e17 KB |
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. | |
a4f96412 | 68 | #include <dir.h> |
2bda0e17 KB |
69 | #endif |
70 | ||
a4f96412 VZ |
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 | ||
04ef50df | 81 | #if defined(__WIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__) |
a4f96412 | 82 | #include <io.h> |
2bda0e17 | 83 | |
a4f96412 VZ |
84 | #ifndef __GNUWIN32__ |
85 | #include <shellapi.h> | |
86 | #endif | |
2bda0e17 KB |
87 | #endif |
88 | ||
2bda0e17 | 89 | #ifndef __WATCOMC__ |
3f4a0c5b VZ |
90 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
91 | #include <errno.h> | |
92 | #endif | |
2bda0e17 | 93 | #endif |
2bda0e17 | 94 | |
c030b70f | 95 | //// BEGIN for console support: VC++ only |
631f1bfe | 96 | #ifdef __VISUALC__ |
c030b70f | 97 | |
3f4a0c5b | 98 | #include "wx/msw/msvcrt.h" |
c030b70f | 99 | |
3f4a0c5b | 100 | #include <fcntl.h> |
c030b70f | 101 | |
3f4a0c5b | 102 | #include "wx/ioswrap.h" |
c030b70f | 103 | |
c030b70f JS |
104 | /* Need to undef new if including crtdbg.h */ |
105 | # ifdef new | |
106 | # undef new | |
107 | # endif | |
108 | ||
1e6d9499 | 109 | #ifndef __WIN16__ |
c030b70f | 110 | # include <crtdbg.h> |
1e6d9499 | 111 | #endif |
c030b70f JS |
112 | |
113 | # if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
f6bcfd97 | 114 | # define new new(__TFILE__,__LINE__) |
c030b70f JS |
115 | # endif |
116 | ||
631f1bfe JS |
117 | #endif |
118 | // __VISUALC__ | |
c030b70f JS |
119 | /// END for console support |
120 | ||
b568d04f VZ |
121 | // ---------------------------------------------------------------------------- |
122 | // constants | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
2bda0e17 | 125 | // In the WIN.INI file |
223d09f6 | 126 | static const wxChar WX_SECTION[] = wxT("wxWindows"); |
b568d04f VZ |
127 | static const wxChar eUSERNAME[] = wxT("UserName"); |
128 | ||
129 | // these are only used under Win16 | |
04ef50df | 130 | #if !defined(__WIN32__) && !defined(__WXMICROWIN__) |
223d09f6 KB |
131 | static const wxChar eHOSTNAME[] = wxT("HostName"); |
132 | static const wxChar eUSERID[] = wxT("UserId"); | |
b568d04f VZ |
133 | #endif // !Win32 |
134 | ||
04ef50df JS |
135 | #ifndef __WXMICROWIN__ |
136 | ||
b568d04f VZ |
137 | // ============================================================================ |
138 | // implementation | |
139 | // ============================================================================ | |
2bda0e17 | 140 | |
b568d04f VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // get host name and related | |
143 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 144 | |
1f0500b3 | 145 | // Get hostname only (without domain name) |
837e5743 | 146 | bool wxGetHostName(wxChar *buf, int maxSize) |
2bda0e17 | 147 | { |
04ef50df | 148 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) |
b568d04f VZ |
149 | DWORD nSize = maxSize; |
150 | if ( !::GetComputerName(buf, &nSize) ) | |
151 | { | |
f6bcfd97 | 152 | wxLogLastError(wxT("GetComputerName")); |
b568d04f VZ |
153 | |
154 | return FALSE; | |
155 | } | |
156 | ||
157 | return TRUE; | |
2bda0e17 | 158 | #else |
b568d04f VZ |
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; | |
2bda0e17 KB |
168 | #endif |
169 | } | |
170 | ||
1f0500b3 | 171 | // get full hostname (with domain name if possible) |
b568d04f VZ |
172 | bool wxGetFullHostName(wxChar *buf, int maxSize) |
173 | { | |
04ef50df | 174 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) |
1f0500b3 | 175 | // TODO should use GetComputerNameEx() when available |
79180098 VZ |
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 | |
1f0500b3 VZ |
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 | |
d5b18b50 | 197 | pHostEnt = gethostbyaddr(pHostEnt->h_addr, 4, AF_INET); |
1f0500b3 VZ |
198 | } |
199 | ||
200 | if ( pHostEnt ) | |
201 | { | |
2b5f62a0 | 202 | host = wxString::FromAscii(pHostEnt->h_name); |
1f0500b3 VZ |
203 | } |
204 | } | |
205 | } | |
206 | ||
207 | WSACleanup(); | |
208 | ||
79180098 | 209 | if ( !host.empty() ) |
1f0500b3 VZ |
210 | { |
211 | wxStrncpy(buf, host, maxSize); | |
212 | ||
213 | return TRUE; | |
214 | } | |
215 | } | |
79180098 VZ |
216 | #endif // wxUSE_SOCKETS |
217 | ||
1f0500b3 VZ |
218 | #endif // Win32 |
219 | ||
b568d04f VZ |
220 | return wxGetHostName(buf, maxSize); |
221 | } | |
222 | ||
2bda0e17 | 223 | // Get user ID e.g. jacs |
837e5743 | 224 | bool wxGetUserId(wxChar *buf, int maxSize) |
2bda0e17 | 225 | { |
04ef50df | 226 | #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) |
0a144271 VZ |
227 | DWORD nSize = maxSize; |
228 | if ( ::GetUserName(buf, &nSize) == 0 ) | |
229 | { | |
a4f96412 | 230 | // actually, it does happen on Win9x if the user didn't log on |
223d09f6 | 231 | DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize); |
a4f96412 VZ |
232 | if ( res == 0 ) |
233 | { | |
234 | // not found | |
235 | return FALSE; | |
236 | } | |
0a144271 VZ |
237 | } |
238 | ||
239 | return TRUE; | |
0a144271 | 240 | #else // Win16 or Win32s |
b568d04f VZ |
241 | wxChar *user; |
242 | const wxChar *default_id = wxT("anonymous"); | |
2bda0e17 | 243 | |
b568d04f VZ |
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 | } | |
a4f96412 | 256 | |
b568d04f | 257 | return *buf ? TRUE : FALSE; |
2bda0e17 KB |
258 | #endif |
259 | } | |
260 | ||
261 | // Get user name e.g. Julian Smart | |
837e5743 | 262 | bool wxGetUserName(wxChar *buf, int maxSize) |
2bda0e17 | 263 | { |
0a144271 | 264 | #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__) |
b568d04f VZ |
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 | |
2bda0e17 | 274 | #endif |
b568d04f | 275 | { |
a4f96412 | 276 | #ifdef USE_NET_API |
b568d04f VZ |
277 | CHAR szUserName[256]; |
278 | if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) ) | |
279 | return FALSE; | |
a4f96412 | 280 | |
b568d04f VZ |
281 | // TODO how to get the domain name? |
282 | CHAR *szDomain = ""; | |
a4f96412 | 283 | |
b568d04f VZ |
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; | |
a4f96412 | 288 | |
b568d04f | 289 | USER_INFO_2 *ui2; // User structure |
a4f96412 | 290 | |
b568d04f VZ |
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) ); | |
a4f96412 | 296 | |
b568d04f VZ |
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")); | |
a4f96412 | 301 | |
b568d04f VZ |
302 | goto error; |
303 | } | |
a4f96412 | 304 | |
b568d04f VZ |
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; | |
a4f96412 | 315 | |
b568d04f VZ |
316 | case NERR_InvalidComputer: |
317 | wxLogError(wxT("Invalid domain controller name.")); | |
a4f96412 | 318 | |
b568d04f | 319 | goto error; |
a4f96412 | 320 | |
b568d04f VZ |
321 | case NERR_UserNotFound: |
322 | wxLogError(wxT("Invalid user name '%s'."), szUserName); | |
a4f96412 | 323 | |
b568d04f | 324 | goto error; |
a4f96412 | 325 | |
b568d04f VZ |
326 | default: |
327 | wxLogSysError(wxT("Can't get information about user")); | |
a4f96412 | 328 | |
b568d04f VZ |
329 | goto error; |
330 | } | |
a4f96412 | 331 | |
b568d04f VZ |
332 | // Convert the Unicode full name to ANSI |
333 | WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1, | |
334 | buf, maxSize, NULL, NULL ); | |
a4f96412 | 335 | |
b568d04f | 336 | return TRUE; |
a4f96412 VZ |
337 | |
338 | error: | |
b568d04f | 339 | wxLogError(wxT("Couldn't look up full user name.")); |
a4f96412 | 340 | |
b568d04f | 341 | return FALSE; |
a4f96412 | 342 | #else // !USE_NET_API |
b568d04f VZ |
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 | } | |
0a144271 | 350 | |
b568d04f VZ |
351 | if ( !ok ) |
352 | { | |
353 | wxStrncpy(buf, wxT("Unknown User"), maxSize); | |
354 | } | |
a4f96412 | 355 | #endif // Win32/16 |
b568d04f | 356 | } |
0a144271 | 357 | |
b568d04f | 358 | return TRUE; |
2bda0e17 KB |
359 | } |
360 | ||
b568d04f | 361 | const wxChar* wxGetHomeDir(wxString *pstr) |
2bda0e17 | 362 | { |
b568d04f | 363 | wxString& strDir = *pstr; |
2bda0e17 | 364 | |
b568d04f VZ |
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; | |
2bda0e17 | 374 | |
b568d04f VZ |
375 | // add a trailing slash if needed |
376 | if ( strDir.Last() != wxT('/') ) | |
377 | strDir << wxT('/'); | |
3ffbc733 | 378 | |
e02e8816 | 379 | #ifdef __CYGWIN__ |
3ffbc733 VZ |
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 | |
b568d04f VZ |
385 | #else // Windows |
386 | #ifdef __WIN32__ | |
96c21216 VZ |
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 | ||
b568d04f | 393 | if ( szHome != NULL ) |
96c21216 VZ |
394 | { |
395 | strDir = szHome; | |
b568d04f | 396 | } |
96c21216 VZ |
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")); | |
2bda0e17 | 424 | |
96c21216 VZ |
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 | |
b568d04f | 434 | #else // Win16 |
96c21216 | 435 | // Win16 has no idea about home, so use the executable directory instead |
b568d04f | 436 | #endif // WIN16/32 |
2bda0e17 | 437 | |
b568d04f VZ |
438 | // 260 was taken from windef.h |
439 | #ifndef MAX_PATH | |
440 | #define MAX_PATH 260 | |
441 | #endif | |
2bda0e17 | 442 | |
b568d04f VZ |
443 | wxString strPath; |
444 | ::GetModuleFileName(::GetModuleHandle(NULL), | |
445 | strPath.GetWriteBuf(MAX_PATH), MAX_PATH); | |
446 | strPath.UngetWriteBuf(); | |
2bda0e17 | 447 | |
b568d04f VZ |
448 | // extract the dir name |
449 | wxSplitPath(strPath, &strDir, NULL, NULL); | |
58a33cb4 | 450 | |
b568d04f VZ |
451 | #endif // UNIX/Win |
452 | ||
453 | return strDir.c_str(); | |
afb74891 VZ |
454 | } |
455 | ||
33ac7e6f | 456 | wxChar *wxGetUserHome(const wxString& WXUNUSED(user)) |
2bda0e17 | 457 | { |
b568d04f VZ |
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; | |
2bda0e17 | 462 | |
b568d04f | 463 | return (wxChar *)wxGetHomeDir(&s_home); |
2bda0e17 KB |
464 | } |
465 | ||
b568d04f | 466 | bool wxDirExists(const wxString& dir) |
2bda0e17 | 467 | { |
04ef50df JS |
468 | #ifdef __WXMICROWIN__ |
469 | return wxPathExist(dir); | |
470 | #elif defined(__WIN32__) | |
cf1eeea3 | 471 | DWORD attribs = GetFileAttributes(dir); |
3897b707 | 472 | return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY)); |
b568d04f VZ |
473 | #else // Win16 |
474 | #ifdef __BORLANDC__ | |
475 | struct ffblk fileInfo; | |
476 | #else | |
477 | struct find_t fileInfo; | |
478 | #endif | |
b568d04f VZ |
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 | } | |
2bda0e17 | 491 | |
eadd7bd2 VZ |
492 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
493 | { | |
494 | if ( path.empty() ) | |
495 | return FALSE; | |
496 | ||
ee88cb34 MB |
497 | // old w32api don't have ULARGE_INTEGER |
498 | #if defined(__WIN32__) && \ | |
499 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 )) | |
eadd7bd2 VZ |
500 | // GetDiskFreeSpaceEx() is not available under original Win95, check for |
501 | // it | |
9ee966ec VZ |
502 | typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR, |
503 | PULARGE_INTEGER, | |
504 | PULARGE_INTEGER, | |
505 | PULARGE_INTEGER); | |
eadd7bd2 VZ |
506 | |
507 | GetDiskFreeSpaceEx_t | |
bb0e27ee | 508 | pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress |
eadd7bd2 VZ |
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 | ||
221e1181 VZ |
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 | |
eadd7bd2 VZ |
541 | if ( pTotal ) |
542 | { | |
221e1181 | 543 | *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart); |
eadd7bd2 VZ |
544 | } |
545 | ||
546 | if ( pFree ) | |
547 | { | |
221e1181 | 548 | *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart); |
eadd7bd2 VZ |
549 | } |
550 | } | |
551 | else | |
552 | #endif // Win32 | |
553 | { | |
bb0e27ee VZ |
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 | ||
eadd7bd2 VZ |
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 | ||
eadd7bd2 VZ |
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 | ||
308978f6 VZ |
595 | // ---------------------------------------------------------------------------- |
596 | // env vars | |
597 | // ---------------------------------------------------------------------------- | |
598 | ||
599 | bool wxGetEnv(const wxString& var, wxString *value) | |
600 | { | |
788722ac JS |
601 | #ifdef __WIN16__ |
602 | const wxChar* ret = wxGetenv(var); | |
e622dd68 VZ |
603 | if ( !ret ) |
604 | return FALSE; | |
605 | ||
606 | if ( value ) | |
788722ac JS |
607 | { |
608 | *value = ret; | |
788722ac | 609 | } |
e622dd68 VZ |
610 | |
611 | return TRUE; | |
612 | #else // Win32 | |
308978f6 VZ |
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; | |
e622dd68 | 628 | #endif // Win16/32 |
308978f6 VZ |
629 | } |
630 | ||
1fb45475 VZ |
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 | ||
b568d04f VZ |
649 | // ---------------------------------------------------------------------------- |
650 | // process management | |
651 | // ---------------------------------------------------------------------------- | |
652 | ||
50567b69 VZ |
653 | #ifdef __WIN32__ |
654 | ||
655 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() | |
656 | struct wxFindByPidParams | |
b568d04f | 657 | { |
50567b69 VZ |
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 | ||
667 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
668 | // level) window belonging to the given process | |
669 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
670 | { | |
671 | DWORD pid; | |
672 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
673 | ||
674 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
675 | if ( pid == params->pid ) | |
676 | { | |
677 | // remember the window we found | |
678 | params->hwnd = hwnd; | |
679 | ||
680 | // return FALSE to stop the enumeration | |
681 | return FALSE; | |
682 | } | |
683 | ||
684 | // continue enumeration | |
685 | return TRUE; | |
686 | } | |
e949bf13 | 687 | |
50567b69 VZ |
688 | #endif // __WIN32__ |
689 | ||
690 | int wxKill(long pid, wxSignal sig, wxKillError *krc) | |
691 | { | |
692 | #ifdef __WIN32__ | |
693 | // get the process handle to operate on | |
694 | HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | | |
695 | PROCESS_TERMINATE | | |
696 | PROCESS_QUERY_INFORMATION, | |
697 | FALSE, // not inheritable | |
698 | (DWORD)pid); | |
699 | if ( hProcess == NULL ) | |
e949bf13 | 700 | { |
50567b69 | 701 | if ( krc ) |
e949bf13 | 702 | { |
50567b69 | 703 | if ( ::GetLastError() == ERROR_ACCESS_DENIED ) |
e949bf13 | 704 | { |
50567b69 VZ |
705 | *krc = wxKILL_ACCESS_DENIED; |
706 | } | |
707 | else | |
708 | { | |
709 | *krc = wxKILL_NO_PROCESS; | |
710 | } | |
711 | } | |
712 | ||
713 | return -1; | |
714 | } | |
715 | ||
716 | bool ok = TRUE; | |
717 | switch ( sig ) | |
718 | { | |
719 | case wxSIGKILL: | |
720 | // kill the process forcefully returning -1 as error code | |
721 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
722 | { | |
723 | wxLogSysError(_("Failed to kill process %d"), pid); | |
724 | ||
725 | if ( krc ) | |
e949bf13 | 726 | { |
50567b69 VZ |
727 | // this is not supposed to happen if we could open the |
728 | // process | |
729 | *krc = wxKILL_ERROR; | |
e949bf13 | 730 | } |
50567b69 VZ |
731 | |
732 | ok = FALSE; | |
e949bf13 | 733 | } |
50567b69 VZ |
734 | break; |
735 | ||
736 | case wxSIGNONE: | |
737 | // do nothing, we just want to test for process existence | |
738 | break; | |
739 | ||
740 | default: | |
741 | // any other signal means "terminate" | |
742 | { | |
743 | wxFindByPidParams params; | |
744 | params.pid = (DWORD)pid; | |
745 | ||
746 | // EnumWindows() has nice semantics: it returns 0 if it found | |
747 | // something or if an error occured and non zero if it | |
748 | // enumerated all the window | |
749 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
750 | { | |
751 | // did we find any window? | |
752 | if ( params.hwnd ) | |
753 | { | |
754 | // tell the app to close | |
755 | // | |
756 | // NB: this is the harshest way, the app won't have | |
757 | // opportunity to save any files, for example, but | |
758 | // this is probably what we want here. If not we | |
759 | // can also use SendMesageTimeout(WM_CLOSE) | |
760 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
761 | { | |
762 | wxLogLastError(_T("PostMessage(WM_QUIT)")); | |
763 | } | |
764 | } | |
765 | else // it was an error then | |
766 | { | |
767 | wxLogLastError(_T("EnumWindows")); | |
768 | ||
769 | ok = FALSE; | |
770 | } | |
771 | } | |
772 | else // no windows for this PID | |
773 | { | |
774 | if ( krc ) | |
775 | { | |
776 | *krc = wxKILL_ERROR; | |
777 | } | |
778 | ||
779 | ok = FALSE; | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | // the return code | |
785 | DWORD rc; | |
786 | ||
787 | if ( ok ) | |
788 | { | |
789 | // as we wait for a short time, we can use just WaitForSingleObject() | |
790 | // and not MsgWaitForMultipleObjects() | |
791 | switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) ) | |
792 | { | |
793 | case WAIT_OBJECT_0: | |
794 | // process terminated | |
795 | if ( !::GetExitCodeProcess(hProcess, &rc) ) | |
796 | { | |
797 | wxLogLastError(_T("GetExitCodeProcess")); | |
798 | } | |
799 | break; | |
800 | ||
801 | default: | |
802 | wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") ); | |
803 | // fall through | |
804 | ||
805 | case WAIT_FAILED: | |
806 | wxLogLastError(_T("WaitForSingleObject")); | |
807 | // fall through | |
808 | ||
809 | case WAIT_TIMEOUT: | |
810 | if ( krc ) | |
811 | { | |
812 | *krc = wxKILL_ERROR; | |
813 | } | |
814 | ||
815 | rc = STILL_ACTIVE; | |
816 | break; | |
e949bf13 JS |
817 | } |
818 | } | |
50567b69 VZ |
819 | else // !ok |
820 | { | |
821 | // just to suppress the warnings about uninitialized variable | |
822 | rc = 0; | |
823 | } | |
e949bf13 | 824 | |
50567b69 | 825 | ::CloseHandle(hProcess); |
b568d04f | 826 | |
50567b69 VZ |
827 | // the return code is the same as from Unix kill(): 0 if killed |
828 | // successfully or -1 on error | |
829 | if ( sig == wxSIGNONE ) | |
e949bf13 | 830 | { |
50567b69 VZ |
831 | if ( ok && rc == STILL_ACTIVE ) |
832 | { | |
833 | // there is such process => success | |
e949bf13 | 834 | return 0; |
50567b69 | 835 | } |
e949bf13 | 836 | } |
50567b69 | 837 | else // not SIGNONE |
e949bf13 | 838 | { |
50567b69 VZ |
839 | if ( ok && rc != STILL_ACTIVE ) |
840 | { | |
841 | // killed => success | |
e949bf13 | 842 | return 0; |
50567b69 | 843 | } |
e949bf13 | 844 | } |
f6ba47d9 | 845 | #else // Win16 |
50567b69 VZ |
846 | wxFAIL_MSG( _T("not implemented") ); |
847 | #endif // Win32/Win16 | |
848 | ||
849 | // error | |
850 | return -1; | |
2bda0e17 KB |
851 | } |
852 | ||
b568d04f VZ |
853 | // Execute a program in an Interactive Shell |
854 | bool wxShell(const wxString& command) | |
2bda0e17 | 855 | { |
b568d04f VZ |
856 | wxChar *shell = wxGetenv(wxT("COMSPEC")); |
857 | if ( !shell ) | |
ba14d986 | 858 | shell = (wxChar*) wxT("\\COMMAND.COM"); |
b568d04f VZ |
859 | |
860 | wxString cmd; | |
861 | if ( !command ) | |
862 | { | |
863 | // just the shell | |
864 | cmd = shell; | |
865 | } | |
866 | else | |
867 | { | |
868 | // pass the command to execute to the command processor | |
869 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
870 | } | |
871 | ||
011a524e | 872 | return wxExecute(cmd, wxEXEC_SYNC) == 0; |
2bda0e17 KB |
873 | } |
874 | ||
f6ba47d9 VZ |
875 | // Shutdown or reboot the PC |
876 | bool wxShutdown(wxShutdownFlags wFlags) | |
877 | { | |
878 | #ifdef __WIN32__ | |
879 | bool bOK = TRUE; | |
880 | ||
881 | if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K | |
882 | { | |
883 | // Get a token for this process. | |
884 | HANDLE hToken; | |
885 | bOK = ::OpenProcessToken(GetCurrentProcess(), | |
886 | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, | |
887 | &hToken) != 0; | |
3bce6687 | 888 | #ifndef __WXWINE__ |
f6ba47d9 VZ |
889 | if ( bOK ) |
890 | { | |
891 | TOKEN_PRIVILEGES tkp; | |
892 | ||
893 | // Get the LUID for the shutdown privilege. | |
894 | ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, | |
895 | &tkp.Privileges[0].Luid); | |
896 | ||
897 | tkp.PrivilegeCount = 1; // one privilege to set | |
898 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
899 | ||
900 | // Get the shutdown privilege for this process. | |
901 | ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, | |
902 | (PTOKEN_PRIVILEGES)NULL, 0); | |
903 | ||
904 | // Cannot test the return value of AdjustTokenPrivileges. | |
905 | bOK = ::GetLastError() == ERROR_SUCCESS; | |
906 | } | |
3bce6687 | 907 | #endif |
f6ba47d9 VZ |
908 | } |
909 | ||
910 | if ( bOK ) | |
911 | { | |
912 | UINT flags = EWX_SHUTDOWN | EWX_FORCE; | |
913 | switch ( wFlags ) | |
914 | { | |
915 | case wxSHUTDOWN_POWEROFF: | |
916 | flags |= EWX_POWEROFF; | |
917 | break; | |
918 | ||
919 | case wxSHUTDOWN_REBOOT: | |
920 | flags |= EWX_REBOOT; | |
921 | break; | |
922 | ||
923 | default: | |
924 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
925 | return FALSE; | |
926 | } | |
927 | ||
928 | bOK = ::ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_REBOOT, 0) != 0; | |
929 | } | |
930 | ||
931 | return bOK; | |
932 | #else // Win16 | |
933 | return FALSE; | |
934 | #endif // Win32/16 | |
935 | } | |
936 | ||
b568d04f VZ |
937 | // ---------------------------------------------------------------------------- |
938 | // misc | |
939 | // ---------------------------------------------------------------------------- | |
940 | ||
941 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
942 | long wxGetFreeMemory() | |
2bda0e17 | 943 | { |
b568d04f VZ |
944 | #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__) |
945 | MEMORYSTATUS memStatus; | |
946 | memStatus.dwLength = sizeof(MEMORYSTATUS); | |
947 | GlobalMemoryStatus(&memStatus); | |
948 | return memStatus.dwAvailPhys; | |
949 | #else | |
950 | return (long)GetFreeSpace(0); | |
951 | #endif | |
2bda0e17 KB |
952 | } |
953 | ||
c1cb4153 VZ |
954 | unsigned long wxGetProcessId() |
955 | { | |
956 | #ifdef __WIN32__ | |
957 | return ::GetCurrentProcessId(); | |
958 | #else | |
959 | return 0; | |
960 | #endif | |
961 | } | |
962 | ||
2bda0e17 | 963 | // Emit a beeeeeep |
634903fd | 964 | void wxBell() |
2bda0e17 | 965 | { |
b568d04f | 966 | ::MessageBeep((UINT)-1); // default sound |
2bda0e17 KB |
967 | } |
968 | ||
bdc72a22 | 969 | wxString wxGetOsDescription() |
2bda0e17 | 970 | { |
bdc72a22 VZ |
971 | #ifdef __WIN32__ |
972 | wxString str; | |
973 | ||
974 | OSVERSIONINFO info; | |
975 | wxZeroMemory(info); | |
976 | ||
977 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
978 | if ( ::GetVersionEx(&info) ) | |
979 | { | |
980 | switch ( info.dwPlatformId ) | |
981 | { | |
982 | case VER_PLATFORM_WIN32s: | |
983 | str = _("Win32s on Windows 3.1"); | |
984 | break; | |
985 | ||
986 | case VER_PLATFORM_WIN32_WINDOWS: | |
987 | str.Printf(_("Windows 9%c"), | |
a46a73a6 | 988 | info.dwMinorVersion == 0 ? _T('5') : _T('8')); |
bdc72a22 VZ |
989 | if ( !wxIsEmpty(info.szCSDVersion) ) |
990 | { | |
991 | str << _T(" (") << info.szCSDVersion << _T(')'); | |
992 | } | |
993 | break; | |
994 | ||
995 | case VER_PLATFORM_WIN32_NT: | |
996 | str.Printf(_T("Windows NT %lu.%lu (build %lu"), | |
997 | info.dwMajorVersion, | |
998 | info.dwMinorVersion, | |
999 | info.dwBuildNumber); | |
1000 | if ( !wxIsEmpty(info.szCSDVersion) ) | |
1001 | { | |
1002 | str << _T(", ") << info.szCSDVersion; | |
1003 | } | |
1004 | str << _T(')'); | |
1005 | break; | |
1006 | } | |
1007 | } | |
1008 | else | |
1009 | { | |
1010 | wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen | |
1011 | } | |
1012 | ||
1013 | return str; | |
1014 | #else // Win16 | |
1015 | return _("Windows 3.1"); | |
1016 | #endif // Win32/16 | |
1017 | } | |
6f65e337 | 1018 | |
bdc72a22 VZ |
1019 | int wxGetOsVersion(int *majorVsn, int *minorVsn) |
1020 | { | |
2432b92d | 1021 | #if defined(__WIN32__) && !defined(__SC__) |
6af638ed | 1022 | static int ver = -1, major = -1, minor = -1; |
96c21216 | 1023 | |
6af638ed | 1024 | if ( ver == -1 ) |
bdc72a22 | 1025 | { |
6af638ed VS |
1026 | OSVERSIONINFO info; |
1027 | wxZeroMemory(info); | |
ec5d7799 | 1028 | |
6af638ed VS |
1029 | ver = wxWINDOWS; |
1030 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
1031 | if ( ::GetVersionEx(&info) ) | |
bdc72a22 | 1032 | { |
6af638ed VS |
1033 | major = info.dwMajorVersion; |
1034 | minor = info.dwMinorVersion; | |
bdc72a22 | 1035 | |
6af638ed VS |
1036 | switch ( info.dwPlatformId ) |
1037 | { | |
1038 | case VER_PLATFORM_WIN32s: | |
1039 | ver = wxWIN32S; | |
1040 | break; | |
bdc72a22 | 1041 | |
6af638ed VS |
1042 | case VER_PLATFORM_WIN32_WINDOWS: |
1043 | ver = wxWIN95; | |
1044 | break; | |
1045 | ||
1046 | case VER_PLATFORM_WIN32_NT: | |
1047 | ver = wxWINDOWS_NT; | |
1048 | break; | |
1049 | } | |
bdc72a22 VZ |
1050 | } |
1051 | } | |
1052 | ||
6af638ed VS |
1053 | if (majorVsn && major != -1) |
1054 | *majorVsn = major; | |
1055 | if (minorVsn && minor != -1) | |
1056 | *minorVsn = minor; | |
96c21216 | 1057 | |
6af638ed | 1058 | return ver; |
bdc72a22 VZ |
1059 | #else // Win16 |
1060 | int retValue = wxWINDOWS; | |
1061 | #ifdef __WINDOWS_386__ | |
1062 | retValue = wxWIN386; | |
1063 | #else | |
1064 | #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS | |
1065 | extern HANDLE g_hPenWin; | |
1066 | retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS; | |
1067 | #endif | |
1068 | #endif | |
1069 | ||
1070 | if (majorVsn) | |
1071 | *majorVsn = 3; | |
1072 | if (minorVsn) | |
1073 | *minorVsn = 1; | |
1074 | ||
1075 | return retValue; | |
6f65e337 | 1076 | #endif |
2bda0e17 KB |
1077 | } |
1078 | ||
b568d04f VZ |
1079 | // ---------------------------------------------------------------------------- |
1080 | // sleep functions | |
1081 | // ---------------------------------------------------------------------------- | |
1082 | ||
1083 | #if wxUSE_GUI | |
1084 | ||
1e6feb95 VZ |
1085 | #if wxUSE_TIMER |
1086 | ||
b568d04f VZ |
1087 | // Sleep for nSecs seconds. Attempt a Windows implementation using timers. |
1088 | static bool gs_inTimer = FALSE; | |
1089 | ||
1e6feb95 | 1090 | class wxSleepTimer : public wxTimer |
b568d04f VZ |
1091 | { |
1092 | public: | |
1093 | virtual void Notify() | |
1094 | { | |
1095 | gs_inTimer = FALSE; | |
1096 | Stop(); | |
1097 | } | |
1098 | }; | |
1099 | ||
1100 | static wxTimer *wxTheSleepTimer = NULL; | |
1101 | ||
1102 | void wxUsleep(unsigned long milliseconds) | |
1103 | { | |
1104 | #ifdef __WIN32__ | |
1105 | ::Sleep(milliseconds); | |
1e6feb95 | 1106 | #else // !Win32 |
b568d04f VZ |
1107 | if (gs_inTimer) |
1108 | return; | |
1109 | ||
1110 | wxTheSleepTimer = new wxSleepTimer; | |
1111 | gs_inTimer = TRUE; | |
1112 | wxTheSleepTimer->Start(milliseconds); | |
1113 | while (gs_inTimer) | |
1114 | { | |
1115 | if (wxTheApp->Pending()) | |
1116 | wxTheApp->Dispatch(); | |
1117 | } | |
1118 | delete wxTheSleepTimer; | |
1119 | wxTheSleepTimer = NULL; | |
1e6feb95 | 1120 | #endif // Win32/!Win32 |
b568d04f VZ |
1121 | } |
1122 | ||
1123 | void wxSleep(int nSecs) | |
1124 | { | |
1125 | if (gs_inTimer) | |
1126 | return; | |
1127 | ||
1128 | wxTheSleepTimer = new wxSleepTimer; | |
1129 | gs_inTimer = TRUE; | |
1130 | wxTheSleepTimer->Start(nSecs*1000); | |
1131 | while (gs_inTimer) | |
1132 | { | |
1133 | if (wxTheApp->Pending()) | |
1134 | wxTheApp->Dispatch(); | |
1135 | } | |
1136 | delete wxTheSleepTimer; | |
1137 | wxTheSleepTimer = NULL; | |
1138 | } | |
1139 | ||
1140 | // Consume all events until no more left | |
1141 | void wxFlushEvents() | |
1142 | { | |
1143 | // wxYield(); | |
1144 | } | |
1145 | ||
1e6feb95 VZ |
1146 | #endif // wxUSE_TIMER |
1147 | ||
b568d04f VZ |
1148 | #elif defined(__WIN32__) // wxUSE_GUI |
1149 | ||
1150 | void wxUsleep(unsigned long milliseconds) | |
1151 | { | |
1152 | ::Sleep(milliseconds); | |
1153 | } | |
1154 | ||
1155 | void wxSleep(int nSecs) | |
1156 | { | |
1157 | wxUsleep(1000*nSecs); | |
1158 | } | |
1159 | ||
1160 | #endif // wxUSE_GUI/!wxUSE_GUI | |
373658eb | 1161 | #endif // __WXMICROWIN__ |
b568d04f VZ |
1162 | |
1163 | // ---------------------------------------------------------------------------- | |
1164 | // deprecated (in favour of wxLog) log functions | |
1165 | // ---------------------------------------------------------------------------- | |
1166 | ||
73deed44 | 1167 | #if WXWIN_COMPATIBILITY_2_2 |
b568d04f VZ |
1168 | |
1169 | // Output a debug mess., in a system dependent fashion. | |
04ef50df | 1170 | #ifndef __WXMICROWIN__ |
b568d04f VZ |
1171 | void wxDebugMsg(const wxChar *fmt ...) |
1172 | { | |
1173 | va_list ap; | |
1174 | static wxChar buffer[512]; | |
1175 | ||
1176 | if (!wxTheApp->GetWantDebugOutput()) | |
f6ba47d9 | 1177 | return; |
b568d04f VZ |
1178 | |
1179 | va_start(ap, fmt); | |
1180 | ||
f6ba47d9 VZ |
1181 | wvsprintf(buffer,fmt,ap); |
1182 | OutputDebugString((LPCTSTR)buffer); | |
b568d04f VZ |
1183 | |
1184 | va_end(ap); | |
1185 | } | |
1186 | ||
1187 | // Non-fatal error: pop up message box and (possibly) continue | |
1188 | void wxError(const wxString& msg, const wxString& title) | |
1189 | { | |
1190 | wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg); | |
1191 | if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title, | |
1192 | MB_ICONSTOP | MB_YESNO) == IDNO) | |
1193 | wxExit(); | |
1194 | } | |
1195 | ||
1196 | // Fatal error: pop up message box and abort | |
1197 | void wxFatalError(const wxString& msg, const wxString& title) | |
1198 | { | |
1199 | wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg); | |
1200 | FatalAppExit(0, (LPCTSTR)wxBuffer); | |
1201 | } | |
04ef50df | 1202 | #endif // __WXMICROWIN__ |
b568d04f | 1203 | |
73deed44 VZ |
1204 | #endif // WXWIN_COMPATIBILITY_2_2 |
1205 | ||
1206 | #if wxUSE_GUI | |
1207 | ||
b568d04f VZ |
1208 | // ---------------------------------------------------------------------------- |
1209 | // functions to work with .INI files | |
1210 | // ---------------------------------------------------------------------------- | |
1211 | ||
2bda0e17 | 1212 | // Reading and writing resources (eg WIN.INI, .Xdefaults) |
47d67540 | 1213 | #if wxUSE_RESOURCES |
2bda0e17 KB |
1214 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) |
1215 | { | |
223d09f6 | 1216 | if (file != wxT("")) |
8fb3a512 JS |
1217 | // Don't know what the correct cast should be, but it doesn't |
1218 | // compile in BC++/16-bit without this cast. | |
1219 | #if !defined(__WIN32__) | |
1220 | return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0); | |
1221 | #else | |
837e5743 | 1222 | return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0); |
8fb3a512 | 1223 | #endif |
2bda0e17 | 1224 | else |
837e5743 | 1225 | return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0); |
2bda0e17 KB |
1226 | } |
1227 | ||
1228 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
1229 | { | |
b568d04f VZ |
1230 | wxString buf; |
1231 | buf.Printf(wxT("%.4f"), value); | |
1232 | ||
1233 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1234 | } |
1235 | ||
1236 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
1237 | { | |
b568d04f VZ |
1238 | wxString buf; |
1239 | buf.Printf(wxT("%ld"), value); | |
1240 | ||
1241 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1242 | } |
1243 | ||
1244 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
1245 | { | |
b568d04f VZ |
1246 | wxString buf; |
1247 | buf.Printf(wxT("%d"), value); | |
1248 | ||
1249 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1250 | } |
1251 | ||
837e5743 | 1252 | bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file) |
2bda0e17 | 1253 | { |
b568d04f VZ |
1254 | static const wxChar defunkt[] = wxT("$$default"); |
1255 | if (file != wxT("")) | |
1256 | { | |
1257 | int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1258 | (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file); | |
1259 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1260 | return FALSE; | |
1261 | } | |
1262 | else | |
1263 | { | |
1264 | int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1265 | (LPTSTR)wxBuffer, 1000); | |
1266 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1267 | return FALSE; | |
2bda0e17 | 1268 | } |
b568d04f VZ |
1269 | if (*value) delete[] (*value); |
1270 | *value = copystring(wxBuffer); | |
1271 | return TRUE; | |
1272 | } | |
2bda0e17 KB |
1273 | |
1274 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
1275 | { | |
b568d04f VZ |
1276 | wxChar *s = NULL; |
1277 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1278 | if (succ) | |
1279 | { | |
1280 | *value = (float)wxStrtod(s, NULL); | |
1281 | delete[] s; | |
1282 | return TRUE; | |
1283 | } | |
1284 | else return FALSE; | |
2bda0e17 KB |
1285 | } |
1286 | ||
1287 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
1288 | { | |
b568d04f VZ |
1289 | wxChar *s = NULL; |
1290 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1291 | if (succ) | |
1292 | { | |
1293 | *value = wxStrtol(s, NULL, 10); | |
1294 | delete[] s; | |
1295 | return TRUE; | |
1296 | } | |
1297 | else return FALSE; | |
2bda0e17 KB |
1298 | } |
1299 | ||
1300 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
1301 | { | |
b568d04f VZ |
1302 | wxChar *s = NULL; |
1303 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1304 | if (succ) | |
1305 | { | |
1306 | *value = (int)wxStrtol(s, NULL, 10); | |
1307 | delete[] s; | |
1308 | return TRUE; | |
1309 | } | |
1310 | else return FALSE; | |
2bda0e17 | 1311 | } |
47d67540 | 1312 | #endif // wxUSE_RESOURCES |
2bda0e17 | 1313 | |
634903fd | 1314 | // --------------------------------------------------------------------------- |
25889d3c | 1315 | // helper functions for showing a "busy" cursor |
634903fd VZ |
1316 | // --------------------------------------------------------------------------- |
1317 | ||
bfbd6dc1 VZ |
1318 | static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor |
1319 | static HCURSOR gs_wxBusyCursorOld = 0; // old cursor | |
634903fd | 1320 | static int gs_wxBusyCursorCount = 0; |
2bda0e17 | 1321 | |
bfbd6dc1 VZ |
1322 | extern HCURSOR wxGetCurrentBusyCursor() |
1323 | { | |
1324 | return gs_wxBusyCursor; | |
1325 | } | |
1326 | ||
2bda0e17 KB |
1327 | // Set the cursor to the busy cursor for all windows |
1328 | void wxBeginBusyCursor(wxCursor *cursor) | |
1329 | { | |
634903fd VZ |
1330 | if ( gs_wxBusyCursorCount++ == 0 ) |
1331 | { | |
1332 | gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR(); | |
04ef50df | 1333 | #ifndef __WXMICROWIN__ |
634903fd | 1334 | gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor); |
04ef50df | 1335 | #endif |
634903fd VZ |
1336 | } |
1337 | //else: nothing to do, already set | |
2bda0e17 KB |
1338 | } |
1339 | ||
1340 | // Restore cursor to normal | |
634903fd | 1341 | void wxEndBusyCursor() |
2bda0e17 | 1342 | { |
634903fd | 1343 | wxCHECK_RET( gs_wxBusyCursorCount > 0, |
223d09f6 | 1344 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); |
b07135ba | 1345 | |
634903fd VZ |
1346 | if ( --gs_wxBusyCursorCount == 0 ) |
1347 | { | |
04ef50df | 1348 | #ifndef __WXMICROWIN__ |
634903fd | 1349 | ::SetCursor(gs_wxBusyCursorOld); |
04ef50df | 1350 | #endif |
634903fd VZ |
1351 | gs_wxBusyCursorOld = 0; |
1352 | } | |
2bda0e17 KB |
1353 | } |
1354 | ||
1355 | // TRUE if we're between the above two calls | |
634903fd | 1356 | bool wxIsBusy() |
2bda0e17 | 1357 | { |
373658eb | 1358 | return gs_wxBusyCursorCount > 0; |
b07135ba | 1359 | } |
2bda0e17 | 1360 | |
2bda0e17 KB |
1361 | // Check whether this window wants to process messages, e.g. Stop button |
1362 | // in long calculations. | |
1363 | bool wxCheckForInterrupt(wxWindow *wnd) | |
1364 | { | |
b568d04f VZ |
1365 | wxCHECK( wnd, FALSE ); |
1366 | ||
743e0a66 | 1367 | MSG msg; |
b568d04f VZ |
1368 | while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) ) |
1369 | { | |
1370 | ::TranslateMessage(&msg); | |
1371 | ::DispatchMessage(&msg); | |
743e0a66 | 1372 | } |
634903fd | 1373 | |
b568d04f | 1374 | return TRUE; |
2bda0e17 KB |
1375 | } |
1376 | ||
1377 | // MSW only: get user-defined resource from the .res file. | |
1378 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
1379 | ||
04ef50df | 1380 | #ifndef __WXMICROWIN__ |
837e5743 | 1381 | wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType) |
2bda0e17 | 1382 | { |
b568d04f VZ |
1383 | HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType); |
1384 | if ( hResource == 0 ) | |
1385 | return NULL; | |
1386 | ||
1387 | HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); | |
1388 | if ( hData == 0 ) | |
1389 | return NULL; | |
2bda0e17 | 1390 | |
b568d04f VZ |
1391 | wxChar *theText = (wxChar *)::LockResource(hData); |
1392 | if ( !theText ) | |
1393 | return NULL; | |
b07135ba | 1394 | |
8caa4ed1 JS |
1395 | // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). |
1396 | // so we need to find the length of the resource. | |
1397 | int len = ::SizeofResource(wxGetInstance(), hResource); | |
1398 | wxChar *s = new wxChar[len+1]; | |
1399 | wxStrncpy(s,theText,len); | |
1400 | s[len]=0; | |
1401 | ||
1402 | // wxChar *s = copystring(theText); | |
2bda0e17 | 1403 | |
b568d04f | 1404 | // Obsolete in WIN32 |
2bda0e17 | 1405 | #ifndef __WIN32__ |
b568d04f | 1406 | UnlockResource(hData); |
2bda0e17 KB |
1407 | #endif |
1408 | ||
b568d04f VZ |
1409 | // No need?? |
1410 | // GlobalFree(hData); | |
2bda0e17 | 1411 | |
b568d04f | 1412 | return s; |
2bda0e17 | 1413 | } |
373658eb | 1414 | #endif // __WXMICROWIN__ |
b568d04f VZ |
1415 | |
1416 | // ---------------------------------------------------------------------------- | |
1417 | // get display info | |
1418 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1419 | |
d3db92b2 JS |
1420 | // See also the wxGetMousePosition in window.cpp |
1421 | // Deprecated: use wxPoint wxGetMousePosition() instead | |
2bda0e17 KB |
1422 | void wxGetMousePosition( int* x, int* y ) |
1423 | { | |
b568d04f VZ |
1424 | POINT pt; |
1425 | GetCursorPos( & pt ); | |
1426 | if ( x ) *x = pt.x; | |
1427 | if ( y ) *y = pt.y; | |
2bda0e17 KB |
1428 | }; |
1429 | ||
1430 | // Return TRUE if we have a colour display | |
634903fd | 1431 | bool wxColourDisplay() |
2bda0e17 | 1432 | { |
04ef50df JS |
1433 | #ifdef __WXMICROWIN__ |
1434 | // MICROWIN_TODO | |
1435 | return TRUE; | |
1436 | #else | |
807a903e VZ |
1437 | // this function is called from wxDC ctor so it is called a *lot* of times |
1438 | // hence we optimize it a bit but doign the check only once | |
1439 | // | |
1440 | // this should be MT safe as only the GUI thread (holding the GUI mutex) | |
1441 | // can call us | |
1442 | static int s_isColour = -1; | |
1443 | ||
1444 | if ( s_isColour == -1 ) | |
1445 | { | |
1446 | ScreenHDC dc; | |
1447 | int noCols = ::GetDeviceCaps(dc, NUMCOLORS); | |
1448 | ||
1449 | s_isColour = (noCols == -1) || (noCols > 2); | |
1450 | } | |
b568d04f | 1451 | |
807a903e | 1452 | return s_isColour != 0; |
04ef50df | 1453 | #endif |
2bda0e17 KB |
1454 | } |
1455 | ||
1456 | // Returns depth of screen | |
634903fd | 1457 | int wxDisplayDepth() |
2bda0e17 | 1458 | { |
b568d04f VZ |
1459 | ScreenHDC dc; |
1460 | return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL); | |
2bda0e17 KB |
1461 | } |
1462 | ||
1463 | // Get size of display | |
1464 | void wxDisplaySize(int *width, int *height) | |
1465 | { | |
04ef50df | 1466 | #ifdef __WXMICROWIN__ |
54800df8 JS |
1467 | RECT rect; |
1468 | HWND hWnd = GetDesktopWindow(); | |
1469 | ::GetWindowRect(hWnd, & rect); | |
1470 | ||
373658eb VZ |
1471 | if ( width ) |
1472 | *width = rect.right - rect.left; | |
1473 | if ( height ) | |
1474 | *height = rect.bottom - rect.top; | |
1475 | #else // !__WXMICROWIN__ | |
b568d04f | 1476 | ScreenHDC dc; |
3f4a0c5b | 1477 | |
373658eb VZ |
1478 | if ( width ) |
1479 | *width = ::GetDeviceCaps(dc, HORZRES); | |
1480 | if ( height ) | |
1481 | *height = ::GetDeviceCaps(dc, VERTRES); | |
1482 | #endif // __WXMICROWIN__/!__WXMICROWIN__ | |
7f555861 JS |
1483 | } |
1484 | ||
904a68b6 RL |
1485 | void wxDisplaySizeMM(int *width, int *height) |
1486 | { | |
04ef50df JS |
1487 | #ifdef __WXMICROWIN__ |
1488 | // MICROWIN_TODO | |
373658eb VZ |
1489 | if ( width ) |
1490 | *width = 0; | |
1491 | if ( height ) | |
1492 | *height = 0; | |
04ef50df | 1493 | #else |
904a68b6 RL |
1494 | ScreenHDC dc; |
1495 | ||
373658eb VZ |
1496 | if ( width ) |
1497 | *width = ::GetDeviceCaps(dc, HORZSIZE); | |
1498 | if ( height ) | |
1499 | *height = ::GetDeviceCaps(dc, VERTSIZE); | |
04ef50df | 1500 | #endif |
904a68b6 RL |
1501 | } |
1502 | ||
ec5d7799 RD |
1503 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
1504 | { | |
04ef50df | 1505 | #if defined(__WIN16__) || defined(__WXMICROWIN__) |
788722ac JS |
1506 | *x = 0; *y = 0; |
1507 | wxDisplaySize(width, height); | |
1508 | #else | |
ec5d7799 RD |
1509 | // Determine the desktop dimensions minus the taskbar and any other |
1510 | // special decorations... | |
1511 | RECT r; | |
788722ac | 1512 | |
ec5d7799 RD |
1513 | SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); |
1514 | if (x) *x = r.left; | |
1515 | if (y) *y = r.top; | |
1516 | if (width) *width = r.right - r.left; | |
1517 | if (height) *height = r.bottom - r.top; | |
788722ac | 1518 | #endif |
ec5d7799 RD |
1519 | } |
1520 | ||
cc2b7472 VZ |
1521 | // --------------------------------------------------------------------------- |
1522 | // window information functions | |
1523 | // --------------------------------------------------------------------------- | |
1524 | ||
1c4a764c VZ |
1525 | wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd) |
1526 | { | |
1527 | wxString str; | |
5acc0d5f VZ |
1528 | |
1529 | if ( hWnd ) | |
1530 | { | |
1531 | int len = GetWindowTextLength((HWND)hWnd) + 1; | |
1532 | ::GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len); | |
1533 | str.UngetWriteBuf(); | |
1534 | } | |
1c4a764c VZ |
1535 | |
1536 | return str; | |
1537 | } | |
1538 | ||
cc2b7472 VZ |
1539 | wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd) |
1540 | { | |
1541 | wxString str; | |
1542 | ||
5acc0d5f VZ |
1543 | // MICROWIN_TODO |
1544 | #ifndef __WXMICROWIN__ | |
1545 | if ( hWnd ) | |
cc2b7472 | 1546 | { |
5acc0d5f | 1547 | int len = 256; // some starting value |
cc2b7472 | 1548 | |
5acc0d5f | 1549 | for ( ;; ) |
cc2b7472 | 1550 | { |
5acc0d5f VZ |
1551 | int count = ::GetClassName((HWND)hWnd, str.GetWriteBuf(len), len); |
1552 | ||
1553 | str.UngetWriteBuf(); | |
1554 | if ( count == len ) | |
1555 | { | |
1556 | // the class name might have been truncated, retry with larger | |
1557 | // buffer | |
1558 | len *= 2; | |
1559 | } | |
1560 | else | |
1561 | { | |
1562 | break; | |
1563 | } | |
cc2b7472 VZ |
1564 | } |
1565 | } | |
5acc0d5f | 1566 | #endif // !__WXMICROWIN__ |
cc2b7472 VZ |
1567 | |
1568 | return str; | |
1569 | } | |
1570 | ||
42e69d6b | 1571 | WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd) |
cc2b7472 VZ |
1572 | { |
1573 | #ifndef __WIN32__ | |
33ac7e6f | 1574 | return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID); |
cc2b7472 | 1575 | #else // Win32 |
33ac7e6f | 1576 | return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID); |
cc2b7472 VZ |
1577 | #endif // Win16/32 |
1578 | } | |
1579 | ||
b0c89ddf JS |
1580 | // ---------------------------------------------------------------------------- |
1581 | // Metafile helpers | |
1582 | // ---------------------------------------------------------------------------- | |
1583 | ||
1584 | extern void PixelToHIMETRIC(LONG *x, LONG *y) | |
1585 | { | |
1586 | ScreenHDC hdcRef; | |
1587 | ||
1588 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1589 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1590 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1591 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1592 | ||
1593 | *x *= (iWidthMM * 100); | |
1594 | *x /= iWidthPels; | |
1595 | *y *= (iHeightMM * 100); | |
1596 | *y /= iHeightPels; | |
1597 | } | |
1598 | ||
1599 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
1600 | { | |
1601 | ScreenHDC hdcRef; | |
1602 | ||
1603 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1604 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1605 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1606 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1607 | ||
1608 | *x *= iWidthPels; | |
1609 | *x /= (iWidthMM * 100); | |
1610 | *y *= iHeightPels; | |
1611 | *y /= (iHeightMM * 100); | |
1612 | } | |
1613 | ||
1614 | #endif // wxUSE_GUI | |
1615 | ||
373658eb VZ |
1616 | #ifdef __WXMICROWIN__ |
1617 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
7f555861 | 1618 | { |
373658eb VZ |
1619 | // MICROWIN_TODO |
1620 | if (majorVsn) *majorVsn = 0; | |
1621 | if (minorVsn) *minorVsn = 0; | |
1622 | return wxUNIX; | |
1623 | } | |
1624 | #endif // __WXMICROWIN__ | |
7f555861 | 1625 | |
373658eb VZ |
1626 | // ---------------------------------------------------------------------------- |
1627 | // Win32 codepage conversion functions | |
1628 | // ---------------------------------------------------------------------------- | |
7f555861 | 1629 | |
373658eb | 1630 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
7f555861 | 1631 | |
1f86850d VZ |
1632 | // wxGetNativeFontEncoding() doesn't exist neither in wxBase nor in wxUniv |
1633 | #if wxUSE_GUI && !defined(__WXUNIVERSAL__) | |
c030b70f | 1634 | |
373658eb | 1635 | #include "wx/fontmap.h" |
c030b70f | 1636 | |
373658eb VZ |
1637 | // VZ: the new version of wxCharsetToCodepage() is more politically correct |
1638 | // and should work on other Windows versions as well but the old version is | |
1639 | // still needed for !wxUSE_FONTMAP || !wxUSE_GUI case | |
c030b70f | 1640 | |
373658eb | 1641 | extern long wxEncodingToCodepage(wxFontEncoding encoding) |
c030b70f | 1642 | { |
373658eb VZ |
1643 | // translate encoding into the Windows CHARSET |
1644 | wxNativeEncodingInfo natveEncInfo; | |
1645 | if ( !wxGetNativeFontEncoding(encoding, &natveEncInfo) ) | |
1646 | return -1; | |
1647 | ||
1648 | // translate CHARSET to code page | |
1649 | CHARSETINFO csetInfo; | |
1650 | if ( !::TranslateCharsetInfo((DWORD *)(DWORD)natveEncInfo.charset, | |
1651 | &csetInfo, | |
1652 | TCI_SRCCHARSET) ) | |
1653 | { | |
1654 | wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)")); | |
1655 | ||
1656 | return -1; | |
1657 | } | |
1658 | ||
1659 | return csetInfo.ciACP; | |
c030b70f | 1660 | } |
373658eb VZ |
1661 | |
1662 | #if wxUSE_FONTMAP | |
1663 | ||
1664 | extern long wxCharsetToCodepage(const wxChar *name) | |
c030b70f | 1665 | { |
373658eb VZ |
1666 | // first get the font encoding for this charset |
1667 | if ( !name ) | |
1668 | return -1; | |
1669 | ||
142b3bc2 | 1670 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE); |
373658eb VZ |
1671 | if ( enc == wxFONTENCODING_SYSTEM ) |
1672 | return -1; | |
1673 | ||
1674 | // the use the helper function | |
1675 | return wxEncodingToCodepage(enc); | |
c030b70f | 1676 | } |
c030b70f | 1677 | |
373658eb VZ |
1678 | #endif // wxUSE_FONTMAP |
1679 | ||
1680 | #endif // wxUSE_GUI | |
1681 | ||
1682 | // include old wxCharsetToCodepage() by OK if needed | |
1683 | #if !wxUSE_GUI || !wxUSE_FONTMAP | |
1684 | ||
1685 | #include "wx/msw/registry.h" | |
1686 | ||
1687 | // this should work if Internet Exploiter is installed | |
1688 | extern long wxCharsetToCodepage(const wxChar *name) | |
04ef50df | 1689 | { |
373658eb VZ |
1690 | if (!name) |
1691 | return GetACP(); | |
1692 | ||
1693 | long CP=-1; | |
1694 | ||
1695 | wxString cn(name); | |
1696 | do { | |
1697 | wxString path(wxT("MIME\\Database\\Charset\\")); | |
1698 | path += cn; | |
1699 | wxRegKey key(wxRegKey::HKCR, path); | |
1700 | ||
1701 | if (!key.Exists()) break; | |
1702 | ||
1703 | // two cases: either there's an AliasForCharset string, | |
1704 | // or there are Codepage and InternetEncoding dwords. | |
1705 | // The InternetEncoding gives us the actual encoding, | |
1706 | // the Codepage just says which Windows character set to | |
1707 | // use when displaying the data. | |
1708 | if (key.HasValue(wxT("InternetEncoding")) && | |
1709 | key.QueryValue(wxT("InternetEncoding"), &CP)) break; | |
1710 | ||
1711 | // no encoding, see if it's an alias | |
1712 | if (!key.HasValue(wxT("AliasForCharset")) || | |
1713 | !key.QueryValue(wxT("AliasForCharset"), cn)) break; | |
1714 | } while (1); | |
1715 | ||
1716 | return CP; | |
04ef50df | 1717 | } |
373658eb VZ |
1718 | |
1719 | #endif // !wxUSE_GUI || !wxUSE_FONTMAP | |
1720 | ||
1721 | #endif // Win32 | |
c030b70f | 1722 |