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