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>
37 #include "wx/msw/missing.h" // CHARSET_HANGUL
39 #if defined(__GNUWIN32_OLD__) || defined(__WXWINCE__)
40 // apparently we need to include winsock.h to get WSADATA and other stuff
41 // used in wxGetFullHostName() with the old mingw32 versions
47 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
55 #if defined(__CYGWIN__)
56 #include <sys/unistd.h>
58 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
61 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
62 // this (3.1 I believe) and how to test for it.
63 // If this works for Borland 4.0 as well, then no worries.
67 // VZ: there is some code using NetXXX() functions to get the full user name:
68 // I don't think it's a good idea because they don't work under Win95 and
69 // seem to return the same as wxGetUserId() under NT. If you really want
70 // to use them, just #define USE_NET_API
77 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
88 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // In the WIN.INI file
98 static const wxChar WX_SECTION
[] = wxT("wxWindows");
99 static const wxChar eUSERNAME
[] = wxT("UserName");
101 // these are only used under Win16
102 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
103 static const wxChar eHOSTNAME
[] = wxT("HostName");
104 static const wxChar eUSERID
[] = wxT("UserId");
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
112 // get host name and related
113 // ----------------------------------------------------------------------------
115 // Get hostname only (without domain name)
116 bool wxGetHostName(wxChar
*buf
, int maxSize
)
118 #if defined(__WXWINCE__)
120 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
121 DWORD nSize
= maxSize
;
122 if ( !::GetComputerName(buf
, &nSize
) )
124 wxLogLastError(wxT("GetComputerName"));
132 const wxChar
*default_host
= wxT("noname");
134 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
135 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
137 wxStrncpy(buf
, sysname
, maxSize
- 1);
138 buf
[maxSize
] = wxT('\0');
139 return *buf
? TRUE
: FALSE
;
143 // get full hostname (with domain name if possible)
144 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
146 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
147 // TODO should use GetComputerNameEx() when available
149 // the idea is that if someone had set wxUSE_SOCKETS to 0 the code
150 // shouldn't use winsock.dll (a.k.a. ws2_32.dll) at all so only use this
151 // code if we link with it anyhow
154 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
158 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
160 // gethostname() won't usually include the DNS domain name, for
161 // this we need to work a bit more
162 if ( !strchr(bufA
, '.') )
164 struct hostent
*pHostEnt
= gethostbyname(bufA
);
168 // Windows will use DNS internally now
169 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, AF_INET
);
174 host
= wxString::FromAscii(pHostEnt
->h_name
);
183 wxStrncpy(buf
, host
, maxSize
);
189 #endif // wxUSE_SOCKETS
193 return wxGetHostName(buf
, maxSize
);
196 // Get user ID e.g. jacs
197 bool wxGetUserId(wxChar
*buf
, int maxSize
)
199 #if defined(__WXWINCE__)
201 #elif defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
202 DWORD nSize
= maxSize
;
203 if ( ::GetUserName(buf
, &nSize
) == 0 )
205 // actually, it does happen on Win9x if the user didn't log on
206 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
215 #else // Win16 or Win32s
217 const wxChar
*default_id
= wxT("anonymous");
219 // Can't assume we have NIS (PC-NFS) or some other ID daemon
221 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
222 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
224 // Use wxWindows configuration data (comming soon)
225 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
229 wxStrncpy(buf
, user
, maxSize
- 1);
232 return *buf
? TRUE
: FALSE
;
236 // Get user name e.g. Julian Smart
237 bool wxGetUserName(wxChar
*buf
, int maxSize
)
239 #if defined(__WXWINCE__)
241 #elif defined(USE_NET_API)
242 CHAR szUserName
[256];
243 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
246 // TODO how to get the domain name?
249 // the code is based on the MSDN example (also see KB article Q119670)
250 WCHAR wszUserName
[256]; // Unicode user name
251 WCHAR wszDomain
[256];
254 USER_INFO_2
*ui2
; // User structure
256 // Convert ANSI user name and domain to Unicode
257 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
258 wszUserName
, WXSIZEOF(wszUserName
) );
259 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
260 wszDomain
, WXSIZEOF(wszDomain
) );
262 // Get the computer name of a DC for the domain.
263 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
265 wxLogError(wxT("Can not find domain controller"));
270 // Look up the user on the DC
271 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
272 (LPWSTR
)&wszUserName
,
273 2, // level - we want USER_INFO_2
281 case NERR_InvalidComputer
:
282 wxLogError(wxT("Invalid domain controller name."));
286 case NERR_UserNotFound
:
287 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
292 wxLogSysError(wxT("Can't get information about user"));
297 // Convert the Unicode full name to ANSI
298 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
299 buf
, maxSize
, NULL
, NULL
);
304 wxLogError(wxT("Couldn't look up full user name."));
307 #else // !USE_NET_API
308 // Could use NIS, MS-Mail or other site specific programs
309 // Use wxWindows configuration data
310 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxEmptyString
, buf
, maxSize
- 1) != 0;
313 ok
= wxGetUserId(buf
, maxSize
);
318 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
325 const wxChar
* wxGetHomeDir(wxString
*pstr
)
327 wxString
& strDir
= *pstr
;
329 #if defined(__UNIX__)
330 const wxChar
*szHome
= wxGetenv("HOME");
331 if ( szHome
== NULL
) {
333 wxLogWarning(_("can't find user's HOME, using current directory."));
339 // add a trailing slash if needed
340 if ( strDir
.Last() != wxT('/') )
344 // Cygwin returns unix type path but that does not work well
345 static wxChar windowsPath
[MAX_PATH
];
346 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
347 strDir
= windowsPath
;
349 #elif defined(__WXWINCE__)
355 // If we have a valid HOME directory, as is used on many machines that
356 // have unix utilities on them, we should use that.
357 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
359 if ( szHome
!= NULL
)
363 else // no HOME, try HOMEDRIVE/PATH
365 szHome
= wxGetenv(wxT("HOMEDRIVE"));
366 if ( szHome
!= NULL
)
368 szHome
= wxGetenv(wxT("HOMEPATH"));
370 if ( szHome
!= NULL
)
374 // the idea is that under NT these variables have default values
375 // of "%systemdrive%:" and "\\". As we don't want to create our
376 // config files in the root directory of the system drive, we will
377 // create it in our program's dir. However, if the user took care
378 // to set HOMEPATH to something other than "\\", we suppose that he
379 // knows what he is doing and use the supplied value.
380 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
385 if ( strDir
.empty() )
387 // If we have a valid USERPROFILE directory, as is the case in
388 // Windows NT, 2000 and XP, we should use that as our home directory.
389 szHome
= wxGetenv(wxT("USERPROFILE"));
391 if ( szHome
!= NULL
)
395 if ( !strDir
.empty() )
397 return strDir
.c_str();
399 //else: fall back to the prograrm directory
401 // Win16 has no idea about home, so use the executable directory instead
404 // 260 was taken from windef.h
410 ::GetModuleFileName(::GetModuleHandle(NULL
),
411 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
412 strPath
.UngetWriteBuf();
414 // extract the dir name
415 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
419 return strDir
.c_str();
422 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
424 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
425 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
426 // static buffer and sometimes I don't even know what.
427 static wxString s_home
;
429 return (wxChar
*)wxGetHomeDir(&s_home
);
432 bool wxDirExists(const wxString
& dir
)
434 #ifdef __WXMICROWIN__
435 return wxPathExist(dir
);
436 #elif defined(__WIN32__)
437 DWORD attribs
= GetFileAttributes(dir
);
438 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
441 struct ffblk fileInfo
;
443 struct find_t fileInfo
;
445 // In Borland findfirst has a different argument
446 // ordering from _dos_findfirst. But _dos_findfirst
447 // _should_ be ok in both MS and Borland... why not?
449 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
450 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
452 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
453 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
458 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
;
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 bool wxGetEnv(const wxString
& var
, wxString
*value
)
574 #elif defined(__WIN16__)
575 const wxChar
* ret
= wxGetenv(var
);
586 // first get the size of the buffer
587 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
590 // this means that there is no such variable
596 (void)::GetEnvironmentVariable(var
, value
->GetWriteBuf(dwRet
), dwRet
);
597 value
->UngetWriteBuf();
604 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
606 // some compilers have putenv() or _putenv() or _wputenv() but it's better
607 // to always use Win32 function directly instead of dealing with them
608 #if defined(__WIN32__) && !defined(__WXWINCE__)
609 if ( !::SetEnvironmentVariable(var
, value
) )
611 wxLogLastError(_T("SetEnvironmentVariable"));
617 #else // no way to set env vars
622 // ----------------------------------------------------------------------------
623 // process management
624 // ----------------------------------------------------------------------------
626 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
627 struct wxFindByPidParams
629 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
631 // the HWND used to return the result
634 // the PID we're looking from
637 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
640 // wxKill helper: EnumWindows() callback which is used to find the first (top
641 // level) window belonging to the given process
642 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
645 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
647 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
648 if ( pid
== params
->pid
)
650 // remember the window we found
653 // return FALSE to stop the enumeration
657 // continue enumeration
661 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
663 // get the process handle to operate on
664 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
666 PROCESS_QUERY_INFORMATION
,
667 FALSE
, // not inheritable
669 if ( hProcess
== NULL
)
673 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
675 *krc
= wxKILL_ACCESS_DENIED
;
679 *krc
= wxKILL_NO_PROCESS
;
690 // kill the process forcefully returning -1 as error code
691 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
693 wxLogSysError(_("Failed to kill process %d"), pid
);
697 // this is not supposed to happen if we could open the
707 // do nothing, we just want to test for process existence
711 // any other signal means "terminate"
713 wxFindByPidParams params
;
714 params
.pid
= (DWORD
)pid
;
716 // EnumWindows() has nice semantics: it returns 0 if it found
717 // something or if an error occured and non zero if it
718 // enumerated all the window
719 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
721 // did we find any window?
724 // tell the app to close
726 // NB: this is the harshest way, the app won't have
727 // opportunity to save any files, for example, but
728 // this is probably what we want here. If not we
729 // can also use SendMesageTimeout(WM_CLOSE)
730 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
732 wxLogLastError(_T("PostMessage(WM_QUIT)"));
735 else // it was an error then
737 wxLogLastError(_T("EnumWindows"));
742 else // no windows for this PID
759 // as we wait for a short time, we can use just WaitForSingleObject()
760 // and not MsgWaitForMultipleObjects()
761 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
764 // process terminated
765 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
767 wxLogLastError(_T("GetExitCodeProcess"));
772 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
776 wxLogLastError(_T("WaitForSingleObject"));
791 // just to suppress the warnings about uninitialized variable
795 ::CloseHandle(hProcess
);
797 // the return code is the same as from Unix kill(): 0 if killed
798 // successfully or -1 on error
800 // be careful to interpret rc correctly: for wxSIGNONE we return success if
801 // the process exists, for all the other sig values -- if it doesn't
803 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
817 // Execute a program in an Interactive Shell
818 bool wxShell(const wxString
& command
)
823 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
825 shell
= (wxChar
*) wxT("\\COMMAND.COM");
835 // pass the command to execute to the command processor
836 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
839 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
843 // Shutdown or reboot the PC
844 bool wxShutdown(wxShutdownFlags wFlags
)
848 #elif defined(__WIN32__)
851 if ( wxGetOsVersion(NULL
, NULL
) == wxWINDOWS_NT
) // if is NT or 2K
853 // Get a token for this process.
855 bOK
= ::OpenProcessToken(GetCurrentProcess(),
856 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
,
860 TOKEN_PRIVILEGES tkp
;
862 // Get the LUID for the shutdown privilege.
863 ::LookupPrivilegeValue(NULL
, SE_SHUTDOWN_NAME
,
864 &tkp
.Privileges
[0].Luid
);
866 tkp
.PrivilegeCount
= 1; // one privilege to set
867 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
869 // Get the shutdown privilege for this process.
870 ::AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0,
871 (PTOKEN_PRIVILEGES
)NULL
, 0);
873 // Cannot test the return value of AdjustTokenPrivileges.
874 bOK
= ::GetLastError() == ERROR_SUCCESS
;
880 UINT flags
= EWX_SHUTDOWN
| EWX_FORCE
;
883 case wxSHUTDOWN_POWEROFF
:
884 flags
|= EWX_POWEROFF
;
887 case wxSHUTDOWN_REBOOT
:
892 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
896 bOK
= ::ExitWindowsEx(EWX_SHUTDOWN
| EWX_FORCE
| EWX_REBOOT
, 0) != 0;
905 // ----------------------------------------------------------------------------
907 // ----------------------------------------------------------------------------
909 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
910 long wxGetFreeMemory()
912 #if defined(__WIN32__) && !defined(__BORLANDC__)
913 MEMORYSTATUS memStatus
;
914 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
915 GlobalMemoryStatus(&memStatus
);
916 return memStatus
.dwAvailPhys
;
918 return (long)GetFreeSpace(0);
922 unsigned long wxGetProcessId()
925 return ::GetCurrentProcessId();
934 ::MessageBeep((UINT
)-1); // default sound
937 wxString
wxGetOsDescription()
945 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
946 if ( ::GetVersionEx(&info
) )
948 switch ( info
.dwPlatformId
)
950 case VER_PLATFORM_WIN32s
:
951 str
= _("Win32s on Windows 3.1");
954 case VER_PLATFORM_WIN32_WINDOWS
:
955 str
.Printf(_("Windows 9%c"),
956 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
957 if ( !wxIsEmpty(info
.szCSDVersion
) )
959 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
963 case VER_PLATFORM_WIN32_NT
:
964 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
968 if ( !wxIsEmpty(info
.szCSDVersion
) )
970 str
<< _T(", ") << info
.szCSDVersion
;
978 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
983 return _("Windows 3.1");
987 int wxAppTraits::GetOSVersion(int *verMaj
, int *verMin
)
989 // cache the version info, it's not going to change
991 // NB: this is MT-safe, we may use these static vars from different threads
992 // but as they always have the same value it doesn't matter
993 static int s_ver
= -1,
1003 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1004 if ( ::GetVersionEx(&info
) )
1006 s_major
= info
.dwMajorVersion
;
1007 s_minor
= info
.dwMinorVersion
;
1009 switch ( info
.dwPlatformId
)
1011 case VER_PLATFORM_WIN32s
:
1015 case VER_PLATFORM_WIN32_WINDOWS
:
1019 case VER_PLATFORM_WIN32_NT
:
1020 s_ver
= wxWINDOWS_NT
;
1023 case VER_PLATFORM_WIN32_CE
:
1024 s_ver
= wxWINDOWS_CE
;
1039 // ----------------------------------------------------------------------------
1041 // ----------------------------------------------------------------------------
1043 void wxUsleep(unsigned long milliseconds
)
1045 ::Sleep(milliseconds
);
1048 void wxSleep(int nSecs
)
1050 wxUsleep(1000*nSecs
);
1053 // ----------------------------------------------------------------------------
1054 // font encoding <-> Win32 codepage conversion functions
1055 // ----------------------------------------------------------------------------
1057 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
)
1061 // although this function is supposed to return an exact match, do do
1062 // some mappings here for the most common case of "standard" encoding
1063 case wxFONTENCODING_SYSTEM
:
1064 return DEFAULT_CHARSET
;
1066 case wxFONTENCODING_ISO8859_1
:
1067 case wxFONTENCODING_ISO8859_15
:
1068 case wxFONTENCODING_CP1252
:
1069 return ANSI_CHARSET
;
1071 #if !defined(__WXMICROWIN__)
1072 // The following four fonts are multi-byte charsets
1073 case wxFONTENCODING_CP932
:
1074 return SHIFTJIS_CHARSET
;
1076 case wxFONTENCODING_CP936
:
1077 return GB2312_CHARSET
;
1079 case wxFONTENCODING_CP949
:
1080 return HANGUL_CHARSET
;
1082 case wxFONTENCODING_CP950
:
1083 return CHINESEBIG5_CHARSET
;
1085 // The rest are single byte encodings
1086 case wxFONTENCODING_CP1250
:
1087 return EASTEUROPE_CHARSET
;
1089 case wxFONTENCODING_CP1251
:
1090 return RUSSIAN_CHARSET
;
1092 case wxFONTENCODING_CP1253
:
1093 return GREEK_CHARSET
;
1095 case wxFONTENCODING_CP1254
:
1096 return TURKISH_CHARSET
;
1098 case wxFONTENCODING_CP1255
:
1099 return HEBREW_CHARSET
;
1101 case wxFONTENCODING_CP1256
:
1102 return ARABIC_CHARSET
;
1104 case wxFONTENCODING_CP1257
:
1105 return BALTIC_CHARSET
;
1107 case wxFONTENCODING_CP874
:
1108 return THAI_CHARSET
;
1109 #endif // !__WXMICROWIN__
1111 case wxFONTENCODING_CP437
:
1115 // no way to translate this encoding into a Windows charset
1120 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1121 // looks up the vlaues in the registry and the new one which is more
1122 // politically correct and has more chances to work on other Windows versions
1123 // as well but the old version is still needed for !wxUSE_FONTMAP case
1126 #include "wx/fontmap.h"
1128 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
)
1130 // translate encoding into the Windows CHARSET
1131 long charset
= wxEncodingToCharset(encoding
);
1132 if ( charset
== -1 )
1135 // translate CHARSET to code page
1136 CHARSETINFO csetInfo
;
1137 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1141 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1146 return csetInfo
.ciACP
;
1149 extern long wxCharsetToCodepage(const wxChar
*name
)
1151 // first get the font encoding for this charset
1155 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1156 if ( enc
== wxFONTENCODING_SYSTEM
)
1159 // the use the helper function
1160 return wxEncodingToCodepage(enc
);
1163 #else // !wxUSE_FONTMAP
1165 #include "wx/msw/registry.h"
1167 // this should work if Internet Exploiter is installed
1168 extern long wxCharsetToCodepage(const wxChar
*name
)
1175 wxString
path(wxT("MIME\\Database\\Charset\\"));
1178 // follow the alias loop
1181 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1186 // two cases: either there's an AliasForCharset string,
1187 // or there are Codepage and InternetEncoding dwords.
1188 // The InternetEncoding gives us the actual encoding,
1189 // the Codepage just says which Windows character set to
1190 // use when displaying the data.
1191 if (key
.HasValue(wxT("InternetEncoding")) &&
1192 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1195 // no encoding, see if it's an alias
1196 if (!key
.HasValue(wxT("AliasForCharset")) ||
1197 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1204 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP