]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utils.cpp
Now allow SetMenuBar to be called several times.
[wxWidgets.git] / src / msw / utils.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
b568d04f 2// Name: mse/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
457 return wxExecute(cmd, FALSE) != 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
6f65e337
JS
483// Chris Breeze 27/5/98: revised WIN32 code to
484// detect WindowsNT correctly
2bda0e17
KB
485int wxGetOsVersion(int *majorVsn, int *minorVsn)
486{
6f65e337
JS
487 if (majorVsn) *majorVsn = 0;
488 if (minorVsn) *minorVsn = 0;
489
2432b92d 490#if defined(__WIN32__) && !defined(__SC__)
6f65e337
JS
491 OSVERSIONINFO info;
492 memset(&info, 0, sizeof(OSVERSIONINFO));
493 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
494 if (GetVersionEx(&info))
495 {
496 if (majorVsn) *majorVsn = info.dwMajorVersion;
497 if (minorVsn) *minorVsn = info.dwMinorVersion;
498 switch (info.dwPlatformId)
743e0a66
VZ
499 {
500 case VER_PLATFORM_WIN32s:
501 return wxWIN32S;
502 break;
503 case VER_PLATFORM_WIN32_WINDOWS:
504 return wxWIN95;
505 break;
506 case VER_PLATFORM_WIN32_NT:
507 return wxWINDOWS_NT;
508 break;
6f65e337 509 }
743e0a66
VZ
510 }
511 return wxWINDOWS; // error if we get here, return generic value
6f65e337
JS
512#else
513 // Win16 code...
2432b92d 514 int retValue = 0;
6f65e337 515# ifdef __WINDOWS_386__
2bda0e17 516 retValue = wxWIN386;
6f65e337 517# else
47d67540 518# if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
81d66cf3
JS
519 extern HANDLE g_hPenWin;
520 retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS ;
6f65e337
JS
521# endif
522# endif
2bda0e17
KB
523 // @@@@ To be completed. I don't have the manual here...
524 if (majorVsn) *majorVsn = 3 ;
525 if (minorVsn) *minorVsn = 1 ;
526 return retValue ;
6f65e337 527#endif
2bda0e17
KB
528}
529
b568d04f
VZ
530// ----------------------------------------------------------------------------
531// sleep functions
532// ----------------------------------------------------------------------------
533
534#if wxUSE_GUI
535
536// Sleep for nSecs seconds. Attempt a Windows implementation using timers.
537static bool gs_inTimer = FALSE;
538
539class wxSleepTimer: public wxTimer
540{
541public:
542 virtual void Notify()
543 {
544 gs_inTimer = FALSE;
545 Stop();
546 }
547};
548
549static wxTimer *wxTheSleepTimer = NULL;
550
551void wxUsleep(unsigned long milliseconds)
552{
553#ifdef __WIN32__
554 ::Sleep(milliseconds);
555#else
556 if (gs_inTimer)
557 return;
558
559 wxTheSleepTimer = new wxSleepTimer;
560 gs_inTimer = TRUE;
561 wxTheSleepTimer->Start(milliseconds);
562 while (gs_inTimer)
563 {
564 if (wxTheApp->Pending())
565 wxTheApp->Dispatch();
566 }
567 delete wxTheSleepTimer;
568 wxTheSleepTimer = NULL;
569#endif
570}
571
572void wxSleep(int nSecs)
573{
574 if (gs_inTimer)
575 return;
576
577 wxTheSleepTimer = new wxSleepTimer;
578 gs_inTimer = TRUE;
579 wxTheSleepTimer->Start(nSecs*1000);
580 while (gs_inTimer)
581 {
582 if (wxTheApp->Pending())
583 wxTheApp->Dispatch();
584 }
585 delete wxTheSleepTimer;
586 wxTheSleepTimer = NULL;
587}
588
589// Consume all events until no more left
590void wxFlushEvents()
591{
592// wxYield();
593}
594
595#elif defined(__WIN32__) // wxUSE_GUI
596
597void wxUsleep(unsigned long milliseconds)
598{
599 ::Sleep(milliseconds);
600}
601
602void wxSleep(int nSecs)
603{
604 wxUsleep(1000*nSecs);
605}
606
607#endif // wxUSE_GUI/!wxUSE_GUI
608
609// ----------------------------------------------------------------------------
610// deprecated (in favour of wxLog) log functions
611// ----------------------------------------------------------------------------
612
613#if wxUSE_GUI
614
615// Output a debug mess., in a system dependent fashion.
616void wxDebugMsg(const wxChar *fmt ...)
617{
618 va_list ap;
619 static wxChar buffer[512];
620
621 if (!wxTheApp->GetWantDebugOutput())
622 return ;
623
624 va_start(ap, fmt);
625
626 wvsprintf(buffer,fmt,ap) ;
627 OutputDebugString((LPCTSTR)buffer) ;
628
629 va_end(ap);
630}
631
632// Non-fatal error: pop up message box and (possibly) continue
633void wxError(const wxString& msg, const wxString& title)
634{
635 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
636 if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title,
637 MB_ICONSTOP | MB_YESNO) == IDNO)
638 wxExit();
639}
640
641// Fatal error: pop up message box and abort
642void wxFatalError(const wxString& msg, const wxString& title)
643{
644 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
645 FatalAppExit(0, (LPCTSTR)wxBuffer);
646}
647
648// ----------------------------------------------------------------------------
649// functions to work with .INI files
650// ----------------------------------------------------------------------------
651
2bda0e17 652// Reading and writing resources (eg WIN.INI, .Xdefaults)
47d67540 653#if wxUSE_RESOURCES
2bda0e17
KB
654bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
655{
223d09f6 656 if (file != wxT(""))
8fb3a512
JS
657// Don't know what the correct cast should be, but it doesn't
658// compile in BC++/16-bit without this cast.
659#if !defined(__WIN32__)
660 return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0);
661#else
837e5743 662 return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0);
8fb3a512 663#endif
2bda0e17 664 else
837e5743 665 return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0);
2bda0e17
KB
666}
667
668bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
669{
b568d04f
VZ
670 wxString buf;
671 buf.Printf(wxT("%.4f"), value);
672
673 return wxWriteResource(section, entry, buf, file);
2bda0e17
KB
674}
675
676bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
677{
b568d04f
VZ
678 wxString buf;
679 buf.Printf(wxT("%ld"), value);
680
681 return wxWriteResource(section, entry, buf, file);
2bda0e17
KB
682}
683
684bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
685{
b568d04f
VZ
686 wxString buf;
687 buf.Printf(wxT("%d"), value);
688
689 return wxWriteResource(section, entry, buf, file);
2bda0e17
KB
690}
691
837e5743 692bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file)
2bda0e17 693{
b568d04f
VZ
694 static const wxChar defunkt[] = wxT("$$default");
695 if (file != wxT(""))
696 {
697 int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt,
698 (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file);
699 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
700 return FALSE;
701 }
702 else
703 {
704 int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt,
705 (LPTSTR)wxBuffer, 1000);
706 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
707 return FALSE;
2bda0e17 708 }
b568d04f
VZ
709 if (*value) delete[] (*value);
710 *value = copystring(wxBuffer);
711 return TRUE;
712}
2bda0e17
KB
713
714bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
715{
b568d04f
VZ
716 wxChar *s = NULL;
717 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
718 if (succ)
719 {
720 *value = (float)wxStrtod(s, NULL);
721 delete[] s;
722 return TRUE;
723 }
724 else return FALSE;
2bda0e17
KB
725}
726
727bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
728{
b568d04f
VZ
729 wxChar *s = NULL;
730 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
731 if (succ)
732 {
733 *value = wxStrtol(s, NULL, 10);
734 delete[] s;
735 return TRUE;
736 }
737 else return FALSE;
2bda0e17
KB
738}
739
740bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
741{
b568d04f
VZ
742 wxChar *s = NULL;
743 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
744 if (succ)
745 {
746 *value = (int)wxStrtol(s, NULL, 10);
747 delete[] s;
748 return TRUE;
749 }
750 else return FALSE;
2bda0e17 751}
47d67540 752#endif // wxUSE_RESOURCES
2bda0e17 753
634903fd 754// ---------------------------------------------------------------------------
25889d3c 755// helper functions for showing a "busy" cursor
634903fd
VZ
756// ---------------------------------------------------------------------------
757
25889d3c
JS
758HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
759HCURSOR gs_wxBusyCursorOld = 0; // old cursor
634903fd 760static int gs_wxBusyCursorCount = 0;
2bda0e17
KB
761
762// Set the cursor to the busy cursor for all windows
763void wxBeginBusyCursor(wxCursor *cursor)
764{
634903fd
VZ
765 if ( gs_wxBusyCursorCount++ == 0 )
766 {
767 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
768 gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor);
769 }
770 //else: nothing to do, already set
2bda0e17
KB
771}
772
773// Restore cursor to normal
634903fd 774void wxEndBusyCursor()
2bda0e17 775{
634903fd 776 wxCHECK_RET( gs_wxBusyCursorCount > 0,
223d09f6 777 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
b07135ba 778
634903fd
VZ
779 if ( --gs_wxBusyCursorCount == 0 )
780 {
781 ::SetCursor(gs_wxBusyCursorOld);
782
783 gs_wxBusyCursorOld = 0;
784 }
2bda0e17
KB
785}
786
787// TRUE if we're between the above two calls
634903fd 788bool wxIsBusy()
2bda0e17 789{
634903fd 790 return (gs_wxBusyCursorCount > 0);
b07135ba 791}
2bda0e17 792
2bda0e17
KB
793// Check whether this window wants to process messages, e.g. Stop button
794// in long calculations.
795bool wxCheckForInterrupt(wxWindow *wnd)
796{
b568d04f
VZ
797 wxCHECK( wnd, FALSE );
798
743e0a66 799 MSG msg;
b568d04f
VZ
800 while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) )
801 {
802 ::TranslateMessage(&msg);
803 ::DispatchMessage(&msg);
743e0a66 804 }
634903fd 805
b568d04f 806 return TRUE;
2bda0e17
KB
807}
808
b568d04f
VZ
809#endif // wxUSE_GUI
810
2bda0e17
KB
811// MSW only: get user-defined resource from the .res file.
812// Returns NULL or newly-allocated memory, so use delete[] to clean up.
813
837e5743 814wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
2bda0e17 815{
b568d04f
VZ
816 HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType);
817 if ( hResource == 0 )
818 return NULL;
819
820 HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
821 if ( hData == 0 )
822 return NULL;
2bda0e17 823
b568d04f
VZ
824 wxChar *theText = (wxChar *)::LockResource(hData);
825 if ( !theText )
826 return NULL;
b07135ba 827
b568d04f 828 wxChar *s = copystring(theText);
2bda0e17 829
b568d04f 830 // Obsolete in WIN32
2bda0e17 831#ifndef __WIN32__
b568d04f 832 UnlockResource(hData);
2bda0e17
KB
833#endif
834
b568d04f
VZ
835 // No need??
836 // GlobalFree(hData);
2bda0e17 837
b568d04f 838 return s;
2bda0e17 839}
b568d04f
VZ
840
841// ----------------------------------------------------------------------------
842// get display info
843// ----------------------------------------------------------------------------
2bda0e17
KB
844
845void wxGetMousePosition( int* x, int* y )
846{
b568d04f
VZ
847 POINT pt;
848 GetCursorPos( & pt );
849 if ( x ) *x = pt.x;
850 if ( y ) *y = pt.y;
2bda0e17
KB
851};
852
853// Return TRUE if we have a colour display
634903fd 854bool wxColourDisplay()
2bda0e17 855{
b568d04f
VZ
856 ScreenHDC dc;
857 int noCols = GetDeviceCaps(dc, NUMCOLORS);
858
859 return (noCols == -1) || (noCols > 2);
2bda0e17
KB
860}
861
862// Returns depth of screen
634903fd 863int wxDisplayDepth()
2bda0e17 864{
b568d04f
VZ
865 ScreenHDC dc;
866 return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL);
2bda0e17
KB
867}
868
869// Get size of display
870void wxDisplaySize(int *width, int *height)
871{
b568d04f 872 ScreenHDC dc;
3f4a0c5b 873
b568d04f
VZ
874 if ( width ) *width = GetDeviceCaps(dc, HORZRES);
875 if ( height ) *height = GetDeviceCaps(dc, VERTRES);
7f555861
JS
876}
877
cc2b7472
VZ
878// ---------------------------------------------------------------------------
879// window information functions
880// ---------------------------------------------------------------------------
881
1c4a764c
VZ
882wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
883{
884 wxString str;
885 int len = GetWindowTextLength((HWND)hWnd) + 1;
886 GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len);
887 str.UngetWriteBuf();
888
889 return str;
890}
891
cc2b7472
VZ
892wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
893{
894 wxString str;
895
896 int len = 256; // some starting value
897
898 for ( ;; )
899 {
900 // as we've #undefined GetClassName we must now manually choose the
901 // right function to call
902 int count =
903
904 #ifndef __WIN32__
905 GetClassName
906 #else // Win32
907 #ifdef UNICODE
908 GetClassNameW
909 #else // !Unicode
910 #ifdef __TWIN32__
911 GetClassName
912 #else // !Twin32
913 GetClassNameA
914 #endif // Twin32/!Twin32
915 #endif // Unicode/ANSI
916 #endif // Win16/32
917 ((HWND)hWnd, str.GetWriteBuf(len), len);
918
919 str.UngetWriteBuf();
920 if ( count == len )
921 {
922 // the class name might have been truncated, retry with larger
923 // buffer
924 len *= 2;
925 }
926 else
927 {
928 break;
929 }
930 }
931
932 return str;
933}
934
42e69d6b 935WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
cc2b7472
VZ
936{
937#ifndef __WIN32__
42e69d6b 938 return GetWindowWord((HWND)hWnd, GWW_ID);
cc2b7472 939#else // Win32
42e69d6b 940 return GetWindowLong((HWND)hWnd, GWL_ID);
cc2b7472
VZ
941#endif // Win16/32
942}
943
7f555861
JS
944#if 0
945//------------------------------------------------------------------------
946// wild character routines
947//------------------------------------------------------------------------
948
949bool wxIsWild( const wxString& pattern )
950{
951 wxString tmp = pattern;
952 char *pat = WXSTRINGCAST(tmp);
953 while (*pat) {
3f4a0c5b
VZ
954 switch (*pat++) {
955 case '?': case '*': case '[': case '{':
956 return TRUE;
957 case '\\':
958 if (!*pat++)
959 return FALSE;
960 }
7f555861
JS
961 }
962 return FALSE;
963};
964
965
966bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
967{
968 wxString tmp1 = pat;
969 char *pattern = WXSTRINGCAST(tmp1);
970 wxString tmp2 = text;
971 char *str = WXSTRINGCAST(tmp2);
972 char c;
973 char *cp;
974 bool done = FALSE, ret_code, ok;
975 // Below is for vi fans
976 const char OB = '{', CB = '}';
977
978 // dot_special means '.' only matches '.'
979 if (dot_special && *str == '.' && *pattern != *str)
3f4a0c5b 980 return FALSE;
7f555861
JS
981
982 while ((*pattern != '\0') && (!done)
983 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
3f4a0c5b
VZ
984 switch (*pattern) {
985 case '\\':
986 pattern++;
987 if (*pattern != '\0')
988 pattern++;
989 break;
990 case '*':
991 pattern++;
992 ret_code = FALSE;
993 while ((*str!='\0')
994 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
995 /*loop*/;
996 if (ret_code) {
997 while (*str != '\0')
998 str++;
999 while (*pattern != '\0')
1000 pattern++;
1001 }
1002 break;
1003 case '[':
1004 pattern++;
1005 repeat:
1006 if ((*pattern == '\0') || (*pattern == ']')) {
1007 done = TRUE;
1008 break;
1009 }
1010 if (*pattern == '\\') {
1011 pattern++;
1012 if (*pattern == '\0') {
1013 done = TRUE;
1014 break;
1015 }
1016 }
1017 if (*(pattern + 1) == '-') {
1018 c = *pattern;
1019 pattern += 2;
1020 if (*pattern == ']') {
1021 done = TRUE;
1022 break;
1023 }
1024 if (*pattern == '\\') {
1025 pattern++;
1026 if (*pattern == '\0') {
1027 done = TRUE;
1028 break;
1029 }
1030 }
1031 if ((*str < c) || (*str > *pattern)) {
1032 pattern++;
1033 goto repeat;
1034 }
1035 } else if (*pattern != *str) {
1036 pattern++;
1037 goto repeat;
1038 }
1039 pattern++;
1040 while ((*pattern != ']') && (*pattern != '\0')) {
1041 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
1042 pattern++;
1043 pattern++;
1044 }
1045 if (*pattern != '\0') {
1046 pattern++, str++;
1047 }
1048 break;
1049 case '?':
1050 pattern++;
1051 str++;
1052 break;
1053 case OB:
1054 pattern++;
1055 while ((*pattern != CB) && (*pattern != '\0')) {
1056 cp = str;
1057 ok = TRUE;
1058 while (ok && (*cp != '\0') && (*pattern != '\0')
1059 && (*pattern != ',') && (*pattern != CB)) {
1060 if (*pattern == '\\')
1061 pattern++;
1062 ok = (*pattern++ == *cp++);
1063 }
1064 if (*pattern == '\0') {
1065 ok = FALSE;
1066 done = TRUE;
1067 break;
1068 } else if (ok) {
1069 str = cp;
1070 while ((*pattern != CB) && (*pattern != '\0')) {
1071 if (*++pattern == '\\') {
1072 if (*++pattern == CB)
1073 pattern++;
1074 }
1075 }
1076 } else {
1077 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
1078 if (*++pattern == '\\') {
7f555861 1079 if (*++pattern == CB || *pattern == ',')
3f4a0c5b
VZ
1080 pattern++;
1081 }
1082 }
1083 }
1084 if (*pattern != '\0')
1085 pattern++;
1086 }
1087 break;
1088 default:
1089 if (*str == *pattern) {
1090 str++, pattern++;
1091 } else {
1092 done = TRUE;
1093 }
1094 }
7f555861
JS
1095 }
1096 while (*pattern == '*')
3f4a0c5b 1097 pattern++;
7f555861
JS
1098 return ((*str == '\0') && (*pattern == '\0'));
1099};
1100
1101#endif
1102
cf45d1f4 1103#if 0
c030b70f
JS
1104
1105// maximum mumber of lines the output console should have
1106static const WORD MAX_CONSOLE_LINES = 500;
1107
1108BOOL WINAPI MyConsoleHandler( DWORD dwCtrlType ) { // control signal type
3f4a0c5b
VZ
1109 FreeConsole();
1110 return TRUE;
c030b70f
JS
1111}
1112
1113void wxRedirectIOToConsole()
1114{
1115 int hConHandle;
1116 long lStdHandle;
1117 CONSOLE_SCREEN_BUFFER_INFO coninfo;
1118 FILE *fp;
1119
1120 // allocate a console for this app
1121 AllocConsole();
1122
1123 // set the screen buffer to be big enough to let us scroll text
3f4a0c5b 1124 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
c030b70f
JS
1125 &coninfo);
1126 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
3f4a0c5b 1127 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
c030b70f
JS
1128 coninfo.dwSize);
1129
1130 // redirect unbuffered STDOUT to the console
1131 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
1132 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1133 if(hConHandle <= 0) return;
1134 fp = _fdopen( hConHandle, "w" );
1135 *stdout = *fp;
1136 setvbuf( stdout, NULL, _IONBF, 0 );
1137
1138 // redirect unbuffered STDIN to the console
1139 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
1140 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1141 if(hConHandle <= 0) return;
1142 fp = _fdopen( hConHandle, "r" );
1143 *stdin = *fp;
1144 setvbuf( stdin, NULL, _IONBF, 0 );
1145
1146 // redirect unbuffered STDERR to the console
1147 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
1148 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
1149 if(hConHandle <= 0) return;
1150 fp = _fdopen( hConHandle, "w" );
1151 *stderr = *fp;
1152 setvbuf( stderr, NULL, _IONBF, 0 );
3f4a0c5b
VZ
1153
1154 // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
c030b70f
JS
1155 // point to console as well
1156 ios::sync_with_stdio();
1157
3f4a0c5b 1158 SetConsoleCtrlHandler(MyConsoleHandler, TRUE);
c030b70f
JS
1159}
1160#else
1161// Not supported
1162void wxRedirectIOToConsole()
1163{
1164}
1165#endif
1166
1167