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"
54 static const wxChar WX_SECTION
[] = _T("wxWindows");
55 static const wxChar eHOSTNAME
[] = _T("HostName");
56 static const wxChar eUSERID
[] = _T("UserId");
57 static const wxChar eUSERNAME
[] = _T("UserName");
59 // For the following functions we SHOULD fill in support
60 // for Windows-NT (which I don't know) as I assume it begin
61 // a POSIX Unix (so claims MS) that it has some special
62 // functions beyond those provided by WinSock
64 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
73 unsigned long ulLevel
= 0;
74 unsigned char* zBuffer
= NULL
;
75 unsigned long ulBuffer
= 256;
76 unsigned long* pulTotalAvail
= NULL
;
78 NetBios32GetInfo( (const unsigned char*)zServer
79 ,(const unsigned char*)zComputer
85 strcpy(zBuf
, zServer
);
88 const wxChar
* zDefaultHost
= _T("noname");
90 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
92 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
101 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
102 zBuf
[nMaxSize
] = _T('\0');
104 return *zBuf
? TRUE
: FALSE
;
107 // Get user ID e.g. jacs
115 // UPM procs return 0 on success
116 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
117 if (lrc
== 0) return TRUE
;
132 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
142 return((int)::DosKillProcess(0, (PID
)lPid
));
146 // Execute a program in an Interactive Shell
149 const wxString
& rCommand
152 wxChar
* zShell
= _T("CMD.EXE");
155 STARTDATA SData
= {0};
156 PSZ PgmTitle
= "Command Shell";
160 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
163 SData
.Length
= sizeof(STARTDATA
);
164 SData
.Related
= SSF_RELATED_INDEPENDENT
;
165 SData
.FgBg
= SSF_FGBG_FORE
;
166 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
167 SData
.PgmTitle
= PgmTitle
;
168 SData
.PgmName
= zShell
;
170 sInputs
= "/C " + rCommand
;
171 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
173 SData
.Environment
= 0;
174 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
175 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
178 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
181 SData
.InitXSize
= 200;
182 SData
.InitYSize
= 140;
184 SData
.ObjectBuffer
= (char*)achObjBuf
;
185 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
187 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
188 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
193 ::DosGetInfoBlocks(&ptib
, &ppib
);
195 ::DosWaitChild( DCWA_PROCESS
205 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
206 long wxGetFreeMemory()
208 void* pMemptr
= NULL
;
213 lMemFlags
= PAG_FREE
;
214 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
220 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
221 static bool inTimer
= FALSE
;
223 class wxSleepTimer
: public wxTimer
233 static wxTimer
* wxTheSleepTimer
= NULL
;
236 unsigned long ulMilliseconds
239 ::DosSleep(ulMilliseconds
);
246 ::DosSleep(1000 * nSecs
);
249 // Consume all events until no more left
255 // Output a debug mess., in a system dependent fashion.
257 const wxChar
* zFmt
...
261 static wxChar zBuffer
[512];
263 if (!wxTheApp
->GetWantDebugOutput())
266 sprintf(zBuffer
, zFmt
, vAp
) ;
270 // Non-fatal error: pop up message box and (possibly) continue
273 , const wxString
& rTitle
276 wxBuffer
= new wxChar
[256];
277 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
278 if (::WinMessageBox( HWND_DESKTOP
281 ,(PSZ
)WXSTRINGCAST rTitle
283 ,MB_ICONEXCLAMATION
| MB_YESNO
289 // Fatal error: pop up message box and abort
292 , const wxString
& rTitle
297 ulRc
= ::WinMessageBox( HWND_DESKTOP
304 DosExit(EXIT_PROCESS
, ulRc
);
310 DosBeep(1000,1000); // 1kHz during 1 sec.
313 // Chris Breeze 27/5/98: revised WIN32 code to
314 // detect WindowsNT correctly
320 ULONG ulSysInfo
[QSV_MAX
] = {0};
323 ulrc
= ::DosQuerySysInfo( 1L
326 ,sizeof(ULONG
) * QSV_MAX
330 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
331 *pMajorVsn
= *pMajorVsn
/10;
332 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
333 return wxWINDOWS_OS2
;
335 return wxWINDOWS
; // error if we get here, return generic value
338 // Reading and writing resources (eg WIN.INI, .Xdefaults)
340 bool wxWriteResource(
341 const wxString
& rSection
342 , const wxString
& rEntry
343 , const wxString
& rValue
344 , const wxString
& rFile
352 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
355 return (::PrfWriteProfileString( hIni
356 ,(PSZ
)WXSTRINGCAST rSection
357 ,(PSZ
)WXSTRINGCAST rEntry
358 ,(PSZ
)WXSTRINGCAST rValue
363 return (::PrfWriteProfileString( HINI_PROFILE
364 ,(PSZ
)WXSTRINGCAST rSection
365 ,(PSZ
)WXSTRINGCAST rEntry
366 ,(PSZ
)WXSTRINGCAST rValue
371 bool wxWriteResource(
372 const wxString
& rSection
373 , const wxString
& rEntry
375 , const wxString
& rFile
380 wxSprintf(zBuf
, "%.4f", fValue
);
381 return wxWriteResource( rSection
388 bool wxWriteResource(
389 const wxString
& rSection
390 , const wxString
& rEntry
392 , const wxString
& rFile
397 wxSprintf(zBuf
, "%ld", lValue
);
398 return wxWriteResource( rSection
405 bool wxWriteResource(
406 const wxString
& rSection
407 , const wxString
& rEntry
409 , const wxString
& rFile
414 wxSprintf(zBuf
, "%d", lValue
);
415 return wxWriteResource( rSection
423 const wxString
& rSection
424 , const wxString
& rEntry
426 , const wxString
& rFile
431 wxChar zDefunkt
[] = _T("$$default");
436 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
439 ULONG n
= ::PrfQueryProfileString( hIni
440 ,(PSZ
)WXSTRINGCAST rSection
441 ,(PSZ
)WXSTRINGCAST rEntry
448 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
457 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
458 ,(PSZ
)WXSTRINGCAST rSection
459 ,(PSZ
)WXSTRINGCAST rEntry
466 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
470 strcpy((char*)*ppValue
, zBuf
);
475 const wxString
& rSection
476 , const wxString
& rEntry
478 , const wxString
& rFile
483 zStr
= new wxChar
[1000];
484 bool bSucc
= wxGetResource( rSection
492 *pValue
= (float)wxStrtod(zStr
, NULL
);
504 const wxString
& rSection
505 , const wxString
& rEntry
507 , const wxString
& rFile
512 zStr
= new wxChar
[1000];
513 bool bSucc
= wxGetResource( rSection
521 *pValue
= wxStrtol(zStr
, NULL
, 10);
533 const wxString
& rSection
534 , const wxString
& rEntry
536 , const wxString
& rFile
541 zStr
= new wxChar
[1000];
542 bool bSucc
= wxGetResource( rSection
550 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
560 #endif // wxUSE_RESOURCES
562 // ---------------------------------------------------------------------------
563 // helper functions for showing a "busy" cursor
564 // ---------------------------------------------------------------------------
566 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
567 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
568 static int gs_wxBusyCursorCount
= 0;
570 // Set the cursor to the busy cursor for all windows
571 void wxBeginBusyCursor(
575 if ( gs_wxBusyCursorCount
++ == 0 )
577 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
578 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
580 //else: nothing to do, already set
583 // Restore cursor to normal
584 void wxEndBusyCursor()
586 wxCHECK_RET( gs_wxBusyCursorCount
> 0
587 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
590 if (--gs_wxBusyCursorCount
== 0)
592 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
593 gs_wxBusyCursorOld
= 0;
597 // TRUE if we're between the above two calls
600 return (gs_wxBusyCursorCount
> 0);
603 // ---------------------------------------------------------------------------
604 const wxChar
* wxGetHomeDir(
608 wxString
& rStrDir
= *pStr
;
610 // OS/2 has no idea about home,
611 // so use the working directory instead?
613 // 256 was taken from os2def.h
615 # define MAX_PATH 256
621 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
623 return rStrDir
.c_str();
627 wxChar
* wxGetUserHome (
628 const wxString
& rUser
632 wxString
sUser1(rUser
);
634 wxBuffer
= new wxChar
[256];
636 if (sUser1
!= _T(""))
640 if (wxGetUserId( zTmp
641 ,sizeof(zTmp
)/sizeof(char)
644 // Guests belong in the temp dir
645 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
647 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
648 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
649 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
651 return *zHome
? zHome
: (wxChar
*)_T("\\");
653 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
658 if (sUser1
== _T(""))
660 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
662 wxStrcpy(wxBuffer
, zHome
);
663 Unix2DosFilename(wxBuffer
);
664 wxStrcpy(zHome
, wxBuffer
);
670 return NULL
; // No home known!
673 // Check whether this window wants to process messages, e.g. Stop button
674 // in long calculations.
675 bool wxCheckForInterrupt(
683 HWND hwndFilter
= NULLHANDLE
;
684 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
686 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
688 ::WinDispatchMsg(hab
, &vMsg
);
690 return TRUE
;//*** temporary?
694 wxFAIL_MSG(_T("pWnd==NULL !!!"));
695 return FALSE
;//*** temporary?
699 void wxGetMousePosition(
706 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
711 // Return TRUE if we have a colour display
712 bool wxColourDisplay()
718 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
719 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
720 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
721 return(lColors
> 1L);
724 // Returns depth of screen
733 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
734 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
735 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
736 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
738 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
739 DevCloseDC(hdcScreen
);
743 // Get size of display
754 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
755 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
756 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, &lWidth
);
757 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, &lHeight
);
758 DevCloseDC(hdcScreen
);
759 *pWidth
= (int)lWidth
;
760 *pHeight
= (int)lHeight
;
767 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
770 // ---------------------------------------------------------------------------
771 // window information functions
772 // ---------------------------------------------------------------------------
774 wxString WXDLLEXPORT
wxGetWindowText(
779 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
781 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
782 vStr
.UngetWriteBuf();
787 wxString WXDLLEXPORT
wxGetWindowClass(
792 int nLen
= 256; // some starting value
796 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
798 vStr
.UngetWriteBuf();
801 // the class name might have been truncated, retry with larger
813 WXWORD WXDLLEXPORT
wxGetWindowId(
817 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);
820 wxString WXDLLEXPORT
wxPMErrorToStr(
827 // Remove the high order byte -- it is useless
829 vError
&= 0x0000ffff;
832 case PMERR_INVALID_HWND
:
833 sError
= wxT("Invalid window handle specified");
836 case PMERR_INVALID_FLAG
:
837 sError
= wxT("Invalid flag bit set");
840 case PMERR_NO_MSG_QUEUE
:
841 sError
= wxT("No message queue available");
844 case PMERR_INVALID_PARM
:
845 sError
= wxT("Parameter contained invalid data");
848 case PMERR_INVALID_PARAMETERS
:
849 sError
= wxT("Parameter value is out of range");
852 case PMERR_PARAMETER_OUT_OF_RANGE
:
853 sError
= wxT("Parameter value is out of range");
856 case PMERR_INVALID_INTEGER_ATOM
:
857 sError
= wxT("Not a valid atom");
860 case PMERR_INVALID_HATOMTBL
:
861 sError
= wxT("Atom table handle is invalid");
864 case PMERR_INVALID_ATOM_NAME
:
865 sError
= wxT("Not a valid atom name");
868 case PMERR_ATOM_NAME_NOT_FOUND
:
869 sError
= wxT("Valid name format, but cannot find name in atom table");
873 sError
= wxT("Unknown error");
876 } // end of wxPMErrorToStr