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"
51 static const wxChar WX_SECTION
[] = _T("wxWindows");
52 static const wxChar eHOSTNAME
[] = _T("HostName");
53 static const wxChar eUSERID
[] = _T("UserId");
54 static const wxChar eUSERNAME
[] = _T("UserName");
56 // For the following functions we SHOULD fill in support
57 // for Windows-NT (which I don't know) as I assume it begin
58 // a POSIX Unix (so claims MS) that it has some special
59 // functions beyond those provided by WinSock
61 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
70 unsigned long ulLevel
= 0;
71 unsigned char* zBuffer
= NULL
;
72 unsigned long ulBuffer
= 256;
73 unsigned long* pulTotalAvail
= NULL
;
75 NetBios32GetInfo( (const unsigned char*)zServer
76 ,(const unsigned char*)zComputer
82 strcpy(zBuf
, zServer
);
85 const wxChar
* zDefaultHost
= _T("noname");
87 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
89 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
98 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
99 zBuf
[nMaxSize
] = _T('\0');
101 return *zBuf
? TRUE
: FALSE
;
104 // Get user ID e.g. jacs
112 // UPM procs return 0 on success
113 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
114 if (lrc
== 0) return TRUE
;
129 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
139 return((int)::DosKillProcess(0, (PID
)lPid
));
143 // Execute a program in an Interactive Shell
146 const wxString
& rCommand
149 wxChar
* zShell
= _T("CMD.EXE");
152 STARTDATA SData
= {0};
153 PSZ PgmTitle
= "Command Shell";
157 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
160 SData
.Length
= sizeof(STARTDATA
);
161 SData
.Related
= SSF_RELATED_INDEPENDENT
;
162 SData
.FgBg
= SSF_FGBG_FORE
;
163 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
164 SData
.PgmTitle
= PgmTitle
;
165 SData
.PgmName
= zShell
;
167 sInputs
= "/C " + rCommand
;
168 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
170 SData
.Environment
= 0;
171 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
172 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
175 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
178 SData
.InitXSize
= 200;
179 SData
.InitYSize
= 140;
181 SData
.ObjectBuffer
= (char*)achObjBuf
;
182 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
184 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
185 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
190 ::DosGetInfoBlocks(&ptib
, &ppib
);
192 ::DosWaitChild( DCWA_PROCESS
202 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
203 long wxGetFreeMemory()
205 void* pMemptr
= NULL
;
210 lMemFlags
= PAG_FREE
;
211 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
217 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
218 static bool inTimer
= FALSE
;
220 class wxSleepTimer
: public wxTimer
230 static wxTimer
* wxTheSleepTimer
= NULL
;
233 unsigned long ulMilliseconds
236 ::DosSleep(ulMilliseconds
/1000l);
243 ::DosSleep(1000 * nSecs
);
246 // Consume all events until no more left
252 // Output a debug mess., in a system dependent fashion.
254 const wxChar
* zFmt
...
258 static wxChar zBuffer
[512];
260 if (!wxTheApp
->GetWantDebugOutput())
263 sprintf(zBuffer
, zFmt
, vAp
) ;
267 // Non-fatal error: pop up message box and (possibly) continue
270 , const wxString
& rTitle
273 wxBuffer
= new wxChar
[256];
274 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
275 if (::WinMessageBox( HWND_DESKTOP
278 ,(PSZ
)WXSTRINGCAST rTitle
280 ,MB_ICONEXCLAMATION
| MB_YESNO
286 // Fatal error: pop up message box and abort
289 , const wxString
& rTitle
294 ulRc
= ::WinMessageBox( HWND_DESKTOP
301 DosExit(EXIT_PROCESS
, ulRc
);
307 DosBeep(1000,1000); // 1kHz during 1 sec.
310 // Chris Breeze 27/5/98: revised WIN32 code to
311 // detect WindowsNT correctly
317 ULONG ulSysInfo
[QSV_MAX
] = {0};
320 ulrc
= ::DosQuerySysInfo( 1L
323 ,sizeof(ULONG
) * QSV_MAX
327 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
328 *pMajorVsn
= *pMajorVsn
/10;
329 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
330 return wxWINDOWS_OS2
;
332 return wxWINDOWS
; // error if we get here, return generic value
335 // Reading and writing resources (eg WIN.INI, .Xdefaults)
337 bool wxWriteResource(
338 const wxString
& rSection
339 , const wxString
& rEntry
340 , const wxString
& rValue
341 , const wxString
& rFile
349 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
352 return (::PrfWriteProfileString( hIni
353 ,(PSZ
)WXSTRINGCAST rSection
354 ,(PSZ
)WXSTRINGCAST rEntry
355 ,(PSZ
)WXSTRINGCAST rValue
360 return (::PrfWriteProfileString( HINI_PROFILE
361 ,(PSZ
)WXSTRINGCAST rSection
362 ,(PSZ
)WXSTRINGCAST rEntry
363 ,(PSZ
)WXSTRINGCAST rValue
368 bool wxWriteResource(
369 const wxString
& rSection
370 , const wxString
& rEntry
372 , const wxString
& rFile
377 wxSprintf(zBuf
, "%.4f", fValue
);
378 return wxWriteResource( rSection
385 bool wxWriteResource(
386 const wxString
& rSection
387 , const wxString
& rEntry
389 , const wxString
& rFile
394 wxSprintf(zBuf
, "%ld", lValue
);
395 return wxWriteResource( rSection
402 bool wxWriteResource(
403 const wxString
& rSection
404 , const wxString
& rEntry
406 , const wxString
& rFile
411 wxSprintf(zBuf
, "%d", lValue
);
412 return wxWriteResource( rSection
420 const wxString
& rSection
421 , const wxString
& rEntry
423 , const wxString
& rFile
428 wxChar zDefunkt
[] = _T("$$default");
433 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
436 ULONG n
= ::PrfQueryProfileString( hIni
437 ,(PSZ
)WXSTRINGCAST rSection
438 ,(PSZ
)WXSTRINGCAST rEntry
445 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
454 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
455 ,(PSZ
)WXSTRINGCAST rSection
456 ,(PSZ
)WXSTRINGCAST rEntry
463 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
467 strcpy((char*)*ppValue
, zBuf
);
472 const wxString
& rSection
473 , const wxString
& rEntry
475 , const wxString
& rFile
480 zStr
= new wxChar
[1000];
481 bool bSucc
= wxGetResource( rSection
489 *pValue
= (float)wxStrtod(zStr
, NULL
);
501 const wxString
& rSection
502 , const wxString
& rEntry
504 , const wxString
& rFile
509 zStr
= new wxChar
[1000];
510 bool bSucc
= wxGetResource( rSection
518 *pValue
= wxStrtol(zStr
, NULL
, 10);
530 const wxString
& rSection
531 , const wxString
& rEntry
533 , const wxString
& rFile
538 zStr
= new wxChar
[1000];
539 bool bSucc
= wxGetResource( rSection
547 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
557 #endif // wxUSE_RESOURCES
559 // ---------------------------------------------------------------------------
560 // helper functions for showing a "busy" cursor
561 // ---------------------------------------------------------------------------
563 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
564 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
565 static int gs_wxBusyCursorCount
= 0;
567 // Set the cursor to the busy cursor for all windows
568 void wxBeginBusyCursor(
572 if ( gs_wxBusyCursorCount
++ == 0 )
574 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
575 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
577 //else: nothing to do, already set
580 // Restore cursor to normal
581 void wxEndBusyCursor()
583 wxCHECK_RET( gs_wxBusyCursorCount
> 0
584 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
587 if (--gs_wxBusyCursorCount
== 0)
589 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
590 gs_wxBusyCursorOld
= 0;
594 // TRUE if we're between the above two calls
597 return (gs_wxBusyCursorCount
> 0);
600 // ---------------------------------------------------------------------------
601 const wxChar
* wxGetHomeDir(
605 wxString
& rStrDir
= *pStr
;
607 // OS/2 has no idea about home,
608 // so use the working directory instead?
610 // 256 was taken from os2def.h
612 # define MAX_PATH 256
618 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
620 return rStrDir
.c_str();
624 wxChar
* wxGetUserHome (
625 const wxString
& rUser
629 wxString
sUser1(rUser
);
631 wxBuffer
= new wxChar
[256];
633 if (sUser1
!= _T(""))
637 if (wxGetUserId( zTmp
638 ,sizeof(zTmp
)/sizeof(char)
641 // Guests belong in the temp dir
642 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
644 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
645 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
646 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
648 return *zHome
? zHome
: (wxChar
*)_T("\\");
650 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
655 if (sUser1
== _T(""))
657 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
659 wxStrcpy(wxBuffer
, zHome
);
660 Unix2DosFilename(wxBuffer
);
661 wxStrcpy(zHome
, wxBuffer
);
667 return NULL
; // No home known!
670 // Check whether this window wants to process messages, e.g. Stop button
671 // in long calculations.
672 bool wxCheckForInterrupt(
680 HWND hwndFilter
= NULLHANDLE
;
681 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
683 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
685 ::WinDispatchMsg(hab
, &vMsg
);
687 return TRUE
;//*** temporary?
691 wxFAIL_MSG(_T("pWnd==NULL !!!"));
692 return FALSE
;//*** temporary?
696 void wxGetMousePosition(
703 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
708 // Return TRUE if we have a colour display
709 bool wxColourDisplay()
715 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
716 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
717 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
718 return(lColors
> 1L);
721 // Returns depth of screen
730 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
731 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
732 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
733 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
735 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
736 DevCloseDC(hdcScreen
);
740 // Get size of display
751 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
752 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
753 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, &lWidth
);
754 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, &lHeight
);
755 DevCloseDC(hdcScreen
);
756 *pWidth
= (int)lWidth
;
757 *pHeight
= (int)lHeight
;
764 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
767 // ---------------------------------------------------------------------------
768 // window information functions
769 // ---------------------------------------------------------------------------
771 wxString WXDLLEXPORT
wxGetWindowText(
776 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
778 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
779 vStr
.UngetWriteBuf();
784 wxString WXDLLEXPORT
wxGetWindowClass(
789 int nLen
= 256; // some starting value
793 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
795 vStr
.UngetWriteBuf();
798 // the class name might have been truncated, retry with larger
810 WXWORD WXDLLEXPORT
wxGetWindowId(
814 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);
817 wxString WXDLLEXPORT
wxPMErrorToStr(
824 // Remove the high order byte -- it is useless
826 vError
&= 0x0000ffff;
829 case PMERR_INVALID_HWND
:
830 sError
= wxT("Invalid window handle specified");
833 case PMERR_INVALID_FLAG
:
834 sError
= wxT("Invalid flag bit set");
837 case PMERR_NO_MSG_QUEUE
:
838 sError
= wxT("No message queue available");
841 case PMERR_INVALID_PARM
:
842 sError
= wxT("Parameter contained invalid data");
845 case PMERR_INVALID_PARAMETERS
:
846 sError
= wxT("Parameter value is out of range");
849 case PMERR_PARAMETER_OUT_OF_RANGE
:
850 sError
= wxT("Parameter value is out of range");
853 case PMERR_INVALID_INTEGER_ATOM
:
854 sError
= wxT("Not a valid atom");
857 case PMERR_INVALID_HATOMTBL
:
858 sError
= wxT("Atom table handle is invalid");
861 case PMERR_INVALID_ATOM_NAME
:
862 sError
= wxT("Not a valid atom name");
865 case PMERR_ATOM_NAME_NOT_FOUND
:
866 sError
= wxT("Valid name format, but cannot find name in atom table");
870 sError
= wxT("Unknown error");
873 } // end of wxPMErrorToStr