1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now.
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/cursor.h"
26 #include "wx/os2/private.h"
49 // In the WIN.INI file
50 static const wxChar WX_SECTION
[] = "wxWindows";
51 static const wxChar eHOSTNAME
[] = "HostName";
52 static const wxChar eUSERID
[] = "UserId";
53 static const wxChar eUSERNAME
[] = "UserName";
55 // For the following functions we SHOULD fill in support
56 // for Windows-NT (which I don't know) as I assume it begin
57 // a POSIX Unix (so claims MS) that it has some special
58 // functions beyond those provided by WinSock
60 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
61 bool wxGetHostName(wxChar
*buf
, int maxSize
)
66 unsigned long ulLevel
;
67 unsigned char* pbBuffer
;
68 unsigned long ulBuffer
;
69 unsigned long* pulTotalAvail
;
71 NetBios32GetInfo( server
81 const wxChar
*default_host
= _T("noname");
83 if ((sysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
) {
84 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
86 wxStrncpy(buf
, sysname
, maxSize
- 1);
87 buf
[maxSize
] = _T('\0');
89 return *buf
? TRUE
: FALSE
;
92 // Get user ID e.g. jacs
93 bool wxGetUserId(wxChar
*buf
, int maxSize
)
95 return(U32ELOCL(bub
, maxSize
));
98 bool wxGetUserName(wxChar
*buf
, int maxSize
)
101 wxGetUserId(buf
, maxSize
);
103 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, _T(""), buf
, maxSize
- 1) != 0;
106 ok
= wxGetUserId(buf
, maxSize
);
111 wxStrncpy(buf
, _T("Unknown User"), maxSize
);
117 int wxKill(long pid
, int sig
)
123 // Execute a program in an Interactive Shell
125 bool wxShell(const wxString
& command
)
128 if ((shell
= wxGetenv("COMSPEC")) == NULL
)
133 wxSprintf(tmp
, "%s /c %s", shell
, WXSTRINGCAST command
);
135 wxStrcpy(tmp
, shell
);
137 return (wxExecute((wxChar
*)tmp
, FALSE
) != 0);
140 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
141 long wxGetFreeMemory()
143 return (long)GetFreeSpace(0);
146 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
147 static bool inTimer
= FALSE
;
149 class wxSleepTimer
: public wxTimer
159 static wxTimer
*wxTheSleepTimer
= NULL
;
161 void wxUsleep(unsigned long milliseconds
)
163 ::DosSleep(milliseconds
);
166 void wxSleep(int nSecs
)
171 wxTheSleepTimer
= new wxSleepTimer
;
173 wxTheSleepTimer
->Start(nSecs
*1000);
176 if (wxTheApp
->Pending())
177 wxTheApp
->Dispatch();
179 delete wxTheSleepTimer
;
180 wxTheSleepTimer
= NULL
;
183 // Consume all events until no more left
189 // Output a debug mess., in a system dependent fashion.
190 void wxDebugMsg(const wxChar
*fmt
...)
193 static wxChar buffer
[512];
195 if (!wxTheApp
->GetWantDebugOutput())
200 sprintf(buffer
,fmt
,ap
) ;
206 // Non-fatal error: pop up message box and (possibly) continue
207 void wxError(const wxString
& msg
, const wxString
& title
)
209 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST msg
);
210 if (::WinMessageBox( HWND_DESKTOP
213 ,(PSZ
)WXSTRINGCAST title
214 ,MB_ICONEXCLAMATION
| MB_YESNO
219 // Fatal error: pop up message box and abort
220 void wxFatalError(const wxString
& msg
, const wxString
& title
)
225 WinMessageBox( HWND_DESKTOP
232 DosExit(EXIT_PROCESS
, rc
);
238 DosBeep(1000,1000); // 1kHz during 1 sec.
241 // Chris Breeze 27/5/98: revised WIN32 code to
242 // detect WindowsNT correctly
243 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
245 ULONG aulSysInfo
[QSV_MAX
] = {0};
247 if (DosQuerySysInfo( 1L
250 ,sizeof(ULONG
) * QSV_MAX
253 *majorVsn
= aulSysInfo
[QSV_VERSION_MAJOR
];
254 *minorVsn
= aulSysInfo
[QSV_VERSION_MINOR
];
255 return wxWINDOWS_OS2
;
257 return wxWINDOWS
; // error if we get here, return generic value
260 // Reading and writing resources (eg WIN.INI, .Xdefaults)
262 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
265 return (WritePrivateProfileString((PCSZ
)WXSTRINGCAST section
, (PCSZ
)WXSTRINGCAST entry
, (PCSZ
)value
, (PCSZ
)WXSTRINGCAST file
) != 0);
267 return (WriteProfileString((PCSZ
)WXSTRINGCAST section
, (PCSZ
)WXSTRINGCAST entry
, (PCSZ
)WXSTRINGCAST value
) != 0);
270 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
273 wxSprintf(buf
, "%.4f", value
);
274 return wxWriteResource(section
, entry
, buf
, file
);
277 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
280 wxSprintf(buf
, "%ld", value
);
281 return wxWriteResource(section
, entry
, buf
, file
);
284 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
287 wxSprintf(buf
, "%d", value
);
288 return wxWriteResource(section
, entry
, buf
, file
);
291 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
293 static const wxChar defunkt
[] = "$$default";
296 int n
= GetPrivateProfileString((PCSZ
)WXSTRINGCAST section
, (PCSZ
)WXSTRINGCAST entry
, (PCSZ
)defunkt
,
297 (PSZ
)wxBuffer
, 1000, (PCSZ
)WXSTRINGCAST file
);
298 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
303 int n
= GetProfileString((PCSZ
)WXSTRINGCAST section
, (PCSZ
)WXSTRINGCAST entry
, (LPCTSTR
)defunkt
,
304 (PSZ
)wxBuffer
, 1000);
305 if (n
== 0 || wxStrcmp(wxBuffer
, defunkt
) == 0)
308 if (*value
) delete[] (*value
);
309 *value
= copystring(wxBuffer
);
313 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
316 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
319 *value
= (float)wxStrtod(s
, NULL
);
326 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
329 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
332 *value
= wxStrtol(s
, NULL
, 10);
339 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
342 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
345 *value
= (int)wxStrtol(s
, NULL
, 10);
351 #endif // wxUSE_RESOURCES
353 // ---------------------------------------------------------------------------
354 // helper functions for showing a "busy" cursor
355 // ---------------------------------------------------------------------------
357 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
358 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
359 static int gs_wxBusyCursorCount
= 0;
361 // Set the cursor to the busy cursor for all windows
362 void wxBeginBusyCursor(wxCursor
*cursor
)
364 if ( gs_wxBusyCursorCount
++ == 0 )
366 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
367 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
369 //else: nothing to do, already set
372 // Restore cursor to normal
373 void wxEndBusyCursor()
375 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
376 "no matching wxBeginBusyCursor() for wxEndBusyCursor()");
378 if ( --gs_wxBusyCursorCount
== 0 )
380 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
381 gs_wxBusyCursorOld
= 0;
385 // TRUE if we're between the above two calls
388 return (gs_wxBusyCursorCount
> 0);
391 // ---------------------------------------------------------------------------
392 const wxChar
* wxGetHomeDir(wxString
*pstr
)
394 wxString
& strDir
= *pstr
;
396 // OS/2 has no idea about home,
397 // so use the working directory instead?
399 // 256 was taken from os2def.h
401 # define MAX_PATH 256
408 ::DosQueryCurrentDir( 0, DirName
, &DirLen
);
410 return strDir
.c_str();
414 wxChar
*wxGetUserHome (const wxString
& user
)
417 wxString
user1(user
);
419 if (user1
!= _T("")) {
421 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
422 // Guests belong in the temp dir
423 if (wxStricmp(tmp
, "annonymous") == 0) {
424 if ((home
= wxGetenv("TMP")) != NULL
||
425 (home
= wxGetenv("TMPDIR")) != NULL
||
426 (home
= wxGetenv("TEMP")) != NULL
)
427 return *home
? home
: (wxChar
*)_T("\\");
429 if (wxStricmp(tmp
, WXSTRINGCAST user1
) == 0)
434 if ((home
= wxGetenv("HOME")) != NULL
)
436 wxStrcpy(wxBuffer
, home
);
437 Unix2DosFilename(wxBuffer
);
440 return NULL
; // No home known!
443 // Check whether this window wants to process messages, e.g. Stop button
444 // in long calculations.
445 bool wxCheckForInterrupt(wxWindow
*wnd
)
452 HWND win
= (HWND
) wnd
->GetHWND();
453 while(::WinPeekMsg(hab
,&msg
,hwndFilter
,0,0,PM_REMOVE
))
455 ::WinDispatchMsg( hab
, &qmsg
);
457 return TRUE
;//*** temporary?
460 wxFAIL_MSG("wnd==NULL !!!");
462 return FALSE
;//*** temporary?
466 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
473 * #if !defined(__WIN32__) || defined(__TWIN32__)
474 * HRSRC hResource = ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
477 * HRSRC hResource = ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
479 * HRSRC hResource = ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
483 * if (hResource == 0)
485 * HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
488 * wxChar *theText = (wxChar *)LockResource(hData);
492 s
= copystring(theText
);
497 void wxGetMousePosition( int* x
, int* y
)
500 GetCursorPos( & pt
);
505 // Return TRUE if we have a colour display
506 bool wxColourDisplay()
509 // TODO: use DosQueryDevCaps to figure it out
513 // Returns depth of screen
516 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
517 long lArray
[CAPS_COLOR_BITCOUNT
];
528 nPlanes
= (int)lArray
[CAPS_COLOR_PLANES
];
529 nBitsPerPixel
= (int)lArray
[CAPS_COLOR_BITCOUNT
];
530 nDepth
= nPlanes
* nBitsPerPixel
;
536 // Get size of display
537 void wxDisplaySize(int *width
, int *height
)
539 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
540 long lArray
[CAPS_HEIGHT
];
548 *pWidth
= (int)lArray
[CAPS_WIDTH
];
549 *pHeight
= (int)lArray
[CAPS_HEIGHT
];
554 bool wxDirExists(const wxString
& dir
)
556 // TODO: Control program file stuff
560 // ---------------------------------------------------------------------------
561 // window information functions
562 // ---------------------------------------------------------------------------
564 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
567 long len
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
568 ::WinQueryWindowText((HWND
)hWnd
, str
.GetWriteBuf((int)len
), len
);
574 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
578 int len
= 256; // some starting value
582 int count
= ::WinQueryClassName((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
587 // the class name might have been truncated, retry with larger
599 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
601 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);