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 // This breaks _at least_ mingw!!
184 #if defined(__WIN32__) && !defined(__TWIN32__)
185 // TODO should use GetComputerNameEx() when available
187 if ( WSAStartup(MAKEWORD(1, 1), &wsa
) == 0 )
191 if ( gethostname(bufA
, WXSIZEOF(bufA
)) == 0 )
193 // gethostname() won't usually include the DNS domain name, for
194 // this we need to work a bit more
195 if ( !strchr(bufA
, '.') )
197 struct hostent
*pHostEnt
= gethostbyname(bufA
);
201 // Windows will use DNS internally now
202 pHostEnt
= gethostbyaddr(pHostEnt
->h_addr
, 4, PF_INET
);
207 host
= pHostEnt
->h_name
;
216 wxStrncpy(buf
, host
, maxSize
);
225 return wxGetHostName(buf
, maxSize
);
228 // Get user ID e.g. jacs
229 bool wxGetUserId(wxChar
*buf
, int maxSize
)
231 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__)
232 DWORD nSize
= maxSize
;
233 if ( ::GetUserName(buf
, &nSize
) == 0 )
235 // actually, it does happen on Win9x if the user didn't log on
236 DWORD res
= ::GetEnvironmentVariable(wxT("username"), buf
, maxSize
);
245 #else // Win16 or Win32s
247 const wxChar
*default_id
= wxT("anonymous");
249 // Can't assume we have NIS (PC-NFS) or some other ID daemon
251 if ( (user
= wxGetenv(wxT("USER"))) == NULL
&&
252 (user
= wxGetenv(wxT("LOGNAME"))) == NULL
)
254 // Use wxWindows configuration data (comming soon)
255 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
259 wxStrncpy(buf
, user
, maxSize
- 1);
262 return *buf
? TRUE
: FALSE
;
266 // Get user name e.g. Julian Smart
267 bool wxGetUserName(wxChar
*buf
, int maxSize
)
269 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
270 extern HANDLE g_hPenWin
; // PenWindows Running?
273 // PenWindows Does have a user concept!
274 // Get the current owner of the recognizer
275 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
276 strncpy(buf
, wxBuffer
, maxSize
- 1);
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
, wxT(""), buf
, maxSize
- 1) != 0;
353 ok
= wxGetUserId(buf
, maxSize
);
358 wxStrncpy(buf
, wxT("Unknown User"), maxSize
);
366 const wxChar
* wxGetHomeDir(wxString
*pstr
)
368 wxString
& strDir
= *pstr
;
370 #if defined(__UNIX__) && !defined(__TWIN32__)
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 const wxChar
*szHome
= wxGetenv(wxT("HOMEDRIVE"));
386 if ( szHome
!= NULL
)
388 szHome
= wxGetenv(wxT("HOMEPATH"));
389 if ( szHome
!= NULL
) {
392 // the idea is that under NT these variables have default values
393 // of "%systemdrive%:" and "\\". As we don't want to create our
394 // config files in the root directory of the system drive, we will
395 // create it in our program's dir. However, if the user took care
396 // to set HOMEPATH to something other than "\\", we suppose that he
397 // knows what he is doing and use the supplied value.
398 if ( wxStrcmp(szHome
, wxT("\\")) != 0 )
399 return strDir
.c_str();
403 // Win16 has no idea about home, so use the working directory instead
406 // 260 was taken from windef.h
412 ::GetModuleFileName(::GetModuleHandle(NULL
),
413 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
414 strPath
.UngetWriteBuf();
416 // extract the dir name
417 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
421 return strDir
.c_str();
424 wxChar
*wxGetUserHome(const wxString
& user
)
426 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
427 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
428 // static buffer and sometimes I don't even know what.
429 static wxString s_home
;
431 return (wxChar
*)wxGetHomeDir(&s_home
);
434 bool wxDirExists(const wxString
& dir
)
436 #if defined(__WIN32__)
437 WIN32_FIND_DATA fileInfo
;
440 struct ffblk fileInfo
;
442 struct find_t fileInfo
;
446 #if defined(__WIN32__)
447 HANDLE h
= ::FindFirstFile(dir
, &fileInfo
);
449 if ( h
== INVALID_HANDLE_VALUE
)
451 wxLogLastError("FindFirstFile");
458 return (fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
460 // In Borland findfirst has a different argument
461 // ordering from _dos_findfirst. But _dos_findfirst
462 // _should_ be ok in both MS and Borland... why not?
464 return (findfirst(dir
, &fileInfo
, _A_SUBDIR
) == 0 &&
465 (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0);
467 return (_dos_findfirst(dir
, _A_SUBDIR
, &fileInfo
) == 0) &&
468 ((fileInfo
.attrib
& _A_SUBDIR
) != 0);
473 // ----------------------------------------------------------------------------
474 // process management
475 // ----------------------------------------------------------------------------
477 int wxKill(long pid
, int sig
)
479 // TODO use SendMessage(WM_QUIT) and TerminateProcess() if needed
484 // Execute a program in an Interactive Shell
485 bool wxShell(const wxString
& command
)
487 wxChar
*shell
= wxGetenv(wxT("COMSPEC"));
489 shell
= wxT("\\COMMAND.COM");
499 // pass the command to execute to the command processor
500 cmd
.Printf(wxT("%s /c %s"), shell
, command
.c_str());
503 return wxExecute(cmd
, TRUE
/* sync */) != 0;
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
510 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
511 long wxGetFreeMemory()
513 #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__)
514 MEMORYSTATUS memStatus
;
515 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
516 GlobalMemoryStatus(&memStatus
);
517 return memStatus
.dwAvailPhys
;
519 return (long)GetFreeSpace(0);
526 ::MessageBeep((UINT
)-1); // default sound
529 wxString
wxGetOsDescription()
537 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
538 if ( ::GetVersionEx(&info
) )
540 switch ( info
.dwPlatformId
)
542 case VER_PLATFORM_WIN32s
:
543 str
= _("Win32s on Windows 3.1");
546 case VER_PLATFORM_WIN32_WINDOWS
:
547 str
.Printf(_("Windows 9%c"),
548 info
.dwMinorVersion
== 0 ? _T('5') : _T('9'));
549 if ( !wxIsEmpty(info
.szCSDVersion
) )
551 str
<< _T(" (") << info
.szCSDVersion
<< _T(')');
555 case VER_PLATFORM_WIN32_NT
:
556 str
.Printf(_T("Windows NT %lu.%lu (build %lu"),
560 if ( !wxIsEmpty(info
.szCSDVersion
) )
562 str
<< _T(", ") << info
.szCSDVersion
;
570 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
575 return _("Windows 3.1");
579 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
581 #if defined(__WIN32__) && !defined(__SC__)
585 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
586 if ( ::GetVersionEx(&info
) )
589 *majorVsn
= info
.dwMajorVersion
;
591 *minorVsn
= info
.dwMinorVersion
;
593 switch ( info
.dwPlatformId
)
595 case VER_PLATFORM_WIN32s
:
598 case VER_PLATFORM_WIN32_WINDOWS
:
601 case VER_PLATFORM_WIN32_NT
:
606 return wxWINDOWS
; // error if we get here, return generic value
608 int retValue
= wxWINDOWS
;
609 #ifdef __WINDOWS_386__
612 #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
613 extern HANDLE g_hPenWin
;
614 retValue
= g_hPenWin
? wxPENWINDOWS
: wxWINDOWS
;
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
633 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
634 static bool gs_inTimer
= FALSE
;
636 class wxSleepTimer
: public wxTimer
639 virtual void Notify()
646 static wxTimer
*wxTheSleepTimer
= NULL
;
648 void wxUsleep(unsigned long milliseconds
)
651 ::Sleep(milliseconds
);
656 wxTheSleepTimer
= new wxSleepTimer
;
658 wxTheSleepTimer
->Start(milliseconds
);
661 if (wxTheApp
->Pending())
662 wxTheApp
->Dispatch();
664 delete wxTheSleepTimer
;
665 wxTheSleepTimer
= NULL
;
669 void wxSleep(int nSecs
)
674 wxTheSleepTimer
= new wxSleepTimer
;
676 wxTheSleepTimer
->Start(nSecs
*1000);
679 if (wxTheApp
->Pending())
680 wxTheApp
->Dispatch();
682 delete wxTheSleepTimer
;
683 wxTheSleepTimer
= NULL
;
686 // Consume all events until no more left
692 #elif defined(__WIN32__) // wxUSE_GUI
694 void wxUsleep(unsigned long milliseconds
)
696 ::Sleep(milliseconds
);
699 void wxSleep(int nSecs
)
701 wxUsleep(1000*nSecs
);
704 #endif // wxUSE_GUI/!wxUSE_GUI
706 // ----------------------------------------------------------------------------
707 // deprecated (in favour of wxLog) log functions
708 // ----------------------------------------------------------------------------
712 // Output a debug mess., in a system dependent fashion.
713 void wxDebugMsg(const wxChar
*fmt
...)
716 static wxChar buffer
[512];
718 if (!wxTheApp
->GetWantDebugOutput())
723 wvsprintf(buffer
,fmt
,ap
) ;
724 OutputDebugString((LPCTSTR
)buffer
) ;
729 // Non-fatal error: pop up message box and (possibly) continue
730 void wxError(const wxString
& msg
, const wxString
& title
)
732 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
733 if (MessageBox(NULL
, (LPCTSTR
)wxBuffer
, (LPCTSTR
)WXSTRINGCAST title
,
734 MB_ICONSTOP
| MB_YESNO
) == IDNO
)
738 // Fatal error: pop up message box and abort
739 void wxFatalError(const wxString
& msg
, const wxString
& title
)
741 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
742 FatalAppExit(0, (LPCTSTR
)wxBuffer
);
745 // ----------------------------------------------------------------------------
746 // functions to work with .INI files
747 // ----------------------------------------------------------------------------
749 // Reading and writing resources (eg WIN.INI, .Xdefaults)
751 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
754 // Don't know what the correct cast should be, but it doesn't
755 // compile in BC++/16-bit without this cast.
756 #if !defined(__WIN32__)
757 return (WritePrivateProfileString((const char*) section
, (const char*) entry
, (const char*) value
, (const char*) file
) != 0);
759 return (WritePrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)value
, (LPCTSTR
)WXSTRINGCAST file
) != 0);
762 return (WriteProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)WXSTRINGCAST value
) != 0);
765 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
768 buf
.Printf(wxT("%.4f"), value
);
770 return wxWriteResource(section
, entry
, buf
, file
);
773 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
776 buf
.Printf(wxT("%ld"), value
);
778 return wxWriteResource(section
, entry
, buf
, file
);
781 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
784 buf
.Printf(wxT("%d"), value
);
786 return wxWriteResource(section
, entry
, buf
, file
);
789 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
791 static const wxChar defunkt
[] = wxT("$$default");
794 int n
= GetPrivateProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
795 (LPTSTR
)wxBuffer
, 1000, (LPCTSTR
)WXSTRINGCAST file
);
796 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
801 int n
= GetProfileString((LPCTSTR
)WXSTRINGCAST section
, (LPCTSTR
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
802 (LPTSTR
)wxBuffer
, 1000);
803 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
806 if (*value
) delete[] (*value
);
807 *value
= copystring(wxBuffer
);
811 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
814 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
817 *value
= (float)wxStrtod(s
, NULL
);
824 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
827 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
830 *value
= wxStrtol(s
, NULL
, 10);
837 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
840 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
843 *value
= (int)wxStrtol(s
, NULL
, 10);
849 #endif // wxUSE_RESOURCES
851 // ---------------------------------------------------------------------------
852 // helper functions for showing a "busy" cursor
853 // ---------------------------------------------------------------------------
855 static HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
856 static HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
857 static int gs_wxBusyCursorCount
= 0;
859 extern HCURSOR
wxGetCurrentBusyCursor()
861 return gs_wxBusyCursor
;
864 // Set the cursor to the busy cursor for all windows
865 void wxBeginBusyCursor(wxCursor
*cursor
)
867 if ( gs_wxBusyCursorCount
++ == 0 )
869 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
870 gs_wxBusyCursorOld
= ::SetCursor(gs_wxBusyCursor
);
872 //else: nothing to do, already set
875 // Restore cursor to normal
876 void wxEndBusyCursor()
878 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
879 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
881 if ( --gs_wxBusyCursorCount
== 0 )
883 ::SetCursor(gs_wxBusyCursorOld
);
885 gs_wxBusyCursorOld
= 0;
889 // TRUE if we're between the above two calls
892 return (gs_wxBusyCursorCount
> 0);
895 // Check whether this window wants to process messages, e.g. Stop button
896 // in long calculations.
897 bool wxCheckForInterrupt(wxWindow
*wnd
)
899 wxCHECK( wnd
, FALSE
);
902 while ( ::PeekMessage(&msg
, GetHwndOf(wnd
), 0, 0, PM_REMOVE
) )
904 ::TranslateMessage(&msg
);
905 ::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
);
1054 //------------------------------------------------------------------------
1055 // wild character routines
1056 //------------------------------------------------------------------------
1058 bool wxIsWild( const wxString
& pattern
)
1060 wxString tmp
= pattern
;
1061 char *pat
= WXSTRINGCAST(tmp
);
1064 case '?': case '*': case '[': case '{':
1075 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1077 wxString tmp1
= pat
;
1078 char *pattern
= WXSTRINGCAST(tmp1
);
1079 wxString tmp2
= text
;
1080 char *str
= WXSTRINGCAST(tmp2
);
1083 bool done
= FALSE
, ret_code
, ok
;
1084 // Below is for vi fans
1085 const char OB
= '{', CB
= '}';
1087 // dot_special means '.' only matches '.'
1088 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1091 while ((*pattern
!= '\0') && (!done
)
1092 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1096 if (*pattern
!= '\0')
1103 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1106 while (*str
!= '\0')
1108 while (*pattern
!= '\0')
1115 if ((*pattern
== '\0') || (*pattern
== ']')) {
1119 if (*pattern
== '\\') {
1121 if (*pattern
== '\0') {
1126 if (*(pattern
+ 1) == '-') {
1129 if (*pattern
== ']') {
1133 if (*pattern
== '\\') {
1135 if (*pattern
== '\0') {
1140 if ((*str
< c
) || (*str
> *pattern
)) {
1144 } else if (*pattern
!= *str
) {
1149 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1150 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1154 if (*pattern
!= '\0') {
1164 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1167 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1168 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1169 if (*pattern
== '\\')
1171 ok
= (*pattern
++ == *cp
++);
1173 if (*pattern
== '\0') {
1179 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1180 if (*++pattern
== '\\') {
1181 if (*++pattern
== CB
)
1186 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1187 if (*++pattern
== '\\') {
1188 if (*++pattern
== CB
|| *pattern
== ',')
1193 if (*pattern
!= '\0')
1198 if (*str
== *pattern
) {
1205 while (*pattern
== '*')
1207 return ((*str
== '\0') && (*pattern
== '\0'));
1214 // maximum mumber of lines the output console should have
1215 static const WORD MAX_CONSOLE_LINES
= 500;
1217 BOOL WINAPI
MyConsoleHandler( DWORD dwCtrlType
) { // control signal type
1222 void wxRedirectIOToConsole()
1226 CONSOLE_SCREEN_BUFFER_INFO coninfo
;
1229 // allocate a console for this app
1232 // set the screen buffer to be big enough to let us scroll text
1233 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
),
1235 coninfo
.dwSize
.Y
= MAX_CONSOLE_LINES
;
1236 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
),
1239 // redirect unbuffered STDOUT to the console
1240 lStdHandle
= (long)GetStdHandle(STD_OUTPUT_HANDLE
);
1241 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1242 if(hConHandle
<= 0) return;
1243 fp
= _fdopen( hConHandle
, "w" );
1245 setvbuf( stdout
, NULL
, _IONBF
, 0 );
1247 // redirect unbuffered STDIN to the console
1248 lStdHandle
= (long)GetStdHandle(STD_INPUT_HANDLE
);
1249 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1250 if(hConHandle
<= 0) return;
1251 fp
= _fdopen( hConHandle
, "r" );
1253 setvbuf( stdin
, NULL
, _IONBF
, 0 );
1255 // redirect unbuffered STDERR to the console
1256 lStdHandle
= (long)GetStdHandle(STD_ERROR_HANDLE
);
1257 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1258 if(hConHandle
<= 0) return;
1259 fp
= _fdopen( hConHandle
, "w" );
1261 setvbuf( stderr
, NULL
, _IONBF
, 0 );
1263 // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
1264 // point to console as well
1265 ios::sync_with_stdio();
1267 SetConsoleCtrlHandler(MyConsoleHandler
, TRUE
);
1271 void wxRedirectIOToConsole()