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 #ifdef __GNUWIN32_OLD__
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 #if wxUSE_ON_FATAL_EXCEPTION
98 static bool gs_handleExceptions
= FALSE
;
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // In the WIN.INI file
106 static const wxChar WX_SECTION
[] = wxT("wxWindows");
107 static const wxChar eUSERNAME
[] = wxT("UserName");
109 // these are only used under Win16
110 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
111 static const wxChar eHOSTNAME
[] = wxT("HostName");
112 static const wxChar eUSERID
[] = wxT("UserId");
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // get host name and related
121 // ----------------------------------------------------------------------------
123 // Get hostname only (without domain name)
124 bool wxGetHostName(wxChar
*buf
, int maxSize
)
126 #if defined(__WXWINCE__)
128 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
129 DWORD nSize
= maxSize
;
130 if ( !::GetComputerName(buf
, &nSize
) )
132 wxLogLastError(wxT("GetComputerName"));
140 const wxChar
*default_host
= wxT("noname");
142 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
143 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
145 wxStrncpy(buf
, sysname
, maxSize
- 1);
146 buf
[maxSize
] = wxT('\0');
147 return *buf
? TRUE
: FALSE
;
151 // get full hostname (with domain name if possible)
152 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
154 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
155 // TODO should use GetComputerNameEx() when available
157 // the idea is that if someone had set wxUSE_SOCKETS to 0 the code
158 // shouldn't use winsock.dll (a.k.a. ws2_32.dll) at all so only use this
159 // code if we link with it anyhow
163 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
167 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
169 // gethostname() won't usually include the DNS domain name, for
170 // this we need to work a bit more
171 if ( !strchr(bufA
, '.') )
173 struct hostent
*pHostEnt
= gethostbyname(bufA
);
177 // Windows will use DNS internally now
178 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, AF_INET
);
183 host
= wxString::FromAscii(pHostEnt
->h_name
);
192 wxStrncpy(buf
, host
, maxSize
);
198 #endif // wxUSE_SOCKETS
202 return wxGetHostName(buf
, maxSize
);
205 // Get user ID e.g. jacs
206 bool wxGetUserId(wxChar
*buf
, int maxSize
)
208 #if defined(__WXWINCE__)
210 #elif defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
211 DWORD nSize
= maxSize
;
212 if ( ::GetUserName(buf
, &nSize
) == 0 )
214 // actually, it does happen on Win9x if the user didn't log on
215 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
224 #else // Win16 or Win32s
226 const wxChar
*default_id
= wxT("anonymous");
228 // Can't assume we have NIS (PC-NFS) or some other ID daemon
230 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
231 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
233 // Use wxWindows configuration data (comming soon)
234 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
238 wxStrncpy(buf
, user
, maxSize
- 1);
241 return *buf
? TRUE
: FALSE
;
245 // Get user name e.g. Julian Smart
246 bool wxGetUserName(wxChar
*buf
, int maxSize
)
248 #if defined(__WXWINCE__)
250 #elif defined(USE_NET_API)
251 CHAR szUserName
[256];
252 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
255 // TODO how to get the domain name?
258 // the code is based on the MSDN example (also see KB article Q119670)
259 WCHAR wszUserName
[256]; // Unicode user name
260 WCHAR wszDomain
[256];
263 USER_INFO_2
*ui2
; // User structure
265 // Convert ANSI user name and domain to Unicode
266 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
267 wszUserName
, WXSIZEOF(wszUserName
) );
268 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
269 wszDomain
, WXSIZEOF(wszDomain
) );
271 // Get the computer name of a DC for the domain.
272 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
274 wxLogError(wxT("Can not find domain controller"));
279 // Look up the user on the DC
280 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
281 (LPWSTR
)&wszUserName
,
282 2, // level - we want USER_INFO_2
290 case NERR_InvalidComputer
:
291 wxLogError(wxT("Invalid domain controller name."));
295 case NERR_UserNotFound
:
296 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
301 wxLogSysError(wxT("Can't get information about user"));
306 // Convert the Unicode full name to ANSI
307 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
308 buf
, maxSize
, NULL
, NULL
);
313 wxLogError(wxT("Couldn't look up full user name."));
316 #else // !USE_NET_API
317 // Could use NIS, MS-Mail or other site specific programs
318 // Use wxWindows configuration data
319 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxEmptyString
, buf
, maxSize
- 1) != 0;
322 ok
= wxGetUserId(buf
, maxSize
);
327 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
334 const wxChar
* wxGetHomeDir(wxString
*pstr
)
336 wxString
& strDir
= *pstr
;
338 #if defined(__UNIX__)
339 const wxChar
*szHome
= wxGetenv("HOME");
340 if ( szHome
== NULL
) {
342 wxLogWarning(_("can't find user's HOME, using current directory."));
348 // add a trailing slash if needed
349 if ( strDir
.Last() != wxT('/') )
353 // Cygwin returns unix type path but that does not work well
354 static wxChar windowsPath
[MAX_PATH
];
355 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
356 strDir
= windowsPath
;
358 #elif defined(__WXWINCE__)
364 // If we have a valid HOME directory, as is used on many machines that
365 // have unix utilities on them, we should use that.
366 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
368 if ( szHome
!= NULL
)
372 else // no HOME, try HOMEDRIVE/PATH
374 szHome
= wxGetenv(wxT("HOMEDRIVE"));
375 if ( szHome
!= NULL
)
377 szHome
= wxGetenv(wxT("HOMEPATH"));
379 if ( szHome
!= NULL
)
383 // the idea is that under NT these variables have default values
384 // of "%systemdrive%:" and "\\". As we don't want to create our
385 // config files in the root directory of the system drive, we will
386 // create it in our program's dir. However, if the user took care
387 // to set HOMEPATH to something other than "\\", we suppose that he
388 // knows what he is doing and use the supplied value.
389 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
394 if ( strDir
.empty() )
396 // If we have a valid USERPROFILE directory, as is the case in
397 // Windows NT, 2000 and XP, we should use that as our home directory.
398 szHome
= wxGetenv(wxT("USERPROFILE"));
400 if ( szHome
!= NULL
)
404 if ( !strDir
.empty() )
406 return strDir
.c_str();
408 //else: fall back to the prograrm directory
410 // Win16 has no idea about home, so use the executable directory instead
413 // 260 was taken from windef.h
419 ::GetModuleFileName(::GetModuleHandle(NULL
),
420 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
421 strPath
.UngetWriteBuf();
423 // extract the dir name
424 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
428 return strDir
.c_str();
431 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
433 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
434 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
435 // static buffer and sometimes I don't even know what.
436 static wxString s_home
;
438 return (wxChar
*)wxGetHomeDir(&s_home
);
441 bool wxDirExists(const wxString
& dir
)
443 #ifdef __WXMICROWIN__
444 return wxPathExist(dir
);
445 #elif defined(__WIN32__)
446 DWORD attribs
= GetFileAttributes(dir
);
447 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
450 struct ffblk fileInfo
;
452 struct find_t fileInfo
;
454 // In Borland findfirst has a different argument
455 // ordering from _dos_findfirst. But _dos_findfirst
456 // _should_ be ok in both MS and Borland... why not?
458 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
459 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
461 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
462 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
467 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
475 // old w32api don't have ULARGE_INTEGER
476 #if defined(__WIN32__) && \
477 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
478 // GetDiskFreeSpaceEx() is not available under original Win95, check for
480 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
486 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
488 ::GetModuleHandle(_T("kernel32.dll")),
490 "GetDiskFreeSpaceExW"
492 "GetDiskFreeSpaceExA"
496 if ( pGetDiskFreeSpaceEx
)
498 ULARGE_INTEGER bytesFree
, bytesTotal
;
500 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
501 if ( !pGetDiskFreeSpaceEx(path
,
506 wxLogLastError(_T("GetDiskFreeSpaceEx"));
511 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
512 // two 32 bit fields which may be or may be not named - try to make it
513 // compile in all cases
514 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
521 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
526 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
532 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
533 // should be used instead - but if it's not available, fall back on
534 // GetDiskFreeSpace() nevertheless...
536 DWORD lSectorsPerCluster
,
538 lNumberOfFreeClusters
,
539 lTotalNumberOfClusters
;
541 // FIXME: this is wrong, we should extract the root drive from path
542 // instead, but this is the job for wxFileName...
543 if ( !::GetDiskFreeSpace(path
,
546 &lNumberOfFreeClusters
,
547 &lTotalNumberOfClusters
) )
549 wxLogLastError(_T("GetDiskFreeSpace"));
554 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
555 lBytesPerCluster
*= lBytesPerSector
;
559 *pTotal
= lBytesPerCluster
;
560 *pTotal
*= lTotalNumberOfClusters
;
565 *pFree
= lBytesPerCluster
;
566 *pFree
*= lNumberOfFreeClusters
;
575 // ----------------------------------------------------------------------------
577 // ----------------------------------------------------------------------------
579 bool wxGetEnv(const wxString
& var
, wxString
*value
)
583 #elif defined(__WIN16__)
584 const wxChar
* ret
= wxGetenv(var
);
595 // first get the size of the buffer
596 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
599 // this means that there is no such variable
605 (void)::GetEnvironmentVariable(var
, value
->GetWriteBuf(dwRet
), dwRet
);
606 value
->UngetWriteBuf();
613 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
615 // some compilers have putenv() or _putenv() or _wputenv() but it's better
616 // to always use Win32 function directly instead of dealing with them
617 #if defined(__WIN32__) && !defined(__WXWINCE__)
618 if ( !::SetEnvironmentVariable(var
, value
) )
620 wxLogLastError(_T("SetEnvironmentVariable"));
626 #else // no way to set env vars
631 // ----------------------------------------------------------------------------
632 // process management
633 // ----------------------------------------------------------------------------
635 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
636 struct wxFindByPidParams
638 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
640 // the HWND used to return the result
643 // the PID we're looking from
646 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
649 // wxKill helper: EnumWindows() callback which is used to find the first (top
650 // level) window belonging to the given process
651 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
654 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
656 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
657 if ( pid
== params
->pid
)
659 // remember the window we found
662 // return FALSE to stop the enumeration
666 // continue enumeration
670 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
672 // get the process handle to operate on
673 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
675 PROCESS_QUERY_INFORMATION
,
676 FALSE
, // not inheritable
678 if ( hProcess
== NULL
)
682 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
684 *krc
= wxKILL_ACCESS_DENIED
;
688 *krc
= wxKILL_NO_PROCESS
;
699 // kill the process forcefully returning -1 as error code
700 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
702 wxLogSysError(_("Failed to kill process %d"), pid
);
706 // this is not supposed to happen if we could open the
716 // do nothing, we just want to test for process existence
720 // any other signal means "terminate"
722 wxFindByPidParams params
;
723 params
.pid
= (DWORD
)pid
;
725 // EnumWindows() has nice semantics: it returns 0 if it found
726 // something or if an error occured and non zero if it
727 // enumerated all the window
728 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
730 // did we find any window?
733 // tell the app to close
735 // NB: this is the harshest way, the app won't have
736 // opportunity to save any files, for example, but
737 // this is probably what we want here. If not we
738 // can also use SendMesageTimeout(WM_CLOSE)
739 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
741 wxLogLastError(_T("PostMessage(WM_QUIT)"));
744 else // it was an error then
746 wxLogLastError(_T("EnumWindows"));
751 else // no windows for this PID
768 // as we wait for a short time, we can use just WaitForSingleObject()
769 // and not MsgWaitForMultipleObjects()
770 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
773 // process terminated
774 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
776 wxLogLastError(_T("GetExitCodeProcess"));
781 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
785 wxLogLastError(_T("WaitForSingleObject"));
800 // just to suppress the warnings about uninitialized variable
804 ::CloseHandle(hProcess
);
806 // the return code is the same as from Unix kill(): 0 if killed
807 // successfully or -1 on error
809 // be careful to interpret rc correctly: for wxSIGNONE we return success if
810 // the process exists, for all the other sig values -- if it doesn't
812 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
826 // Execute a program in an Interactive Shell
827 bool wxShell(const wxString
& command
)
832 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
834 shell
= (wxChar
*) wxT("\\COMMAND.COM");
844 // pass the command to execute to the command processor
845 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
848 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
852 // Shutdown or reboot the PC
853 bool wxShutdown(wxShutdownFlags wFlags
)
857 #elif defined(__WIN32__)
860 if ( wxGetOsVersion(NULL
, NULL
) == wxWINDOWS_NT
) // if is NT or 2K
862 // Get a token for this process.
864 bOK
= ::OpenProcessToken(GetCurrentProcess(),
865 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
,
869 TOKEN_PRIVILEGES tkp
;
871 // Get the LUID for the shutdown privilege.
872 ::LookupPrivilegeValue(NULL
, SE_SHUTDOWN_NAME
,
873 &tkp
.Privileges
[0].Luid
);
875 tkp
.PrivilegeCount
= 1; // one privilege to set
876 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
878 // Get the shutdown privilege for this process.
879 ::AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0,
880 (PTOKEN_PRIVILEGES
)NULL
, 0);
882 // Cannot test the return value of AdjustTokenPrivileges.
883 bOK
= ::GetLastError() == ERROR_SUCCESS
;
889 UINT flags
= EWX_SHUTDOWN
| EWX_FORCE
;
892 case wxSHUTDOWN_POWEROFF
:
893 flags
|= EWX_POWEROFF
;
896 case wxSHUTDOWN_REBOOT
:
901 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
905 bOK
= ::ExitWindowsEx(EWX_SHUTDOWN
| EWX_FORCE
| EWX_REBOOT
, 0) != 0;
914 // ----------------------------------------------------------------------------
916 // ----------------------------------------------------------------------------
918 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
919 long wxGetFreeMemory()
921 #if defined(__WIN32__) && !defined(__BORLANDC__)
922 MEMORYSTATUS memStatus
;
923 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
924 GlobalMemoryStatus(&memStatus
);
925 return memStatus
.dwAvailPhys
;
927 return (long)GetFreeSpace(0);
931 unsigned long wxGetProcessId()
934 return ::GetCurrentProcessId();
943 ::MessageBeep((UINT
)-1); // default sound
946 wxString
wxGetOsDescription()
954 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
955 if ( ::GetVersionEx(&info
) )
957 switch ( info
.dwPlatformId
)
959 case VER_PLATFORM_WIN32s
:
960 str
= _("Win32s on Windows 3.1");
963 case VER_PLATFORM_WIN32_WINDOWS
:
964 str
.Printf(_("Windows 9%c"),
965 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
966 if ( !wxIsEmpty(info
.szCSDVersion
) )
968 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
972 case VER_PLATFORM_WIN32_NT
:
973 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
977 if ( !wxIsEmpty(info
.szCSDVersion
) )
979 str
<< _T(", ") << info
.szCSDVersion
;
987 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
992 return _("Windows 3.1");
996 int wxAppTraits::GetOSVersion(int *verMaj
, int *verMin
)
998 // cache the version info, it's not going to change
1000 // NB: this is MT-safe, we may use these static vars from different threads
1001 // but as they always have the same value it doesn't matter
1002 static int s_ver
= -1,
1012 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1013 if ( ::GetVersionEx(&info
) )
1015 s_major
= info
.dwMajorVersion
;
1016 s_minor
= info
.dwMinorVersion
;
1018 switch ( info
.dwPlatformId
)
1020 case VER_PLATFORM_WIN32s
:
1024 case VER_PLATFORM_WIN32_WINDOWS
:
1028 case VER_PLATFORM_WIN32_NT
:
1029 s_ver
= wxWINDOWS_NT
;
1032 case VER_PLATFORM_WIN32_CE
:
1033 s_ver
= wxWINDOWS_CE
;
1048 // ----------------------------------------------------------------------------
1050 // ----------------------------------------------------------------------------
1052 void wxUsleep(unsigned long milliseconds
)
1054 ::Sleep(milliseconds
);
1057 void wxSleep(int nSecs
)
1059 wxUsleep(1000*nSecs
);
1062 // ----------------------------------------------------------------------------
1063 // font encoding <-> Win32 codepage conversion functions
1064 // ----------------------------------------------------------------------------
1066 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
)
1070 // although this function is supposed to return an exact match, do do
1071 // some mappings here for the most common case of "standard" encoding
1072 case wxFONTENCODING_SYSTEM
:
1073 return DEFAULT_CHARSET
;
1075 case wxFONTENCODING_ISO8859_1
:
1076 case wxFONTENCODING_ISO8859_15
:
1077 case wxFONTENCODING_CP1252
:
1078 return ANSI_CHARSET
;
1080 #if !defined(__WXMICROWIN__)
1081 // The following four fonts are multi-byte charsets
1082 case wxFONTENCODING_CP932
:
1083 return SHIFTJIS_CHARSET
;
1085 case wxFONTENCODING_CP936
:
1086 return GB2312_CHARSET
;
1088 case wxFONTENCODING_CP949
:
1089 return HANGUL_CHARSET
;
1091 case wxFONTENCODING_CP950
:
1092 return CHINESEBIG5_CHARSET
;
1094 // The rest are single byte encodings
1095 case wxFONTENCODING_CP1250
:
1096 return EASTEUROPE_CHARSET
;
1098 case wxFONTENCODING_CP1251
:
1099 return RUSSIAN_CHARSET
;
1101 case wxFONTENCODING_CP1253
:
1102 return GREEK_CHARSET
;
1104 case wxFONTENCODING_CP1254
:
1105 return TURKISH_CHARSET
;
1107 case wxFONTENCODING_CP1255
:
1108 return HEBREW_CHARSET
;
1110 case wxFONTENCODING_CP1256
:
1111 return ARABIC_CHARSET
;
1113 case wxFONTENCODING_CP1257
:
1114 return BALTIC_CHARSET
;
1116 case wxFONTENCODING_CP874
:
1117 return THAI_CHARSET
;
1118 #endif // !__WXMICROWIN__
1120 case wxFONTENCODING_CP437
:
1124 // no way to translate this encoding into a Windows charset
1129 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1130 // looks up the vlaues in the registry and the new one which is more
1131 // politically correct and has more chances to work on other Windows versions
1132 // as well but the old version is still needed for !wxUSE_FONTMAP case
1135 #include "wx/fontmap.h"
1137 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
)
1139 // translate encoding into the Windows CHARSET
1140 long charset
= wxEncodingToCharset(encoding
);
1141 if ( charset
== -1 )
1144 // translate CHARSET to code page
1145 CHARSETINFO csetInfo
;
1146 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1150 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1155 return csetInfo
.ciACP
;
1158 extern long wxCharsetToCodepage(const wxChar
*name
)
1160 // first get the font encoding for this charset
1164 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1165 if ( enc
== wxFONTENCODING_SYSTEM
)
1168 // the use the helper function
1169 return wxEncodingToCodepage(enc
);
1172 #else // !wxUSE_FONTMAP
1174 #include "wx/msw/registry.h"
1176 // this should work if Internet Exploiter is installed
1177 extern long wxCharsetToCodepage(const wxChar
*name
)
1184 wxString
path(wxT("MIME\\Database\\Charset\\"));
1187 // follow the alias loop
1190 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1195 // two cases: either there's an AliasForCharset string,
1196 // or there are Codepage and InternetEncoding dwords.
1197 // The InternetEncoding gives us the actual encoding,
1198 // the Codepage just says which Windows character set to
1199 // use when displaying the data.
1200 if (key
.HasValue(wxT("InternetEncoding")) &&
1201 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1204 // no encoding, see if it's an alias
1205 if (!key
.HasValue(wxT("AliasForCharset")) ||
1206 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1213 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1215 // ----------------------------------------------------------------------------
1216 // wxApp::OnFatalException() support
1217 // ----------------------------------------------------------------------------
1219 bool wxHandleFatalExceptions(bool doit
)
1221 #if wxUSE_ON_FATAL_EXCEPTION
1222 // assume this can only be called from the main thread
1223 gs_handleExceptions
= doit
;
1227 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to use this function"));
1234 #if wxUSE_ON_FATAL_EXCEPTION
1236 extern unsigned long wxGlobalSEHandler()
1238 if ( gs_handleExceptions
&& wxTheApp
)
1240 // give the user a chance to do something special about this
1241 wxTheApp
->OnFatalException();
1243 // this will execute our handler and terminate the process
1244 return EXCEPTION_EXECUTE_HANDLER
;
1247 return EXCEPTION_CONTINUE_SEARCH
;
1250 #endif // wxUSE_ON_FATAL_EXCEPTION