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