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