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 short nLevel
= 0;
69 unsigned char* zBuffer
;
70 unsigned short nBuffer
;
71 unsigned short* pnTotalAvail
;
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
109 // UPM procs return 0 on success
110 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
111 if (lrc
== 0) return TRUE
;
125 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
135 return((int)::DosKillProcess(0, (PID
)lPid
));
139 // Execute a program in an Interactive Shell
142 const wxString
& rCommand
147 if ((zShell
= wxGetenv(_T("COMSPEC"))) == NULL
)
148 zShell
= _T("\\CMD.EXE");
156 ,WXSTRINGCAST rCommand
159 wxStrcpy(zTmp
, zShell
);
161 return (wxExecute((wxChar
*)zTmp
, FALSE
) != 0);
164 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
165 long wxGetFreeMemory(
173 lMemFlags
= PAG_FREE
;
174 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
180 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
181 static bool inTimer
= FALSE
;
183 class wxSleepTimer
: public wxTimer
193 static wxTimer
* wxTheSleepTimer
= NULL
;
196 unsigned long ulMilliseconds
199 ::DosSleep(ulMilliseconds
);
206 ::DosSleep(1000 * nSecs
);
209 // Consume all events until no more left
215 // Output a debug mess., in a system dependent fashion.
217 const wxChar
* zFmt
...
221 static wxChar zBuffer
[512];
223 if (!wxTheApp
->GetWantDebugOutput())
226 sprintf(zBuffer
, zFmt
, vAp
) ;
230 // Non-fatal error: pop up message box and (possibly) continue
233 , const wxString
& rTitle
236 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
237 if (::WinMessageBox( HWND_DESKTOP
240 ,(PSZ
)WXSTRINGCAST rTitle
242 ,MB_ICONEXCLAMATION
| MB_YESNO
247 // Fatal error: pop up message box and abort
250 , const wxString
& rTitle
255 ulRc
= ::WinMessageBox( HWND_DESKTOP
262 DosExit(EXIT_PROCESS
, ulRc
);
268 DosBeep(1000,1000); // 1kHz during 1 sec.
271 // Chris Breeze 27/5/98: revised WIN32 code to
272 // detect WindowsNT correctly
278 ULONG ulSysInfo
[QSV_MAX
] = {0};
280 if (::DosQuerySysInfo( 1L
283 ,sizeof(ULONG
) * QSV_MAX
286 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
287 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
288 return wxWINDOWS_OS2
;
290 return wxWINDOWS
; // error if we get here, return generic value
293 // Reading and writing resources (eg WIN.INI, .Xdefaults)
295 bool wxWriteResource(
296 const wxString
& rSection
297 , const wxString
& rEntry
298 , const wxString
& rValue
299 , const wxString
& rFile
307 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
310 return (::PrfWriteProfileString( hIni
311 ,(PSZ
)WXSTRINGCAST rSection
312 ,(PSZ
)WXSTRINGCAST rEntry
313 ,(PSZ
)WXSTRINGCAST rValue
318 return (::PrfWriteProfileString( HINI_PROFILE
319 ,(PSZ
)WXSTRINGCAST rSection
320 ,(PSZ
)WXSTRINGCAST rEntry
321 ,(PSZ
)WXSTRINGCAST rValue
326 bool wxWriteResource(
327 const wxString
& rSection
328 , const wxString
& rEntry
330 , const wxString
& rFile
335 wxSprintf(zBuf
, "%.4f", fValue
);
336 return wxWriteResource( rSection
343 bool wxWriteResource(
344 const wxString
& rSection
345 , const wxString
& rEntry
347 , const wxString
& rFile
352 wxSprintf(zBuf
, "%ld", lValue
);
353 return wxWriteResource( rSection
360 bool wxWriteResource(
361 const wxString
& rSection
362 , const wxString
& rEntry
364 , const wxString
& rFile
369 wxSprintf(zBuf
, "%d", lValue
);
370 return wxWriteResource( rSection
378 const wxString
& rSection
379 , const wxString
& rEntry
381 , const wxString
& rFile
386 static const wxChar zDefunkt
[] = _T("$$default");
390 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
393 ULONG n
= ::PrfQueryProfileString( hIni
394 ,(PSZ
)WXSTRINGCAST rSection
395 ,(PSZ
)WXSTRINGCAST rEntry
400 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
408 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
409 ,(PSZ
)WXSTRINGCAST rSection
410 ,(PSZ
)WXSTRINGCAST rEntry
415 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
420 *ppValue
= copystring(wxBuffer
);
425 const wxString
& rSection
426 , const wxString
& rEntry
428 , const wxString
& rFile
432 bool bSucc
= wxGetResource( rSection
440 *pValue
= (float)wxStrtod(zStr
, NULL
);
448 const wxString
& rSection
449 , const wxString
& rEntry
451 , const wxString
& rFile
455 bool bSucc
= wxGetResource( rSection
463 *pValue
= wxStrtol(zStr
, NULL
, 10);
471 const wxString
& rSection
472 , const wxString
& rEntry
474 , const wxString
& rFile
478 bool bSucc
= wxGetResource( rSection
486 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
492 #endif // wxUSE_RESOURCES
494 // ---------------------------------------------------------------------------
495 // helper functions for showing a "busy" cursor
496 // ---------------------------------------------------------------------------
498 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
499 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
500 static int gs_wxBusyCursorCount
= 0;
502 // Set the cursor to the busy cursor for all windows
503 void wxBeginBusyCursor(
507 if ( gs_wxBusyCursorCount
++ == 0 )
509 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
510 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
512 //else: nothing to do, already set
515 // Restore cursor to normal
516 void wxEndBusyCursor()
518 wxCHECK_RET( gs_wxBusyCursorCount
> 0
519 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
522 if (--gs_wxBusyCursorCount
== 0)
524 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
525 gs_wxBusyCursorOld
= 0;
529 // TRUE if we're between the above two calls
532 return (gs_wxBusyCursorCount
> 0);
535 // ---------------------------------------------------------------------------
536 const wxChar
* wxGetHomeDir(
540 wxString
& rStrDir
= *pStr
;
542 // OS/2 has no idea about home,
543 // so use the working directory instead?
545 // 256 was taken from os2def.h
547 # define MAX_PATH 256
553 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
555 return rStrDir
.c_str();
559 wxChar
* wxGetUserHome (
560 const wxString
& rUser
564 wxString
sUser1(rUser
);
566 if (sUser1
!= _T(""))
570 if (wxGetUserId( zTmp
571 ,sizeof(zTmp
)/sizeof(char)
574 // Guests belong in the temp dir
575 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
577 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
578 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
579 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
580 return *zHome
? zHome
: (wxChar
*)_T("\\");
582 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
586 if (sUser1
== _T(""))
587 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
589 wxStrcpy(wxBuffer
, zHome
);
590 Unix2DosFilename(wxBuffer
);
593 return NULL
; // No home known!
596 // Check whether this window wants to process messages, e.g. Stop button
597 // in long calculations.
598 bool wxCheckForInterrupt(
607 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
609 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
611 ::WinDispatchMsg(hab
, &vMsg
);
613 return TRUE
;//*** temporary?
617 wxFAIL_MSG(_T("pWnd==NULL !!!"));
618 return FALSE
;//*** temporary?
622 void wxGetMousePosition(
629 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
634 // Return TRUE if we have a colour display
635 bool wxColourDisplay()
641 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
642 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
643 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
644 return(lColors
> 1L);
647 // Returns depth of screen
656 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
657 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
658 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
659 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
661 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
662 DevCloseDC(hdcScreen
);
666 // Get size of display
675 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
676 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
677 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, (PLONG
)pWidth
);
678 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, (PLONG
)pHeight
);
679 DevCloseDC(hdcScreen
);
686 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
689 // ---------------------------------------------------------------------------
690 // window information functions
691 // ---------------------------------------------------------------------------
693 wxString WXDLLEXPORT
wxGetWindowText(
698 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
700 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
701 vStr
.UngetWriteBuf();
706 wxString WXDLLEXPORT
wxGetWindowClass(
711 int nLen
= 256; // some starting value
715 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
717 vStr
.UngetWriteBuf();
720 // the class name might have been truncated, retry with larger
732 WXWORD WXDLLEXPORT
wxGetWindowId(
736 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);