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(
340 const wxString
& rSection
341 , const wxString
& rEntry
343 , const wxString
& rFile
348 wxSprintf(zBuf
, "%ld", lValue
);
349 return wxWriteResource( rSection
356 bool wxWriteResource(
357 const wxString
& rSection
358 , const wxString
& rEntry
360 , const wxString
& rFile
365 wxSprintf(zBuf
, "%d", lValue
);
366 return wxWriteResource( rSection
374 const wxString
& rSection
375 , const wxString
& rEntry
377 , const wxString
& rFile
382 static const wxChar zDefunkt
[] = _T("$$default");
386 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
389 ULONG n
= ::PrfQueryProfileString( hIni
390 ,(PSZ
)WXSTRINGCAST rSection
391 ,(PSZ
)WXSTRINGCAST rEntry
396 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
404 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
405 ,(PSZ
)WXSTRINGCAST rSection
406 ,(PSZ
)WXSTRINGCAST rEntry
411 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
416 *ppValue
= copystring(wxBuffer
);
421 const wxString
& rSection
422 , const wxString
& rEntry
424 , const wxString
& rFile
428 bool bSucc
= wxGetResource( rSection
436 *pValue
= (float)wxStrtod(zStr
, NULL
);
444 const wxString
& rSection
445 , const wxString
& rEntry
447 , const wxString
& rFile
451 bool bSucc
= wxGetResource( rSection
459 *pValue
= wxStrtol(zStr
, NULL
, 10);
467 const wxString
& rSection
468 , const wxString
& rEntry
470 , const wxString
& rFile
474 bool bSucc
= wxGetResource( rSection
482 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
488 #endif // wxUSE_RESOURCES
490 // ---------------------------------------------------------------------------
491 // helper functions for showing a "busy" cursor
492 // ---------------------------------------------------------------------------
494 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
495 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
496 static int gs_wxBusyCursorCount
= 0;
498 // Set the cursor to the busy cursor for all windows
499 void wxBeginBusyCursor(
503 if ( gs_wxBusyCursorCount
++ == 0 )
505 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
506 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
508 //else: nothing to do, already set
511 // Restore cursor to normal
512 void wxEndBusyCursor()
514 wxCHECK_RET( gs_wxBusyCursorCount
> 0
515 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
518 if (--gs_wxBusyCursorCount
== 0)
520 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
521 gs_wxBusyCursorOld
= 0;
525 // TRUE if we're between the above two calls
528 return (gs_wxBusyCursorCount
> 0);
531 // ---------------------------------------------------------------------------
532 const wxChar
* wxGetHomeDir(
536 wxString
& rStrDir
= *pStr
;
538 // OS/2 has no idea about home,
539 // so use the working directory instead?
541 // 256 was taken from os2def.h
543 # define MAX_PATH 256
549 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
551 return rStrDir
.c_str();
555 wxChar
* wxGetUserHome (
556 const wxString
& rUser
560 wxString
sUser1(rUser
);
562 if (sUser1
!= _T(""))
566 if (wxGetUserId( zTmp
567 ,sizeof(zTmp
)/sizeof(char)
570 // Guests belong in the temp dir
571 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
573 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
574 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
575 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
576 return *zHome
? zHome
: (wxChar
*)_T("\\");
578 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
582 if (sUser1
== _T(""))
583 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
585 wxStrcpy(wxBuffer
, zHome
);
586 Unix2DosFilename(wxBuffer
);
589 return NULL
; // No home known!
592 // Check whether this window wants to process messages, e.g. Stop button
593 // in long calculations.
594 bool wxCheckForInterrupt(
603 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
605 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
607 ::WinDispatchMsg(hab
, &vMsg
);
609 return TRUE
;//*** temporary?
613 wxFAIL_MSG(_T("pWnd==NULL !!!"));
614 return FALSE
;//*** temporary?
618 void wxGetMousePosition(
625 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
630 // Return TRUE if we have a colour display
631 bool wxColourDisplay()
637 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
638 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
639 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
640 return(lColors
> 1L);
643 // Returns depth of screen
652 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
653 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
654 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
655 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
657 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
658 DevCloseDC(hdcScreen
);
662 // Get size of display
671 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
672 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
673 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, (PLONG
)pWidth
);
674 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, (PLONG
)pHeight
);
675 DevCloseDC(hdcScreen
);
682 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
685 // ---------------------------------------------------------------------------
686 // window information functions
687 // ---------------------------------------------------------------------------
689 wxString WXDLLEXPORT
wxGetWindowText(
694 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
696 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
697 vStr
.UngetWriteBuf();
702 wxString WXDLLEXPORT
wxGetWindowClass(
707 int nLen
= 256; // some starting value
711 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
713 vStr
.UngetWriteBuf();
716 // the class name might have been truncated, retry with larger
728 WXWORD WXDLLEXPORT
wxGetWindowId(
732 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);