1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     Various utilities 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21 // #pragma implementation "utils.h"   // Note: this is done in utilscmn.cpp now. 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  34     #include "wx/cursor.h" 
  39 // In some mingws there is a missing extern "C" int the winsock header, 
  40 // so we put it here just to be safe. Note that this must appear _before_ 
  41 // #include "wx/msw/private.h" which itself includes <windows.h>, as this  
  42 // one in turn includes <winsock.h> unless we define WIN32_LEAN_AND_MEAN. 
  44 #if defined(__WIN32__) && !defined(__TWIN32__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) 
  46     #include <winsock.h>    // we use socket functions in wxGetFullHostName() 
  50 #include "wx/msw/private.h"     // includes <windows.h> 
  56 #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) 
  64 #if defined(__GNUWIN32__) && !defined(__TWIN32__) 
  65     #include <sys/unistd.h> 
  69 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs 
  70                     // this (3.1 I believe) and how to test for it. 
  71                     // If this works for Borland 4.0 as well, then no worries. 
  75 // VZ: there is some code using NetXXX() functions to get the full user name: 
  76 //     I don't think it's a good idea because they don't work under Win95 and 
  77 //     seem to return the same as wxGetUserId() under NT. If you really want 
  78 //     to use them, just #define USE_NET_API 
  85 #if defined(__WIN32__) && !defined(__WXWINE__) 
  97     #if !(defined(_MSC_VER) && (_MSC_VER > 800)) 
 103 //// BEGIN for console support: VC++ only 
 106 #include "wx/msw/msvcrt.h" 
 110 #include "wx/ioswrap.h" 
 113 // N.B. BC++ doesn't have istream.h, ostream.h 
 115 #  include <fstream.h> 
 120 /* Need to undef new if including crtdbg.h */ 
 129 #  if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS 
 130 #  define new new(__TFILE__,__LINE__) 
 135 /// END for console support 
 137 // ---------------------------------------------------------------------------- 
 139 // ---------------------------------------------------------------------------- 
 141 // In the WIN.INI file 
 142 static const wxChar WX_SECTION
[] = wxT("wxWindows"); 
 143 static const wxChar eUSERNAME
[]  = wxT("UserName"); 
 145 // these are only used under Win16 
 147 static const wxChar eHOSTNAME
[]  = wxT("HostName"); 
 148 static const wxChar eUSERID
[]    = wxT("UserId"); 
 151 // ============================================================================ 
 153 // ============================================================================ 
 155 // ---------------------------------------------------------------------------- 
 156 // get host name and related 
 157 // ---------------------------------------------------------------------------- 
 159 // Get hostname only (without domain name) 
 160 bool wxGetHostName(wxChar 
*buf
, int maxSize
) 
 162 #if defined(__WIN32__) && !defined(__TWIN32__) 
 163     DWORD nSize 
= maxSize
; 
 164     if ( !::GetComputerName(buf
, &nSize
) ) 
 166         wxLogLastError(wxT("GetComputerName")); 
 174     const wxChar 
*default_host 
= wxT("noname"); 
 176     if ((sysname 
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) { 
 177         GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize 
- 1); 
 179         wxStrncpy(buf
, sysname
, maxSize 
- 1); 
 180     buf
[maxSize
] = wxT('\0'); 
 181     return *buf 
? TRUE 
: FALSE
; 
 185 // get full hostname (with domain name if possible) 
 186 bool wxGetFullHostName(wxChar 
*buf
, int maxSize
) 
 188 #if defined(__WIN32__) && !defined(__TWIN32__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) 
 189     // TODO should use GetComputerNameEx() when available 
 191     if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 ) 
 195         if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 ) 
 197             // gethostname() won't usually include the DNS domain name, for 
 198             // this we need to work a bit more 
 199             if ( !strchr(bufA
, '.') ) 
 201                 struct hostent 
*pHostEnt 
=  gethostbyname(bufA
); 
 205                     // Windows will use DNS internally now 
 206                     pHostEnt 
= gethostbyaddr(pHostEnt
->h_addr
, 4, PF_INET
); 
 211                     host 
= pHostEnt
->h_name
; 
 220             wxStrncpy(buf
, host
, maxSize
); 
 227     return wxGetHostName(buf
, maxSize
); 
 230 // Get user ID e.g. jacs 
 231 bool wxGetUserId(wxChar 
*buf
, int maxSize
) 
 233 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__) 
 234     DWORD nSize 
= maxSize
; 
 235     if ( ::GetUserName(buf
, &nSize
) == 0 ) 
 237         // actually, it does happen on Win9x if the user didn't log on 
 238         DWORD res 
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
); 
 247 #else   // Win16 or Win32s 
 249     const wxChar 
*default_id 
= wxT("anonymous"); 
 251     // Can't assume we have NIS (PC-NFS) or some other ID daemon 
 253     if (  (user 
= wxGetenv(wxT("USER"))) == NULL 
&& 
 254             (user 
= wxGetenv(wxT("LOGNAME"))) == NULL 
) 
 256         // Use wxWindows configuration data (comming soon) 
 257         GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize 
- 1); 
 261         wxStrncpy(buf
, user
, maxSize 
- 1); 
 264     return *buf 
? TRUE 
: FALSE
; 
 268 // Get user name e.g. Julian Smart 
 269 bool wxGetUserName(wxChar 
*buf
, int maxSize
) 
 271 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__) 
 272     extern HANDLE g_hPenWin
; // PenWindows Running? 
 275         // PenWindows Does have a user concept! 
 276         // Get the current owner of the recognizer 
 277         GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize 
- 1, "PENWIN.INI"); 
 278         strncpy(buf
, wxBuffer
, maxSize 
- 1); 
 284         CHAR szUserName
[256]; 
 285         if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) ) 
 288         // TODO how to get the domain name? 
 291         // the code is based on the MSDN example (also see KB article Q119670) 
 292         WCHAR wszUserName
[256];          // Unicode user name 
 293         WCHAR wszDomain
[256]; 
 296         USER_INFO_2 
*ui2
;         // User structure 
 298         // Convert ANSI user name and domain to Unicode 
 299         MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1, 
 300                 wszUserName
, WXSIZEOF(wszUserName
) ); 
 301         MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1, 
 302                 wszDomain
, WXSIZEOF(wszDomain
) ); 
 304         // Get the computer name of a DC for the domain. 
 305         if ( NetGetDCName( NULL
, wszDomain
, &ComputerName 
) != NERR_Success 
) 
 307             wxLogError(wxT("Can not find domain controller")); 
 312         // Look up the user on the DC 
 313         NET_API_STATUS status 
= NetUserGetInfo( (LPWSTR
)ComputerName
, 
 314                 (LPWSTR
)&wszUserName
, 
 315                 2, // level - we want USER_INFO_2 
 323             case NERR_InvalidComputer
: 
 324                 wxLogError(wxT("Invalid domain controller name.")); 
 328             case NERR_UserNotFound
: 
 329                 wxLogError(wxT("Invalid user name '%s'."), szUserName
); 
 334                 wxLogSysError(wxT("Can't get information about user")); 
 339         // Convert the Unicode full name to ANSI 
 340         WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1, 
 341                 buf
, maxSize
, NULL
, NULL 
); 
 346         wxLogError(wxT("Couldn't look up full user name.")); 
 349 #else  // !USE_NET_API 
 350         // Could use NIS, MS-Mail or other site specific programs 
 351         // Use wxWindows configuration data 
 352         bool ok 
= GetProfileString(WX_SECTION
, eUSERNAME
, wxT(""), buf
, maxSize 
- 1) != 0; 
 355             ok 
= wxGetUserId(buf
, maxSize
); 
 360             wxStrncpy(buf
, wxT("Unknown User"), maxSize
); 
 368 const wxChar
* wxGetHomeDir(wxString 
*pstr
) 
 370   wxString
& strDir 
= *pstr
; 
 372   #if defined(__UNIX__) && !defined(__TWIN32__) 
 373     const wxChar 
*szHome 
= wxGetenv("HOME"); 
 374     if ( szHome 
== NULL 
) { 
 376       wxLogWarning(_("can't find user's HOME, using current directory.")); 
 382     // add a trailing slash if needed 
 383     if ( strDir
.Last() != wxT('/') ) 
 387       const wxChar 
*szHome 
= wxGetenv(wxT("HOMEDRIVE")); 
 388       if ( szHome 
!= NULL 
) 
 390       szHome 
= wxGetenv(wxT("HOMEPATH")); 
 391       if ( szHome 
!= NULL 
) { 
 394         // the idea is that under NT these variables have default values 
 395         // of "%systemdrive%:" and "\\". As we don't want to create our 
 396         // config files in the root directory of the system drive, we will 
 397         // create it in our program's dir. However, if the user took care 
 398         // to set HOMEPATH to something other than "\\", we suppose that he 
 399         // knows what he is doing and use the supplied value. 
 400         if ( wxStrcmp(szHome
, wxT("\\")) != 0 ) 
 401           return strDir
.c_str(); 
 405       // Win16 has no idea about home, so use the working directory instead 
 408     // 260 was taken from windef.h 
 414     ::GetModuleFileName(::GetModuleHandle(NULL
), 
 415                         strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
); 
 416     strPath
.UngetWriteBuf(); 
 418     // extract the dir name 
 419     wxSplitPath(strPath
, &strDir
, NULL
, NULL
); 
 423   return strDir
.c_str(); 
 426 wxChar 
*wxGetUserHome(const wxString
& user
) 
 428     // VZ: the old code here never worked for user != "" anyhow! Moreover, it 
 429     //     returned sometimes a malloc()'d pointer, sometimes a pointer to a 
 430     //     static buffer and sometimes I don't even know what. 
 431     static wxString s_home
; 
 433     return (wxChar 
*)wxGetHomeDir(&s_home
); 
 436 bool wxDirExists(const wxString
& dir
) 
 438 #if defined(__WIN32__) 
 439     WIN32_FIND_DATA fileInfo
; 
 442         struct ffblk fileInfo
; 
 444         struct find_t fileInfo
; 
 448 #if defined(__WIN32__) 
 449     HANDLE h 
= ::FindFirstFile(dir
, &fileInfo
); 
 451     if ( h 
== INVALID_HANDLE_VALUE 
) 
 453         wxLogLastError(wxT("FindFirstFile")); 
 460     return (fileInfo
.dwFileAttributes 
& FILE_ATTRIBUTE_DIRECTORY
) != 0; 
 462     // In Borland findfirst has a different argument 
 463     // ordering from _dos_findfirst. But _dos_findfirst 
 464     // _should_ be ok in both MS and Borland... why not? 
 466         return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 && 
 467                (fileInfo
.ff_attrib 
& _A_SUBDIR
) != 0); 
 469         return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) && 
 470                ((fileInfo
.attrib 
& _A_SUBDIR
) != 0); 
 475 // ---------------------------------------------------------------------------- 
 476 // process management 
 477 // ---------------------------------------------------------------------------- 
 479 int wxKill(long pid
, int sig
) 
 481     // TODO use SendMessage(WM_QUIT) and TerminateProcess() if needed 
 486 // Execute a program in an Interactive Shell 
 487 bool wxShell(const wxString
& command
) 
 489     wxChar 
*shell 
= wxGetenv(wxT("COMSPEC")); 
 491         shell 
= wxT("\\COMMAND.COM"); 
 501         // pass the command to execute to the command processor 
 502         cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str()); 
 505     return wxExecute(cmd
, TRUE 
/* sync */) != 0; 
 508 // ---------------------------------------------------------------------------- 
 510 // ---------------------------------------------------------------------------- 
 512 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) 
 513 long wxGetFreeMemory() 
 515 #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__) 
 516     MEMORYSTATUS memStatus
; 
 517     memStatus
.dwLength 
= sizeof(MEMORYSTATUS
); 
 518     GlobalMemoryStatus(&memStatus
); 
 519     return memStatus
.dwAvailPhys
; 
 521     return (long)GetFreeSpace(0); 
 528     ::MessageBeep((UINT
)-1);        // default sound 
 531 wxString 
wxGetOsDescription() 
 539     info
.dwOSVersionInfoSize 
= sizeof(OSVERSIONINFO
); 
 540     if ( ::GetVersionEx(&info
) ) 
 542         switch ( info
.dwPlatformId 
) 
 544             case VER_PLATFORM_WIN32s
: 
 545                 str 
= _("Win32s on Windows 3.1"); 
 548             case VER_PLATFORM_WIN32_WINDOWS
: 
 549                 str
.Printf(_("Windows 9%c"), 
 550                            info
.dwMinorVersion 
== 0 ? _T('5') : _T('9')); 
 551                 if ( !wxIsEmpty(info
.szCSDVersion
) ) 
 553                     str 
<< _T(" (") << info
.szCSDVersion 
<< _T(')'); 
 557             case VER_PLATFORM_WIN32_NT
: 
 558                 str
.Printf(_T("Windows NT %lu.%lu (build %lu"), 
 562                 if ( !wxIsEmpty(info
.szCSDVersion
) ) 
 564                     str 
<< _T(", ") << info
.szCSDVersion
; 
 572         wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen 
 577     return _("Windows 3.1"); 
 581 int wxGetOsVersion(int *majorVsn
, int *minorVsn
) 
 583 #if defined(__WIN32__) && !defined(__SC__) 
 587     info
.dwOSVersionInfoSize 
= sizeof(OSVERSIONINFO
); 
 588     if ( ::GetVersionEx(&info
) ) 
 591             *majorVsn 
= info
.dwMajorVersion
; 
 593             *minorVsn 
= info
.dwMinorVersion
; 
 595         switch ( info
.dwPlatformId 
) 
 597             case VER_PLATFORM_WIN32s
: 
 600             case VER_PLATFORM_WIN32_WINDOWS
: 
 603             case VER_PLATFORM_WIN32_NT
: 
 608     return wxWINDOWS
; // error if we get here, return generic value 
 610     int retValue 
= wxWINDOWS
; 
 611     #ifdef __WINDOWS_386__ 
 614         #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS 
 615             extern HANDLE g_hPenWin
; 
 616             retValue 
= g_hPenWin 
? wxPENWINDOWS 
: wxWINDOWS
; 
 629 // ---------------------------------------------------------------------------- 
 631 // ---------------------------------------------------------------------------- 
 635 // Sleep for nSecs seconds. Attempt a Windows implementation using timers. 
 636 static bool gs_inTimer 
= FALSE
; 
 638 class wxSleepTimer
: public wxTimer
 
 641     virtual void Notify() 
 648 static wxTimer 
*wxTheSleepTimer 
= NULL
; 
 650 void wxUsleep(unsigned long milliseconds
) 
 653     ::Sleep(milliseconds
); 
 658     wxTheSleepTimer 
= new wxSleepTimer
; 
 660     wxTheSleepTimer
->Start(milliseconds
); 
 663         if (wxTheApp
->Pending()) 
 664             wxTheApp
->Dispatch(); 
 666     delete wxTheSleepTimer
; 
 667     wxTheSleepTimer 
= NULL
; 
 671 void wxSleep(int nSecs
) 
 676     wxTheSleepTimer 
= new wxSleepTimer
; 
 678     wxTheSleepTimer
->Start(nSecs
*1000); 
 681         if (wxTheApp
->Pending()) 
 682             wxTheApp
->Dispatch(); 
 684     delete wxTheSleepTimer
; 
 685     wxTheSleepTimer 
= NULL
; 
 688 // Consume all events until no more left 
 694 #elif defined(__WIN32__) // wxUSE_GUI 
 696 void wxUsleep(unsigned long milliseconds
) 
 698     ::Sleep(milliseconds
); 
 701 void wxSleep(int nSecs
) 
 703     wxUsleep(1000*nSecs
); 
 706 #endif // wxUSE_GUI/!wxUSE_GUI 
 708 // ---------------------------------------------------------------------------- 
 709 // deprecated (in favour of wxLog) log functions 
 710 // ---------------------------------------------------------------------------- 
 714 // Output a debug mess., in a system dependent fashion. 
 715 void wxDebugMsg(const wxChar 
*fmt 
...) 
 718   static wxChar buffer
[512]; 
 720   if (!wxTheApp
->GetWantDebugOutput()) 
 725   wvsprintf(buffer
,fmt
,ap
) ; 
 726   OutputDebugString((LPCTSTR
)buffer
) ; 
 731 // Non-fatal error: pop up message box and (possibly) continue 
 732 void wxError(const wxString
& msg
, const wxString
& title
) 
 734   wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
); 
 735   if (MessageBox(NULL
, (LPCTSTR
)wxBuffer
, (LPCTSTR
)WXSTRINGCAST title
, 
 736              MB_ICONSTOP 
| MB_YESNO
) == IDNO
) 
 740 // Fatal error: pop up message box and abort 
 741 void wxFatalError(const wxString
& msg
, const wxString
& title
) 
 743   wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
); 
 744   FatalAppExit(0, (LPCTSTR
)wxBuffer
); 
 747 // ---------------------------------------------------------------------------- 
 748 // functions to work with .INI files 
 749 // ---------------------------------------------------------------------------- 
 751 // Reading and writing resources (eg WIN.INI, .Xdefaults) 
 753 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
) 
 756 // Don't know what the correct cast should be, but it doesn't 
 757 // compile in BC++/16-bit without this cast. 
 758 #if !defined(__WIN32__) 
 759     return (WritePrivateProfileString((const char*) section
, (const char*) entry
, (const char*) value
, (const char*) file
) != 0); 
 761     return (WritePrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)value
, (LPCTSTR
)WXSTRINGCAST file
) != 0); 
 764     return (WriteProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)WXSTRINGCAST value
) != 0); 
 767 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
) 
 770     buf
.Printf(wxT("%.4f"), value
); 
 772     return wxWriteResource(section
, entry
, buf
, file
); 
 775 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
) 
 778     buf
.Printf(wxT("%ld"), value
); 
 780     return wxWriteResource(section
, entry
, buf
, file
); 
 783 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
) 
 786     buf
.Printf(wxT("%d"), value
); 
 788     return wxWriteResource(section
, entry
, buf
, file
); 
 791 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar 
**value
, const wxString
& file
) 
 793     static const wxChar defunkt
[] = wxT("$$default"); 
 796         int n 
= GetPrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
, 
 797                 (LPTSTR
)wxBuffer
, 1000, (LPCTSTR
)WXSTRINGCAST file
); 
 798         if (n 
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0) 
 803         int n 
= GetProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
, 
 804                 (LPTSTR
)wxBuffer
, 1000); 
 805         if (n 
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0) 
 808     if (*value
) delete[] (*value
); 
 809     *value 
= copystring(wxBuffer
); 
 813 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
) 
 816     bool succ 
= wxGetResource(section
, entry
, (wxChar 
**)&s
, file
); 
 819         *value 
= (float)wxStrtod(s
, NULL
); 
 826 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
) 
 829     bool succ 
= wxGetResource(section
, entry
, (wxChar 
**)&s
, file
); 
 832         *value 
= wxStrtol(s
, NULL
, 10); 
 839 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
) 
 842     bool succ 
= wxGetResource(section
, entry
, (wxChar 
**)&s
, file
); 
 845         *value 
= (int)wxStrtol(s
, NULL
, 10); 
 851 #endif // wxUSE_RESOURCES 
 853 // --------------------------------------------------------------------------- 
 854 // helper functions for showing a "busy" cursor 
 855 // --------------------------------------------------------------------------- 
 857 static HCURSOR gs_wxBusyCursor 
= 0;     // new, busy cursor 
 858 static HCURSOR gs_wxBusyCursorOld 
= 0;  // old cursor 
 859 static int gs_wxBusyCursorCount 
= 0; 
 861 extern HCURSOR 
wxGetCurrentBusyCursor() 
 863     return gs_wxBusyCursor
; 
 866 // Set the cursor to the busy cursor for all windows 
 867 void wxBeginBusyCursor(wxCursor 
*cursor
) 
 869     if ( gs_wxBusyCursorCount
++ == 0 ) 
 871         gs_wxBusyCursor 
= (HCURSOR
)cursor
->GetHCURSOR(); 
 872         gs_wxBusyCursorOld 
= ::SetCursor(gs_wxBusyCursor
); 
 874     //else: nothing to do, already set 
 877 // Restore cursor to normal 
 878 void wxEndBusyCursor() 
 880     wxCHECK_RET( gs_wxBusyCursorCount 
> 0, 
 881                  wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); 
 883     if ( --gs_wxBusyCursorCount 
== 0 ) 
 885         ::SetCursor(gs_wxBusyCursorOld
); 
 887         gs_wxBusyCursorOld 
= 0; 
 891 // TRUE if we're between the above two calls 
 894   return (gs_wxBusyCursorCount 
> 0); 
 897 // Check whether this window wants to process messages, e.g. Stop button 
 898 // in long calculations. 
 899 bool wxCheckForInterrupt(wxWindow 
*wnd
) 
 901     wxCHECK( wnd
, FALSE 
); 
 904     while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) ) 
 906         ::TranslateMessage(&msg
); 
 907         ::DispatchMessage(&msg
); 
 913 // MSW only: get user-defined resource from the .res file. 
 914 // Returns NULL or newly-allocated memory, so use delete[] to clean up. 
 916 wxChar 
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
) 
 918     HRSRC hResource 
= ::FindResource(wxGetInstance(), resourceName
, resourceType
); 
 919     if ( hResource 
== 0 ) 
 922     HGLOBAL hData 
= ::LoadResource(wxGetInstance(), hResource
); 
 926     wxChar 
*theText 
= (wxChar 
*)::LockResource(hData
); 
 930     // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). 
 931     // so we need to find the length of the resource. 
 932     int len 
= ::SizeofResource(wxGetInstance(), hResource
); 
 933     wxChar  
*s 
= new wxChar
[len
+1]; 
 934     wxStrncpy(s
,theText
,len
); 
 937     // wxChar *s = copystring(theText); 
 941     UnlockResource(hData
); 
 945     //  GlobalFree(hData); 
 950 // ---------------------------------------------------------------------------- 
 952 // ---------------------------------------------------------------------------- 
 954 void wxGetMousePosition( int* x
, int* y 
) 
 957     GetCursorPos( & pt 
); 
 962 // Return TRUE if we have a colour display 
 963 bool wxColourDisplay() 
 966     int noCols 
= GetDeviceCaps(dc
, NUMCOLORS
); 
 968     return (noCols 
== -1) || (noCols 
> 2); 
 971 // Returns depth of screen 
 975     return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
); 
 978 // Get size of display 
 979 void wxDisplaySize(int *width
, int *height
) 
 983     if ( width 
) *width 
= GetDeviceCaps(dc
, HORZRES
); 
 984     if ( height 
) *height 
= GetDeviceCaps(dc
, VERTRES
); 
 987 // --------------------------------------------------------------------------- 
 988 // window information functions 
 989 // --------------------------------------------------------------------------- 
 991 wxString WXDLLEXPORT 
wxGetWindowText(WXHWND hWnd
) 
 994     int len 
= GetWindowTextLength((HWND
)hWnd
) + 1; 
 995     GetWindowText((HWND
)hWnd
, str
.GetWriteBuf(len
), len
); 
1001 wxString WXDLLEXPORT 
wxGetWindowClass(WXHWND hWnd
) 
1005     int len 
= 256; // some starting value 
1009         // as we've #undefined GetClassName we must now manually choose the 
1010         // right function to call 
1023                 #endif // Twin32/!Twin32 
1024             #endif // Unicode/ANSI 
1026                                     ((HWND
)hWnd
, str
.GetWriteBuf(len
), len
); 
1028         str
.UngetWriteBuf(); 
1031             // the class name might have been truncated, retry with larger 
1044 WXWORD WXDLLEXPORT 
wxGetWindowId(WXHWND hWnd
) 
1047     return GetWindowWord((HWND
)hWnd
, GWW_ID
); 
1049     return GetWindowLong((HWND
)hWnd
, GWL_ID
); 
1056 //------------------------------------------------------------------------ 
1057 // wild character routines 
1058 //------------------------------------------------------------------------ 
1060 bool wxIsWild( const wxString
& pattern 
) 
1062   wxString tmp 
= pattern
; 
1063   char *pat 
= WXSTRINGCAST(tmp
); 
1066   case '?': case '*': case '[': case '{': 
1077 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special 
) 
1079   wxString tmp1 
= pat
; 
1080   char *pattern 
= WXSTRINGCAST(tmp1
); 
1081   wxString tmp2 
= text
; 
1082   char *str 
= WXSTRINGCAST(tmp2
); 
1085     bool done 
= FALSE
, ret_code
, ok
; 
1086     // Below is for vi fans 
1087     const char OB 
= '{', CB 
= '}'; 
1089     // dot_special means '.' only matches '.' 
1090     if (dot_special 
&& *str 
== '.' && *pattern 
!= *str
) 
1093     while ((*pattern 
!= '\0') && (!done
) 
1094     && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) { 
1098       if (*pattern 
!= '\0') 
1105       && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
)))) 
1108     while (*str 
!= '\0') 
1110     while (*pattern 
!= '\0') 
1117       if ((*pattern 
== '\0') || (*pattern 
== ']')) { 
1121       if (*pattern 
== '\\') { 
1123     if (*pattern 
== '\0') { 
1128       if (*(pattern 
+ 1) == '-') { 
1131     if (*pattern 
== ']') { 
1135     if (*pattern 
== '\\') { 
1137         if (*pattern 
== '\0') { 
1142     if ((*str 
< c
) || (*str 
> *pattern
)) { 
1146       } else if (*pattern 
!= *str
) { 
1151       while ((*pattern 
!= ']') && (*pattern 
!= '\0')) { 
1152     if ((*pattern 
== '\\') && (*(pattern 
+ 1) != '\0')) 
1156       if (*pattern 
!= '\0') { 
1166       while ((*pattern 
!= CB
) && (*pattern 
!= '\0')) { 
1169     while (ok 
&& (*cp 
!= '\0') && (*pattern 
!= '\0') 
1170     &&  (*pattern 
!= ',') && (*pattern 
!= CB
)) { 
1171         if (*pattern 
== '\\') 
1173         ok 
= (*pattern
++ == *cp
++); 
1175     if (*pattern 
== '\0') { 
1181         while ((*pattern 
!= CB
) && (*pattern 
!= '\0')) { 
1182       if (*++pattern 
== '\\') { 
1183           if (*++pattern 
== CB
) 
1188         while (*pattern
!=CB 
&& *pattern
!=',' && *pattern
!='\0') { 
1189       if (*++pattern 
== '\\') { 
1190                             if (*++pattern 
== CB 
|| *pattern 
== ',') 
1195     if (*pattern 
!= '\0') 
1200       if (*str 
== *pattern
) { 
1207     while (*pattern 
== '*') 
1209     return ((*str 
== '\0') && (*pattern 
== '\0')); 
1216 // maximum mumber of lines the output console should have 
1217 static const WORD MAX_CONSOLE_LINES 
= 500; 
1219 BOOL WINAPI 
MyConsoleHandler( DWORD dwCtrlType 
) {   //  control signal type 
1224 void wxRedirectIOToConsole() 
1228     CONSOLE_SCREEN_BUFFER_INFO coninfo
; 
1231     // allocate a console for this app 
1234     // set the screen buffer to be big enough to let us scroll text 
1235     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), 
1237     coninfo
.dwSize
.Y 
= MAX_CONSOLE_LINES
; 
1238     SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
), 
1241     // redirect unbuffered STDOUT to the console 
1242     lStdHandle 
= (long)GetStdHandle(STD_OUTPUT_HANDLE
); 
1243     hConHandle 
= _open_osfhandle(lStdHandle
, _O_TEXT
); 
1244     if(hConHandle 
<= 0) return; 
1245     fp 
= _fdopen( hConHandle
, "w" ); 
1247     setvbuf( stdout
, NULL
, _IONBF
, 0 ); 
1249     // redirect unbuffered STDIN to the console 
1250     lStdHandle 
= (long)GetStdHandle(STD_INPUT_HANDLE
); 
1251     hConHandle 
= _open_osfhandle(lStdHandle
, _O_TEXT
); 
1252     if(hConHandle 
<= 0) return; 
1253     fp 
= _fdopen( hConHandle
, "r" ); 
1255     setvbuf( stdin
, NULL
, _IONBF
, 0 ); 
1257     // redirect unbuffered STDERR to the console 
1258     lStdHandle 
= (long)GetStdHandle(STD_ERROR_HANDLE
); 
1259     hConHandle 
= _open_osfhandle(lStdHandle
, _O_TEXT
); 
1260     if(hConHandle 
<= 0) return; 
1261     fp 
= _fdopen( hConHandle
, "w" ); 
1263     setvbuf( stderr
, NULL
, _IONBF
, 0 ); 
1265     // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog 
1266     // point to console as well 
1267     ios::sync_with_stdio(); 
1269     SetConsoleCtrlHandler(MyConsoleHandler
, TRUE
); 
1273 void wxRedirectIOToConsole()