]>
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 | { |
0a144271 | 243 | #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__) |
b568d04f VZ |
244 | extern HANDLE g_hPenWin; // PenWindows Running? |
245 | if (g_hPenWin) | |
246 | { | |
247 | // PenWindows Does have a user concept! | |
248 | // Get the current owner of the recognizer | |
249 | GetPrivateProfileString("Current", "User", default_name, wxBuffer, maxSize - 1, "PENWIN.INI"); | |
250 | strncpy(buf, wxBuffer, maxSize - 1); | |
251 | } | |
252 | else | |
2bda0e17 | 253 | #endif |
b568d04f | 254 | { |
a4f96412 | 255 | #ifdef USE_NET_API |
b568d04f VZ |
256 | CHAR szUserName[256]; |
257 | if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) ) | |
258 | return FALSE; | |
a4f96412 | 259 | |
b568d04f VZ |
260 | // TODO how to get the domain name? |
261 | CHAR *szDomain = ""; | |
a4f96412 | 262 | |
b568d04f VZ |
263 | // the code is based on the MSDN example (also see KB article Q119670) |
264 | WCHAR wszUserName[256]; // Unicode user name | |
265 | WCHAR wszDomain[256]; | |
266 | LPBYTE ComputerName; | |
a4f96412 | 267 | |
b568d04f | 268 | USER_INFO_2 *ui2; // User structure |
a4f96412 | 269 | |
b568d04f VZ |
270 | // Convert ANSI user name and domain to Unicode |
271 | MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1, | |
272 | wszUserName, WXSIZEOF(wszUserName) ); | |
273 | MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1, | |
274 | wszDomain, WXSIZEOF(wszDomain) ); | |
a4f96412 | 275 | |
b568d04f VZ |
276 | // Get the computer name of a DC for the domain. |
277 | if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success ) | |
278 | { | |
279 | wxLogError(wxT("Can not find domain controller")); | |
a4f96412 | 280 | |
b568d04f VZ |
281 | goto error; |
282 | } | |
a4f96412 | 283 | |
b568d04f VZ |
284 | // Look up the user on the DC |
285 | NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName, | |
286 | (LPWSTR)&wszUserName, | |
287 | 2, // level - we want USER_INFO_2 | |
288 | (LPBYTE *) &ui2 ); | |
289 | switch ( status ) | |
290 | { | |
291 | case NERR_Success: | |
292 | // ok | |
293 | break; | |
a4f96412 | 294 | |
b568d04f VZ |
295 | case NERR_InvalidComputer: |
296 | wxLogError(wxT("Invalid domain controller name.")); | |
a4f96412 | 297 | |
b568d04f | 298 | goto error; |
a4f96412 | 299 | |
b568d04f VZ |
300 | case NERR_UserNotFound: |
301 | wxLogError(wxT("Invalid user name '%s'."), szUserName); | |
a4f96412 | 302 | |
b568d04f | 303 | goto error; |
a4f96412 | 304 | |
b568d04f VZ |
305 | default: |
306 | wxLogSysError(wxT("Can't get information about user")); | |
a4f96412 | 307 | |
b568d04f VZ |
308 | goto error; |
309 | } | |
a4f96412 | 310 | |
b568d04f VZ |
311 | // Convert the Unicode full name to ANSI |
312 | WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1, | |
313 | buf, maxSize, NULL, NULL ); | |
a4f96412 | 314 | |
b568d04f | 315 | return TRUE; |
a4f96412 VZ |
316 | |
317 | error: | |
b568d04f | 318 | wxLogError(wxT("Couldn't look up full user name.")); |
a4f96412 | 319 | |
b568d04f | 320 | return FALSE; |
a4f96412 | 321 | #else // !USE_NET_API |
b568d04f VZ |
322 | // Could use NIS, MS-Mail or other site specific programs |
323 | // Use wxWindows configuration data | |
324 | bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxT(""), buf, maxSize - 1) != 0; | |
325 | if ( !ok ) | |
326 | { | |
327 | ok = wxGetUserId(buf, maxSize); | |
328 | } | |
0a144271 | 329 | |
b568d04f VZ |
330 | if ( !ok ) |
331 | { | |
332 | wxStrncpy(buf, wxT("Unknown User"), maxSize); | |
333 | } | |
a4f96412 | 334 | #endif // Win32/16 |
b568d04f | 335 | } |
0a144271 | 336 | |
b568d04f | 337 | return TRUE; |
2bda0e17 KB |
338 | } |
339 | ||
b568d04f | 340 | const wxChar* wxGetHomeDir(wxString *pstr) |
2bda0e17 | 341 | { |
b568d04f | 342 | wxString& strDir = *pstr; |
2bda0e17 | 343 | |
b39dbf34 | 344 | #if defined(__UNIX__) |
b568d04f VZ |
345 | const wxChar *szHome = wxGetenv("HOME"); |
346 | if ( szHome == NULL ) { | |
347 | // we're homeless... | |
348 | wxLogWarning(_("can't find user's HOME, using current directory.")); | |
349 | strDir = wxT("."); | |
350 | } | |
351 | else | |
352 | strDir = szHome; | |
2bda0e17 | 353 | |
b568d04f VZ |
354 | // add a trailing slash if needed |
355 | if ( strDir.Last() != wxT('/') ) | |
356 | strDir << wxT('/'); | |
3ffbc733 | 357 | |
e02e8816 | 358 | #ifdef __CYGWIN__ |
3ffbc733 VZ |
359 | // Cygwin returns unix type path but that does not work well |
360 | static wxChar windowsPath[MAX_PATH]; | |
361 | cygwin_conv_to_full_win32_path(strDir, windowsPath); | |
362 | strDir = windowsPath; | |
363 | #endif | |
b568d04f VZ |
364 | #else // Windows |
365 | #ifdef __WIN32__ | |
96c21216 VZ |
366 | strDir.clear(); |
367 | ||
368 | // If we have a valid HOME directory, as is used on many machines that | |
369 | // have unix utilities on them, we should use that. | |
370 | const wxChar *szHome = wxGetenv(wxT("HOME")); | |
371 | ||
b568d04f | 372 | if ( szHome != NULL ) |
96c21216 VZ |
373 | { |
374 | strDir = szHome; | |
b568d04f | 375 | } |
96c21216 VZ |
376 | else // no HOME, try HOMEDRIVE/PATH |
377 | { | |
378 | szHome = wxGetenv(wxT("HOMEDRIVE")); | |
379 | if ( szHome != NULL ) | |
380 | strDir << szHome; | |
381 | szHome = wxGetenv(wxT("HOMEPATH")); | |
382 | ||
383 | if ( szHome != NULL ) | |
384 | { | |
385 | strDir << szHome; | |
386 | ||
387 | // the idea is that under NT these variables have default values | |
388 | // of "%systemdrive%:" and "\\". As we don't want to create our | |
389 | // config files in the root directory of the system drive, we will | |
390 | // create it in our program's dir. However, if the user took care | |
391 | // to set HOMEPATH to something other than "\\", we suppose that he | |
392 | // knows what he is doing and use the supplied value. | |
393 | if ( wxStrcmp(szHome, wxT("\\")) == 0 ) | |
394 | strDir.clear(); | |
395 | } | |
396 | } | |
397 | ||
398 | if ( strDir.empty() ) | |
399 | { | |
400 | // If we have a valid USERPROFILE directory, as is the case in | |
401 | // Windows NT, 2000 and XP, we should use that as our home directory. | |
402 | szHome = wxGetenv(wxT("USERPROFILE")); | |
2bda0e17 | 403 | |
96c21216 VZ |
404 | if ( szHome != NULL ) |
405 | strDir = szHome; | |
406 | } | |
407 | ||
408 | if ( !strDir.empty() ) | |
409 | { | |
410 | return strDir.c_str(); | |
411 | } | |
412 | //else: fall back to the prograrm directory | |
b568d04f | 413 | #else // Win16 |
96c21216 | 414 | // Win16 has no idea about home, so use the executable directory instead |
b568d04f | 415 | #endif // WIN16/32 |
2bda0e17 | 416 | |
b568d04f VZ |
417 | // 260 was taken from windef.h |
418 | #ifndef MAX_PATH | |
419 | #define MAX_PATH 260 | |
420 | #endif | |
2bda0e17 | 421 | |
b568d04f VZ |
422 | wxString strPath; |
423 | ::GetModuleFileName(::GetModuleHandle(NULL), | |
424 | strPath.GetWriteBuf(MAX_PATH), MAX_PATH); | |
425 | strPath.UngetWriteBuf(); | |
2bda0e17 | 426 | |
b568d04f VZ |
427 | // extract the dir name |
428 | wxSplitPath(strPath, &strDir, NULL, NULL); | |
58a33cb4 | 429 | |
b568d04f VZ |
430 | #endif // UNIX/Win |
431 | ||
432 | return strDir.c_str(); | |
afb74891 VZ |
433 | } |
434 | ||
33ac7e6f | 435 | wxChar *wxGetUserHome(const wxString& WXUNUSED(user)) |
2bda0e17 | 436 | { |
b568d04f VZ |
437 | // VZ: the old code here never worked for user != "" anyhow! Moreover, it |
438 | // returned sometimes a malloc()'d pointer, sometimes a pointer to a | |
439 | // static buffer and sometimes I don't even know what. | |
440 | static wxString s_home; | |
2bda0e17 | 441 | |
b568d04f | 442 | return (wxChar *)wxGetHomeDir(&s_home); |
2bda0e17 KB |
443 | } |
444 | ||
b568d04f | 445 | bool wxDirExists(const wxString& dir) |
2bda0e17 | 446 | { |
04ef50df JS |
447 | #ifdef __WXMICROWIN__ |
448 | return wxPathExist(dir); | |
449 | #elif defined(__WIN32__) | |
cf1eeea3 | 450 | DWORD attribs = GetFileAttributes(dir); |
3897b707 | 451 | return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY)); |
b568d04f VZ |
452 | #else // Win16 |
453 | #ifdef __BORLANDC__ | |
454 | struct ffblk fileInfo; | |
455 | #else | |
456 | struct find_t fileInfo; | |
457 | #endif | |
b568d04f VZ |
458 | // In Borland findfirst has a different argument |
459 | // ordering from _dos_findfirst. But _dos_findfirst | |
460 | // _should_ be ok in both MS and Borland... why not? | |
461 | #ifdef __BORLANDC__ | |
462 | return (findfirst(dir, &fileInfo, _A_SUBDIR) == 0 && | |
463 | (fileInfo.ff_attrib & _A_SUBDIR) != 0); | |
464 | #else | |
465 | return (_dos_findfirst(dir, _A_SUBDIR, &fileInfo) == 0) && | |
466 | ((fileInfo.attrib & _A_SUBDIR) != 0); | |
467 | #endif | |
468 | #endif // Win32/16 | |
469 | } | |
2bda0e17 | 470 | |
eadd7bd2 VZ |
471 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
472 | { | |
473 | if ( path.empty() ) | |
474 | return FALSE; | |
475 | ||
ee88cb34 MB |
476 | // old w32api don't have ULARGE_INTEGER |
477 | #if defined(__WIN32__) && \ | |
478 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 )) | |
eadd7bd2 VZ |
479 | // GetDiskFreeSpaceEx() is not available under original Win95, check for |
480 | // it | |
9ee966ec VZ |
481 | typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR, |
482 | PULARGE_INTEGER, | |
483 | PULARGE_INTEGER, | |
484 | PULARGE_INTEGER); | |
eadd7bd2 VZ |
485 | |
486 | GetDiskFreeSpaceEx_t | |
bb0e27ee | 487 | pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress |
eadd7bd2 VZ |
488 | ( |
489 | ::GetModuleHandle(_T("kernel32.dll")), | |
490 | #if wxUSE_UNICODE | |
491 | "GetDiskFreeSpaceExW" | |
492 | #else | |
493 | "GetDiskFreeSpaceExA" | |
494 | #endif | |
495 | ); | |
496 | ||
497 | if ( pGetDiskFreeSpaceEx ) | |
498 | { | |
499 | ULARGE_INTEGER bytesFree, bytesTotal; | |
500 | ||
501 | // may pass the path as is, GetDiskFreeSpaceEx() is smart enough | |
502 | if ( !pGetDiskFreeSpaceEx(path, | |
503 | &bytesFree, | |
504 | &bytesTotal, | |
505 | NULL) ) | |
506 | { | |
507 | wxLogLastError(_T("GetDiskFreeSpaceEx")); | |
508 | ||
509 | return FALSE; | |
510 | } | |
511 | ||
221e1181 VZ |
512 | // ULARGE_INTEGER is a union of a 64 bit value and a struct containing |
513 | // two 32 bit fields which may be or may be not named - try to make it | |
514 | // compile in all cases | |
515 | #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT) | |
516 | #define UL(ul) ul.u | |
517 | #else // anon union | |
518 | #define UL(ul) ul | |
519 | #endif | |
eadd7bd2 VZ |
520 | if ( pTotal ) |
521 | { | |
221e1181 | 522 | *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart); |
eadd7bd2 VZ |
523 | } |
524 | ||
525 | if ( pFree ) | |
526 | { | |
221e1181 | 527 | *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart); |
eadd7bd2 VZ |
528 | } |
529 | } | |
530 | else | |
531 | #endif // Win32 | |
532 | { | |
bb0e27ee VZ |
533 | // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx() |
534 | // should be used instead - but if it's not available, fall back on | |
535 | // GetDiskFreeSpace() nevertheless... | |
536 | ||
eadd7bd2 VZ |
537 | DWORD lSectorsPerCluster, |
538 | lBytesPerSector, | |
539 | lNumberOfFreeClusters, | |
540 | lTotalNumberOfClusters; | |
541 | ||
542 | // FIXME: this is wrong, we should extract the root drive from path | |
543 | // instead, but this is the job for wxFileName... | |
544 | if ( !::GetDiskFreeSpace(path, | |
545 | &lSectorsPerCluster, | |
546 | &lBytesPerSector, | |
547 | &lNumberOfFreeClusters, | |
548 | &lTotalNumberOfClusters) ) | |
549 | { | |
550 | wxLogLastError(_T("GetDiskFreeSpace")); | |
551 | ||
552 | return FALSE; | |
553 | } | |
554 | ||
eadd7bd2 VZ |
555 | wxLongLong lBytesPerCluster = lSectorsPerCluster; |
556 | lBytesPerCluster *= lBytesPerSector; | |
557 | ||
558 | if ( pTotal ) | |
559 | { | |
560 | *pTotal = lBytesPerCluster; | |
561 | *pTotal *= lTotalNumberOfClusters; | |
562 | } | |
563 | ||
564 | if ( pFree ) | |
565 | { | |
566 | *pFree = lBytesPerCluster; | |
567 | *pFree *= lNumberOfFreeClusters; | |
568 | } | |
569 | } | |
570 | ||
571 | return TRUE; | |
572 | } | |
573 | ||
308978f6 VZ |
574 | // ---------------------------------------------------------------------------- |
575 | // env vars | |
576 | // ---------------------------------------------------------------------------- | |
577 | ||
578 | bool wxGetEnv(const wxString& var, wxString *value) | |
579 | { | |
788722ac JS |
580 | #ifdef __WIN16__ |
581 | const wxChar* ret = wxGetenv(var); | |
e622dd68 VZ |
582 | if ( !ret ) |
583 | return FALSE; | |
584 | ||
585 | if ( value ) | |
788722ac JS |
586 | { |
587 | *value = ret; | |
788722ac | 588 | } |
e622dd68 VZ |
589 | |
590 | return TRUE; | |
591 | #else // Win32 | |
308978f6 VZ |
592 | // first get the size of the buffer |
593 | DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0); | |
594 | if ( !dwRet ) | |
595 | { | |
596 | // this means that there is no such variable | |
597 | return FALSE; | |
598 | } | |
599 | ||
600 | if ( value ) | |
601 | { | |
602 | (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet); | |
603 | value->UngetWriteBuf(); | |
604 | } | |
605 | ||
606 | return TRUE; | |
e622dd68 | 607 | #endif // Win16/32 |
308978f6 VZ |
608 | } |
609 | ||
1fb45475 VZ |
610 | bool wxSetEnv(const wxString& var, const wxChar *value) |
611 | { | |
612 | // some compilers have putenv() or _putenv() or _wputenv() but it's better | |
613 | // to always use Win32 function directly instead of dealing with them | |
614 | #if defined(__WIN32__) | |
615 | if ( !::SetEnvironmentVariable(var, value) ) | |
616 | { | |
617 | wxLogLastError(_T("SetEnvironmentVariable")); | |
618 | ||
619 | return FALSE; | |
620 | } | |
621 | ||
622 | return TRUE; | |
623 | #else // no way to set env vars | |
624 | return FALSE; | |
625 | #endif | |
626 | } | |
627 | ||
b568d04f VZ |
628 | // ---------------------------------------------------------------------------- |
629 | // process management | |
630 | // ---------------------------------------------------------------------------- | |
631 | ||
50567b69 VZ |
632 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() |
633 | struct wxFindByPidParams | |
b568d04f | 634 | { |
50567b69 VZ |
635 | wxFindByPidParams() { hwnd = 0; pid = 0; } |
636 | ||
637 | // the HWND used to return the result | |
638 | HWND hwnd; | |
639 | ||
640 | // the PID we're looking from | |
641 | DWORD pid; | |
22f3361e VZ |
642 | |
643 | DECLARE_NO_COPY_CLASS(wxFindByPidParams) | |
50567b69 VZ |
644 | }; |
645 | ||
646 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
647 | // level) window belonging to the given process | |
648 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
649 | { | |
650 | DWORD pid; | |
651 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
652 | ||
653 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
654 | if ( pid == params->pid ) | |
655 | { | |
656 | // remember the window we found | |
657 | params->hwnd = hwnd; | |
658 | ||
659 | // return FALSE to stop the enumeration | |
660 | return FALSE; | |
661 | } | |
662 | ||
663 | // continue enumeration | |
664 | return TRUE; | |
665 | } | |
e949bf13 | 666 | |
50567b69 VZ |
667 | int wxKill(long pid, wxSignal sig, wxKillError *krc) |
668 | { | |
50567b69 VZ |
669 | // get the process handle to operate on |
670 | HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | | |
671 | PROCESS_TERMINATE | | |
672 | PROCESS_QUERY_INFORMATION, | |
673 | FALSE, // not inheritable | |
674 | (DWORD)pid); | |
675 | if ( hProcess == NULL ) | |
e949bf13 | 676 | { |
50567b69 | 677 | if ( krc ) |
e949bf13 | 678 | { |
50567b69 | 679 | if ( ::GetLastError() == ERROR_ACCESS_DENIED ) |
e949bf13 | 680 | { |
50567b69 VZ |
681 | *krc = wxKILL_ACCESS_DENIED; |
682 | } | |
683 | else | |
684 | { | |
685 | *krc = wxKILL_NO_PROCESS; | |
686 | } | |
687 | } | |
688 | ||
689 | return -1; | |
690 | } | |
691 | ||
692 | bool ok = TRUE; | |
693 | switch ( sig ) | |
694 | { | |
695 | case wxSIGKILL: | |
696 | // kill the process forcefully returning -1 as error code | |
697 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
698 | { | |
699 | wxLogSysError(_("Failed to kill process %d"), pid); | |
700 | ||
701 | if ( krc ) | |
e949bf13 | 702 | { |
50567b69 VZ |
703 | // this is not supposed to happen if we could open the |
704 | // process | |
705 | *krc = wxKILL_ERROR; | |
e949bf13 | 706 | } |
50567b69 VZ |
707 | |
708 | ok = FALSE; | |
e949bf13 | 709 | } |
50567b69 VZ |
710 | break; |
711 | ||
712 | case wxSIGNONE: | |
713 | // do nothing, we just want to test for process existence | |
714 | break; | |
715 | ||
716 | default: | |
717 | // any other signal means "terminate" | |
718 | { | |
719 | wxFindByPidParams params; | |
720 | params.pid = (DWORD)pid; | |
721 | ||
722 | // EnumWindows() has nice semantics: it returns 0 if it found | |
723 | // something or if an error occured and non zero if it | |
724 | // enumerated all the window | |
725 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
726 | { | |
727 | // did we find any window? | |
728 | if ( params.hwnd ) | |
729 | { | |
730 | // tell the app to close | |
731 | // | |
732 | // NB: this is the harshest way, the app won't have | |
733 | // opportunity to save any files, for example, but | |
734 | // this is probably what we want here. If not we | |
735 | // can also use SendMesageTimeout(WM_CLOSE) | |
736 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
737 | { | |
738 | wxLogLastError(_T("PostMessage(WM_QUIT)")); | |
739 | } | |
740 | } | |
741 | else // it was an error then | |
742 | { | |
743 | wxLogLastError(_T("EnumWindows")); | |
744 | ||
745 | ok = FALSE; | |
746 | } | |
747 | } | |
748 | else // no windows for this PID | |
749 | { | |
750 | if ( krc ) | |
751 | { | |
752 | *krc = wxKILL_ERROR; | |
753 | } | |
754 | ||
755 | ok = FALSE; | |
756 | } | |
757 | } | |
758 | } | |
759 | ||
760 | // the return code | |
761 | DWORD rc; | |
762 | ||
763 | if ( ok ) | |
764 | { | |
765 | // as we wait for a short time, we can use just WaitForSingleObject() | |
766 | // and not MsgWaitForMultipleObjects() | |
767 | switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) ) | |
768 | { | |
769 | case WAIT_OBJECT_0: | |
770 | // process terminated | |
771 | if ( !::GetExitCodeProcess(hProcess, &rc) ) | |
772 | { | |
773 | wxLogLastError(_T("GetExitCodeProcess")); | |
774 | } | |
775 | break; | |
776 | ||
777 | default: | |
778 | wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") ); | |
779 | // fall through | |
780 | ||
781 | case WAIT_FAILED: | |
782 | wxLogLastError(_T("WaitForSingleObject")); | |
783 | // fall through | |
784 | ||
785 | case WAIT_TIMEOUT: | |
786 | if ( krc ) | |
787 | { | |
788 | *krc = wxKILL_ERROR; | |
789 | } | |
790 | ||
791 | rc = STILL_ACTIVE; | |
792 | break; | |
e949bf13 JS |
793 | } |
794 | } | |
50567b69 VZ |
795 | else // !ok |
796 | { | |
797 | // just to suppress the warnings about uninitialized variable | |
798 | rc = 0; | |
799 | } | |
e949bf13 | 800 | |
50567b69 | 801 | ::CloseHandle(hProcess); |
b568d04f | 802 | |
50567b69 VZ |
803 | // the return code is the same as from Unix kill(): 0 if killed |
804 | // successfully or -1 on error | |
b1a28eef VZ |
805 | // |
806 | // be careful to interpret rc correctly: for wxSIGNONE we return success if | |
807 | // the process exists, for all the other sig values -- if it doesn't | |
808 | if ( ok && | |
809 | ((sig == wxSIGNONE) == (rc == STILL_ACTIVE)) ) | |
e949bf13 | 810 | { |
b1a28eef | 811 | if ( krc ) |
50567b69 | 812 | { |
b1a28eef | 813 | *krc = wxKILL_OK; |
50567b69 | 814 | } |
b1a28eef VZ |
815 | |
816 | return 0; | |
e949bf13 | 817 | } |
50567b69 VZ |
818 | |
819 | // error | |
820 | return -1; | |
2bda0e17 KB |
821 | } |
822 | ||
b568d04f VZ |
823 | // Execute a program in an Interactive Shell |
824 | bool wxShell(const wxString& command) | |
2bda0e17 | 825 | { |
b568d04f VZ |
826 | wxChar *shell = wxGetenv(wxT("COMSPEC")); |
827 | if ( !shell ) | |
ba14d986 | 828 | shell = (wxChar*) wxT("\\COMMAND.COM"); |
b568d04f VZ |
829 | |
830 | wxString cmd; | |
831 | if ( !command ) | |
832 | { | |
833 | // just the shell | |
834 | cmd = shell; | |
835 | } | |
836 | else | |
837 | { | |
838 | // pass the command to execute to the command processor | |
839 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
840 | } | |
841 | ||
011a524e | 842 | return wxExecute(cmd, wxEXEC_SYNC) == 0; |
2bda0e17 KB |
843 | } |
844 | ||
d8c65cf4 | 845 | // Shutdown or reboot the PC |
f6ba47d9 VZ |
846 | bool wxShutdown(wxShutdownFlags wFlags) |
847 | { | |
848 | #ifdef __WIN32__ | |
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 VS |
1021 | break; |
1022 | } | |
bdc72a22 VZ |
1023 | } |
1024 | } | |
1025 | ||
2739d4f0 VZ |
1026 | if ( verMaj ) |
1027 | *verMaj = s_major; | |
1028 | if ( verMin ) | |
1029 | *verMin = s_minor; | |
bdc72a22 | 1030 | |
2739d4f0 | 1031 | return s_ver; |
2bda0e17 KB |
1032 | } |
1033 | ||
b568d04f VZ |
1034 | // ---------------------------------------------------------------------------- |
1035 | // sleep functions | |
1036 | // ---------------------------------------------------------------------------- | |
1037 | ||
b568d04f VZ |
1038 | void wxUsleep(unsigned long milliseconds) |
1039 | { | |
1040 | ::Sleep(milliseconds); | |
1041 | } | |
1042 | ||
1043 | void wxSleep(int nSecs) | |
1044 | { | |
1045 | wxUsleep(1000*nSecs); | |
1046 | } | |
1047 | ||
b568d04f | 1048 | // ---------------------------------------------------------------------------- |
e2478fde | 1049 | // font encoding <-> Win32 codepage conversion functions |
b568d04f VZ |
1050 | // ---------------------------------------------------------------------------- |
1051 | ||
e2478fde | 1052 | extern long wxEncodingToCharset(wxFontEncoding encoding) |
b568d04f | 1053 | { |
e2478fde | 1054 | switch ( encoding ) |
b568d04f | 1055 | { |
e2478fde VZ |
1056 | // although this function is supposed to return an exact match, do do |
1057 | // some mappings here for the most common case of "standard" encoding | |
1058 | case wxFONTENCODING_SYSTEM: | |
1059 | return DEFAULT_CHARSET; | |
2bda0e17 | 1060 | |
e2478fde VZ |
1061 | case wxFONTENCODING_ISO8859_1: |
1062 | case wxFONTENCODING_ISO8859_15: | |
1063 | case wxFONTENCODING_CP1252: | |
1064 | return ANSI_CHARSET; | |
2bda0e17 | 1065 | |
e2478fde VZ |
1066 | #if !defined(__WXMICROWIN__) |
1067 | // The following four fonts are multi-byte charsets | |
1068 | case wxFONTENCODING_CP932: | |
1069 | return SHIFTJIS_CHARSET; | |
2bda0e17 | 1070 | |
e2478fde VZ |
1071 | case wxFONTENCODING_CP936: |
1072 | return GB2312_CHARSET; | |
2bda0e17 | 1073 | |
e2478fde VZ |
1074 | case wxFONTENCODING_CP949: |
1075 | return HANGUL_CHARSET; | |
634903fd | 1076 | |
e2478fde VZ |
1077 | case wxFONTENCODING_CP950: |
1078 | return CHINESEBIG5_CHARSET; | |
2bda0e17 | 1079 | |
e2478fde VZ |
1080 | // The rest are single byte encodings |
1081 | case wxFONTENCODING_CP1250: | |
1082 | return EASTEUROPE_CHARSET; | |
bfbd6dc1 | 1083 | |
e2478fde VZ |
1084 | case wxFONTENCODING_CP1251: |
1085 | return RUSSIAN_CHARSET; | |
2bda0e17 | 1086 | |
e2478fde VZ |
1087 | case wxFONTENCODING_CP1253: |
1088 | return GREEK_CHARSET; | |
b07135ba | 1089 | |
e2478fde VZ |
1090 | case wxFONTENCODING_CP1254: |
1091 | return TURKISH_CHARSET; | |
2bda0e17 | 1092 | |
e2478fde VZ |
1093 | case wxFONTENCODING_CP1255: |
1094 | return HEBREW_CHARSET; | |
2bda0e17 | 1095 | |
e2478fde VZ |
1096 | case wxFONTENCODING_CP1256: |
1097 | return ARABIC_CHARSET; | |
b568d04f | 1098 | |
e2478fde VZ |
1099 | case wxFONTENCODING_CP1257: |
1100 | return BALTIC_CHARSET; | |
634903fd | 1101 | |
e2478fde VZ |
1102 | case wxFONTENCODING_CP874: |
1103 | return THAI_CHARSET; | |
5acc0d5f | 1104 | #endif // !__WXMICROWIN__ |
cc2b7472 | 1105 | |
e2478fde VZ |
1106 | case wxFONTENCODING_CP437: |
1107 | return OEM_CHARSET; | |
ea28b885 VZ |
1108 | |
1109 | default: | |
1110 | // no way to translate this encoding into a Windows charset | |
69557827 | 1111 | return -1; |
e2478fde | 1112 | } |
373658eb | 1113 | } |
7f555861 | 1114 | |
e2478fde VZ |
1115 | // we have 2 versions of wxCharsetToCodepage(): the old one which directly |
1116 | // looks up the vlaues in the registry and the new one which is more | |
1117 | // politically correct and has more chances to work on other Windows versions | |
1118 | // as well but the old version is still needed for !wxUSE_FONTMAP case | |
1119 | #if wxUSE_FONTMAP | |
c030b70f | 1120 | |
373658eb | 1121 | #include "wx/fontmap.h" |
c030b70f | 1122 | |
373658eb | 1123 | extern long wxEncodingToCodepage(wxFontEncoding encoding) |
c030b70f | 1124 | { |
373658eb | 1125 | // translate encoding into the Windows CHARSET |
e2478fde VZ |
1126 | long charset = wxEncodingToCharset(encoding); |
1127 | if ( charset == -1 ) | |
373658eb VZ |
1128 | return -1; |
1129 | ||
1130 | // translate CHARSET to code page | |
1131 | CHARSETINFO csetInfo; | |
e2478fde | 1132 | if ( !::TranslateCharsetInfo((DWORD *)(DWORD)charset, |
373658eb VZ |
1133 | &csetInfo, |
1134 | TCI_SRCCHARSET) ) | |
1135 | { | |
1136 | wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)")); | |
1137 | ||
1138 | return -1; | |
1139 | } | |
1140 | ||
1141 | return csetInfo.ciACP; | |
c030b70f | 1142 | } |
373658eb | 1143 | |
373658eb | 1144 | extern long wxCharsetToCodepage(const wxChar *name) |
c030b70f | 1145 | { |
373658eb VZ |
1146 | // first get the font encoding for this charset |
1147 | if ( !name ) | |
1148 | return -1; | |
1149 | ||
142b3bc2 | 1150 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE); |
373658eb VZ |
1151 | if ( enc == wxFONTENCODING_SYSTEM ) |
1152 | return -1; | |
1153 | ||
1154 | // the use the helper function | |
1155 | return wxEncodingToCodepage(enc); | |
c030b70f | 1156 | } |
c030b70f | 1157 | |
e2478fde | 1158 | #else // !wxUSE_FONTMAP |
373658eb VZ |
1159 | |
1160 | #include "wx/msw/registry.h" | |
1161 | ||
1162 | // this should work if Internet Exploiter is installed | |
1163 | extern long wxCharsetToCodepage(const wxChar *name) | |
04ef50df | 1164 | { |
373658eb VZ |
1165 | if (!name) |
1166 | return GetACP(); | |
1167 | ||
e2478fde | 1168 | long CP = -1; |
373658eb | 1169 | |
e2478fde | 1170 | wxString path(wxT("MIME\\Database\\Charset\\")); |
373658eb | 1171 | wxString cn(name); |
373658eb | 1172 | |
e2478fde VZ |
1173 | // follow the alias loop |
1174 | for ( ;; ) | |
1175 | { | |
1176 | wxRegKey key(wxRegKey::HKCR, path + cn); | |
1177 | ||
1178 | if (!key.Exists()) | |
1179 | break; | |
373658eb VZ |
1180 | |
1181 | // two cases: either there's an AliasForCharset string, | |
1182 | // or there are Codepage and InternetEncoding dwords. | |
1183 | // The InternetEncoding gives us the actual encoding, | |
1184 | // the Codepage just says which Windows character set to | |
1185 | // use when displaying the data. | |
1186 | if (key.HasValue(wxT("InternetEncoding")) && | |
e2478fde VZ |
1187 | key.QueryValue(wxT("InternetEncoding"), &CP)) |
1188 | break; | |
373658eb VZ |
1189 | |
1190 | // no encoding, see if it's an alias | |
1191 | if (!key.HasValue(wxT("AliasForCharset")) || | |
e2478fde VZ |
1192 | !key.QueryValue(wxT("AliasForCharset"), cn)) |
1193 | break; | |
1194 | } | |
373658eb VZ |
1195 | |
1196 | return CP; | |
04ef50df | 1197 | } |
373658eb | 1198 | |
e2478fde | 1199 | #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP |
c030b70f | 1200 | |
94826170 VZ |
1201 | // ---------------------------------------------------------------------------- |
1202 | // wxApp::OnFatalException() support | |
1203 | // ---------------------------------------------------------------------------- | |
1204 | ||
1205 | bool wxHandleFatalExceptions(bool doit) | |
1206 | { | |
1207 | #if wxUSE_ON_FATAL_EXCEPTION | |
1208 | // assume this can only be called from the main thread | |
1209 | gs_handleExceptions = doit; | |
1210 | ||
1211 | return TRUE; | |
1212 | #else | |
1213 | wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to use this function")); | |
1214 | ||
1215 | (void)doit; | |
1216 | return FALSE; | |
1217 | #endif | |
1218 | } | |
1219 | ||
1220 | #if wxUSE_ON_FATAL_EXCEPTION | |
1221 | ||
1222 | extern unsigned long wxGlobalSEHandler() | |
1223 | { | |
1224 | if ( gs_handleExceptions && wxTheApp ) | |
1225 | { | |
1226 | // give the user a chance to do something special about this | |
1227 | wxTheApp->OnFatalException(); | |
1228 | ||
1229 | // this will execute our handler and terminate the process | |
1230 | return EXCEPTION_EXECUTE_HANDLER; | |
1231 | } | |
1232 | ||
1233 | return EXCEPTION_CONTINUE_SEARCH; | |
1234 | } | |
1235 | ||
1236 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1237 |