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