]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utils.cpp
added wxGetDiskSpace for Win/Unix
[wxWidgets.git] / src / msw / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 // #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now.
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/utils.h"
33 #include "wx/app.h"
34 #include "wx/cursor.h"
35 #include "wx/intl.h"
36 #include "wx/log.h"
37 #endif //WX_PRECOMP
38
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.
43 //
44 #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
45 extern "C" {
46 #include <winsock.h> // we use socket functions in wxGetFullHostName()
47 }
48 #endif
49
50 #include "wx/msw/private.h" // includes <windows.h>
51
52 #include "wx/timer.h"
53
54 #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__)
55 #include <direct.h>
56
57 #ifndef __MWERKS__
58 #include <dos.h>
59 #endif
60 #endif //GNUWIN32
61
62 #if defined(__GNUWIN32__) && !defined(__TWIN32__)
63 #include <sys/unistd.h>
64 #include <sys/stat.h>
65 #endif //GNUWIN32
66
67 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
68 // this (3.1 I believe) and how to test for it.
69 // If this works for Borland 4.0 as well, then no worries.
70 #include <dir.h>
71 #endif
72
73 // VZ: there is some code using NetXXX() functions to get the full user name:
74 // I don't think it's a good idea because they don't work under Win95 and
75 // seem to return the same as wxGetUserId() under NT. If you really want
76 // to use them, just #define USE_NET_API
77 #undef USE_NET_API
78
79 #ifdef USE_NET_API
80 #include <lm.h>
81 #endif // USE_NET_API
82
83 #if defined(__WIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__)
84 #include <io.h>
85
86 #ifndef __GNUWIN32__
87 #include <shellapi.h>
88 #endif
89 #endif
90
91 #ifndef __WATCOMC__
92 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
93 #include <errno.h>
94 #endif
95 #endif
96
97 //// BEGIN for console support: VC++ only
98 #ifdef __VISUALC__
99
100 #include "wx/msw/msvcrt.h"
101
102 #include <fcntl.h>
103
104 #include "wx/ioswrap.h"
105
106 #include "wx/ioswrap.h"
107
108 /* Need to undef new if including crtdbg.h */
109 # ifdef new
110 # undef new
111 # endif
112
113 #ifndef __WIN16__
114 # include <crtdbg.h>
115 #endif
116
117 # if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
118 # define new new(__TFILE__,__LINE__)
119 # endif
120
121 #endif
122 // __VISUALC__
123 /// END for console support
124
125 // ----------------------------------------------------------------------------
126 // constants
127 // ----------------------------------------------------------------------------
128
129 // In the WIN.INI file
130 static const wxChar WX_SECTION[] = wxT("wxWindows");
131 static const wxChar eUSERNAME[] = wxT("UserName");
132
133 // these are only used under Win16
134 #if !defined(__WIN32__) && !defined(__WXMICROWIN__)
135 static const wxChar eHOSTNAME[] = wxT("HostName");
136 static const wxChar eUSERID[] = wxT("UserId");
137 #endif // !Win32
138
139 #ifndef __WXMICROWIN__
140
141 // ============================================================================
142 // implementation
143 // ============================================================================
144
145 // ----------------------------------------------------------------------------
146 // get host name and related
147 // ----------------------------------------------------------------------------
148
149 // Get hostname only (without domain name)
150 bool wxGetHostName(wxChar *buf, int maxSize)
151 {
152 #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__)
153 DWORD nSize = maxSize;
154 if ( !::GetComputerName(buf, &nSize) )
155 {
156 wxLogLastError(wxT("GetComputerName"));
157
158 return FALSE;
159 }
160
161 return TRUE;
162 #else
163 wxChar *sysname;
164 const wxChar *default_host = wxT("noname");
165
166 if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) {
167 GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
168 } else
169 wxStrncpy(buf, sysname, maxSize - 1);
170 buf[maxSize] = wxT('\0');
171 return *buf ? TRUE : FALSE;
172 #endif
173 }
174
175 // get full hostname (with domain name if possible)
176 bool wxGetFullHostName(wxChar *buf, int maxSize)
177 {
178 #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
179 // TODO should use GetComputerNameEx() when available
180 WSADATA wsa;
181 if ( WSAStartup(MAKEWORD(1, 1), &wsa) == 0 )
182 {
183 wxString host;
184 char bufA[256];
185 if ( gethostname(bufA, WXSIZEOF(bufA)) == 0 )
186 {
187 // gethostname() won't usually include the DNS domain name, for
188 // this we need to work a bit more
189 if ( !strchr(bufA, '.') )
190 {
191 struct hostent *pHostEnt = gethostbyname(bufA);
192
193 if ( pHostEnt )
194 {
195 // Windows will use DNS internally now
196 pHostEnt = gethostbyaddr(pHostEnt->h_addr, 4, PF_INET);
197 }
198
199 if ( pHostEnt )
200 {
201 host = pHostEnt->h_name;
202 }
203 }
204 }
205
206 WSACleanup();
207
208 if ( !!host )
209 {
210 wxStrncpy(buf, host, maxSize);
211
212 return TRUE;
213 }
214 }
215 #endif // Win32
216
217 return wxGetHostName(buf, maxSize);
218 }
219
220 // Get user ID e.g. jacs
221 bool wxGetUserId(wxChar *buf, int maxSize)
222 {
223 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__)
224 DWORD nSize = maxSize;
225 if ( ::GetUserName(buf, &nSize) == 0 )
226 {
227 // actually, it does happen on Win9x if the user didn't log on
228 DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize);
229 if ( res == 0 )
230 {
231 // not found
232 return FALSE;
233 }
234 }
235
236 return TRUE;
237 #else // Win16 or Win32s
238 wxChar *user;
239 const wxChar *default_id = wxT("anonymous");
240
241 // Can't assume we have NIS (PC-NFS) or some other ID daemon
242 // So we ...
243 if ( (user = wxGetenv(wxT("USER"))) == NULL &&
244 (user = wxGetenv(wxT("LOGNAME"))) == NULL )
245 {
246 // Use wxWindows configuration data (comming soon)
247 GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1);
248 }
249 else
250 {
251 wxStrncpy(buf, user, maxSize - 1);
252 }
253
254 return *buf ? TRUE : FALSE;
255 #endif
256 }
257
258 // Get user name e.g. Julian Smart
259 bool wxGetUserName(wxChar *buf, int maxSize)
260 {
261 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
262 extern HANDLE g_hPenWin; // PenWindows Running?
263 if (g_hPenWin)
264 {
265 // PenWindows Does have a user concept!
266 // Get the current owner of the recognizer
267 GetPrivateProfileString("Current", "User", default_name, wxBuffer, maxSize - 1, "PENWIN.INI");
268 strncpy(buf, wxBuffer, maxSize - 1);
269 }
270 else
271 #endif
272 {
273 #ifdef USE_NET_API
274 CHAR szUserName[256];
275 if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
276 return FALSE;
277
278 // TODO how to get the domain name?
279 CHAR *szDomain = "";
280
281 // the code is based on the MSDN example (also see KB article Q119670)
282 WCHAR wszUserName[256]; // Unicode user name
283 WCHAR wszDomain[256];
284 LPBYTE ComputerName;
285
286 USER_INFO_2 *ui2; // User structure
287
288 // Convert ANSI user name and domain to Unicode
289 MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1,
290 wszUserName, WXSIZEOF(wszUserName) );
291 MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1,
292 wszDomain, WXSIZEOF(wszDomain) );
293
294 // Get the computer name of a DC for the domain.
295 if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success )
296 {
297 wxLogError(wxT("Can not find domain controller"));
298
299 goto error;
300 }
301
302 // Look up the user on the DC
303 NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName,
304 (LPWSTR)&wszUserName,
305 2, // level - we want USER_INFO_2
306 (LPBYTE *) &ui2 );
307 switch ( status )
308 {
309 case NERR_Success:
310 // ok
311 break;
312
313 case NERR_InvalidComputer:
314 wxLogError(wxT("Invalid domain controller name."));
315
316 goto error;
317
318 case NERR_UserNotFound:
319 wxLogError(wxT("Invalid user name '%s'."), szUserName);
320
321 goto error;
322
323 default:
324 wxLogSysError(wxT("Can't get information about user"));
325
326 goto error;
327 }
328
329 // Convert the Unicode full name to ANSI
330 WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
331 buf, maxSize, NULL, NULL );
332
333 return TRUE;
334
335 error:
336 wxLogError(wxT("Couldn't look up full user name."));
337
338 return FALSE;
339 #else // !USE_NET_API
340 // Could use NIS, MS-Mail or other site specific programs
341 // Use wxWindows configuration data
342 bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxT(""), buf, maxSize - 1) != 0;
343 if ( !ok )
344 {
345 ok = wxGetUserId(buf, maxSize);
346 }
347
348 if ( !ok )
349 {
350 wxStrncpy(buf, wxT("Unknown User"), maxSize);
351 }
352 #endif // Win32/16
353 }
354
355 return TRUE;
356 }
357
358 const wxChar* wxGetHomeDir(wxString *pstr)
359 {
360 wxString& strDir = *pstr;
361
362 #if defined(__UNIX__) && !defined(__TWIN32__)
363 const wxChar *szHome = wxGetenv("HOME");
364 if ( szHome == NULL ) {
365 // we're homeless...
366 wxLogWarning(_("can't find user's HOME, using current directory."));
367 strDir = wxT(".");
368 }
369 else
370 strDir = szHome;
371
372 // add a trailing slash if needed
373 if ( strDir.Last() != wxT('/') )
374 strDir << wxT('/');
375 #else // Windows
376 #ifdef __WIN32__
377 const wxChar *szHome = wxGetenv(wxT("HOMEDRIVE"));
378 if ( szHome != NULL )
379 strDir << szHome;
380 szHome = wxGetenv(wxT("HOMEPATH"));
381 if ( szHome != NULL ) {
382 strDir << szHome;
383
384 // the idea is that under NT these variables have default values
385 // of "%systemdrive%:" and "\\". As we don't want to create our
386 // config files in the root directory of the system drive, we will
387 // create it in our program's dir. However, if the user took care
388 // to set HOMEPATH to something other than "\\", we suppose that he
389 // knows what he is doing and use the supplied value.
390 if ( wxStrcmp(szHome, wxT("\\")) != 0 )
391 return strDir.c_str();
392 }
393
394 #else // Win16
395 // Win16 has no idea about home, so use the working directory instead
396 #endif // WIN16/32
397
398 // 260 was taken from windef.h
399 #ifndef MAX_PATH
400 #define MAX_PATH 260
401 #endif
402
403 wxString strPath;
404 ::GetModuleFileName(::GetModuleHandle(NULL),
405 strPath.GetWriteBuf(MAX_PATH), MAX_PATH);
406 strPath.UngetWriteBuf();
407
408 // extract the dir name
409 wxSplitPath(strPath, &strDir, NULL, NULL);
410
411 #endif // UNIX/Win
412
413 return strDir.c_str();
414 }
415
416 wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
417 {
418 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
419 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
420 // static buffer and sometimes I don't even know what.
421 static wxString s_home;
422
423 return (wxChar *)wxGetHomeDir(&s_home);
424 }
425
426 bool wxDirExists(const wxString& dir)
427 {
428 #ifdef __WXMICROWIN__
429 return wxPathExist(dir);
430 #elif defined(__WIN32__)
431 DWORD attribs = GetFileAttributes(dir);
432 return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY));
433 #else // Win16
434 #ifdef __BORLANDC__
435 struct ffblk fileInfo;
436 #else
437 struct find_t fileInfo;
438 #endif
439 // In Borland findfirst has a different argument
440 // ordering from _dos_findfirst. But _dos_findfirst
441 // _should_ be ok in both MS and Borland... why not?
442 #ifdef __BORLANDC__
443 return (findfirst(dir, &fileInfo, _A_SUBDIR) == 0 &&
444 (fileInfo.ff_attrib & _A_SUBDIR) != 0);
445 #else
446 return (_dos_findfirst(dir, _A_SUBDIR, &fileInfo) == 0) &&
447 ((fileInfo.attrib & _A_SUBDIR) != 0);
448 #endif
449 #endif // Win32/16
450 }
451
452 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
453 {
454 if ( path.empty() )
455 return FALSE;
456
457 #ifdef __WIN32__
458 // GetDiskFreeSpaceEx() is not available under original Win95, check for
459 // it
460 typedef BOOL (*GetDiskFreeSpaceEx_t)(LPCTSTR,
461 PULARGE_INTEGER,
462 PULARGE_INTEGER,
463 PULARGE_INTEGER);
464
465 GetDiskFreeSpaceEx_t
466 pGetDiskFreeSpaceEx = ::GetProcAddress
467 (
468 ::GetModuleHandle(_T("kernel32.dll")),
469 #if wxUSE_UNICODE
470 "GetDiskFreeSpaceExW"
471 #else
472 "GetDiskFreeSpaceExA"
473 #endif
474 );
475
476 if ( pGetDiskFreeSpaceEx )
477 {
478 ULARGE_INTEGER bytesFree, bytesTotal;
479
480 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
481 if ( !pGetDiskFreeSpaceEx(path,
482 &bytesFree,
483 &bytesTotal,
484 NULL) )
485 {
486 wxLogLastError(_T("GetDiskFreeSpaceEx"));
487
488 return FALSE;
489 }
490
491 if ( pTotal )
492 {
493 *pTotal = wxLongLong(bytesTotal.HighPart, bytesTotal.LowPart);
494 }
495
496 if ( pFree )
497 {
498 *pFree = wxLongLong(bytesFree.HighPart, bytesFree.LowPart);
499 }
500 }
501 else
502 #endif // Win32
503 {
504 DWORD lSectorsPerCluster,
505 lBytesPerSector,
506 lNumberOfFreeClusters,
507 lTotalNumberOfClusters;
508
509 // FIXME: this is wrong, we should extract the root drive from path
510 // instead, but this is the job for wxFileName...
511 if ( !::GetDiskFreeSpace(path,
512 &lSectorsPerCluster,
513 &lBytesPerSector,
514 &lNumberOfFreeClusters,
515 &lTotalNumberOfClusters) )
516 {
517 wxLogLastError(_T("GetDiskFreeSpace"));
518
519 return FALSE;
520 }
521
522 // there's a problem with drives larger than 2GB on non Win32!!
523 // let's calculate it, nevertheless....
524 wxLongLong lBytesPerCluster = lSectorsPerCluster;
525 lBytesPerCluster *= lBytesPerSector;
526
527 if ( pTotal )
528 {
529 *pTotal = lBytesPerCluster;
530 *pTotal *= lTotalNumberOfClusters;
531 }
532
533 if ( pFree )
534 {
535 *pFree = lBytesPerCluster;
536 *pFree *= lNumberOfFreeClusters;
537 }
538 }
539
540 return TRUE;
541 }
542
543 // ----------------------------------------------------------------------------
544 // env vars
545 // ----------------------------------------------------------------------------
546
547 bool wxGetEnv(const wxString& var, wxString *value)
548 {
549 #ifdef __WIN16__
550 const wxChar* ret = wxGetenv(var);
551 if (ret)
552 {
553 *value = ret;
554 return TRUE;
555 }
556 else
557 return FALSE;
558 #else
559 // first get the size of the buffer
560 DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0);
561 if ( !dwRet )
562 {
563 // this means that there is no such variable
564 return FALSE;
565 }
566
567 if ( value )
568 {
569 (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet);
570 value->UngetWriteBuf();
571 }
572
573 return TRUE;
574 #endif
575 }
576
577 bool wxSetEnv(const wxString& var, const wxChar *value)
578 {
579 // some compilers have putenv() or _putenv() or _wputenv() but it's better
580 // to always use Win32 function directly instead of dealing with them
581 #if defined(__WIN32__)
582 if ( !::SetEnvironmentVariable(var, value) )
583 {
584 wxLogLastError(_T("SetEnvironmentVariable"));
585
586 return FALSE;
587 }
588
589 return TRUE;
590 #else // no way to set env vars
591 return FALSE;
592 #endif
593 }
594
595 // ----------------------------------------------------------------------------
596 // process management
597 // ----------------------------------------------------------------------------
598
599 #ifdef __WIN32__
600
601 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
602 struct wxFindByPidParams
603 {
604 wxFindByPidParams() { hwnd = 0; pid = 0; }
605
606 // the HWND used to return the result
607 HWND hwnd;
608
609 // the PID we're looking from
610 DWORD pid;
611 };
612
613 // wxKill helper: EnumWindows() callback which is used to find the first (top
614 // level) window belonging to the given process
615 BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam)
616 {
617 DWORD pid;
618 (void)::GetWindowThreadProcessId(hwnd, &pid);
619
620 wxFindByPidParams *params = (wxFindByPidParams *)lParam;
621 if ( pid == params->pid )
622 {
623 // remember the window we found
624 params->hwnd = hwnd;
625
626 // return FALSE to stop the enumeration
627 return FALSE;
628 }
629
630 // continue enumeration
631 return TRUE;
632 }
633
634 #endif // __WIN32__
635
636 int wxKill(long pid, wxSignal sig, wxKillError *krc)
637 {
638 #ifdef __WIN32__
639 // get the process handle to operate on
640 HANDLE hProcess = ::OpenProcess(SYNCHRONIZE |
641 PROCESS_TERMINATE |
642 PROCESS_QUERY_INFORMATION,
643 FALSE, // not inheritable
644 (DWORD)pid);
645 if ( hProcess == NULL )
646 {
647 if ( krc )
648 {
649 if ( ::GetLastError() == ERROR_ACCESS_DENIED )
650 {
651 *krc = wxKILL_ACCESS_DENIED;
652 }
653 else
654 {
655 *krc = wxKILL_NO_PROCESS;
656 }
657 }
658
659 return -1;
660 }
661
662 bool ok = TRUE;
663 switch ( sig )
664 {
665 case wxSIGKILL:
666 // kill the process forcefully returning -1 as error code
667 if ( !::TerminateProcess(hProcess, (UINT)-1) )
668 {
669 wxLogSysError(_("Failed to kill process %d"), pid);
670
671 if ( krc )
672 {
673 // this is not supposed to happen if we could open the
674 // process
675 *krc = wxKILL_ERROR;
676 }
677
678 ok = FALSE;
679 }
680 break;
681
682 case wxSIGNONE:
683 // do nothing, we just want to test for process existence
684 break;
685
686 default:
687 // any other signal means "terminate"
688 {
689 wxFindByPidParams params;
690 params.pid = (DWORD)pid;
691
692 // EnumWindows() has nice semantics: it returns 0 if it found
693 // something or if an error occured and non zero if it
694 // enumerated all the window
695 if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)&params) )
696 {
697 // did we find any window?
698 if ( params.hwnd )
699 {
700 // tell the app to close
701 //
702 // NB: this is the harshest way, the app won't have
703 // opportunity to save any files, for example, but
704 // this is probably what we want here. If not we
705 // can also use SendMesageTimeout(WM_CLOSE)
706 if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) )
707 {
708 wxLogLastError(_T("PostMessage(WM_QUIT)"));
709 }
710 }
711 else // it was an error then
712 {
713 wxLogLastError(_T("EnumWindows"));
714
715 ok = FALSE;
716 }
717 }
718 else // no windows for this PID
719 {
720 if ( krc )
721 {
722 *krc = wxKILL_ERROR;
723 }
724
725 ok = FALSE;
726 }
727 }
728 }
729
730 // the return code
731 DWORD rc;
732
733 if ( ok )
734 {
735 // as we wait for a short time, we can use just WaitForSingleObject()
736 // and not MsgWaitForMultipleObjects()
737 switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) )
738 {
739 case WAIT_OBJECT_0:
740 // process terminated
741 if ( !::GetExitCodeProcess(hProcess, &rc) )
742 {
743 wxLogLastError(_T("GetExitCodeProcess"));
744 }
745 break;
746
747 default:
748 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
749 // fall through
750
751 case WAIT_FAILED:
752 wxLogLastError(_T("WaitForSingleObject"));
753 // fall through
754
755 case WAIT_TIMEOUT:
756 if ( krc )
757 {
758 *krc = wxKILL_ERROR;
759 }
760
761 rc = STILL_ACTIVE;
762 break;
763 }
764 }
765 else // !ok
766 {
767 // just to suppress the warnings about uninitialized variable
768 rc = 0;
769 }
770
771 ::CloseHandle(hProcess);
772
773 // the return code is the same as from Unix kill(): 0 if killed
774 // successfully or -1 on error
775 if ( sig == wxSIGNONE )
776 {
777 if ( ok && rc == STILL_ACTIVE )
778 {
779 // there is such process => success
780 return 0;
781 }
782 }
783 else // not SIGNONE
784 {
785 if ( ok && rc != STILL_ACTIVE )
786 {
787 // killed => success
788 return 0;
789 }
790 }
791 #else // Win15
792 wxFAIL_MSG( _T("not implemented") );
793 #endif // Win32/Win16
794
795 // error
796 return -1;
797 }
798
799 // Execute a program in an Interactive Shell
800 bool wxShell(const wxString& command)
801 {
802 wxChar *shell = wxGetenv(wxT("COMSPEC"));
803 if ( !shell )
804 shell = wxT("\\COMMAND.COM");
805
806 wxString cmd;
807 if ( !command )
808 {
809 // just the shell
810 cmd = shell;
811 }
812 else
813 {
814 // pass the command to execute to the command processor
815 cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
816 }
817
818 return wxExecute(cmd, TRUE /* sync */) != 0;
819 }
820
821 // ----------------------------------------------------------------------------
822 // misc
823 // ----------------------------------------------------------------------------
824
825 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
826 long wxGetFreeMemory()
827 {
828 #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__)
829 MEMORYSTATUS memStatus;
830 memStatus.dwLength = sizeof(MEMORYSTATUS);
831 GlobalMemoryStatus(&memStatus);
832 return memStatus.dwAvailPhys;
833 #else
834 return (long)GetFreeSpace(0);
835 #endif
836 }
837
838 // Emit a beeeeeep
839 void wxBell()
840 {
841 ::MessageBeep((UINT)-1); // default sound
842 }
843
844 wxString wxGetOsDescription()
845 {
846 #ifdef __WIN32__
847 wxString str;
848
849 OSVERSIONINFO info;
850 wxZeroMemory(info);
851
852 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
853 if ( ::GetVersionEx(&info) )
854 {
855 switch ( info.dwPlatformId )
856 {
857 case VER_PLATFORM_WIN32s:
858 str = _("Win32s on Windows 3.1");
859 break;
860
861 case VER_PLATFORM_WIN32_WINDOWS:
862 str.Printf(_("Windows 9%c"),
863 info.dwMinorVersion == 0 ? _T('5') : _T('8'));
864 if ( !wxIsEmpty(info.szCSDVersion) )
865 {
866 str << _T(" (") << info.szCSDVersion << _T(')');
867 }
868 break;
869
870 case VER_PLATFORM_WIN32_NT:
871 str.Printf(_T("Windows NT %lu.%lu (build %lu"),
872 info.dwMajorVersion,
873 info.dwMinorVersion,
874 info.dwBuildNumber);
875 if ( !wxIsEmpty(info.szCSDVersion) )
876 {
877 str << _T(", ") << info.szCSDVersion;
878 }
879 str << _T(')');
880 break;
881 }
882 }
883 else
884 {
885 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
886 }
887
888 return str;
889 #else // Win16
890 return _("Windows 3.1");
891 #endif // Win32/16
892 }
893
894 int wxGetOsVersion(int *majorVsn, int *minorVsn)
895 {
896 #if defined(__WIN32__) && !defined(__SC__)
897 OSVERSIONINFO info;
898 wxZeroMemory(info);
899
900 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
901 if ( ::GetVersionEx(&info) )
902 {
903 if (majorVsn)
904 *majorVsn = info.dwMajorVersion;
905 if (minorVsn)
906 *minorVsn = info.dwMinorVersion;
907
908 switch ( info.dwPlatformId )
909 {
910 case VER_PLATFORM_WIN32s:
911 return wxWIN32S;
912
913 case VER_PLATFORM_WIN32_WINDOWS:
914 return wxWIN95;
915
916 case VER_PLATFORM_WIN32_NT:
917 return wxWINDOWS_NT;
918 }
919 }
920
921 return wxWINDOWS; // error if we get here, return generic value
922 #else // Win16
923 int retValue = wxWINDOWS;
924 #ifdef __WINDOWS_386__
925 retValue = wxWIN386;
926 #else
927 #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
928 extern HANDLE g_hPenWin;
929 retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS;
930 #endif
931 #endif
932
933 if (majorVsn)
934 *majorVsn = 3;
935 if (minorVsn)
936 *minorVsn = 1;
937
938 return retValue;
939 #endif
940 }
941
942 // ----------------------------------------------------------------------------
943 // sleep functions
944 // ----------------------------------------------------------------------------
945
946 #if wxUSE_GUI
947
948 #if wxUSE_TIMER
949
950 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
951 static bool gs_inTimer = FALSE;
952
953 class wxSleepTimer : public wxTimer
954 {
955 public:
956 virtual void Notify()
957 {
958 gs_inTimer = FALSE;
959 Stop();
960 }
961 };
962
963 static wxTimer *wxTheSleepTimer = NULL;
964
965 void wxUsleep(unsigned long milliseconds)
966 {
967 #ifdef __WIN32__
968 ::Sleep(milliseconds);
969 #else // !Win32
970 if (gs_inTimer)
971 return;
972
973 wxTheSleepTimer = new wxSleepTimer;
974 gs_inTimer = TRUE;
975 wxTheSleepTimer->Start(milliseconds);
976 while (gs_inTimer)
977 {
978 if (wxTheApp->Pending())
979 wxTheApp->Dispatch();
980 }
981 delete wxTheSleepTimer;
982 wxTheSleepTimer = NULL;
983 #endif // Win32/!Win32
984 }
985
986 void wxSleep(int nSecs)
987 {
988 if (gs_inTimer)
989 return;
990
991 wxTheSleepTimer = new wxSleepTimer;
992 gs_inTimer = TRUE;
993 wxTheSleepTimer->Start(nSecs*1000);
994 while (gs_inTimer)
995 {
996 if (wxTheApp->Pending())
997 wxTheApp->Dispatch();
998 }
999 delete wxTheSleepTimer;
1000 wxTheSleepTimer = NULL;
1001 }
1002
1003 // Consume all events until no more left
1004 void wxFlushEvents()
1005 {
1006 // wxYield();
1007 }
1008
1009 #endif // wxUSE_TIMER
1010
1011 #elif defined(__WIN32__) // wxUSE_GUI
1012
1013 void wxUsleep(unsigned long milliseconds)
1014 {
1015 ::Sleep(milliseconds);
1016 }
1017
1018 void wxSleep(int nSecs)
1019 {
1020 wxUsleep(1000*nSecs);
1021 }
1022
1023 #endif // wxUSE_GUI/!wxUSE_GUI
1024 #endif
1025 // __WXMICROWIN__
1026
1027 // ----------------------------------------------------------------------------
1028 // deprecated (in favour of wxLog) log functions
1029 // ----------------------------------------------------------------------------
1030
1031 #if wxUSE_GUI
1032
1033 // Output a debug mess., in a system dependent fashion.
1034 #ifndef __WXMICROWIN__
1035 void wxDebugMsg(const wxChar *fmt ...)
1036 {
1037 va_list ap;
1038 static wxChar buffer[512];
1039
1040 if (!wxTheApp->GetWantDebugOutput())
1041 return ;
1042
1043 va_start(ap, fmt);
1044
1045 wvsprintf(buffer,fmt,ap) ;
1046 OutputDebugString((LPCTSTR)buffer) ;
1047
1048 va_end(ap);
1049 }
1050
1051 // Non-fatal error: pop up message box and (possibly) continue
1052 void wxError(const wxString& msg, const wxString& title)
1053 {
1054 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
1055 if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title,
1056 MB_ICONSTOP | MB_YESNO) == IDNO)
1057 wxExit();
1058 }
1059
1060 // Fatal error: pop up message box and abort
1061 void wxFatalError(const wxString& msg, const wxString& title)
1062 {
1063 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
1064 FatalAppExit(0, (LPCTSTR)wxBuffer);
1065 }
1066 #endif // __WXMICROWIN__
1067
1068 // ----------------------------------------------------------------------------
1069 // functions to work with .INI files
1070 // ----------------------------------------------------------------------------
1071
1072 // Reading and writing resources (eg WIN.INI, .Xdefaults)
1073 #if wxUSE_RESOURCES
1074 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
1075 {
1076 if (file != wxT(""))
1077 // Don't know what the correct cast should be, but it doesn't
1078 // compile in BC++/16-bit without this cast.
1079 #if !defined(__WIN32__)
1080 return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0);
1081 #else
1082 return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0);
1083 #endif
1084 else
1085 return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0);
1086 }
1087
1088 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
1089 {
1090 wxString buf;
1091 buf.Printf(wxT("%.4f"), value);
1092
1093 return wxWriteResource(section, entry, buf, file);
1094 }
1095
1096 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
1097 {
1098 wxString buf;
1099 buf.Printf(wxT("%ld"), value);
1100
1101 return wxWriteResource(section, entry, buf, file);
1102 }
1103
1104 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
1105 {
1106 wxString buf;
1107 buf.Printf(wxT("%d"), value);
1108
1109 return wxWriteResource(section, entry, buf, file);
1110 }
1111
1112 bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file)
1113 {
1114 static const wxChar defunkt[] = wxT("$$default");
1115 if (file != wxT(""))
1116 {
1117 int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt,
1118 (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file);
1119 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
1120 return FALSE;
1121 }
1122 else
1123 {
1124 int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt,
1125 (LPTSTR)wxBuffer, 1000);
1126 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
1127 return FALSE;
1128 }
1129 if (*value) delete[] (*value);
1130 *value = copystring(wxBuffer);
1131 return TRUE;
1132 }
1133
1134 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
1135 {
1136 wxChar *s = NULL;
1137 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
1138 if (succ)
1139 {
1140 *value = (float)wxStrtod(s, NULL);
1141 delete[] s;
1142 return TRUE;
1143 }
1144 else return FALSE;
1145 }
1146
1147 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
1148 {
1149 wxChar *s = NULL;
1150 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
1151 if (succ)
1152 {
1153 *value = wxStrtol(s, NULL, 10);
1154 delete[] s;
1155 return TRUE;
1156 }
1157 else return FALSE;
1158 }
1159
1160 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
1161 {
1162 wxChar *s = NULL;
1163 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
1164 if (succ)
1165 {
1166 *value = (int)wxStrtol(s, NULL, 10);
1167 delete[] s;
1168 return TRUE;
1169 }
1170 else return FALSE;
1171 }
1172 #endif // wxUSE_RESOURCES
1173
1174 // ---------------------------------------------------------------------------
1175 // helper functions for showing a "busy" cursor
1176 // ---------------------------------------------------------------------------
1177
1178 static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
1179 static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
1180 static int gs_wxBusyCursorCount = 0;
1181
1182 extern HCURSOR wxGetCurrentBusyCursor()
1183 {
1184 return gs_wxBusyCursor;
1185 }
1186
1187 // Set the cursor to the busy cursor for all windows
1188 void wxBeginBusyCursor(wxCursor *cursor)
1189 {
1190 if ( gs_wxBusyCursorCount++ == 0 )
1191 {
1192 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
1193 #ifndef __WXMICROWIN__
1194 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
1195 #endif
1196 }
1197 //else: nothing to do, already set
1198 }
1199
1200 // Restore cursor to normal
1201 void wxEndBusyCursor()
1202 {
1203 wxCHECK_RET( gs_wxBusyCursorCount > 0,
1204 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
1205
1206 if ( --gs_wxBusyCursorCount == 0 )
1207 {
1208 #ifndef __WXMICROWIN__
1209 ::SetCursor(gs_wxBusyCursorOld);
1210 #endif
1211 gs_wxBusyCursorOld = 0;
1212 }
1213 }
1214
1215 // TRUE if we're between the above two calls
1216 bool wxIsBusy()
1217 {
1218 return (gs_wxBusyCursorCount > 0);
1219 }
1220
1221 // Check whether this window wants to process messages, e.g. Stop button
1222 // in long calculations.
1223 bool wxCheckForInterrupt(wxWindow *wnd)
1224 {
1225 wxCHECK( wnd, FALSE );
1226
1227 MSG msg;
1228 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
1229 {
1230 ::TranslateMessage(&msg);
1231 ::DispatchMessage(&msg);
1232 }
1233
1234 return TRUE;
1235 }
1236
1237 // MSW only: get user-defined resource from the .res file.
1238 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
1239
1240 #ifndef __WXMICROWIN__
1241 wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
1242 {
1243 HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType);
1244 if ( hResource == 0 )
1245 return NULL;
1246
1247 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
1248 if ( hData == 0 )
1249 return NULL;
1250
1251 wxChar *theText = (wxChar *)::LockResource(hData);
1252 if ( !theText )
1253 return NULL;
1254
1255 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
1256 // so we need to find the length of the resource.
1257 int len = ::SizeofResource(wxGetInstance(), hResource);
1258 wxChar *s = new wxChar[len+1];
1259 wxStrncpy(s,theText,len);
1260 s[len]=0;
1261
1262 // wxChar *s = copystring(theText);
1263
1264 // Obsolete in WIN32
1265 #ifndef __WIN32__
1266 UnlockResource(hData);
1267 #endif
1268
1269 // No need??
1270 // GlobalFree(hData);
1271
1272 return s;
1273 }
1274 #endif
1275
1276 // ----------------------------------------------------------------------------
1277 // get display info
1278 // ----------------------------------------------------------------------------
1279
1280 // See also the wxGetMousePosition in window.cpp
1281 // Deprecated: use wxPoint wxGetMousePosition() instead
1282 void wxGetMousePosition( int* x, int* y )
1283 {
1284 POINT pt;
1285 GetCursorPos( & pt );
1286 if ( x ) *x = pt.x;
1287 if ( y ) *y = pt.y;
1288 };
1289
1290 // Return TRUE if we have a colour display
1291 bool wxColourDisplay()
1292 {
1293 #ifdef __WXMICROWIN__
1294 // MICROWIN_TODO
1295 return TRUE;
1296 #else
1297 // this function is called from wxDC ctor so it is called a *lot* of times
1298 // hence we optimize it a bit but doign the check only once
1299 //
1300 // this should be MT safe as only the GUI thread (holding the GUI mutex)
1301 // can call us
1302 static int s_isColour = -1;
1303
1304 if ( s_isColour == -1 )
1305 {
1306 ScreenHDC dc;
1307 int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
1308
1309 s_isColour = (noCols == -1) || (noCols > 2);
1310 }
1311
1312 return s_isColour != 0;
1313 #endif
1314 }
1315
1316 // Returns depth of screen
1317 int wxDisplayDepth()
1318 {
1319 ScreenHDC dc;
1320 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
1321 }
1322
1323 // Get size of display
1324 void wxDisplaySize(int *width, int *height)
1325 {
1326 #ifdef __WXMICROWIN__
1327 RECT rect;
1328 HWND hWnd = GetDesktopWindow();
1329 ::GetWindowRect(hWnd, & rect);
1330
1331 *width = rect.right - rect.left;
1332 *height = rect.bottom - rect.top;
1333 #else
1334 ScreenHDC dc;
1335
1336 if ( width ) *width = GetDeviceCaps(dc, HORZRES);
1337 if ( height ) *height = GetDeviceCaps(dc, VERTRES);
1338 #endif
1339 }
1340
1341 void wxDisplaySizeMM(int *width, int *height)
1342 {
1343 #ifdef __WXMICROWIN__
1344 // MICROWIN_TODO
1345 *width = 0; * height = 0;
1346 #else
1347 ScreenHDC dc;
1348
1349 if ( width ) *width = GetDeviceCaps(dc, HORZSIZE);
1350 if ( height ) *height = GetDeviceCaps(dc, VERTSIZE);
1351 #endif
1352 }
1353
1354 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
1355 {
1356 #if defined(__WIN16__) || defined(__WXMICROWIN__)
1357 *x = 0; *y = 0;
1358 wxDisplaySize(width, height);
1359 #else
1360 // Determine the desktop dimensions minus the taskbar and any other
1361 // special decorations...
1362 RECT r;
1363
1364 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
1365 if (x) *x = r.left;
1366 if (y) *y = r.top;
1367 if (width) *width = r.right - r.left;
1368 if (height) *height = r.bottom - r.top;
1369 #endif
1370 }
1371
1372
1373 // ---------------------------------------------------------------------------
1374 // window information functions
1375 // ---------------------------------------------------------------------------
1376
1377 wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
1378 {
1379 wxString str;
1380 int len = GetWindowTextLength((HWND)hWnd) + 1;
1381 GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len);
1382 str.UngetWriteBuf();
1383
1384 return str;
1385 }
1386
1387 wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
1388 {
1389 #ifdef __WXMICROWIN__
1390 // MICROWIN_TODO
1391 return wxEmptyString;
1392 #else
1393 wxString str;
1394
1395 int len = 256; // some starting value
1396
1397 for ( ;; )
1398 {
1399 // as we've #undefined GetClassName we must now manually choose the
1400 // right function to call
1401 int count =
1402
1403 #ifndef __WIN32__
1404 GetClassName
1405 #else // Win32
1406 #ifdef UNICODE
1407 GetClassNameW
1408 #else // !Unicode
1409 #ifdef __TWIN32__
1410 GetClassName
1411 #else // !Twin32
1412 GetClassNameA
1413 #endif // Twin32/!Twin32
1414 #endif // Unicode/ANSI
1415 #endif // Win16/32
1416 ((HWND)hWnd, str.GetWriteBuf(len), len);
1417
1418 str.UngetWriteBuf();
1419 if ( count == len )
1420 {
1421 // the class name might have been truncated, retry with larger
1422 // buffer
1423 len *= 2;
1424 }
1425 else
1426 {
1427 break;
1428 }
1429 }
1430
1431 return str;
1432 #endif
1433 }
1434
1435 WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
1436 {
1437 #ifndef __WIN32__
1438 return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID);
1439 #else // Win32
1440 return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID);
1441 #endif // Win16/32
1442 }
1443
1444 #endif // wxUSE_GUI
1445
1446 #if wxUSE_GUI
1447
1448 // ----------------------------------------------------------------------------
1449 // Metafile helpers
1450 // ----------------------------------------------------------------------------
1451
1452 extern void PixelToHIMETRIC(LONG *x, LONG *y)
1453 {
1454 ScreenHDC hdcRef;
1455
1456 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
1457 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
1458 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
1459 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
1460
1461 *x *= (iWidthMM * 100);
1462 *x /= iWidthPels;
1463 *y *= (iHeightMM * 100);
1464 *y /= iHeightPels;
1465 }
1466
1467 extern void HIMETRICToPixel(LONG *x, LONG *y)
1468 {
1469 ScreenHDC hdcRef;
1470
1471 int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
1472 iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
1473 iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
1474 iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
1475
1476 *x *= iWidthPels;
1477 *x /= (iWidthMM * 100);
1478 *y *= iHeightPels;
1479 *y /= (iHeightMM * 100);
1480 }
1481
1482 #endif // wxUSE_GUI
1483
1484 #if 0
1485 //------------------------------------------------------------------------
1486 // wild character routines
1487 //------------------------------------------------------------------------
1488
1489 bool wxIsWild( const wxString& pattern )
1490 {
1491 wxString tmp = pattern;
1492 char *pat = WXSTRINGCAST(tmp);
1493 while (*pat) {
1494 switch (*pat++) {
1495 case '?': case '*': case '[': case '{':
1496 return TRUE;
1497 case '\\':
1498 if (!*pat++)
1499 return FALSE;
1500 }
1501 }
1502 return FALSE;
1503 };
1504
1505
1506 bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
1507 {
1508 wxString tmp1 = pat;
1509 char *pattern = WXSTRINGCAST(tmp1);
1510 wxString tmp2 = text;
1511 char *str = WXSTRINGCAST(tmp2);
1512 char c;
1513 char *cp;
1514 bool done = FALSE, ret_code, ok;
1515 // Below is for vi fans
1516 const char OB = '{', CB = '}';
1517
1518 // dot_special means '.' only matches '.'
1519 if (dot_special && *str == '.' && *pattern != *str)
1520 return FALSE;
1521
1522 while ((*pattern != '\0') && (!done)
1523 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
1524 switch (*pattern) {
1525 case '\\':
1526 pattern++;
1527 if (*pattern != '\0')
1528 pattern++;
1529 break;
1530 case '*':
1531 pattern++;
1532 ret_code = FALSE;
1533 while ((*str!='\0')
1534 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
1535 /*loop*/;
1536 if (ret_code) {
1537 while (*str != '\0')
1538 str++;
1539 while (*pattern != '\0')
1540 pattern++;
1541 }
1542 break;
1543 case '[':
1544 pattern++;
1545 repeat:
1546 if ((*pattern == '\0') || (*pattern == ']')) {
1547 done = TRUE;
1548 break;
1549 }
1550 if (*pattern == '\\') {
1551 pattern++;
1552 if (*pattern == '\0') {
1553 done = TRUE;
1554 break;
1555 }
1556 }
1557 if (*(pattern + 1) == '-') {
1558 c = *pattern;
1559 pattern += 2;
1560 if (*pattern == ']') {
1561 done = TRUE;
1562 break;
1563 }
1564 if (*pattern == '\\') {
1565 pattern++;
1566 if (*pattern == '\0') {
1567 done = TRUE;
1568 break;
1569 }
1570 }
1571 if ((*str < c) || (*str > *pattern)) {
1572 pattern++;
1573 goto repeat;
1574 }
1575 } else if (*pattern != *str) {
1576 pattern++;
1577 goto repeat;
1578 }
1579 pattern++;
1580 while ((*pattern != ']') && (*pattern != '\0')) {
1581 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
1582 pattern++;
1583 pattern++;
1584 }
1585 if (*pattern != '\0') {
1586 pattern++, str++;
1587 }
1588 break;
1589 case '?':
1590 pattern++;
1591 str++;
1592 break;
1593 case OB:
1594 pattern++;
1595 while ((*pattern != CB) && (*pattern != '\0')) {
1596 cp = str;
1597 ok = TRUE;
1598 while (ok && (*cp != '\0') && (*pattern != '\0')
1599 && (*pattern != ',') && (*pattern != CB)) {
1600 if (*pattern == '\\')
1601 pattern++;
1602 ok = (*pattern++ == *cp++);
1603 }
1604 if (*pattern == '\0') {
1605 ok = FALSE;
1606 done = TRUE;
1607 break;
1608 } else if (ok) {
1609 str = cp;
1610 while ((*pattern != CB) && (*pattern != '\0')) {
1611 if (*++pattern == '\\') {
1612 if (*++pattern == CB)
1613 pattern++;
1614 }
1615 }
1616 } else {
1617 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
1618 if (*++pattern == '\\') {
1619 if (*++pattern == CB || *pattern == ',')
1620 pattern++;
1621 }
1622 }
1623 }
1624 if (*pattern != '\0')
1625 pattern++;
1626 }
1627 break;
1628 default:
1629 if (*str == *pattern) {
1630 str++, pattern++;
1631 } else {
1632 done = TRUE;
1633 }
1634 }
1635 }
1636 while (*pattern == '*')
1637 pattern++;
1638 return ((*str == '\0') && (*pattern == '\0'));
1639 };
1640
1641 #endif // 0
1642
1643 #if 0
1644
1645 // maximum mumber of lines the output console should have
1646 static const WORD MAX_CONSOLE_LINES = 500;
1647
1648 BOOL WINAPI MyConsoleHandler( DWORD dwCtrlType ) { // control signal type
1649 FreeConsole();
1650 return TRUE;
1651 }
1652
1653 void wxRedirectIOToConsole()
1654 {
1655 int hConHandle;
1656 long lStdHandle;
1657 CONSOLE_SCREEN_BUFFER_INFO coninfo;
1658 FILE *fp;
1659
1660 // allocate a console for this app
1661 AllocConsole();
1662
1663 // set the screen buffer to be big enough to let us scroll text
1664 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
1665 &coninfo);
1666 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
1667 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
1668 coninfo.dwSize);
1669
1670 // redirect unbuffered STDOUT to the console
1671 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
1672 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1673 if(hConHandle <= 0) return;
1674 fp = _fdopen( hConHandle, "w" );
1675 *stdout = *fp;
1676 setvbuf( stdout, NULL, _IONBF, 0 );
1677
1678 // redirect unbuffered STDIN to the console
1679 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
1680 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1681 if(hConHandle <= 0) return;
1682 fp = _fdopen( hConHandle, "r" );
1683 *stdin = *fp;
1684 setvbuf( stdin, NULL, _IONBF, 0 );
1685
1686 // redirect unbuffered STDERR to the console
1687 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
1688 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1689 if(hConHandle <= 0) return;
1690 fp = _fdopen( hConHandle, "w" );
1691 *stderr = *fp;
1692 setvbuf( stderr, NULL, _IONBF, 0 );
1693
1694 // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
1695 // point to console as well
1696 ios::sync_with_stdio();
1697
1698 SetConsoleCtrlHandler(MyConsoleHandler, TRUE);
1699 }
1700 #else
1701 // Not supported
1702 void wxRedirectIOToConsole()
1703 {
1704 }
1705 #endif
1706
1707 #ifdef __WXMICROWIN__
1708 int wxGetOsVersion(int *majorVsn, int *minorVsn)
1709 {
1710 // MICROWIN_TODO
1711 if (majorVsn) *majorVsn = 0;
1712 if (minorVsn) *minorVsn = 0;
1713 return wxUNIX;
1714 }
1715 #endif
1716