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
;
70 unsigned long ulBuffer
;
71 unsigned long* pulTotalAvail
;
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
109 // UPM procs return 0 on success
110 lrc
= U32ELOCU((unsigned char*)zBuf
, (unsigned long *)&nType
);
111 if (lrc
== 0) return TRUE
;
125 wxStrncpy(zBuf
, _T("Unknown User"), nMaxSize
);
135 return((int)::DosKillProcess(0, (PID
)lPid
));
139 // Execute a program in an Interactive Shell
142 const wxString
& rCommand
145 wxChar
* zShell
= _T("CMD.EXE");
148 STARTDATA SData
= {0};
149 PSZ PgmTitle
= "Command Shell";
153 UCHAR achObjBuf
[256] = {0}; //error data if DosStart fails
156 SData
.Length
= sizeof(STARTDATA
);
157 SData
.Related
= SSF_RELATED_INDEPENDENT
;
158 SData
.FgBg
= SSF_FGBG_FORE
;
159 SData
.TraceOpt
= SSF_TRACEOPT_NONE
;
160 SData
.PgmTitle
= PgmTitle
;
161 SData
.PgmName
= zShell
;
163 sInputs
= "/C " + rCommand
;
164 SData
.PgmInputs
= NULL
; //(BYTE*)sInputs.c_str();
166 SData
.Environment
= 0;
167 SData
.InheritOpt
= SSF_INHERTOPT_SHELL
;
168 SData
.SessionType
= SSF_TYPE_WINDOWABLEVIO
;
171 SData
.PgmControl
= SSF_CONTROL_VISIBLE
| SSF_CONTROL_MAXIMIZE
;
174 SData
.InitXSize
= 200;
175 SData
.InitYSize
= 140;
177 SData
.ObjectBuffer
= (char*)achObjBuf
;
178 SData
.ObjectBuffLen
= (ULONG
)sizeof(achObjBuf
);
180 rc
= ::DosStartSession(&SData
, &ulSessID
, &vPid
);
186 ::DosGetInfoBlocks(&ptib
, &ppib
);
188 ::DosWaitChild( DCWA_PROCESS
198 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
199 long wxGetFreeMemory(
207 lMemFlags
= PAG_FREE
;
208 rc
= ::DosQueryMem(pMemptr
, &lSize
, &lMemFlags
);
214 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
215 static bool inTimer
= FALSE
;
217 class wxSleepTimer
: public wxTimer
227 static wxTimer
* wxTheSleepTimer
= NULL
;
230 unsigned long ulMilliseconds
233 ::DosSleep(ulMilliseconds
);
240 ::DosSleep(1000 * nSecs
);
243 // Consume all events until no more left
249 // Output a debug mess., in a system dependent fashion.
251 const wxChar
* zFmt
...
255 static wxChar zBuffer
[512];
257 if (!wxTheApp
->GetWantDebugOutput())
260 sprintf(zBuffer
, zFmt
, vAp
) ;
264 // Non-fatal error: pop up message box and (possibly) continue
267 , const wxString
& rTitle
270 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST rMsg
);
271 if (::WinMessageBox( HWND_DESKTOP
274 ,(PSZ
)WXSTRINGCAST rTitle
276 ,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};
314 if (::DosQuerySysInfo( 1L
317 ,sizeof(ULONG
) * QSV_MAX
320 *pMajorVsn
= ulSysInfo
[QSV_VERSION_MAJOR
];
321 *pMinorVsn
= ulSysInfo
[QSV_VERSION_MINOR
];
322 return wxWINDOWS_OS2
;
324 return wxWINDOWS
; // error if we get here, return generic value
327 // Reading and writing resources (eg WIN.INI, .Xdefaults)
329 bool wxWriteResource(
330 const wxString
& rSection
331 , const wxString
& rEntry
332 , const wxString
& rValue
333 , const wxString
& rFile
341 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
344 return (::PrfWriteProfileString( hIni
345 ,(PSZ
)WXSTRINGCAST rSection
346 ,(PSZ
)WXSTRINGCAST rEntry
347 ,(PSZ
)WXSTRINGCAST rValue
352 return (::PrfWriteProfileString( HINI_PROFILE
353 ,(PSZ
)WXSTRINGCAST rSection
354 ,(PSZ
)WXSTRINGCAST rEntry
355 ,(PSZ
)WXSTRINGCAST rValue
360 bool wxWriteResource(
361 const wxString
& rSection
362 , const wxString
& rEntry
364 , const wxString
& rFile
369 wxSprintf(zBuf
, "%.4f", fValue
);
370 return wxWriteResource( rSection
377 bool wxWriteResource(
378 const wxString
& rSection
379 , const wxString
& rEntry
381 , const wxString
& rFile
386 wxSprintf(zBuf
, "%ld", lValue
);
387 return wxWriteResource( rSection
394 bool wxWriteResource(
395 const wxString
& rSection
396 , const wxString
& rEntry
398 , const wxString
& rFile
403 wxSprintf(zBuf
, "%d", lValue
);
404 return wxWriteResource( rSection
412 const wxString
& rSection
413 , const wxString
& rEntry
415 , const wxString
& rFile
420 static const wxChar zDefunkt
[] = _T("$$default");
424 hIni
= ::PrfOpenProfile(hab
, (PSZ
)WXSTRINGCAST rFile
);
427 ULONG n
= ::PrfQueryProfileString( hIni
428 ,(PSZ
)WXSTRINGCAST rSection
429 ,(PSZ
)WXSTRINGCAST rEntry
434 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
442 ULONG n
= ::PrfQueryProfileString( HINI_PROFILE
443 ,(PSZ
)WXSTRINGCAST rSection
444 ,(PSZ
)WXSTRINGCAST rEntry
449 if (n
== 0L || wxStrcmp(wxBuffer
, zDefunkt
) == 0)
454 *ppValue
= copystring(wxBuffer
);
459 const wxString
& rSection
460 , const wxString
& rEntry
462 , const wxString
& rFile
466 bool bSucc
= wxGetResource( rSection
474 *pValue
= (float)wxStrtod(zStr
, NULL
);
482 const wxString
& rSection
483 , const wxString
& rEntry
485 , const wxString
& rFile
489 bool bSucc
= wxGetResource( rSection
497 *pValue
= wxStrtol(zStr
, NULL
, 10);
505 const wxString
& rSection
506 , const wxString
& rEntry
508 , const wxString
& rFile
512 bool bSucc
= wxGetResource( rSection
520 *pValue
= (int)wxStrtol(zStr
, NULL
, 10);
526 #endif // wxUSE_RESOURCES
528 // ---------------------------------------------------------------------------
529 // helper functions for showing a "busy" cursor
530 // ---------------------------------------------------------------------------
532 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
533 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
534 static int gs_wxBusyCursorCount
= 0;
536 // Set the cursor to the busy cursor for all windows
537 void wxBeginBusyCursor(
541 if ( gs_wxBusyCursorCount
++ == 0 )
543 gs_wxBusyCursor
= (HCURSOR
)pCursor
->GetHCURSOR();
544 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
546 //else: nothing to do, already set
549 // Restore cursor to normal
550 void wxEndBusyCursor()
552 wxCHECK_RET( gs_wxBusyCursorCount
> 0
553 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
556 if (--gs_wxBusyCursorCount
== 0)
558 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
559 gs_wxBusyCursorOld
= 0;
563 // TRUE if we're between the above two calls
566 return (gs_wxBusyCursorCount
> 0);
569 // ---------------------------------------------------------------------------
570 const wxChar
* wxGetHomeDir(
574 wxString
& rStrDir
= *pStr
;
576 // OS/2 has no idea about home,
577 // so use the working directory instead?
579 // 256 was taken from os2def.h
581 # define MAX_PATH 256
587 ::DosQueryCurrentDir(0, zDirName
, &ulDirLen
);
589 return rStrDir
.c_str();
593 wxChar
* wxGetUserHome (
594 const wxString
& rUser
598 wxString
sUser1(rUser
);
600 if (sUser1
!= _T(""))
604 if (wxGetUserId( zTmp
605 ,sizeof(zTmp
)/sizeof(char)
608 // Guests belong in the temp dir
609 if (wxStricmp(zTmp
, _T("annonymous")) == 0)
611 if ((zHome
= wxGetenv(_T("TMP"))) != NULL
||
612 (zHome
= wxGetenv(_T("TMPDIR"))) != NULL
||
613 (zHome
= wxGetenv(_T("TEMP"))) != NULL
)
614 return *zHome
? zHome
: (wxChar
*)_T("\\");
616 if (wxStricmp(zTmp
, WXSTRINGCAST sUser1
) == 0)
620 if (sUser1
== _T(""))
621 if ((zHome
= wxGetenv(_T("HOME"))) != NULL
)
623 wxStrcpy(wxBuffer
, zHome
);
624 Unix2DosFilename(wxBuffer
);
627 return NULL
; // No home known!
630 // Check whether this window wants to process messages, e.g. Stop button
631 // in long calculations.
632 bool wxCheckForInterrupt(
641 HWND hwndWin
= (HWND
) pWnd
->GetHWND();
643 while(::WinPeekMsg(hab
, &vMsg
, hwndFilter
, 0, 0, PM_REMOVE
))
645 ::WinDispatchMsg(hab
, &vMsg
);
647 return TRUE
;//*** temporary?
651 wxFAIL_MSG(_T("pWnd==NULL !!!"));
652 return FALSE
;//*** temporary?
656 void wxGetMousePosition(
663 ::WinQueryPointerPos(HWND_DESKTOP
, &vPt
);
668 // Return TRUE if we have a colour display
669 bool wxColourDisplay()
675 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
676 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
677 ::DevQueryCaps(hdcScreen
, CAPS_COLORS
, 1L, &lColors
);
678 return(lColors
> 1L);
681 // Returns depth of screen
690 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
691 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
692 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_PLANES
, 1L, &lPlanes
);
693 ::DevQueryCaps(hdcScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitsPerPixel
);
695 nDepth
= (int)(lPlanes
* lBitsPerPixel
);
696 DevCloseDC(hdcScreen
);
700 // Get size of display
709 hpsScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
710 hdcScreen
= ::GpiQueryDevice(hpsScreen
);
711 ::DevQueryCaps(hdcScreen
, CAPS_WIDTH
, 1L, (PLONG
)pWidth
);
712 ::DevQueryCaps(hdcScreen
, CAPS_HEIGHT
, 1L, (PLONG
)pHeight
);
713 DevCloseDC(hdcScreen
);
720 return (::DosSetCurrentDir(WXSTRINGCAST rDir
));
723 // ---------------------------------------------------------------------------
724 // window information functions
725 // ---------------------------------------------------------------------------
727 wxString WXDLLEXPORT
wxGetWindowText(
732 long lLen
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
734 ::WinQueryWindowText((HWND
)hWnd
, lLen
, vStr
.GetWriteBuf((int)lLen
));
735 vStr
.UngetWriteBuf();
740 wxString WXDLLEXPORT
wxGetWindowClass(
745 int nLen
= 256; // some starting value
749 int nCount
= ::WinQueryClassName((HWND
)hWnd
, nLen
, vStr
.GetWriteBuf(nLen
));
751 vStr
.UngetWriteBuf();
754 // the class name might have been truncated, retry with larger
766 WXWORD WXDLLEXPORT
wxGetWindowId(
770 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);