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 #include "wx/msw/private.h" // includes <windows.h>
45 #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__)
53 #if defined(__GNUWIN32__) && !defined(__TWIN32__)
54 #include <sys/unistd.h>
58 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
59 // this (3.1 I believe) and how to test for it.
60 // If this works for Borland 4.0 as well, then no worries.
64 #if defined(__WIN32__) && !defined(__TWIN32__)
65 #include <winsock.h> // we use socket functions in wxGetFullHostName()
68 // VZ: there is some code using NetXXX() functions to get the full user name:
69 // I don't think it's a good idea because they don't work under Win95 and
70 // seem to return the same as wxGetUserId() under NT. If you really want
71 // to use them, just #define USE_NET_API
78 #if defined(__WIN32__) && !defined(__WXWINE__)
90 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
96 //// BEGIN for console support: VC++ only
99 #include "wx/msw/msvcrt.h"
103 #include "wx/ioswrap.h"
106 // N.B. BC++ doesn't have istream.h, ostream.h
108 # include <fstream.h>
113 /* Need to undef new if including crtdbg.h */
122 # if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
123 # define new new(__FILE__,__LINE__)
128 /// END for console support
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // In the WIN.INI file
135 static const wxChar WX_SECTION
[] = wxT("wxWindows");
136 static const wxChar eUSERNAME
[] = wxT("UserName");
138 // these are only used under Win16
140 static const wxChar eHOSTNAME
[] = wxT("HostName");
141 static const wxChar eUSERID
[] = wxT("UserId");
144 // ============================================================================
146 // ============================================================================
148 // ----------------------------------------------------------------------------
149 // get host name and related
150 // ----------------------------------------------------------------------------
152 // Get hostname only (without domain name)
153 bool wxGetHostName(wxChar
*buf
, int maxSize
)
155 #if defined(__WIN32__) && !defined(__TWIN32__)
156 DWORD nSize
= maxSize
;
157 if ( !::GetComputerName(buf
, &nSize
) )
159 wxLogLastError("GetComputerName");
167 const wxChar
*default_host
= wxT("noname");
169 if ((sysname
= wxGetenv(wxT("SYSTEM_NAME"))) == NULL
) {
170 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
172 wxStrncpy(buf
, sysname
, maxSize
- 1);
173 buf
[maxSize
] = wxT('\0');
174 return *buf
? TRUE
: FALSE
;
178 // get full hostname (with domain name if possible)
179 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
181 #if defined(__WIN32__) && !defined(__TWIN32__)
182 // TODO should use GetComputerNameEx() when available
184 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
188 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
190 // gethostname() won't usually include the DNS domain name, for
191 // this we need to work a bit more
192 if ( !strchr(bufA
, '.') )
194 struct hostent
*pHostEnt
= gethostbyname(bufA
);
198 // Windows will use DNS internally now
199 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, PF_INET
);
204 host
= pHostEnt
->h_name
;
213 wxStrncpy(buf
, host
, maxSize
);
220 return wxGetHostName(buf
, maxSize
);
223 // Get user ID e.g. jacs
224 bool wxGetUserId(wxChar
*buf
, int maxSize
)
226 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__)
227 DWORD nSize
= maxSize
;
228 if ( ::GetUserName(buf
, &nSize
) == 0 )
230 // actually, it does happen on Win9x if the user didn't log on
231 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
240 #else // Win16 or Win32s
242 const wxChar
*default_id
= wxT("anonymous");
244 // Can't assume we have NIS (PC-NFS) or some other ID daemon
246 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
247 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
249 // Use wxWindows configuration data (comming soon)
250 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
254 wxStrncpy(buf
, user
, maxSize
- 1);
257 return *buf
? TRUE
: FALSE
;
261 // Get user name e.g. Julian Smart
262 bool wxGetUserName(wxChar
*buf
, int maxSize
)
264 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
265 extern HANDLE g_hPenWin
; // PenWindows Running?
268 // PenWindows Does have a user concept!
269 // Get the current owner of the recognizer
270 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
271 strncpy(buf
, wxBuffer
, maxSize
- 1);
277 CHAR szUserName
[256];
278 if ( !wxGetUserId(szUserName
, WXSIZEOF(szUserName
)) )
281 // TODO how to get the domain name?
284 // the code is based on the MSDN example (also see KB article Q119670)
285 WCHAR wszUserName
[256]; // Unicode user name
286 WCHAR wszDomain
[256];
289 USER_INFO_2
*ui2
; // User structure
291 // Convert ANSI user name and domain to Unicode
292 MultiByteToWideChar( CP_ACP
, 0, szUserName
, strlen(szUserName
)+1,
293 wszUserName
, WXSIZEOF(wszUserName
) );
294 MultiByteToWideChar( CP_ACP
, 0, szDomain
, strlen(szDomain
)+1,
295 wszDomain
, WXSIZEOF(wszDomain
) );
297 // Get the computer name of a DC for the domain.
298 if ( NetGetDCName( NULL
, wszDomain
, &ComputerName
) != NERR_Success
)
300 wxLogError(wxT("Can not find domain controller"));
305 // Look up the user on the DC
306 NET_API_STATUS status
= NetUserGetInfo( (LPWSTR
)ComputerName
,
307 (LPWSTR
)&wszUserName
,
308 2, // level - we want USER_INFO_2
316 case NERR_InvalidComputer
:
317 wxLogError(wxT("Invalid domain controller name."));
321 case NERR_UserNotFound
:
322 wxLogError(wxT("Invalid user name '%s'."), szUserName
);
327 wxLogSysError(wxT("Can't get information about user"));
332 // Convert the Unicode full name to ANSI
333 WideCharToMultiByte( CP_ACP
, 0, ui2
->usri2_full_name
, -1,
334 buf
, maxSize
, NULL
, NULL
);
339 wxLogError(wxT("Couldn't look up full user name."));
342 #else // !USE_NET_API
343 // Could use NIS, MS-Mail or other site specific programs
344 // Use wxWindows configuration data
345 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, wxT(""), buf
, maxSize
- 1) != 0;
348 ok
= wxGetUserId(buf
, maxSize
);
353 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
361 const wxChar
* wxGetHomeDir(wxString
*pstr
)
363 wxString
& strDir
= *pstr
;
365 #if defined(__UNIX__) && !defined(__TWIN32__)
366 const wxChar
*szHome
= wxGetenv("HOME");
367 if ( szHome
== NULL
) {
369 wxLogWarning(_("can't find user's HOME, using current directory."));
375 // add a trailing slash if needed
376 if ( strDir
.Last() != wxT('/') )
380 const wxChar
*szHome
= wxGetenv(wxT("HOMEDRIVE"));
381 if ( szHome
!= NULL
)
383 szHome
= wxGetenv(wxT("HOMEPATH"));
384 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 )
394 return strDir
.c_str();
398 // Win16 has no idea about home, so use the working directory instead
401 // 260 was taken from windef.h
407 ::GetModuleFileName(::GetModuleHandle(NULL
),
408 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
409 strPath
.UngetWriteBuf();
411 // extract the dir name
412 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
416 return strDir
.c_str();
419 wxChar
*wxGetUserHome(const wxString
& user
)
421 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
422 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
423 // static buffer and sometimes I don't even know what.
424 static wxString s_home
;
426 return (wxChar
*)wxGetHomeDir(&s_home
);
429 bool wxDirExists(const wxString
& dir
)
431 #if defined(__WIN32__)
432 WIN32_FIND_DATA fileInfo
;
435 struct ffblk fileInfo
;
437 struct find_t fileInfo
;
441 #if defined(__WIN32__)
442 HANDLE h
= ::FindFirstFile(dir
, &fileInfo
);
444 if ( h
== INVALID_HANDLE_VALUE
)
446 wxLogLastError("FindFirstFile");
453 return (fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
455 // In Borland findfirst has a different argument
456 // ordering from _dos_findfirst. But _dos_findfirst
457 // _should_ be ok in both MS and Borland... why not?
459 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
460 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
462 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
463 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
468 // ----------------------------------------------------------------------------
469 // process management
470 // ----------------------------------------------------------------------------
472 int wxKill(long pid
, int sig
)
474 // TODO use SendMessage(WM_QUIT) and TerminateProcess() if needed
479 // Execute a program in an Interactive Shell
480 bool wxShell(const wxString
& command
)
482 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
484 shell
= wxT("\\COMMAND.COM");
494 // pass the command to execute to the command processor
495 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
498 return wxExecute(cmd
, TRUE
/* sync */) != 0;
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
506 long wxGetFreeMemory()
508 #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__)
509 MEMORYSTATUS memStatus
;
510 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
511 GlobalMemoryStatus(&memStatus
);
512 return memStatus
.dwAvailPhys
;
514 return (long)GetFreeSpace(0);
521 ::MessageBeep((UINT
)-1); // default sound
524 wxString
wxGetOsDescription()
532 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
533 if ( ::GetVersionEx(&info
) )
535 switch ( info
.dwPlatformId
)
537 case VER_PLATFORM_WIN32s
:
538 str
= _("Win32s on Windows 3.1");
541 case VER_PLATFORM_WIN32_WINDOWS
:
542 str
.Printf(_("Windows 9%c"),
543 info
.dwMinorVersion
== 0 ? _T('5') : _T('9'));
544 if ( !wxIsEmpty(info
.szCSDVersion
) )
546 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
550 case VER_PLATFORM_WIN32_NT
:
551 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
555 if ( !wxIsEmpty(info
.szCSDVersion
) )
557 str
<< _T(", ") << info
.szCSDVersion
;
565 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
570 return _("Windows 3.1");
574 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
576 #if defined(__WIN32__) && !defined(__SC__)
580 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
581 if ( ::GetVersionEx(&info
) )
584 *majorVsn
= info
.dwMajorVersion
;
586 *minorVsn
= info
.dwMinorVersion
;
588 switch ( info
.dwPlatformId
)
590 case VER_PLATFORM_WIN32s
:
593 case VER_PLATFORM_WIN32_WINDOWS
:
596 case VER_PLATFORM_WIN32_NT
:
601 return wxWINDOWS
; // error if we get here, return generic value
603 int retValue
= wxWINDOWS
;
604 #ifdef __WINDOWS_386__
607 #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
608 extern HANDLE g_hPenWin
;
609 retValue
= g_hPenWin
? wxPENWINDOWS
: wxWINDOWS
;
622 // ----------------------------------------------------------------------------
624 // ----------------------------------------------------------------------------
628 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
629 static bool gs_inTimer
= FALSE
;
631 class wxSleepTimer
: public wxTimer
634 virtual void Notify()
641 static wxTimer
*wxTheSleepTimer
= NULL
;
643 void wxUsleep(unsigned long milliseconds
)
646 ::Sleep(milliseconds
);
651 wxTheSleepTimer
= new wxSleepTimer
;
653 wxTheSleepTimer
->Start(milliseconds
);
656 if (wxTheApp
->Pending())
657 wxTheApp
->Dispatch();
659 delete wxTheSleepTimer
;
660 wxTheSleepTimer
= NULL
;
664 void wxSleep(int nSecs
)
669 wxTheSleepTimer
= new wxSleepTimer
;
671 wxTheSleepTimer
->Start(nSecs
*1000);
674 if (wxTheApp
->Pending())
675 wxTheApp
->Dispatch();
677 delete wxTheSleepTimer
;
678 wxTheSleepTimer
= NULL
;
681 // Consume all events until no more left
687 #elif defined(__WIN32__) // wxUSE_GUI
689 void wxUsleep(unsigned long milliseconds
)
691 ::Sleep(milliseconds
);
694 void wxSleep(int nSecs
)
696 wxUsleep(1000*nSecs
);
699 #endif // wxUSE_GUI/!wxUSE_GUI
701 // ----------------------------------------------------------------------------
702 // deprecated (in favour of wxLog) log functions
703 // ----------------------------------------------------------------------------
707 // Output a debug mess., in a system dependent fashion.
708 void wxDebugMsg(const wxChar
*fmt
...)
711 static wxChar buffer
[512];
713 if (!wxTheApp
->GetWantDebugOutput())
718 wvsprintf(buffer
,fmt
,ap
) ;
719 OutputDebugString((LPCTSTR
)buffer
) ;
724 // Non-fatal error: pop up message box and (possibly) continue
725 void wxError(const wxString
& msg
, const wxString
& title
)
727 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
728 if (MessageBox(NULL
, (LPCTSTR
)wxBuffer
, (LPCTSTR
)WXSTRINGCAST title
,
729 MB_ICONSTOP
| MB_YESNO
) == IDNO
)
733 // Fatal error: pop up message box and abort
734 void wxFatalError(const wxString
& msg
, const wxString
& title
)
736 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
737 FatalAppExit(0, (LPCTSTR
)wxBuffer
);
740 // ----------------------------------------------------------------------------
741 // functions to work with .INI files
742 // ----------------------------------------------------------------------------
744 // Reading and writing resources (eg WIN.INI, .Xdefaults)
746 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
749 // Don't know what the correct cast should be, but it doesn't
750 // compile in BC++/16-bit without this cast.
751 #if !defined(__WIN32__)
752 return (WritePrivateProfileString((const char*) section
, (const char*) entry
, (const char*) value
, (const char*) file
) != 0);
754 return (WritePrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)value
, (LPCTSTR
)WXSTRINGCAST file
) != 0);
757 return (WriteProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)WXSTRINGCAST value
) != 0);
760 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
763 buf
.Printf(wxT("%.4f"), value
);
765 return wxWriteResource(section
, entry
, buf
, file
);
768 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
771 buf
.Printf(wxT("%ld"), value
);
773 return wxWriteResource(section
, entry
, buf
, file
);
776 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
779 buf
.Printf(wxT("%d"), value
);
781 return wxWriteResource(section
, entry
, buf
, file
);
784 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
786 static const wxChar defunkt
[] = wxT("$$default");
789 int n
= GetPrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
790 (LPTSTR
)wxBuffer
, 1000, (LPCTSTR
)WXSTRINGCAST file
);
791 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
796 int n
= GetProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
797 (LPTSTR
)wxBuffer
, 1000);
798 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
801 if (*value
) delete[] (*value
);
802 *value
= copystring(wxBuffer
);
806 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
809 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
812 *value
= (float)wxStrtod(s
, NULL
);
819 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
822 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
825 *value
= wxStrtol(s
, NULL
, 10);
832 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
835 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
838 *value
= (int)wxStrtol(s
, NULL
, 10);
844 #endif // wxUSE_RESOURCES
846 // ---------------------------------------------------------------------------
847 // helper functions for showing a "busy" cursor
848 // ---------------------------------------------------------------------------
850 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
851 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
852 static int gs_wxBusyCursorCount
= 0;
854 extern HCURSOR
wxGetCurrentBusyCursor()
856 return gs_wxBusyCursor
;
859 // Set the cursor to the busy cursor for all windows
860 void wxBeginBusyCursor(wxCursor
*cursor
)
862 if ( gs_wxBusyCursorCount
++ == 0 )
864 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
865 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
867 //else: nothing to do, already set
870 // Restore cursor to normal
871 void wxEndBusyCursor()
873 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
874 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
876 if ( --gs_wxBusyCursorCount
== 0 )
878 ::SetCursor(gs_wxBusyCursorOld
);
880 gs_wxBusyCursorOld
= 0;
884 // TRUE if we're between the above two calls
887 return (gs_wxBusyCursorCount
> 0);
890 // Check whether this window wants to process messages, e.g. Stop button
891 // in long calculations.
892 bool wxCheckForInterrupt(wxWindow
*wnd
)
894 wxCHECK( wnd
, FALSE
);
897 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
899 ::TranslateMessage(&msg
);
900 ::DispatchMessage(&msg
);
908 // MSW only: get user-defined resource from the .res file.
909 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
911 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
913 HRSRC hResource
= ::FindResource(wxGetInstance(), resourceName
, resourceType
);
914 if ( hResource
== 0 )
917 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
921 wxChar
*theText
= (wxChar
*)::LockResource(hData
);
925 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
926 // so we need to find the length of the resource.
927 int len
= ::SizeofResource(wxGetInstance(), hResource
);
928 wxChar
*s
= new wxChar
[len
+1];
929 wxStrncpy(s
,theText
,len
);
932 // wxChar *s = copystring(theText);
936 UnlockResource(hData
);
940 // GlobalFree(hData);
945 // ----------------------------------------------------------------------------
947 // ----------------------------------------------------------------------------
949 void wxGetMousePosition( int* x
, int* y
)
952 GetCursorPos( & pt
);
957 // Return TRUE if we have a colour display
958 bool wxColourDisplay()
961 int noCols
= GetDeviceCaps(dc
, NUMCOLORS
);
963 return (noCols
== -1) || (noCols
> 2);
966 // Returns depth of screen
970 return GetDeviceCaps(dc
, PLANES
) * GetDeviceCaps(dc
, BITSPIXEL
);
973 // Get size of display
974 void wxDisplaySize(int *width
, int *height
)
978 if ( width
) *width
= GetDeviceCaps(dc
, HORZRES
);
979 if ( height
) *height
= GetDeviceCaps(dc
, VERTRES
);
982 // ---------------------------------------------------------------------------
983 // window information functions
984 // ---------------------------------------------------------------------------
986 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
989 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
990 GetWindowText((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
996 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
1000 int len
= 256; // some starting value
1004 // as we've #undefined GetClassName we must now manually choose the
1005 // right function to call
1018 #endif // Twin32/!Twin32
1019 #endif // Unicode/ANSI
1021 ((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
1023 str
.UngetWriteBuf();
1026 // the class name might have been truncated, retry with larger
1039 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
1042 return GetWindowWord((HWND
)hWnd
, GWW_ID
);
1044 return GetWindowLong((HWND
)hWnd
, GWL_ID
);
1049 //------------------------------------------------------------------------
1050 // wild character routines
1051 //------------------------------------------------------------------------
1053 bool wxIsWild( const wxString
& pattern
)
1055 wxString tmp
= pattern
;
1056 char *pat
= WXSTRINGCAST(tmp
);
1059 case '?': case '*': case '[': case '{':
1070 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1072 wxString tmp1
= pat
;
1073 char *pattern
= WXSTRINGCAST(tmp1
);
1074 wxString tmp2
= text
;
1075 char *str
= WXSTRINGCAST(tmp2
);
1078 bool done
= FALSE
, ret_code
, ok
;
1079 // Below is for vi fans
1080 const char OB
= '{', CB
= '}';
1082 // dot_special means '.' only matches '.'
1083 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1086 while ((*pattern
!= '\0') && (!done
)
1087 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1091 if (*pattern
!= '\0')
1098 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1101 while (*str
!= '\0')
1103 while (*pattern
!= '\0')
1110 if ((*pattern
== '\0') || (*pattern
== ']')) {
1114 if (*pattern
== '\\') {
1116 if (*pattern
== '\0') {
1121 if (*(pattern
+ 1) == '-') {
1124 if (*pattern
== ']') {
1128 if (*pattern
== '\\') {
1130 if (*pattern
== '\0') {
1135 if ((*str
< c
) || (*str
> *pattern
)) {
1139 } else if (*pattern
!= *str
) {
1144 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1145 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1149 if (*pattern
!= '\0') {
1159 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1162 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1163 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1164 if (*pattern
== '\\')
1166 ok
= (*pattern
++ == *cp
++);
1168 if (*pattern
== '\0') {
1174 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1175 if (*++pattern
== '\\') {
1176 if (*++pattern
== CB
)
1181 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1182 if (*++pattern
== '\\') {
1183 if (*++pattern
== CB
|| *pattern
== ',')
1188 if (*pattern
!= '\0')
1193 if (*str
== *pattern
) {
1200 while (*pattern
== '*')
1202 return ((*str
== '\0') && (*pattern
== '\0'));
1209 // maximum mumber of lines the output console should have
1210 static const WORD MAX_CONSOLE_LINES
= 500;
1212 BOOL WINAPI
MyConsoleHandler( DWORD dwCtrlType
) { // control signal type
1217 void wxRedirectIOToConsole()
1221 CONSOLE_SCREEN_BUFFER_INFO coninfo
;
1224 // allocate a console for this app
1227 // set the screen buffer to be big enough to let us scroll text
1228 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
),
1230 coninfo
.dwSize
.Y
= MAX_CONSOLE_LINES
;
1231 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
),
1234 // redirect unbuffered STDOUT to the console
1235 lStdHandle
= (long)GetStdHandle(STD_OUTPUT_HANDLE
);
1236 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1237 if(hConHandle
<= 0) return;
1238 fp
= _fdopen( hConHandle
, "w" );
1240 setvbuf( stdout
, NULL
, _IONBF
, 0 );
1242 // redirect unbuffered STDIN to the console
1243 lStdHandle
= (long)GetStdHandle(STD_INPUT_HANDLE
);
1244 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1245 if(hConHandle
<= 0) return;
1246 fp
= _fdopen( hConHandle
, "r" );
1248 setvbuf( stdin
, NULL
, _IONBF
, 0 );
1250 // redirect unbuffered STDERR to the console
1251 lStdHandle
= (long)GetStdHandle(STD_ERROR_HANDLE
);
1252 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1253 if(hConHandle
<= 0) return;
1254 fp
= _fdopen( hConHandle
, "w" );
1256 setvbuf( stderr
, NULL
, _IONBF
, 0 );
1258 // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
1259 // point to console as well
1260 ios::sync_with_stdio();
1262 SetConsoleCtrlHandler(MyConsoleHandler
, TRUE
);
1266 void wxRedirectIOToConsole()