| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: utils.cpp |
| 3 | // Purpose: Various utilities |
| 4 | // Author: David Webster |
| 5 | // Modified by: |
| 6 | // Created: 09/17/99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) David Webster |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifndef WX_PRECOMP |
| 16 | #include "wx/setup.h" |
| 17 | #include "wx/utils.h" |
| 18 | #include "wx/app.h" |
| 19 | #include "wx/cursor.h" |
| 20 | #endif //WX_PRECOMP |
| 21 | |
| 22 | #include "wx/os2/private.h" |
| 23 | #include "wx/timer.h" |
| 24 | #include "wx/intl.h" |
| 25 | |
| 26 | #include <ctype.h> |
| 27 | #ifdef __EMX__ |
| 28 | #include <dirent.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/log.h" |
| 32 | |
| 33 | #include <io.h> |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <errno.h> |
| 39 | #include <stdarg.h> |
| 40 | |
| 41 | #define PURE_32 |
| 42 | |
| 43 | #ifndef __EMX__ |
| 44 | #include <upm.h> |
| 45 | #include <netcons.h> |
| 46 | #include <netbios.h> |
| 47 | #endif |
| 48 | |
| 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"); |
| 53 | |
| 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 |
| 58 | |
| 59 | // Get full hostname (eg. DoDo.BSn-Germany.crg.de) |
| 60 | bool wxGetHostName( |
| 61 | wxChar* zBuf |
| 62 | , int nMaxSize |
| 63 | ) |
| 64 | { |
| 65 | #if wxUSE_NET_API |
| 66 | char zServer[256]; |
| 67 | char zComputer[256]; |
| 68 | unsigned long ulLevel = 0; |
| 69 | unsigned char* zBuffer = NULL; |
| 70 | unsigned long ulBuffer = 256; |
| 71 | unsigned long* pulTotalAvail = NULL; |
| 72 | |
| 73 | NetBios32GetInfo( (const unsigned char*)zServer |
| 74 | ,(const unsigned char*)zComputer |
| 75 | ,ulLevel |
| 76 | ,zBuffer |
| 77 | ,ulBuffer |
| 78 | ,pulTotalAvail |
| 79 | ); |
| 80 | strcpy(zBuf, zServer); |
| 81 | #else |
| 82 | wxChar* zSysname; |
| 83 | const wxChar* zDefaultHost = _T("noname"); |
| 84 | |
| 85 | if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL) |
| 86 | { |
| 87 | ULONG n = ::PrfQueryProfileString( HINI_PROFILE |
| 88 | ,(PSZ)WX_SECTION |
| 89 | ,(PSZ)eHOSTNAME |
| 90 | ,(PSZ)zDefaultHost |
| 91 | ,(void*)zBuf |
| 92 | ,(ULONG)nMaxSize - 1 |
| 93 | ); |
| 94 | } |
| 95 | else |
| 96 | wxStrncpy(zBuf, zSysname, nMaxSize - 1); |
| 97 | zBuf[nMaxSize] = _T('\0'); |
| 98 | #endif |
| 99 | return *zBuf ? TRUE : FALSE; |
| 100 | } |
| 101 | |
| 102 | // Get user ID e.g. jacs |
| 103 | bool wxGetUserId( |
| 104 | wxChar* zBuf |
| 105 | , int nType |
| 106 | ) |
| 107 | { |
| 108 | #ifndef __EMX__ |
| 109 | long lrc; |
| 110 | // UPM procs return 0 on success |
| 111 | lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType); |
| 112 | if (lrc == 0) return TRUE; |
| 113 | #endif |
| 114 | return FALSE; |
| 115 | } |
| 116 | |
| 117 | bool wxGetUserName( |
| 118 | wxChar* zBuf |
| 119 | , int nMaxSize |
| 120 | ) |
| 121 | { |
| 122 | #ifdef USE_NET_API |
| 123 | wxGetUserId( zBuf |
| 124 | ,nMaxSize |
| 125 | ); |
| 126 | #else |
| 127 | wxStrncpy(zBuf, _T("Unknown User"), nMaxSize); |
| 128 | #endif |
| 129 | return TRUE; |
| 130 | } |
| 131 | |
| 132 | int wxKill( |
| 133 | long lPid |
| 134 | , int nSig |
| 135 | ) |
| 136 | { |
| 137 | return((int)::DosKillProcess(0, (PID)lPid)); |
| 138 | } |
| 139 | |
| 140 | // |
| 141 | // Execute a program in an Interactive Shell |
| 142 | // |
| 143 | bool wxShell( |
| 144 | const wxString& rCommand |
| 145 | ) |
| 146 | { |
| 147 | wxChar* zShell = _T("CMD.EXE"); |
| 148 | wxString sInputs; |
| 149 | wxChar zTmp[255]; |
| 150 | STARTDATA SData = {0}; |
| 151 | PSZ PgmTitle = "Command Shell"; |
| 152 | APIRET rc; |
| 153 | PID vPid = 0; |
| 154 | ULONG ulSessID = 0; |
| 155 | UCHAR achObjBuf[256] = {0}; //error data if DosStart fails |
| 156 | RESULTCODES vResult; |
| 157 | |
| 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; |
| 164 | |
| 165 | sInputs = "/C " + rCommand; |
| 166 | SData.PgmInputs = (BYTE*)sInputs.c_str(); |
| 167 | SData.TermQ = 0; |
| 168 | SData.Environment = 0; |
| 169 | SData.InheritOpt = SSF_INHERTOPT_SHELL; |
| 170 | SData.SessionType = SSF_TYPE_WINDOWABLEVIO; |
| 171 | SData.IconFile = 0; |
| 172 | SData.PgmHandle = 0; |
| 173 | SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE; |
| 174 | SData.InitXPos = 30; |
| 175 | SData.InitYPos = 40; |
| 176 | SData.InitXSize = 200; |
| 177 | SData.InitYSize = 140; |
| 178 | SData.Reserved = 0; |
| 179 | SData.ObjectBuffer = (char*)achObjBuf; |
| 180 | SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf); |
| 181 | |
| 182 | rc = ::DosStartSession(&SData, &ulSessID, &vPid); |
| 183 | if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND |
| 184 | { |
| 185 | PTIB ptib; |
| 186 | PPIB ppib; |
| 187 | |
| 188 | ::DosGetInfoBlocks(&ptib, &ppib); |
| 189 | |
| 190 | ::DosWaitChild( DCWA_PROCESS |
| 191 | ,DCWW_WAIT |
| 192 | ,&vResult |
| 193 | ,&ppib->pib_ulpid |
| 194 | ,vPid |
| 195 | ); |
| 196 | } |
| 197 | return (rc != 0); |
| 198 | } |
| 199 | |
| 200 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) |
| 201 | long wxGetFreeMemory() |
| 202 | { |
| 203 | void* pMemptr = NULL; |
| 204 | ULONG lSize; |
| 205 | ULONG lMemFlags; |
| 206 | APIRET rc; |
| 207 | |
| 208 | lMemFlags = PAG_FREE; |
| 209 | rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags); |
| 210 | if (rc != 0) |
| 211 | return -1L; |
| 212 | return (long)lSize; |
| 213 | } |
| 214 | |
| 215 | // ---------------------------------------------------------------------------- |
| 216 | // env vars |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | |
| 219 | bool wxGetEnv(const wxString& var, wxString *value) |
| 220 | { |
| 221 | // wxGetenv is defined as getenv() |
| 222 | wxChar *p = wxGetenv(var); |
| 223 | if ( !p ) |
| 224 | return FALSE; |
| 225 | |
| 226 | if ( value ) |
| 227 | { |
| 228 | *value = p; |
| 229 | } |
| 230 | |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
| 234 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
| 235 | { |
| 236 | #if defined(HAVE_SETENV) |
| 237 | return setenv(variable.mb_str(), value ? wxString(value).mb_str().data() |
| 238 | : NULL, 1 /* overwrite */) == 0; |
| 239 | #elif defined(HAVE_PUTENV) |
| 240 | wxString s = variable; |
| 241 | if ( value ) |
| 242 | s << _T('=') << value; |
| 243 | |
| 244 | // transform to ANSI |
| 245 | const char *p = s.mb_str(); |
| 246 | |
| 247 | // the string will be free()d by libc |
| 248 | char *buf = (char *)malloc(strlen(p) + 1); |
| 249 | strcpy(buf, p); |
| 250 | |
| 251 | return putenv(buf) == 0; |
| 252 | #else // no way to set an env var |
| 253 | return FALSE; |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | |
| 258 | // Sleep for nSecs seconds. Attempt a Windows implementation using timers. |
| 259 | static bool inTimer = FALSE; |
| 260 | |
| 261 | class wxSleepTimer: public wxTimer |
| 262 | { |
| 263 | public: |
| 264 | inline void Notify() |
| 265 | { |
| 266 | inTimer = FALSE; |
| 267 | Stop(); |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | static wxTimer* wxTheSleepTimer = NULL; |
| 272 | |
| 273 | void wxUsleep( |
| 274 | unsigned long ulMilliseconds |
| 275 | ) |
| 276 | { |
| 277 | ::DosSleep(ulMilliseconds/1000l); |
| 278 | } |
| 279 | |
| 280 | void wxSleep( |
| 281 | int nSecs |
| 282 | ) |
| 283 | { |
| 284 | ::DosSleep(1000 * nSecs); |
| 285 | } |
| 286 | |
| 287 | // Consume all events until no more left |
| 288 | void wxFlushEvents() |
| 289 | { |
| 290 | // wxYield(); |
| 291 | } |
| 292 | |
| 293 | // Output a debug mess., in a system dependent fashion. |
| 294 | void wxDebugMsg( |
| 295 | const wxChar* zFmt ... |
| 296 | ) |
| 297 | { |
| 298 | va_list vAp; |
| 299 | static wxChar zBuffer[512]; |
| 300 | |
| 301 | if (!wxTheApp->GetWantDebugOutput()) |
| 302 | return ; |
| 303 | va_start(vAp, zFmt); |
| 304 | sprintf(zBuffer, zFmt, vAp) ; |
| 305 | va_end(vAp); |
| 306 | } |
| 307 | |
| 308 | // Non-fatal error: pop up message box and (possibly) continue |
| 309 | void wxError( |
| 310 | const wxString& rMsg |
| 311 | , const wxString& rTitle |
| 312 | ) |
| 313 | { |
| 314 | wxBuffer = new wxChar[256]; |
| 315 | wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg); |
| 316 | if (::WinMessageBox( HWND_DESKTOP |
| 317 | ,NULL |
| 318 | ,(PSZ)wxBuffer |
| 319 | ,(PSZ)WXSTRINGCAST rTitle |
| 320 | ,0 |
| 321 | ,MB_ICONEXCLAMATION | MB_YESNO |
| 322 | ) == MBID_YES) |
| 323 | delete[] wxBuffer; |
| 324 | wxExit(); |
| 325 | } |
| 326 | |
| 327 | // Fatal error: pop up message box and abort |
| 328 | void wxFatalError( |
| 329 | const wxString& rMsg |
| 330 | , const wxString& rTitle |
| 331 | ) |
| 332 | { |
| 333 | unsigned long ulRc; |
| 334 | |
| 335 | ulRc = ::WinMessageBox( HWND_DESKTOP |
| 336 | ,NULL |
| 337 | ,WXSTRINGCAST rMsg |
| 338 | ,WXSTRINGCAST rTitle |
| 339 | ,0 |
| 340 | ,MB_NOICON | MB_OK |
| 341 | ); |
| 342 | DosExit(EXIT_PROCESS, ulRc); |
| 343 | } |
| 344 | |
| 345 | // Emit a beeeeeep |
| 346 | void wxBell() |
| 347 | { |
| 348 | DosBeep(1000,1000); // 1kHz during 1 sec. |
| 349 | } |
| 350 | |
| 351 | // Chris Breeze 27/5/98: revised WIN32 code to |
| 352 | // detect WindowsNT correctly |
| 353 | int wxGetOsVersion( |
| 354 | int* pMajorVsn |
| 355 | , int* pMinorVsn |
| 356 | ) |
| 357 | { |
| 358 | ULONG ulSysInfo[QSV_MAX] = {0}; |
| 359 | APIRET ulrc; |
| 360 | |
| 361 | ulrc = ::DosQuerySysInfo( 1L |
| 362 | ,QSV_MAX |
| 363 | ,(PVOID)ulSysInfo |
| 364 | ,sizeof(ULONG) * QSV_MAX |
| 365 | ); |
| 366 | if (ulrc == 0L) |
| 367 | { |
| 368 | *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR]; |
| 369 | *pMajorVsn = *pMajorVsn/10; |
| 370 | *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR]; |
| 371 | return wxWINDOWS_OS2; |
| 372 | } |
| 373 | return wxWINDOWS; // error if we get here, return generic value |
| 374 | } |
| 375 | |
| 376 | // Reading and writing resources (eg WIN.INI, .Xdefaults) |
| 377 | #if wxUSE_RESOURCES |
| 378 | bool wxWriteResource( |
| 379 | const wxString& rSection |
| 380 | , const wxString& rEntry |
| 381 | , const wxString& rValue |
| 382 | , const wxString& rFile |
| 383 | ) |
| 384 | { |
| 385 | HAB hab = 0; |
| 386 | HINI hIni = 0; |
| 387 | |
| 388 | if (rFile != "") |
| 389 | { |
| 390 | hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile); |
| 391 | if (hIni != 0L) |
| 392 | { |
| 393 | return (::PrfWriteProfileString( hIni |
| 394 | ,(PSZ)WXSTRINGCAST rSection |
| 395 | ,(PSZ)WXSTRINGCAST rEntry |
| 396 | ,(PSZ)WXSTRINGCAST rValue |
| 397 | )); |
| 398 | } |
| 399 | } |
| 400 | else |
| 401 | return (::PrfWriteProfileString( HINI_PROFILE |
| 402 | ,(PSZ)WXSTRINGCAST rSection |
| 403 | ,(PSZ)WXSTRINGCAST rEntry |
| 404 | ,(PSZ)WXSTRINGCAST rValue |
| 405 | )); |
| 406 | return FALSE; |
| 407 | } |
| 408 | |
| 409 | bool wxWriteResource( |
| 410 | const wxString& rSection |
| 411 | , const wxString& rEntry |
| 412 | , float fValue |
| 413 | , const wxString& rFile |
| 414 | ) |
| 415 | { |
| 416 | wxChar zBuf[50]; |
| 417 | |
| 418 | wxSprintf(zBuf, "%.4f", fValue); |
| 419 | return wxWriteResource( rSection |
| 420 | ,rEntry |
| 421 | ,zBuf |
| 422 | ,rFile |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | bool wxWriteResource( |
| 427 | const wxString& rSection |
| 428 | , const wxString& rEntry |
| 429 | , long lValue |
| 430 | , const wxString& rFile |
| 431 | ) |
| 432 | { |
| 433 | wxChar zBuf[50]; |
| 434 | |
| 435 | wxSprintf(zBuf, "%ld", lValue); |
| 436 | return wxWriteResource( rSection |
| 437 | ,rEntry |
| 438 | ,zBuf |
| 439 | ,rFile |
| 440 | ); |
| 441 | } |
| 442 | |
| 443 | bool wxWriteResource( |
| 444 | const wxString& rSection |
| 445 | , const wxString& rEntry |
| 446 | , int lValue |
| 447 | , const wxString& rFile |
| 448 | ) |
| 449 | { |
| 450 | wxChar zBuf[50]; |
| 451 | |
| 452 | wxSprintf(zBuf, "%d", lValue); |
| 453 | return wxWriteResource( rSection |
| 454 | ,rEntry |
| 455 | ,zBuf |
| 456 | ,rFile |
| 457 | ); |
| 458 | } |
| 459 | |
| 460 | bool wxGetResource( |
| 461 | const wxString& rSection |
| 462 | , const wxString& rEntry |
| 463 | , wxChar** ppValue |
| 464 | , const wxString& rFile |
| 465 | ) |
| 466 | { |
| 467 | HAB hab = 0; |
| 468 | HINI hIni = 0; |
| 469 | wxChar zDefunkt[] = _T("$$default"); |
| 470 | char zBuf[1000]; |
| 471 | |
| 472 | if (rFile != "") |
| 473 | { |
| 474 | hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile); |
| 475 | if (hIni != 0L) |
| 476 | { |
| 477 | ULONG n = ::PrfQueryProfileString( hIni |
| 478 | ,(PSZ)WXSTRINGCAST rSection |
| 479 | ,(PSZ)WXSTRINGCAST rEntry |
| 480 | ,(PSZ)zDefunkt |
| 481 | ,(PVOID)zBuf |
| 482 | ,1000 |
| 483 | ); |
| 484 | if (zBuf == NULL) |
| 485 | return FALSE; |
| 486 | if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0) |
| 487 | return FALSE; |
| 488 | zBuf[n-1] = '\0'; |
| 489 | } |
| 490 | else |
| 491 | return FALSE; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | ULONG n = ::PrfQueryProfileString( HINI_PROFILE |
| 496 | ,(PSZ)WXSTRINGCAST rSection |
| 497 | ,(PSZ)WXSTRINGCAST rEntry |
| 498 | ,(PSZ)zDefunkt |
| 499 | ,(PVOID)zBuf |
| 500 | ,1000 |
| 501 | ); |
| 502 | if (zBuf == NULL) |
| 503 | return FALSE; |
| 504 | if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0) |
| 505 | return FALSE; |
| 506 | zBuf[n-1] = '\0'; |
| 507 | } |
| 508 | strcpy((char*)*ppValue, zBuf); |
| 509 | return TRUE; |
| 510 | } |
| 511 | |
| 512 | bool wxGetResource( |
| 513 | const wxString& rSection |
| 514 | , const wxString& rEntry |
| 515 | , float* pValue |
| 516 | , const wxString& rFile |
| 517 | ) |
| 518 | { |
| 519 | wxChar* zStr = NULL; |
| 520 | |
| 521 | zStr = new wxChar[1000]; |
| 522 | bool bSucc = wxGetResource( rSection |
| 523 | ,rEntry |
| 524 | ,(wxChar **)&zStr |
| 525 | ,rFile |
| 526 | ); |
| 527 | |
| 528 | if (bSucc) |
| 529 | { |
| 530 | *pValue = (float)wxStrtod(zStr, NULL); |
| 531 | delete[] zStr; |
| 532 | return TRUE; |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | delete[] zStr; |
| 537 | return FALSE; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | bool wxGetResource( |
| 542 | const wxString& rSection |
| 543 | , const wxString& rEntry |
| 544 | , long* pValue |
| 545 | , const wxString& rFile |
| 546 | ) |
| 547 | { |
| 548 | wxChar* zStr = NULL; |
| 549 | |
| 550 | zStr = new wxChar[1000]; |
| 551 | bool bSucc = wxGetResource( rSection |
| 552 | ,rEntry |
| 553 | ,(wxChar **)&zStr |
| 554 | ,rFile |
| 555 | ); |
| 556 | |
| 557 | if (bSucc) |
| 558 | { |
| 559 | *pValue = wxStrtol(zStr, NULL, 10); |
| 560 | delete[] zStr; |
| 561 | return TRUE; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | delete[] zStr; |
| 566 | return FALSE; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | bool wxGetResource( |
| 571 | const wxString& rSection |
| 572 | , const wxString& rEntry |
| 573 | , int* pValue |
| 574 | , const wxString& rFile |
| 575 | ) |
| 576 | { |
| 577 | wxChar* zStr = NULL; |
| 578 | |
| 579 | zStr = new wxChar[1000]; |
| 580 | bool bSucc = wxGetResource( rSection |
| 581 | ,rEntry |
| 582 | ,(wxChar **)&zStr |
| 583 | ,rFile |
| 584 | ); |
| 585 | |
| 586 | if (bSucc) |
| 587 | { |
| 588 | *pValue = (int)wxStrtol(zStr, NULL, 10); |
| 589 | delete[] zStr; |
| 590 | return TRUE; |
| 591 | } |
| 592 | else |
| 593 | { |
| 594 | delete[] zStr; |
| 595 | return FALSE; |
| 596 | } |
| 597 | } |
| 598 | #endif // wxUSE_RESOURCES |
| 599 | |
| 600 | // --------------------------------------------------------------------------- |
| 601 | // helper functions for showing a "busy" cursor |
| 602 | // --------------------------------------------------------------------------- |
| 603 | |
| 604 | HCURSOR gs_wxBusyCursor = 0; // new, busy cursor |
| 605 | HCURSOR gs_wxBusyCursorOld = 0; // old cursor |
| 606 | static int gs_wxBusyCursorCount = 0; |
| 607 | |
| 608 | // Set the cursor to the busy cursor for all windows |
| 609 | void wxBeginBusyCursor( |
| 610 | wxCursor* pCursor |
| 611 | ) |
| 612 | { |
| 613 | if ( gs_wxBusyCursorCount++ == 0 ) |
| 614 | { |
| 615 | gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR(); |
| 616 | ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor); |
| 617 | } |
| 618 | //else: nothing to do, already set |
| 619 | } |
| 620 | |
| 621 | // Restore cursor to normal |
| 622 | void wxEndBusyCursor() |
| 623 | { |
| 624 | wxCHECK_RET( gs_wxBusyCursorCount > 0 |
| 625 | ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()") |
| 626 | ); |
| 627 | |
| 628 | if (--gs_wxBusyCursorCount == 0) |
| 629 | { |
| 630 | ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld); |
| 631 | gs_wxBusyCursorOld = 0; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // TRUE if we're between the above two calls |
| 636 | bool wxIsBusy() |
| 637 | { |
| 638 | return (gs_wxBusyCursorCount > 0); |
| 639 | } |
| 640 | |
| 641 | // --------------------------------------------------------------------------- |
| 642 | const wxChar* wxGetHomeDir( |
| 643 | wxString* pStr |
| 644 | ) |
| 645 | { |
| 646 | wxString& rStrDir = *pStr; |
| 647 | |
| 648 | // OS/2 has no idea about home, |
| 649 | // so use the working directory instead? |
| 650 | |
| 651 | // 256 was taken from os2def.h |
| 652 | #ifndef MAX_PATH |
| 653 | # define MAX_PATH 256 |
| 654 | #endif |
| 655 | |
| 656 | char zDirName[256]; |
| 657 | ULONG ulDirLen; |
| 658 | |
| 659 | ::DosQueryCurrentDir(0, zDirName, &ulDirLen); |
| 660 | rStrDir = zDirName; |
| 661 | return rStrDir.c_str(); |
| 662 | } |
| 663 | |
| 664 | // Hack for OS/2 |
| 665 | wxChar* wxGetUserHome ( |
| 666 | const wxString& rUser |
| 667 | ) |
| 668 | { |
| 669 | wxChar* zHome; |
| 670 | wxString sUser1(rUser); |
| 671 | |
| 672 | wxBuffer = new wxChar[256]; |
| 673 | #ifndef __EMX__ |
| 674 | if (sUser1 != _T("")) |
| 675 | { |
| 676 | wxChar zTmp[64]; |
| 677 | |
| 678 | if (wxGetUserId( zTmp |
| 679 | ,sizeof(zTmp)/sizeof(char) |
| 680 | )) |
| 681 | { |
| 682 | // Guests belong in the temp dir |
| 683 | if (wxStricmp(zTmp, _T("annonymous")) == 0) |
| 684 | { |
| 685 | if ((zHome = wxGetenv(_T("TMP"))) != NULL || |
| 686 | (zHome = wxGetenv(_T("TMPDIR"))) != NULL || |
| 687 | (zHome = wxGetenv(_T("TEMP"))) != NULL) |
| 688 | delete[] wxBuffer; |
| 689 | return *zHome ? zHome : (wxChar*)_T("\\"); |
| 690 | } |
| 691 | if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0) |
| 692 | sUser1 = _T(""); |
| 693 | } |
| 694 | } |
| 695 | #endif |
| 696 | if (sUser1 == _T("")) |
| 697 | { |
| 698 | if ((zHome = wxGetenv(_T("HOME"))) != NULL) |
| 699 | { |
| 700 | wxStrcpy(wxBuffer, zHome); |
| 701 | Unix2DosFilename(wxBuffer); |
| 702 | wxStrcpy(zHome, wxBuffer); |
| 703 | delete[] wxBuffer; |
| 704 | return zHome; |
| 705 | } |
| 706 | } |
| 707 | delete[] wxBuffer; |
| 708 | return NULL; // No home known! |
| 709 | } |
| 710 | |
| 711 | // Check whether this window wants to process messages, e.g. Stop button |
| 712 | // in long calculations. |
| 713 | bool wxCheckForInterrupt( |
| 714 | wxWindow* pWnd |
| 715 | ) |
| 716 | { |
| 717 | if(pWnd) |
| 718 | { |
| 719 | QMSG vMsg; |
| 720 | HAB hab = 0; |
| 721 | HWND hwndFilter = NULLHANDLE; |
| 722 | HWND hwndWin= (HWND) pWnd->GetHWND(); |
| 723 | |
| 724 | while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE)) |
| 725 | { |
| 726 | ::WinDispatchMsg(hab, &vMsg); |
| 727 | } |
| 728 | return TRUE;//*** temporary? |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | wxFAIL_MSG(_T("pWnd==NULL !!!")); |
| 733 | return FALSE;//*** temporary? |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | void wxGetMousePosition( |
| 738 | int* pX |
| 739 | , int* pY |
| 740 | ) |
| 741 | { |
| 742 | POINTL vPt; |
| 743 | |
| 744 | ::WinQueryPointerPos(HWND_DESKTOP, &vPt); |
| 745 | *pX = vPt.x; |
| 746 | *pY = vPt.y; |
| 747 | }; |
| 748 | |
| 749 | // Return TRUE if we have a colour display |
| 750 | bool wxColourDisplay() |
| 751 | { |
| 752 | HPS hpsScreen; |
| 753 | HDC hdcScreen; |
| 754 | LONG lColors; |
| 755 | |
| 756 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); |
| 757 | hdcScreen = ::GpiQueryDevice(hpsScreen); |
| 758 | ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors); |
| 759 | return(lColors > 1L); |
| 760 | } |
| 761 | |
| 762 | // Returns depth of screen |
| 763 | int wxDisplayDepth() |
| 764 | { |
| 765 | HPS hpsScreen; |
| 766 | HDC hdcScreen; |
| 767 | LONG lPlanes; |
| 768 | LONG lBitsPerPixel; |
| 769 | LONG nDepth; |
| 770 | |
| 771 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); |
| 772 | hdcScreen = ::GpiQueryDevice(hpsScreen); |
| 773 | ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes); |
| 774 | ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel); |
| 775 | |
| 776 | nDepth = (int)(lPlanes * lBitsPerPixel); |
| 777 | DevCloseDC(hdcScreen); |
| 778 | return (nDepth); |
| 779 | } |
| 780 | |
| 781 | // Get size of display |
| 782 | void wxDisplaySize( |
| 783 | int* pWidth |
| 784 | , int* pHeight |
| 785 | ) |
| 786 | { |
| 787 | HPS hpsScreen; |
| 788 | HDC hdcScreen; |
| 789 | LONG lWidth; |
| 790 | LONG lHeight; |
| 791 | |
| 792 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); |
| 793 | hdcScreen = ::GpiQueryDevice(hpsScreen); |
| 794 | ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, &lWidth); |
| 795 | ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, &lHeight); |
| 796 | DevCloseDC(hdcScreen); |
| 797 | *pWidth = (int)lWidth; |
| 798 | *pHeight = (int)lHeight; |
| 799 | } |
| 800 | |
| 801 | void wxDisplaySizeMM( |
| 802 | int* pWidth |
| 803 | , int* pHeight |
| 804 | ) |
| 805 | { |
| 806 | HPS hpsScreen; |
| 807 | HDC hdcScreen; |
| 808 | |
| 809 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); |
| 810 | hdcScreen = ::GpiQueryDevice(hpsScreen); |
| 811 | |
| 812 | if (pWidth) |
| 813 | ::DevQueryCaps( hdcScreen |
| 814 | ,CAPS_HORIZONTAL_RESOLUTION |
| 815 | ,1L |
| 816 | ,(PLONG)pWidth |
| 817 | ); |
| 818 | if (pHeight) |
| 819 | ::DevQueryCaps( hdcScreen |
| 820 | ,CAPS_VERTICAL_RESOLUTION |
| 821 | ,1L |
| 822 | ,(PLONG)pHeight |
| 823 | ); |
| 824 | } |
| 825 | |
| 826 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
| 827 | { |
| 828 | // This is supposed to return desktop dimensions minus any window |
| 829 | // manager panels, menus, taskbars, etc. If there is a way to do that |
| 830 | // for this platform please fix this function, otherwise it defaults |
| 831 | // to the entire desktop. |
| 832 | if (x) *x = 0; |
| 833 | if (y) *y = 0; |
| 834 | wxDisplaySize(width, height); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | bool wxDirExists( |
| 839 | const wxString& rDir |
| 840 | ) |
| 841 | { |
| 842 | return (::DosSetCurrentDir(WXSTRINGCAST rDir)); |
| 843 | } |
| 844 | |
| 845 | // --------------------------------------------------------------------------- |
| 846 | // window information functions |
| 847 | // --------------------------------------------------------------------------- |
| 848 | |
| 849 | wxString WXDLLEXPORT wxGetWindowText( |
| 850 | WXHWND hWnd |
| 851 | ) |
| 852 | { |
| 853 | wxString vStr; |
| 854 | long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1; |
| 855 | |
| 856 | ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen)); |
| 857 | vStr.UngetWriteBuf(); |
| 858 | |
| 859 | return vStr; |
| 860 | } |
| 861 | |
| 862 | wxString WXDLLEXPORT wxGetWindowClass( |
| 863 | WXHWND hWnd |
| 864 | ) |
| 865 | { |
| 866 | wxString vStr; |
| 867 | int nLen = 256; // some starting value |
| 868 | |
| 869 | for ( ;; ) |
| 870 | { |
| 871 | int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen)); |
| 872 | |
| 873 | vStr.UngetWriteBuf(); |
| 874 | if (nCount == nLen ) |
| 875 | { |
| 876 | // the class name might have been truncated, retry with larger |
| 877 | // buffer |
| 878 | nLen *= 2; |
| 879 | } |
| 880 | else |
| 881 | { |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | return vStr; |
| 886 | } |
| 887 | |
| 888 | WXWORD WXDLLEXPORT wxGetWindowId( |
| 889 | WXHWND hWnd |
| 890 | ) |
| 891 | { |
| 892 | return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID); |
| 893 | } |
| 894 | |
| 895 | wxString WXDLLEXPORT wxPMErrorToStr( |
| 896 | ERRORID vError |
| 897 | ) |
| 898 | { |
| 899 | wxString sError; |
| 900 | |
| 901 | // |
| 902 | // Remove the high order byte -- it is useless |
| 903 | // |
| 904 | vError &= 0x0000ffff; |
| 905 | switch(vError) |
| 906 | { |
| 907 | case PMERR_INVALID_HWND: |
| 908 | sError = wxT("Invalid window handle specified"); |
| 909 | break; |
| 910 | |
| 911 | case PMERR_INVALID_FLAG: |
| 912 | sError = wxT("Invalid flag bit set"); |
| 913 | break; |
| 914 | |
| 915 | case PMERR_NO_MSG_QUEUE: |
| 916 | sError = wxT("No message queue available"); |
| 917 | break; |
| 918 | |
| 919 | case PMERR_INVALID_PARM: |
| 920 | sError = wxT("Parameter contained invalid data"); |
| 921 | break; |
| 922 | |
| 923 | case PMERR_INVALID_PARAMETERS: |
| 924 | sError = wxT("Parameter value is out of range"); |
| 925 | break; |
| 926 | |
| 927 | case PMERR_PARAMETER_OUT_OF_RANGE: |
| 928 | sError = wxT("Parameter value is out of range"); |
| 929 | break; |
| 930 | |
| 931 | case PMERR_INVALID_INTEGER_ATOM: |
| 932 | sError = wxT("Not a valid atom"); |
| 933 | break; |
| 934 | |
| 935 | case PMERR_INVALID_HATOMTBL: |
| 936 | sError = wxT("Atom table handle is invalid"); |
| 937 | break; |
| 938 | |
| 939 | case PMERR_INVALID_ATOM_NAME: |
| 940 | sError = wxT("Not a valid atom name"); |
| 941 | break; |
| 942 | |
| 943 | case PMERR_ATOM_NAME_NOT_FOUND: |
| 944 | sError = wxT("Valid name format, but cannot find name in atom table"); |
| 945 | break; |
| 946 | |
| 947 | default: |
| 948 | sError = wxT("Unknown error"); |
| 949 | } |
| 950 | return(sError); |
| 951 | } // end of wxPMErrorToStr |
| 952 | |
| 953 | |