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 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
424 extern char *wxOsVersion
;
432 #ifdef __WINDOWS_386__
436 #if !defined(__WATCOMC__) && !defined(__GNUWIN32__) && USE_PENWINDOWS
437 extern HANDLE hPenWin
;
438 retValue
= hPenWin
? wxPENWINDOWS
: wxWINDOWS
;
443 DWORD Version
= GetVersion() ;
444 WORD lowWord
= LOWORD(Version
) ;
448 if (strcmp(wxOsVersion
, "Win95") == 0)
450 else if (strcmp(wxOsVersion
, "Win32s") == 0)
452 else if (strcmp(wxOsVersion
, "Windows") == 0)
454 else if (strcmp(wxOsVersion
, "WinNT") == 0)
457 bool Win32s
= (( Version
& 0x80000000 ) != 0);
458 bool Win95
= (( Version
& 0xFF ) >= 4);
459 bool WinNT
= Version
< 0x80000000;
461 // Get the version number
463 *majorVsn
= LOBYTE( lowWord
);
465 *minorVsn
= HIBYTE( lowWord
);
476 // retValue = ((high & 0x8000)==0) ? wxWINDOWS_NT : wxWIN32S ;
478 // @@@@ To be completed. I don't have the manual here...
479 if (majorVsn
) *majorVsn
= 3 ;
480 if (minorVsn
) *minorVsn
= 1 ;
484 // Reading and writing resources (eg WIN.INI, .Xdefaults)
486 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
489 return (WritePrivateProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)value
, (LPCSTR
)WXSTRINGCAST file
) != 0);
491 return (WriteProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)WXSTRINGCAST value
) != 0);
494 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
497 sprintf(buf
, "%.4f", value
);
498 return wxWriteResource(section
, entry
, buf
, file
);
501 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
504 sprintf(buf
, "%ld", value
);
505 return wxWriteResource(section
, entry
, buf
, file
);
508 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
511 sprintf(buf
, "%d", value
);
512 return wxWriteResource(section
, entry
, buf
, file
);
515 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
517 static const char defunkt
[] = "$$default";
520 int n
= GetPrivateProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)defunkt
,
521 (LPSTR
)wxBuffer
, 1000, (LPCSTR
)WXSTRINGCAST file
);
522 if (n
== 0 || strcmp(wxBuffer
, defunkt
) == 0)
527 int n
= GetProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)defunkt
,
528 (LPSTR
)wxBuffer
, 1000);
529 if (n
== 0 || strcmp(wxBuffer
, defunkt
) == 0)
532 if (*value
) delete[] (*value
);
533 *value
= copystring(wxBuffer
);
537 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
540 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
543 *value
= (float)strtod(s
, NULL
);
550 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
553 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
556 *value
= strtol(s
, NULL
, 10);
563 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
566 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
569 *value
= (int)strtol(s
, NULL
, 10);
575 #endif // USE_RESOURCES
578 static HCURSOR wxBusyCursorOld
= 0;
579 static int wxBusyCursorCount
= 0;
581 // Set the cursor to the busy cursor for all windows
582 void wxBeginBusyCursor(wxCursor
*cursor
)
584 wxBusyCursorCount
++;
585 if (wxBusyCursorCount
== 1)
587 wxBusyCursorOld
= ::SetCursor((HCURSOR
) cursor
->GetHCURSOR());
591 (void)::SetCursor((HCURSOR
) cursor
->GetHCURSOR());
595 // Restore cursor to normal
596 void wxEndBusyCursor(void)
598 if (wxBusyCursorCount
== 0)
601 wxBusyCursorCount
--;
602 if (wxBusyCursorCount
== 0)
604 ::SetCursor(wxBusyCursorOld
);
609 // TRUE if we're between the above two calls
612 return (wxBusyCursorCount
> 0);
616 char *wxGetUserHome (const wxString
& user
)
619 wxString
user1(user
);
623 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
624 // Guests belong in the temp dir
625 if (stricmp(tmp
, "annonymous") == 0) {
626 if ((home
= getenv("TMP")) != NULL
||
627 (home
= getenv("TMPDIR")) != NULL
||
628 (home
= getenv("TEMP")) != NULL
)
629 return *home
? home
: "\\";
631 if (stricmp(tmp
, WXSTRINGCAST user1
) == 0)
636 if ((home
= getenv("HOME")) != NULL
)
638 strcpy(wxBuffer
, home
);
639 Unix2DosFilename(wxBuffer
);
642 return NULL
; // No home known!
645 // Check whether this window wants to process messages, e.g. Stop button
646 // in long calculations.
647 bool wxCheckForInterrupt(wxWindow
*wnd
)
651 HWND win
= (HWND
) wnd
->GetHWND();
652 while(PeekMessage(&msg
,win
,0,0,PM_REMOVE
)){
653 TranslateMessage(&msg
);
654 DispatchMessage(&msg
);
656 return TRUE
;//*** temporary?
659 wxError("wnd==NULL !!!");
660 return FALSE
;//*** temporary?
664 // MSW only: get user-defined resource from the .res file.
665 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
668 char *wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
672 HRSRC hResource
= ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
675 HRSRC hResource
= ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
677 HRSRC hResource
= ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
683 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
686 char *theText
= (char *)LockResource(hData
);
690 s
= copystring(theText
);
694 UnlockResource(hData
);
698 // GlobalFree(hData);
704 void wxGetMousePosition( int* x
, int* y
)
707 GetCursorPos( & pt
);
712 // Return TRUE if we have a colour display
713 bool wxColourDisplay(void)
715 HDC dc
= ::GetDC(NULL
);
717 int noCols
= GetDeviceCaps(dc
, NUMCOLORS
);
718 if ((noCols
== -1) || (noCols
> 2))
726 // Returns depth of screen
727 int wxDisplayDepth(void)
729 HDC dc
= ::GetDC(NULL
);
730 int planes
= GetDeviceCaps(dc
, PLANES
);
731 int bitsPerPixel
= GetDeviceCaps(dc
, BITSPIXEL
);
732 int depth
= planes
*bitsPerPixel
;
737 // Get size of display
738 void wxDisplaySize(int *width
, int *height
)
740 HDC dc
= ::GetDC(NULL
);
741 *width
= GetDeviceCaps(dc
, HORZRES
); *height
= GetDeviceCaps(dc
, VERTRES
);