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/confbase.h" // for wxExpandEnvVars()
39 #include "wx/msw/private.h" // includes <windows.h>
40 #include "wx/msw/missing.h" // CHARSET_HANGUL
42 #if defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) \
43 || defined(__CYGWIN32__)
44 // apparently we need to include winsock.h to get WSADATA and other stuff
45 // used in wxGetFullHostName() with the old mingw32 versions
51 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
59 #if defined(__CYGWIN__)
60 #include <sys/unistd.h>
62 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
65 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
66 // this (3.1 I believe) and how to test for it.
67 // If this works for Borland 4.0 as well, then no worries.
71 // VZ: there is some code using NetXXX() functions to get the full user name:
72 // I don't think it's a good idea because they don't work under Win95 and
73 // seem to return the same as wxGetUserId() under NT. If you really want
74 // to use them, just #define USE_NET_API
81 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
92 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
97 // 260 was taken from windef.h
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // In the WIN.INI file
107 static const wxChar WX_SECTION
[] = wxT("wxWindows");
108 static const wxChar eUSERNAME
[] = wxT("UserName");
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
115 // get host name and related
116 // ----------------------------------------------------------------------------
118 // Get hostname only (without domain name)
119 bool wxGetHostName(wxChar
*buf
, int maxSize
)
121 #if defined(__WXWINCE__)
123 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
124 DWORD nSize
= maxSize
;
125 if ( !::GetComputerName(buf
, &nSize
) )
127 wxLogLastError(wxT("GetComputerName"));
135 const wxChar
*default_host
= wxT("noname");
137 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
138 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
140 wxStrncpy(buf
, sysname
, maxSize
- 1);
141 buf
[maxSize
] = wxT('\0');
142 return *buf
? TRUE
: FALSE
;
146 // get full hostname (with domain name if possible)
147 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
149 #if !defined( __WXMICROWIN__) && wxUSE_DYNAMIC_LOADER
150 // TODO should use GetComputerNameEx() when available
152 // we don't want to always link with Winsock DLL as we might not use it at
153 // all, so load it dynamically here if needed (and don't complain if it is
154 // missing, we handle this)
157 wxDynamicLibrary
dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM
);
158 if ( dllWinsock
.IsLoaded() )
160 typedef int (PASCAL
*WSAStartup_t
)(WORD
, WSADATA
*);
161 typedef int (PASCAL
*gethostname_t
)(char *, int);
162 typedef hostent
* (PASCAL
*gethostbyname_t
)(const char *);
163 typedef hostent
* (PASCAL
*gethostbyaddr_t
)(const char *, int , int);
164 typedef int (PASCAL
*WSACleanup_t
)(void);
166 #define LOAD_WINSOCK_FUNC(func) \
168 pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
170 LOAD_WINSOCK_FUNC(WSAStartup
);
173 if ( pfnWSAStartup
&& pfnWSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
175 LOAD_WINSOCK_FUNC(gethostname
);
178 if ( pfngethostname
)
181 if ( pfngethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
183 // gethostname() won't usually include the DNS domain name,
184 // for this we need to work a bit more
185 if ( !strchr(bufA
, '.') )
187 LOAD_WINSOCK_FUNC(gethostbyname
);
189 struct hostent
*pHostEnt
= pfngethostbyname
190 ? pfngethostbyname(bufA
)
195 // Windows will use DNS internally now
196 LOAD_WINSOCK_FUNC(gethostbyaddr
);
198 pHostEnt
= pfngethostbyaddr
199 ? pfngethostbyaddr(pHostEnt
->h_addr
,
206 host
= wxString::FromAscii(pHostEnt
->h_name
);
212 LOAD_WINSOCK_FUNC(WSACleanup
);
219 wxStrncpy(buf
, host
, maxSize
);
225 #endif // !__WXMICROWIN__
227 return wxGetHostName(buf
, maxSize
);
230 // Get user ID e.g. jacs
231 bool wxGetUserId(wxChar
*buf
, int maxSize
)
233 #if defined(__WXWINCE__)
235 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
236 DWORD nSize
= maxSize
;
237 if ( ::GetUserName(buf
, &nSize
) == 0 )
239 // actually, it does happen on Win9x if the user didn't log on
240 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
249 #else // __WXMICROWIN__
251 const wxChar
*default_id
= wxT("anonymous");
253 // Can't assume we have NIS (PC-NFS) or some other ID daemon
255 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
256 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
258 // Use wxWidgets configuration data (comming soon)
259 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
263 wxStrncpy(buf
, user
, maxSize
- 1);
266 return *buf
? TRUE
: FALSE
;
270 // Get user name e.g. Julian Smart
271 bool wxGetUserName(wxChar
*buf
, int maxSize
)
273 #if defined(__WXWINCE__)
275 #elif defined(USE_NET_API)
276 CHAR szUserName
[256];
277 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
280 // TODO how to get the domain name?
283 // the code is based on the MSDN example (also see KB article Q119670)
284 WCHAR wszUserName
[256]; // Unicode user name
285 WCHAR wszDomain
[256];
288 USER_INFO_2
*ui2
; // User structure
290 // Convert ANSI user name and domain to Unicode
291 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
292 wszUserName
, WXSIZEOF(wszUserName
) );
293 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
294 wszDomain
, WXSIZEOF(wszDomain
) );
296 // Get the computer name of a DC for the domain.
297 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
299 wxLogError(wxT("Can not find domain controller"));
304 // Look up the user on the DC
305 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
306 (LPWSTR
)&wszUserName
,
307 2, // level - we want USER_INFO_2
315 case NERR_InvalidComputer
:
316 wxLogError(wxT("Invalid domain controller name."));
320 case NERR_UserNotFound
:
321 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
326 wxLogSysError(wxT("Can't get information about user"));
331 // Convert the Unicode full name to ANSI
332 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
333 buf
, maxSize
, NULL
, NULL
);
338 wxLogError(wxT("Couldn't look up full user name."));
341 #else // !USE_NET_API
342 // Could use NIS, MS-Mail or other site specific programs
343 // Use wxWidgets configuration data
344 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxEmptyString
, buf
, maxSize
- 1) != 0;
347 ok
= wxGetUserId(buf
, maxSize
);
352 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
359 const wxChar
* wxGetHomeDir(wxString
*pstr
)
361 wxString
& strDir
= *pstr
;
363 // first branch is for Cygwin
364 #if defined(__UNIX__)
365 const wxChar
*szHome
= wxGetenv("HOME");
366 if ( szHome
== NULL
) {
368 wxLogWarning(_("can't find user's HOME, using current directory."));
374 // add a trailing slash if needed
375 if ( strDir
.Last() != wxT('/') )
379 // Cygwin returns unix type path but that does not work well
380 static wxChar windowsPath
[MAX_PATH
];
381 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
382 strDir
= windowsPath
;
384 #elif defined(__WXWINCE__)
389 // If we have a valid HOME directory, as is used on many machines that
390 // have unix utilities on them, we should use that.
391 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
393 if ( szHome
!= NULL
)
397 else // no HOME, try HOMEDRIVE/PATH
399 szHome
= wxGetenv(wxT("HOMEDRIVE"));
400 if ( szHome
!= NULL
)
402 szHome
= wxGetenv(wxT("HOMEPATH"));
404 if ( szHome
!= NULL
)
408 // the idea is that under NT these variables have default values
409 // of "%systemdrive%:" and "\\". As we don't want to create our
410 // config files in the root directory of the system drive, we will
411 // create it in our program's dir. However, if the user took care
412 // to set HOMEPATH to something other than "\\", we suppose that he
413 // knows what he is doing and use the supplied value.
414 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
419 if ( strDir
.empty() )
421 // If we have a valid USERPROFILE directory, as is the case in
422 // Windows NT, 2000 and XP, we should use that as our home directory.
423 szHome
= wxGetenv(wxT("USERPROFILE"));
425 if ( szHome
!= NULL
)
429 if ( !strDir
.empty() )
431 // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the
432 // value once again, it shouldn't hurt anyhow
433 strDir
= wxExpandEnvVars(strDir
);
435 else // fall back to the program directory
438 ::GetModuleFileName(::GetModuleHandle(NULL
),
439 wxStringBuffer(strPath
, MAX_PATH
), MAX_PATH
);
441 // extract the dir name
442 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
446 return strDir
.c_str();
449 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
451 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
452 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
453 // static buffer and sometimes I don't even know what.
454 static wxString s_home
;
456 return (wxChar
*)wxGetHomeDir(&s_home
);
459 bool wxDirExists(const wxString
& dir
)
461 #ifdef __WXMICROWIN__
462 return wxPathExist(dir
);
463 #elif defined(__WIN32__)
464 DWORD attribs
= GetFileAttributes(dir
);
465 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
466 #endif // Win32/__WXMICROWIN__
469 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
477 // old w32api don't have ULARGE_INTEGER
478 #if defined(__WIN32__) && \
479 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
480 // GetDiskFreeSpaceEx() is not available under original Win95, check for
482 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
488 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
490 ::GetModuleHandle(_T("kernel32.dll")),
492 "GetDiskFreeSpaceExW"
494 "GetDiskFreeSpaceExA"
498 if ( pGetDiskFreeSpaceEx
)
500 ULARGE_INTEGER bytesFree
, bytesTotal
;
502 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
503 if ( !pGetDiskFreeSpaceEx(path
,
508 wxLogLastError(_T("GetDiskFreeSpaceEx"));
513 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
514 // two 32 bit fields which may be or may be not named - try to make it
515 // compile in all cases
516 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
523 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
528 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
534 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
535 // should be used instead - but if it's not available, fall back on
536 // GetDiskFreeSpace() nevertheless...
538 DWORD lSectorsPerCluster
,
540 lNumberOfFreeClusters
,
541 lTotalNumberOfClusters
;
543 // FIXME: this is wrong, we should extract the root drive from path
544 // instead, but this is the job for wxFileName...
545 if ( !::GetDiskFreeSpace(path
,
548 &lNumberOfFreeClusters
,
549 &lTotalNumberOfClusters
) )
551 wxLogLastError(_T("GetDiskFreeSpace"));
556 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
557 lBytesPerCluster
*= lBytesPerSector
;
561 *pTotal
= lBytesPerCluster
;
562 *pTotal
*= lTotalNumberOfClusters
;
567 *pFree
= lBytesPerCluster
;
568 *pFree
*= lNumberOfFreeClusters
;
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 bool wxGetEnv(const wxString
& var
, wxString
*value
)
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
, wxStringBuffer(*value
, dwRet
),
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(flags
, 0) != 0;
903 // ----------------------------------------------------------------------------
905 // ----------------------------------------------------------------------------
907 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
908 long wxGetFreeMemory()
910 #if defined(__WIN32__) && !defined(__BORLANDC__)
911 MEMORYSTATUS memStatus
;
912 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
913 GlobalMemoryStatus(&memStatus
);
914 return memStatus
.dwAvailPhys
;
916 return (long)GetFreeSpace(0);
920 unsigned long wxGetProcessId()
922 return ::GetCurrentProcessId();
928 ::MessageBeep((UINT
)-1); // default sound
931 wxString
wxGetOsDescription()
938 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
939 if ( ::GetVersionEx(&info
) )
941 switch ( info
.dwPlatformId
)
943 case VER_PLATFORM_WIN32s
:
944 str
= _("Win32s on Windows 3.1");
947 case VER_PLATFORM_WIN32_WINDOWS
:
948 str
.Printf(_("Windows 9%c"),
949 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
950 if ( !wxIsEmpty(info
.szCSDVersion
) )
952 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
956 case VER_PLATFORM_WIN32_NT
:
957 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
961 if ( !wxIsEmpty(info
.szCSDVersion
) )
963 str
<< _T(", ") << info
.szCSDVersion
;
971 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
977 wxToolkitInfo
& wxAppTraits::GetToolkitInfo()
979 // cache the version info, it's not going to change
981 // NB: this is MT-safe, we may use these static vars from different threads
982 // but as they always have the same value it doesn't matter
983 static int s_ver
= -1,
993 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
994 if ( ::GetVersionEx(&info
) )
996 s_major
= info
.dwMajorVersion
;
997 s_minor
= info
.dwMinorVersion
;
999 switch ( info
.dwPlatformId
)
1001 case VER_PLATFORM_WIN32s
:
1005 case VER_PLATFORM_WIN32_WINDOWS
:
1009 case VER_PLATFORM_WIN32_NT
:
1010 s_ver
= wxWINDOWS_NT
;
1013 case VER_PLATFORM_WIN32_CE
:
1014 s_ver
= wxWINDOWS_CE
;
1021 static wxToolkitInfo info
;
1022 info
.versionMajor
= s_major
;
1023 info
.versionMinor
= s_minor
;
1025 info
.name
= _T("wxBase");
1029 // ----------------------------------------------------------------------------
1031 // ----------------------------------------------------------------------------
1033 void wxMilliSleep(unsigned long milliseconds
)
1035 ::Sleep(milliseconds
);
1038 void wxMicroSleep(unsigned long microseconds
)
1040 wxMilliSleep(microseconds
/1000);
1043 void wxSleep(int nSecs
)
1045 wxMilliSleep(1000*nSecs
);
1048 // ----------------------------------------------------------------------------
1049 // font encoding <-> Win32 codepage conversion functions
1050 // ----------------------------------------------------------------------------
1052 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
)
1056 // although this function is supposed to return an exact match, do do
1057 // some mappings here for the most common case of "standard" encoding
1058 case wxFONTENCODING_SYSTEM
:
1059 return DEFAULT_CHARSET
;
1061 case wxFONTENCODING_ISO8859_1
:
1062 case wxFONTENCODING_ISO8859_15
:
1063 case wxFONTENCODING_CP1252
:
1064 return ANSI_CHARSET
;
1066 #if !defined(__WXMICROWIN__)
1067 // The following four fonts are multi-byte charsets
1068 case wxFONTENCODING_CP932
:
1069 return SHIFTJIS_CHARSET
;
1071 case wxFONTENCODING_CP936
:
1072 return GB2312_CHARSET
;
1074 case wxFONTENCODING_CP949
:
1075 return HANGUL_CHARSET
;
1077 case wxFONTENCODING_CP950
:
1078 return CHINESEBIG5_CHARSET
;
1080 // The rest are single byte encodings
1081 case wxFONTENCODING_CP1250
:
1082 return EASTEUROPE_CHARSET
;
1084 case wxFONTENCODING_CP1251
:
1085 return RUSSIAN_CHARSET
;
1087 case wxFONTENCODING_CP1253
:
1088 return GREEK_CHARSET
;
1090 case wxFONTENCODING_CP1254
:
1091 return TURKISH_CHARSET
;
1093 case wxFONTENCODING_CP1255
:
1094 return HEBREW_CHARSET
;
1096 case wxFONTENCODING_CP1256
:
1097 return ARABIC_CHARSET
;
1099 case wxFONTENCODING_CP1257
:
1100 return BALTIC_CHARSET
;
1102 case wxFONTENCODING_CP874
:
1103 return THAI_CHARSET
;
1104 #endif // !__WXMICROWIN__
1106 case wxFONTENCODING_CP437
:
1110 // no way to translate this encoding into a Windows charset
1115 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1116 // looks up the vlaues in the registry and the new one which is more
1117 // politically correct and has more chances to work on other Windows versions
1118 // as well but the old version is still needed for !wxUSE_FONTMAP case
1121 #include "wx/fontmap.h"
1123 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
)
1125 // translate encoding into the Windows CHARSET
1126 long charset
= wxEncodingToCharset(encoding
);
1127 if ( charset
== -1 )
1130 // translate CHARSET to code page
1131 CHARSETINFO csetInfo
;
1132 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1136 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1141 return csetInfo
.ciACP
;
1144 extern long wxCharsetToCodepage(const wxChar
*name
)
1146 // first get the font encoding for this charset
1150 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1151 if ( enc
== wxFONTENCODING_SYSTEM
)
1154 // the use the helper function
1155 return wxEncodingToCodepage(enc
);
1158 #else // !wxUSE_FONTMAP
1160 #include "wx/msw/registry.h"
1162 // this should work if Internet Exploiter is installed
1163 extern long wxCharsetToCodepage(const wxChar
*name
)
1170 wxString
path(wxT("MIME\\Database\\Charset\\"));
1173 // follow the alias loop
1176 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1181 // two cases: either there's an AliasForCharset string,
1182 // or there are Codepage and InternetEncoding dwords.
1183 // The InternetEncoding gives us the actual encoding,
1184 // the Codepage just says which Windows character set to
1185 // use when displaying the data.
1186 if (key
.HasValue(wxT("InternetEncoding")) &&
1187 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1190 // no encoding, see if it's an alias
1191 if (!key
.HasValue(wxT("AliasForCharset")) ||
1192 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1199 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1202 Creates a hidden window with supplied window proc registering the class for
1203 it if necesssary (i.e. the first time only). Caller is responsible for
1204 destroying the window and unregistering the class (note that this must be
1205 done because wxWidgets may be used as a DLL and so may be loaded/unloaded
1206 multiple times into/from the same process so we cna't rely on automatic
1207 Windows class unregistration).
1209 pclassname is a pointer to a caller stored classname, which must initially be
1210 NULL. classname is the desired wndclass classname. If function succesfully
1211 registers the class, pclassname will be set to classname.
1213 extern "C" WXDLLIMPEXP_BASE HWND
1214 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
)
1216 wxCHECK_MSG( classname
&& pclassname
&& wndproc
, NULL
,
1217 _T("NULL parameter in wxCreateHiddenWindow") );
1219 // register the class fi we need to first
1220 if ( *pclassname
== NULL
)
1223 wxZeroMemory(wndclass
);
1225 wndclass
.lpfnWndProc
= wndproc
;
1226 wndclass
.hInstance
= wxGetInstance();
1227 wndclass
.lpszClassName
= classname
;
1229 if ( !::RegisterClass(&wndclass
) )
1231 wxLogLastError(wxT("RegisterClass() in wxCreateHiddenWindow"));
1236 *pclassname
= classname
;
1239 // next create the window
1240 HWND hwnd
= ::CreateWindow
1254 wxLogLastError(wxT("CreateWindow() in wxCreateHiddenWindow"));