]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utils.cpp
Include wx/menu.h according to precompiled headers of wx/wx.h (with other minor clean...
[wxWidgets.git] / src / msw / utils.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
119f7bb7 2// Name: src/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 8// Copyright: (c) Julian Smart
65571936 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
66d84d11 34#include "wx/msw/registry.h"
2739d4f0 35#include "wx/apptrait.h"
23ee4b48 36#include "wx/dynlib.h"
15cdcf6a 37#include "wx/dynload.h"
a45fb5b4 38#include "wx/scopeguard.h"
2739d4f0 39
2dc357ea
VZ
40#include "wx/confbase.h" // for wxExpandEnvVars()
41
b568d04f 42#include "wx/msw/private.h" // includes <windows.h>
fcd0c90f 43#include "wx/msw/missing.h" // CHARSET_HANGUL
b568d04f 44
f6afe7fd
RN
45#if defined(__CYGWIN__)
46 //CYGWIN gives annoying warning about runtime stuff if we don't do this
47# define USE_SYS_TYPES_FD_SET
48# include <sys/types.h>
49#endif
50
7acf6a92
JS
51// Doesn't work with Cygwin at present
52#if wxUSE_SOCKETS && (defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) || defined(__CYGWIN32__))
c1cb4153
VZ
53 // apparently we need to include winsock.h to get WSADATA and other stuff
54 // used in wxGetFullHostName() with the old mingw32 versions
55 #include <winsock.h>
56#endif
57
2bda0e17
KB
58#include "wx/timer.h"
59
4676948b 60#if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
a4f96412 61 #include <direct.h>
ce3ed50d 62
a4f96412
VZ
63 #ifndef __MWERKS__
64 #include <dos.h>
65 #endif
743e0a66 66#endif //GNUWIN32
2bda0e17 67
b39dbf34 68#if defined(__CYGWIN__)
a4f96412
VZ
69 #include <sys/unistd.h>
70 #include <sys/stat.h>
3ffbc733 71 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
743e0a66
VZ
72#endif //GNUWIN32
73
2bda0e17
KB
74#ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
75 // this (3.1 I believe) and how to test for it.
76 // If this works for Borland 4.0 as well, then no worries.
a4f96412 77 #include <dir.h>
2bda0e17
KB
78#endif
79
a4f96412
VZ
80// VZ: there is some code using NetXXX() functions to get the full user name:
81// I don't think it's a good idea because they don't work under Win95 and
82// seem to return the same as wxGetUserId() under NT. If you really want
83// to use them, just #define USE_NET_API
84#undef USE_NET_API
85
86#ifdef USE_NET_API
87 #include <lm.h>
88#endif // USE_NET_API
89
4676948b 90#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
2e38557f
JS
91 #ifndef __UNIX__
92 #include <io.h>
93 #endif
2bda0e17 94
a4f96412
VZ
95 #ifndef __GNUWIN32__
96 #include <shellapi.h>
97 #endif
2bda0e17
KB
98#endif
99
2bda0e17 100#ifndef __WATCOMC__
3f4a0c5b
VZ
101 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
102 #include <errno.h>
103 #endif
2bda0e17 104#endif
2bda0e17 105
e0f6b731
JS
106// For wxKillAllChildren
107#include <tlhelp32.h>
108
b568d04f
VZ
109// ----------------------------------------------------------------------------
110// constants
111// ----------------------------------------------------------------------------
112
2bda0e17 113// In the WIN.INI file
119f7bb7 114#if (!defined(USE_NET_API) && !defined(__WXWINCE__)) || defined(__WXMICROWIN__)
e7d9c398 115static const wxChar WX_SECTION[] = wxT("wxWindows");
119f7bb7
WS
116#endif
117
118#if (!defined(USE_NET_API) && !defined(__WXWINCE__))
b568d04f 119static const wxChar eUSERNAME[] = wxT("UserName");
119f7bb7 120#endif
b568d04f 121
b568d04f
VZ
122// ============================================================================
123// implementation
124// ============================================================================
2bda0e17 125
b568d04f
VZ
126// ----------------------------------------------------------------------------
127// get host name and related
128// ----------------------------------------------------------------------------
2bda0e17 129
1f0500b3 130// Get hostname only (without domain name)
7bea7b91
WS
131bool wxGetHostName(wxChar *WXUNUSED_IN_WINCE(buf),
132 int WXUNUSED_IN_WINCE(maxSize))
2bda0e17 133{
4676948b 134#if defined(__WXWINCE__)
040e5f77 135 // TODO-CE
7010702f 136 return false;
4676948b 137#elif defined(__WIN32__) && !defined(__WXMICROWIN__)
b568d04f
VZ
138 DWORD nSize = maxSize;
139 if ( !::GetComputerName(buf, &nSize) )
140 {
f6bcfd97 141 wxLogLastError(wxT("GetComputerName"));
b568d04f 142
7010702f 143 return false;
b568d04f
VZ
144 }
145
27d2dbbc 146 return true;
2bda0e17 147#else
b568d04f
VZ
148 wxChar *sysname;
149 const wxChar *default_host = wxT("noname");
150
151 if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) {
152 GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
153 } else
154 wxStrncpy(buf, sysname, maxSize - 1);
155 buf[maxSize] = wxT('\0');
27d2dbbc 156 return *buf ? true : false;
2bda0e17
KB
157#endif
158}
159
1f0500b3 160// get full hostname (with domain name if possible)
b568d04f
VZ
161bool wxGetFullHostName(wxChar *buf, int maxSize)
162{
7acf6a92 163#if !defined( __WXMICROWIN__) && wxUSE_DYNAMIC_LOADER && wxUSE_SOCKETS
1f0500b3 164 // TODO should use GetComputerNameEx() when available
79180098 165
15cdcf6a 166 // we don't want to always link with Winsock DLL as we might not use it at
7b28e0ed
VZ
167 // all, so load it dynamically here if needed (and don't complain if it is
168 // missing, we handle this)
169 wxLogNull noLog;
170
15cdcf6a
VZ
171 wxDynamicLibrary dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM);
172 if ( dllWinsock.IsLoaded() )
1f0500b3 173 {
15cdcf6a
VZ
174 typedef int (PASCAL *WSAStartup_t)(WORD, WSADATA *);
175 typedef int (PASCAL *gethostname_t)(char *, int);
176 typedef hostent* (PASCAL *gethostbyname_t)(const char *);
177 typedef hostent* (PASCAL *gethostbyaddr_t)(const char *, int , int);
178 typedef int (PASCAL *WSACleanup_t)(void);
1f0500b3 179
15cdcf6a
VZ
180 #define LOAD_WINSOCK_FUNC(func) \
181 func ## _t \
182 pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
1f0500b3 183
15cdcf6a
VZ
184 LOAD_WINSOCK_FUNC(WSAStartup);
185
186 WSADATA wsa;
187 if ( pfnWSAStartup && pfnWSAStartup(MAKEWORD(1, 1), &wsa) == 0 )
188 {
189 LOAD_WINSOCK_FUNC(gethostname);
190
191 wxString host;
192 if ( pfngethostname )
193 {
194 char bufA[256];
195 if ( pfngethostname(bufA, WXSIZEOF(bufA)) == 0 )
1f0500b3 196 {
15cdcf6a
VZ
197 // gethostname() won't usually include the DNS domain name,
198 // for this we need to work a bit more
199 if ( !strchr(bufA, '.') )
200 {
201 LOAD_WINSOCK_FUNC(gethostbyname);
202
203 struct hostent *pHostEnt = pfngethostbyname
204 ? pfngethostbyname(bufA)
205 : NULL;
206
207 if ( pHostEnt )
208 {
209 // Windows will use DNS internally now
210 LOAD_WINSOCK_FUNC(gethostbyaddr);
211
212 pHostEnt = pfngethostbyaddr
213 ? pfngethostbyaddr(pHostEnt->h_addr,
214 4, AF_INET)
215 : NULL;
216 }
217
218 if ( pHostEnt )
219 {
220 host = wxString::FromAscii(pHostEnt->h_name);
221 }
222 }
1f0500b3
VZ
223 }
224 }
1f0500b3 225
15cdcf6a
VZ
226 LOAD_WINSOCK_FUNC(WSACleanup);
227 if ( pfnWSACleanup )
228 pfnWSACleanup();
1f0500b3 229
1f0500b3 230
15cdcf6a
VZ
231 if ( !host.empty() )
232 {
233 wxStrncpy(buf, host, maxSize);
234
27d2dbbc 235 return true;
15cdcf6a 236 }
1f0500b3
VZ
237 }
238 }
15cdcf6a 239#endif // !__WXMICROWIN__
1f0500b3 240
b568d04f
VZ
241 return wxGetHostName(buf, maxSize);
242}
243
2bda0e17 244// Get user ID e.g. jacs
7bea7b91
WS
245bool wxGetUserId(wxChar *WXUNUSED_IN_WINCE(buf),
246 int WXUNUSED_IN_WINCE(maxSize))
2bda0e17 247{
4676948b 248#if defined(__WXWINCE__)
040e5f77 249 // TODO-CE
7010702f 250 return false;
3a5bcc4d 251#elif defined(__WIN32__) && !defined(__WXMICROWIN__)
0a144271
VZ
252 DWORD nSize = maxSize;
253 if ( ::GetUserName(buf, &nSize) == 0 )
254 {
a4f96412 255 // actually, it does happen on Win9x if the user didn't log on
223d09f6 256 DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize);
a4f96412
VZ
257 if ( res == 0 )
258 {
259 // not found
27d2dbbc 260 return false;
a4f96412 261 }
0a144271
VZ
262 }
263
27d2dbbc 264 return true;
3a5bcc4d 265#else // __WXMICROWIN__
b568d04f
VZ
266 wxChar *user;
267 const wxChar *default_id = wxT("anonymous");
2bda0e17 268
b568d04f
VZ
269 // Can't assume we have NIS (PC-NFS) or some other ID daemon
270 // So we ...
119f7bb7
WS
271 if ( (user = wxGetenv(wxT("USER"))) == NULL &&
272 (user = wxGetenv(wxT("LOGNAME"))) == NULL )
b568d04f 273 {
77ffb593 274 // Use wxWidgets configuration data (comming soon)
b568d04f
VZ
275 GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1);
276 }
277 else
278 {
279 wxStrncpy(buf, user, maxSize - 1);
280 }
a4f96412 281
27d2dbbc 282 return *buf ? true : false;
2bda0e17
KB
283#endif
284}
285
286// Get user name e.g. Julian Smart
119f7bb7 287bool wxGetUserName(wxChar *buf, int maxSize)
2bda0e17 288{
119f7bb7
WS
289 wxCHECK_MSG( buf && ( maxSize > 0 ), false,
290 _T("empty buffer in wxGetUserName") );
4676948b 291#if defined(__WXWINCE__)
66d84d11
WS
292 wxLogNull noLog;
293 wxRegKey key(wxRegKey::HKCU, wxT("ControlPanel\\Owner"));
119f7bb7
WS
294 if(!key.Open(wxRegKey::Read))
295 return false;
296 wxString name;
66d84d11 297 if(!key.QueryValue(wxT("Owner"),name))
119f7bb7 298 return false;
86f6a6d1
WS
299 wxStrncpy(buf, name.c_str(), maxSize-1);
300 buf[maxSize-1] = _T('\0');
119f7bb7 301 return true;
4676948b 302#elif defined(USE_NET_API)
3e3be693
VZ
303 CHAR szUserName[256];
304 if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
7010702f 305 return false;
a4f96412 306
3e3be693
VZ
307 // TODO how to get the domain name?
308 CHAR *szDomain = "";
a4f96412 309
3e3be693
VZ
310 // the code is based on the MSDN example (also see KB article Q119670)
311 WCHAR wszUserName[256]; // Unicode user name
312 WCHAR wszDomain[256];
313 LPBYTE ComputerName;
a4f96412 314
3e3be693 315 USER_INFO_2 *ui2; // User structure
a4f96412 316
3e3be693
VZ
317 // Convert ANSI user name and domain to Unicode
318 MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1,
319 wszUserName, WXSIZEOF(wszUserName) );
320 MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1,
321 wszDomain, WXSIZEOF(wszDomain) );
a4f96412 322
3e3be693
VZ
323 // Get the computer name of a DC for the domain.
324 if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success )
325 {
326 wxLogError(wxT("Can not find domain controller"));
a4f96412 327
3e3be693
VZ
328 goto error;
329 }
a4f96412 330
3e3be693
VZ
331 // Look up the user on the DC
332 NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName,
333 (LPWSTR)&wszUserName,
334 2, // level - we want USER_INFO_2
335 (LPBYTE *) &ui2 );
336 switch ( status )
337 {
338 case NERR_Success:
339 // ok
340 break;
a4f96412 341
3e3be693
VZ
342 case NERR_InvalidComputer:
343 wxLogError(wxT("Invalid domain controller name."));
a4f96412 344
3e3be693 345 goto error;
a4f96412 346
3e3be693
VZ
347 case NERR_UserNotFound:
348 wxLogError(wxT("Invalid user name '%s'."), szUserName);
a4f96412 349
3e3be693 350 goto error;
a4f96412 351
3e3be693
VZ
352 default:
353 wxLogSysError(wxT("Can't get information about user"));
a4f96412 354
3e3be693
VZ
355 goto error;
356 }
a4f96412 357
3e3be693
VZ
358 // Convert the Unicode full name to ANSI
359 WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
360 buf, maxSize, NULL, NULL );
a4f96412 361
27d2dbbc 362 return true;
a4f96412
VZ
363
364error:
3e3be693 365 wxLogError(wxT("Couldn't look up full user name."));
a4f96412 366
27d2dbbc 367 return false;
a4f96412 368#else // !USE_NET_API
3e3be693 369 // Could use NIS, MS-Mail or other site specific programs
77ffb593 370 // Use wxWidgets configuration data
fda7962d 371 bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxEmptyString, buf, maxSize - 1) != 0;
3e3be693
VZ
372 if ( !ok )
373 {
374 ok = wxGetUserId(buf, maxSize);
375 }
0a144271 376
3e3be693
VZ
377 if ( !ok )
378 {
379 wxStrncpy(buf, wxT("Unknown User"), maxSize);
b568d04f 380 }
0a144271 381
27d2dbbc 382 return true;
040e5f77 383#endif // Win32/16
2bda0e17
KB
384}
385
b568d04f 386const wxChar* wxGetHomeDir(wxString *pstr)
2bda0e17 387{
2dc357ea 388 wxString& strDir = *pstr;
2bda0e17 389
2dc357ea 390 // first branch is for Cygwin
4676948b 391#if defined(__UNIX__)
b568d04f
VZ
392 const wxChar *szHome = wxGetenv("HOME");
393 if ( szHome == NULL ) {
394 // we're homeless...
395 wxLogWarning(_("can't find user's HOME, using current directory."));
396 strDir = wxT(".");
397 }
398 else
399 strDir = szHome;
2bda0e17 400
b568d04f
VZ
401 // add a trailing slash if needed
402 if ( strDir.Last() != wxT('/') )
403 strDir << wxT('/');
3ffbc733 404
e02e8816 405 #ifdef __CYGWIN__
2dc357ea
VZ
406 // Cygwin returns unix type path but that does not work well
407 static wxChar windowsPath[MAX_PATH];
408 cygwin_conv_to_full_win32_path(strDir, windowsPath);
409 strDir = windowsPath;
3ffbc733 410 #endif
4676948b 411#elif defined(__WXWINCE__)
0367d339 412 strDir = wxT("\\");
4676948b 413#else
2dc357ea 414 strDir.clear();
96c21216 415
2dc357ea
VZ
416 // If we have a valid HOME directory, as is used on many machines that
417 // have unix utilities on them, we should use that.
418 const wxChar *szHome = wxGetenv(wxT("HOME"));
96c21216 419
2dc357ea
VZ
420 if ( szHome != NULL )
421 {
96c21216 422 strDir = szHome;
2dc357ea
VZ
423 }
424 else // no HOME, try HOMEDRIVE/PATH
425 {
426 szHome = wxGetenv(wxT("HOMEDRIVE"));
427 if ( szHome != NULL )
96c21216 428 strDir << szHome;
2dc357ea 429 szHome = wxGetenv(wxT("HOMEPATH"));
96c21216 430
2dc357ea
VZ
431 if ( szHome != NULL )
432 {
96c21216
VZ
433 strDir << szHome;
434
435 // the idea is that under NT these variables have default values
436 // of "%systemdrive%:" and "\\". As we don't want to create our
437 // config files in the root directory of the system drive, we will
438 // create it in our program's dir. However, if the user took care
439 // to set HOMEPATH to something other than "\\", we suppose that he
440 // knows what he is doing and use the supplied value.
441 if ( wxStrcmp(szHome, wxT("\\")) == 0 )
2dc357ea
VZ
442 strDir.clear();
443 }
444 }
96c21216 445
2dc357ea
VZ
446 if ( strDir.empty() )
447 {
448 // If we have a valid USERPROFILE directory, as is the case in
449 // Windows NT, 2000 and XP, we should use that as our home directory.
450 szHome = wxGetenv(wxT("USERPROFILE"));
2bda0e17 451
2dc357ea 452 if ( szHome != NULL )
96c21216 453 strDir = szHome;
2dc357ea 454 }
2bda0e17 455
2dc357ea
VZ
456 if ( !strDir.empty() )
457 {
458 // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the
459 // value once again, it shouldn't hurt anyhow
460 strDir = wxExpandEnvVars(strDir);
461 }
462 else // fall back to the program directory
463 {
77d8d6cd
VZ
464 // extract the directory component of the program file name
465 wxSplitPath(wxGetFullModuleName(), &strDir, NULL, NULL);
2dc357ea 466 }
4676948b 467#endif // UNIX/Win
b568d04f 468
2dc357ea 469 return strDir.c_str();
afb74891
VZ
470}
471
33ac7e6f 472wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
2bda0e17 473{
b568d04f
VZ
474 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
475 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
476 // static buffer and sometimes I don't even know what.
477 static wxString s_home;
2bda0e17 478
b568d04f 479 return (wxChar *)wxGetHomeDir(&s_home);
2bda0e17
KB
480}
481
7bea7b91 482bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path),
7ba7c4e6
VZ
483 wxDiskspaceSize_t *WXUNUSED_IN_WINCE(pTotal),
484 wxDiskspaceSize_t *WXUNUSED_IN_WINCE(pFree))
eadd7bd2 485{
4676948b 486#ifdef __WXWINCE__
040e5f77 487 // TODO-CE
7010702f 488 return false;
4676948b 489#else
eadd7bd2 490 if ( path.empty() )
7010702f 491 return false;
eadd7bd2 492
ee88cb34
MB
493// old w32api don't have ULARGE_INTEGER
494#if defined(__WIN32__) && \
495 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
eadd7bd2
VZ
496 // GetDiskFreeSpaceEx() is not available under original Win95, check for
497 // it
9ee966ec
VZ
498 typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR,
499 PULARGE_INTEGER,
500 PULARGE_INTEGER,
501 PULARGE_INTEGER);
eadd7bd2
VZ
502
503 GetDiskFreeSpaceEx_t
bb0e27ee 504 pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress
eadd7bd2
VZ
505 (
506 ::GetModuleHandle(_T("kernel32.dll")),
507#if wxUSE_UNICODE
508 "GetDiskFreeSpaceExW"
509#else
510 "GetDiskFreeSpaceExA"
511#endif
512 );
513
514 if ( pGetDiskFreeSpaceEx )
515 {
516 ULARGE_INTEGER bytesFree, bytesTotal;
517
518 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
519 if ( !pGetDiskFreeSpaceEx(path,
520 &bytesFree,
521 &bytesTotal,
522 NULL) )
523 {
524 wxLogLastError(_T("GetDiskFreeSpaceEx"));
525
27d2dbbc 526 return false;
eadd7bd2
VZ
527 }
528
221e1181
VZ
529 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
530 // two 32 bit fields which may be or may be not named - try to make it
531 // compile in all cases
532#if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
533 #define UL(ul) ul.u
534#else // anon union
535 #define UL(ul) ul
536#endif
eadd7bd2
VZ
537 if ( pTotal )
538 {
7ba7c4e6
VZ
539#if wxUSE_LONGLONG
540 *pTotal = wxDiskspaceSize_t(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart);
541#else
542 *pTotal = wxDiskspaceSize_t(UL(bytesTotal).LowPart);
543#endif
eadd7bd2
VZ
544 }
545
546 if ( pFree )
547 {
7ba7c4e6 548#if wxUSE_LONGLONG
221e1181 549 *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart);
7ba7c4e6
VZ
550#else
551 *pFree = wxDiskspaceSize_t(UL(bytesFree).LowPart);
552#endif
eadd7bd2
VZ
553 }
554 }
555 else
556#endif // Win32
557 {
bb0e27ee
VZ
558 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
559 // should be used instead - but if it's not available, fall back on
560 // GetDiskFreeSpace() nevertheless...
561
eadd7bd2
VZ
562 DWORD lSectorsPerCluster,
563 lBytesPerSector,
564 lNumberOfFreeClusters,
565 lTotalNumberOfClusters;
566
567 // FIXME: this is wrong, we should extract the root drive from path
568 // instead, but this is the job for wxFileName...
569 if ( !::GetDiskFreeSpace(path,
570 &lSectorsPerCluster,
571 &lBytesPerSector,
572 &lNumberOfFreeClusters,
573 &lTotalNumberOfClusters) )
574 {
575 wxLogLastError(_T("GetDiskFreeSpace"));
576
27d2dbbc 577 return false;
eadd7bd2
VZ
578 }
579
7ba7c4e6 580 wxDiskspaceSize_t lBytesPerCluster = (wxDiskspaceSize_t) lSectorsPerCluster;
eadd7bd2
VZ
581 lBytesPerCluster *= lBytesPerSector;
582
583 if ( pTotal )
584 {
585 *pTotal = lBytesPerCluster;
586 *pTotal *= lTotalNumberOfClusters;
587 }
588
589 if ( pFree )
590 {
591 *pFree = lBytesPerCluster;
592 *pFree *= lNumberOfFreeClusters;
593 }
594 }
595
27d2dbbc 596 return true;
4676948b
JS
597#endif
598 // __WXWINCE__
eadd7bd2
VZ
599}
600
308978f6
VZ
601// ----------------------------------------------------------------------------
602// env vars
603// ----------------------------------------------------------------------------
604
7bea7b91
WS
605bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var),
606 wxString *WXUNUSED_IN_WINCE(value))
308978f6 607{
4676948b 608#ifdef __WXWINCE__
040e5f77 609 // no environment variables under CE
27d2dbbc 610 return false;
e622dd68 611#else // Win32
308978f6
VZ
612 // first get the size of the buffer
613 DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0);
614 if ( !dwRet )
615 {
616 // this means that there is no such variable
27d2dbbc 617 return false;
308978f6
VZ
618 }
619
620 if ( value )
621 {
de564874
MB
622 (void)::GetEnvironmentVariable(var, wxStringBuffer(*value, dwRet),
623 dwRet);
308978f6
VZ
624 }
625
27d2dbbc 626 return true;
3a5bcc4d 627#endif // WinCE/32
308978f6
VZ
628}
629
7bea7b91
WS
630bool wxSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
631 const wxChar *WXUNUSED_IN_WINCE(value))
1fb45475
VZ
632{
633 // some compilers have putenv() or _putenv() or _wputenv() but it's better
634 // to always use Win32 function directly instead of dealing with them
7bea7b91
WS
635#ifdef __WXWINCE__
636 // no environment variables under CE
637 return false;
638#else
1fb45475
VZ
639 if ( !::SetEnvironmentVariable(var, value) )
640 {
641 wxLogLastError(_T("SetEnvironmentVariable"));
642
27d2dbbc 643 return false;
1fb45475
VZ
644 }
645
27d2dbbc 646 return true;
1fb45475
VZ
647#endif
648}
649
b568d04f
VZ
650// ----------------------------------------------------------------------------
651// process management
652// ----------------------------------------------------------------------------
653
50567b69
VZ
654// structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
655struct wxFindByPidParams
b568d04f 656{
50567b69
VZ
657 wxFindByPidParams() { hwnd = 0; pid = 0; }
658
659 // the HWND used to return the result
660 HWND hwnd;
661
662 // the PID we're looking from
663 DWORD pid;
22f3361e
VZ
664
665 DECLARE_NO_COPY_CLASS(wxFindByPidParams)
50567b69
VZ
666};
667
668// wxKill helper: EnumWindows() callback which is used to find the first (top
669// level) window belonging to the given process
670BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam)
671{
672 DWORD pid;
673 (void)::GetWindowThreadProcessId(hwnd, &pid);
674
675 wxFindByPidParams *params = (wxFindByPidParams *)lParam;
676 if ( pid == params->pid )
677 {
678 // remember the window we found
679 params->hwnd = hwnd;
680
681 // return FALSE to stop the enumeration
682 return FALSE;
683 }
684
685 // continue enumeration
686 return TRUE;
687}
e949bf13 688
e0f6b731
JS
689int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc);
690
691int wxKill(long pid, wxSignal sig, wxKillError *krc, int flags)
50567b69 692{
e0f6b731
JS
693 if (flags & wxKILL_CHILDREN)
694 wxKillAllChildren(pid, sig, krc);
9d8aca48 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 {
a45fb5b4
VZ
706 // recognize wxKILL_ACCESS_DENIED as special because this doesn't
707 // mean that the process doesn't exist and this is important for
708 // wxProcess::Exists()
709 *krc = ::GetLastError() == ERROR_ACCESS_DENIED
710 ? wxKILL_ACCESS_DENIED
711 : wxKILL_NO_PROCESS;
50567b69
VZ
712 }
713
714 return -1;
715 }
716
a45fb5b4
VZ
717 wxON_BLOCK_EXIT1(::CloseHandle, hProcess);
718
27d2dbbc 719 bool ok = true;
50567b69
VZ
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 734
27d2dbbc 735 ok = false;
e949bf13 736 }
50567b69
VZ
737 break;
738
739 case wxSIGNONE:
740 // do nothing, we just want to test for process existence
a45fb5b4
VZ
741 if ( krc )
742 *krc = wxKILL_OK;
743 return 0;
50567b69
VZ
744
745 default:
746 // any other signal means "terminate"
747 {
748 wxFindByPidParams params;
749 params.pid = (DWORD)pid;
750
751 // EnumWindows() has nice semantics: it returns 0 if it found
3103e8a9 752 // something or if an error occurred and non zero if it
50567b69
VZ
753 // enumerated all the window
754 if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)&params) )
755 {
756 // did we find any window?
757 if ( params.hwnd )
758 {
759 // tell the app to close
760 //
3103e8a9 761 // NB: this is the harshest way, the app won't have an
50567b69
VZ
762 // opportunity to save any files, for example, but
763 // this is probably what we want here. If not we
764 // can also use SendMesageTimeout(WM_CLOSE)
765 if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) )
766 {
767 wxLogLastError(_T("PostMessage(WM_QUIT)"));
768 }
769 }
770 else // it was an error then
771 {
772 wxLogLastError(_T("EnumWindows"));
773
27d2dbbc 774 ok = false;
50567b69
VZ
775 }
776 }
777 else // no windows for this PID
778 {
779 if ( krc )
50567b69 780 *krc = wxKILL_ERROR;
50567b69 781
27d2dbbc 782 ok = false;
50567b69
VZ
783 }
784 }
785 }
786
787 // the return code
a45fb5b4 788 DWORD rc wxDUMMY_INITIALIZE(0);
50567b69
VZ
789 if ( ok )
790 {
791 // as we wait for a short time, we can use just WaitForSingleObject()
792 // and not MsgWaitForMultipleObjects()
793 switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) )
794 {
795 case WAIT_OBJECT_0:
796 // process terminated
797 if ( !::GetExitCodeProcess(hProcess, &rc) )
798 {
799 wxLogLastError(_T("GetExitCodeProcess"));
800 }
801 break;
802
803 default:
804 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
805 // fall through
806
807 case WAIT_FAILED:
808 wxLogLastError(_T("WaitForSingleObject"));
809 // fall through
810
811 case WAIT_TIMEOUT:
812 if ( krc )
50567b69 813 *krc = wxKILL_ERROR;
50567b69
VZ
814
815 rc = STILL_ACTIVE;
816 break;
e949bf13
JS
817 }
818 }
819
b568d04f 820
50567b69
VZ
821 // the return code is the same as from Unix kill(): 0 if killed
822 // successfully or -1 on error
a45fb5b4
VZ
823 if ( !ok || rc == STILL_ACTIVE )
824 return -1;
b1a28eef 825
a45fb5b4
VZ
826 if ( krc )
827 *krc = wxKILL_OK;
50567b69 828
a45fb5b4 829 return 0;
2bda0e17
KB
830}
831
78a054f6
WS
832typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_t)(DWORD,DWORD);
833typedef BOOL (WINAPI *Process32_t)(HANDLE,LPPROCESSENTRY32);
834
835CreateToolhelp32Snapshot_t lpfCreateToolhelp32Snapshot;
836Process32_t lpfProcess32First, lpfProcess32Next;
9d8aca48 837
e0f6b731
JS
838static void InitToolHelp32()
839{
840 static bool s_initToolHelpDone = false;
9d8aca48 841
e0f6b731
JS
842 if (s_initToolHelpDone)
843 return;
9d8aca48 844
e0f6b731
JS
845 s_initToolHelpDone = true;
846
847 lpfCreateToolhelp32Snapshot = NULL;
848 lpfProcess32First = NULL;
849 lpfProcess32Next = NULL;
9d8aca48 850
32c0c61c
WS
851#if wxUSE_DYNLIB_CLASS
852
78a054f6 853 wxDynamicLibrary dllKernel(_T("kernel32.dll"), wxDL_VERBATIM);
9d8aca48 854
e0f6b731
JS
855 // Get procedure addresses.
856 // We are linking to these functions of Kernel32
857 // explicitly, because otherwise a module using
858 // this code would fail to load under Windows NT,
859 // which does not have the Toolhelp32
860 // functions in the Kernel 32.
78a054f6
WS
861 lpfCreateToolhelp32Snapshot =
862 (CreateToolhelp32Snapshot_t)dllKernel.RawGetSymbol(_T("CreateToolhelp32Snapshot"));
9d8aca48 863
78a054f6
WS
864 lpfProcess32First =
865 (Process32_t)dllKernel.RawGetSymbol(_T("Process32First"));
e0f6b731 866
78a054f6
WS
867 lpfProcess32Next =
868 (Process32_t)dllKernel.RawGetSymbol(_T("Process32Next"));
32c0c61c
WS
869
870#endif // wxUSE_DYNLIB_CLASS
e0f6b731
JS
871}
872
873// By John Skiff
874int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc)
875{
876 InitToolHelp32();
9d8aca48 877
e0f6b731
JS
878 if (krc)
879 *krc = wxKILL_OK;
9d8aca48 880
e0f6b731
JS
881 // If not implemented for this platform (e.g. NT 4.0), silently ignore
882 if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next)
883 return 0;
9d8aca48 884
e0f6b731
JS
885 // Take a snapshot of all processes in the system.
886 HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
887 if (hProcessSnap == INVALID_HANDLE_VALUE) {
888 if (krc)
889 *krc = wxKILL_ERROR;
890 return -1;
891 }
9d8aca48 892
e0f6b731 893 //Fill in the size of the structure before using it.
04a18b0d
WS
894 PROCESSENTRY32 pe;
895 wxZeroMemory(pe);
e0f6b731 896 pe.dwSize = sizeof(PROCESSENTRY32);
9d8aca48 897
e0f6b731
JS
898 // Walk the snapshot of the processes, and for each process,
899 // kill it if its parent is pid.
900 if (!lpfProcess32First(hProcessSnap, &pe)) {
901 // Can't get first process.
902 if (krc)
903 *krc = wxKILL_ERROR;
904 CloseHandle (hProcessSnap);
905 return -1;
906 }
9d8aca48 907
e0f6b731
JS
908 do {
909 if (pe.th32ParentProcessID == (DWORD) pid) {
910 if (wxKill(pe.th32ProcessID, sig, krc))
911 return -1;
912 }
913 } while (lpfProcess32Next (hProcessSnap, &pe));
9d8aca48
WS
914
915
e0f6b731
JS
916 return 0;
917}
918
b568d04f
VZ
919// Execute a program in an Interactive Shell
920bool wxShell(const wxString& command)
2bda0e17 921{
7010702f
WS
922 wxString cmd;
923
4676948b 924#ifdef __WXWINCE__
7010702f 925 cmd = command;
4676948b 926#else
b568d04f
VZ
927 wxChar *shell = wxGetenv(wxT("COMSPEC"));
928 if ( !shell )
ba14d986 929 shell = (wxChar*) wxT("\\COMMAND.COM");
b568d04f 930
b568d04f
VZ
931 if ( !command )
932 {
933 // just the shell
934 cmd = shell;
935 }
936 else
937 {
938 // pass the command to execute to the command processor
939 cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
940 }
7010702f 941#endif
b568d04f 942
011a524e 943 return wxExecute(cmd, wxEXEC_SYNC) == 0;
2bda0e17
KB
944}
945
d8c65cf4 946// Shutdown or reboot the PC
7bea7b91 947bool wxShutdown(wxShutdownFlags WXUNUSED_IN_WINCE(wFlags))
f6ba47d9 948{
4676948b 949#ifdef __WXWINCE__
040e5f77 950 // TODO-CE
27d2dbbc 951 return false;
4676948b 952#elif defined(__WIN32__)
27d2dbbc 953 bool bOK = true;
f6ba47d9
VZ
954
955 if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K
956 {
d8c65cf4
RD
957 // Get a token for this process.
958 HANDLE hToken;
f6ba47d9
VZ
959 bOK = ::OpenProcessToken(GetCurrentProcess(),
960 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
961 &hToken) != 0;
962 if ( bOK )
963 {
d8c65cf4 964 TOKEN_PRIVILEGES tkp;
f6ba47d9 965
d8c65cf4 966 // Get the LUID for the shutdown privilege.
f6ba47d9 967 ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
d8c65cf4 968 &tkp.Privileges[0].Luid);
f6ba47d9 969
d8c65cf4
RD
970 tkp.PrivilegeCount = 1; // one privilege to set
971 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
f6ba47d9 972
d8c65cf4 973 // Get the shutdown privilege for this process.
f6ba47d9 974 ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
d8c65cf4 975 (PTOKEN_PRIVILEGES)NULL, 0);
f6ba47d9 976
d8c65cf4 977 // Cannot test the return value of AdjustTokenPrivileges.
f6ba47d9
VZ
978 bOK = ::GetLastError() == ERROR_SUCCESS;
979 }
980 }
981
982 if ( bOK )
983 {
984 UINT flags = EWX_SHUTDOWN | EWX_FORCE;
985 switch ( wFlags )
986 {
987 case wxSHUTDOWN_POWEROFF:
988 flags |= EWX_POWEROFF;
989 break;
990
991 case wxSHUTDOWN_REBOOT:
992 flags |= EWX_REBOOT;
993 break;
994
995 default:
996 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
27d2dbbc 997 return false;
f6ba47d9
VZ
998 }
999
999836aa 1000 bOK = ::ExitWindowsEx(flags, 0) != 0;
f6ba47d9
VZ
1001 }
1002
1003 return bOK;
f6ba47d9
VZ
1004#endif // Win32/16
1005}
1006
8ea92b4d
WS
1007wxPowerType wxGetPowerType()
1008{
1009 // TODO
1010 return wxPOWER_UNKNOWN;
1011}
1012
1013wxBatteryState wxGetBatteryState()
1014{
1015 // TODO
1016 return wxBATTERY_UNKNOWN_STATE;
1017}
1018
b568d04f
VZ
1019// ----------------------------------------------------------------------------
1020// misc
1021// ----------------------------------------------------------------------------
1022
1023// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
9d8aca48 1024wxMemorySize wxGetFreeMemory()
2bda0e17 1025{
9d8aca48
WS
1026#if defined(__WIN64__)
1027 MEMORYSTATUSEX memStatex;
106d80ad
JS
1028 memStatex.dwLength = sizeof (memStatex);
1029 ::GlobalMemoryStatusEx (&memStatex);
1030 return (wxMemorySize)memStatex.ullAvailPhys;
2b7295c9 1031#else /* if defined(__WIN32__) */
b568d04f
VZ
1032 MEMORYSTATUS memStatus;
1033 memStatus.dwLength = sizeof(MEMORYSTATUS);
2b7295c9 1034 ::GlobalMemoryStatus(&memStatus);
9d8aca48 1035 return (wxMemorySize)memStatus.dwAvailPhys;
b568d04f 1036#endif
2bda0e17
KB
1037}
1038
c1cb4153
VZ
1039unsigned long wxGetProcessId()
1040{
c1cb4153 1041 return ::GetCurrentProcessId();
c1cb4153
VZ
1042}
1043
2bda0e17 1044// Emit a beeeeeep
634903fd 1045void wxBell()
2bda0e17 1046{
b568d04f 1047 ::MessageBeep((UINT)-1); // default sound
2bda0e17
KB
1048}
1049
c50a4038
VZ
1050bool wxIsDebuggerRunning()
1051{
532d575b 1052#if wxUSE_DYNLIB_CLASS
c50a4038
VZ
1053 // IsDebuggerPresent() is not available under Win95, so load it dynamically
1054 wxDynamicLibrary dll(_T("kernel32.dll"), wxDL_VERBATIM);
1055
1056 typedef BOOL (WINAPI *IsDebuggerPresent_t)();
1057 if ( !dll.HasSymbol(_T("IsDebuggerPresent")) )
1058 {
1059 // no way to know, assume no
1060 return false;
1061 }
1062
1063 return (*(IsDebuggerPresent_t)dll.GetSymbol(_T("IsDebuggerPresent")))() != 0;
532d575b
WS
1064#else
1065 return false;
1066#endif
c50a4038
VZ
1067}
1068
1069// ----------------------------------------------------------------------------
1070// OS version
1071// ----------------------------------------------------------------------------
1072
bdc72a22 1073wxString wxGetOsDescription()
2bda0e17 1074{
bdc72a22
VZ
1075 wxString str;
1076
1077 OSVERSIONINFO info;
1078 wxZeroMemory(info);
1079
1080 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1081 if ( ::GetVersionEx(&info) )
1082 {
1083 switch ( info.dwPlatformId )
1084 {
1085 case VER_PLATFORM_WIN32s:
1086 str = _("Win32s on Windows 3.1");
1087 break;
1088
1089 case VER_PLATFORM_WIN32_WINDOWS:
db1d0193
VZ
1090 switch (info.dwMinorVersion)
1091 {
1092 case 0:
1093 if ( info.szCSDVersion[1] == 'B' ||
1094 info.szCSDVersion[1] == 'C' )
1095 {
1096 str = _("Windows 95 OSR2");
1097 }
1098 else
1099 {
1100 str = _("Windows 95");
1101 }
1102 break;
1103 case 10:
1104 if ( info.szCSDVersion[1] == 'B' ||
1105 info.szCSDVersion[1] == 'C' )
1106 {
1107 str = _("Windows 98 SE");
1108 }
1109 else
1110 {
1111 str = _("Windows 98");
1112 }
1113 break;
1114 case 90:
1115 str = _("Windows ME");
1116 break;
1117 default:
1118 str.Printf(_("Windows 9x (%d.%d)"),
1119 info.dwMajorVersion,
1120 info.dwMinorVersion);
1121 break;
1122 }
bdc72a22
VZ
1123 if ( !wxIsEmpty(info.szCSDVersion) )
1124 {
1125 str << _T(" (") << info.szCSDVersion << _T(')');
1126 }
1127 break;
1128
1129 case VER_PLATFORM_WIN32_NT:
db1d0193
VZ
1130 if ( info.dwMajorVersion == 5 )
1131 {
1132 switch ( info.dwMinorVersion )
1133 {
1134 case 0:
1135 str.Printf(_("Windows 2000 (build %lu"),
1136 info.dwBuildNumber);
1137 break;
1138 case 1:
1139 str.Printf(_("Windows XP (build %lu"),
1140 info.dwBuildNumber);
1141 break;
1142 case 2:
1143 str.Printf(_("Windows Server 2003 (build %lu"),
1144 info.dwBuildNumber);
1145 break;
1146 }
1147 }
1148 if ( wxIsEmpty(str) )
1149 {
1150 str.Printf(_("Windows NT %lu.%lu (build %lu"),
bdc72a22
VZ
1151 info.dwMajorVersion,
1152 info.dwMinorVersion,
1153 info.dwBuildNumber);
db1d0193 1154 }
bdc72a22
VZ
1155 if ( !wxIsEmpty(info.szCSDVersion) )
1156 {
1157 str << _T(", ") << info.szCSDVersion;
1158 }
1159 str << _T(')');
1160 break;
1161 }
1162 }
1163 else
1164 {
1165 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
1166 }
1167
1168 return str;
bdc72a22 1169}
6f65e337 1170
324899f6 1171wxToolkitInfo& wxAppTraits::GetToolkitInfo()
bdc72a22 1172{
2739d4f0
VZ
1173 // cache the version info, it's not going to change
1174 //
1175 // NB: this is MT-safe, we may use these static vars from different threads
1176 // but as they always have the same value it doesn't matter
1177 static int s_ver = -1,
1178 s_major = -1,
1179 s_minor = -1;
96c21216 1180
2739d4f0 1181 if ( s_ver == -1 )
bdc72a22 1182 {
6af638ed
VS
1183 OSVERSIONINFO info;
1184 wxZeroMemory(info);
ec5d7799 1185
2739d4f0 1186 s_ver = wxWINDOWS;
6af638ed
VS
1187 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1188 if ( ::GetVersionEx(&info) )
bdc72a22 1189 {
2739d4f0
VZ
1190 s_major = info.dwMajorVersion;
1191 s_minor = info.dwMinorVersion;
bdc72a22 1192
c75d190a
JS
1193#ifdef __SMARTPHONE__
1194 s_ver = wxWINDOWS_SMARTPHONE;
1195#elif defined(__POCKETPC__)
1196 s_ver = wxWINDOWS_POCKETPC;
1197#else
6af638ed
VS
1198 switch ( info.dwPlatformId )
1199 {
1200 case VER_PLATFORM_WIN32s:
2739d4f0 1201 s_ver = wxWIN32S;
6af638ed 1202 break;
bdc72a22 1203
6af638ed 1204 case VER_PLATFORM_WIN32_WINDOWS:
2739d4f0 1205 s_ver = wxWIN95;
6af638ed
VS
1206 break;
1207
1208 case VER_PLATFORM_WIN32_NT:
2739d4f0 1209 s_ver = wxWINDOWS_NT;
6af638ed 1210 break;
1f867767 1211#ifdef __WXWINCE__
f07dc2e2
JS
1212 case VER_PLATFORM_WIN32_CE:
1213 s_ver = wxWINDOWS_CE;
27b2dd53 1214#endif
6af638ed 1215 }
27b2dd53 1216#endif
bdc72a22
VZ
1217 }
1218 }
1219
eccd1992 1220 static wxToolkitInfo info;
a8eaaeb2
VS
1221 info.versionMajor = s_major;
1222 info.versionMinor = s_minor;
1223 info.os = s_ver;
1224 info.name = _T("wxBase");
324899f6 1225 return info;
2bda0e17
KB
1226}
1227
4c5da5e4
VZ
1228wxWinVersion wxGetWinVersion()
1229{
1230 int verMaj,
1231 verMin;
1232 switch ( wxGetOsVersion(&verMaj, &verMin) )
1233 {
1234 case wxWIN95:
1235 if ( verMaj == 4 )
1236 {
1237 switch ( verMin )
1238 {
1239 case 0:
1240 return wxWinVersion_95;
1241
1242 case 10:
1243 return wxWinVersion_98;
1244
1245 case 90:
1246 return wxWinVersion_ME;
1247 }
1248 }
1249 break;
1250
1251 case wxWINDOWS_NT:
1252 switch ( verMaj )
1253 {
1254 case 3:
1255 return wxWinVersion_NT3;
1256
1257 case 4:
1258 return wxWinVersion_NT4;
1259
1260 case 5:
1261 switch ( verMin )
1262 {
1263 case 0:
1264 return wxWinVersion_2000;
1265
1266 case 1:
1267 return wxWinVersion_XP;
1268
1269 case 2:
1270 return wxWinVersion_2003;
1271 }
1272 break;
1273
1274 case 6:
1275 return wxWinVersion_NT6;
1276 }
1277 break;
1278
1279 }
1280
1281 return wxWinVersion_Unknown;
1282}
1283
b568d04f
VZ
1284// ----------------------------------------------------------------------------
1285// sleep functions
1286// ----------------------------------------------------------------------------
1287
08873d36 1288void wxMilliSleep(unsigned long milliseconds)
b568d04f
VZ
1289{
1290 ::Sleep(milliseconds);
1291}
1292
08873d36
VZ
1293void wxMicroSleep(unsigned long microseconds)
1294{
1295 wxMilliSleep(microseconds/1000);
1296}
1297
b568d04f
VZ
1298void wxSleep(int nSecs)
1299{
08873d36 1300 wxMilliSleep(1000*nSecs);
b568d04f
VZ
1301}
1302
b568d04f 1303// ----------------------------------------------------------------------------
e2478fde 1304// font encoding <-> Win32 codepage conversion functions
b568d04f
VZ
1305// ----------------------------------------------------------------------------
1306
bddd7a8d 1307extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding)
b568d04f 1308{
e2478fde 1309 switch ( encoding )
b568d04f 1310 {
e2478fde
VZ
1311 // although this function is supposed to return an exact match, do do
1312 // some mappings here for the most common case of "standard" encoding
1313 case wxFONTENCODING_SYSTEM:
1314 return DEFAULT_CHARSET;
2bda0e17 1315
e2478fde
VZ
1316 case wxFONTENCODING_ISO8859_1:
1317 case wxFONTENCODING_ISO8859_15:
1318 case wxFONTENCODING_CP1252:
1319 return ANSI_CHARSET;
2bda0e17 1320
e2478fde
VZ
1321#if !defined(__WXMICROWIN__)
1322 // The following four fonts are multi-byte charsets
1323 case wxFONTENCODING_CP932:
1324 return SHIFTJIS_CHARSET;
2bda0e17 1325
e2478fde
VZ
1326 case wxFONTENCODING_CP936:
1327 return GB2312_CHARSET;
2bda0e17 1328
90805c24 1329#ifndef __WXWINCE__
e2478fde
VZ
1330 case wxFONTENCODING_CP949:
1331 return HANGUL_CHARSET;
90805c24 1332#endif
634903fd 1333
e2478fde
VZ
1334 case wxFONTENCODING_CP950:
1335 return CHINESEBIG5_CHARSET;
2bda0e17 1336
e2478fde
VZ
1337 // The rest are single byte encodings
1338 case wxFONTENCODING_CP1250:
1339 return EASTEUROPE_CHARSET;
bfbd6dc1 1340
e2478fde
VZ
1341 case wxFONTENCODING_CP1251:
1342 return RUSSIAN_CHARSET;
2bda0e17 1343
e2478fde
VZ
1344 case wxFONTENCODING_CP1253:
1345 return GREEK_CHARSET;
b07135ba 1346
e2478fde
VZ
1347 case wxFONTENCODING_CP1254:
1348 return TURKISH_CHARSET;
2bda0e17 1349
e2478fde
VZ
1350 case wxFONTENCODING_CP1255:
1351 return HEBREW_CHARSET;
2bda0e17 1352
e2478fde
VZ
1353 case wxFONTENCODING_CP1256:
1354 return ARABIC_CHARSET;
b568d04f 1355
e2478fde
VZ
1356 case wxFONTENCODING_CP1257:
1357 return BALTIC_CHARSET;
634903fd 1358
e2478fde
VZ
1359 case wxFONTENCODING_CP874:
1360 return THAI_CHARSET;
5acc0d5f 1361#endif // !__WXMICROWIN__
cc2b7472 1362
e2478fde
VZ
1363 case wxFONTENCODING_CP437:
1364 return OEM_CHARSET;
ea28b885
VZ
1365
1366 default:
1367 // no way to translate this encoding into a Windows charset
69557827 1368 return -1;
e2478fde 1369 }
373658eb 1370}
7f555861 1371
e2478fde
VZ
1372// we have 2 versions of wxCharsetToCodepage(): the old one which directly
1373// looks up the vlaues in the registry and the new one which is more
1374// politically correct and has more chances to work on other Windows versions
1375// as well but the old version is still needed for !wxUSE_FONTMAP case
1376#if wxUSE_FONTMAP
c030b70f 1377
373658eb 1378#include "wx/fontmap.h"
c030b70f 1379
bddd7a8d 1380extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding)
c030b70f 1381{
b169661f
RR
1382 // There don't seem to be symbolic names for
1383 // these under Windows so I just copied the
1384 // values from MSDN.
04a18b0d 1385
fa78de82 1386 unsigned int ret;
04a18b0d 1387
b169661f 1388 switch (encoding)
373658eb 1389 {
fa78de82
RR
1390 case wxFONTENCODING_ISO8859_1: ret = 28591; break;
1391 case wxFONTENCODING_ISO8859_2: ret = 28592; break;
1392 case wxFONTENCODING_ISO8859_3: ret = 28593; break;
1393 case wxFONTENCODING_ISO8859_4: ret = 28594; break;
1394 case wxFONTENCODING_ISO8859_5: ret = 28595; break;
1395 case wxFONTENCODING_ISO8859_6: ret = 28596; break;
1396 case wxFONTENCODING_ISO8859_7: ret = 28597; break;
1397 case wxFONTENCODING_ISO8859_8: ret = 28598; break;
1398 case wxFONTENCODING_ISO8859_9: ret = 28599; break;
1399 case wxFONTENCODING_ISO8859_10: ret = 28600; break;
1400 case wxFONTENCODING_ISO8859_11: ret = 28601; break;
b169661f 1401 // case wxFONTENCODING_ISO8859_12, // doesn't exist currently, but put it
04a18b0d 1402 case wxFONTENCODING_ISO8859_13: ret = 28603; break;
fa78de82
RR
1403 case wxFONTENCODING_ISO8859_14: ret = 28604; break;
1404 case wxFONTENCODING_ISO8859_15: ret = 28605; break;
1405 case wxFONTENCODING_KOI8: ret = 20866; break;
1406 case wxFONTENCODING_KOI8_U: ret = 21866; break;
1407 case wxFONTENCODING_CP437: ret = 437; break;
1408 case wxFONTENCODING_CP850: ret = 850; break;
1409 case wxFONTENCODING_CP852: ret = 852; break;
1410 case wxFONTENCODING_CP855: ret = 855; break;
1411 case wxFONTENCODING_CP866: ret = 866; break;
1412 case wxFONTENCODING_CP874: ret = 874; break;
1413 case wxFONTENCODING_CP932: ret = 932; break;
1414 case wxFONTENCODING_CP936: ret = 936; break;
1415 case wxFONTENCODING_CP949: ret = 949; break;
1416 case wxFONTENCODING_CP950: ret = 950; break;
1417 case wxFONTENCODING_CP1250: ret = 1250; break;
1418 case wxFONTENCODING_CP1251: ret = 1251; break;
1419 case wxFONTENCODING_CP1252: ret = 1252; break;
1420 case wxFONTENCODING_CP1253: ret = 1253; break;
1421 case wxFONTENCODING_CP1254: ret = 1254; break;
1422 case wxFONTENCODING_CP1255: ret = 1255; break;
1423 case wxFONTENCODING_CP1256: ret = 1256; break;
1424 case wxFONTENCODING_CP1257: ret = 1257; break;
a50ca843 1425 case wxFONTENCODING_EUC_JP: ret = 20932; break;
fa78de82
RR
1426 case wxFONTENCODING_MACROMAN: ret = 10000; break;
1427 case wxFONTENCODING_MACJAPANESE: ret = 10001; break;
1428 case wxFONTENCODING_MACCHINESETRAD: ret = 10002; break;
1429 case wxFONTENCODING_MACKOREAN: ret = 10003; break;
1430 case wxFONTENCODING_MACARABIC: ret = 10004; break;
1431 case wxFONTENCODING_MACHEBREW: ret = 10005; break;
1432 case wxFONTENCODING_MACGREEK: ret = 10006; break;
1433 case wxFONTENCODING_MACCYRILLIC: ret = 10007; break;
1434 case wxFONTENCODING_MACTHAI: ret = 10021; break;
1435 case wxFONTENCODING_MACCHINESESIMP: ret = 10008; break;
1436 case wxFONTENCODING_MACCENTRALEUR: ret = 10029; break;
1437 case wxFONTENCODING_MACCROATIAN: ret = 10082; break;
1438 case wxFONTENCODING_MACICELANDIC: ret = 10079; break;
1439 case wxFONTENCODING_MACROMANIAN: ret = 10009; break;
1440 case wxFONTENCODING_UTF7: ret = 65000; break;
1441 case wxFONTENCODING_UTF8: ret = 65001; break;
1442 default: return -1;
373658eb 1443 }
04a18b0d 1444
fa78de82
RR
1445 if (::IsValidCodePage(ret) == 0)
1446 return -1;
04a18b0d 1447
fa78de82
RR
1448 CPINFO info;
1449 if (::GetCPInfo(ret, &info) == 0)
1450 return -1;
373658eb 1451
fa78de82 1452 return (long) ret;
c030b70f 1453}
373658eb 1454
373658eb 1455extern long wxCharsetToCodepage(const wxChar *name)
c030b70f 1456{
373658eb
VZ
1457 // first get the font encoding for this charset
1458 if ( !name )
1459 return -1;
1460
267e11c5 1461 wxFontEncoding enc = wxFontMapperBase::Get()->CharsetToEncoding(name, false);
373658eb
VZ
1462 if ( enc == wxFONTENCODING_SYSTEM )
1463 return -1;
1464
1465 // the use the helper function
1466 return wxEncodingToCodepage(enc);
c030b70f 1467}
c030b70f 1468
e2478fde 1469#else // !wxUSE_FONTMAP
373658eb
VZ
1470
1471#include "wx/msw/registry.h"
1472
1473// this should work if Internet Exploiter is installed
1474extern long wxCharsetToCodepage(const wxChar *name)
04ef50df 1475{
373658eb
VZ
1476 if (!name)
1477 return GetACP();
1478
e2478fde 1479 long CP = -1;
373658eb 1480
e2478fde 1481 wxString path(wxT("MIME\\Database\\Charset\\"));
373658eb 1482 wxString cn(name);
373658eb 1483
e2478fde
VZ
1484 // follow the alias loop
1485 for ( ;; )
1486 {
1487 wxRegKey key(wxRegKey::HKCR, path + cn);
1488
1489 if (!key.Exists())
1490 break;
373658eb
VZ
1491
1492 // two cases: either there's an AliasForCharset string,
1493 // or there are Codepage and InternetEncoding dwords.
1494 // The InternetEncoding gives us the actual encoding,
1495 // the Codepage just says which Windows character set to
1496 // use when displaying the data.
1497 if (key.HasValue(wxT("InternetEncoding")) &&
e2478fde
VZ
1498 key.QueryValue(wxT("InternetEncoding"), &CP))
1499 break;
373658eb
VZ
1500
1501 // no encoding, see if it's an alias
1502 if (!key.HasValue(wxT("AliasForCharset")) ||
e2478fde
VZ
1503 !key.QueryValue(wxT("AliasForCharset"), cn))
1504 break;
1505 }
373658eb
VZ
1506
1507 return CP;
04ef50df 1508}
373658eb 1509
e2478fde 1510#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
c030b70f 1511
eccd1992
VZ
1512/*
1513 Creates a hidden window with supplied window proc registering the class for
1514 it if necesssary (i.e. the first time only). Caller is responsible for
1515 destroying the window and unregistering the class (note that this must be
77ffb593 1516 done because wxWidgets may be used as a DLL and so may be loaded/unloaded
eccd1992
VZ
1517 multiple times into/from the same process so we cna't rely on automatic
1518 Windows class unregistration).
1519
1520 pclassname is a pointer to a caller stored classname, which must initially be
3103e8a9 1521 NULL. classname is the desired wndclass classname. If function successfully
eccd1992
VZ
1522 registers the class, pclassname will be set to classname.
1523 */
487f2d58 1524extern "C" WXDLLIMPEXP_BASE HWND
eccd1992
VZ
1525wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc)
1526{
1527 wxCHECK_MSG( classname && pclassname && wndproc, NULL,
1528 _T("NULL parameter in wxCreateHiddenWindow") );
1529
1530 // register the class fi we need to first
1531 if ( *pclassname == NULL )
1532 {
1533 WNDCLASS wndclass;
1534 wxZeroMemory(wndclass);
1535
1536 wndclass.lpfnWndProc = wndproc;
1537 wndclass.hInstance = wxGetInstance();
1538 wndclass.lpszClassName = classname;
1539
1540 if ( !::RegisterClass(&wndclass) )
1541 {
1542 wxLogLastError(wxT("RegisterClass() in wxCreateHiddenWindow"));
1543
1544 return NULL;
1545 }
1546
1547 *pclassname = classname;
1548 }
1549
1550 // next create the window
1551 HWND hwnd = ::CreateWindow
1552 (
1553 *pclassname,
1554 NULL,
1555 0, 0, 0, 0,
1556 0,
1557 (HWND) NULL,
1558 (HMENU)NULL,
1559 wxGetInstance(),
1560 (LPVOID) NULL
1561 );
1562
1563 if ( !hwnd )
1564 {
1565 wxLogLastError(wxT("CreateWindow() in wxCreateHiddenWindow"));
1566 }
1567
1568 return hwnd;
1569}