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/msw/private.h" // includes <windows.h>
36 #ifdef __GNUWIN32_OLD__
37 // apparently we need to include winsock.h to get WSADATA and other stuff
38 // used in wxGetFullHostName() with the old mingw32 versions
44 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__)
52 #if defined(__CYGWIN__)
53 #include <sys/unistd.h>
55 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
58 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
59 // this (3.1 I believe) and how to test for it.
60 // If this works for Borland 4.0 as well, then no worries.
64 // VZ: there is some code using NetXXX() functions to get the full user name:
65 // I don't think it's a good idea because they don't work under Win95 and
66 // seem to return the same as wxGetUserId() under NT. If you really want
67 // to use them, just #define USE_NET_API
74 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
85 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 // In the WIN.INI file
95 static const wxChar WX_SECTION
[] = wxT("wxWindows");
96 static const wxChar eUSERNAME
[] = wxT("UserName");
98 // these are only used under Win16
99 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
100 static const wxChar eHOSTNAME
[] = wxT("HostName");
101 static const wxChar eUSERID
[] = wxT("UserId");
104 // ============================================================================
106 // ============================================================================
108 // ----------------------------------------------------------------------------
109 // get host name and related
110 // ----------------------------------------------------------------------------
112 // Get hostname only (without domain name)
113 bool wxGetHostName(wxChar
*buf
, int maxSize
)
115 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
116 DWORD nSize
= maxSize
;
117 if ( !::GetComputerName(buf
, &nSize
) )
119 wxLogLastError(wxT("GetComputerName"));
127 const wxChar
*default_host
= wxT("noname");
129 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
130 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
132 wxStrncpy(buf
, sysname
, maxSize
- 1);
133 buf
[maxSize
] = wxT('\0');
134 return *buf
? TRUE
: FALSE
;
138 // get full hostname (with domain name if possible)
139 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
141 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
142 // TODO should use GetComputerNameEx() when available
144 // the idea is that if someone had set wxUSE_SOCKETS to 0 the code
145 // shouldn't use winsock.dll (a.k.a. ws2_32.dll) at all so only use this
146 // code if we link with it anyhow
150 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
154 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
156 // gethostname() won't usually include the DNS domain name, for
157 // this we need to work a bit more
158 if ( !strchr(bufA
, '.') )
160 struct hostent
*pHostEnt
= gethostbyname(bufA
);
164 // Windows will use DNS internally now
165 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, AF_INET
);
170 host
= wxString::FromAscii(pHostEnt
->h_name
);
179 wxStrncpy(buf
, host
, maxSize
);
185 #endif // wxUSE_SOCKETS
189 return wxGetHostName(buf
, maxSize
);
192 // Get user ID e.g. jacs
193 bool wxGetUserId(wxChar
*buf
, int maxSize
)
195 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
196 DWORD nSize
= maxSize
;
197 if ( ::GetUserName(buf
, &nSize
) == 0 )
199 // actually, it does happen on Win9x if the user didn't log on
200 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
209 #else // Win16 or Win32s
211 const wxChar
*default_id
= wxT("anonymous");
213 // Can't assume we have NIS (PC-NFS) or some other ID daemon
215 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
216 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
218 // Use wxWindows configuration data (comming soon)
219 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
223 wxStrncpy(buf
, user
, maxSize
- 1);
226 return *buf
? TRUE
: FALSE
;
230 // Get user name e.g. Julian Smart
231 bool wxGetUserName(wxChar
*buf
, int maxSize
)
233 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
234 extern HANDLE g_hPenWin
; // PenWindows Running?
237 // PenWindows Does have a user concept!
238 // Get the current owner of the recognizer
239 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
240 strncpy(buf
, wxBuffer
, maxSize
- 1);
246 CHAR szUserName
[256];
247 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
250 // TODO how to get the domain name?
253 // the code is based on the MSDN example (also see KB article Q119670)
254 WCHAR wszUserName
[256]; // Unicode user name
255 WCHAR wszDomain
[256];
258 USER_INFO_2
*ui2
; // User structure
260 // Convert ANSI user name and domain to Unicode
261 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
262 wszUserName
, WXSIZEOF(wszUserName
) );
263 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
264 wszDomain
, WXSIZEOF(wszDomain
) );
266 // Get the computer name of a DC for the domain.
267 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
269 wxLogError(wxT("Can not find domain controller"));
274 // Look up the user on the DC
275 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
276 (LPWSTR
)&wszUserName
,
277 2, // level - we want USER_INFO_2
285 case NERR_InvalidComputer
:
286 wxLogError(wxT("Invalid domain controller name."));
290 case NERR_UserNotFound
:
291 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
296 wxLogSysError(wxT("Can't get information about user"));
301 // Convert the Unicode full name to ANSI
302 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
303 buf
, maxSize
, NULL
, NULL
);
308 wxLogError(wxT("Couldn't look up full user name."));
311 #else // !USE_NET_API
312 // Could use NIS, MS-Mail or other site specific programs
313 // Use wxWindows configuration data
314 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxT(""), buf
, maxSize
- 1) != 0;
317 ok
= wxGetUserId(buf
, maxSize
);
322 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
330 const wxChar
* wxGetHomeDir(wxString
*pstr
)
332 wxString
& strDir
= *pstr
;
334 #if defined(__UNIX__)
335 const wxChar
*szHome
= wxGetenv("HOME");
336 if ( szHome
== NULL
) {
338 wxLogWarning(_("can't find user's HOME, using current directory."));
344 // add a trailing slash if needed
345 if ( strDir
.Last() != wxT('/') )
349 // Cygwin returns unix type path but that does not work well
350 static wxChar windowsPath
[MAX_PATH
];
351 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
352 strDir
= windowsPath
;
358 // If we have a valid HOME directory, as is used on many machines that
359 // have unix utilities on them, we should use that.
360 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
362 if ( szHome
!= NULL
)
366 else // no HOME, try HOMEDRIVE/PATH
368 szHome
= wxGetenv(wxT("HOMEDRIVE"));
369 if ( szHome
!= NULL
)
371 szHome
= wxGetenv(wxT("HOMEPATH"));
373 if ( szHome
!= NULL
)
377 // the idea is that under NT these variables have default values
378 // of "%systemdrive%:" and "\\". As we don't want to create our
379 // config files in the root directory of the system drive, we will
380 // create it in our program's dir. However, if the user took care
381 // to set HOMEPATH to something other than "\\", we suppose that he
382 // knows what he is doing and use the supplied value.
383 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
388 if ( strDir
.empty() )
390 // If we have a valid USERPROFILE directory, as is the case in
391 // Windows NT, 2000 and XP, we should use that as our home directory.
392 szHome
= wxGetenv(wxT("USERPROFILE"));
394 if ( szHome
!= NULL
)
398 if ( !strDir
.empty() )
400 return strDir
.c_str();
402 //else: fall back to the prograrm directory
404 // Win16 has no idea about home, so use the executable directory instead
407 // 260 was taken from windef.h
413 ::GetModuleFileName(::GetModuleHandle(NULL
),
414 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
415 strPath
.UngetWriteBuf();
417 // extract the dir name
418 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
422 return strDir
.c_str();
425 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
427 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
428 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
429 // static buffer and sometimes I don't even know what.
430 static wxString s_home
;
432 return (wxChar
*)wxGetHomeDir(&s_home
);
435 bool wxDirExists(const wxString
& dir
)
437 #ifdef __WXMICROWIN__
438 return wxPathExist(dir
);
439 #elif defined(__WIN32__)
440 DWORD attribs
= GetFileAttributes(dir
);
441 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
444 struct ffblk fileInfo
;
446 struct find_t fileInfo
;
448 // In Borland findfirst has a different argument
449 // ordering from _dos_findfirst. But _dos_findfirst
450 // _should_ be ok in both MS and Borland... why not?
452 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
453 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
455 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
456 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
461 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
466 // old w32api don't have ULARGE_INTEGER
467 #if defined(__WIN32__) && \
468 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
469 // GetDiskFreeSpaceEx() is not available under original Win95, check for
471 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
477 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
479 ::GetModuleHandle(_T("kernel32.dll")),
481 "GetDiskFreeSpaceExW"
483 "GetDiskFreeSpaceExA"
487 if ( pGetDiskFreeSpaceEx
)
489 ULARGE_INTEGER bytesFree
, bytesTotal
;
491 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
492 if ( !pGetDiskFreeSpaceEx(path
,
497 wxLogLastError(_T("GetDiskFreeSpaceEx"));
502 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
503 // two 32 bit fields which may be or may be not named - try to make it
504 // compile in all cases
505 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
512 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
517 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
523 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
524 // should be used instead - but if it's not available, fall back on
525 // GetDiskFreeSpace() nevertheless...
527 DWORD lSectorsPerCluster
,
529 lNumberOfFreeClusters
,
530 lTotalNumberOfClusters
;
532 // FIXME: this is wrong, we should extract the root drive from path
533 // instead, but this is the job for wxFileName...
534 if ( !::GetDiskFreeSpace(path
,
537 &lNumberOfFreeClusters
,
538 &lTotalNumberOfClusters
) )
540 wxLogLastError(_T("GetDiskFreeSpace"));
545 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
546 lBytesPerCluster
*= lBytesPerSector
;
550 *pTotal
= lBytesPerCluster
;
551 *pTotal
*= lTotalNumberOfClusters
;
556 *pFree
= lBytesPerCluster
;
557 *pFree
*= lNumberOfFreeClusters
;
564 // ----------------------------------------------------------------------------
566 // ----------------------------------------------------------------------------
568 bool wxGetEnv(const wxString
& var
, wxString
*value
)
571 const wxChar
* ret
= wxGetenv(var
);
582 // first get the size of the buffer
583 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
586 // this means that there is no such variable
592 (void)::GetEnvironmentVariable(var
, value
->GetWriteBuf(dwRet
), dwRet
);
593 value
->UngetWriteBuf();
600 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
602 // some compilers have putenv() or _putenv() or _wputenv() but it's better
603 // to always use Win32 function directly instead of dealing with them
604 #if defined(__WIN32__)
605 if ( !::SetEnvironmentVariable(var
, value
) )
607 wxLogLastError(_T("SetEnvironmentVariable"));
613 #else // no way to set env vars
618 // ----------------------------------------------------------------------------
619 // process management
620 // ----------------------------------------------------------------------------
622 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
623 struct wxFindByPidParams
625 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
627 // the HWND used to return the result
630 // the PID we're looking from
633 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
636 // wxKill helper: EnumWindows() callback which is used to find the first (top
637 // level) window belonging to the given process
638 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
641 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
643 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
644 if ( pid
== params
->pid
)
646 // remember the window we found
649 // return FALSE to stop the enumeration
653 // continue enumeration
657 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
659 // get the process handle to operate on
660 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
662 PROCESS_QUERY_INFORMATION
,
663 FALSE
, // not inheritable
665 if ( hProcess
== NULL
)
669 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
671 *krc
= wxKILL_ACCESS_DENIED
;
675 *krc
= wxKILL_NO_PROCESS
;
686 // kill the process forcefully returning -1 as error code
687 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
689 wxLogSysError(_("Failed to kill process %d"), pid
);
693 // this is not supposed to happen if we could open the
703 // do nothing, we just want to test for process existence
707 // any other signal means "terminate"
709 wxFindByPidParams params
;
710 params
.pid
= (DWORD
)pid
;
712 // EnumWindows() has nice semantics: it returns 0 if it found
713 // something or if an error occured and non zero if it
714 // enumerated all the window
715 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
717 // did we find any window?
720 // tell the app to close
722 // NB: this is the harshest way, the app won't have
723 // opportunity to save any files, for example, but
724 // this is probably what we want here. If not we
725 // can also use SendMesageTimeout(WM_CLOSE)
726 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
728 wxLogLastError(_T("PostMessage(WM_QUIT)"));
731 else // it was an error then
733 wxLogLastError(_T("EnumWindows"));
738 else // no windows for this PID
755 // as we wait for a short time, we can use just WaitForSingleObject()
756 // and not MsgWaitForMultipleObjects()
757 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
760 // process terminated
761 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
763 wxLogLastError(_T("GetExitCodeProcess"));
768 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
772 wxLogLastError(_T("WaitForSingleObject"));
787 // just to suppress the warnings about uninitialized variable
791 ::CloseHandle(hProcess
);
793 // the return code is the same as from Unix kill(): 0 if killed
794 // successfully or -1 on error
796 // be careful to interpret rc correctly: for wxSIGNONE we return success if
797 // the process exists, for all the other sig values -- if it doesn't
799 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
813 // Execute a program in an Interactive Shell
814 bool wxShell(const wxString
& command
)
816 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
818 shell
= (wxChar
*) wxT("\\COMMAND.COM");
828 // pass the command to execute to the command processor
829 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
832 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
835 // Shutdown or reboot the PC
836 bool wxShutdown(wxShutdownFlags wFlags
)
841 if ( wxGetOsVersion(NULL
, NULL
) == wxWINDOWS_NT
) // if is NT or 2K
843 // Get a token for this process.
845 bOK
= ::OpenProcessToken(GetCurrentProcess(),
846 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
,
850 TOKEN_PRIVILEGES tkp
;
852 // Get the LUID for the shutdown privilege.
853 ::LookupPrivilegeValue(NULL
, SE_SHUTDOWN_NAME
,
854 &tkp
.Privileges
[0].Luid
);
856 tkp
.PrivilegeCount
= 1; // one privilege to set
857 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
859 // Get the shutdown privilege for this process.
860 ::AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0,
861 (PTOKEN_PRIVILEGES
)NULL
, 0);
863 // Cannot test the return value of AdjustTokenPrivileges.
864 bOK
= ::GetLastError() == ERROR_SUCCESS
;
870 UINT flags
= EWX_SHUTDOWN
| EWX_FORCE
;
873 case wxSHUTDOWN_POWEROFF
:
874 flags
|= EWX_POWEROFF
;
877 case wxSHUTDOWN_REBOOT
:
882 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
886 bOK
= ::ExitWindowsEx(EWX_SHUTDOWN
| EWX_FORCE
| EWX_REBOOT
, 0) != 0;
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
900 long wxGetFreeMemory()
902 #if defined(__WIN32__) && !defined(__BORLANDC__)
903 MEMORYSTATUS memStatus
;
904 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
905 GlobalMemoryStatus(&memStatus
);
906 return memStatus
.dwAvailPhys
;
908 return (long)GetFreeSpace(0);
912 unsigned long wxGetProcessId()
915 return ::GetCurrentProcessId();
924 ::MessageBeep((UINT
)-1); // default sound
927 wxString
wxGetOsDescription()
935 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
936 if ( ::GetVersionEx(&info
) )
938 switch ( info
.dwPlatformId
)
940 case VER_PLATFORM_WIN32s
:
941 str
= _("Win32s on Windows 3.1");
944 case VER_PLATFORM_WIN32_WINDOWS
:
945 str
.Printf(_("Windows 9%c"),
946 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
947 if ( !wxIsEmpty(info
.szCSDVersion
) )
949 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
953 case VER_PLATFORM_WIN32_NT
:
954 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
958 if ( !wxIsEmpty(info
.szCSDVersion
) )
960 str
<< _T(", ") << info
.szCSDVersion
;
968 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
973 return _("Windows 3.1");
977 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
979 #if defined(__WIN32__)
980 static int ver
= -1, major
= -1, minor
= -1;
988 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
989 if ( ::GetVersionEx(&info
) )
991 major
= info
.dwMajorVersion
;
992 minor
= info
.dwMinorVersion
;
994 switch ( info
.dwPlatformId
)
996 case VER_PLATFORM_WIN32s
:
1000 case VER_PLATFORM_WIN32_WINDOWS
:
1004 case VER_PLATFORM_WIN32_NT
:
1011 if (majorVsn
&& major
!= -1)
1013 if (minorVsn
&& minor
!= -1)
1018 int retValue
= wxWINDOWS
;
1019 #ifdef __WINDOWS_386__
1020 retValue
= wxWIN386
;
1022 #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
1023 extern HANDLE g_hPenWin
;
1024 retValue
= g_hPenWin
? wxPENWINDOWS
: wxWINDOWS
;
1037 // ----------------------------------------------------------------------------
1039 // ----------------------------------------------------------------------------
1041 void wxUsleep(unsigned long milliseconds
)
1043 ::Sleep(milliseconds
);
1046 void wxSleep(int nSecs
)
1048 wxUsleep(1000*nSecs
);
1051 // ----------------------------------------------------------------------------
1052 // font encoding <-> Win32 codepage conversion functions
1053 // ----------------------------------------------------------------------------
1055 extern long wxEncodingToCharset(wxFontEncoding encoding
)
1059 // although this function is supposed to return an exact match, do do
1060 // some mappings here for the most common case of "standard" encoding
1061 case wxFONTENCODING_SYSTEM
:
1062 return DEFAULT_CHARSET
;
1064 case wxFONTENCODING_ISO8859_1
:
1065 case wxFONTENCODING_ISO8859_15
:
1066 case wxFONTENCODING_CP1252
:
1067 return ANSI_CHARSET
;
1069 #if !defined(__WXMICROWIN__)
1070 // The following four fonts are multi-byte charsets
1071 case wxFONTENCODING_CP932
:
1072 return SHIFTJIS_CHARSET
;
1074 case wxFONTENCODING_CP936
:
1075 return GB2312_CHARSET
;
1077 case wxFONTENCODING_CP949
:
1078 return HANGUL_CHARSET
;
1080 case wxFONTENCODING_CP950
:
1081 return CHINESEBIG5_CHARSET
;
1083 // The rest are single byte encodings
1084 case wxFONTENCODING_CP1250
:
1085 return EASTEUROPE_CHARSET
;
1087 case wxFONTENCODING_CP1251
:
1088 return RUSSIAN_CHARSET
;
1090 case wxFONTENCODING_CP1253
:
1091 return GREEK_CHARSET
;
1093 case wxFONTENCODING_CP1254
:
1094 return TURKISH_CHARSET
;
1096 case wxFONTENCODING_CP1255
:
1097 return HEBREW_CHARSET
;
1099 case wxFONTENCODING_CP1256
:
1100 return ARABIC_CHARSET
;
1102 case wxFONTENCODING_CP1257
:
1103 return BALTIC_CHARSET
;
1105 case wxFONTENCODING_CP874
:
1106 return THAI_CHARSET
;
1107 #endif // !__WXMICROWIN__
1109 case wxFONTENCODING_CP437
:
1113 // no way to translate this encoding into a Windows charset
1117 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1118 // looks up the vlaues in the registry and the new one which is more
1119 // politically correct and has more chances to work on other Windows versions
1120 // as well but the old version is still needed for !wxUSE_FONTMAP case
1123 #include "wx/fontmap.h"
1125 extern long wxEncodingToCodepage(wxFontEncoding encoding
)
1127 // translate encoding into the Windows CHARSET
1128 long charset
= wxEncodingToCharset(encoding
);
1129 if ( charset
== -1 )
1132 // translate CHARSET to code page
1133 CHARSETINFO csetInfo
;
1134 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1138 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1143 return csetInfo
.ciACP
;
1146 extern long wxCharsetToCodepage(const wxChar
*name
)
1148 // first get the font encoding for this charset
1152 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1153 if ( enc
== wxFONTENCODING_SYSTEM
)
1156 // the use the helper function
1157 return wxEncodingToCodepage(enc
);
1160 #else // !wxUSE_FONTMAP
1162 #include "wx/msw/registry.h"
1164 // this should work if Internet Exploiter is installed
1165 extern long wxCharsetToCodepage(const wxChar
*name
)
1172 wxString
path(wxT("MIME\\Database\\Charset\\"));
1175 // follow the alias loop
1178 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1183 // two cases: either there's an AliasForCharset string,
1184 // or there are Codepage and InternetEncoding dwords.
1185 // The InternetEncoding gives us the actual encoding,
1186 // the Codepage just says which Windows character set to
1187 // use when displaying the data.
1188 if (key
.HasValue(wxT("InternetEncoding")) &&
1189 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1192 // no encoding, see if it's an alias
1193 if (!key
.HasValue(wxT("AliasForCharset")) ||
1194 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1201 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP