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
= 0;
69 unsigned char* zBuffer
= NULL
;
70 unsigned long ulBuffer
= 256;
71 unsigned long* pulTotalAvail
= NULL
;
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
110 // UPM procs return 0 on success
111 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
112 if (lrc
== 0) return TRUE
;
127 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
137 return((int)::DosKillProcess(0, (PID
)lPid
));
141 // Execute a program in an Interactive Shell
144 const wxString
& rCommand
147 wxChar
* zShell
= _T("CMD.EXE");
150 STARTDATA SData
= {0};
151 PSZ PgmTitle
= "Command Shell";
155 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
158 SData
.Length
= sizeof(STARTDATA
);
159 SData
.Related
= SSF_RELATED_INDEPENDENT
;
160 SData
.FgBg
= SSF_FGBG_FORE
;
161 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
162 SData
.PgmTitle
= PgmTitle
;
163 SData
.PgmName
= zShell
;
165 sInputs
= "/C " + rCommand
;
166 SData
.PgmInputs
= (BYTE
*)sInputs
.c_str();
168 SData
.Environment
= 0;
169 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
170 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
173 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
176 SData
.InitXSize
= 200;
177 SData
.InitYSize
= 140;
179 SData
.ObjectBuffer
= (char*)achObjBuf
;
180 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
182 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
183 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
188 ::DosGetInfoBlocks(&ptib
, &ppib
);
190 ::DosWaitChild( DCWA_PROCESS
200 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
201 long wxGetFreeMemory()
203 void* pMemptr
= NULL
;
208 lMemFlags
= PAG_FREE
;
209 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 bool wxGetEnv(const wxString
& var
, wxString
*value
)
221 // wxGetenv is defined as getenv()
222 wxChar
*p
= wxGetenv(var
);
234 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
236 #if defined(HAVE_SETENV)
237 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
238 : NULL
, 1 /* overwrite */) == 0;
239 #elif defined(HAVE_PUTENV)
240 wxString s
= variable
;
242 s
<< _T('=') << value
;
245 const char *p
= s
.mb_str();
247 // the string will be free()d by libc
248 char *buf
= (char *)malloc(strlen(p
) + 1);
251 return putenv(buf
) == 0;
252 #else // no way to set an env var
258 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
259 static bool inTimer
= FALSE
;
261 class wxSleepTimer
: public wxTimer
271 static wxTimer
* wxTheSleepTimer
= NULL
;
274 unsigned long ulMilliseconds
277 ::DosSleep(ulMilliseconds
/1000l);
284 ::DosSleep(1000 * nSecs
);
287 // Consume all events until no more left
293 // Output a debug mess., in a system dependent fashion.
295 const wxChar
* zFmt
...
299 static wxChar zBuffer
[512];
301 if (!wxTheApp
->GetWantDebugOutput())
304 sprintf(zBuffer
, zFmt
, vAp
) ;
308 // Non-fatal error: pop up message box and (possibly) continue
311 , const wxString
& rTitle
314 wxBuffer
= new wxChar
[256];
315 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
316 if (::WinMessageBox( HWND_DESKTOP
319 ,(PSZ
)WXSTRINGCAST rTitle
321 ,MB_ICONEXCLAMATION
| MB_YESNO
327 // Fatal error: pop up message box and abort
330 , const wxString
& rTitle
335 ulRc
= ::WinMessageBox( HWND_DESKTOP
342 DosExit(EXIT_PROCESS
, ulRc
);
348 DosBeep(1000,1000); // 1kHz during 1 sec.
351 // Chris Breeze 27/5/98: revised WIN32 code to
352 // detect WindowsNT correctly
358 ULONG ulSysInfo
[QSV_MAX
] = {0};
361 ulrc
= ::DosQuerySysInfo( 1L
364 ,sizeof(ULONG
) * QSV_MAX
368 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
369 *pMajorVsn
= *pMajorVsn
/10;
370 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
371 return wxWINDOWS_OS2
;
373 return wxWINDOWS
; // error if we get here, return generic value
376 // Reading and writing resources (eg WIN.INI, .Xdefaults)
378 bool wxWriteResource(
379 const wxString
& rSection
380 , const wxString
& rEntry
381 , const wxString
& rValue
382 , const wxString
& rFile
390 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
393 return (::PrfWriteProfileString( hIni
394 ,(PSZ
)WXSTRINGCAST rSection
395 ,(PSZ
)WXSTRINGCAST rEntry
396 ,(PSZ
)WXSTRINGCAST rValue
401 return (::PrfWriteProfileString( HINI_PROFILE
402 ,(PSZ
)WXSTRINGCAST rSection
403 ,(PSZ
)WXSTRINGCAST rEntry
404 ,(PSZ
)WXSTRINGCAST rValue
409 bool wxWriteResource(
410 const wxString
& rSection
411 , const wxString
& rEntry
413 , const wxString
& rFile
418 wxSprintf(zBuf
, "%.4f", fValue
);
419 return wxWriteResource( rSection
426 bool wxWriteResource(
427 const wxString
& rSection
428 , const wxString
& rEntry
430 , const wxString
& rFile
435 wxSprintf(zBuf
, "%ld", lValue
);
436 return wxWriteResource( rSection
443 bool wxWriteResource(
444 const wxString
& rSection
445 , const wxString
& rEntry
447 , const wxString
& rFile
452 wxSprintf(zBuf
, "%d", lValue
);
453 return wxWriteResource( rSection
461 const wxString
& rSection
462 , const wxString
& rEntry
464 , const wxString
& rFile
469 wxChar zDefunkt
[] = _T("$$default");
474 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
477 ULONG n
= ::PrfQueryProfileString( hIni
478 ,(PSZ
)WXSTRINGCAST rSection
479 ,(PSZ
)WXSTRINGCAST rEntry
486 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
495 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
496 ,(PSZ
)WXSTRINGCAST rSection
497 ,(PSZ
)WXSTRINGCAST rEntry
504 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
508 strcpy((char*)*ppValue
, zBuf
);
513 const wxString
& rSection
514 , const wxString
& rEntry
516 , const wxString
& rFile
521 zStr
= new wxChar
[1000];
522 bool bSucc
= wxGetResource( rSection
530 *pValue
= (float)wxStrtod(zStr
, NULL
);
542 const wxString
& rSection
543 , const wxString
& rEntry
545 , const wxString
& rFile
550 zStr
= new wxChar
[1000];
551 bool bSucc
= wxGetResource( rSection
559 *pValue
= wxStrtol(zStr
, NULL
, 10);
571 const wxString
& rSection
572 , const wxString
& rEntry
574 , const wxString
& rFile
579 zStr
= new wxChar
[1000];
580 bool bSucc
= wxGetResource( rSection
588 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
598 #endif // wxUSE_RESOURCES
600 // ---------------------------------------------------------------------------
601 // helper functions for showing a "busy" cursor
602 // ---------------------------------------------------------------------------
604 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
605 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
606 static int gs_wxBusyCursorCount
= 0;
608 // Set the cursor to the busy cursor for all windows
609 void wxBeginBusyCursor(
613 if ( gs_wxBusyCursorCount
++ == 0 )
615 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
616 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
618 //else: nothing to do, already set
621 // Restore cursor to normal
622 void wxEndBusyCursor()
624 wxCHECK_RET( gs_wxBusyCursorCount
> 0
625 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
628 if (--gs_wxBusyCursorCount
== 0)
630 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
631 gs_wxBusyCursorOld
= 0;
635 // TRUE if we're between the above two calls
638 return (gs_wxBusyCursorCount
> 0);
641 // ---------------------------------------------------------------------------
642 const wxChar
* wxGetHomeDir(
646 wxString
& rStrDir
= *pStr
;
648 // OS/2 has no idea about home,
649 // so use the working directory instead?
651 // 256 was taken from os2def.h
653 # define MAX_PATH 256
659 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
661 return rStrDir
.c_str();
665 wxChar
* wxGetUserHome (
666 const wxString
& rUser
670 wxString
sUser1(rUser
);
672 wxBuffer
= new wxChar
[256];
674 if (sUser1
!= _T(""))
678 if (wxGetUserId( zTmp
679 ,sizeof(zTmp
)/sizeof(char)
682 // Guests belong in the temp dir
683 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
685 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
686 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
687 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
689 return *zHome
? zHome
: (wxChar
*)_T("\\");
691 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
696 if (sUser1
== _T(""))
698 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
700 wxStrcpy(wxBuffer
, zHome
);
701 Unix2DosFilename(wxBuffer
);
702 wxStrcpy(zHome
, wxBuffer
);
708 return NULL
; // No home known!
711 // Check whether this window wants to process messages, e.g. Stop button
712 // in long calculations.
713 bool wxCheckForInterrupt(
721 HWND hwndFilter
= NULLHANDLE
;
722 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
724 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
726 ::WinDispatchMsg(hab
, &vMsg
);
728 return TRUE
;//*** temporary?
732 wxFAIL_MSG(_T("pWnd==NULL !!!"));
733 return FALSE
;//*** temporary?
737 void wxGetMousePosition(
744 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
749 // Return TRUE if we have a colour display
750 bool wxColourDisplay()
756 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
757 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
758 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
759 return(lColors
> 1L);
762 // Returns depth of screen
771 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
772 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
773 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
774 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
776 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
777 DevCloseDC(hdcScreen
);
781 // Get size of display
792 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
793 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
794 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, &lWidth
);
795 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, &lHeight
);
796 DevCloseDC(hdcScreen
);
797 *pWidth
= (int)lWidth
;
798 *pHeight
= (int)lHeight
;
801 void wxDisplaySizeMM(
809 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
810 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
813 ::DevQueryCaps( hdcScreen
814 ,CAPS_HORIZONTAL_RESOLUTION
819 ::DevQueryCaps( hdcScreen
820 ,CAPS_VERTICAL_RESOLUTION
830 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
833 // ---------------------------------------------------------------------------
834 // window information functions
835 // ---------------------------------------------------------------------------
837 wxString WXDLLEXPORT
wxGetWindowText(
842 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
844 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
845 vStr
.UngetWriteBuf();
850 wxString WXDLLEXPORT
wxGetWindowClass(
855 int nLen
= 256; // some starting value
859 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
861 vStr
.UngetWriteBuf();
864 // the class name might have been truncated, retry with larger
876 WXWORD WXDLLEXPORT
wxGetWindowId(
880 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);
883 wxString WXDLLEXPORT
wxPMErrorToStr(
890 // Remove the high order byte -- it is useless
892 vError
&= 0x0000ffff;
895 case PMERR_INVALID_HWND
:
896 sError
= wxT("Invalid window handle specified");
899 case PMERR_INVALID_FLAG
:
900 sError
= wxT("Invalid flag bit set");
903 case PMERR_NO_MSG_QUEUE
:
904 sError
= wxT("No message queue available");
907 case PMERR_INVALID_PARM
:
908 sError
= wxT("Parameter contained invalid data");
911 case PMERR_INVALID_PARAMETERS
:
912 sError
= wxT("Parameter value is out of range");
915 case PMERR_PARAMETER_OUT_OF_RANGE
:
916 sError
= wxT("Parameter value is out of range");
919 case PMERR_INVALID_INTEGER_ATOM
:
920 sError
= wxT("Not a valid atom");
923 case PMERR_INVALID_HATOMTBL
:
924 sError
= wxT("Atom table handle is invalid");
927 case PMERR_INVALID_ATOM_NAME
:
928 sError
= wxT("Not a valid atom name");
931 case PMERR_ATOM_NAME_NOT_FOUND
:
932 sError
= wxT("Valid name format, but cannot find name in atom table");
936 sError
= wxT("Unknown error");
939 } // end of wxPMErrorToStr