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 #if wxUSE_ON_FATAL_EXCEPTION
97 static bool gs_handleExceptions
= FALSE
;
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // In the WIN.INI file
105 static const wxChar WX_SECTION
[] = wxT("wxWindows");
106 static const wxChar eUSERNAME
[] = wxT("UserName");
108 // these are only used under Win16
109 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
110 static const wxChar eHOSTNAME
[] = wxT("HostName");
111 static const wxChar eUSERID
[] = wxT("UserId");
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
119 // get host name and related
120 // ----------------------------------------------------------------------------
122 // Get hostname only (without domain name)
123 bool wxGetHostName(wxChar
*buf
, int maxSize
)
125 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
126 DWORD nSize
= maxSize
;
127 if ( !::GetComputerName(buf
, &nSize
) )
129 wxLogLastError(wxT("GetComputerName"));
137 const wxChar
*default_host
= wxT("noname");
139 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
140 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
142 wxStrncpy(buf
, sysname
, maxSize
- 1);
143 buf
[maxSize
] = wxT('\0');
144 return *buf
? TRUE
: FALSE
;
148 // get full hostname (with domain name if possible)
149 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
151 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
152 // TODO should use GetComputerNameEx() when available
154 // the idea is that if someone had set wxUSE_SOCKETS to 0 the code
155 // shouldn't use winsock.dll (a.k.a. ws2_32.dll) at all so only use this
156 // code if we link with it anyhow
160 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
164 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
166 // gethostname() won't usually include the DNS domain name, for
167 // this we need to work a bit more
168 if ( !strchr(bufA
, '.') )
170 struct hostent
*pHostEnt
= gethostbyname(bufA
);
174 // Windows will use DNS internally now
175 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, AF_INET
);
180 host
= wxString::FromAscii(pHostEnt
->h_name
);
189 wxStrncpy(buf
, host
, maxSize
);
195 #endif // wxUSE_SOCKETS
199 return wxGetHostName(buf
, maxSize
);
202 // Get user ID e.g. jacs
203 bool wxGetUserId(wxChar
*buf
, int maxSize
)
205 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
206 DWORD nSize
= maxSize
;
207 if ( ::GetUserName(buf
, &nSize
) == 0 )
209 // actually, it does happen on Win9x if the user didn't log on
210 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
219 #else // Win16 or Win32s
221 const wxChar
*default_id
= wxT("anonymous");
223 // Can't assume we have NIS (PC-NFS) or some other ID daemon
225 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
226 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
228 // Use wxWindows configuration data (comming soon)
229 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
233 wxStrncpy(buf
, user
, maxSize
- 1);
236 return *buf
? TRUE
: FALSE
;
240 // Get user name e.g. Julian Smart
241 bool wxGetUserName(wxChar
*buf
, int maxSize
)
243 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
244 extern HANDLE g_hPenWin
; // PenWindows Running?
247 // PenWindows Does have a user concept!
248 // Get the current owner of the recognizer
249 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
250 strncpy(buf
, wxBuffer
, maxSize
- 1);
256 CHAR szUserName
[256];
257 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
260 // TODO how to get the domain name?
263 // the code is based on the MSDN example (also see KB article Q119670)
264 WCHAR wszUserName
[256]; // Unicode user name
265 WCHAR wszDomain
[256];
268 USER_INFO_2
*ui2
; // User structure
270 // Convert ANSI user name and domain to Unicode
271 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
272 wszUserName
, WXSIZEOF(wszUserName
) );
273 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
274 wszDomain
, WXSIZEOF(wszDomain
) );
276 // Get the computer name of a DC for the domain.
277 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
279 wxLogError(wxT("Can not find domain controller"));
284 // Look up the user on the DC
285 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
286 (LPWSTR
)&wszUserName
,
287 2, // level - we want USER_INFO_2
295 case NERR_InvalidComputer
:
296 wxLogError(wxT("Invalid domain controller name."));
300 case NERR_UserNotFound
:
301 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
306 wxLogSysError(wxT("Can't get information about user"));
311 // Convert the Unicode full name to ANSI
312 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
313 buf
, maxSize
, NULL
, NULL
);
318 wxLogError(wxT("Couldn't look up full user name."));
321 #else // !USE_NET_API
322 // Could use NIS, MS-Mail or other site specific programs
323 // Use wxWindows configuration data
324 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxT(""), buf
, maxSize
- 1) != 0;
327 ok
= wxGetUserId(buf
, maxSize
);
332 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
340 const wxChar
* wxGetHomeDir(wxString
*pstr
)
342 wxString
& strDir
= *pstr
;
344 #if defined(__UNIX__)
345 const wxChar
*szHome
= wxGetenv("HOME");
346 if ( szHome
== NULL
) {
348 wxLogWarning(_("can't find user's HOME, using current directory."));
354 // add a trailing slash if needed
355 if ( strDir
.Last() != wxT('/') )
359 // Cygwin returns unix type path but that does not work well
360 static wxChar windowsPath
[MAX_PATH
];
361 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
362 strDir
= windowsPath
;
368 // If we have a valid HOME directory, as is used on many machines that
369 // have unix utilities on them, we should use that.
370 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
372 if ( szHome
!= NULL
)
376 else // no HOME, try HOMEDRIVE/PATH
378 szHome
= wxGetenv(wxT("HOMEDRIVE"));
379 if ( szHome
!= NULL
)
381 szHome
= wxGetenv(wxT("HOMEPATH"));
383 if ( szHome
!= NULL
)
387 // the idea is that under NT these variables have default values
388 // of "%systemdrive%:" and "\\". As we don't want to create our
389 // config files in the root directory of the system drive, we will
390 // create it in our program's dir. However, if the user took care
391 // to set HOMEPATH to something other than "\\", we suppose that he
392 // knows what he is doing and use the supplied value.
393 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
398 if ( strDir
.empty() )
400 // If we have a valid USERPROFILE directory, as is the case in
401 // Windows NT, 2000 and XP, we should use that as our home directory.
402 szHome
= wxGetenv(wxT("USERPROFILE"));
404 if ( szHome
!= NULL
)
408 if ( !strDir
.empty() )
410 return strDir
.c_str();
412 //else: fall back to the prograrm directory
414 // Win16 has no idea about home, so use the executable directory instead
417 // 260 was taken from windef.h
423 ::GetModuleFileName(::GetModuleHandle(NULL
),
424 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
425 strPath
.UngetWriteBuf();
427 // extract the dir name
428 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
432 return strDir
.c_str();
435 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
437 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
438 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
439 // static buffer and sometimes I don't even know what.
440 static wxString s_home
;
442 return (wxChar
*)wxGetHomeDir(&s_home
);
445 bool wxDirExists(const wxString
& dir
)
447 #ifdef __WXMICROWIN__
448 return wxPathExist(dir
);
449 #elif defined(__WIN32__)
450 DWORD attribs
= GetFileAttributes(dir
);
451 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
454 struct ffblk fileInfo
;
456 struct find_t fileInfo
;
458 // In Borland findfirst has a different argument
459 // ordering from _dos_findfirst. But _dos_findfirst
460 // _should_ be ok in both MS and Borland... why not?
462 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
463 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
465 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
466 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
471 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
476 // old w32api don't have ULARGE_INTEGER
477 #if defined(__WIN32__) && \
478 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
479 // GetDiskFreeSpaceEx() is not available under original Win95, check for
481 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
487 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
489 ::GetModuleHandle(_T("kernel32.dll")),
491 "GetDiskFreeSpaceExW"
493 "GetDiskFreeSpaceExA"
497 if ( pGetDiskFreeSpaceEx
)
499 ULARGE_INTEGER bytesFree
, bytesTotal
;
501 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
502 if ( !pGetDiskFreeSpaceEx(path
,
507 wxLogLastError(_T("GetDiskFreeSpaceEx"));
512 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
513 // two 32 bit fields which may be or may be not named - try to make it
514 // compile in all cases
515 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
522 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
527 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
533 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
534 // should be used instead - but if it's not available, fall back on
535 // GetDiskFreeSpace() nevertheless...
537 DWORD lSectorsPerCluster
,
539 lNumberOfFreeClusters
,
540 lTotalNumberOfClusters
;
542 // FIXME: this is wrong, we should extract the root drive from path
543 // instead, but this is the job for wxFileName...
544 if ( !::GetDiskFreeSpace(path
,
547 &lNumberOfFreeClusters
,
548 &lTotalNumberOfClusters
) )
550 wxLogLastError(_T("GetDiskFreeSpace"));
555 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
556 lBytesPerCluster
*= lBytesPerSector
;
560 *pTotal
= lBytesPerCluster
;
561 *pTotal
*= lTotalNumberOfClusters
;
566 *pFree
= lBytesPerCluster
;
567 *pFree
*= lNumberOfFreeClusters
;
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 bool wxGetEnv(const wxString
& var
, wxString
*value
)
581 const wxChar
* ret
= wxGetenv(var
);
592 // first get the size of the buffer
593 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
596 // this means that there is no such variable
602 (void)::GetEnvironmentVariable(var
, value
->GetWriteBuf(dwRet
), dwRet
);
603 value
->UngetWriteBuf();
610 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
612 // some compilers have putenv() or _putenv() or _wputenv() but it's better
613 // to always use Win32 function directly instead of dealing with them
614 #if defined(__WIN32__)
615 if ( !::SetEnvironmentVariable(var
, value
) )
617 wxLogLastError(_T("SetEnvironmentVariable"));
623 #else // no way to set env vars
628 // ----------------------------------------------------------------------------
629 // process management
630 // ----------------------------------------------------------------------------
632 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
633 struct wxFindByPidParams
635 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
637 // the HWND used to return the result
640 // the PID we're looking from
643 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
646 // wxKill helper: EnumWindows() callback which is used to find the first (top
647 // level) window belonging to the given process
648 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
651 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
653 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
654 if ( pid
== params
->pid
)
656 // remember the window we found
659 // return FALSE to stop the enumeration
663 // continue enumeration
667 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
669 // get the process handle to operate on
670 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
672 PROCESS_QUERY_INFORMATION
,
673 FALSE
, // not inheritable
675 if ( hProcess
== NULL
)
679 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
681 *krc
= wxKILL_ACCESS_DENIED
;
685 *krc
= wxKILL_NO_PROCESS
;
696 // kill the process forcefully returning -1 as error code
697 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
699 wxLogSysError(_("Failed to kill process %d"), pid
);
703 // this is not supposed to happen if we could open the
713 // do nothing, we just want to test for process existence
717 // any other signal means "terminate"
719 wxFindByPidParams params
;
720 params
.pid
= (DWORD
)pid
;
722 // EnumWindows() has nice semantics: it returns 0 if it found
723 // something or if an error occured and non zero if it
724 // enumerated all the window
725 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
727 // did we find any window?
730 // tell the app to close
732 // NB: this is the harshest way, the app won't have
733 // opportunity to save any files, for example, but
734 // this is probably what we want here. If not we
735 // can also use SendMesageTimeout(WM_CLOSE)
736 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
738 wxLogLastError(_T("PostMessage(WM_QUIT)"));
741 else // it was an error then
743 wxLogLastError(_T("EnumWindows"));
748 else // no windows for this PID
765 // as we wait for a short time, we can use just WaitForSingleObject()
766 // and not MsgWaitForMultipleObjects()
767 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
770 // process terminated
771 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
773 wxLogLastError(_T("GetExitCodeProcess"));
778 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
782 wxLogLastError(_T("WaitForSingleObject"));
797 // just to suppress the warnings about uninitialized variable
801 ::CloseHandle(hProcess
);
803 // the return code is the same as from Unix kill(): 0 if killed
804 // successfully or -1 on error
806 // be careful to interpret rc correctly: for wxSIGNONE we return success if
807 // the process exists, for all the other sig values -- if it doesn't
809 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
823 // Execute a program in an Interactive Shell
824 bool wxShell(const wxString
& command
)
826 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
828 shell
= (wxChar
*) wxT("\\COMMAND.COM");
838 // pass the command to execute to the command processor
839 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
842 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
845 // Shutdown or reboot the PC
846 bool wxShutdown(wxShutdownFlags wFlags
)
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
;
1034 // ----------------------------------------------------------------------------
1036 // ----------------------------------------------------------------------------
1038 void wxUsleep(unsigned long milliseconds
)
1040 ::Sleep(milliseconds
);
1043 void wxSleep(int nSecs
)
1045 wxUsleep(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
1201 // ----------------------------------------------------------------------------
1202 // wxApp::OnFatalException() support
1203 // ----------------------------------------------------------------------------
1205 bool wxHandleFatalExceptions(bool doit
)
1207 #if wxUSE_ON_FATAL_EXCEPTION
1208 // assume this can only be called from the main thread
1209 gs_handleExceptions
= doit
;
1213 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to use this function"));
1220 #if wxUSE_ON_FATAL_EXCEPTION
1222 extern unsigned long wxGlobalSEHandler()
1224 if ( gs_handleExceptions
&& wxTheApp
)
1226 // give the user a chance to do something special about this
1227 wxTheApp
->OnFatalException();
1229 // this will execute our handler and terminate the process
1230 return EXCEPTION_EXECUTE_HANDLER
;
1233 return EXCEPTION_CONTINUE_SEARCH
;
1236 #endif // wxUSE_ON_FATAL_EXCEPTION