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"
36 #include "wx/msw/private.h" // includes <windows.h>
38 #ifdef __GNUWIN32_OLD__
39 // apparently we need to include winsock.h to get WSADATA and other stuff
40 // used in wxGetFullHostName() with the old mingw32 versions
46 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__)
54 #if defined(__CYGWIN__)
55 #include <sys/unistd.h>
57 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
60 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
61 // this (3.1 I believe) and how to test for it.
62 // If this works for Borland 4.0 as well, then no worries.
66 // VZ: there is some code using NetXXX() functions to get the full user name:
67 // I don't think it's a good idea because they don't work under Win95 and
68 // seem to return the same as wxGetUserId() under NT. If you really want
69 // to use them, just #define USE_NET_API
76 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
87 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // In the WIN.INI file
97 static const wxChar WX_SECTION
[] = wxT("wxWindows");
98 static const wxChar eUSERNAME
[] = wxT("UserName");
100 // these are only used under Win16
101 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
102 static const wxChar eHOSTNAME
[] = wxT("HostName");
103 static const wxChar eUSERID
[] = wxT("UserId");
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
111 // get host name and related
112 // ----------------------------------------------------------------------------
114 // Get hostname only (without domain name)
115 bool wxGetHostName(wxChar
*buf
, int maxSize
)
117 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
118 DWORD nSize
= maxSize
;
119 if ( !::GetComputerName(buf
, &nSize
) )
121 wxLogLastError(wxT("GetComputerName"));
129 const wxChar
*default_host
= wxT("noname");
131 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
132 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
134 wxStrncpy(buf
, sysname
, maxSize
- 1);
135 buf
[maxSize
] = wxT('\0');
136 return *buf
? TRUE
: FALSE
;
140 // get full hostname (with domain name if possible)
141 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
143 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
144 // TODO should use GetComputerNameEx() when available
146 // the idea is that if someone had set wxUSE_SOCKETS to 0 the code
147 // shouldn't use winsock.dll (a.k.a. ws2_32.dll) at all so only use this
148 // code if we link with it anyhow
152 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
156 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
158 // gethostname() won't usually include the DNS domain name, for
159 // this we need to work a bit more
160 if ( !strchr(bufA
, '.') )
162 struct hostent
*pHostEnt
= gethostbyname(bufA
);
166 // Windows will use DNS internally now
167 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, AF_INET
);
172 host
= wxString::FromAscii(pHostEnt
->h_name
);
181 wxStrncpy(buf
, host
, maxSize
);
187 #endif // wxUSE_SOCKETS
191 return wxGetHostName(buf
, maxSize
);
194 // Get user ID e.g. jacs
195 bool wxGetUserId(wxChar
*buf
, int maxSize
)
197 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
198 DWORD nSize
= maxSize
;
199 if ( ::GetUserName(buf
, &nSize
) == 0 )
201 // actually, it does happen on Win9x if the user didn't log on
202 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
211 #else // Win16 or Win32s
213 const wxChar
*default_id
= wxT("anonymous");
215 // Can't assume we have NIS (PC-NFS) or some other ID daemon
217 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
218 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
220 // Use wxWindows configuration data (comming soon)
221 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
225 wxStrncpy(buf
, user
, maxSize
- 1);
228 return *buf
? TRUE
: FALSE
;
232 // Get user name e.g. Julian Smart
233 bool wxGetUserName(wxChar
*buf
, int maxSize
)
235 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
236 extern HANDLE g_hPenWin
; // PenWindows Running?
239 // PenWindows Does have a user concept!
240 // Get the current owner of the recognizer
241 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
242 strncpy(buf
, wxBuffer
, maxSize
- 1);
248 CHAR szUserName
[256];
249 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
252 // TODO how to get the domain name?
255 // the code is based on the MSDN example (also see KB article Q119670)
256 WCHAR wszUserName
[256]; // Unicode user name
257 WCHAR wszDomain
[256];
260 USER_INFO_2
*ui2
; // User structure
262 // Convert ANSI user name and domain to Unicode
263 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
264 wszUserName
, WXSIZEOF(wszUserName
) );
265 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
266 wszDomain
, WXSIZEOF(wszDomain
) );
268 // Get the computer name of a DC for the domain.
269 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
271 wxLogError(wxT("Can not find domain controller"));
276 // Look up the user on the DC
277 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
278 (LPWSTR
)&wszUserName
,
279 2, // level - we want USER_INFO_2
287 case NERR_InvalidComputer
:
288 wxLogError(wxT("Invalid domain controller name."));
292 case NERR_UserNotFound
:
293 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
298 wxLogSysError(wxT("Can't get information about user"));
303 // Convert the Unicode full name to ANSI
304 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
305 buf
, maxSize
, NULL
, NULL
);
310 wxLogError(wxT("Couldn't look up full user name."));
313 #else // !USE_NET_API
314 // Could use NIS, MS-Mail or other site specific programs
315 // Use wxWindows configuration data
316 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxT(""), buf
, maxSize
- 1) != 0;
319 ok
= wxGetUserId(buf
, maxSize
);
324 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
332 const wxChar
* wxGetHomeDir(wxString
*pstr
)
334 wxString
& strDir
= *pstr
;
336 #if defined(__UNIX__)
337 const wxChar
*szHome
= wxGetenv("HOME");
338 if ( szHome
== NULL
) {
340 wxLogWarning(_("can't find user's HOME, using current directory."));
346 // add a trailing slash if needed
347 if ( strDir
.Last() != wxT('/') )
351 // Cygwin returns unix type path but that does not work well
352 static wxChar windowsPath
[MAX_PATH
];
353 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
354 strDir
= windowsPath
;
360 // If we have a valid HOME directory, as is used on many machines that
361 // have unix utilities on them, we should use that.
362 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
364 if ( szHome
!= NULL
)
368 else // no HOME, try HOMEDRIVE/PATH
370 szHome
= wxGetenv(wxT("HOMEDRIVE"));
371 if ( szHome
!= NULL
)
373 szHome
= wxGetenv(wxT("HOMEPATH"));
375 if ( szHome
!= NULL
)
379 // the idea is that under NT these variables have default values
380 // of "%systemdrive%:" and "\\". As we don't want to create our
381 // config files in the root directory of the system drive, we will
382 // create it in our program's dir. However, if the user took care
383 // to set HOMEPATH to something other than "\\", we suppose that he
384 // knows what he is doing and use the supplied value.
385 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
390 if ( strDir
.empty() )
392 // If we have a valid USERPROFILE directory, as is the case in
393 // Windows NT, 2000 and XP, we should use that as our home directory.
394 szHome
= wxGetenv(wxT("USERPROFILE"));
396 if ( szHome
!= NULL
)
400 if ( !strDir
.empty() )
402 return strDir
.c_str();
404 //else: fall back to the prograrm directory
406 // Win16 has no idea about home, so use the executable directory instead
409 // 260 was taken from windef.h
415 ::GetModuleFileName(::GetModuleHandle(NULL
),
416 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
417 strPath
.UngetWriteBuf();
419 // extract the dir name
420 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
424 return strDir
.c_str();
427 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
429 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
430 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
431 // static buffer and sometimes I don't even know what.
432 static wxString s_home
;
434 return (wxChar
*)wxGetHomeDir(&s_home
);
437 bool wxDirExists(const wxString
& dir
)
439 #ifdef __WXMICROWIN__
440 return wxPathExist(dir
);
441 #elif defined(__WIN32__)
442 DWORD attribs
= GetFileAttributes(dir
);
443 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
446 struct ffblk fileInfo
;
448 struct find_t fileInfo
;
450 // In Borland findfirst has a different argument
451 // ordering from _dos_findfirst. But _dos_findfirst
452 // _should_ be ok in both MS and Borland... why not?
454 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
455 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
457 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
458 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
463 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
468 // old w32api don't have ULARGE_INTEGER
469 #if defined(__WIN32__) && \
470 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
471 // GetDiskFreeSpaceEx() is not available under original Win95, check for
473 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
479 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
481 ::GetModuleHandle(_T("kernel32.dll")),
483 "GetDiskFreeSpaceExW"
485 "GetDiskFreeSpaceExA"
489 if ( pGetDiskFreeSpaceEx
)
491 ULARGE_INTEGER bytesFree
, bytesTotal
;
493 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
494 if ( !pGetDiskFreeSpaceEx(path
,
499 wxLogLastError(_T("GetDiskFreeSpaceEx"));
504 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
505 // two 32 bit fields which may be or may be not named - try to make it
506 // compile in all cases
507 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
514 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
519 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
525 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
526 // should be used instead - but if it's not available, fall back on
527 // GetDiskFreeSpace() nevertheless...
529 DWORD lSectorsPerCluster
,
531 lNumberOfFreeClusters
,
532 lTotalNumberOfClusters
;
534 // FIXME: this is wrong, we should extract the root drive from path
535 // instead, but this is the job for wxFileName...
536 if ( !::GetDiskFreeSpace(path
,
539 &lNumberOfFreeClusters
,
540 &lTotalNumberOfClusters
) )
542 wxLogLastError(_T("GetDiskFreeSpace"));
547 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
548 lBytesPerCluster
*= lBytesPerSector
;
552 *pTotal
= lBytesPerCluster
;
553 *pTotal
*= lTotalNumberOfClusters
;
558 *pFree
= lBytesPerCluster
;
559 *pFree
*= lNumberOfFreeClusters
;
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 bool wxGetEnv(const wxString
& var
, wxString
*value
)
573 const wxChar
* ret
= wxGetenv(var
);
584 // first get the size of the buffer
585 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
588 // this means that there is no such variable
594 (void)::GetEnvironmentVariable(var
, value
->GetWriteBuf(dwRet
), dwRet
);
595 value
->UngetWriteBuf();
602 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
604 // some compilers have putenv() or _putenv() or _wputenv() but it's better
605 // to always use Win32 function directly instead of dealing with them
606 #if defined(__WIN32__)
607 if ( !::SetEnvironmentVariable(var
, value
) )
609 wxLogLastError(_T("SetEnvironmentVariable"));
615 #else // no way to set env vars
620 // ----------------------------------------------------------------------------
621 // process management
622 // ----------------------------------------------------------------------------
624 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
625 struct wxFindByPidParams
627 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
629 // the HWND used to return the result
632 // the PID we're looking from
635 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
638 // wxKill helper: EnumWindows() callback which is used to find the first (top
639 // level) window belonging to the given process
640 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
643 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
645 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
646 if ( pid
== params
->pid
)
648 // remember the window we found
651 // return FALSE to stop the enumeration
655 // continue enumeration
659 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
661 // get the process handle to operate on
662 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
664 PROCESS_QUERY_INFORMATION
,
665 FALSE
, // not inheritable
667 if ( hProcess
== NULL
)
671 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
673 *krc
= wxKILL_ACCESS_DENIED
;
677 *krc
= wxKILL_NO_PROCESS
;
688 // kill the process forcefully returning -1 as error code
689 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
691 wxLogSysError(_("Failed to kill process %d"), pid
);
695 // this is not supposed to happen if we could open the
705 // do nothing, we just want to test for process existence
709 // any other signal means "terminate"
711 wxFindByPidParams params
;
712 params
.pid
= (DWORD
)pid
;
714 // EnumWindows() has nice semantics: it returns 0 if it found
715 // something or if an error occured and non zero if it
716 // enumerated all the window
717 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
719 // did we find any window?
722 // tell the app to close
724 // NB: this is the harshest way, the app won't have
725 // opportunity to save any files, for example, but
726 // this is probably what we want here. If not we
727 // can also use SendMesageTimeout(WM_CLOSE)
728 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
730 wxLogLastError(_T("PostMessage(WM_QUIT)"));
733 else // it was an error then
735 wxLogLastError(_T("EnumWindows"));
740 else // no windows for this PID
757 // as we wait for a short time, we can use just WaitForSingleObject()
758 // and not MsgWaitForMultipleObjects()
759 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
762 // process terminated
763 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
765 wxLogLastError(_T("GetExitCodeProcess"));
770 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
774 wxLogLastError(_T("WaitForSingleObject"));
789 // just to suppress the warnings about uninitialized variable
793 ::CloseHandle(hProcess
);
795 // the return code is the same as from Unix kill(): 0 if killed
796 // successfully or -1 on error
798 // be careful to interpret rc correctly: for wxSIGNONE we return success if
799 // the process exists, for all the other sig values -- if it doesn't
801 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
815 // Execute a program in an Interactive Shell
816 bool wxShell(const wxString
& command
)
818 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
820 shell
= (wxChar
*) wxT("\\COMMAND.COM");
830 // pass the command to execute to the command processor
831 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
834 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
837 // Shutdown or reboot the PC
838 bool wxShutdown(wxShutdownFlags wFlags
)
843 if ( wxGetOsVersion(NULL
, NULL
) == wxWINDOWS_NT
) // if is NT or 2K
845 // Get a token for this process.
847 bOK
= ::OpenProcessToken(GetCurrentProcess(),
848 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
,
852 TOKEN_PRIVILEGES tkp
;
854 // Get the LUID for the shutdown privilege.
855 ::LookupPrivilegeValue(NULL
, SE_SHUTDOWN_NAME
,
856 &tkp
.Privileges
[0].Luid
);
858 tkp
.PrivilegeCount
= 1; // one privilege to set
859 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
861 // Get the shutdown privilege for this process.
862 ::AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0,
863 (PTOKEN_PRIVILEGES
)NULL
, 0);
865 // Cannot test the return value of AdjustTokenPrivileges.
866 bOK
= ::GetLastError() == ERROR_SUCCESS
;
872 UINT flags
= EWX_SHUTDOWN
| EWX_FORCE
;
875 case wxSHUTDOWN_POWEROFF
:
876 flags
|= EWX_POWEROFF
;
879 case wxSHUTDOWN_REBOOT
:
884 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
888 bOK
= ::ExitWindowsEx(EWX_SHUTDOWN
| EWX_FORCE
| EWX_REBOOT
, 0) != 0;
897 // ----------------------------------------------------------------------------
899 // ----------------------------------------------------------------------------
901 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
902 long wxGetFreeMemory()
904 #if defined(__WIN32__) && !defined(__BORLANDC__)
905 MEMORYSTATUS memStatus
;
906 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
907 GlobalMemoryStatus(&memStatus
);
908 return memStatus
.dwAvailPhys
;
910 return (long)GetFreeSpace(0);
914 unsigned long wxGetProcessId()
917 return ::GetCurrentProcessId();
926 ::MessageBeep((UINT
)-1); // default sound
929 wxString
wxGetOsDescription()
937 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
938 if ( ::GetVersionEx(&info
) )
940 switch ( info
.dwPlatformId
)
942 case VER_PLATFORM_WIN32s
:
943 str
= _("Win32s on Windows 3.1");
946 case VER_PLATFORM_WIN32_WINDOWS
:
947 str
.Printf(_("Windows 9%c"),
948 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
949 if ( !wxIsEmpty(info
.szCSDVersion
) )
951 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
955 case VER_PLATFORM_WIN32_NT
:
956 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
960 if ( !wxIsEmpty(info
.szCSDVersion
) )
962 str
<< _T(", ") << info
.szCSDVersion
;
970 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
975 return _("Windows 3.1");
979 int wxAppTraits::GetOSVersion(int *verMaj
, int *verMin
)
981 // cache the version info, it's not going to change
983 // NB: this is MT-safe, we may use these static vars from different threads
984 // but as they always have the same value it doesn't matter
985 static int s_ver
= -1,
995 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
996 if ( ::GetVersionEx(&info
) )
998 s_major
= info
.dwMajorVersion
;
999 s_minor
= info
.dwMinorVersion
;
1001 switch ( info
.dwPlatformId
)
1003 case VER_PLATFORM_WIN32s
:
1007 case VER_PLATFORM_WIN32_WINDOWS
:
1011 case VER_PLATFORM_WIN32_NT
:
1012 s_ver
= wxWINDOWS_NT
;
1026 // ----------------------------------------------------------------------------
1028 // ----------------------------------------------------------------------------
1030 void wxUsleep(unsigned long milliseconds
)
1032 ::Sleep(milliseconds
);
1035 void wxSleep(int nSecs
)
1037 wxUsleep(1000*nSecs
);
1040 // ----------------------------------------------------------------------------
1041 // font encoding <-> Win32 codepage conversion functions
1042 // ----------------------------------------------------------------------------
1044 extern long wxEncodingToCharset(wxFontEncoding encoding
)
1048 // although this function is supposed to return an exact match, do do
1049 // some mappings here for the most common case of "standard" encoding
1050 case wxFONTENCODING_SYSTEM
:
1051 return DEFAULT_CHARSET
;
1053 case wxFONTENCODING_ISO8859_1
:
1054 case wxFONTENCODING_ISO8859_15
:
1055 case wxFONTENCODING_CP1252
:
1056 return ANSI_CHARSET
;
1058 #if !defined(__WXMICROWIN__)
1059 // The following four fonts are multi-byte charsets
1060 case wxFONTENCODING_CP932
:
1061 return SHIFTJIS_CHARSET
;
1063 case wxFONTENCODING_CP936
:
1064 return GB2312_CHARSET
;
1066 case wxFONTENCODING_CP949
:
1067 return HANGUL_CHARSET
;
1069 case wxFONTENCODING_CP950
:
1070 return CHINESEBIG5_CHARSET
;
1072 // The rest are single byte encodings
1073 case wxFONTENCODING_CP1250
:
1074 return EASTEUROPE_CHARSET
;
1076 case wxFONTENCODING_CP1251
:
1077 return RUSSIAN_CHARSET
;
1079 case wxFONTENCODING_CP1253
:
1080 return GREEK_CHARSET
;
1082 case wxFONTENCODING_CP1254
:
1083 return TURKISH_CHARSET
;
1085 case wxFONTENCODING_CP1255
:
1086 return HEBREW_CHARSET
;
1088 case wxFONTENCODING_CP1256
:
1089 return ARABIC_CHARSET
;
1091 case wxFONTENCODING_CP1257
:
1092 return BALTIC_CHARSET
;
1094 case wxFONTENCODING_CP874
:
1095 return THAI_CHARSET
;
1096 #endif // !__WXMICROWIN__
1098 case wxFONTENCODING_CP437
:
1102 // no way to translate this encoding into a Windows charset
1106 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1107 // looks up the vlaues in the registry and the new one which is more
1108 // politically correct and has more chances to work on other Windows versions
1109 // as well but the old version is still needed for !wxUSE_FONTMAP case
1112 #include "wx/fontmap.h"
1114 extern long wxEncodingToCodepage(wxFontEncoding encoding
)
1116 // translate encoding into the Windows CHARSET
1117 long charset
= wxEncodingToCharset(encoding
);
1118 if ( charset
== -1 )
1121 // translate CHARSET to code page
1122 CHARSETINFO csetInfo
;
1123 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1127 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1132 return csetInfo
.ciACP
;
1135 extern long wxCharsetToCodepage(const wxChar
*name
)
1137 // first get the font encoding for this charset
1141 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1142 if ( enc
== wxFONTENCODING_SYSTEM
)
1145 // the use the helper function
1146 return wxEncodingToCodepage(enc
);
1149 #else // !wxUSE_FONTMAP
1151 #include "wx/msw/registry.h"
1153 // this should work if Internet Exploiter is installed
1154 extern long wxCharsetToCodepage(const wxChar
*name
)
1161 wxString
path(wxT("MIME\\Database\\Charset\\"));
1164 // follow the alias loop
1167 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1172 // two cases: either there's an AliasForCharset string,
1173 // or there are Codepage and InternetEncoding dwords.
1174 // The InternetEncoding gives us the actual encoding,
1175 // the Codepage just says which Windows character set to
1176 // use when displaying the data.
1177 if (key
.HasValue(wxT("InternetEncoding")) &&
1178 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1181 // no encoding, see if it's an alias
1182 if (!key
.HasValue(wxT("AliasForCharset")) ||
1183 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1190 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP