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