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 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
216 static bool inTimer
= FALSE
;
218 class wxSleepTimer
: public wxTimer
228 static wxTimer
* wxTheSleepTimer
= NULL
;
231 unsigned long ulMilliseconds
234 ::DosSleep(ulMilliseconds
/1000l);
241 ::DosSleep(1000 * nSecs
);
244 // Consume all events until no more left
250 // Output a debug mess., in a system dependent fashion.
252 const wxChar
* zFmt
...
256 static wxChar zBuffer
[512];
258 if (!wxTheApp
->GetWantDebugOutput())
261 sprintf(zBuffer
, zFmt
, vAp
) ;
265 // Non-fatal error: pop up message box and (possibly) continue
268 , const wxString
& rTitle
271 wxBuffer
= new wxChar
[256];
272 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
273 if (::WinMessageBox( HWND_DESKTOP
276 ,(PSZ
)WXSTRINGCAST rTitle
278 ,MB_ICONEXCLAMATION
| MB_YESNO
284 // Fatal error: pop up message box and abort
287 , const wxString
& rTitle
292 ulRc
= ::WinMessageBox( HWND_DESKTOP
299 DosExit(EXIT_PROCESS
, ulRc
);
305 DosBeep(1000,1000); // 1kHz during 1 sec.
308 // Chris Breeze 27/5/98: revised WIN32 code to
309 // detect WindowsNT correctly
315 ULONG ulSysInfo
[QSV_MAX
] = {0};
318 ulrc
= ::DosQuerySysInfo( 1L
321 ,sizeof(ULONG
) * QSV_MAX
325 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
326 *pMajorVsn
= *pMajorVsn
/10;
327 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
328 return wxWINDOWS_OS2
;
330 return wxWINDOWS
; // error if we get here, return generic value
333 // Reading and writing resources (eg WIN.INI, .Xdefaults)
335 bool wxWriteResource(
336 const wxString
& rSection
337 , const wxString
& rEntry
338 , const wxString
& rValue
339 , const wxString
& rFile
347 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
350 return (::PrfWriteProfileString( hIni
351 ,(PSZ
)WXSTRINGCAST rSection
352 ,(PSZ
)WXSTRINGCAST rEntry
353 ,(PSZ
)WXSTRINGCAST rValue
358 return (::PrfWriteProfileString( HINI_PROFILE
359 ,(PSZ
)WXSTRINGCAST rSection
360 ,(PSZ
)WXSTRINGCAST rEntry
361 ,(PSZ
)WXSTRINGCAST rValue
366 bool wxWriteResource(
367 const wxString
& rSection
368 , const wxString
& rEntry
370 , const wxString
& rFile
375 wxSprintf(zBuf
, "%.4f", fValue
);
376 return wxWriteResource( rSection
383 bool wxWriteResource(
384 const wxString
& rSection
385 , const wxString
& rEntry
387 , const wxString
& rFile
392 wxSprintf(zBuf
, "%ld", lValue
);
393 return wxWriteResource( rSection
400 bool wxWriteResource(
401 const wxString
& rSection
402 , const wxString
& rEntry
404 , const wxString
& rFile
409 wxSprintf(zBuf
, "%d", lValue
);
410 return wxWriteResource( rSection
418 const wxString
& rSection
419 , const wxString
& rEntry
421 , const wxString
& rFile
426 wxChar zDefunkt
[] = _T("$$default");
431 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
434 ULONG n
= ::PrfQueryProfileString( hIni
435 ,(PSZ
)WXSTRINGCAST rSection
436 ,(PSZ
)WXSTRINGCAST rEntry
443 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
452 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
453 ,(PSZ
)WXSTRINGCAST rSection
454 ,(PSZ
)WXSTRINGCAST rEntry
461 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
465 strcpy((char*)*ppValue
, zBuf
);
470 const wxString
& rSection
471 , const wxString
& rEntry
473 , const wxString
& rFile
478 zStr
= new wxChar
[1000];
479 bool bSucc
= wxGetResource( rSection
487 *pValue
= (float)wxStrtod(zStr
, NULL
);
499 const wxString
& rSection
500 , const wxString
& rEntry
502 , const wxString
& rFile
507 zStr
= new wxChar
[1000];
508 bool bSucc
= wxGetResource( rSection
516 *pValue
= wxStrtol(zStr
, NULL
, 10);
528 const wxString
& rSection
529 , const wxString
& rEntry
531 , const wxString
& rFile
536 zStr
= new wxChar
[1000];
537 bool bSucc
= wxGetResource( rSection
545 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
555 #endif // wxUSE_RESOURCES
557 // ---------------------------------------------------------------------------
558 // helper functions for showing a "busy" cursor
559 // ---------------------------------------------------------------------------
561 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
562 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
563 static int gs_wxBusyCursorCount
= 0;
565 // Set the cursor to the busy cursor for all windows
566 void wxBeginBusyCursor(
570 if ( gs_wxBusyCursorCount
++ == 0 )
572 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
573 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
575 //else: nothing to do, already set
578 // Restore cursor to normal
579 void wxEndBusyCursor()
581 wxCHECK_RET( gs_wxBusyCursorCount
> 0
582 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
585 if (--gs_wxBusyCursorCount
== 0)
587 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
588 gs_wxBusyCursorOld
= 0;
592 // TRUE if we're between the above two calls
595 return (gs_wxBusyCursorCount
> 0);
598 // ---------------------------------------------------------------------------
599 const wxChar
* wxGetHomeDir(
603 wxString
& rStrDir
= *pStr
;
605 // OS/2 has no idea about home,
606 // so use the working directory instead?
608 // 256 was taken from os2def.h
610 # define MAX_PATH 256
616 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
618 return rStrDir
.c_str();
622 wxChar
* wxGetUserHome (
623 const wxString
& rUser
627 wxString
sUser1(rUser
);
629 wxBuffer
= new wxChar
[256];
631 if (sUser1
!= _T(""))
635 if (wxGetUserId( zTmp
636 ,sizeof(zTmp
)/sizeof(char)
639 // Guests belong in the temp dir
640 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
642 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
643 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
644 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
646 return *zHome
? zHome
: (wxChar
*)_T("\\");
648 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
653 if (sUser1
== _T(""))
655 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
657 wxStrcpy(wxBuffer
, zHome
);
658 Unix2DosFilename(wxBuffer
);
659 wxStrcpy(zHome
, wxBuffer
);
665 return NULL
; // No home known!
668 // Check whether this window wants to process messages, e.g. Stop button
669 // in long calculations.
670 bool wxCheckForInterrupt(
678 HWND hwndFilter
= NULLHANDLE
;
679 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
681 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
683 ::WinDispatchMsg(hab
, &vMsg
);
685 return TRUE
;//*** temporary?
689 wxFAIL_MSG(_T("pWnd==NULL !!!"));
690 return FALSE
;//*** temporary?
694 void wxGetMousePosition(
701 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
706 // Return TRUE if we have a colour display
707 bool wxColourDisplay()
713 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
714 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
715 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
716 return(lColors
> 1L);
719 // Returns depth of screen
728 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
729 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
730 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
731 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
733 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
734 DevCloseDC(hdcScreen
);
738 // Get size of display
749 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
750 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
751 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, &lWidth
);
752 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, &lHeight
);
753 DevCloseDC(hdcScreen
);
754 *pWidth
= (int)lWidth
;
755 *pHeight
= (int)lHeight
;
762 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
765 // ---------------------------------------------------------------------------
766 // window information functions
767 // ---------------------------------------------------------------------------
769 wxString WXDLLEXPORT
wxGetWindowText(
774 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
776 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
777 vStr
.UngetWriteBuf();
782 wxString WXDLLEXPORT
wxGetWindowClass(
787 int nLen
= 256; // some starting value
791 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
793 vStr
.UngetWriteBuf();
796 // the class name might have been truncated, retry with larger
808 WXWORD WXDLLEXPORT
wxGetWindowId(
812 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);
815 wxString WXDLLEXPORT
wxPMErrorToStr(
822 // Remove the high order byte -- it is useless
824 vError
&= 0x0000ffff;
827 case PMERR_INVALID_HWND
:
828 sError
= wxT("Invalid window handle specified");
831 case PMERR_INVALID_FLAG
:
832 sError
= wxT("Invalid flag bit set");
835 case PMERR_NO_MSG_QUEUE
:
836 sError
= wxT("No message queue available");
839 case PMERR_INVALID_PARM
:
840 sError
= wxT("Parameter contained invalid data");
843 case PMERR_INVALID_PARAMETERS
:
844 sError
= wxT("Parameter value is out of range");
847 case PMERR_PARAMETER_OUT_OF_RANGE
:
848 sError
= wxT("Parameter value is out of range");
851 case PMERR_INVALID_INTEGER_ATOM
:
852 sError
= wxT("Not a valid atom");
855 case PMERR_INVALID_HATOMTBL
:
856 sError
= wxT("Atom table handle is invalid");
859 case PMERR_INVALID_ATOM_NAME
:
860 sError
= wxT("Not a valid atom name");
863 case PMERR_ATOM_NAME_NOT_FOUND
:
864 sError
= wxT("Valid name format, but cannot find name in atom table");
868 sError
= wxT("Unknown error");
871 } // end of wxPMErrorToStr