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"
48 static const wxChar WX_SECTION
[] = _T("wxWindows");
49 static const wxChar eHOSTNAME
[] = _T("HostName");
50 static const wxChar eUSERID
[] = _T("UserId");
51 static const wxChar eUSERNAME
[] = _T("UserName");
53 // For the following functions we SHOULD fill in support
54 // for Windows-NT (which I don't know) as I assume it begin
55 // a POSIX Unix (so claims MS) that it has some special
56 // functions beyond those provided by WinSock
58 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
67 unsigned long ulLevel
= 0;
68 unsigned char* zBuffer
;
69 unsigned long ulBuffer
;
70 unsigned long* pulTotalAvail
;
72 NetBios32GetInfo( (const unsigned char*)zServer
73 ,(const unsigned char*)zComputer
79 strcpy(zBuf
, zServer
);
82 const wxChar
* zDefaultHost
= _T("noname");
84 if ((zSysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
86 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
95 wxStrncpy(zBuf
, zSysname
, nMaxSize
- 1);
96 zBuf
[nMaxSize
] = _T('\0');
98 return *zBuf
? TRUE
: FALSE
;
101 // Get user ID e.g. jacs
108 // UPM procs return 0 on success
109 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
110 if (lrc
== 0) return TRUE
;
124 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
134 return((int)::DosKillProcess(0, (PID
)lPid
));
138 // Execute a program in an Interactive Shell
141 const wxString
& rCommand
144 wxChar
* zShell
= _T("CMD.EXE");
147 STARTDATA SData
= {0};
148 PSZ PgmTitle
= "Command Shell";
152 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
155 SData
.Length
= sizeof(STARTDATA
);
156 SData
.Related
= SSF_RELATED_INDEPENDENT
;
157 SData
.FgBg
= SSF_FGBG_FORE
;
158 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
159 SData
.PgmTitle
= PgmTitle
;
160 SData
.PgmName
= zShell
;
162 sInputs
= "/C " + rCommand
;
163 SData
.PgmInputs
= NULL
; //(BYTE*)sInputs.c_str();
165 SData
.Environment
= 0;
166 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
167 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
170 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
173 SData
.InitXSize
= 200;
174 SData
.InitYSize
= 140;
176 SData
.ObjectBuffer
= (char*)achObjBuf
;
177 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
179 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
185 ::DosGetInfoBlocks(&ptib
, &ppib
);
187 ::DosWaitChild( DCWA_PROCESS
197 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
198 long wxGetFreeMemory(
206 lMemFlags
= PAG_FREE
;
207 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
213 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
214 static bool inTimer
= FALSE
;
216 class wxSleepTimer
: public wxTimer
226 static wxTimer
* wxTheSleepTimer
= NULL
;
229 unsigned long ulMilliseconds
232 ::DosSleep(ulMilliseconds
);
239 ::DosSleep(1000 * nSecs
);
242 // Consume all events until no more left
248 // Output a debug mess., in a system dependent fashion.
250 const wxChar
* zFmt
...
254 static wxChar zBuffer
[512];
256 if (!wxTheApp
->GetWantDebugOutput())
259 sprintf(zBuffer
, zFmt
, vAp
) ;
263 // Non-fatal error: pop up message box and (possibly) continue
266 , const wxString
& rTitle
269 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
270 if (::WinMessageBox( HWND_DESKTOP
273 ,(PSZ
)WXSTRINGCAST rTitle
275 ,MB_ICONEXCLAMATION
| MB_YESNO
280 // Fatal error: pop up message box and abort
283 , const wxString
& rTitle
288 ulRc
= ::WinMessageBox( HWND_DESKTOP
295 DosExit(EXIT_PROCESS
, ulRc
);
301 DosBeep(1000,1000); // 1kHz during 1 sec.
304 // Chris Breeze 27/5/98: revised WIN32 code to
305 // detect WindowsNT correctly
311 ULONG ulSysInfo
[QSV_MAX
] = {0};
313 if (::DosQuerySysInfo( 1L
316 ,sizeof(ULONG
) * QSV_MAX
319 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
320 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
321 return wxWINDOWS_OS2
;
323 return wxWINDOWS
; // error if we get here, return generic value
326 // Reading and writing resources (eg WIN.INI, .Xdefaults)
328 bool wxWriteResource(
329 const wxString
& rSection
330 , const wxString
& rEntry
331 , const wxString
& rValue
332 , const wxString
& rFile
340 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
343 return (::PrfWriteProfileString( hIni
344 ,(PSZ
)WXSTRINGCAST rSection
345 ,(PSZ
)WXSTRINGCAST rEntry
346 ,(PSZ
)WXSTRINGCAST rValue
351 return (::PrfWriteProfileString( HINI_PROFILE
352 ,(PSZ
)WXSTRINGCAST rSection
353 ,(PSZ
)WXSTRINGCAST rEntry
354 ,(PSZ
)WXSTRINGCAST rValue
359 bool wxWriteResource(
360 const wxString
& rSection
361 , const wxString
& rEntry
363 , const wxString
& rFile
368 wxSprintf(zBuf
, "%.4f", fValue
);
369 return wxWriteResource( rSection
376 bool wxWriteResource(
377 const wxString
& rSection
378 , const wxString
& rEntry
380 , const wxString
& rFile
385 wxSprintf(zBuf
, "%ld", lValue
);
386 return wxWriteResource( rSection
393 bool wxWriteResource(
394 const wxString
& rSection
395 , const wxString
& rEntry
397 , const wxString
& rFile
402 wxSprintf(zBuf
, "%d", lValue
);
403 return wxWriteResource( rSection
411 const wxString
& rSection
412 , const wxString
& rEntry
414 , const wxString
& rFile
419 static const wxChar zDefunkt
[] = _T("$$default");
423 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
426 ULONG n
= ::PrfQueryProfileString( hIni
427 ,(PSZ
)WXSTRINGCAST rSection
428 ,(PSZ
)WXSTRINGCAST rEntry
433 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
441 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
442 ,(PSZ
)WXSTRINGCAST rSection
443 ,(PSZ
)WXSTRINGCAST rEntry
448 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
453 *ppValue
= copystring(wxBuffer
);
458 const wxString
& rSection
459 , const wxString
& rEntry
461 , const wxString
& rFile
465 bool bSucc
= wxGetResource( rSection
473 *pValue
= (float)wxStrtod(zStr
, NULL
);
481 const wxString
& rSection
482 , const wxString
& rEntry
484 , const wxString
& rFile
488 bool bSucc
= wxGetResource( rSection
496 *pValue
= wxStrtol(zStr
, NULL
, 10);
504 const wxString
& rSection
505 , const wxString
& rEntry
507 , const wxString
& rFile
511 bool bSucc
= wxGetResource( rSection
519 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
525 #endif // wxUSE_RESOURCES
527 // ---------------------------------------------------------------------------
528 // helper functions for showing a "busy" cursor
529 // ---------------------------------------------------------------------------
531 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
532 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
533 static int gs_wxBusyCursorCount
= 0;
535 // Set the cursor to the busy cursor for all windows
536 void wxBeginBusyCursor(
540 if ( gs_wxBusyCursorCount
++ == 0 )
542 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
543 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
545 //else: nothing to do, already set
548 // Restore cursor to normal
549 void wxEndBusyCursor()
551 wxCHECK_RET( gs_wxBusyCursorCount
> 0
552 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
555 if (--gs_wxBusyCursorCount
== 0)
557 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
558 gs_wxBusyCursorOld
= 0;
562 // TRUE if we're between the above two calls
565 return (gs_wxBusyCursorCount
> 0);
568 // ---------------------------------------------------------------------------
569 const wxChar
* wxGetHomeDir(
573 wxString
& rStrDir
= *pStr
;
575 // OS/2 has no idea about home,
576 // so use the working directory instead?
578 // 256 was taken from os2def.h
580 # define MAX_PATH 256
586 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
588 return rStrDir
.c_str();
592 wxChar
* wxGetUserHome (
593 const wxString
& rUser
597 wxString
sUser1(rUser
);
599 if (sUser1
!= _T(""))
603 if (wxGetUserId( zTmp
604 ,sizeof(zTmp
)/sizeof(char)
607 // Guests belong in the temp dir
608 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
610 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
611 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
612 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
613 return *zHome
? zHome
: (wxChar
*)_T("\\");
615 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
619 if (sUser1
== _T(""))
620 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
622 wxStrcpy(wxBuffer
, zHome
);
623 Unix2DosFilename(wxBuffer
);
626 return NULL
; // No home known!
629 // Check whether this window wants to process messages, e.g. Stop button
630 // in long calculations.
631 bool wxCheckForInterrupt(
640 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
642 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
644 ::WinDispatchMsg(hab
, &vMsg
);
646 return TRUE
;//*** temporary?
650 wxFAIL_MSG(_T("pWnd==NULL !!!"));
651 return FALSE
;//*** temporary?
655 void wxGetMousePosition(
662 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
667 // Return TRUE if we have a colour display
668 bool wxColourDisplay()
674 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
675 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
676 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
677 return(lColors
> 1L);
680 // Returns depth of screen
689 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
690 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
691 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
692 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
694 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
695 DevCloseDC(hdcScreen
);
699 // Get size of display
708 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
709 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
710 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, (PLONG
)pWidth
);
711 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, (PLONG
)pHeight
);
712 DevCloseDC(hdcScreen
);
719 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
722 // ---------------------------------------------------------------------------
723 // window information functions
724 // ---------------------------------------------------------------------------
726 wxString WXDLLEXPORT
wxGetWindowText(
731 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
733 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
734 vStr
.UngetWriteBuf();
739 wxString WXDLLEXPORT
wxGetWindowClass(
744 int nLen
= 256; // some starting value
748 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
750 vStr
.UngetWriteBuf();
753 // the class name might have been truncated, retry with larger
765 WXWORD WXDLLEXPORT
wxGetWindowId(
769 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);