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