1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
14 #pragma implementation "utils.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
28 #include "wx/cursor.h"
31 #include "wx/msw/private.h"
42 #include <sys/unistd.h>
48 #define stricmp strcasecmp
51 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
52 // this (3.1 I believe) and how to test for it.
53 // If this works for Borland 4.0 as well, then no worries.
69 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
75 // In the WIN.INI file
76 static const char WX_SECTION
[] = "wxWindows";
77 static const char eHOSTNAME
[] = "HostName";
78 static const char eUSERID
[] = "UserId";
79 static const char eUSERNAME
[] = "UserName";
81 // For the following functions we SHOULD fill in support
82 // for Windows-NT (which I don't know) as I assume it begin
83 // a POSIX Unix (so claims MS) that it has some special
84 // functions beyond those provided by WinSock
86 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
87 bool wxGetHostName(char *buf
, int maxSize
)
90 DWORD nSize
= maxSize
;
91 return (::GetComputerName(buf
, &nSize
) != 0);
94 const char *default_host
= "noname";
96 if ((sysname
= getenv("SYSTEM_NAME")) == NULL
) {
97 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
99 strncpy(buf
, sysname
, maxSize
- 1);
101 return *buf
? TRUE
: FALSE
;
105 // Get user ID e.g. jacs
106 bool wxGetUserId(char *buf
, int maxSize
)
108 #if defined(__WIN32__) && !defined(__win32s__) && 0
109 // Gets the current user's full name according to the MS article PSS ID
111 // Seems to be the same as the login name for me?
112 char *UserName
= new char[256];
113 char *Domain
= new char[256];
114 DWORD maxCharacters
= 255;
115 GetUserName( UserName
, &maxCharacters
);
116 GetComputerName( Domain
, &maxCharacters
);
118 WCHAR wszUserName
[256]; // Unicode user name
119 WCHAR wszDomain
[256];
122 struct _SERVER_INFO_100
*si100
; // Server structure
123 struct _USER_INFO_2
*ui
; // User structure
125 // Convert ASCII user name and domain to Unicode.
127 MultiByteToWideChar( CP_ACP
, 0, UserName
,
128 strlen(UserName
)+1, wszUserName
, sizeof(wszUserName
) );
129 MultiByteToWideChar( CP_ACP
, 0, Domain
,
130 strlen(Domain
)+1, wszDomain
, sizeof(wszDomain
) );
132 // Get the computer name of a DC for the specified domain.
133 // >If you get a link error on this, include netapi32.lib<
135 NetGetDCName( NULL
, wszDomain
, &ComputerName
);
137 // Look up the user on the DC.
139 if(NetUserGetInfo( (LPWSTR
) ComputerName
,
140 (LPWSTR
) &wszUserName
, 2, (LPBYTE
*) &ui
))
142 printf( "Error getting user information.\n" );
146 // Convert the Unicode full name to ASCII.
148 WideCharToMultiByte( CP_ACP
, 0, ui
->usri2_full_name
,
149 -1, buf
, 256, NULL
, NULL
);
153 DWORD nSize = maxSize;
154 return ::GetUserName(buf, &nSize);
158 const char *default_id
= "anonymous";
160 // Can't assume we have NIS (PC-NFS) or some other ID daemon
162 if ( (user
= getenv("USER")) == NULL
&&
163 (user
= getenv("LOGNAME")) == NULL
) {
164 // Use wxWindows configuration data (comming soon)
165 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
167 strncpy(buf
, user
, maxSize
- 1);
168 return *buf
? TRUE
: FALSE
;
172 // Get user name e.g. Julian Smart
173 bool wxGetUserName(char *buf
, int maxSize
)
175 const char *default_name
= "Unknown User";
176 #if defined(__WIN32__)
178 DWORD nSize = maxSize;
179 In VC++ 4.0, results in unresolved symbol __imp__GetUserNameA
180 if (GetUserName(buf, &nSize))
184 // Could use NIS, MS-Mail or other site specific programs
185 // Use wxWindows configuration data
186 GetProfileString(WX_SECTION
, eUSERNAME
, default_name
, buf
, maxSize
- 1);
187 return *buf
? TRUE
: FALSE
;
190 #if !defined(__WATCOMC__) && !defined(__GNUWIN32__) && USE_PENWINDOWS
191 extern HANDLE hPenWin
; // PenWindows Running?
194 // PenWindows Does have a user concept!
195 // Get the current owner of the recognizer
196 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
197 strncpy(buf
, wxBuffer
, maxSize
- 1);
202 // Could use NIS, MS-Mail or other site specific programs
203 // Use wxWindows configuration data
204 GetProfileString(WX_SECTION
, eUSERNAME
, default_name
, buf
, maxSize
- 1);
206 return *buf
? TRUE
: FALSE
;
210 // Execute a command (e.g. another program) in a
211 // system-independent manner.
213 long wxExecute(char **argv
, bool sync
)
222 for (argc
= 0; argv
[argc
]; argc
++)
225 strcat(command
, " ");
226 strcat(command
, argv
[argc
]);
229 return wxExecute((char *)command
, sync
);
232 long wxExecute(const wxString
& command
, bool sync
)
244 // copy the command line
245 clen
= command
.Length();
246 if (!clen
) return -1;
247 cl
= (char *) calloc( 1, 256);
249 strcpy( cl
, WXSTRINGCAST command
);
251 // isolate command and arguments
252 argp
= strchr( cl
, ' ');
256 // execute the command
258 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? (HWND
) wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
259 (const wchar_t) "open", (const wchar_t) cl
, (const wchar_t) argp
, (const wchar_t) NULL
, SW_SHOWNORMAL
);
261 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
262 "open", cl
, argp
, NULL
, SW_SHOWNORMAL
);
265 if (((long)result
) <= 32) {
276 // waiting until command executed
279 dresult
= GetModuleFileName( result
, cl
, 256);
282 /* long lastError = GetLastError(); */
287 long instanceID
= WinExec((LPCSTR
) WXSTRINGCAST command
, SW_SHOW
);
288 if (instanceID
< 32) return(0);
294 running
= GetModuleUsage((HANDLE
)instanceID
);
301 int wxKill(long pid
, int sig
)
307 // Execute a program in an Interactive Shell
310 wxShell(const wxString
& command
)
313 if ((shell
= getenv("COMSPEC")) == NULL
)
314 shell
= "\\COMMAND.COM";
318 sprintf(tmp
, "%s /c %s", shell
, WXSTRINGCAST command
);
322 return (wxExecute((char *)tmp
, FALSE
) != 0);
325 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
326 long wxGetFreeMemory(void)
328 #if defined(__WIN32__) && !defined(__BORLANDC__)
329 MEMORYSTATUS memStatus
;
330 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
331 GlobalMemoryStatus(&memStatus
);
332 return memStatus
.dwAvailPhys
;
334 return (long)GetFreeSpace(0);
338 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
339 static bool inTimer
= FALSE
;
340 class wxSleepTimer
: public wxTimer
343 inline void Notify(void)
350 static wxTimer
*wxTheSleepTimer
= NULL
;
352 void wxSleep(int nSecs
)
354 #if 0 // WIN32 hangs app
360 wxTheSleepTimer
= new wxSleepTimer
;
362 wxTheSleepTimer
->Start(nSecs
*1000);
365 if (wxTheApp
->Pending())
366 wxTheApp
->Dispatch();
368 delete wxTheSleepTimer
;
369 wxTheSleepTimer
= NULL
;
373 // Consume all events until no more left
374 void wxFlushEvents(void)
379 // Output a debug mess., in a system dependent fashion.
380 void wxDebugMsg(const char *fmt
...)
383 static char buffer
[512];
385 if (!wxTheApp
->GetWantDebugOutput())
390 wvsprintf(buffer
,fmt
,ap
) ;
391 OutputDebugString((LPCSTR
)buffer
) ;
396 // Non-fatal error: pop up message box and (possibly) continue
397 void wxError(const wxString
& msg
, const wxString
& title
)
399 sprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST msg
);
400 if (MessageBox(NULL
, (LPCSTR
)wxBuffer
, (LPCSTR
)WXSTRINGCAST title
,
401 MB_ICONSTOP
| MB_YESNO
) == IDNO
)
405 // Fatal error: pop up message box and abort
406 void wxFatalError(const wxString
& msg
, const wxString
& title
)
408 sprintf(wxBuffer
, "%s: %s", WXSTRINGCAST title
, WXSTRINGCAST msg
);
409 FatalAppExit(0, (LPCSTR
)wxBuffer
);
416 Beep(1000,1000) ; // 1kHz during 1 sec.
422 // Chris Breeze 27/5/98: revised WIN32 code to
423 // detect WindowsNT correctly
424 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
426 extern char *wxOsVersion
;
427 if (majorVsn
) *majorVsn
= 0;
428 if (minorVsn
) *minorVsn
= 0;
432 memset(&info
, 0, sizeof(OSVERSIONINFO
));
433 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
434 if (GetVersionEx(&info
))
436 if (majorVsn
) *majorVsn
= info
.dwMajorVersion
;
437 if (minorVsn
) *minorVsn
= info
.dwMinorVersion
;
438 switch (info
.dwPlatformId
)
440 case VER_PLATFORM_WIN32s
:
443 case VER_PLATFORM_WIN32_WINDOWS
:
446 case VER_PLATFORM_WIN32_NT
:
451 return wxWINDOWS
; // error if we get here, return generic value
455 # ifdef __WINDOWS_386__
458 # if !defined(__WATCOMC__) && !defined(GNUWIN32)
459 extern HANDLE hPenWin
;
460 retValue
= hPenWin
? wxPENWINDOWS
: wxWINDOWS
;
463 // @@@@ To be completed. I don't have the manual here...
464 if (majorVsn
) *majorVsn
= 3 ;
465 if (minorVsn
) *minorVsn
= 1 ;
470 // Reading and writing resources (eg WIN.INI, .Xdefaults)
472 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
475 return (WritePrivateProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)value
, (LPCSTR
)WXSTRINGCAST file
) != 0);
477 return (WriteProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)WXSTRINGCAST value
) != 0);
480 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
483 sprintf(buf
, "%.4f", value
);
484 return wxWriteResource(section
, entry
, buf
, file
);
487 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
490 sprintf(buf
, "%ld", value
);
491 return wxWriteResource(section
, entry
, buf
, file
);
494 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
497 sprintf(buf
, "%d", value
);
498 return wxWriteResource(section
, entry
, buf
, file
);
501 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
503 static const char defunkt
[] = "$$default";
506 int n
= GetPrivateProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)defunkt
,
507 (LPSTR
)wxBuffer
, 1000, (LPCSTR
)WXSTRINGCAST file
);
508 if (n
== 0 || strcmp(wxBuffer
, defunkt
) == 0)
513 int n
= GetProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)defunkt
,
514 (LPSTR
)wxBuffer
, 1000);
515 if (n
== 0 || strcmp(wxBuffer
, defunkt
) == 0)
518 if (*value
) delete[] (*value
);
519 *value
= copystring(wxBuffer
);
523 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
526 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
529 *value
= (float)strtod(s
, NULL
);
536 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
539 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
542 *value
= strtol(s
, NULL
, 10);
549 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
552 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
555 *value
= (int)strtol(s
, NULL
, 10);
561 #endif // USE_RESOURCES
564 static HCURSOR wxBusyCursorOld
= 0;
565 static int wxBusyCursorCount
= 0;
567 // Set the cursor to the busy cursor for all windows
568 void wxBeginBusyCursor(wxCursor
*cursor
)
570 wxBusyCursorCount
++;
571 if (wxBusyCursorCount
== 1)
573 wxBusyCursorOld
= ::SetCursor((HCURSOR
) cursor
->GetHCURSOR());
577 (void)::SetCursor((HCURSOR
) cursor
->GetHCURSOR());
581 // Restore cursor to normal
582 void wxEndBusyCursor(void)
584 if (wxBusyCursorCount
== 0)
587 wxBusyCursorCount
--;
588 if (wxBusyCursorCount
== 0)
590 ::SetCursor(wxBusyCursorOld
);
595 // TRUE if we're between the above two calls
598 return (wxBusyCursorCount
> 0);
602 char *wxGetUserHome (const wxString
& user
)
605 wxString
user1(user
);
609 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
610 // Guests belong in the temp dir
611 if (stricmp(tmp
, "annonymous") == 0) {
612 if ((home
= getenv("TMP")) != NULL
||
613 (home
= getenv("TMPDIR")) != NULL
||
614 (home
= getenv("TEMP")) != NULL
)
615 return *home
? home
: "\\";
617 if (stricmp(tmp
, WXSTRINGCAST user1
) == 0)
622 if ((home
= getenv("HOME")) != NULL
)
624 strcpy(wxBuffer
, home
);
625 Unix2DosFilename(wxBuffer
);
628 return NULL
; // No home known!
631 // Check whether this window wants to process messages, e.g. Stop button
632 // in long calculations.
633 bool wxCheckForInterrupt(wxWindow
*wnd
)
637 HWND win
= (HWND
) wnd
->GetHWND();
638 while(PeekMessage(&msg
,win
,0,0,PM_REMOVE
)){
639 TranslateMessage(&msg
);
640 DispatchMessage(&msg
);
642 return TRUE
;//*** temporary?
645 wxError("wnd==NULL !!!");
646 return FALSE
;//*** temporary?
650 // MSW only: get user-defined resource from the .res file.
651 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
654 char *wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
658 HRSRC hResource
= ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
661 HRSRC hResource
= ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
663 HRSRC hResource
= ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
669 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
672 char *theText
= (char *)LockResource(hData
);
676 s
= copystring(theText
);
680 UnlockResource(hData
);
684 // GlobalFree(hData);
690 void wxGetMousePosition( int* x
, int* y
)
693 GetCursorPos( & pt
);
698 // Return TRUE if we have a colour display
699 bool wxColourDisplay(void)
701 HDC dc
= ::GetDC(NULL
);
703 int noCols
= GetDeviceCaps(dc
, NUMCOLORS
);
704 if ((noCols
== -1) || (noCols
> 2))
712 // Returns depth of screen
713 int wxDisplayDepth(void)
715 HDC dc
= ::GetDC(NULL
);
716 int planes
= GetDeviceCaps(dc
, PLANES
);
717 int bitsPerPixel
= GetDeviceCaps(dc
, BITSPIXEL
);
718 int depth
= planes
*bitsPerPixel
;
723 // Get size of display
724 void wxDisplaySize(int *width
, int *height
)
726 HDC dc
= ::GetDC(NULL
);
727 *width
= GetDeviceCaps(dc
, HORZRES
); *height
= GetDeviceCaps(dc
, VERTRES
);