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"
47 // In the WIN.INI file
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)
59 bool wxGetHostName(wxChar
*buf
, int maxSize
)
64 unsigned long ulLevel
;
65 unsigned char* pbBuffer
;
66 unsigned long ulBuffer
;
67 unsigned long* pulTotalAvail
;
69 NetBios32GetInfo( server
79 const wxChar
*default_host
= _T("noname");
81 if ((sysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
83 // GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
86 wxStrncpy(buf
, sysname
, maxSize
- 1);
87 buf
[maxSize
] = _T('\0');
89 return *buf
? TRUE
: FALSE
;
92 // Get user ID e.g. jacs
93 bool wxGetUserId(wxChar
*buf
, int maxSize
)
95 return(U32ELOCL((unsigned char*)buf
, (unsigned long *)&maxSize
));
98 bool wxGetUserName(wxChar
*buf
, int maxSize
)
101 wxGetUserId(buf
, maxSize
);
103 wxStrncpy(buf
, _T("Unknown User"), maxSize
);
108 int wxKill(long pid
, int sig
)
114 // Execute a program in an Interactive Shell
116 bool wxShell(const wxString
& command
)
119 if ((shell
= wxGetenv(_T("COMSPEC"))) == NULL
)
120 shell
= _T("\\CMD.EXE");
124 wxSprintf(tmp
, "%s /c %s", shell
, WXSTRINGCAST command
);
126 wxStrcpy(tmp
, shell
);
128 return (wxExecute((wxChar
*)tmp
, FALSE
) != 0);
131 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
132 long wxGetFreeMemory()
134 // return (long)GetFreeSpace(0);
138 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
139 static bool inTimer
= FALSE
;
141 class wxSleepTimer
: public wxTimer
151 static wxTimer
*wxTheSleepTimer
= NULL
;
153 void wxUsleep(unsigned long milliseconds
)
155 ::DosSleep(milliseconds
);
158 void wxSleep(int nSecs
)
163 wxTheSleepTimer
= new wxSleepTimer
;
165 wxTheSleepTimer
->Start(nSecs
*1000);
168 if (wxTheApp
->Pending())
169 wxTheApp
->Dispatch();
171 delete wxTheSleepTimer
;
172 wxTheSleepTimer
= NULL
;
175 // Consume all events until no more left
181 // Output a debug mess., in a system dependent fashion.
182 void wxDebugMsg(const wxChar
*fmt
...)
185 static wxChar buffer
[512];
187 if (!wxTheApp
->GetWantDebugOutput())
192 sprintf(buffer
,fmt
,ap
) ;
197 // Non-fatal error: pop up message box and (possibly) continue
198 void wxError(const wxString
& msg
, const wxString
& title
)
200 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST msg
);
201 if (::WinMessageBox( HWND_DESKTOP
204 ,(PSZ
)WXSTRINGCAST title
206 ,MB_ICONEXCLAMATION
| MB_YESNO
211 // Fatal error: pop up message box and abort
212 void wxFatalError(const wxString
& rMsg
, const wxString
& rTitle
)
216 rc
= ::WinMessageBox( HWND_DESKTOP
223 DosExit(EXIT_PROCESS
, rc
);
229 DosBeep(1000,1000); // 1kHz during 1 sec.
232 // Chris Breeze 27/5/98: revised WIN32 code to
233 // detect WindowsNT correctly
234 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
236 ULONG aulSysInfo
[QSV_MAX
] = {0};
238 if (DosQuerySysInfo( 1L
241 ,sizeof(ULONG
) * QSV_MAX
244 *majorVsn
= aulSysInfo
[QSV_VERSION_MAJOR
];
245 *minorVsn
= aulSysInfo
[QSV_VERSION_MINOR
];
246 return wxWINDOWS_OS2
;
248 return wxWINDOWS
; // error if we get here, return generic value
251 // Reading and writing resources (eg WIN.INI, .Xdefaults)
252 // TODO: Ability to read and write to an INI file
255 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
260 return (WritePrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)value, (PCSZ)WXSTRINGCAST file) != 0);
262 return (WriteProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)WXSTRINGCAST value) != 0);
267 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
270 wxSprintf(buf
, "%.4f", value
);
271 return wxWriteResource(section
, entry
, buf
, file
);
274 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
277 wxSprintf(buf
, "%ld", value
);
278 return wxWriteResource(section
, entry
, buf
, file
);
281 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
284 wxSprintf(buf
, "%d", value
);
285 return wxWriteResource(section
, entry
, buf
, file
);
288 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
290 static const wxChar defunkt
[] = _T("$$default");
295 int n = GetPrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)defunkt,
296 (PSZ)wxBuffer, 1000, (PCSZ)WXSTRINGCAST file);
297 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
302 int n = GetProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (LPCTSTR)defunkt,
303 (PSZ)wxBuffer, 1000);
304 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
307 if (*value) delete[] (*value);
308 *value = copystring(wxBuffer);
314 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
317 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
320 *value
= (float)wxStrtod(s
, NULL
);
327 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
330 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
333 *value
= wxStrtol(s
, NULL
, 10);
340 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
343 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
346 *value
= (int)wxStrtol(s
, NULL
, 10);
352 #endif // wxUSE_RESOURCES
354 // ---------------------------------------------------------------------------
355 // helper functions for showing a "busy" cursor
356 // ---------------------------------------------------------------------------
358 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
359 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
360 static int gs_wxBusyCursorCount
= 0;
362 // Set the cursor to the busy cursor for all windows
363 void wxBeginBusyCursor(wxCursor
*cursor
)
365 if ( gs_wxBusyCursorCount
++ == 0 )
367 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
368 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
370 //else: nothing to do, already set
373 // Restore cursor to normal
374 void wxEndBusyCursor()
376 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
377 _T("no matching wxBeginBusyCursor() for wxEndBusyCursor()"));
379 if ( --gs_wxBusyCursorCount
== 0 )
381 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
382 gs_wxBusyCursorOld
= 0;
386 // TRUE if we're between the above two calls
389 return (gs_wxBusyCursorCount
> 0);
392 // ---------------------------------------------------------------------------
393 const wxChar
* wxGetHomeDir(wxString
*pstr
)
395 wxString
& strDir
= *pstr
;
397 // OS/2 has no idea about home,
398 // so use the working directory instead?
400 // 256 was taken from os2def.h
402 # define MAX_PATH 256
409 ::DosQueryCurrentDir( 0, DirName
, &DirLen
);
411 return strDir
.c_str();
415 wxChar
*wxGetUserHome (const wxString
& user
)
418 wxString
user1(user
);
420 if (user1
!= _T("")) {
422 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
423 // Guests belong in the temp dir
424 if (wxStricmp(tmp
, _T("annonymous")) == 0) {
425 if ((home
= wxGetenv(_T("TMP"))) != NULL
||
426 (home
= wxGetenv(_T("TMPDIR"))) != NULL
||
427 (home
= wxGetenv(_T("TEMP"))) != NULL
)
428 return *home
? home
: (wxChar
*)_T("\\");
430 if (wxStricmp(tmp
, WXSTRINGCAST user1
) == 0)
435 if ((home
= wxGetenv(_T("HOME"))) != NULL
)
437 wxStrcpy(wxBuffer
, home
);
438 Unix2DosFilename(wxBuffer
);
441 return NULL
; // No home known!
444 // Check whether this window wants to process messages, e.g. Stop button
445 // in long calculations.
446 bool wxCheckForInterrupt(wxWindow
*wnd
)
453 HWND win
= (HWND
) wnd
->GetHWND();
454 while(::WinPeekMsg(hab
,&msg
,hwndFilter
,0,0,PM_REMOVE
))
456 ::WinDispatchMsg( hab
, &msg
);
458 return TRUE
;//*** temporary?
461 wxFAIL_MSG(_T("wnd==NULL !!!"));
463 return FALSE
;//*** temporary?
467 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
474 * #if !defined(__WIN32__) || defined(__TWIN32__)
475 * HRSRC hResource = ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
478 * HRSRC hResource = ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
480 * HRSRC hResource = ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
484 * if (hResource == 0)
486 * HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
489 * wxChar *theText = (wxChar *)LockResource(hData);
493 * s = copystring(theText);
498 void wxGetMousePosition( int* x
, int* y
)
501 ::WinQueryPointerPos( HWND_DESKTOP
, & pt
);
506 // Return TRUE if we have a colour display
507 bool wxColourDisplay()
510 // TODO: use DosQueryDevCaps to figure it out
514 // Returns depth of screen
517 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
518 long lArray
[CAPS_COLOR_BITCOUNT
];
529 nPlanes
= (int)lArray
[CAPS_COLOR_PLANES
];
530 nBitsPerPixel
= (int)lArray
[CAPS_COLOR_BITCOUNT
];
531 nDepth
= nPlanes
* nBitsPerPixel
;
537 // Get size of display
538 void wxDisplaySize(int *width
, int *height
)
540 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
541 long lArray
[CAPS_HEIGHT
];
549 *width
= (int)lArray
[CAPS_WIDTH
];
550 *height
= (int)lArray
[CAPS_HEIGHT
];
555 bool wxDirExists(const wxString
& dir
)
557 // TODO: Control program file stuff
561 // ---------------------------------------------------------------------------
562 // window information functions
563 // ---------------------------------------------------------------------------
565 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
568 long len
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
569 ::WinQueryWindowText((HWND
)hWnd
, len
, str
.GetWriteBuf((int)len
));
575 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
579 int len
= 256; // some starting value
583 int count
= ::WinQueryClassName((HWND
)hWnd
, len
, str
.GetWriteBuf(len
));
588 // the class name might have been truncated, retry with larger
600 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
602 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);