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 // these are only used under Win16
111 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
112 static const wxChar eHOSTNAME
[] = wxT("HostName");
113 static const wxChar eUSERID
[] = wxT("UserId");
116 // ============================================================================
118 // ============================================================================
120 // ----------------------------------------------------------------------------
121 // get host name and related
122 // ----------------------------------------------------------------------------
124 // Get hostname only (without domain name)
125 bool wxGetHostName(wxChar
*buf
, int maxSize
)
127 #if defined(__WXWINCE__)
129 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
130 DWORD nSize
= maxSize
;
131 if ( !::GetComputerName(buf
, &nSize
) )
133 wxLogLastError(wxT("GetComputerName"));
141 const wxChar
*default_host
= wxT("noname");
143 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
144 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
146 wxStrncpy(buf
, sysname
, maxSize
- 1);
147 buf
[maxSize
] = wxT('\0');
148 return *buf
? TRUE
: FALSE
;
152 // get full hostname (with domain name if possible)
153 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
155 #if !defined( __WXMICROWIN__) && wxUSE_DYNAMIC_LOADER
156 // TODO should use GetComputerNameEx() when available
158 // we don't want to always link with Winsock DLL as we might not use it at
159 // all, so load it dynamically here if needed (and don't complain if it is
160 // missing, we handle this)
163 wxDynamicLibrary
dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM
);
164 if ( dllWinsock
.IsLoaded() )
166 typedef int (PASCAL
*WSAStartup_t
)(WORD
, WSADATA
*);
167 typedef int (PASCAL
*gethostname_t
)(char *, int);
168 typedef hostent
* (PASCAL
*gethostbyname_t
)(const char *);
169 typedef hostent
* (PASCAL
*gethostbyaddr_t
)(const char *, int , int);
170 typedef int (PASCAL
*WSACleanup_t
)(void);
172 #define LOAD_WINSOCK_FUNC(func) \
174 pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
176 LOAD_WINSOCK_FUNC(WSAStartup
);
179 if ( pfnWSAStartup
&& pfnWSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
181 LOAD_WINSOCK_FUNC(gethostname
);
184 if ( pfngethostname
)
187 if ( pfngethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
189 // gethostname() won't usually include the DNS domain name,
190 // for this we need to work a bit more
191 if ( !strchr(bufA
, '.') )
193 LOAD_WINSOCK_FUNC(gethostbyname
);
195 struct hostent
*pHostEnt
= pfngethostbyname
196 ? pfngethostbyname(bufA
)
201 // Windows will use DNS internally now
202 LOAD_WINSOCK_FUNC(gethostbyaddr
);
204 pHostEnt
= pfngethostbyaddr
205 ? pfngethostbyaddr(pHostEnt
->h_addr
,
212 host
= wxString::FromAscii(pHostEnt
->h_name
);
218 LOAD_WINSOCK_FUNC(WSACleanup
);
225 wxStrncpy(buf
, host
, maxSize
);
231 #endif // !__WXMICROWIN__
233 return wxGetHostName(buf
, maxSize
);
236 // Get user ID e.g. jacs
237 bool wxGetUserId(wxChar
*buf
, int maxSize
)
239 #if defined(__WXWINCE__)
241 #elif defined(__WIN32__) && !defined(__win32s__) && !defined(__WXMICROWIN__)
242 DWORD nSize
= maxSize
;
243 if ( ::GetUserName(buf
, &nSize
) == 0 )
245 // actually, it does happen on Win9x if the user didn't log on
246 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
255 #else // Win16 or Win32s
257 const wxChar
*default_id
= wxT("anonymous");
259 // Can't assume we have NIS (PC-NFS) or some other ID daemon
261 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
262 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
264 // Use wxWindows configuration data (comming soon)
265 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
269 wxStrncpy(buf
, user
, maxSize
- 1);
272 return *buf
? TRUE
: FALSE
;
276 // Get user name e.g. Julian Smart
277 bool wxGetUserName(wxChar
*buf
, int maxSize
)
279 #if defined(__WXWINCE__)
281 #elif defined(USE_NET_API)
282 CHAR szUserName
[256];
283 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
286 // TODO how to get the domain name?
289 // the code is based on the MSDN example (also see KB article Q119670)
290 WCHAR wszUserName
[256]; // Unicode user name
291 WCHAR wszDomain
[256];
294 USER_INFO_2
*ui2
; // User structure
296 // Convert ANSI user name and domain to Unicode
297 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
298 wszUserName
, WXSIZEOF(wszUserName
) );
299 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
300 wszDomain
, WXSIZEOF(wszDomain
) );
302 // Get the computer name of a DC for the domain.
303 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
305 wxLogError(wxT("Can not find domain controller"));
310 // Look up the user on the DC
311 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
312 (LPWSTR
)&wszUserName
,
313 2, // level - we want USER_INFO_2
321 case NERR_InvalidComputer
:
322 wxLogError(wxT("Invalid domain controller name."));
326 case NERR_UserNotFound
:
327 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
332 wxLogSysError(wxT("Can't get information about user"));
337 // Convert the Unicode full name to ANSI
338 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
339 buf
, maxSize
, NULL
, NULL
);
344 wxLogError(wxT("Couldn't look up full user name."));
347 #else // !USE_NET_API
348 // Could use NIS, MS-Mail or other site specific programs
349 // Use wxWindows configuration data
350 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxEmptyString
, buf
, maxSize
- 1) != 0;
353 ok
= wxGetUserId(buf
, maxSize
);
358 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
365 const wxChar
* wxGetHomeDir(wxString
*pstr
)
367 wxString
& strDir
= *pstr
;
369 // first branch is for Cygwin
370 #if defined(__UNIX__)
371 const wxChar
*szHome
= wxGetenv("HOME");
372 if ( szHome
== NULL
) {
374 wxLogWarning(_("can't find user's HOME, using current directory."));
380 // add a trailing slash if needed
381 if ( strDir
.Last() != wxT('/') )
385 // Cygwin returns unix type path but that does not work well
386 static wxChar windowsPath
[MAX_PATH
];
387 cygwin_conv_to_full_win32_path(strDir
, windowsPath
);
388 strDir
= windowsPath
;
390 #elif defined(__WXWINCE__)
395 // If we have a valid HOME directory, as is used on many machines that
396 // have unix utilities on them, we should use that.
397 const wxChar
*szHome
= wxGetenv(wxT("HOME"));
399 if ( szHome
!= NULL
)
403 else // no HOME, try HOMEDRIVE/PATH
405 szHome
= wxGetenv(wxT("HOMEDRIVE"));
406 if ( szHome
!= NULL
)
408 szHome
= wxGetenv(wxT("HOMEPATH"));
410 if ( szHome
!= NULL
)
414 // the idea is that under NT these variables have default values
415 // of "%systemdrive%:" and "\\". As we don't want to create our
416 // config files in the root directory of the system drive, we will
417 // create it in our program's dir. However, if the user took care
418 // to set HOMEPATH to something other than "\\", we suppose that he
419 // knows what he is doing and use the supplied value.
420 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
425 if ( strDir
.empty() )
427 // If we have a valid USERPROFILE directory, as is the case in
428 // Windows NT, 2000 and XP, we should use that as our home directory.
429 szHome
= wxGetenv(wxT("USERPROFILE"));
431 if ( szHome
!= NULL
)
435 if ( !strDir
.empty() )
437 // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the
438 // value once again, it shouldn't hurt anyhow
439 strDir
= wxExpandEnvVars(strDir
);
441 else // fall back to the program directory
444 ::GetModuleFileName(::GetModuleHandle(NULL
),
445 wxStringBuffer(strPath
, MAX_PATH
), MAX_PATH
);
447 // extract the dir name
448 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
452 return strDir
.c_str();
455 wxChar
*wxGetUserHome(const wxString
& WXUNUSED(user
))
457 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
458 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
459 // static buffer and sometimes I don't even know what.
460 static wxString s_home
;
462 return (wxChar
*)wxGetHomeDir(&s_home
);
465 bool wxDirExists(const wxString
& dir
)
467 #ifdef __WXMICROWIN__
468 return wxPathExist(dir
);
469 #elif defined(__WIN32__)
470 DWORD attribs
= GetFileAttributes(dir
);
471 return ((attribs
!= (DWORD
)-1) && (attribs
& FILE_ATTRIBUTE_DIRECTORY
));
474 struct ffblk fileInfo
;
476 struct find_t fileInfo
;
478 // In Borland findfirst has a different argument
479 // ordering from _dos_findfirst. But _dos_findfirst
480 // _should_ be ok in both MS and Borland... why not?
482 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
483 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
485 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
486 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
491 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
499 // old w32api don't have ULARGE_INTEGER
500 #if defined(__WIN32__) && \
501 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
502 // GetDiskFreeSpaceEx() is not available under original Win95, check for
504 typedef BOOL (WINAPI
*GetDiskFreeSpaceEx_t
)(LPCTSTR
,
510 pGetDiskFreeSpaceEx
= (GetDiskFreeSpaceEx_t
)::GetProcAddress
512 ::GetModuleHandle(_T("kernel32.dll")),
514 "GetDiskFreeSpaceExW"
516 "GetDiskFreeSpaceExA"
520 if ( pGetDiskFreeSpaceEx
)
522 ULARGE_INTEGER bytesFree
, bytesTotal
;
524 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
525 if ( !pGetDiskFreeSpaceEx(path
,
530 wxLogLastError(_T("GetDiskFreeSpaceEx"));
535 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
536 // two 32 bit fields which may be or may be not named - try to make it
537 // compile in all cases
538 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
545 *pTotal
= wxLongLong(UL(bytesTotal
).HighPart
, UL(bytesTotal
).LowPart
);
550 *pFree
= wxLongLong(UL(bytesFree
).HighPart
, UL(bytesFree
).LowPart
);
556 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
557 // should be used instead - but if it's not available, fall back on
558 // GetDiskFreeSpace() nevertheless...
560 DWORD lSectorsPerCluster
,
562 lNumberOfFreeClusters
,
563 lTotalNumberOfClusters
;
565 // FIXME: this is wrong, we should extract the root drive from path
566 // instead, but this is the job for wxFileName...
567 if ( !::GetDiskFreeSpace(path
,
570 &lNumberOfFreeClusters
,
571 &lTotalNumberOfClusters
) )
573 wxLogLastError(_T("GetDiskFreeSpace"));
578 wxLongLong lBytesPerCluster
= lSectorsPerCluster
;
579 lBytesPerCluster
*= lBytesPerSector
;
583 *pTotal
= lBytesPerCluster
;
584 *pTotal
*= lTotalNumberOfClusters
;
589 *pFree
= lBytesPerCluster
;
590 *pFree
*= lNumberOfFreeClusters
;
599 // ----------------------------------------------------------------------------
601 // ----------------------------------------------------------------------------
603 bool wxGetEnv(const wxString
& var
, wxString
*value
)
607 #elif defined(__WIN16__)
608 const wxChar
* ret
= wxGetenv(var
);
619 // first get the size of the buffer
620 DWORD dwRet
= ::GetEnvironmentVariable(var
, NULL
, 0);
623 // this means that there is no such variable
629 (void)::GetEnvironmentVariable(var
, wxStringBuffer(*value
, dwRet
),
637 bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
639 // some compilers have putenv() or _putenv() or _wputenv() but it's better
640 // to always use Win32 function directly instead of dealing with them
641 #if defined(__WIN32__) && !defined(__WXWINCE__)
642 if ( !::SetEnvironmentVariable(var
, value
) )
644 wxLogLastError(_T("SetEnvironmentVariable"));
650 #else // no way to set env vars
655 // ----------------------------------------------------------------------------
656 // process management
657 // ----------------------------------------------------------------------------
659 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
660 struct wxFindByPidParams
662 wxFindByPidParams() { hwnd
= 0; pid
= 0; }
664 // the HWND used to return the result
667 // the PID we're looking from
670 DECLARE_NO_COPY_CLASS(wxFindByPidParams
)
673 // wxKill helper: EnumWindows() callback which is used to find the first (top
674 // level) window belonging to the given process
675 BOOL CALLBACK
wxEnumFindByPidProc(HWND hwnd
, LPARAM lParam
)
678 (void)::GetWindowThreadProcessId(hwnd
, &pid
);
680 wxFindByPidParams
*params
= (wxFindByPidParams
*)lParam
;
681 if ( pid
== params
->pid
)
683 // remember the window we found
686 // return FALSE to stop the enumeration
690 // continue enumeration
694 int wxKill(long pid
, wxSignal sig
, wxKillError
*krc
)
696 // get the process handle to operate on
697 HANDLE hProcess
= ::OpenProcess(SYNCHRONIZE
|
699 PROCESS_QUERY_INFORMATION
,
700 FALSE
, // not inheritable
702 if ( hProcess
== NULL
)
706 if ( ::GetLastError() == ERROR_ACCESS_DENIED
)
708 *krc
= wxKILL_ACCESS_DENIED
;
712 *krc
= wxKILL_NO_PROCESS
;
723 // kill the process forcefully returning -1 as error code
724 if ( !::TerminateProcess(hProcess
, (UINT
)-1) )
726 wxLogSysError(_("Failed to kill process %d"), pid
);
730 // this is not supposed to happen if we could open the
740 // do nothing, we just want to test for process existence
744 // any other signal means "terminate"
746 wxFindByPidParams params
;
747 params
.pid
= (DWORD
)pid
;
749 // EnumWindows() has nice semantics: it returns 0 if it found
750 // something or if an error occured and non zero if it
751 // enumerated all the window
752 if ( !::EnumWindows(wxEnumFindByPidProc
, (LPARAM
)¶ms
) )
754 // did we find any window?
757 // tell the app to close
759 // NB: this is the harshest way, the app won't have
760 // opportunity to save any files, for example, but
761 // this is probably what we want here. If not we
762 // can also use SendMesageTimeout(WM_CLOSE)
763 if ( !::PostMessage(params
.hwnd
, WM_QUIT
, 0, 0) )
765 wxLogLastError(_T("PostMessage(WM_QUIT)"));
768 else // it was an error then
770 wxLogLastError(_T("EnumWindows"));
775 else // no windows for this PID
792 // as we wait for a short time, we can use just WaitForSingleObject()
793 // and not MsgWaitForMultipleObjects()
794 switch ( ::WaitForSingleObject(hProcess
, 500 /* msec */) )
797 // process terminated
798 if ( !::GetExitCodeProcess(hProcess
, &rc
) )
800 wxLogLastError(_T("GetExitCodeProcess"));
805 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
809 wxLogLastError(_T("WaitForSingleObject"));
824 // just to suppress the warnings about uninitialized variable
828 ::CloseHandle(hProcess
);
830 // the return code is the same as from Unix kill(): 0 if killed
831 // successfully or -1 on error
833 // be careful to interpret rc correctly: for wxSIGNONE we return success if
834 // the process exists, for all the other sig values -- if it doesn't
836 ((sig
== wxSIGNONE
) == (rc
== STILL_ACTIVE
)) )
850 // Execute a program in an Interactive Shell
851 bool wxShell(const wxString
& command
)
856 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
858 shell
= (wxChar
*) wxT("\\COMMAND.COM");
868 // pass the command to execute to the command processor
869 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
872 return wxExecute(cmd
, wxEXEC_SYNC
) == 0;
876 // Shutdown or reboot the PC
877 bool wxShutdown(wxShutdownFlags wFlags
)
881 #elif defined(__WIN32__)
884 if ( wxGetOsVersion(NULL
, NULL
) == wxWINDOWS_NT
) // if is NT or 2K
886 // Get a token for this process.
888 bOK
= ::OpenProcessToken(GetCurrentProcess(),
889 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
,
893 TOKEN_PRIVILEGES tkp
;
895 // Get the LUID for the shutdown privilege.
896 ::LookupPrivilegeValue(NULL
, SE_SHUTDOWN_NAME
,
897 &tkp
.Privileges
[0].Luid
);
899 tkp
.PrivilegeCount
= 1; // one privilege to set
900 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
902 // Get the shutdown privilege for this process.
903 ::AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0,
904 (PTOKEN_PRIVILEGES
)NULL
, 0);
906 // Cannot test the return value of AdjustTokenPrivileges.
907 bOK
= ::GetLastError() == ERROR_SUCCESS
;
913 UINT flags
= EWX_SHUTDOWN
| EWX_FORCE
;
916 case wxSHUTDOWN_POWEROFF
:
917 flags
|= EWX_POWEROFF
;
920 case wxSHUTDOWN_REBOOT
:
925 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
929 bOK
= ::ExitWindowsEx(flags
, 0) != 0;
938 // ----------------------------------------------------------------------------
940 // ----------------------------------------------------------------------------
942 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
943 long wxGetFreeMemory()
945 #if defined(__WIN32__) && !defined(__BORLANDC__)
946 MEMORYSTATUS memStatus
;
947 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
948 GlobalMemoryStatus(&memStatus
);
949 return memStatus
.dwAvailPhys
;
951 return (long)GetFreeSpace(0);
955 unsigned long wxGetProcessId()
958 return ::GetCurrentProcessId();
967 ::MessageBeep((UINT
)-1); // default sound
970 wxString
wxGetOsDescription()
978 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
979 if ( ::GetVersionEx(&info
) )
981 switch ( info
.dwPlatformId
)
983 case VER_PLATFORM_WIN32s
:
984 str
= _("Win32s on Windows 3.1");
987 case VER_PLATFORM_WIN32_WINDOWS
:
988 str
.Printf(_("Windows 9%c"),
989 info
.dwMinorVersion
== 0 ? _T('5') : _T('8'));
990 if ( !wxIsEmpty(info
.szCSDVersion
) )
992 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
996 case VER_PLATFORM_WIN32_NT
:
997 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
1000 info
.dwBuildNumber
);
1001 if ( !wxIsEmpty(info
.szCSDVersion
) )
1003 str
<< _T(", ") << info
.szCSDVersion
;
1011 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
1016 return _("Windows 3.1");
1020 wxToolkitInfo
& wxAppTraits::GetToolkitInfo()
1022 // cache the version info, it's not going to change
1024 // NB: this is MT-safe, we may use these static vars from different threads
1025 // but as they always have the same value it doesn't matter
1026 static int s_ver
= -1,
1036 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1037 if ( ::GetVersionEx(&info
) )
1039 s_major
= info
.dwMajorVersion
;
1040 s_minor
= info
.dwMinorVersion
;
1042 switch ( info
.dwPlatformId
)
1044 case VER_PLATFORM_WIN32s
:
1048 case VER_PLATFORM_WIN32_WINDOWS
:
1052 case VER_PLATFORM_WIN32_NT
:
1053 s_ver
= wxWINDOWS_NT
;
1056 case VER_PLATFORM_WIN32_CE
:
1057 s_ver
= wxWINDOWS_CE
;
1064 static wxToolkitInfo info
;
1065 info
.versionMajor
= s_major
;
1066 info
.versionMinor
= s_minor
;
1068 info
.name
= _T("wxBase");
1072 // ----------------------------------------------------------------------------
1074 // ----------------------------------------------------------------------------
1076 void wxUsleep(unsigned long milliseconds
)
1078 ::Sleep(milliseconds
);
1081 void wxSleep(int nSecs
)
1083 wxUsleep(1000*nSecs
);
1086 // ----------------------------------------------------------------------------
1087 // font encoding <-> Win32 codepage conversion functions
1088 // ----------------------------------------------------------------------------
1090 extern WXDLLIMPEXP_BASE
long wxEncodingToCharset(wxFontEncoding encoding
)
1094 // although this function is supposed to return an exact match, do do
1095 // some mappings here for the most common case of "standard" encoding
1096 case wxFONTENCODING_SYSTEM
:
1097 return DEFAULT_CHARSET
;
1099 case wxFONTENCODING_ISO8859_1
:
1100 case wxFONTENCODING_ISO8859_15
:
1101 case wxFONTENCODING_CP1252
:
1102 return ANSI_CHARSET
;
1104 #if !defined(__WXMICROWIN__)
1105 // The following four fonts are multi-byte charsets
1106 case wxFONTENCODING_CP932
:
1107 return SHIFTJIS_CHARSET
;
1109 case wxFONTENCODING_CP936
:
1110 return GB2312_CHARSET
;
1112 case wxFONTENCODING_CP949
:
1113 return HANGUL_CHARSET
;
1115 case wxFONTENCODING_CP950
:
1116 return CHINESEBIG5_CHARSET
;
1118 // The rest are single byte encodings
1119 case wxFONTENCODING_CP1250
:
1120 return EASTEUROPE_CHARSET
;
1122 case wxFONTENCODING_CP1251
:
1123 return RUSSIAN_CHARSET
;
1125 case wxFONTENCODING_CP1253
:
1126 return GREEK_CHARSET
;
1128 case wxFONTENCODING_CP1254
:
1129 return TURKISH_CHARSET
;
1131 case wxFONTENCODING_CP1255
:
1132 return HEBREW_CHARSET
;
1134 case wxFONTENCODING_CP1256
:
1135 return ARABIC_CHARSET
;
1137 case wxFONTENCODING_CP1257
:
1138 return BALTIC_CHARSET
;
1140 case wxFONTENCODING_CP874
:
1141 return THAI_CHARSET
;
1142 #endif // !__WXMICROWIN__
1144 case wxFONTENCODING_CP437
:
1148 // no way to translate this encoding into a Windows charset
1153 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1154 // looks up the vlaues in the registry and the new one which is more
1155 // politically correct and has more chances to work on other Windows versions
1156 // as well but the old version is still needed for !wxUSE_FONTMAP case
1159 #include "wx/fontmap.h"
1161 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
)
1163 // translate encoding into the Windows CHARSET
1164 long charset
= wxEncodingToCharset(encoding
);
1165 if ( charset
== -1 )
1168 // translate CHARSET to code page
1169 CHARSETINFO csetInfo
;
1170 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)charset
,
1174 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
1179 return csetInfo
.ciACP
;
1182 extern long wxCharsetToCodepage(const wxChar
*name
)
1184 // first get the font encoding for this charset
1188 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
1189 if ( enc
== wxFONTENCODING_SYSTEM
)
1192 // the use the helper function
1193 return wxEncodingToCodepage(enc
);
1196 #else // !wxUSE_FONTMAP
1198 #include "wx/msw/registry.h"
1200 // this should work if Internet Exploiter is installed
1201 extern long wxCharsetToCodepage(const wxChar
*name
)
1208 wxString
path(wxT("MIME\\Database\\Charset\\"));
1211 // follow the alias loop
1214 wxRegKey
key(wxRegKey::HKCR
, path
+ cn
);
1219 // two cases: either there's an AliasForCharset string,
1220 // or there are Codepage and InternetEncoding dwords.
1221 // The InternetEncoding gives us the actual encoding,
1222 // the Codepage just says which Windows character set to
1223 // use when displaying the data.
1224 if (key
.HasValue(wxT("InternetEncoding")) &&
1225 key
.QueryValue(wxT("InternetEncoding"), &CP
))
1228 // no encoding, see if it's an alias
1229 if (!key
.HasValue(wxT("AliasForCharset")) ||
1230 !key
.QueryValue(wxT("AliasForCharset"), cn
))
1237 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1240 Creates a hidden window with supplied window proc registering the class for
1241 it if necesssary (i.e. the first time only). Caller is responsible for
1242 destroying the window and unregistering the class (note that this must be
1243 done because wxWindows may be used as a DLL and so may be loaded/unloaded
1244 multiple times into/from the same process so we cna't rely on automatic
1245 Windows class unregistration).
1247 pclassname is a pointer to a caller stored classname, which must initially be
1248 NULL. classname is the desired wndclass classname. If function succesfully
1249 registers the class, pclassname will be set to classname.
1251 extern "C" WXDLLIMPEXP_BASE HWND
1252 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
)
1254 wxCHECK_MSG( classname
&& pclassname
&& wndproc
, NULL
,
1255 _T("NULL parameter in wxCreateHiddenWindow") );
1257 // register the class fi we need to first
1258 if ( *pclassname
== NULL
)
1261 wxZeroMemory(wndclass
);
1263 wndclass
.lpfnWndProc
= wndproc
;
1264 wndclass
.hInstance
= wxGetInstance();
1265 wndclass
.lpszClassName
= classname
;
1267 if ( !::RegisterClass(&wndclass
) )
1269 wxLogLastError(wxT("RegisterClass() in wxCreateHiddenWindow"));
1274 *pclassname
= classname
;
1277 // next create the window
1278 HWND hwnd
= ::CreateWindow
1292 wxLogLastError(wxT("CreateWindow() in wxCreateHiddenWindow"));