]>
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 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
a4f96412 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
a4f96412 VZ |
28 | #include "wx/utils.h" |
29 | #include "wx/app.h" | |
1f0500b3 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
743e0a66 | 32 | #endif //WX_PRECOMP |
2bda0e17 | 33 | |
2739d4f0 VZ |
34 | #include "wx/apptrait.h" |
35 | ||
b568d04f | 36 | #include "wx/msw/private.h" // includes <windows.h> |
fcd0c90f | 37 | #include "wx/msw/missing.h" // CHARSET_HANGUL |
b568d04f | 38 | |
882dfc67 | 39 | #if defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) |
c1cb4153 VZ |
40 | // apparently we need to include winsock.h to get WSADATA and other stuff |
41 | // used in wxGetFullHostName() with the old mingw32 versions | |
42 | #include <winsock.h> | |
43 | #endif | |
44 | ||
2bda0e17 KB |
45 | #include "wx/timer.h" |
46 | ||
4676948b | 47 | #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
a4f96412 | 48 | #include <direct.h> |
ce3ed50d | 49 | |
a4f96412 VZ |
50 | #ifndef __MWERKS__ |
51 | #include <dos.h> | |
52 | #endif | |
743e0a66 | 53 | #endif //GNUWIN32 |
2bda0e17 | 54 | |
b39dbf34 | 55 | #if defined(__CYGWIN__) |
a4f96412 VZ |
56 | #include <sys/unistd.h> |
57 | #include <sys/stat.h> | |
3ffbc733 | 58 | #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path() |
743e0a66 VZ |
59 | #endif //GNUWIN32 |
60 | ||
2bda0e17 KB |
61 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs |
62 | // this (3.1 I believe) and how to test for it. | |
63 | // If this works for Borland 4.0 as well, then no worries. | |
a4f96412 | 64 | #include <dir.h> |
2bda0e17 KB |
65 | #endif |
66 | ||
a4f96412 VZ |
67 | // VZ: there is some code using NetXXX() functions to get the full user name: |
68 | // I don't think it's a good idea because they don't work under Win95 and | |
69 | // seem to return the same as wxGetUserId() under NT. If you really want | |
70 | // to use them, just #define USE_NET_API | |
71 | #undef USE_NET_API | |
72 | ||
73 | #ifdef USE_NET_API | |
74 | #include <lm.h> | |
75 | #endif // USE_NET_API | |
76 | ||
4676948b | 77 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
2e38557f JS |
78 | #ifndef __UNIX__ |
79 | #include <io.h> | |
80 | #endif | |
2bda0e17 | 81 | |
a4f96412 VZ |
82 | #ifndef __GNUWIN32__ |
83 | #include <shellapi.h> | |
84 | #endif | |
2bda0e17 KB |
85 | #endif |
86 | ||
2bda0e17 | 87 | #ifndef __WATCOMC__ |
3f4a0c5b VZ |
88 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
89 | #include <errno.h> | |
90 | #endif | |
2bda0e17 | 91 | #endif |
2bda0e17 | 92 | |
b568d04f VZ |
93 | // ---------------------------------------------------------------------------- |
94 | // constants | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
2bda0e17 | 97 | // In the WIN.INI file |
223d09f6 | 98 | static const wxChar WX_SECTION[] = wxT("wxWindows"); |
b568d04f VZ |
99 | static const wxChar eUSERNAME[] = wxT("UserName"); |
100 | ||
101 | // these are only used under Win16 | |
04ef50df | 102 | #if !defined(__WIN32__) && !defined(__WXMICROWIN__) |
223d09f6 KB |
103 | static const wxChar eHOSTNAME[] = wxT("HostName"); |
104 | static const wxChar eUSERID[] = wxT("UserId"); | |
b568d04f VZ |
105 | #endif // !Win32 |
106 | ||
107 | // ============================================================================ | |
108 | // implementation | |
109 | // ============================================================================ | |
2bda0e17 | 110 | |
b568d04f VZ |
111 | // ---------------------------------------------------------------------------- |
112 | // get host name and related | |
113 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 114 | |
1f0500b3 | 115 | // Get hostname only (without domain name) |
837e5743 | 116 | bool wxGetHostName(wxChar *buf, int maxSize) |
2bda0e17 | 117 | { |
4676948b JS |
118 | #if defined(__WXWINCE__) |
119 | return FALSE; | |
120 | #elif defined(__WIN32__) && !defined(__WXMICROWIN__) | |
b568d04f VZ |
121 | DWORD nSize = maxSize; |
122 | if ( !::GetComputerName(buf, &nSize) ) | |
123 | { | |
f6bcfd97 | 124 | wxLogLastError(wxT("GetComputerName")); |
b568d04f VZ |
125 | |
126 | return FALSE; | |
127 | } | |
128 | ||
129 | return TRUE; | |
2bda0e17 | 130 | #else |
b568d04f VZ |
131 | wxChar *sysname; |
132 | const wxChar *default_host = wxT("noname"); | |
133 | ||
134 | if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) { | |
135 | GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1); | |
136 | } else | |
137 | wxStrncpy(buf, sysname, maxSize - 1); | |
138 | buf[maxSize] = wxT('\0'); | |
139 | return *buf ? TRUE : FALSE; | |
2bda0e17 KB |
140 | #endif |
141 | } | |
142 | ||
1f0500b3 | 143 | // get full hostname (with domain name if possible) |
b568d04f VZ |
144 | bool wxGetFullHostName(wxChar *buf, int maxSize) |
145 | { | |
b39dbf34 | 146 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) |
1f0500b3 | 147 | // TODO should use GetComputerNameEx() when available |
79180098 VZ |
148 | |
149 | // the idea is that if someone had set wxUSE_SOCKETS to 0 the code | |
150 | // shouldn't use winsock.dll (a.k.a. ws2_32.dll) at all so only use this | |
151 | // code if we link with it anyhow | |
152 | #if wxUSE_SOCKETS | |
1f0500b3 VZ |
153 | WSADATA wsa; |
154 | if ( WSAStartup(MAKEWORD(1, 1), &wsa) == 0 ) | |
155 | { | |
156 | wxString host; | |
157 | char bufA[256]; | |
158 | if ( gethostname(bufA, WXSIZEOF(bufA)) == 0 ) | |
159 | { | |
160 | // gethostname() won't usually include the DNS domain name, for | |
161 | // this we need to work a bit more | |
162 | if ( !strchr(bufA, '.') ) | |
163 | { | |
164 | struct hostent *pHostEnt = gethostbyname(bufA); | |
165 | ||
166 | if ( pHostEnt ) | |
167 | { | |
168 | // Windows will use DNS internally now | |
d5b18b50 | 169 | pHostEnt = gethostbyaddr(pHostEnt->h_addr, 4, AF_INET); |
1f0500b3 VZ |
170 | } |
171 | ||
172 | if ( pHostEnt ) | |
173 | { | |
2b5f62a0 | 174 | host = wxString::FromAscii(pHostEnt->h_name); |
1f0500b3 VZ |
175 | } |
176 | } | |
177 | } | |
178 | ||
179 | WSACleanup(); | |
180 | ||
79180098 | 181 | if ( !host.empty() ) |
1f0500b3 VZ |
182 | { |
183 | wxStrncpy(buf, host, maxSize); | |
184 | ||
185 | return TRUE; | |
186 | } | |
187 | } | |
4ce1efe1 | 188 | |
79180098 VZ |
189 | #endif // wxUSE_SOCKETS |
190 | ||
1f0500b3 VZ |
191 | #endif // Win32 |
192 | ||
b568d04f VZ |
193 | return wxGetHostName(buf, maxSize); |
194 | } | |
195 | ||
2bda0e17 | 196 | // Get user ID e.g. jacs |
837e5743 | 197 | bool wxGetUserId(wxChar *buf, int maxSize) |
2bda0e17 | 198 | { |
4676948b JS |
199 | #if defined(__WXWINCE__) |
200 | return FALSE; | |
201 | #elif defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__) | |
0a144271 VZ |
202 | DWORD nSize = maxSize; |
203 | if ( ::GetUserName(buf, &nSize) == 0 ) | |
204 | { | |
a4f96412 | 205 | // actually, it does happen on Win9x if the user didn't log on |
223d09f6 | 206 | DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize); |
a4f96412 VZ |
207 | if ( res == 0 ) |
208 | { | |
209 | // not found | |
210 | return FALSE; | |
211 | } | |
0a144271 VZ |
212 | } |
213 | ||
214 | return TRUE; | |
0a144271 | 215 | #else // Win16 or Win32s |
b568d04f VZ |
216 | wxChar *user; |
217 | const wxChar *default_id = wxT("anonymous"); | |
2bda0e17 | 218 | |
b568d04f VZ |
219 | // Can't assume we have NIS (PC-NFS) or some other ID daemon |
220 | // So we ... | |
221 | if ( (user = wxGetenv(wxT("USER"))) == NULL && | |
222 | (user = wxGetenv(wxT("LOGNAME"))) == NULL ) | |
223 | { | |
224 | // Use wxWindows configuration data (comming soon) | |
225 | GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1); | |
226 | } | |
227 | else | |
228 | { | |
229 | wxStrncpy(buf, user, maxSize - 1); | |
230 | } | |
a4f96412 | 231 | |
b568d04f | 232 | return *buf ? TRUE : FALSE; |
2bda0e17 KB |
233 | #endif |
234 | } | |
235 | ||
236 | // Get user name e.g. Julian Smart | |
837e5743 | 237 | bool wxGetUserName(wxChar *buf, int maxSize) |
2bda0e17 | 238 | { |
4676948b JS |
239 | #if defined(__WXWINCE__) |
240 | return FALSE; | |
241 | #elif defined(USE_NET_API) | |
3e3be693 VZ |
242 | CHAR szUserName[256]; |
243 | if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) ) | |
244 | return FALSE; | |
a4f96412 | 245 | |
3e3be693 VZ |
246 | // TODO how to get the domain name? |
247 | CHAR *szDomain = ""; | |
a4f96412 | 248 | |
3e3be693 VZ |
249 | // the code is based on the MSDN example (also see KB article Q119670) |
250 | WCHAR wszUserName[256]; // Unicode user name | |
251 | WCHAR wszDomain[256]; | |
252 | LPBYTE ComputerName; | |
a4f96412 | 253 | |
3e3be693 | 254 | USER_INFO_2 *ui2; // User structure |
a4f96412 | 255 | |
3e3be693 VZ |
256 | // Convert ANSI user name and domain to Unicode |
257 | MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1, | |
258 | wszUserName, WXSIZEOF(wszUserName) ); | |
259 | MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1, | |
260 | wszDomain, WXSIZEOF(wszDomain) ); | |
a4f96412 | 261 | |
3e3be693 VZ |
262 | // Get the computer name of a DC for the domain. |
263 | if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success ) | |
264 | { | |
265 | wxLogError(wxT("Can not find domain controller")); | |
a4f96412 | 266 | |
3e3be693 VZ |
267 | goto error; |
268 | } | |
a4f96412 | 269 | |
3e3be693 VZ |
270 | // Look up the user on the DC |
271 | NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName, | |
272 | (LPWSTR)&wszUserName, | |
273 | 2, // level - we want USER_INFO_2 | |
274 | (LPBYTE *) &ui2 ); | |
275 | switch ( status ) | |
276 | { | |
277 | case NERR_Success: | |
278 | // ok | |
279 | break; | |
a4f96412 | 280 | |
3e3be693 VZ |
281 | case NERR_InvalidComputer: |
282 | wxLogError(wxT("Invalid domain controller name.")); | |
a4f96412 | 283 | |
3e3be693 | 284 | goto error; |
a4f96412 | 285 | |
3e3be693 VZ |
286 | case NERR_UserNotFound: |
287 | wxLogError(wxT("Invalid user name '%s'."), szUserName); | |
a4f96412 | 288 | |
3e3be693 | 289 | goto error; |
a4f96412 | 290 | |
3e3be693 VZ |
291 | default: |
292 | wxLogSysError(wxT("Can't get information about user")); | |
a4f96412 | 293 | |
3e3be693 VZ |
294 | goto error; |
295 | } | |
a4f96412 | 296 | |
3e3be693 VZ |
297 | // Convert the Unicode full name to ANSI |
298 | WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1, | |
299 | buf, maxSize, NULL, NULL ); | |
a4f96412 | 300 | |
3e3be693 | 301 | return TRUE; |
a4f96412 VZ |
302 | |
303 | error: | |
3e3be693 | 304 | wxLogError(wxT("Couldn't look up full user name.")); |
a4f96412 | 305 | |
3e3be693 | 306 | return FALSE; |
a4f96412 | 307 | #else // !USE_NET_API |
3e3be693 VZ |
308 | // Could use NIS, MS-Mail or other site specific programs |
309 | // Use wxWindows configuration data | |
fda7962d | 310 | bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxEmptyString, buf, maxSize - 1) != 0; |
3e3be693 VZ |
311 | if ( !ok ) |
312 | { | |
313 | ok = wxGetUserId(buf, maxSize); | |
314 | } | |
0a144271 | 315 | |
3e3be693 VZ |
316 | if ( !ok ) |
317 | { | |
318 | wxStrncpy(buf, wxT("Unknown User"), maxSize); | |
b568d04f | 319 | } |
3e3be693 | 320 | #endif // Win32/16 |
0a144271 | 321 | |
b568d04f | 322 | return TRUE; |
2bda0e17 KB |
323 | } |
324 | ||
b568d04f | 325 | const wxChar* wxGetHomeDir(wxString *pstr) |
2bda0e17 | 326 | { |
b568d04f | 327 | wxString& strDir = *pstr; |
2bda0e17 | 328 | |
4676948b | 329 | #if defined(__UNIX__) |
b568d04f VZ |
330 | const wxChar *szHome = wxGetenv("HOME"); |
331 | if ( szHome == NULL ) { | |
332 | // we're homeless... | |
333 | wxLogWarning(_("can't find user's HOME, using current directory.")); | |
334 | strDir = wxT("."); | |
335 | } | |
336 | else | |
337 | strDir = szHome; | |
2bda0e17 | 338 | |
b568d04f VZ |
339 | // add a trailing slash if needed |
340 | if ( strDir.Last() != wxT('/') ) | |
341 | strDir << wxT('/'); | |
3ffbc733 | 342 | |
e02e8816 | 343 | #ifdef __CYGWIN__ |
3ffbc733 VZ |
344 | // Cygwin returns unix type path but that does not work well |
345 | static wxChar windowsPath[MAX_PATH]; | |
346 | cygwin_conv_to_full_win32_path(strDir, windowsPath); | |
347 | strDir = windowsPath; | |
348 | #endif | |
4676948b JS |
349 | #elif defined(__WXWINCE__) |
350 | // Nothing | |
351 | #else | |
b568d04f | 352 | #ifdef __WIN32__ |
96c21216 VZ |
353 | strDir.clear(); |
354 | ||
355 | // If we have a valid HOME directory, as is used on many machines that | |
356 | // have unix utilities on them, we should use that. | |
357 | const wxChar *szHome = wxGetenv(wxT("HOME")); | |
358 | ||
b568d04f | 359 | if ( szHome != NULL ) |
96c21216 VZ |
360 | { |
361 | strDir = szHome; | |
b568d04f | 362 | } |
96c21216 VZ |
363 | else // no HOME, try HOMEDRIVE/PATH |
364 | { | |
365 | szHome = wxGetenv(wxT("HOMEDRIVE")); | |
366 | if ( szHome != NULL ) | |
367 | strDir << szHome; | |
368 | szHome = wxGetenv(wxT("HOMEPATH")); | |
369 | ||
370 | if ( szHome != NULL ) | |
371 | { | |
372 | strDir << szHome; | |
373 | ||
374 | // the idea is that under NT these variables have default values | |
375 | // of "%systemdrive%:" and "\\". As we don't want to create our | |
376 | // config files in the root directory of the system drive, we will | |
377 | // create it in our program's dir. However, if the user took care | |
378 | // to set HOMEPATH to something other than "\\", we suppose that he | |
379 | // knows what he is doing and use the supplied value. | |
380 | if ( wxStrcmp(szHome, wxT("\\")) == 0 ) | |
381 | strDir.clear(); | |
382 | } | |
383 | } | |
384 | ||
385 | if ( strDir.empty() ) | |
386 | { | |
387 | // If we have a valid USERPROFILE directory, as is the case in | |
388 | // Windows NT, 2000 and XP, we should use that as our home directory. | |
389 | szHome = wxGetenv(wxT("USERPROFILE")); | |
2bda0e17 | 390 | |
96c21216 VZ |
391 | if ( szHome != NULL ) |
392 | strDir = szHome; | |
393 | } | |
394 | ||
395 | if ( !strDir.empty() ) | |
396 | { | |
397 | return strDir.c_str(); | |
398 | } | |
399 | //else: fall back to the prograrm directory | |
b568d04f | 400 | #else // Win16 |
96c21216 | 401 | // Win16 has no idea about home, so use the executable directory instead |
b568d04f | 402 | #endif // WIN16/32 |
2bda0e17 | 403 | |
b568d04f VZ |
404 | // 260 was taken from windef.h |
405 | #ifndef MAX_PATH | |
406 | #define MAX_PATH 260 | |
407 | #endif | |
2bda0e17 | 408 | |
b568d04f VZ |
409 | wxString strPath; |
410 | ::GetModuleFileName(::GetModuleHandle(NULL), | |
411 | strPath.GetWriteBuf(MAX_PATH), MAX_PATH); | |
412 | strPath.UngetWriteBuf(); | |
2bda0e17 | 413 | |
b568d04f VZ |
414 | // extract the dir name |
415 | wxSplitPath(strPath, &strDir, NULL, NULL); | |
58a33cb4 | 416 | |
4676948b | 417 | #endif // UNIX/Win |
b568d04f VZ |
418 | |
419 | return strDir.c_str(); | |
afb74891 VZ |
420 | } |
421 | ||
33ac7e6f | 422 | wxChar *wxGetUserHome(const wxString& WXUNUSED(user)) |
2bda0e17 | 423 | { |
b568d04f VZ |
424 | // VZ: the old code here never worked for user != "" anyhow! Moreover, it |
425 | // returned sometimes a malloc()'d pointer, sometimes a pointer to a | |
426 | // static buffer and sometimes I don't even know what. | |
427 | static wxString s_home; | |
2bda0e17 | 428 | |
b568d04f | 429 | return (wxChar *)wxGetHomeDir(&s_home); |
2bda0e17 KB |
430 | } |
431 | ||
b568d04f | 432 | bool wxDirExists(const wxString& dir) |
2bda0e17 | 433 | { |
04ef50df JS |
434 | #ifdef __WXMICROWIN__ |
435 | return wxPathExist(dir); | |
436 | #elif defined(__WIN32__) | |
cf1eeea3 | 437 | DWORD attribs = GetFileAttributes(dir); |
3897b707 | 438 | return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY)); |
b568d04f VZ |
439 | #else // Win16 |
440 | #ifdef __BORLANDC__ | |
441 | struct ffblk fileInfo; | |
442 | #else | |
443 | struct find_t fileInfo; | |
444 | #endif | |
b568d04f VZ |
445 | // In Borland findfirst has a different argument |
446 | // ordering from _dos_findfirst. But _dos_findfirst | |
447 | // _should_ be ok in both MS and Borland... why not? | |
448 | #ifdef __BORLANDC__ | |
449 | return (findfirst(dir, &fileInfo, _A_SUBDIR) == 0 && | |
450 | (fileInfo.ff_attrib & _A_SUBDIR) != 0); | |
451 | #else | |
452 | return (_dos_findfirst(dir, _A_SUBDIR, &fileInfo) == 0) && | |
453 | ((fileInfo.attrib & _A_SUBDIR) != 0); | |
454 | #endif | |
455 | #endif // Win32/16 | |
456 | } | |
2bda0e17 | 457 | |
eadd7bd2 VZ |
458 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
459 | { | |
4676948b JS |
460 | #ifdef __WXWINCE__ |
461 | return FALSE; | |
462 | #else | |
eadd7bd2 VZ |
463 | if ( path.empty() ) |
464 | return FALSE; | |
465 | ||
ee88cb34 MB |
466 | // old w32api don't have ULARGE_INTEGER |
467 | #if defined(__WIN32__) && \ | |
468 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 )) | |
eadd7bd2 VZ |
469 | // GetDiskFreeSpaceEx() is not available under original Win95, check for |
470 | // it | |
9ee966ec VZ |
471 | typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR, |
472 | PULARGE_INTEGER, | |
473 | PULARGE_INTEGER, | |
474 | PULARGE_INTEGER); | |
eadd7bd2 VZ |
475 | |
476 | GetDiskFreeSpaceEx_t | |
bb0e27ee | 477 | pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress |
eadd7bd2 VZ |
478 | ( |
479 | ::GetModuleHandle(_T("kernel32.dll")), | |
480 | #if wxUSE_UNICODE | |
481 | "GetDiskFreeSpaceExW" | |
482 | #else | |
483 | "GetDiskFreeSpaceExA" | |
484 | #endif | |
485 | ); | |
486 | ||
487 | if ( pGetDiskFreeSpaceEx ) | |
488 | { | |
489 | ULARGE_INTEGER bytesFree, bytesTotal; | |
490 | ||
491 | // may pass the path as is, GetDiskFreeSpaceEx() is smart enough | |
492 | if ( !pGetDiskFreeSpaceEx(path, | |
493 | &bytesFree, | |
494 | &bytesTotal, | |
495 | NULL) ) | |
496 | { | |
497 | wxLogLastError(_T("GetDiskFreeSpaceEx")); | |
498 | ||
499 | return FALSE; | |
500 | } | |
501 | ||
221e1181 VZ |
502 | // ULARGE_INTEGER is a union of a 64 bit value and a struct containing |
503 | // two 32 bit fields which may be or may be not named - try to make it | |
504 | // compile in all cases | |
505 | #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT) | |
506 | #define UL(ul) ul.u | |
507 | #else // anon union | |
508 | #define UL(ul) ul | |
509 | #endif | |
eadd7bd2 VZ |
510 | if ( pTotal ) |
511 | { | |
221e1181 | 512 | *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart); |
eadd7bd2 VZ |
513 | } |
514 | ||
515 | if ( pFree ) | |
516 | { | |
221e1181 | 517 | *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart); |
eadd7bd2 VZ |
518 | } |
519 | } | |
520 | else | |
521 | #endif // Win32 | |
522 | { | |
bb0e27ee VZ |
523 | // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx() |
524 | // should be used instead - but if it's not available, fall back on | |
525 | // GetDiskFreeSpace() nevertheless... | |
526 | ||
eadd7bd2 VZ |
527 | DWORD lSectorsPerCluster, |
528 | lBytesPerSector, | |
529 | lNumberOfFreeClusters, | |
530 | lTotalNumberOfClusters; | |
531 | ||
532 | // FIXME: this is wrong, we should extract the root drive from path | |
533 | // instead, but this is the job for wxFileName... | |
534 | if ( !::GetDiskFreeSpace(path, | |
535 | &lSectorsPerCluster, | |
536 | &lBytesPerSector, | |
537 | &lNumberOfFreeClusters, | |
538 | &lTotalNumberOfClusters) ) | |
539 | { | |
540 | wxLogLastError(_T("GetDiskFreeSpace")); | |
541 | ||
542 | return FALSE; | |
543 | } | |
544 | ||
eadd7bd2 VZ |
545 | wxLongLong lBytesPerCluster = lSectorsPerCluster; |
546 | lBytesPerCluster *= lBytesPerSector; | |
547 | ||
548 | if ( pTotal ) | |
549 | { | |
550 | *pTotal = lBytesPerCluster; | |
551 | *pTotal *= lTotalNumberOfClusters; | |
552 | } | |
553 | ||
554 | if ( pFree ) | |
555 | { | |
556 | *pFree = lBytesPerCluster; | |
557 | *pFree *= lNumberOfFreeClusters; | |
558 | } | |
559 | } | |
560 | ||
561 | return TRUE; | |
4676948b JS |
562 | #endif |
563 | // __WXWINCE__ | |
eadd7bd2 VZ |
564 | } |
565 | ||
308978f6 VZ |
566 | // ---------------------------------------------------------------------------- |
567 | // env vars | |
568 | // ---------------------------------------------------------------------------- | |
569 | ||
570 | bool wxGetEnv(const wxString& var, wxString *value) | |
571 | { | |
4676948b JS |
572 | #ifdef __WXWINCE__ |
573 | return FALSE; | |
574 | #elif defined(__WIN16__) | |
788722ac | 575 | const wxChar* ret = wxGetenv(var); |
e622dd68 VZ |
576 | if ( !ret ) |
577 | return FALSE; | |
578 | ||
579 | if ( value ) | |
788722ac JS |
580 | { |
581 | *value = ret; | |
788722ac | 582 | } |
e622dd68 VZ |
583 | |
584 | return TRUE; | |
585 | #else // Win32 | |
308978f6 VZ |
586 | // first get the size of the buffer |
587 | DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0); | |
588 | if ( !dwRet ) | |
589 | { | |
590 | // this means that there is no such variable | |
591 | return FALSE; | |
592 | } | |
593 | ||
594 | if ( value ) | |
595 | { | |
596 | (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet); | |
597 | value->UngetWriteBuf(); | |
598 | } | |
599 | ||
600 | return TRUE; | |
e622dd68 | 601 | #endif // Win16/32 |
308978f6 VZ |
602 | } |
603 | ||
1fb45475 VZ |
604 | bool wxSetEnv(const wxString& var, const wxChar *value) |
605 | { | |
606 | // some compilers have putenv() or _putenv() or _wputenv() but it's better | |
607 | // to always use Win32 function directly instead of dealing with them | |
4676948b | 608 | #if defined(__WIN32__) && !defined(__WXWINCE__) |
1fb45475 VZ |
609 | if ( !::SetEnvironmentVariable(var, value) ) |
610 | { | |
611 | wxLogLastError(_T("SetEnvironmentVariable")); | |
612 | ||
613 | return FALSE; | |
614 | } | |
615 | ||
616 | return TRUE; | |
617 | #else // no way to set env vars | |
618 | return FALSE; | |
619 | #endif | |
620 | } | |
621 | ||
b568d04f VZ |
622 | // ---------------------------------------------------------------------------- |
623 | // process management | |
624 | // ---------------------------------------------------------------------------- | |
625 | ||
50567b69 VZ |
626 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() |
627 | struct wxFindByPidParams | |
b568d04f | 628 | { |
50567b69 VZ |
629 | wxFindByPidParams() { hwnd = 0; pid = 0; } |
630 | ||
631 | // the HWND used to return the result | |
632 | HWND hwnd; | |
633 | ||
634 | // the PID we're looking from | |
635 | DWORD pid; | |
22f3361e VZ |
636 | |
637 | DECLARE_NO_COPY_CLASS(wxFindByPidParams) | |
50567b69 VZ |
638 | }; |
639 | ||
640 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
641 | // level) window belonging to the given process | |
642 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
643 | { | |
644 | DWORD pid; | |
645 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
646 | ||
647 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
648 | if ( pid == params->pid ) | |
649 | { | |
650 | // remember the window we found | |
651 | params->hwnd = hwnd; | |
652 | ||
653 | // return FALSE to stop the enumeration | |
654 | return FALSE; | |
655 | } | |
656 | ||
657 | // continue enumeration | |
658 | return TRUE; | |
659 | } | |
e949bf13 | 660 | |
50567b69 VZ |
661 | int wxKill(long pid, wxSignal sig, wxKillError *krc) |
662 | { | |
50567b69 VZ |
663 | // get the process handle to operate on |
664 | HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | | |
665 | PROCESS_TERMINATE | | |
666 | PROCESS_QUERY_INFORMATION, | |
667 | FALSE, // not inheritable | |
668 | (DWORD)pid); | |
669 | if ( hProcess == NULL ) | |
e949bf13 | 670 | { |
50567b69 | 671 | if ( krc ) |
e949bf13 | 672 | { |
50567b69 | 673 | if ( ::GetLastError() == ERROR_ACCESS_DENIED ) |
e949bf13 | 674 | { |
50567b69 VZ |
675 | *krc = wxKILL_ACCESS_DENIED; |
676 | } | |
677 | else | |
678 | { | |
679 | *krc = wxKILL_NO_PROCESS; | |
680 | } | |
681 | } | |
682 | ||
683 | return -1; | |
684 | } | |
685 | ||
686 | bool ok = TRUE; | |
687 | switch ( sig ) | |
688 | { | |
689 | case wxSIGKILL: | |
690 | // kill the process forcefully returning -1 as error code | |
691 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
692 | { | |
693 | wxLogSysError(_("Failed to kill process %d"), pid); | |
694 | ||
695 | if ( krc ) | |
e949bf13 | 696 | { |
50567b69 VZ |
697 | // this is not supposed to happen if we could open the |
698 | // process | |
699 | *krc = wxKILL_ERROR; | |
e949bf13 | 700 | } |
50567b69 VZ |
701 | |
702 | ok = FALSE; | |
e949bf13 | 703 | } |
50567b69 VZ |
704 | break; |
705 | ||
706 | case wxSIGNONE: | |
707 | // do nothing, we just want to test for process existence | |
708 | break; | |
709 | ||
710 | default: | |
711 | // any other signal means "terminate" | |
712 | { | |
713 | wxFindByPidParams params; | |
714 | params.pid = (DWORD)pid; | |
715 | ||
716 | // EnumWindows() has nice semantics: it returns 0 if it found | |
717 | // something or if an error occured and non zero if it | |
718 | // enumerated all the window | |
719 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
720 | { | |
721 | // did we find any window? | |
722 | if ( params.hwnd ) | |
723 | { | |
724 | // tell the app to close | |
725 | // | |
726 | // NB: this is the harshest way, the app won't have | |
727 | // opportunity to save any files, for example, but | |
728 | // this is probably what we want here. If not we | |
729 | // can also use SendMesageTimeout(WM_CLOSE) | |
730 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
731 | { | |
732 | wxLogLastError(_T("PostMessage(WM_QUIT)")); | |
733 | } | |
734 | } | |
735 | else // it was an error then | |
736 | { | |
737 | wxLogLastError(_T("EnumWindows")); | |
738 | ||
739 | ok = FALSE; | |
740 | } | |
741 | } | |
742 | else // no windows for this PID | |
743 | { | |
744 | if ( krc ) | |
745 | { | |
746 | *krc = wxKILL_ERROR; | |
747 | } | |
748 | ||
749 | ok = FALSE; | |
750 | } | |
751 | } | |
752 | } | |
753 | ||
754 | // the return code | |
755 | DWORD rc; | |
756 | ||
757 | if ( ok ) | |
758 | { | |
759 | // as we wait for a short time, we can use just WaitForSingleObject() | |
760 | // and not MsgWaitForMultipleObjects() | |
761 | switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) ) | |
762 | { | |
763 | case WAIT_OBJECT_0: | |
764 | // process terminated | |
765 | if ( !::GetExitCodeProcess(hProcess, &rc) ) | |
766 | { | |
767 | wxLogLastError(_T("GetExitCodeProcess")); | |
768 | } | |
769 | break; | |
770 | ||
771 | default: | |
772 | wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") ); | |
773 | // fall through | |
774 | ||
775 | case WAIT_FAILED: | |
776 | wxLogLastError(_T("WaitForSingleObject")); | |
777 | // fall through | |
778 | ||
779 | case WAIT_TIMEOUT: | |
780 | if ( krc ) | |
781 | { | |
782 | *krc = wxKILL_ERROR; | |
783 | } | |
784 | ||
785 | rc = STILL_ACTIVE; | |
786 | break; | |
e949bf13 JS |
787 | } |
788 | } | |
50567b69 VZ |
789 | else // !ok |
790 | { | |
791 | // just to suppress the warnings about uninitialized variable | |
792 | rc = 0; | |
793 | } | |
e949bf13 | 794 | |
50567b69 | 795 | ::CloseHandle(hProcess); |
b568d04f | 796 | |
50567b69 VZ |
797 | // the return code is the same as from Unix kill(): 0 if killed |
798 | // successfully or -1 on error | |
b1a28eef VZ |
799 | // |
800 | // be careful to interpret rc correctly: for wxSIGNONE we return success if | |
801 | // the process exists, for all the other sig values -- if it doesn't | |
802 | if ( ok && | |
803 | ((sig == wxSIGNONE) == (rc == STILL_ACTIVE)) ) | |
e949bf13 | 804 | { |
b1a28eef | 805 | if ( krc ) |
50567b69 | 806 | { |
b1a28eef | 807 | *krc = wxKILL_OK; |
50567b69 | 808 | } |
b1a28eef VZ |
809 | |
810 | return 0; | |
e949bf13 | 811 | } |
50567b69 VZ |
812 | |
813 | // error | |
814 | return -1; | |
2bda0e17 KB |
815 | } |
816 | ||
b568d04f VZ |
817 | // Execute a program in an Interactive Shell |
818 | bool wxShell(const wxString& command) | |
2bda0e17 | 819 | { |
4676948b JS |
820 | #ifdef __WXWINCE__ |
821 | return FALSE; | |
822 | #else | |
b568d04f VZ |
823 | wxChar *shell = wxGetenv(wxT("COMSPEC")); |
824 | if ( !shell ) | |
ba14d986 | 825 | shell = (wxChar*) wxT("\\COMMAND.COM"); |
b568d04f VZ |
826 | |
827 | wxString cmd; | |
828 | if ( !command ) | |
829 | { | |
830 | // just the shell | |
831 | cmd = shell; | |
832 | } | |
833 | else | |
834 | { | |
835 | // pass the command to execute to the command processor | |
836 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
837 | } | |
838 | ||
011a524e | 839 | return wxExecute(cmd, wxEXEC_SYNC) == 0; |
4676948b | 840 | #endif |
2bda0e17 KB |
841 | } |
842 | ||
d8c65cf4 | 843 | // Shutdown or reboot the PC |
f6ba47d9 VZ |
844 | bool wxShutdown(wxShutdownFlags wFlags) |
845 | { | |
4676948b JS |
846 | #ifdef __WXWINCE__ |
847 | return FALSE; | |
848 | #elif defined(__WIN32__) | |
f6ba47d9 VZ |
849 | bool bOK = TRUE; |
850 | ||
851 | if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K | |
852 | { | |
d8c65cf4 RD |
853 | // Get a token for this process. |
854 | HANDLE hToken; | |
f6ba47d9 VZ |
855 | bOK = ::OpenProcessToken(GetCurrentProcess(), |
856 | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, | |
857 | &hToken) != 0; | |
858 | if ( bOK ) | |
859 | { | |
d8c65cf4 | 860 | TOKEN_PRIVILEGES tkp; |
f6ba47d9 | 861 | |
d8c65cf4 | 862 | // Get the LUID for the shutdown privilege. |
f6ba47d9 | 863 | ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, |
d8c65cf4 | 864 | &tkp.Privileges[0].Luid); |
f6ba47d9 | 865 | |
d8c65cf4 RD |
866 | tkp.PrivilegeCount = 1; // one privilege to set |
867 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
f6ba47d9 | 868 | |
d8c65cf4 | 869 | // Get the shutdown privilege for this process. |
f6ba47d9 | 870 | ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, |
d8c65cf4 | 871 | (PTOKEN_PRIVILEGES)NULL, 0); |
f6ba47d9 | 872 | |
d8c65cf4 | 873 | // Cannot test the return value of AdjustTokenPrivileges. |
f6ba47d9 VZ |
874 | bOK = ::GetLastError() == ERROR_SUCCESS; |
875 | } | |
876 | } | |
877 | ||
878 | if ( bOK ) | |
879 | { | |
880 | UINT flags = EWX_SHUTDOWN | EWX_FORCE; | |
881 | switch ( wFlags ) | |
882 | { | |
883 | case wxSHUTDOWN_POWEROFF: | |
884 | flags |= EWX_POWEROFF; | |
885 | break; | |
886 | ||
887 | case wxSHUTDOWN_REBOOT: | |
888 | flags |= EWX_REBOOT; | |
889 | break; | |
890 | ||
891 | default: | |
892 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
893 | return FALSE; | |
894 | } | |
895 | ||
d8c65cf4 | 896 | bOK = ::ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_REBOOT, 0) != 0; |
f6ba47d9 VZ |
897 | } |
898 | ||
899 | return bOK; | |
900 | #else // Win16 | |
901 | return FALSE; | |
902 | #endif // Win32/16 | |
903 | } | |
904 | ||
b568d04f VZ |
905 | // ---------------------------------------------------------------------------- |
906 | // misc | |
907 | // ---------------------------------------------------------------------------- | |
908 | ||
909 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
910 | long wxGetFreeMemory() | |
2bda0e17 | 911 | { |
b39dbf34 | 912 | #if defined(__WIN32__) && !defined(__BORLANDC__) |
b568d04f VZ |
913 | MEMORYSTATUS memStatus; |
914 | memStatus.dwLength = sizeof(MEMORYSTATUS); | |
915 | GlobalMemoryStatus(&memStatus); | |
916 | return memStatus.dwAvailPhys; | |
917 | #else | |
918 | return (long)GetFreeSpace(0); | |
919 | #endif | |
2bda0e17 KB |
920 | } |
921 | ||
c1cb4153 VZ |
922 | unsigned long wxGetProcessId() |
923 | { | |
924 | #ifdef __WIN32__ | |
925 | return ::GetCurrentProcessId(); | |
926 | #else | |
927 | return 0; | |
928 | #endif | |
929 | } | |
930 | ||
2bda0e17 | 931 | // Emit a beeeeeep |
634903fd | 932 | void wxBell() |
2bda0e17 | 933 | { |
b568d04f | 934 | ::MessageBeep((UINT)-1); // default sound |
2bda0e17 KB |
935 | } |
936 | ||
bdc72a22 | 937 | wxString wxGetOsDescription() |
2bda0e17 | 938 | { |
bdc72a22 VZ |
939 | #ifdef __WIN32__ |
940 | wxString str; | |
941 | ||
942 | OSVERSIONINFO info; | |
943 | wxZeroMemory(info); | |
944 | ||
945 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
946 | if ( ::GetVersionEx(&info) ) | |
947 | { | |
948 | switch ( info.dwPlatformId ) | |
949 | { | |
950 | case VER_PLATFORM_WIN32s: | |
951 | str = _("Win32s on Windows 3.1"); | |
952 | break; | |
953 | ||
954 | case VER_PLATFORM_WIN32_WINDOWS: | |
955 | str.Printf(_("Windows 9%c"), | |
a46a73a6 | 956 | info.dwMinorVersion == 0 ? _T('5') : _T('8')); |
bdc72a22 VZ |
957 | if ( !wxIsEmpty(info.szCSDVersion) ) |
958 | { | |
959 | str << _T(" (") << info.szCSDVersion << _T(')'); | |
960 | } | |
961 | break; | |
962 | ||
963 | case VER_PLATFORM_WIN32_NT: | |
964 | str.Printf(_T("Windows NT %lu.%lu (build %lu"), | |
965 | info.dwMajorVersion, | |
966 | info.dwMinorVersion, | |
967 | info.dwBuildNumber); | |
968 | if ( !wxIsEmpty(info.szCSDVersion) ) | |
969 | { | |
970 | str << _T(", ") << info.szCSDVersion; | |
971 | } | |
972 | str << _T(')'); | |
973 | break; | |
974 | } | |
975 | } | |
976 | else | |
977 | { | |
978 | wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen | |
979 | } | |
980 | ||
981 | return str; | |
982 | #else // Win16 | |
983 | return _("Windows 3.1"); | |
984 | #endif // Win32/16 | |
985 | } | |
6f65e337 | 986 | |
2739d4f0 | 987 | int wxAppTraits::GetOSVersion(int *verMaj, int *verMin) |
bdc72a22 | 988 | { |
2739d4f0 VZ |
989 | // cache the version info, it's not going to change |
990 | // | |
991 | // NB: this is MT-safe, we may use these static vars from different threads | |
992 | // but as they always have the same value it doesn't matter | |
993 | static int s_ver = -1, | |
994 | s_major = -1, | |
995 | s_minor = -1; | |
96c21216 | 996 | |
2739d4f0 | 997 | if ( s_ver == -1 ) |
bdc72a22 | 998 | { |
6af638ed VS |
999 | OSVERSIONINFO info; |
1000 | wxZeroMemory(info); | |
ec5d7799 | 1001 | |
2739d4f0 | 1002 | s_ver = wxWINDOWS; |
6af638ed VS |
1003 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
1004 | if ( ::GetVersionEx(&info) ) | |
bdc72a22 | 1005 | { |
2739d4f0 VZ |
1006 | s_major = info.dwMajorVersion; |
1007 | s_minor = info.dwMinorVersion; | |
bdc72a22 | 1008 | |
6af638ed VS |
1009 | switch ( info.dwPlatformId ) |
1010 | { | |
1011 | case VER_PLATFORM_WIN32s: | |
2739d4f0 | 1012 | s_ver = wxWIN32S; |
6af638ed | 1013 | break; |
bdc72a22 | 1014 | |
6af638ed | 1015 | case VER_PLATFORM_WIN32_WINDOWS: |
2739d4f0 | 1016 | s_ver = wxWIN95; |
6af638ed VS |
1017 | break; |
1018 | ||
1019 | case VER_PLATFORM_WIN32_NT: | |
2739d4f0 | 1020 | s_ver = wxWINDOWS_NT; |
6af638ed | 1021 | break; |
471085e4 | 1022 | #ifdef __WXWINCE__ |
f07dc2e2 JS |
1023 | case VER_PLATFORM_WIN32_CE: |
1024 | s_ver = wxWINDOWS_CE; | |
1025 | break; | |
471085e4 | 1026 | #endif |
6af638ed | 1027 | } |
bdc72a22 VZ |
1028 | } |
1029 | } | |
1030 | ||
2739d4f0 VZ |
1031 | if ( verMaj ) |
1032 | *verMaj = s_major; | |
1033 | if ( verMin ) | |
1034 | *verMin = s_minor; | |
bdc72a22 | 1035 | |
2739d4f0 | 1036 | return s_ver; |
2bda0e17 KB |
1037 | } |
1038 | ||
b568d04f VZ |
1039 | // ---------------------------------------------------------------------------- |
1040 | // sleep functions | |
1041 | // ---------------------------------------------------------------------------- | |
1042 | ||
b568d04f VZ |
1043 | void wxUsleep(unsigned long milliseconds) |
1044 | { | |
1045 | ::Sleep(milliseconds); | |
1046 | } | |
1047 | ||
1048 | void wxSleep(int nSecs) | |
1049 | { | |
1050 | wxUsleep(1000*nSecs); | |
1051 | } | |
1052 | ||
b568d04f | 1053 | // ---------------------------------------------------------------------------- |
e2478fde | 1054 | // font encoding <-> Win32 codepage conversion functions |
b568d04f VZ |
1055 | // ---------------------------------------------------------------------------- |
1056 | ||
bddd7a8d | 1057 | extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding) |
b568d04f | 1058 | { |
e2478fde | 1059 | switch ( encoding ) |
b568d04f | 1060 | { |
e2478fde VZ |
1061 | // although this function is supposed to return an exact match, do do |
1062 | // some mappings here for the most common case of "standard" encoding | |
1063 | case wxFONTENCODING_SYSTEM: | |
1064 | return DEFAULT_CHARSET; | |
2bda0e17 | 1065 | |
e2478fde VZ |
1066 | case wxFONTENCODING_ISO8859_1: |
1067 | case wxFONTENCODING_ISO8859_15: | |
1068 | case wxFONTENCODING_CP1252: | |
1069 | return ANSI_CHARSET; | |
2bda0e17 | 1070 | |
e2478fde VZ |
1071 | #if !defined(__WXMICROWIN__) |
1072 | // The following four fonts are multi-byte charsets | |
1073 | case wxFONTENCODING_CP932: | |
1074 | return SHIFTJIS_CHARSET; | |
2bda0e17 | 1075 | |
e2478fde VZ |
1076 | case wxFONTENCODING_CP936: |
1077 | return GB2312_CHARSET; | |
2bda0e17 | 1078 | |
e2478fde VZ |
1079 | case wxFONTENCODING_CP949: |
1080 | return HANGUL_CHARSET; | |
634903fd | 1081 | |
e2478fde VZ |
1082 | case wxFONTENCODING_CP950: |
1083 | return CHINESEBIG5_CHARSET; | |
2bda0e17 | 1084 | |
e2478fde VZ |
1085 | // The rest are single byte encodings |
1086 | case wxFONTENCODING_CP1250: | |
1087 | return EASTEUROPE_CHARSET; | |
bfbd6dc1 | 1088 | |
e2478fde VZ |
1089 | case wxFONTENCODING_CP1251: |
1090 | return RUSSIAN_CHARSET; | |
2bda0e17 | 1091 | |
e2478fde VZ |
1092 | case wxFONTENCODING_CP1253: |
1093 | return GREEK_CHARSET; | |
b07135ba | 1094 | |
e2478fde VZ |
1095 | case wxFONTENCODING_CP1254: |
1096 | return TURKISH_CHARSET; | |
2bda0e17 | 1097 | |
e2478fde VZ |
1098 | case wxFONTENCODING_CP1255: |
1099 | return HEBREW_CHARSET; | |
2bda0e17 | 1100 | |
e2478fde VZ |
1101 | case wxFONTENCODING_CP1256: |
1102 | return ARABIC_CHARSET; | |
b568d04f | 1103 | |
e2478fde VZ |
1104 | case wxFONTENCODING_CP1257: |
1105 | return BALTIC_CHARSET; | |
634903fd | 1106 | |
e2478fde VZ |
1107 | case wxFONTENCODING_CP874: |
1108 | return THAI_CHARSET; | |
5acc0d5f | 1109 | #endif // !__WXMICROWIN__ |
cc2b7472 | 1110 | |
e2478fde VZ |
1111 | case wxFONTENCODING_CP437: |
1112 | return OEM_CHARSET; | |
ea28b885 VZ |
1113 | |
1114 | default: | |
1115 | // no way to translate this encoding into a Windows charset | |
69557827 | 1116 | return -1; |
e2478fde | 1117 | } |
373658eb | 1118 | } |
7f555861 | 1119 | |
e2478fde VZ |
1120 | // we have 2 versions of wxCharsetToCodepage(): the old one which directly |
1121 | // looks up the vlaues in the registry and the new one which is more | |
1122 | // politically correct and has more chances to work on other Windows versions | |
1123 | // as well but the old version is still needed for !wxUSE_FONTMAP case | |
1124 | #if wxUSE_FONTMAP | |
c030b70f | 1125 | |
373658eb | 1126 | #include "wx/fontmap.h" |
c030b70f | 1127 | |
bddd7a8d | 1128 | extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding) |
c030b70f | 1129 | { |
373658eb | 1130 | // translate encoding into the Windows CHARSET |
e2478fde VZ |
1131 | long charset = wxEncodingToCharset(encoding); |
1132 | if ( charset == -1 ) | |
373658eb VZ |
1133 | return -1; |
1134 | ||
1135 | // translate CHARSET to code page | |
1136 | CHARSETINFO csetInfo; | |
e2478fde | 1137 | if ( !::TranslateCharsetInfo((DWORD *)(DWORD)charset, |
373658eb VZ |
1138 | &csetInfo, |
1139 | TCI_SRCCHARSET) ) | |
1140 | { | |
1141 | wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)")); | |
1142 | ||
1143 | return -1; | |
1144 | } | |
1145 | ||
1146 | return csetInfo.ciACP; | |
c030b70f | 1147 | } |
373658eb | 1148 | |
373658eb | 1149 | extern long wxCharsetToCodepage(const wxChar *name) |
c030b70f | 1150 | { |
373658eb VZ |
1151 | // first get the font encoding for this charset |
1152 | if ( !name ) | |
1153 | return -1; | |
1154 | ||
142b3bc2 | 1155 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE); |
373658eb VZ |
1156 | if ( enc == wxFONTENCODING_SYSTEM ) |
1157 | return -1; | |
1158 | ||
1159 | // the use the helper function | |
1160 | return wxEncodingToCodepage(enc); | |
c030b70f | 1161 | } |
c030b70f | 1162 | |
e2478fde | 1163 | #else // !wxUSE_FONTMAP |
373658eb VZ |
1164 | |
1165 | #include "wx/msw/registry.h" | |
1166 | ||
1167 | // this should work if Internet Exploiter is installed | |
1168 | extern long wxCharsetToCodepage(const wxChar *name) | |
04ef50df | 1169 | { |
373658eb VZ |
1170 | if (!name) |
1171 | return GetACP(); | |
1172 | ||
e2478fde | 1173 | long CP = -1; |
373658eb | 1174 | |
e2478fde | 1175 | wxString path(wxT("MIME\\Database\\Charset\\")); |
373658eb | 1176 | wxString cn(name); |
373658eb | 1177 | |
e2478fde VZ |
1178 | // follow the alias loop |
1179 | for ( ;; ) | |
1180 | { | |
1181 | wxRegKey key(wxRegKey::HKCR, path + cn); | |
1182 | ||
1183 | if (!key.Exists()) | |
1184 | break; | |
373658eb VZ |
1185 | |
1186 | // two cases: either there's an AliasForCharset string, | |
1187 | // or there are Codepage and InternetEncoding dwords. | |
1188 | // The InternetEncoding gives us the actual encoding, | |
1189 | // the Codepage just says which Windows character set to | |
1190 | // use when displaying the data. | |
1191 | if (key.HasValue(wxT("InternetEncoding")) && | |
e2478fde VZ |
1192 | key.QueryValue(wxT("InternetEncoding"), &CP)) |
1193 | break; | |
373658eb VZ |
1194 | |
1195 | // no encoding, see if it's an alias | |
1196 | if (!key.HasValue(wxT("AliasForCharset")) || | |
e2478fde VZ |
1197 | !key.QueryValue(wxT("AliasForCharset"), cn)) |
1198 | break; | |
1199 | } | |
373658eb VZ |
1200 | |
1201 | return CP; | |
04ef50df | 1202 | } |
373658eb | 1203 | |
e2478fde | 1204 | #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP |
c030b70f | 1205 |