]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utils.cpp
Found bug that skrewed up display wrt horizontal
[wxWidgets.git] / src / msw / utils.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
8caa4ed1 2// Name: msw/utils.cpp
2bda0e17
KB
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
743e0a66 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b568d04f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
a3b46648 21// #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now.
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
a4f96412 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
a4f96412
VZ
32 #include "wx/utils.h"
33 #include "wx/app.h"
34 #include "wx/cursor.h"
743e0a66 35#endif //WX_PRECOMP
2bda0e17 36
b568d04f
VZ
37#include "wx/msw/private.h" // includes <windows.h>
38
2bda0e17 39#include "wx/timer.h"
57c208c5 40#include "wx/intl.h"
2bda0e17
KB
41
42#include <ctype.h>
43
5ea105e0 44#if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__)
a4f96412 45 #include <direct.h>
ce3ed50d 46
a4f96412
VZ
47 #ifndef __MWERKS__
48 #include <dos.h>
49 #endif
743e0a66 50#endif //GNUWIN32
2bda0e17 51
57c208c5 52#if defined(__GNUWIN32__) && !defined(__TWIN32__)
a4f96412
VZ
53 #include <sys/unistd.h>
54 #include <sys/stat.h>
743e0a66
VZ
55#endif //GNUWIN32
56
57#include "wx/log.h"
2bda0e17
KB
58
59#ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
60 // this (3.1 I believe) and how to test for it.
61 // If this works for Borland 4.0 as well, then no worries.
a4f96412 62 #include <dir.h>
2bda0e17
KB
63#endif
64
a4f96412
VZ
65// VZ: there is some code using NetXXX() functions to get the full user name:
66// I don't think it's a good idea because they don't work under Win95 and
67// seem to return the same as wxGetUserId() under NT. If you really want
68// to use them, just #define USE_NET_API
69#undef USE_NET_API
70
71#ifdef USE_NET_API
72 #include <lm.h>
73#endif // USE_NET_API
74
5ea105e0 75#if defined(__WIN32__) && !defined(__WXWINE__)
a4f96412 76 #include <io.h>
2bda0e17 77
a4f96412
VZ
78 #ifndef __GNUWIN32__
79 #include <shellapi.h>
80 #endif
2bda0e17
KB
81#endif
82
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#ifndef __WATCOMC__
3f4a0c5b
VZ
87 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
88 #include <errno.h>
89 #endif
2bda0e17
KB
90#endif
91#include <stdarg.h>
92
c030b70f 93//// BEGIN for console support: VC++ only
631f1bfe 94#ifdef __VISUALC__
c030b70f 95
3f4a0c5b 96#include "wx/msw/msvcrt.h"
c030b70f 97
3f4a0c5b 98#include <fcntl.h>
c030b70f 99
3f4a0c5b 100#include "wx/ioswrap.h"
c030b70f
JS
101
102#if wxUSE_IOSTREAMH
103// N.B. BC++ doesn't have istream.h, ostream.h
c030b70f
JS
104# include <io.h>
105# include <fstream.h>
c030b70f 106#else
c030b70f 107# include <fstream>
c030b70f
JS
108#endif
109
110/* Need to undef new if including crtdbg.h */
111# ifdef new
112# undef new
113# endif
114
1e6d9499 115#ifndef __WIN16__
c030b70f 116# include <crtdbg.h>
1e6d9499 117#endif
c030b70f
JS
118
119# if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
120# define new new(__FILE__,__LINE__)
121# endif
122
631f1bfe
JS
123#endif
124 // __VISUALC__
c030b70f
JS
125/// END for console support
126
b568d04f
VZ
127// ----------------------------------------------------------------------------
128// constants
129// ----------------------------------------------------------------------------
130
2bda0e17 131// In the WIN.INI file
223d09f6 132static const wxChar WX_SECTION[] = wxT("wxWindows");
b568d04f
VZ
133static const wxChar eUSERNAME[] = wxT("UserName");
134
135// these are only used under Win16
136#ifndef __WIN32__
223d09f6
KB
137static const wxChar eHOSTNAME[] = wxT("HostName");
138static const wxChar eUSERID[] = wxT("UserId");
b568d04f
VZ
139#endif // !Win32
140
141// ============================================================================
142// implementation
143// ============================================================================
2bda0e17 144
b568d04f
VZ
145// ----------------------------------------------------------------------------
146// get host name and related
147// ----------------------------------------------------------------------------
2bda0e17
KB
148
149// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
837e5743 150bool wxGetHostName(wxChar *buf, int maxSize)
2bda0e17 151{
57c208c5 152#if defined(__WIN32__) && !defined(__TWIN32__)
b568d04f
VZ
153 // TODO should use GetComputerNameEx() when available
154
155 DWORD nSize = maxSize;
156 if ( !::GetComputerName(buf, &nSize) )
157 {
158 wxLogLastError("GetComputerName");
159
160 return FALSE;
161 }
162
163 return TRUE;
2bda0e17 164#else
b568d04f
VZ
165 wxChar *sysname;
166 const wxChar *default_host = wxT("noname");
167
168 if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) {
169 GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
170 } else
171 wxStrncpy(buf, sysname, maxSize - 1);
172 buf[maxSize] = wxT('\0');
173 return *buf ? TRUE : FALSE;
2bda0e17
KB
174#endif
175}
176
b568d04f
VZ
177bool wxGetFullHostName(wxChar *buf, int maxSize)
178{
179 return wxGetHostName(buf, maxSize);
180}
181
2bda0e17 182// Get user ID e.g. jacs
837e5743 183bool wxGetUserId(wxChar *buf, int maxSize)
2bda0e17 184{
57c208c5 185#if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__)
0a144271
VZ
186 DWORD nSize = maxSize;
187 if ( ::GetUserName(buf, &nSize) == 0 )
188 {
a4f96412 189 // actually, it does happen on Win9x if the user didn't log on
223d09f6 190 DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize);
a4f96412
VZ
191 if ( res == 0 )
192 {
193 // not found
194 return FALSE;
195 }
0a144271
VZ
196 }
197
198 return TRUE;
0a144271 199#else // Win16 or Win32s
b568d04f
VZ
200 wxChar *user;
201 const wxChar *default_id = wxT("anonymous");
2bda0e17 202
b568d04f
VZ
203 // Can't assume we have NIS (PC-NFS) or some other ID daemon
204 // So we ...
205 if ( (user = wxGetenv(wxT("USER"))) == NULL &&
206 (user = wxGetenv(wxT("LOGNAME"))) == NULL )
207 {
208 // Use wxWindows configuration data (comming soon)
209 GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1);
210 }
211 else
212 {
213 wxStrncpy(buf, user, maxSize - 1);
214 }
a4f96412 215
b568d04f 216 return *buf ? TRUE : FALSE;
2bda0e17
KB
217#endif
218}
219
220// Get user name e.g. Julian Smart
837e5743 221bool wxGetUserName(wxChar *buf, int maxSize)
2bda0e17 222{
0a144271 223#if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
b568d04f
VZ
224 extern HANDLE g_hPenWin; // PenWindows Running?
225 if (g_hPenWin)
226 {
227 // PenWindows Does have a user concept!
228 // Get the current owner of the recognizer
229 GetPrivateProfileString("Current", "User", default_name, wxBuffer, maxSize - 1, "PENWIN.INI");
230 strncpy(buf, wxBuffer, maxSize - 1);
231 }
232 else
2bda0e17 233#endif
b568d04f 234 {
a4f96412 235#ifdef USE_NET_API
b568d04f
VZ
236 CHAR szUserName[256];
237 if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
238 return FALSE;
a4f96412 239
b568d04f
VZ
240 // TODO how to get the domain name?
241 CHAR *szDomain = "";
a4f96412 242
b568d04f
VZ
243 // the code is based on the MSDN example (also see KB article Q119670)
244 WCHAR wszUserName[256]; // Unicode user name
245 WCHAR wszDomain[256];
246 LPBYTE ComputerName;
a4f96412 247
b568d04f 248 USER_INFO_2 *ui2; // User structure
a4f96412 249
b568d04f
VZ
250 // Convert ANSI user name and domain to Unicode
251 MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1,
252 wszUserName, WXSIZEOF(wszUserName) );
253 MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1,
254 wszDomain, WXSIZEOF(wszDomain) );
a4f96412 255
b568d04f
VZ
256 // Get the computer name of a DC for the domain.
257 if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success )
258 {
259 wxLogError(wxT("Can not find domain controller"));
a4f96412 260
b568d04f
VZ
261 goto error;
262 }
a4f96412 263
b568d04f
VZ
264 // Look up the user on the DC
265 NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName,
266 (LPWSTR)&wszUserName,
267 2, // level - we want USER_INFO_2
268 (LPBYTE *) &ui2 );
269 switch ( status )
270 {
271 case NERR_Success:
272 // ok
273 break;
a4f96412 274
b568d04f
VZ
275 case NERR_InvalidComputer:
276 wxLogError(wxT("Invalid domain controller name."));
a4f96412 277
b568d04f 278 goto error;
a4f96412 279
b568d04f
VZ
280 case NERR_UserNotFound:
281 wxLogError(wxT("Invalid user name '%s'."), szUserName);
a4f96412 282
b568d04f 283 goto error;
a4f96412 284
b568d04f
VZ
285 default:
286 wxLogSysError(wxT("Can't get information about user"));
a4f96412 287
b568d04f
VZ
288 goto error;
289 }
a4f96412 290
b568d04f
VZ
291 // Convert the Unicode full name to ANSI
292 WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
293 buf, maxSize, NULL, NULL );
a4f96412 294
b568d04f 295 return TRUE;
a4f96412
VZ
296
297error:
b568d04f 298 wxLogError(wxT("Couldn't look up full user name."));
a4f96412 299
b568d04f 300 return FALSE;
a4f96412 301#else // !USE_NET_API
b568d04f
VZ
302 // Could use NIS, MS-Mail or other site specific programs
303 // Use wxWindows configuration data
304 bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxT(""), buf, maxSize - 1) != 0;
305 if ( !ok )
306 {
307 ok = wxGetUserId(buf, maxSize);
308 }
0a144271 309
b568d04f
VZ
310 if ( !ok )
311 {
312 wxStrncpy(buf, wxT("Unknown User"), maxSize);
313 }
a4f96412 314#endif // Win32/16
b568d04f 315 }
0a144271 316
b568d04f 317 return TRUE;
2bda0e17
KB
318}
319
b568d04f 320const wxChar* wxGetHomeDir(wxString *pstr)
2bda0e17 321{
b568d04f 322 wxString& strDir = *pstr;
2bda0e17 323
b568d04f
VZ
324 #if defined(__UNIX__) && !defined(__TWIN32__)
325 const wxChar *szHome = wxGetenv("HOME");
326 if ( szHome == NULL ) {
327 // we're homeless...
328 wxLogWarning(_("can't find user's HOME, using current directory."));
329 strDir = wxT(".");
330 }
331 else
332 strDir = szHome;
2bda0e17 333
b568d04f
VZ
334 // add a trailing slash if needed
335 if ( strDir.Last() != wxT('/') )
336 strDir << wxT('/');
337 #else // Windows
338 #ifdef __WIN32__
339 const wxChar *szHome = wxGetenv(wxT("HOMEDRIVE"));
340 if ( szHome != NULL )
341 strDir << szHome;
342 szHome = wxGetenv(wxT("HOMEPATH"));
343 if ( szHome != NULL ) {
344 strDir << szHome;
2bda0e17 345
b568d04f
VZ
346 // the idea is that under NT these variables have default values
347 // of "%systemdrive%:" and "\\". As we don't want to create our
348 // config files in the root directory of the system drive, we will
349 // create it in our program's dir. However, if the user took care
350 // to set HOMEPATH to something other than "\\", we suppose that he
351 // knows what he is doing and use the supplied value.
352 if ( wxStrcmp(szHome, wxT("\\")) != 0 )
353 return strDir.c_str();
354 }
2bda0e17 355
b568d04f
VZ
356 #else // Win16
357 // Win16 has no idea about home, so use the working directory instead
358 #endif // WIN16/32
2bda0e17 359
b568d04f
VZ
360 // 260 was taken from windef.h
361 #ifndef MAX_PATH
362 #define MAX_PATH 260
363 #endif
2bda0e17 364
b568d04f
VZ
365 wxString strPath;
366 ::GetModuleFileName(::GetModuleHandle(NULL),
367 strPath.GetWriteBuf(MAX_PATH), MAX_PATH);
368 strPath.UngetWriteBuf();
2bda0e17 369
b568d04f
VZ
370 // extract the dir name
371 wxSplitPath(strPath, &strDir, NULL, NULL);
58a33cb4 372
b568d04f
VZ
373 #endif // UNIX/Win
374
375 return strDir.c_str();
afb74891
VZ
376}
377
b568d04f 378wxChar *wxGetUserHome(const wxString& user)
2bda0e17 379{
b568d04f
VZ
380 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
381 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
382 // static buffer and sometimes I don't even know what.
383 static wxString s_home;
2bda0e17 384
b568d04f 385 return (wxChar *)wxGetHomeDir(&s_home);
2bda0e17
KB
386}
387
b568d04f 388bool wxDirExists(const wxString& dir)
2bda0e17 389{
b568d04f
VZ
390#if defined(__WIN32__)
391 WIN32_FIND_DATA fileInfo;
392#else // Win16
393 #ifdef __BORLANDC__
394 struct ffblk fileInfo;
395 #else
396 struct find_t fileInfo;
397 #endif
398#endif // Win32/16
2bda0e17 399
b568d04f
VZ
400#if defined(__WIN32__)
401 HANDLE h = ::FindFirstFile(dir, &fileInfo);
2bda0e17 402
b568d04f
VZ
403 if ( h == INVALID_HANDLE_VALUE )
404 {
405 wxLogLastError("FindFirstFile");
2bda0e17 406
b568d04f
VZ
407 return FALSE;
408 }
2bda0e17 409
b568d04f
VZ
410 ::FindClose(h);
411
412 return (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
413#else // Win16
414 // In Borland findfirst has a different argument
415 // ordering from _dos_findfirst. But _dos_findfirst
416 // _should_ be ok in both MS and Borland... why not?
417 #ifdef __BORLANDC__
418 return (findfirst(dir, &fileInfo, _A_SUBDIR) == 0 &&
419 (fileInfo.ff_attrib & _A_SUBDIR) != 0);
420 #else
421 return (_dos_findfirst(dir, _A_SUBDIR, &fileInfo) == 0) &&
422 ((fileInfo.attrib & _A_SUBDIR) != 0);
423 #endif
424#endif // Win32/16
425}
2bda0e17 426
b568d04f
VZ
427// ----------------------------------------------------------------------------
428// process management
429// ----------------------------------------------------------------------------
430
431int wxKill(long pid, int sig)
432{
433 // TODO use SendMessage(WM_QUIT) and TerminateProcess() if needed
434
435 return 0;
2bda0e17
KB
436}
437
b568d04f
VZ
438// Execute a program in an Interactive Shell
439bool wxShell(const wxString& command)
2bda0e17 440{
b568d04f
VZ
441 wxChar *shell = wxGetenv(wxT("COMSPEC"));
442 if ( !shell )
443 shell = wxT("\\COMMAND.COM");
444
445 wxString cmd;
446 if ( !command )
447 {
448 // just the shell
449 cmd = shell;
450 }
451 else
452 {
453 // pass the command to execute to the command processor
454 cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
455 }
456
0d7ea902 457 return wxExecute(cmd, TRUE /* sync */) != 0;
2bda0e17
KB
458}
459
b568d04f
VZ
460// ----------------------------------------------------------------------------
461// misc
462// ----------------------------------------------------------------------------
463
464// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
465long wxGetFreeMemory()
2bda0e17 466{
b568d04f
VZ
467#if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__)
468 MEMORYSTATUS memStatus;
469 memStatus.dwLength = sizeof(MEMORYSTATUS);
470 GlobalMemoryStatus(&memStatus);
471 return memStatus.dwAvailPhys;
472#else
473 return (long)GetFreeSpace(0);
474#endif
2bda0e17
KB
475}
476
477// Emit a beeeeeep
634903fd 478void wxBell()
2bda0e17 479{
b568d04f 480 ::MessageBeep((UINT)-1); // default sound
2bda0e17
KB
481}
482
bdc72a22 483wxString wxGetOsDescription()
2bda0e17 484{
bdc72a22
VZ
485#ifdef __WIN32__
486 wxString str;
487
488 OSVERSIONINFO info;
489 wxZeroMemory(info);
490
491 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
492 if ( ::GetVersionEx(&info) )
493 {
494 switch ( info.dwPlatformId )
495 {
496 case VER_PLATFORM_WIN32s:
497 str = _("Win32s on Windows 3.1");
498 break;
499
500 case VER_PLATFORM_WIN32_WINDOWS:
501 str.Printf(_("Windows 9%c"),
502 info.dwMinorVersion == 0 ? _T('5') : _T('9'));
503 if ( !wxIsEmpty(info.szCSDVersion) )
504 {
505 str << _T(" (") << info.szCSDVersion << _T(')');
506 }
507 break;
508
509 case VER_PLATFORM_WIN32_NT:
510 str.Printf(_T("Windows NT %lu.%lu (build %lu"),
511 info.dwMajorVersion,
512 info.dwMinorVersion,
513 info.dwBuildNumber);
514 if ( !wxIsEmpty(info.szCSDVersion) )
515 {
516 str << _T(", ") << info.szCSDVersion;
517 }
518 str << _T(')');
519 break;
520 }
521 }
522 else
523 {
524 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
525 }
526
527 return str;
528#else // Win16
529 return _("Windows 3.1");
530#endif // Win32/16
531}
6f65e337 532
bdc72a22
VZ
533int wxGetOsVersion(int *majorVsn, int *minorVsn)
534{
2432b92d 535#if defined(__WIN32__) && !defined(__SC__)
bdc72a22
VZ
536 OSVERSIONINFO info;
537 wxZeroMemory(info);
538
539 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
540 if ( ::GetVersionEx(&info) )
541 {
542 if (majorVsn)
543 *majorVsn = info.dwMajorVersion;
544 if (minorVsn)
545 *minorVsn = info.dwMinorVersion;
546
547 switch ( info.dwPlatformId )
548 {
549 case VER_PLATFORM_WIN32s:
550 return wxWIN32S;
551
552 case VER_PLATFORM_WIN32_WINDOWS:
553 return wxWIN95;
554
555 case VER_PLATFORM_WIN32_NT:
556 return wxWINDOWS_NT;
557 }
558 }
559
560 return wxWINDOWS; // error if we get here, return generic value
561#else // Win16
562 int retValue = wxWINDOWS;
563 #ifdef __WINDOWS_386__
564 retValue = wxWIN386;
565 #else
566 #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
567 extern HANDLE g_hPenWin;
568 retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS;
569 #endif
570 #endif
571
572 if (majorVsn)
573 *majorVsn = 3;
574 if (minorVsn)
575 *minorVsn = 1;
576
577 return retValue;
6f65e337 578#endif
2bda0e17
KB
579}
580
b568d04f
VZ
581// ----------------------------------------------------------------------------
582// sleep functions
583// ----------------------------------------------------------------------------
584
585#if wxUSE_GUI
586
587// Sleep for nSecs seconds. Attempt a Windows implementation using timers.
588static bool gs_inTimer = FALSE;
589
590class wxSleepTimer: public wxTimer
591{
592public:
593 virtual void Notify()
594 {
595 gs_inTimer = FALSE;
596 Stop();
597 }
598};
599
600static wxTimer *wxTheSleepTimer = NULL;
601
602void wxUsleep(unsigned long milliseconds)
603{
604#ifdef __WIN32__
605 ::Sleep(milliseconds);
606#else
607 if (gs_inTimer)
608 return;
609
610 wxTheSleepTimer = new wxSleepTimer;
611 gs_inTimer = TRUE;
612 wxTheSleepTimer->Start(milliseconds);
613 while (gs_inTimer)
614 {
615 if (wxTheApp->Pending())
616 wxTheApp->Dispatch();
617 }
618 delete wxTheSleepTimer;
619 wxTheSleepTimer = NULL;
620#endif
621}
622
623void wxSleep(int nSecs)
624{
625 if (gs_inTimer)
626 return;
627
628 wxTheSleepTimer = new wxSleepTimer;
629 gs_inTimer = TRUE;
630 wxTheSleepTimer->Start(nSecs*1000);
631 while (gs_inTimer)
632 {
633 if (wxTheApp->Pending())
634 wxTheApp->Dispatch();
635 }
636 delete wxTheSleepTimer;
637 wxTheSleepTimer = NULL;
638}
639
640// Consume all events until no more left
641void wxFlushEvents()
642{
643// wxYield();
644}
645
646#elif defined(__WIN32__) // wxUSE_GUI
647
648void wxUsleep(unsigned long milliseconds)
649{
650 ::Sleep(milliseconds);
651}
652
653void wxSleep(int nSecs)
654{
655 wxUsleep(1000*nSecs);
656}
657
658#endif // wxUSE_GUI/!wxUSE_GUI
659
660// ----------------------------------------------------------------------------
661// deprecated (in favour of wxLog) log functions
662// ----------------------------------------------------------------------------
663
664#if wxUSE_GUI
665
666// Output a debug mess., in a system dependent fashion.
667void wxDebugMsg(const wxChar *fmt ...)
668{
669 va_list ap;
670 static wxChar buffer[512];
671
672 if (!wxTheApp->GetWantDebugOutput())
673 return ;
674
675 va_start(ap, fmt);
676
677 wvsprintf(buffer,fmt,ap) ;
678 OutputDebugString((LPCTSTR)buffer) ;
679
680 va_end(ap);
681}
682
683// Non-fatal error: pop up message box and (possibly) continue
684void wxError(const wxString& msg, const wxString& title)
685{
686 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
687 if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title,
688 MB_ICONSTOP | MB_YESNO) == IDNO)
689 wxExit();
690}
691
692// Fatal error: pop up message box and abort
693void wxFatalError(const wxString& msg, const wxString& title)
694{
695 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
696 FatalAppExit(0, (LPCTSTR)wxBuffer);
697}
698
699// ----------------------------------------------------------------------------
700// functions to work with .INI files
701// ----------------------------------------------------------------------------
702
2bda0e17 703// Reading and writing resources (eg WIN.INI, .Xdefaults)
47d67540 704#if wxUSE_RESOURCES
2bda0e17
KB
705bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
706{
223d09f6 707 if (file != wxT(""))
8fb3a512
JS
708// Don't know what the correct cast should be, but it doesn't
709// compile in BC++/16-bit without this cast.
710#if !defined(__WIN32__)
711 return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0);
712#else
837e5743 713 return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0);
8fb3a512 714#endif
2bda0e17 715 else
837e5743 716 return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0);
2bda0e17
KB
717}
718
719bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
720{
b568d04f
VZ
721 wxString buf;
722 buf.Printf(wxT("%.4f"), value);
723
724 return wxWriteResource(section, entry, buf, file);
2bda0e17
KB
725}
726
727bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
728{
b568d04f
VZ
729 wxString buf;
730 buf.Printf(wxT("%ld"), value);
731
732 return wxWriteResource(section, entry, buf, file);
2bda0e17
KB
733}
734
735bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
736{
b568d04f
VZ
737 wxString buf;
738 buf.Printf(wxT("%d"), value);
739
740 return wxWriteResource(section, entry, buf, file);
2bda0e17
KB
741}
742
837e5743 743bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file)
2bda0e17 744{
b568d04f
VZ
745 static const wxChar defunkt[] = wxT("$$default");
746 if (file != wxT(""))
747 {
748 int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt,
749 (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file);
750 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
751 return FALSE;
752 }
753 else
754 {
755 int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt,
756 (LPTSTR)wxBuffer, 1000);
757 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
758 return FALSE;
2bda0e17 759 }
b568d04f
VZ
760 if (*value) delete[] (*value);
761 *value = copystring(wxBuffer);
762 return TRUE;
763}
2bda0e17
KB
764
765bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
766{
b568d04f
VZ
767 wxChar *s = NULL;
768 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
769 if (succ)
770 {
771 *value = (float)wxStrtod(s, NULL);
772 delete[] s;
773 return TRUE;
774 }
775 else return FALSE;
2bda0e17
KB
776}
777
778bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
779{
b568d04f
VZ
780 wxChar *s = NULL;
781 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
782 if (succ)
783 {
784 *value = wxStrtol(s, NULL, 10);
785 delete[] s;
786 return TRUE;
787 }
788 else return FALSE;
2bda0e17
KB
789}
790
791bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
792{
b568d04f
VZ
793 wxChar *s = NULL;
794 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
795 if (succ)
796 {
797 *value = (int)wxStrtol(s, NULL, 10);
798 delete[] s;
799 return TRUE;
800 }
801 else return FALSE;
2bda0e17 802}
47d67540 803#endif // wxUSE_RESOURCES
2bda0e17 804
634903fd 805// ---------------------------------------------------------------------------
25889d3c 806// helper functions for showing a "busy" cursor
634903fd
VZ
807// ---------------------------------------------------------------------------
808
bfbd6dc1
VZ
809static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
810static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
634903fd 811static int gs_wxBusyCursorCount = 0;
2bda0e17 812
bfbd6dc1
VZ
813extern HCURSOR wxGetCurrentBusyCursor()
814{
815 return gs_wxBusyCursor;
816}
817
2bda0e17
KB
818// Set the cursor to the busy cursor for all windows
819void wxBeginBusyCursor(wxCursor *cursor)
820{
634903fd
VZ
821 if ( gs_wxBusyCursorCount++ == 0 )
822 {
823 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
824 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
825 }
826 //else: nothing to do, already set
2bda0e17
KB
827}
828
829// Restore cursor to normal
634903fd 830void wxEndBusyCursor()
2bda0e17 831{
634903fd 832 wxCHECK_RET( gs_wxBusyCursorCount > 0,
223d09f6 833 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
b07135ba 834
634903fd
VZ
835 if ( --gs_wxBusyCursorCount == 0 )
836 {
837 ::SetCursor(gs_wxBusyCursorOld);
838
839 gs_wxBusyCursorOld = 0;
840 }
2bda0e17
KB
841}
842
843// TRUE if we're between the above two calls
634903fd 844bool wxIsBusy()
2bda0e17 845{
634903fd 846 return (gs_wxBusyCursorCount > 0);
b07135ba 847}
2bda0e17 848
2bda0e17
KB
849// Check whether this window wants to process messages, e.g. Stop button
850// in long calculations.
851bool wxCheckForInterrupt(wxWindow *wnd)
852{
b568d04f
VZ
853 wxCHECK( wnd, FALSE );
854
743e0a66 855 MSG msg;
b568d04f
VZ
856 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
857 {
858 ::TranslateMessage(&msg);
859 ::DispatchMessage(&msg);
743e0a66 860 }
634903fd 861
b568d04f 862 return TRUE;
2bda0e17
KB
863}
864
b568d04f
VZ
865#endif // wxUSE_GUI
866
2bda0e17
KB
867// MSW only: get user-defined resource from the .res file.
868// Returns NULL or newly-allocated memory, so use delete[] to clean up.
869
837e5743 870wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
2bda0e17 871{
b568d04f
VZ
872 HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType);
873 if ( hResource == 0 )
874 return NULL;
875
876 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
877 if ( hData == 0 )
878 return NULL;
2bda0e17 879
b568d04f
VZ
880 wxChar *theText = (wxChar *)::LockResource(hData);
881 if ( !theText )
882 return NULL;
b07135ba 883
8caa4ed1
JS
884 // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
885 // so we need to find the length of the resource.
886 int len = ::SizeofResource(wxGetInstance(), hResource);
887 wxChar *s = new wxChar[len+1];
888 wxStrncpy(s,theText,len);
889 s[len]=0;
890
891 // wxChar *s = copystring(theText);
2bda0e17 892
b568d04f 893 // Obsolete in WIN32
2bda0e17 894#ifndef __WIN32__
b568d04f 895 UnlockResource(hData);
2bda0e17
KB
896#endif
897
b568d04f
VZ
898 // No need??
899 // GlobalFree(hData);
2bda0e17 900
b568d04f 901 return s;
2bda0e17 902}
b568d04f
VZ
903
904// ----------------------------------------------------------------------------
905// get display info
906// ----------------------------------------------------------------------------
2bda0e17
KB
907
908void wxGetMousePosition( int* x, int* y )
909{
b568d04f
VZ
910 POINT pt;
911 GetCursorPos( & pt );
912 if ( x ) *x = pt.x;
913 if ( y ) *y = pt.y;
2bda0e17
KB
914};
915
916// Return TRUE if we have a colour display
634903fd 917bool wxColourDisplay()
2bda0e17 918{
b568d04f
VZ
919 ScreenHDC dc;
920 int noCols = GetDeviceCaps(dc, NUMCOLORS);
921
922 return (noCols == -1) || (noCols > 2);
2bda0e17
KB
923}
924
925// Returns depth of screen
634903fd 926int wxDisplayDepth()
2bda0e17 927{
b568d04f
VZ
928 ScreenHDC dc;
929 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
2bda0e17
KB
930}
931
932// Get size of display
933void wxDisplaySize(int *width, int *height)
934{
b568d04f 935 ScreenHDC dc;
3f4a0c5b 936
b568d04f
VZ
937 if ( width ) *width = GetDeviceCaps(dc, HORZRES);
938 if ( height ) *height = GetDeviceCaps(dc, VERTRES);
7f555861
JS
939}
940
cc2b7472
VZ
941// ---------------------------------------------------------------------------
942// window information functions
943// ---------------------------------------------------------------------------
944
1c4a764c
VZ
945wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
946{
947 wxString str;
948 int len = GetWindowTextLength((HWND)hWnd) + 1;
949 GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len);
950 str.UngetWriteBuf();
951
952 return str;
953}
954
cc2b7472
VZ
955wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
956{
957 wxString str;
958
959 int len = 256; // some starting value
960
961 for ( ;; )
962 {
963 // as we've #undefined GetClassName we must now manually choose the
964 // right function to call
965 int count =
966
967 #ifndef __WIN32__
968 GetClassName
969 #else // Win32
970 #ifdef UNICODE
971 GetClassNameW
972 #else // !Unicode
973 #ifdef __TWIN32__
974 GetClassName
975 #else // !Twin32
976 GetClassNameA
977 #endif // Twin32/!Twin32
978 #endif // Unicode/ANSI
979 #endif // Win16/32
980 ((HWND)hWnd, str.GetWriteBuf(len), len);
981
982 str.UngetWriteBuf();
983 if ( count == len )
984 {
985 // the class name might have been truncated, retry with larger
986 // buffer
987 len *= 2;
988 }
989 else
990 {
991 break;
992 }
993 }
994
995 return str;
996}
997
42e69d6b 998WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
cc2b7472
VZ
999{
1000#ifndef __WIN32__
42e69d6b 1001 return GetWindowWord((HWND)hWnd, GWW_ID);
cc2b7472 1002#else // Win32
42e69d6b 1003 return GetWindowLong((HWND)hWnd, GWL_ID);
cc2b7472
VZ
1004#endif // Win16/32
1005}
1006
7f555861
JS
1007#if 0
1008//------------------------------------------------------------------------
1009// wild character routines
1010//------------------------------------------------------------------------
1011
1012bool wxIsWild( const wxString& pattern )
1013{
1014 wxString tmp = pattern;
1015 char *pat = WXSTRINGCAST(tmp);
1016 while (*pat) {
3f4a0c5b
VZ
1017 switch (*pat++) {
1018 case '?': case '*': case '[': case '{':
1019 return TRUE;
1020 case '\\':
1021 if (!*pat++)
1022 return FALSE;
1023 }
7f555861
JS
1024 }
1025 return FALSE;
1026};
1027
1028
1029bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
1030{
1031 wxString tmp1 = pat;
1032 char *pattern = WXSTRINGCAST(tmp1);
1033 wxString tmp2 = text;
1034 char *str = WXSTRINGCAST(tmp2);
1035 char c;
1036 char *cp;
1037 bool done = FALSE, ret_code, ok;
1038 // Below is for vi fans
1039 const char OB = '{', CB = '}';
1040
1041 // dot_special means '.' only matches '.'
1042 if (dot_special && *str == '.' && *pattern != *str)
3f4a0c5b 1043 return FALSE;
7f555861
JS
1044
1045 while ((*pattern != '\0') && (!done)
1046 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
3f4a0c5b
VZ
1047 switch (*pattern) {
1048 case '\\':
1049 pattern++;
1050 if (*pattern != '\0')
1051 pattern++;
1052 break;
1053 case '*':
1054 pattern++;
1055 ret_code = FALSE;
1056 while ((*str!='\0')
1057 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
1058 /*loop*/;
1059 if (ret_code) {
1060 while (*str != '\0')
1061 str++;
1062 while (*pattern != '\0')
1063 pattern++;
1064 }
1065 break;
1066 case '[':
1067 pattern++;
1068 repeat:
1069 if ((*pattern == '\0') || (*pattern == ']')) {
1070 done = TRUE;
1071 break;
1072 }
1073 if (*pattern == '\\') {
1074 pattern++;
1075 if (*pattern == '\0') {
1076 done = TRUE;
1077 break;
1078 }
1079 }
1080 if (*(pattern + 1) == '-') {
1081 c = *pattern;
1082 pattern += 2;
1083 if (*pattern == ']') {
1084 done = TRUE;
1085 break;
1086 }
1087 if (*pattern == '\\') {
1088 pattern++;
1089 if (*pattern == '\0') {
1090 done = TRUE;
1091 break;
1092 }
1093 }
1094 if ((*str < c) || (*str > *pattern)) {
1095 pattern++;
1096 goto repeat;
1097 }
1098 } else if (*pattern != *str) {
1099 pattern++;
1100 goto repeat;
1101 }
1102 pattern++;
1103 while ((*pattern != ']') && (*pattern != '\0')) {
1104 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
1105 pattern++;
1106 pattern++;
1107 }
1108 if (*pattern != '\0') {
1109 pattern++, str++;
1110 }
1111 break;
1112 case '?':
1113 pattern++;
1114 str++;
1115 break;
1116 case OB:
1117 pattern++;
1118 while ((*pattern != CB) && (*pattern != '\0')) {
1119 cp = str;
1120 ok = TRUE;
1121 while (ok && (*cp != '\0') && (*pattern != '\0')
1122 && (*pattern != ',') && (*pattern != CB)) {
1123 if (*pattern == '\\')
1124 pattern++;
1125 ok = (*pattern++ == *cp++);
1126 }
1127 if (*pattern == '\0') {
1128 ok = FALSE;
1129 done = TRUE;
1130 break;
1131 } else if (ok) {
1132 str = cp;
1133 while ((*pattern != CB) && (*pattern != '\0')) {
1134 if (*++pattern == '\\') {
1135 if (*++pattern == CB)
1136 pattern++;
1137 }
1138 }
1139 } else {
1140 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
1141 if (*++pattern == '\\') {
7f555861 1142 if (*++pattern == CB || *pattern == ',')
3f4a0c5b
VZ
1143 pattern++;
1144 }
1145 }
1146 }
1147 if (*pattern != '\0')
1148 pattern++;
1149 }
1150 break;
1151 default:
1152 if (*str == *pattern) {
1153 str++, pattern++;
1154 } else {
1155 done = TRUE;
1156 }
1157 }
7f555861
JS
1158 }
1159 while (*pattern == '*')
3f4a0c5b 1160 pattern++;
7f555861
JS
1161 return ((*str == '\0') && (*pattern == '\0'));
1162};
1163
1164#endif
1165
cf45d1f4 1166#if 0
c030b70f
JS
1167
1168// maximum mumber of lines the output console should have
1169static const WORD MAX_CONSOLE_LINES = 500;
1170
1171BOOL WINAPI MyConsoleHandler( DWORD dwCtrlType ) { // control signal type
3f4a0c5b
VZ
1172 FreeConsole();
1173 return TRUE;
c030b70f
JS
1174}
1175
1176void wxRedirectIOToConsole()
1177{
1178 int hConHandle;
1179 long lStdHandle;
1180 CONSOLE_SCREEN_BUFFER_INFO coninfo;
1181 FILE *fp;
1182
1183 // allocate a console for this app
1184 AllocConsole();
1185
1186 // set the screen buffer to be big enough to let us scroll text
3f4a0c5b 1187 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
c030b70f
JS
1188 &coninfo);
1189 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
3f4a0c5b 1190 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
c030b70f
JS
1191 coninfo.dwSize);
1192
1193 // redirect unbuffered STDOUT to the console
1194 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
1195 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1196 if(hConHandle <= 0) return;
1197 fp = _fdopen( hConHandle, "w" );
1198 *stdout = *fp;
1199 setvbuf( stdout, NULL, _IONBF, 0 );
1200
1201 // redirect unbuffered STDIN to the console
1202 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
1203 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1204 if(hConHandle <= 0) return;
1205 fp = _fdopen( hConHandle, "r" );
1206 *stdin = *fp;
1207 setvbuf( stdin, NULL, _IONBF, 0 );
1208
1209 // redirect unbuffered STDERR to the console
1210 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
1211 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1212 if(hConHandle <= 0) return;
1213 fp = _fdopen( hConHandle, "w" );
1214 *stderr = *fp;
1215 setvbuf( stderr, NULL, _IONBF, 0 );
3f4a0c5b
VZ
1216
1217 // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
c030b70f
JS
1218 // point to console as well
1219 ios::sync_with_stdio();
1220
3f4a0c5b 1221 SetConsoleCtrlHandler(MyConsoleHandler, TRUE);
c030b70f
JS
1222}
1223#else
1224// Not supported
1225void wxRedirectIOToConsole()
1226{
1227}
1228#endif
1229
1230