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 static const wxChar WX_SECTION
[] = _T("wxWindows");
50 static const wxChar eHOSTNAME
[] = _T("HostName");
51 static const wxChar eUSERID
[] = _T("UserId");
52 static const wxChar eUSERNAME
[] = _T("UserName");
54 // For the following functions we SHOULD fill in support
55 // for Windows-NT (which I don't know) as I assume it begin
56 // a POSIX Unix (so claims MS) that it has some special
57 // functions beyond those provided by WinSock
59 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
68 unsigned long ulLevel
;
69 unsigned char* zBuffer
;
70 unsigned long ulBuffer
;
71 unsigned long* pulTotalAvail
;
73 NetBios32GetInfo( (const unsigned char*)zServer
74 ,(const unsigned char*)zComputer
80 strcpy(zBuf
, zServer
);
83 const wxChar
* zDefaultHost
= _T("noname");
85 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
87 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
96 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
97 zBuf
[nMaxSize
] = _T('\0');
99 return *zBuf
? TRUE
: FALSE
;
102 // Get user ID e.g. jacs
108 return(U32ELOCL((unsigned char*)zBuf
, (unsigned long *)&nMaxSize
));
121 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
131 return((int)::DosKillProcess(0, (PID
)lPid
));
135 // Execute a program in an Interactive Shell
138 const wxString
& rCommand
143 if ((zShell
= wxGetenv(_T("COMSPEC"))) == NULL
)
144 zShell
= _T("\\CMD.EXE");
152 ,WXSTRINGCAST rCommand
155 wxStrcpy(zTmp
, zShell
);
157 return (wxExecute((wxChar
*)zTmp
, FALSE
) != 0);
160 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
161 long wxGetFreeMemory(
169 lMemFlags
= PAG_FREE
;
170 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
176 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
177 static bool inTimer
= FALSE
;
179 class wxSleepTimer
: public wxTimer
189 static wxTimer
* wxTheSleepTimer
= NULL
;
192 unsigned long ulMilliseconds
195 ::DosSleep(ulMilliseconds
);
202 ::DosSleep(1000 * nSecs
);
205 // Consume all events until no more left
211 // Output a debug mess., in a system dependent fashion.
213 const wxChar
* zFmt
...
217 static wxChar zBuffer
[512];
219 if (!wxTheApp
->GetWantDebugOutput())
222 sprintf(zBuffer
, zFmt
, vAp
) ;
226 // Non-fatal error: pop up message box and (possibly) continue
229 , const wxString
& rTitle
232 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
233 if (::WinMessageBox( HWND_DESKTOP
236 ,(PSZ
)WXSTRINGCAST rTitle
238 ,MB_ICONEXCLAMATION
| MB_YESNO
243 // Fatal error: pop up message box and abort
246 , const wxString
& rTitle
251 ulRc
= ::WinMessageBox( HWND_DESKTOP
258 DosExit(EXIT_PROCESS
, ulRc
);
264 DosBeep(1000,1000); // 1kHz during 1 sec.
267 // Chris Breeze 27/5/98: revised WIN32 code to
268 // detect WindowsNT correctly
274 ULONG ulSysInfo
[QSV_MAX
] = {0};
276 if (::DosQuerySysInfo( 1L
279 ,sizeof(ULONG
) * QSV_MAX
282 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
283 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
284 return wxWINDOWS_OS2
;
286 return wxWINDOWS
; // error if we get here, return generic value
289 // Reading and writing resources (eg WIN.INI, .Xdefaults)
291 bool wxWriteResource(
292 const wxString
& rSection
293 , const wxString
& rEntry
294 , const wxString
& rValue
295 , const wxString
& rFile
303 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
306 return (::PrfWriteProfileString( hIni
307 ,(PSZ
)WXSTRINGCAST rSection
308 ,(PSZ
)WXSTRINGCAST rEntry
309 ,(PSZ
)WXSTRINGCAST rValue
314 return (::PrfWriteProfileString( HINI_PROFILE
315 ,(PSZ
)WXSTRINGCAST rSection
316 ,(PSZ
)WXSTRINGCAST rEntry
317 ,(PSZ
)WXSTRINGCAST rValue
322 bool wxWriteResource(
323 const wxString
& rSection
324 , const wxString
& rEntry
326 , const wxString
& rFile
331 wxSprintf(zBuf
, "%.4f", fValue
);
332 return wxWriteResource( rSection
339 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
342 wxSprintf(buf
, "%ld", value
);
343 return wxWriteResource(section
, entry
, buf
, file
);
346 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
349 wxSprintf(buf
, "%d", value
);
350 return wxWriteResource(section
, entry
, buf
, file
);
353 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
357 static const wxChar defunkt
[] = _T("$$default");
361 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST file
);
364 ULONG n
= ::PrfQueryProfileString( hIni
365 ,(PSZ
)WXSTRINGCAST section
366 ,(PSZ
)WXSTRINGCAST entry
371 if (n
== 0L || wxStrcmp(wxBuffer
, defunkt
) == 0)
379 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
380 ,(PSZ
)WXSTRINGCAST section
381 ,(PSZ
)WXSTRINGCAST entry
386 if (n
== 0L || wxStrcmp(wxBuffer
, defunkt
) == 0)
391 *value
= copystring(wxBuffer
);
395 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
398 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
401 *value
= (float)wxStrtod(s
, NULL
);
408 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
411 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
414 *value
= wxStrtol(s
, NULL
, 10);
421 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
424 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
427 *value
= (int)wxStrtol(s
, NULL
, 10);
433 #endif // wxUSE_RESOURCES
435 // ---------------------------------------------------------------------------
436 // helper functions for showing a "busy" cursor
437 // ---------------------------------------------------------------------------
439 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
440 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
441 static int gs_wxBusyCursorCount
= 0;
443 // Set the cursor to the busy cursor for all windows
444 void wxBeginBusyCursor(wxCursor
*cursor
)
446 if ( gs_wxBusyCursorCount
++ == 0 )
448 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
449 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
451 //else: nothing to do, already set
454 // Restore cursor to normal
455 void wxEndBusyCursor()
457 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
458 _T("no matching wxBeginBusyCursor() for wxEndBusyCursor()"));
460 if ( --gs_wxBusyCursorCount
== 0 )
462 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
463 gs_wxBusyCursorOld
= 0;
467 // TRUE if we're between the above two calls
470 return (gs_wxBusyCursorCount
> 0);
473 // ---------------------------------------------------------------------------
474 const wxChar
* wxGetHomeDir(wxString
*pstr
)
476 wxString
& strDir
= *pstr
;
478 // OS/2 has no idea about home,
479 // so use the working directory instead?
481 // 256 was taken from os2def.h
483 # define MAX_PATH 256
490 ::DosQueryCurrentDir( 0, DirName
, &DirLen
);
492 return strDir
.c_str();
496 wxChar
*wxGetUserHome (const wxString
& user
)
499 wxString
user1(user
);
501 if (user1
!= _T("")) {
503 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
504 // Guests belong in the temp dir
505 if (wxStricmp(tmp
, _T("annonymous")) == 0) {
506 if ((home
= wxGetenv(_T("TMP"))) != NULL
||
507 (home
= wxGetenv(_T("TMPDIR"))) != NULL
||
508 (home
= wxGetenv(_T("TEMP"))) != NULL
)
509 return *home
? home
: (wxChar
*)_T("\\");
511 if (wxStricmp(tmp
, WXSTRINGCAST user1
) == 0)
516 if ((home
= wxGetenv(_T("HOME"))) != NULL
)
518 wxStrcpy(wxBuffer
, home
);
519 Unix2DosFilename(wxBuffer
);
522 return NULL
; // No home known!
525 // Check whether this window wants to process messages, e.g. Stop button
526 // in long calculations.
527 bool wxCheckForInterrupt(wxWindow
*wnd
)
534 HWND win
= (HWND
) wnd
->GetHWND();
535 while(::WinPeekMsg(hab
,&msg
,hwndFilter
,0,0,PM_REMOVE
))
537 ::WinDispatchMsg( hab
, &msg
);
539 return TRUE
;//*** temporary?
542 wxFAIL_MSG(_T("wnd==NULL !!!"));
544 return FALSE
;//*** temporary?
548 void wxGetMousePosition( int* x
, int* y
)
551 ::WinQueryPointerPos( HWND_DESKTOP
, & pt
);
556 // Return TRUE if we have a colour display
557 bool wxColourDisplay()
560 // TODO: use DosQueryDevCaps to figure it out
564 // Returns depth of screen
567 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
568 long lArray
[CAPS_COLOR_BITCOUNT
];
579 nPlanes
= (int)lArray
[CAPS_COLOR_PLANES
];
580 nBitsPerPixel
= (int)lArray
[CAPS_COLOR_BITCOUNT
];
581 nDepth
= nPlanes
* nBitsPerPixel
;
587 // Get size of display
588 void wxDisplaySize(int *width
, int *height
)
590 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
591 long lArray
[CAPS_HEIGHT
];
599 *width
= (int)lArray
[CAPS_WIDTH
];
600 *height
= (int)lArray
[CAPS_HEIGHT
];
605 bool wxDirExists(const wxString
& dir
)
607 // TODO: Control program file stuff
611 // ---------------------------------------------------------------------------
612 // window information functions
613 // ---------------------------------------------------------------------------
615 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
618 long len
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
619 ::WinQueryWindowText((HWND
)hWnd
, len
, str
.GetWriteBuf((int)len
));
625 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
629 int len
= 256; // some starting value
633 int count
= ::WinQueryClassName((HWND
)hWnd
, len
, str
.GetWriteBuf(len
));
638 // the class name might have been truncated, retry with larger
650 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
652 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);