1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/cursor.h"
22 #include "wx/os2/private.h"
49 // In the WIN.INI file
50 static const wxChar WX_SECTION
[] = _T("wxWindows");
51 static const wxChar eHOSTNAME
[] = _T("HostName");
52 static const wxChar eUSERID
[] = _T("UserId");
53 static const wxChar eUSERNAME
[] = _T("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( (const unsigned char*)server
72 ,(const unsigned char*)computer
81 const wxChar
*default_host
= _T("noname");
83 if ((sysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
85 // GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
88 wxStrncpy(buf
, sysname
, maxSize
- 1);
89 buf
[maxSize
] = _T('\0');
91 return *buf
? TRUE
: FALSE
;
94 // Get user ID e.g. jacs
95 bool wxGetUserId(wxChar
*buf
, int maxSize
)
97 return(U32ELOCL((unsigned char*)buf
, (unsigned long *)&maxSize
));
100 bool wxGetUserName(wxChar
*buf
, int maxSize
)
103 wxGetUserId(buf
, maxSize
);
105 wxStrncpy(buf
, _T("Unknown User"), maxSize
);
110 int wxKill(long pid
, int sig
)
116 // Execute a program in an Interactive Shell
118 bool wxShell(const wxString
& command
)
121 if ((shell
= wxGetenv(_T("COMSPEC"))) == NULL
)
122 shell
= _T("\\CMD.EXE");
126 wxSprintf(tmp
, "%s /c %s", shell
, WXSTRINGCAST command
);
128 wxStrcpy(tmp
, shell
);
130 return (wxExecute((wxChar
*)tmp
, FALSE
) != 0);
133 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
134 long wxGetFreeMemory(void *memptr
)
140 lMemFlags
= PAG_FREE
;
141 rc
= ::DosQueryMem(memptr
, &lSize
, &lMemFlags
);
147 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
148 static bool inTimer
= FALSE
;
150 class wxSleepTimer
: public wxTimer
160 static wxTimer
*wxTheSleepTimer
= NULL
;
162 void wxUsleep(unsigned long milliseconds
)
164 ::DosSleep(milliseconds
);
167 void wxSleep(int nSecs
)
169 ::DosSleep( 1000*nSecs
);
172 // Consume all events until no more left
178 // Output a debug mess., in a system dependent fashion.
179 void wxDebugMsg(const wxChar
*fmt
...)
182 static wxChar buffer
[512];
184 if (!wxTheApp
->GetWantDebugOutput())
189 sprintf(buffer
,fmt
,ap
) ;
194 // Non-fatal error: pop up message box and (possibly) continue
195 void wxError(const wxString
& msg
, const wxString
& title
)
197 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST msg
);
198 if (::WinMessageBox( HWND_DESKTOP
201 ,(PSZ
)WXSTRINGCAST title
203 ,MB_ICONEXCLAMATION
| MB_YESNO
208 // Fatal error: pop up message box and abort
209 void wxFatalError(const wxString
& rMsg
, const wxString
& rTitle
)
213 rc
= ::WinMessageBox( HWND_DESKTOP
220 DosExit(EXIT_PROCESS
, rc
);
226 DosBeep(1000,1000); // 1kHz during 1 sec.
229 // Chris Breeze 27/5/98: revised WIN32 code to
230 // detect WindowsNT correctly
231 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
233 ULONG aulSysInfo
[QSV_MAX
] = {0};
235 if (DosQuerySysInfo( 1L
238 ,sizeof(ULONG
) * QSV_MAX
241 *majorVsn
= aulSysInfo
[QSV_VERSION_MAJOR
];
242 *minorVsn
= aulSysInfo
[QSV_VERSION_MINOR
];
243 return wxWINDOWS_OS2
;
245 return wxWINDOWS
; // error if we get here, return generic value
248 // Reading and writing resources (eg WIN.INI, .Xdefaults)
249 // TODO: Ability to read and write to an INI file
252 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
259 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST file
);
262 return (::PrfWriteProfileString( hIni
263 ,(PSZ
)WXSTRINGCAST section
264 ,(PSZ
)WXSTRINGCAST entry
265 ,(PSZ
)WXSTRINGCAST value
270 return (::PrfWriteProfileString( HINI_PROFILE
271 ,(PSZ
)WXSTRINGCAST section
272 ,(PSZ
)WXSTRINGCAST entry
273 ,(PSZ
)WXSTRINGCAST value
278 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
281 wxSprintf(buf
, "%.4f", value
);
282 return wxWriteResource(section
, entry
, buf
, file
);
285 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
288 wxSprintf(buf
, "%ld", value
);
289 return wxWriteResource(section
, entry
, buf
, file
);
292 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
295 wxSprintf(buf
, "%d", value
);
296 return wxWriteResource(section
, entry
, buf
, file
);
299 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
303 static const wxChar defunkt
[] = _T("$$default");
307 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST file
);
310 ULONG n
= ::PrfQueryProfileString( hIni
311 ,(PSZ
)WXSTRINGCAST section
312 ,(PSZ
)WXSTRINGCAST entry
317 if (n
== 0L || wxStrcmp(wxBuffer
, defunkt
) == 0)
325 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
326 ,(PSZ
)WXSTRINGCAST section
327 ,(PSZ
)WXSTRINGCAST entry
332 if (n
== 0L || wxStrcmp(wxBuffer
, defunkt
) == 0)
337 *value
= copystring(wxBuffer
);
341 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
344 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
347 *value
= (float)wxStrtod(s
, NULL
);
354 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
357 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
360 *value
= wxStrtol(s
, NULL
, 10);
367 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
370 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
373 *value
= (int)wxStrtol(s
, NULL
, 10);
379 #endif // wxUSE_RESOURCES
381 // ---------------------------------------------------------------------------
382 // helper functions for showing a "busy" cursor
383 // ---------------------------------------------------------------------------
385 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
386 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
387 static int gs_wxBusyCursorCount
= 0;
389 // Set the cursor to the busy cursor for all windows
390 void wxBeginBusyCursor(wxCursor
*cursor
)
392 if ( gs_wxBusyCursorCount
++ == 0 )
394 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
395 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
397 //else: nothing to do, already set
400 // Restore cursor to normal
401 void wxEndBusyCursor()
403 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
404 _T("no matching wxBeginBusyCursor() for wxEndBusyCursor()"));
406 if ( --gs_wxBusyCursorCount
== 0 )
408 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
409 gs_wxBusyCursorOld
= 0;
413 // TRUE if we're between the above two calls
416 return (gs_wxBusyCursorCount
> 0);
419 // ---------------------------------------------------------------------------
420 const wxChar
* wxGetHomeDir(wxString
*pstr
)
422 wxString
& strDir
= *pstr
;
424 // OS/2 has no idea about home,
425 // so use the working directory instead?
427 // 256 was taken from os2def.h
429 # define MAX_PATH 256
436 ::DosQueryCurrentDir( 0, DirName
, &DirLen
);
438 return strDir
.c_str();
442 wxChar
*wxGetUserHome (const wxString
& user
)
445 wxString
user1(user
);
447 if (user1
!= _T("")) {
449 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
450 // Guests belong in the temp dir
451 if (wxStricmp(tmp
, _T("annonymous")) == 0) {
452 if ((home
= wxGetenv(_T("TMP"))) != NULL
||
453 (home
= wxGetenv(_T("TMPDIR"))) != NULL
||
454 (home
= wxGetenv(_T("TEMP"))) != NULL
)
455 return *home
? home
: (wxChar
*)_T("\\");
457 if (wxStricmp(tmp
, WXSTRINGCAST user1
) == 0)
462 if ((home
= wxGetenv(_T("HOME"))) != NULL
)
464 wxStrcpy(wxBuffer
, home
);
465 Unix2DosFilename(wxBuffer
);
468 return NULL
; // No home known!
471 // Check whether this window wants to process messages, e.g. Stop button
472 // in long calculations.
473 bool wxCheckForInterrupt(wxWindow
*wnd
)
480 HWND win
= (HWND
) wnd
->GetHWND();
481 while(::WinPeekMsg(hab
,&msg
,hwndFilter
,0,0,PM_REMOVE
))
483 ::WinDispatchMsg( hab
, &msg
);
485 return TRUE
;//*** temporary?
488 wxFAIL_MSG(_T("wnd==NULL !!!"));
490 return FALSE
;//*** temporary?
494 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
501 * #if !defined(__WIN32__) || defined(__TWIN32__)
502 * HRSRC hResource = ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
505 * HRSRC hResource = ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
507 * HRSRC hResource = ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
511 * if (hResource == 0)
513 * HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
516 * wxChar *theText = (wxChar *)LockResource(hData);
520 * s = copystring(theText);
525 void wxGetMousePosition( int* x
, int* y
)
528 ::WinQueryPointerPos( HWND_DESKTOP
, & pt
);
533 // Return TRUE if we have a colour display
534 bool wxColourDisplay()
537 // TODO: use DosQueryDevCaps to figure it out
541 // Returns depth of screen
544 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
545 long lArray
[CAPS_COLOR_BITCOUNT
];
556 nPlanes
= (int)lArray
[CAPS_COLOR_PLANES
];
557 nBitsPerPixel
= (int)lArray
[CAPS_COLOR_BITCOUNT
];
558 nDepth
= nPlanes
* nBitsPerPixel
;
564 // Get size of display
565 void wxDisplaySize(int *width
, int *height
)
567 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
568 long lArray
[CAPS_HEIGHT
];
576 *width
= (int)lArray
[CAPS_WIDTH
];
577 *height
= (int)lArray
[CAPS_HEIGHT
];
582 bool wxDirExists(const wxString
& dir
)
584 // TODO: Control program file stuff
588 // ---------------------------------------------------------------------------
589 // window information functions
590 // ---------------------------------------------------------------------------
592 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
595 long len
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
596 ::WinQueryWindowText((HWND
)hWnd
, len
, str
.GetWriteBuf((int)len
));
602 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
606 int len
= 256; // some starting value
610 int count
= ::WinQueryClassName((HWND
)hWnd
, len
, str
.GetWriteBuf(len
));
615 // the class name might have been truncated, retry with larger
627 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
629 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);