1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
34 #include "wx/apptrait.h"
35 #include "wx/dynload.h"
37 #include "wx/msw/private.h" // includes <windows.h>
38 #include "wx/msw/missing.h" // CHARSET_HANGUL
40 #if defined(__GNUWIN32_OLD__) || defined(__WXWINCE__)
41 // apparently we need to include winsock.h to get WSADATA and other stuff
42 // used in wxGetFullHostName() with the old mingw32 versions
48 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
56 #if defined(__CYGWIN__)
57 #include <sys/unistd.h>
59 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
62 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
63 // this (3.1 I believe) and how to test for it.
64 // If this works for Borland 4.0 as well, then no worries.
68 // VZ: there is some code using NetXXX() functions to get the full user name:
69 // I don't think it's a good idea because they don't work under Win95 and
70 // seem to return the same as wxGetUserId() under NT. If you really want
71 // to use them, just #define USE_NET_API
78 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
89 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // In the WIN.INI file
99 static const wxChar WX_SECTION
[] = wxT("wxWindows");
100 static const wxChar eUSERNAME
[] = wxT("UserName");
102 // these are only used under Win16
103 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
104 static const wxChar eHOSTNAME
[] = wxT("HostName");
105 static const wxChar eUSERID
[] = wxT("UserId");
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
113 // get host name and related
114 // ----------------------------------------------------------------------------
116 // Get hostname only (without domain name)
117 bool wxGetHostName(wxChar
*buf
, int maxSize
)
119 #if defined(__WXWINCE__)
121 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
122 DWORD nSize
= maxSize
;
123 if ( !::GetComputerName(buf
, &nSize
) )
125 wxLogLastError(wxT("GetComputerName"));
133 const wxChar
*default_host
= wxT("noname");
135 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
136 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
138 wxStrncpy(buf
, sysname
, maxSize
- 1);
139 buf
[maxSize
] = wxT('\0');
140 return *buf
? TRUE
: FALSE
;
144 // get full hostname (with domain name if possible)
145 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
147 #ifndef __WXMICROWIN__
148 // TODO should use GetComputerNameEx() when available
150 // we don't want to always link with Winsock DLL as we might not use it at
151 // all, so load it dynamically here if needed
152 wxDynamicLibrary
dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM
);
153 if ( dllWinsock
.IsLoaded() )
155 typedef int (PASCAL
*WSAStartup_t
)(WORD
, WSADATA
*);
156 typedef int (PASCAL
*gethostname_t
)(char *, int);
157 typedef hostent
* (PASCAL
*gethostbyname_t
)(const char *);
158 typedef hostent
* (PASCAL
*gethostbyaddr_t
)(const char *, int , int);
159 typedef int (PASCAL
*WSACleanup_t
)(void);
161 #define LOAD_WINSOCK_FUNC(func) \
163 pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
165 LOAD_WINSOCK_FUNC(WSAStartup
);
168 if ( pfnWSAStartup
&& pfnWSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
170 LOAD_WINSOCK_FUNC(gethostname
);
173 if ( pfngethostname
)
176 if ( pfngethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
178 // gethostname() won't usually include the DNS domain name,
179 // for this we need to work a bit more
180 if ( !strchr(bufA
, '.') )
182 LOAD_WINSOCK_FUNC(gethostbyname
);
184 struct hostent
*pHostEnt
= pfngethostbyname
185 ? pfngethostbyname(bufA
)
190 // Windows will use DNS internally now
191 LOAD_WINSOCK_FUNC(gethostbyaddr
);
193 pHostEnt
= pfngethostbyaddr
194 ? pfngethostbyaddr(pHostEnt
->h_addr
,
201 host
= wxString::FromAscii(pHostEnt
->h_name
);
207 LOAD_WINSOCK_FUNC(WSACleanup
);
214 wxStrncpy(buf
, host
, maxSize
);
220 #endif // !__WXMICROWIN__
222 return wxGetHostName(buf
, maxSize
);
225 // Get user ID e.g. jacs
226 bool wxGetUserId(wxChar
*buf
, int maxSize
)
228 #if defined(__WXWINCE__)
230 #elif defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
231 DWORD nSize
= maxSize
;
232 if ( ::GetUserName(buf
, &nSize
) == 0 )
234 // actually, it does happen on Win9x if the user didn't log on
235 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
244 #else // Win16 or Win32s
246 const wxChar
*default_id
= wxT("anonymous");
248 // Can't assume we have NIS (PC-NFS) or some other ID daemon
250 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
251 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
253 // Use wxWindows configuration data (comming soon)
254 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
258 wxStrncpy(buf
, user
, maxSize
- 1);
261 return *buf
? TRUE
: FALSE
;
265 // Get user name e.g. Julian Smart
266 bool wxGetUserName(wxChar
*buf
, int maxSize
)
268 #if defined(__WXWINCE__)
270 #elif defined(USE_NET_API)
271 CHAR szUserName
[256];
272 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
275 // TODO how to get the domain name?
278 // the code is based on the MSDN example (also see KB article Q119670)
279 WCHAR wszUserName
[256]; // Unicode user name
280 WCHAR wszDomain
[256];
283 USER_INFO_2
*ui2
; // User structure
285 // Convert ANSI user name and domain to Unicode
286 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
287 wszUserName
, WXSIZEOF(wszUserName
) );
288 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
289 wszDomain
, WXSIZEOF(wszDomain
) );
291 // Get the computer name of a DC for the domain.
292 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
294 wxLogError(wxT("Can not find domain controller"));
299 // Look up the user on the DC
300 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
301 (LPWSTR
)&wszUserName
,
302 2, // level - we want USER_INFO_2
310 case NERR_InvalidComputer
:
311 wxLogError(wxT("Invalid domain controller name."));
315 case NERR_UserNotFound
:
316 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
321 wxLogSysError(wxT("Can't get information about user"));
326 // Convert the Unicode full name to ANSI
327 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
328 buf
, maxSize
, NULL
, NULL
);
333 wxLogError(wxT("Couldn't look up full user name."));
336 #else // !USE_NET_API
337 // Could use NIS, MS-Mail or other site specific programs
338 // Use wxWindows configuration data
339 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxEmptyString
, buf
, maxSize
- 1) != 0;
342 ok
= wxGetUserId(buf
, maxSize
);
347 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
354 const wxChar
* wxGetHomeDir(wxString
*pstr
)
356 wxString
& strDir
= *pstr
;
358 #if defined(__UNIX__)
359 const wxChar
*szHome
= wxGetenv("HOME");
360 if ( szHome
== NULL
) {
362 wxLogWarning(_("can't find user's HOME, using current directory."));
368 // add a trailing slash if needed
369 if ( strDir
.Last() != wxT('/') )
373 // Cygwin returns unix type path but that does not work well
374 static wxChar windowsPath
[MAX_PATH
];
375 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
376 strDir
= windowsPath
;
378 #elif defined(__WXWINCE__)
384 // If we have a valid HOME directory, as is used on many machines that
385 // have unix utilities on them, we should use that.
386 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
388 if ( szHome
!= NULL
)
392 else // no HOME, try HOMEDRIVE/PATH
394 szHome
= wxGetenv(wxT("HOMEDRIVE"));
395 if ( szHome
!= NULL
)
397 szHome
= wxGetenv(wxT("HOMEPATH"));
399 if ( szHome
!= NULL
)
403 // the idea is that under NT these variables have default values
404 // of "%systemdrive%:" and "\\". As we don't want to create our
405 // config files in the root directory of the system drive, we will
406 // create it in our program's dir. However, if the user took care
407 // to set HOMEPATH to something other than "\\", we suppose that he
408 // knows what he is doing and use the supplied value.
409 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
414 if ( strDir
.empty() )
416 // If we have a valid USERPROFILE directory, as is the case in
417 // Windows NT, 2000 and XP, we should use that as our home directory.
418 szHome
= wxGetenv(wxT("USERPROFILE"));
420 if ( szHome
!= NULL
)
424 if ( !strDir
.empty() )
426 return strDir
.c_str();
428 //else: fall back to the prograrm directory
430 // Win16 has no idea about home, so use the executable directory instead
433 // 260 was taken from windef.h
439 ::GetModuleFileName(::GetModuleHandle(NULL
),
440 wxStringBuffer(strPath
, MAX_PATH
), MAX_PATH
);
442 // extract the dir name
443 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
447 return strDir
.c_str();
450 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
452 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
453 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
454 // static buffer and sometimes I don't even know what.
455 static wxString s_home
;
457 return (wxChar
*)wxGetHomeDir(&s_home
);
460 bool wxDirExists(const wxString
& dir
)
462 #ifdef __WXMICROWIN__
463 return wxPathExist(dir
);
464 #elif defined(__WIN32__)
465 DWORD attribs
= GetFileAttributes(dir
);
466 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
469 struct ffblk fileInfo
;
471 struct find_t fileInfo
;
473 // In Borland findfirst has a different argument
474 // ordering from _dos_findfirst. But _dos_findfirst
475 // _should_ be ok in both MS and Borland... why not?
477 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
478 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
480 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
481 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
486 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
494 // old w32api don't have ULARGE_INTEGER
495 #if defined(__WIN32__) && \
496 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
497 // GetDiskFreeSpaceEx() is not available under original Win95, check for
499 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
505 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
507 ::GetModuleHandle(_T("kernel32.dll")),
509 "GetDiskFreeSpaceExW"
511 "GetDiskFreeSpaceExA"
515 if ( pGetDiskFreeSpaceEx
)
517 ULARGE_INTEGER bytesFree
, bytesTotal
;
519 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
520 if ( !pGetDiskFreeSpaceEx(path
,
525 wxLogLastError(_T("GetDiskFreeSpaceEx"));
530 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
531 // two 32 bit fields which may be or may be not named - try to make it
532 // compile in all cases
533 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
540 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
545 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
551 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
552 // should be used instead - but if it's not available, fall back on
553 // GetDiskFreeSpace() nevertheless...
555 DWORD lSectorsPerCluster
,
557 lNumberOfFreeClusters
,
558 lTotalNumberOfClusters
;
560 // FIXME: this is wrong, we should extract the root drive from path
561 // instead, but this is the job for wxFileName...
562 if ( !::GetDiskFreeSpace(path
,
565 &lNumberOfFreeClusters
,
566 &lTotalNumberOfClusters
) )
568 wxLogLastError(_T("GetDiskFreeSpace"));
573 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
574 lBytesPerCluster
*= lBytesPerSector
;
578 *pTotal
= lBytesPerCluster
;
579 *pTotal
*= lTotalNumberOfClusters
;
584 *pFree
= lBytesPerCluster
;
585 *pFree
*= lNumberOfFreeClusters
;
594 // ----------------------------------------------------------------------------
596 // ----------------------------------------------------------------------------
598 bool wxGetEnv(const wxString
& var
, wxString
*value
)
602 #elif defined(__WIN16__)
603 const wxChar
* ret
= wxGetenv(var
);
614 // first get the size of the buffer
615 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
618 // this means that there is no such variable
624 (void)::GetEnvironmentVariable(var
, wxStringBuffer(*value
, dwRet
),
632 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
634 // some compilers have putenv() or _putenv() or _wputenv() but it's better
635 // to always use Win32 function directly instead of dealing with them
636 #if defined(__WIN32__) && !defined(__WXWINCE__)
637 if ( !::SetEnvironmentVariable(var
, value
) )
639 wxLogLastError(_T("SetEnvironmentVariable"));
645 #else // no way to set env vars
650 // ----------------------------------------------------------------------------
651 // process management
652 // ----------------------------------------------------------------------------
654 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
655 struct wxFindByPidParams
657 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
659 // the HWND used to return the result
662 // the PID we're looking from
665 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
668 // wxKill helper: EnumWindows() callback which is used to find the first (top
669 // level) window belonging to the given process
670 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
673 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
675 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
676 if ( pid
== params
->pid
)
678 // remember the window we found
681 // return FALSE to stop the enumeration
685 // continue enumeration
689 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
691 // get the process handle to operate on
692 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
694 PROCESS_QUERY_INFORMATION
,
695 FALSE
, // not inheritable
697 if ( hProcess
== NULL
)
701 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
703 *krc
= wxKILL_ACCESS_DENIED
;
707 *krc
= wxKILL_NO_PROCESS
;
718 // kill the process forcefully returning -1 as error code
719 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
721 wxLogSysError(_("Failed to kill process %d"), pid
);
725 // this is not supposed to happen if we could open the
735 // do nothing, we just want to test for process existence
739 // any other signal means "terminate"
741 wxFindByPidParams params
;
742 params
.pid
= (DWORD
)pid
;
744 // EnumWindows() has nice semantics: it returns 0 if it found
745 // something or if an error occured and non zero if it
746 // enumerated all the window
747 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
749 // did we find any window?
752 // tell the app to close
754 // NB: this is the harshest way, the app won't have
755 // opportunity to save any files, for example, but
756 // this is probably what we want here. If not we
757 // can also use SendMesageTimeout(WM_CLOSE)
758 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
760 wxLogLastError(_T("PostMessage(WM_QUIT)"));
763 else // it was an error then
765 wxLogLastError(_T("EnumWindows"));
770 else // no windows for this PID
787 // as we wait for a short time, we can use just WaitForSingleObject()
788 // and not MsgWaitForMultipleObjects()
789 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
792 // process terminated
793 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
795 wxLogLastError(_T("GetExitCodeProcess"));
800 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
804 wxLogLastError(_T("WaitForSingleObject"));
819 // just to suppress the warnings about uninitialized variable
823 ::CloseHandle(hProcess
);
825 // the return code is the same as from Unix kill(): 0 if killed
826 // successfully or -1 on error
828 // be careful to interpret rc correctly: for wxSIGNONE we return success if
829 // the process exists, for all the other sig values -- if it doesn't
831 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
845 // Execute a program in an Interactive Shell
846 bool wxShell(const wxString
& command
)
851 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
853 shell
= (wxChar
*) wxT("\\COMMAND.COM");
863 // pass the command to execute to the command processor
864 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
867 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
871 // Shutdown or reboot the PC
872 bool wxShutdown(wxShutdownFlags wFlags
)
876 #elif defined(__WIN32__)
879 if ( wxGetOsVersion(NULL
, NULL
) == wxWINDOWS_NT
) // if is NT or 2K
881 // Get a token for this process.
883 bOK
= ::OpenProcessToken(GetCurrentProcess(),
884 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
,
888 TOKEN_PRIVILEGES tkp
;
890 // Get the LUID for the shutdown privilege.
891 ::LookupPrivilegeValue(NULL
, SE_SHUTDOWN_NAME
,
892 &tkp
.Privileges
[0].Luid
);
894 tkp
.PrivilegeCount
= 1; // one privilege to set
895 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
897 // Get the shutdown privilege for this process.
898 ::AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0,
899 (PTOKEN_PRIVILEGES
)NULL
, 0);
901 // Cannot test the return value of AdjustTokenPrivileges.
902 bOK
= ::GetLastError() == ERROR_SUCCESS
;
908 UINT flags
= EWX_SHUTDOWN
| EWX_FORCE
;
911 case wxSHUTDOWN_POWEROFF
:
912 flags
|= EWX_POWEROFF
;
915 case wxSHUTDOWN_REBOOT
:
920 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
924 bOK
= ::ExitWindowsEx(EWX_SHUTDOWN
| EWX_FORCE
| EWX_REBOOT
, 0) != 0;
933 // ----------------------------------------------------------------------------
935 // ----------------------------------------------------------------------------
937 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
938 long wxGetFreeMemory()
940 #if defined(__WIN32__) && !defined(__BORLANDC__)
941 MEMORYSTATUS memStatus
;
942 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
943 GlobalMemoryStatus(&memStatus
);
944 return memStatus
.dwAvailPhys
;
946 return (long)GetFreeSpace(0);
950 unsigned long wxGetProcessId()
953 return ::GetCurrentProcessId();
962 ::MessageBeep((UINT
)-1); // default sound
965 wxString
wxGetOsDescription()
973 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
974 if ( ::GetVersionEx(&info
) )
976 switch ( info
.dwPlatformId
)
978 case VER_PLATFORM_WIN32s
:
979 str
= _("Win32s on Windows 3.1");
982 case VER_PLATFORM_WIN32_WINDOWS
:
983 str
.Printf(_("Windows 9%c"),
984 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
985 if ( !wxIsEmpty(info
.szCSDVersion
) )
987 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
991 case VER_PLATFORM_WIN32_NT
:
992 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
996 if ( !wxIsEmpty(info
.szCSDVersion
) )
998 str
<< _T(", ") << info
.szCSDVersion
;
1006 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
1011 return _("Windows 3.1");
1015 int wxAppTraits::GetOSVersion(int *verMaj
, int *verMin
)
1017 // cache the version info, it's not going to change
1019 // NB: this is MT-safe, we may use these static vars from different threads
1020 // but as they always have the same value it doesn't matter
1021 static int s_ver
= -1,
1031 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1032 if ( ::GetVersionEx(&info
) )
1034 s_major
= info
.dwMajorVersion
;
1035 s_minor
= info
.dwMinorVersion
;
1037 switch ( info
.dwPlatformId
)
1039 case VER_PLATFORM_WIN32s
:
1043 case VER_PLATFORM_WIN32_WINDOWS
:
1047 case VER_PLATFORM_WIN32_NT
:
1048 s_ver
= wxWINDOWS_NT
;
1051 case VER_PLATFORM_WIN32_CE
:
1052 s_ver
= wxWINDOWS_CE
;
1067 // ----------------------------------------------------------------------------
1069 // ----------------------------------------------------------------------------
1071 void wxUsleep(unsigned long milliseconds
)
1073 ::Sleep(milliseconds
);
1076 void wxSleep(int nSecs
)
1078 wxUsleep(1000*nSecs
);
1081 // ----------------------------------------------------------------------------
1082 // font encoding <-> Win32 codepage conversion functions
1083 // ----------------------------------------------------------------------------
1085 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
)
1089 // although this function is supposed to return an exact match, do do
1090 // some mappings here for the most common case of "standard" encoding
1091 case wxFONTENCODING_SYSTEM
:
1092 return DEFAULT_CHARSET
;
1094 case wxFONTENCODING_ISO8859_1
:
1095 case wxFONTENCODING_ISO8859_15
:
1096 case wxFONTENCODING_CP1252
:
1097 return ANSI_CHARSET
;
1099 #if !defined(__WXMICROWIN__)
1100 // The following four fonts are multi-byte charsets
1101 case wxFONTENCODING_CP932
:
1102 return SHIFTJIS_CHARSET
;
1104 case wxFONTENCODING_CP936
:
1105 return GB2312_CHARSET
;
1107 case wxFONTENCODING_CP949
:
1108 return HANGUL_CHARSET
;
1110 case wxFONTENCODING_CP950
:
1111 return CHINESEBIG5_CHARSET
;
1113 // The rest are single byte encodings
1114 case wxFONTENCODING_CP1250
:
1115 return EASTEUROPE_CHARSET
;
1117 case wxFONTENCODING_CP1251
:
1118 return RUSSIAN_CHARSET
;
1120 case wxFONTENCODING_CP1253
:
1121 return GREEK_CHARSET
;
1123 case wxFONTENCODING_CP1254
:
1124 return TURKISH_CHARSET
;
1126 case wxFONTENCODING_CP1255
:
1127 return HEBREW_CHARSET
;
1129 case wxFONTENCODING_CP1256
:
1130 return ARABIC_CHARSET
;
1132 case wxFONTENCODING_CP1257
:
1133 return BALTIC_CHARSET
;
1135 case wxFONTENCODING_CP874
:
1136 return THAI_CHARSET
;
1137 #endif // !__WXMICROWIN__
1139 case wxFONTENCODING_CP437
:
1143 // no way to translate this encoding into a Windows charset
1148 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1149 // looks up the vlaues in the registry and the new one which is more
1150 // politically correct and has more chances to work on other Windows versions
1151 // as well but the old version is still needed for !wxUSE_FONTMAP case
1154 #include "wx/fontmap.h"
1156 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
)
1158 // translate encoding into the Windows CHARSET
1159 long charset
= wxEncodingToCharset(encoding
);
1160 if ( charset
== -1 )
1163 // translate CHARSET to code page
1164 CHARSETINFO csetInfo
;
1165 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1169 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1174 return csetInfo
.ciACP
;
1177 extern long wxCharsetToCodepage(const wxChar
*name
)
1179 // first get the font encoding for this charset
1183 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1184 if ( enc
== wxFONTENCODING_SYSTEM
)
1187 // the use the helper function
1188 return wxEncodingToCodepage(enc
);
1191 #else // !wxUSE_FONTMAP
1193 #include "wx/msw/registry.h"
1195 // this should work if Internet Exploiter is installed
1196 extern long wxCharsetToCodepage(const wxChar
*name
)
1203 wxString
path(wxT("MIME\\Database\\Charset\\"));
1206 // follow the alias loop
1209 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1214 // two cases: either there's an AliasForCharset string,
1215 // or there are Codepage and InternetEncoding dwords.
1216 // The InternetEncoding gives us the actual encoding,
1217 // the Codepage just says which Windows character set to
1218 // use when displaying the data.
1219 if (key
.HasValue(wxT("InternetEncoding")) &&
1220 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1223 // no encoding, see if it's an alias
1224 if (!key
.HasValue(wxT("AliasForCharset")) ||
1225 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1232 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP