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"
39 // already defined via nerror.h in app.h so undef them
73 static const wxChar WX_SECTION
[] = _T("wxWindows");
74 static const wxChar eHOSTNAME
[] = _T("HostName");
75 static const wxChar eUSERID
[] = _T("UserId");
76 static const wxChar eUSERNAME
[] = _T("UserName");
78 // For the following functions we SHOULD fill in support
79 // for Windows-NT (which I don't know) as I assume it begin
80 // a POSIX Unix (so claims MS) that it has some special
81 // functions beyond those provided by WinSock
83 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
92 unsigned long ulLevel
= 0;
93 unsigned char* zBuffer
= NULL
;
94 unsigned long ulBuffer
= 256;
95 unsigned long* pulTotalAvail
= NULL
;
97 NetBios32GetInfo( (const unsigned char*)zServer
98 ,(const unsigned char*)zComputer
104 strcpy(zBuf
, zServer
);
107 const wxChar
* zDefaultHost
= _T("noname");
109 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
111 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
120 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
121 zBuf
[nMaxSize
] = _T('\0');
123 return *zBuf
? TRUE
: FALSE
;
126 // Get user ID e.g. jacs
134 // UPM procs return 0 on success
135 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
136 if (lrc
== 0) return TRUE
;
151 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
161 return((int)::DosKillProcess(0, (PID
)lPid
));
165 // Execute a program in an Interactive Shell
168 const wxString
& rCommand
171 wxChar
* zShell
= _T("CMD.EXE");
174 STARTDATA SData
= {0};
175 PSZ PgmTitle
= "Command Shell";
179 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
182 SData
.Length
= sizeof(STARTDATA
);
183 SData
.Related
= SSF_RELATED_INDEPENDENT
;
184 SData
.FgBg
= SSF_FGBG_FORE
;
185 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
186 SData
.PgmTitle
= PgmTitle
;
187 SData
.PgmName
= zShell
;
189 sInputs
= "/C " + rCommand
;
190 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
192 SData
.Environment
= 0;
193 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
194 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
197 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
200 SData
.InitXSize
= 200;
201 SData
.InitYSize
= 140;
203 SData
.ObjectBuffer
= (char*)achObjBuf
;
204 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
206 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
207 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
212 ::DosGetInfoBlocks(&ptib
, &ppib
);
214 ::DosWaitChild( DCWA_PROCESS
224 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
225 long wxGetFreeMemory()
227 void* pMemptr
= NULL
;
232 lMemFlags
= PAG_FREE
;
233 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 bool wxGetEnv(const wxString
& var
, wxString
*value
)
245 // wxGetenv is defined as getenv()
246 wxChar
*p
= wxGetenv(var
);
258 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
260 #if defined(HAVE_SETENV)
261 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
262 : NULL
, 1 /* overwrite */) == 0;
263 #elif defined(HAVE_PUTENV)
264 wxString s
= variable
;
266 s
<< _T('=') << value
;
269 const char *p
= s
.mb_str();
271 // the string will be free()d by libc
272 char *buf
= (char *)malloc(strlen(p
) + 1);
275 return putenv(buf
) == 0;
276 #else // no way to set an env var
282 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
283 static bool inTimer
= FALSE
;
285 class wxSleepTimer
: public wxTimer
295 static wxTimer
* wxTheSleepTimer
= NULL
;
298 unsigned long ulMilliseconds
301 ::DosSleep(ulMilliseconds
/1000l);
308 ::DosSleep(1000 * nSecs
);
311 // Consume all events until no more left
317 // Output a debug mess., in a system dependent fashion.
319 const wxChar
* zFmt
...
323 static wxChar zBuffer
[512];
325 if (!wxTheApp
->GetWantDebugOutput())
328 sprintf(zBuffer
, zFmt
, vAp
) ;
332 // Non-fatal error: pop up message box and (possibly) continue
335 , const wxString
& rTitle
338 wxBuffer
= new wxChar
[256];
339 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
340 if (::WinMessageBox( HWND_DESKTOP
343 ,(PSZ
)WXSTRINGCAST rTitle
345 ,MB_ICONEXCLAMATION
| MB_YESNO
351 // Fatal error: pop up message box and abort
354 , const wxString
& rTitle
359 ulRc
= ::WinMessageBox( HWND_DESKTOP
366 DosExit(EXIT_PROCESS
, ulRc
);
372 DosBeep(1000,1000); // 1kHz during 1 sec.
375 // Chris Breeze 27/5/98: revised WIN32 code to
376 // detect WindowsNT correctly
382 ULONG ulSysInfo
[QSV_MAX
] = {0};
385 ulrc
= ::DosQuerySysInfo( 1L
388 ,sizeof(ULONG
) * QSV_MAX
392 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
393 *pMajorVsn
= *pMajorVsn
/10;
394 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
395 return wxWINDOWS_OS2
;
397 return wxWINDOWS
; // error if we get here, return generic value
400 // Reading and writing resources (eg WIN.INI, .Xdefaults)
402 bool wxWriteResource(
403 const wxString
& rSection
404 , const wxString
& rEntry
405 , const wxString
& rValue
406 , const wxString
& rFile
414 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
417 return (::PrfWriteProfileString( hIni
418 ,(PSZ
)WXSTRINGCAST rSection
419 ,(PSZ
)WXSTRINGCAST rEntry
420 ,(PSZ
)WXSTRINGCAST rValue
425 return (::PrfWriteProfileString( HINI_PROFILE
426 ,(PSZ
)WXSTRINGCAST rSection
427 ,(PSZ
)WXSTRINGCAST rEntry
428 ,(PSZ
)WXSTRINGCAST rValue
433 bool wxWriteResource(
434 const wxString
& rSection
435 , const wxString
& rEntry
437 , const wxString
& rFile
442 wxSprintf(zBuf
, "%.4f", fValue
);
443 return wxWriteResource( rSection
450 bool wxWriteResource(
451 const wxString
& rSection
452 , const wxString
& rEntry
454 , const wxString
& rFile
459 wxSprintf(zBuf
, "%ld", lValue
);
460 return wxWriteResource( rSection
467 bool wxWriteResource(
468 const wxString
& rSection
469 , const wxString
& rEntry
471 , const wxString
& rFile
476 wxSprintf(zBuf
, "%d", lValue
);
477 return wxWriteResource( rSection
485 const wxString
& rSection
486 , const wxString
& rEntry
488 , const wxString
& rFile
493 wxChar zDefunkt
[] = _T("$$default");
498 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
501 ULONG n
= ::PrfQueryProfileString( hIni
502 ,(PSZ
)WXSTRINGCAST rSection
503 ,(PSZ
)WXSTRINGCAST rEntry
510 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
519 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
520 ,(PSZ
)WXSTRINGCAST rSection
521 ,(PSZ
)WXSTRINGCAST rEntry
528 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
532 strcpy((char*)*ppValue
, zBuf
);
537 const wxString
& rSection
538 , const wxString
& rEntry
540 , const wxString
& rFile
545 zStr
= new wxChar
[1000];
546 bool bSucc
= wxGetResource( rSection
554 *pValue
= (float)wxStrtod(zStr
, NULL
);
566 const wxString
& rSection
567 , const wxString
& rEntry
569 , const wxString
& rFile
574 zStr
= new wxChar
[1000];
575 bool bSucc
= wxGetResource( rSection
583 *pValue
= wxStrtol(zStr
, NULL
, 10);
595 const wxString
& rSection
596 , const wxString
& rEntry
598 , const wxString
& rFile
603 zStr
= new wxChar
[1000];
604 bool bSucc
= wxGetResource( rSection
612 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
622 #endif // wxUSE_RESOURCES
624 // ---------------------------------------------------------------------------
625 // helper functions for showing a "busy" cursor
626 // ---------------------------------------------------------------------------
628 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
629 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
630 static int gs_wxBusyCursorCount
= 0;
632 // Set the cursor to the busy cursor for all windows
633 void wxBeginBusyCursor(
637 if ( gs_wxBusyCursorCount
++ == 0 )
639 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
640 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
642 //else: nothing to do, already set
645 // Restore cursor to normal
646 void wxEndBusyCursor()
648 wxCHECK_RET( gs_wxBusyCursorCount
> 0
649 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
652 if (--gs_wxBusyCursorCount
== 0)
654 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
655 gs_wxBusyCursorOld
= 0;
659 // TRUE if we're between the above two calls
662 return (gs_wxBusyCursorCount
> 0);
665 // ---------------------------------------------------------------------------
666 const wxChar
* wxGetHomeDir(
670 wxString
& rStrDir
= *pStr
;
672 // OS/2 has no idea about home,
673 // so use the working directory instead?
675 // 256 was taken from os2def.h
677 # define MAX_PATH 256
683 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
685 return rStrDir
.c_str();
689 wxChar
* wxGetUserHome (
690 const wxString
& rUser
694 wxString
sUser1(rUser
);
696 wxBuffer
= new wxChar
[256];
698 if (sUser1
!= _T(""))
702 if (wxGetUserId( zTmp
703 ,sizeof(zTmp
)/sizeof(char)
706 // Guests belong in the temp dir
707 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
709 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
710 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
711 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
713 return *zHome
? zHome
: (wxChar
*)_T("\\");
715 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
720 if (sUser1
== _T(""))
722 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
724 wxStrcpy(wxBuffer
, zHome
);
725 Unix2DosFilename(wxBuffer
);
726 wxStrcpy(zHome
, wxBuffer
);
732 return NULL
; // No home known!
735 // Check whether this window wants to process messages, e.g. Stop button
736 // in long calculations.
737 bool wxCheckForInterrupt(
745 HWND hwndFilter
= NULLHANDLE
;
746 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
748 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
750 ::WinDispatchMsg(hab
, &vMsg
);
752 return TRUE
;//*** temporary?
756 wxFAIL_MSG(_T("pWnd==NULL !!!"));
757 return FALSE
;//*** temporary?
761 void wxGetMousePosition(
768 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
773 // Return TRUE if we have a colour display
774 bool wxColourDisplay()
780 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
781 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
782 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
783 return(lColors
> 1L);
786 // Returns depth of screen
795 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
796 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
797 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
798 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
800 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
801 DevCloseDC(hdcScreen
);
805 // Get size of display
816 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
817 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
818 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, &lWidth
);
819 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, &lHeight
);
820 DevCloseDC(hdcScreen
);
821 *pWidth
= (int)lWidth
;
822 *pHeight
= (int)lHeight
;
825 void wxDisplaySizeMM(
833 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
834 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
837 ::DevQueryCaps( hdcScreen
838 ,CAPS_HORIZONTAL_RESOLUTION
843 ::DevQueryCaps( hdcScreen
844 ,CAPS_VERTICAL_RESOLUTION
850 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
852 // This is supposed to return desktop dimensions minus any window
853 // manager panels, menus, taskbars, etc. If there is a way to do that
854 // for this platform please fix this function, otherwise it defaults
855 // to the entire desktop.
858 wxDisplaySize(width
, height
);
866 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
869 // ---------------------------------------------------------------------------
870 // window information functions
871 // ---------------------------------------------------------------------------
873 wxString WXDLLEXPORT
wxGetWindowText(
878 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
880 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
881 vStr
.UngetWriteBuf();
886 wxString WXDLLEXPORT
wxGetWindowClass(
891 int nLen
= 256; // some starting value
895 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
897 vStr
.UngetWriteBuf();
900 // the class name might have been truncated, retry with larger
912 WXWORD WXDLLEXPORT
wxGetWindowId(
916 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);
919 wxString WXDLLEXPORT
wxPMErrorToStr(
926 // Remove the high order byte -- it is useless
928 vError
&= 0x0000ffff;
931 case PMERR_INVALID_HWND
:
932 sError
= wxT("Invalid window handle specified");
935 case PMERR_INVALID_FLAG
:
936 sError
= wxT("Invalid flag bit set");
939 case PMERR_NO_MSG_QUEUE
:
940 sError
= wxT("No message queue available");
943 case PMERR_INVALID_PARM
:
944 sError
= wxT("Parameter contained invalid data");
947 case PMERR_INVALID_PARAMETERS
:
948 sError
= wxT("Parameter value is out of range");
951 case PMERR_PARAMETER_OUT_OF_RANGE
:
952 sError
= wxT("Parameter value is out of range");
955 case PMERR_INVALID_INTEGER_ATOM
:
956 sError
= wxT("Not a valid atom");
959 case PMERR_INVALID_HATOMTBL
:
960 sError
= wxT("Atom table handle is invalid");
963 case PMERR_INVALID_ATOM_NAME
:
964 sError
= wxT("Not a valid atom name");
967 case PMERR_ATOM_NAME_NOT_FOUND
:
968 sError
= wxT("Valid name format, but cannot find name in atom table");
972 sError
= wxT("Unknown error");
975 } // end of wxPMErrorToStr