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
= NULL
;
69 unsigned long ulBuffer
= 256;
70 unsigned long* pulTotalAvail
= NULL
;
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
= (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
);
180 if (rc
== 0 || rc
== 457) // NO_ERROR or SMG_START_IN_BACKGROUND
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()
200 void* pMemptr
= NULL
;
205 lMemFlags
= PAG_FREE
;
206 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
212 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
213 static bool inTimer
= FALSE
;
215 class wxSleepTimer
: public wxTimer
225 static wxTimer
* wxTheSleepTimer
= NULL
;
228 unsigned long ulMilliseconds
231 ::DosSleep(ulMilliseconds
);
238 ::DosSleep(1000 * nSecs
);
241 // Consume all events until no more left
247 // Output a debug mess., in a system dependent fashion.
249 const wxChar
* zFmt
...
253 static wxChar zBuffer
[512];
255 if (!wxTheApp
->GetWantDebugOutput())
258 sprintf(zBuffer
, zFmt
, vAp
) ;
262 // Non-fatal error: pop up message box and (possibly) continue
265 , const wxString
& rTitle
268 wxBuffer
= new wxChar
[256];
269 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
270 if (::WinMessageBox( HWND_DESKTOP
273 ,(PSZ
)WXSTRINGCAST rTitle
275 ,MB_ICONEXCLAMATION
| MB_YESNO
281 // Fatal error: pop up message box and abort
284 , const wxString
& rTitle
289 ulRc
= ::WinMessageBox( HWND_DESKTOP
296 DosExit(EXIT_PROCESS
, ulRc
);
302 DosBeep(1000,1000); // 1kHz during 1 sec.
305 // Chris Breeze 27/5/98: revised WIN32 code to
306 // detect WindowsNT correctly
312 ULONG ulSysInfo
[QSV_MAX
] = {0};
315 ulrc
= ::DosQuerySysInfo( 1L
318 ,sizeof(ULONG
) * QSV_MAX
322 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
323 *pMajorVsn
= *pMajorVsn
/10;
324 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
325 return wxWINDOWS_OS2
;
327 return wxWINDOWS
; // error if we get here, return generic value
330 // Reading and writing resources (eg WIN.INI, .Xdefaults)
332 bool wxWriteResource(
333 const wxString
& rSection
334 , const wxString
& rEntry
335 , const wxString
& rValue
336 , const wxString
& rFile
344 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
347 return (::PrfWriteProfileString( hIni
348 ,(PSZ
)WXSTRINGCAST rSection
349 ,(PSZ
)WXSTRINGCAST rEntry
350 ,(PSZ
)WXSTRINGCAST rValue
355 return (::PrfWriteProfileString( HINI_PROFILE
356 ,(PSZ
)WXSTRINGCAST rSection
357 ,(PSZ
)WXSTRINGCAST rEntry
358 ,(PSZ
)WXSTRINGCAST rValue
363 bool wxWriteResource(
364 const wxString
& rSection
365 , const wxString
& rEntry
367 , const wxString
& rFile
372 wxSprintf(zBuf
, "%.4f", fValue
);
373 return wxWriteResource( rSection
380 bool wxWriteResource(
381 const wxString
& rSection
382 , const wxString
& rEntry
384 , const wxString
& rFile
389 wxSprintf(zBuf
, "%ld", lValue
);
390 return wxWriteResource( rSection
397 bool wxWriteResource(
398 const wxString
& rSection
399 , const wxString
& rEntry
401 , const wxString
& rFile
406 wxSprintf(zBuf
, "%d", lValue
);
407 return wxWriteResource( rSection
415 const wxString
& rSection
416 , const wxString
& rEntry
418 , const wxString
& rFile
423 wxChar zDefunkt
[] = _T("$$default");
428 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
431 ULONG n
= ::PrfQueryProfileString( hIni
432 ,(PSZ
)WXSTRINGCAST rSection
433 ,(PSZ
)WXSTRINGCAST rEntry
440 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
449 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
450 ,(PSZ
)WXSTRINGCAST rSection
451 ,(PSZ
)WXSTRINGCAST rEntry
458 if (n
== 0L || wxStrcmp(zBuf
, zDefunkt
) == 0)
462 strcpy((char*)*ppValue
, zBuf
);
467 const wxString
& rSection
468 , const wxString
& rEntry
470 , const wxString
& rFile
475 zStr
= new wxChar
[1000];
476 bool bSucc
= wxGetResource( rSection
484 *pValue
= (float)wxStrtod(zStr
, NULL
);
496 const wxString
& rSection
497 , const wxString
& rEntry
499 , const wxString
& rFile
504 zStr
= new wxChar
[1000];
505 bool bSucc
= wxGetResource( rSection
513 *pValue
= wxStrtol(zStr
, NULL
, 10);
525 const wxString
& rSection
526 , const wxString
& rEntry
528 , const wxString
& rFile
533 zStr
= new wxChar
[1000];
534 bool bSucc
= wxGetResource( rSection
542 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
552 #endif // wxUSE_RESOURCES
554 // ---------------------------------------------------------------------------
555 // helper functions for showing a "busy" cursor
556 // ---------------------------------------------------------------------------
558 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
559 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
560 static int gs_wxBusyCursorCount
= 0;
562 // Set the cursor to the busy cursor for all windows
563 void wxBeginBusyCursor(
567 if ( gs_wxBusyCursorCount
++ == 0 )
569 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
570 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
572 //else: nothing to do, already set
575 // Restore cursor to normal
576 void wxEndBusyCursor()
578 wxCHECK_RET( gs_wxBusyCursorCount
> 0
579 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
582 if (--gs_wxBusyCursorCount
== 0)
584 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
585 gs_wxBusyCursorOld
= 0;
589 // TRUE if we're between the above two calls
592 return (gs_wxBusyCursorCount
> 0);
595 // ---------------------------------------------------------------------------
596 const wxChar
* wxGetHomeDir(
600 wxString
& rStrDir
= *pStr
;
602 // OS/2 has no idea about home,
603 // so use the working directory instead?
605 // 256 was taken from os2def.h
607 # define MAX_PATH 256
613 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
615 return rStrDir
.c_str();
619 wxChar
* wxGetUserHome (
620 const wxString
& rUser
624 wxString
sUser1(rUser
);
626 wxBuffer
= new wxChar
[256];
627 if (sUser1
!= _T(""))
631 if (wxGetUserId( zTmp
632 ,sizeof(zTmp
)/sizeof(char)
635 // Guests belong in the temp dir
636 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
638 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
639 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
640 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
642 return *zHome
? zHome
: (wxChar
*)_T("\\");
644 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
648 if (sUser1
== _T(""))
650 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
652 wxStrcpy(wxBuffer
, zHome
);
653 Unix2DosFilename(wxBuffer
);
654 wxStrcpy(zHome
, wxBuffer
);
660 return NULL
; // No home known!
663 // Check whether this window wants to process messages, e.g. Stop button
664 // in long calculations.
665 bool wxCheckForInterrupt(
673 HWND hwndFilter
= NULLHANDLE
;
674 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
676 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
678 ::WinDispatchMsg(hab
, &vMsg
);
680 return TRUE
;//*** temporary?
684 wxFAIL_MSG(_T("pWnd==NULL !!!"));
685 return FALSE
;//*** temporary?
689 void wxGetMousePosition(
696 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
701 // Return TRUE if we have a colour display
702 bool wxColourDisplay()
708 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
709 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
710 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
711 return(lColors
> 1L);
714 // Returns depth of screen
723 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
724 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
725 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
726 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
728 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
729 DevCloseDC(hdcScreen
);
733 // Get size of display
744 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
745 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
746 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, &lWidth
);
747 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, &lHeight
);
748 DevCloseDC(hdcScreen
);
749 *pWidth
= (int)lWidth
;
750 *pHeight
= (int)lHeight
;
757 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
760 // ---------------------------------------------------------------------------
761 // window information functions
762 // ---------------------------------------------------------------------------
764 wxString WXDLLEXPORT
wxGetWindowText(
769 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
771 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
772 vStr
.UngetWriteBuf();
777 wxString WXDLLEXPORT
wxGetWindowClass(
782 int nLen
= 256; // some starting value
786 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
788 vStr
.UngetWriteBuf();
791 // the class name might have been truncated, retry with larger
803 WXWORD WXDLLEXPORT
wxGetWindowId(
807 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);