| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: utilscmn.cpp |
| 3 | // Purpose: Miscellaneous utility functions and classes |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 29/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && !defined(__EMX__) |
| 21 | // Some older compilers (such as EMX) cannot handle |
| 22 | // #pragma interface/implementation correctly, iff |
| 23 | // #pragma implementation is used in _two_ translation |
| 24 | // units (as created by e.g. event.cpp compiled for |
| 25 | // libwx_base and event.cpp compiled for libwx_gui_core). |
| 26 | // So we must not use those pragmas for those compilers in |
| 27 | // such files. |
| 28 | #pragma implementation "utils.h" |
| 29 | #endif |
| 30 | |
| 31 | // For compilers that support precompilation, includes "wx.h". |
| 32 | #include "wx/wxprec.h" |
| 33 | |
| 34 | #ifdef __BORLANDC__ |
| 35 | #pragma hdrstop |
| 36 | #endif |
| 37 | |
| 38 | #ifndef WX_PRECOMP |
| 39 | #include "wx/app.h" |
| 40 | #include "wx/string.h" |
| 41 | #include "wx/utils.h" |
| 42 | #include "wx/intl.h" |
| 43 | #include "wx/log.h" |
| 44 | |
| 45 | #if wxUSE_GUI |
| 46 | #include "wx/window.h" |
| 47 | #include "wx/frame.h" |
| 48 | #include "wx/menu.h" |
| 49 | #include "wx/msgdlg.h" |
| 50 | #include "wx/textdlg.h" |
| 51 | #include "wx/textctrl.h" // for wxTE_PASSWORD |
| 52 | #if wxUSE_ACCEL |
| 53 | #include "wx/menuitem.h" |
| 54 | #include "wx/accel.h" |
| 55 | #endif // wxUSE_ACCEL |
| 56 | #endif // wxUSE_GUI |
| 57 | #endif // WX_PRECOMP |
| 58 | |
| 59 | #include "wx/apptrait.h" |
| 60 | |
| 61 | #include "wx/process.h" |
| 62 | #include "wx/txtstrm.h" |
| 63 | |
| 64 | #if defined(__WXWINCE__) && wxUSE_DATETIME |
| 65 | #include "wx/datetime.h" |
| 66 | #endif |
| 67 | |
| 68 | #include <ctype.h> |
| 69 | #include <stdio.h> |
| 70 | #include <stdlib.h> |
| 71 | #include <string.h> |
| 72 | |
| 73 | #if !defined(__WATCOMC__) |
| 74 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
| 75 | #include <errno.h> |
| 76 | #endif |
| 77 | #endif |
| 78 | |
| 79 | #if wxUSE_GUI |
| 80 | #include "wx/colordlg.h" |
| 81 | #include "wx/fontdlg.h" |
| 82 | #include "wx/notebook.h" |
| 83 | #include "wx/frame.h" |
| 84 | #include "wx/statusbr.h" |
| 85 | #endif // wxUSE_GUI |
| 86 | |
| 87 | #ifndef __WXWINCE__ |
| 88 | #include <time.h> |
| 89 | #else |
| 90 | #include "wx/msw/wince/time.h" |
| 91 | #endif |
| 92 | |
| 93 | #if !defined(__MWERKS__) && !defined(__WXWINCE__) |
| 94 | #include <sys/types.h> |
| 95 | #include <sys/stat.h> |
| 96 | #endif |
| 97 | |
| 98 | #ifdef __WXMSW__ |
| 99 | #include "wx/msw/private.h" |
| 100 | #endif |
| 101 | |
| 102 | #if wxUSE_BASE |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | // common data |
| 106 | // ---------------------------------------------------------------------------- |
| 107 | |
| 108 | #if WXWIN_COMPATIBILITY_2_2 |
| 109 | const wxChar *wxInternalErrorStr = wxT("wxWindows Internal Error"); |
| 110 | const wxChar *wxFatalErrorStr = wxT("wxWindows Fatal Error"); |
| 111 | #endif // WXWIN_COMPATIBILITY_2_2 |
| 112 | |
| 113 | // ============================================================================ |
| 114 | // implementation |
| 115 | // ============================================================================ |
| 116 | |
| 117 | #if WXWIN_COMPATIBILITY_2_4 |
| 118 | |
| 119 | wxChar * |
| 120 | copystring (const wxChar *s) |
| 121 | { |
| 122 | if (s == NULL) s = wxT(""); |
| 123 | size_t len = wxStrlen (s) + 1; |
| 124 | |
| 125 | wxChar *news = new wxChar[len]; |
| 126 | memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest |
| 127 | |
| 128 | return news; |
| 129 | } |
| 130 | |
| 131 | #endif // WXWIN_COMPATIBILITY_2_4 |
| 132 | |
| 133 | // ---------------------------------------------------------------------------- |
| 134 | // String <-> Number conversions (deprecated) |
| 135 | // ---------------------------------------------------------------------------- |
| 136 | |
| 137 | #if WXWIN_COMPATIBILITY_2_4 |
| 138 | |
| 139 | WXDLLIMPEXP_DATA_BASE(const wxChar *) wxFloatToStringStr = wxT("%.2f"); |
| 140 | WXDLLIMPEXP_DATA_BASE(const wxChar *) wxDoubleToStringStr = wxT("%.2f"); |
| 141 | |
| 142 | void |
| 143 | StringToFloat (const wxChar *s, float *number) |
| 144 | { |
| 145 | if (s && *s && number) |
| 146 | *number = (float) wxStrtod (s, (wxChar **) NULL); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | StringToDouble (const wxChar *s, double *number) |
| 151 | { |
| 152 | if (s && *s && number) |
| 153 | *number = wxStrtod (s, (wxChar **) NULL); |
| 154 | } |
| 155 | |
| 156 | wxChar * |
| 157 | FloatToString (float number, const wxChar *fmt) |
| 158 | { |
| 159 | static wxChar buf[256]; |
| 160 | |
| 161 | wxSprintf (buf, fmt, number); |
| 162 | return buf; |
| 163 | } |
| 164 | |
| 165 | wxChar * |
| 166 | DoubleToString (double number, const wxChar *fmt) |
| 167 | { |
| 168 | static wxChar buf[256]; |
| 169 | |
| 170 | wxSprintf (buf, fmt, number); |
| 171 | return buf; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | StringToInt (const wxChar *s, int *number) |
| 176 | { |
| 177 | if (s && *s && number) |
| 178 | *number = (int) wxStrtol (s, (wxChar **) NULL, 10); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | StringToLong (const wxChar *s, long *number) |
| 183 | { |
| 184 | if (s && *s && number) |
| 185 | *number = wxStrtol (s, (wxChar **) NULL, 10); |
| 186 | } |
| 187 | |
| 188 | wxChar * |
| 189 | IntToString (int number) |
| 190 | { |
| 191 | static wxChar buf[20]; |
| 192 | |
| 193 | wxSprintf (buf, wxT("%d"), number); |
| 194 | return buf; |
| 195 | } |
| 196 | |
| 197 | wxChar * |
| 198 | LongToString (long number) |
| 199 | { |
| 200 | static wxChar buf[20]; |
| 201 | |
| 202 | wxSprintf (buf, wxT("%ld"), number); |
| 203 | return buf; |
| 204 | } |
| 205 | |
| 206 | #endif // WXWIN_COMPATIBILITY_2_4 |
| 207 | |
| 208 | // Array used in DecToHex conversion routine. |
| 209 | static wxChar hexArray[] = wxT("0123456789ABCDEF"); |
| 210 | |
| 211 | // Convert 2-digit hex number to decimal |
| 212 | int wxHexToDec(const wxString& buf) |
| 213 | { |
| 214 | int firstDigit, secondDigit; |
| 215 | |
| 216 | if (buf.GetChar(0) >= wxT('A')) |
| 217 | firstDigit = buf.GetChar(0) - wxT('A') + 10; |
| 218 | else |
| 219 | firstDigit = buf.GetChar(0) - wxT('0'); |
| 220 | |
| 221 | if (buf.GetChar(1) >= wxT('A')) |
| 222 | secondDigit = buf.GetChar(1) - wxT('A') + 10; |
| 223 | else |
| 224 | secondDigit = buf.GetChar(1) - wxT('0'); |
| 225 | |
| 226 | return (firstDigit & 0xF) * 16 + (secondDigit & 0xF ); |
| 227 | } |
| 228 | |
| 229 | // Convert decimal integer to 2-character hex string |
| 230 | void wxDecToHex(int dec, wxChar *buf) |
| 231 | { |
| 232 | int firstDigit = (int)(dec/16.0); |
| 233 | int secondDigit = (int)(dec - (firstDigit*16.0)); |
| 234 | buf[0] = hexArray[firstDigit]; |
| 235 | buf[1] = hexArray[secondDigit]; |
| 236 | buf[2] = 0; |
| 237 | } |
| 238 | |
| 239 | // Convert decimal integer to 2-character hex string |
| 240 | wxString wxDecToHex(int dec) |
| 241 | { |
| 242 | wxChar buf[3]; |
| 243 | wxDecToHex(dec, buf); |
| 244 | return wxString(buf); |
| 245 | } |
| 246 | |
| 247 | // ---------------------------------------------------------------------------- |
| 248 | // misc functions |
| 249 | // ---------------------------------------------------------------------------- |
| 250 | |
| 251 | // Return the current date/time |
| 252 | wxString wxNow() |
| 253 | { |
| 254 | #ifdef __WXWINCE__ |
| 255 | #if wxUSE_DATETIME |
| 256 | wxDateTime now = wxDateTime::Now(); |
| 257 | return now.Format(); |
| 258 | #else |
| 259 | return wxEmptyString; |
| 260 | #endif |
| 261 | #else |
| 262 | time_t now = time((time_t *) NULL); |
| 263 | char *date = ctime(&now); |
| 264 | date[24] = '\0'; |
| 265 | return wxString::FromAscii(date); |
| 266 | #endif |
| 267 | } |
| 268 | |
| 269 | const wxChar *wxGetInstallPrefix() |
| 270 | { |
| 271 | wxString prefix; |
| 272 | |
| 273 | if ( wxGetEnv(wxT("WXPREFIX"), &prefix) ) |
| 274 | return prefix.c_str(); |
| 275 | |
| 276 | #ifdef wxINSTALL_PREFIX |
| 277 | return wxT(wxINSTALL_PREFIX); |
| 278 | #else |
| 279 | return wxT(""); |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | wxString wxGetDataDir() |
| 284 | { |
| 285 | wxString format = wxGetInstallPrefix(); |
| 286 | format << wxFILE_SEP_PATH |
| 287 | << wxT("share") << wxFILE_SEP_PATH |
| 288 | << wxT("wx") << wxFILE_SEP_PATH |
| 289 | << wxT("%i.%i"); |
| 290 | wxString dir; |
| 291 | dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION); |
| 292 | return dir; |
| 293 | } |
| 294 | |
| 295 | int wxGetOsVersion(int *verMaj, int *verMin) |
| 296 | { |
| 297 | // we want this function to work even if there is no wxApp |
| 298 | wxConsoleAppTraits traitsConsole; |
| 299 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; |
| 300 | if ( ! traits ) |
| 301 | traits = &traitsConsole; |
| 302 | |
| 303 | wxToolkitInfo& info = traits->GetToolkitInfo(); |
| 304 | if ( verMaj ) |
| 305 | *verMaj = info.versionMajor; |
| 306 | if ( verMin ) |
| 307 | *verMin = info.versionMinor; |
| 308 | return info.os; |
| 309 | } |
| 310 | |
| 311 | // ---------------------------------------------------------------------------- |
| 312 | // network and user id functions |
| 313 | // ---------------------------------------------------------------------------- |
| 314 | |
| 315 | // Get Full RFC822 style email address |
| 316 | bool wxGetEmailAddress(wxChar *address, int maxSize) |
| 317 | { |
| 318 | wxString email = wxGetEmailAddress(); |
| 319 | if ( !email ) |
| 320 | return FALSE; |
| 321 | |
| 322 | wxStrncpy(address, email, maxSize - 1); |
| 323 | address[maxSize - 1] = wxT('\0'); |
| 324 | |
| 325 | return TRUE; |
| 326 | } |
| 327 | |
| 328 | wxString wxGetEmailAddress() |
| 329 | { |
| 330 | wxString email; |
| 331 | |
| 332 | wxString host = wxGetFullHostName(); |
| 333 | if ( !!host ) |
| 334 | { |
| 335 | wxString user = wxGetUserId(); |
| 336 | if ( !!user ) |
| 337 | { |
| 338 | email << user << wxT('@') << host; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return email; |
| 343 | } |
| 344 | |
| 345 | wxString wxGetUserId() |
| 346 | { |
| 347 | static const int maxLoginLen = 256; // FIXME arbitrary number |
| 348 | |
| 349 | wxString buf; |
| 350 | bool ok = wxGetUserId(wxStringBuffer(buf, maxLoginLen), maxLoginLen); |
| 351 | |
| 352 | if ( !ok ) |
| 353 | buf.Empty(); |
| 354 | |
| 355 | return buf; |
| 356 | } |
| 357 | |
| 358 | wxString wxGetUserName() |
| 359 | { |
| 360 | static const int maxUserNameLen = 1024; // FIXME arbitrary number |
| 361 | |
| 362 | wxString buf; |
| 363 | bool ok = wxGetUserName(wxStringBuffer(buf, maxUserNameLen), maxUserNameLen); |
| 364 | |
| 365 | if ( !ok ) |
| 366 | buf.Empty(); |
| 367 | |
| 368 | return buf; |
| 369 | } |
| 370 | |
| 371 | wxString wxGetHostName() |
| 372 | { |
| 373 | static const size_t hostnameSize = 257; |
| 374 | |
| 375 | wxString buf; |
| 376 | bool ok = wxGetHostName(wxStringBuffer(buf, hostnameSize), hostnameSize); |
| 377 | |
| 378 | if ( !ok ) |
| 379 | buf.Empty(); |
| 380 | |
| 381 | return buf; |
| 382 | } |
| 383 | |
| 384 | wxString wxGetFullHostName() |
| 385 | { |
| 386 | static const size_t hostnameSize = 257; |
| 387 | |
| 388 | wxString buf; |
| 389 | bool ok = wxGetFullHostName(wxStringBuffer(buf, hostnameSize), hostnameSize); |
| 390 | |
| 391 | if ( !ok ) |
| 392 | buf.Empty(); |
| 393 | |
| 394 | return buf; |
| 395 | } |
| 396 | |
| 397 | wxString wxGetHomeDir() |
| 398 | { |
| 399 | wxString home; |
| 400 | wxGetHomeDir(&home); |
| 401 | |
| 402 | return home; |
| 403 | } |
| 404 | |
| 405 | #if 0 |
| 406 | |
| 407 | wxString wxGetCurrentDir() |
| 408 | { |
| 409 | wxString dir; |
| 410 | size_t len = 1024; |
| 411 | bool ok; |
| 412 | do |
| 413 | { |
| 414 | ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL; |
| 415 | dir.UngetWriteBuf(); |
| 416 | |
| 417 | if ( !ok ) |
| 418 | { |
| 419 | if ( errno != ERANGE ) |
| 420 | { |
| 421 | wxLogSysError(_T("Failed to get current directory")); |
| 422 | |
| 423 | return wxEmptyString; |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | // buffer was too small, retry with a larger one |
| 428 | len *= 2; |
| 429 | } |
| 430 | } |
| 431 | //else: ok |
| 432 | } while ( !ok ); |
| 433 | |
| 434 | return dir; |
| 435 | } |
| 436 | |
| 437 | #endif // 0 |
| 438 | |
| 439 | // ---------------------------------------------------------------------------- |
| 440 | // wxExecute |
| 441 | // ---------------------------------------------------------------------------- |
| 442 | |
| 443 | // wxDoExecuteWithCapture() helper: reads an entire stream into one array |
| 444 | // |
| 445 | // returns TRUE if ok, FALSE if error |
| 446 | #if wxUSE_STREAMS |
| 447 | static bool ReadAll(wxInputStream *is, wxArrayString& output) |
| 448 | { |
| 449 | wxCHECK_MSG( is, FALSE, _T("NULL stream in wxExecute()?") ); |
| 450 | |
| 451 | // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state |
| 452 | is->Reset(); |
| 453 | |
| 454 | wxTextInputStream tis(*is); |
| 455 | |
| 456 | bool cont = TRUE; |
| 457 | while ( cont ) |
| 458 | { |
| 459 | wxString line = tis.ReadLine(); |
| 460 | if ( is->Eof() ) |
| 461 | break; |
| 462 | |
| 463 | if ( !*is ) |
| 464 | { |
| 465 | cont = FALSE; |
| 466 | } |
| 467 | else |
| 468 | { |
| 469 | output.Add(line); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return cont; |
| 474 | } |
| 475 | #endif // wxUSE_STREAMS |
| 476 | |
| 477 | // this is a private function because it hasn't a clean interface: the first |
| 478 | // array is passed by reference, the second by pointer - instead we have 2 |
| 479 | // public versions of wxExecute() below |
| 480 | static long wxDoExecuteWithCapture(const wxString& command, |
| 481 | wxArrayString& output, |
| 482 | wxArrayString* error) |
| 483 | { |
| 484 | // create a wxProcess which will capture the output |
| 485 | wxProcess *process = new wxProcess; |
| 486 | process->Redirect(); |
| 487 | |
| 488 | long rc = wxExecute(command, wxEXEC_SYNC, process); |
| 489 | |
| 490 | #if wxUSE_STREAMS |
| 491 | if ( rc != -1 ) |
| 492 | { |
| 493 | if ( !ReadAll(process->GetInputStream(), output) ) |
| 494 | rc = -1; |
| 495 | |
| 496 | if ( error ) |
| 497 | { |
| 498 | if ( !ReadAll(process->GetErrorStream(), *error) ) |
| 499 | rc = -1; |
| 500 | } |
| 501 | |
| 502 | } |
| 503 | #endif // wxUSE_STREAMS |
| 504 | |
| 505 | delete process; |
| 506 | |
| 507 | return rc; |
| 508 | } |
| 509 | |
| 510 | long wxExecute(const wxString& command, wxArrayString& output) |
| 511 | { |
| 512 | return wxDoExecuteWithCapture(command, output, NULL); |
| 513 | } |
| 514 | |
| 515 | long wxExecute(const wxString& command, |
| 516 | wxArrayString& output, |
| 517 | wxArrayString& error) |
| 518 | { |
| 519 | return wxDoExecuteWithCapture(command, output, &error); |
| 520 | } |
| 521 | |
| 522 | // ---------------------------------------------------------------------------- |
| 523 | // wxApp::Yield() wrappers for backwards compatibility |
| 524 | // ---------------------------------------------------------------------------- |
| 525 | |
| 526 | bool wxYield() |
| 527 | { |
| 528 | return wxTheApp && wxTheApp->Yield(); |
| 529 | } |
| 530 | |
| 531 | bool wxYieldIfNeeded() |
| 532 | { |
| 533 | return wxTheApp && wxTheApp->Yield(TRUE); |
| 534 | } |
| 535 | |
| 536 | #endif // wxUSE_BASE |
| 537 | |
| 538 | // ============================================================================ |
| 539 | // GUI-only functions from now on |
| 540 | // ============================================================================ |
| 541 | |
| 542 | #if wxUSE_GUI |
| 543 | |
| 544 | // Id generation |
| 545 | static long wxCurrentId = 100; |
| 546 | |
| 547 | long |
| 548 | wxNewId (void) |
| 549 | { |
| 550 | return wxCurrentId++; |
| 551 | } |
| 552 | |
| 553 | long |
| 554 | wxGetCurrentId(void) { return wxCurrentId; } |
| 555 | |
| 556 | void |
| 557 | wxRegisterId (long id) |
| 558 | { |
| 559 | if (id >= wxCurrentId) |
| 560 | wxCurrentId = id + 1; |
| 561 | } |
| 562 | |
| 563 | #if wxUSE_MENUS |
| 564 | |
| 565 | // ---------------------------------------------------------------------------- |
| 566 | // Menu accelerators related functions |
| 567 | // ---------------------------------------------------------------------------- |
| 568 | |
| 569 | wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out) |
| 570 | { |
| 571 | wxString s = wxMenuItem::GetLabelFromText(in); |
| 572 | if ( out ) |
| 573 | { |
| 574 | // go smash their buffer if it's not big enough - I love char * params |
| 575 | memcpy(out, s.c_str(), s.length() * sizeof(wxChar)); |
| 576 | } |
| 577 | else |
| 578 | { |
| 579 | // MYcopystring - for easier search... |
| 580 | out = new wxChar[s.length() + 1]; |
| 581 | wxStrcpy(out, s.c_str()); |
| 582 | } |
| 583 | |
| 584 | return out; |
| 585 | } |
| 586 | |
| 587 | wxString wxStripMenuCodes(const wxString& in) |
| 588 | { |
| 589 | wxString out; |
| 590 | |
| 591 | size_t len = in.length(); |
| 592 | out.reserve(len); |
| 593 | |
| 594 | for ( size_t n = 0; n < len; n++ ) |
| 595 | { |
| 596 | wxChar ch = in[n]; |
| 597 | if ( ch == _T('&') ) |
| 598 | { |
| 599 | // skip it, it is used to introduce the accel char (or to quote |
| 600 | // itself in which case it should still be skipped): note that it |
| 601 | // can't be the last character of the string |
| 602 | if ( ++n == len ) |
| 603 | { |
| 604 | wxLogDebug(_T("Invalid menu string '%s'"), in.c_str()); |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | // use the next char instead |
| 609 | ch = in[n]; |
| 610 | } |
| 611 | } |
| 612 | else if ( ch == _T('\t') ) |
| 613 | { |
| 614 | // everything after TAB is accel string, exit the loop |
| 615 | break; |
| 616 | } |
| 617 | |
| 618 | out += ch; |
| 619 | } |
| 620 | |
| 621 | return out; |
| 622 | } |
| 623 | |
| 624 | #endif // wxUSE_MENUS |
| 625 | |
| 626 | // ---------------------------------------------------------------------------- |
| 627 | // Window search functions |
| 628 | // ---------------------------------------------------------------------------- |
| 629 | |
| 630 | /* |
| 631 | * If parent is non-NULL, look through children for a label or title |
| 632 | * matching the specified string. If NULL, look through all top-level windows. |
| 633 | * |
| 634 | */ |
| 635 | |
| 636 | wxWindow * |
| 637 | wxFindWindowByLabel (const wxString& title, wxWindow * parent) |
| 638 | { |
| 639 | return wxWindow::FindWindowByLabel( title, parent ); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | /* |
| 644 | * If parent is non-NULL, look through children for a name |
| 645 | * matching the specified string. If NULL, look through all top-level windows. |
| 646 | * |
| 647 | */ |
| 648 | |
| 649 | wxWindow * |
| 650 | wxFindWindowByName (const wxString& name, wxWindow * parent) |
| 651 | { |
| 652 | return wxWindow::FindWindowByName( name, parent ); |
| 653 | } |
| 654 | |
| 655 | // Returns menu item id or -1 if none. |
| 656 | int |
| 657 | wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString) |
| 658 | { |
| 659 | #if wxUSE_MENUS |
| 660 | wxMenuBar *menuBar = frame->GetMenuBar (); |
| 661 | if ( menuBar ) |
| 662 | return menuBar->FindMenuItem (menuString, itemString); |
| 663 | #endif // wxUSE_MENUS |
| 664 | |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | // Try to find the deepest child that contains 'pt'. |
| 669 | // We go backwards, to try to allow for controls that are spacially |
| 670 | // within other controls, but are still siblings (e.g. buttons within |
| 671 | // static boxes). Static boxes are likely to be created _before_ controls |
| 672 | // that sit inside them. |
| 673 | wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt) |
| 674 | { |
| 675 | if (!win->IsShown()) |
| 676 | return NULL; |
| 677 | |
| 678 | // Hack for wxNotebook case: at least in wxGTK, all pages |
| 679 | // claim to be shown, so we must only deal with the selected one. |
| 680 | #if wxUSE_NOTEBOOK |
| 681 | if (win->IsKindOf(CLASSINFO(wxNotebook))) |
| 682 | { |
| 683 | wxNotebook* nb = (wxNotebook*) win; |
| 684 | int sel = nb->GetSelection(); |
| 685 | if (sel >= 0) |
| 686 | { |
| 687 | wxWindow* child = nb->GetPage(sel); |
| 688 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); |
| 689 | if (foundWin) |
| 690 | return foundWin; |
| 691 | } |
| 692 | } |
| 693 | #endif |
| 694 | |
| 695 | wxWindowList::compatibility_iterator node = win->GetChildren().GetLast(); |
| 696 | while (node) |
| 697 | { |
| 698 | wxWindow* child = node->GetData(); |
| 699 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); |
| 700 | if (foundWin) |
| 701 | return foundWin; |
| 702 | node = node->GetPrevious(); |
| 703 | } |
| 704 | |
| 705 | wxPoint pos = win->GetPosition(); |
| 706 | wxSize sz = win->GetSize(); |
| 707 | if (win->GetParent()) |
| 708 | { |
| 709 | pos = win->GetParent()->ClientToScreen(pos); |
| 710 | } |
| 711 | |
| 712 | wxRect rect(pos, sz); |
| 713 | if (rect.Inside(pt)) |
| 714 | return win; |
| 715 | else |
| 716 | return NULL; |
| 717 | } |
| 718 | |
| 719 | wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt) |
| 720 | { |
| 721 | // Go backwards through the list since windows |
| 722 | // on top are likely to have been appended most |
| 723 | // recently. |
| 724 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast(); |
| 725 | while (node) |
| 726 | { |
| 727 | wxWindow* win = node->GetData(); |
| 728 | wxWindow* found = wxFindWindowAtPoint(win, pt); |
| 729 | if (found) |
| 730 | return found; |
| 731 | node = node->GetPrevious(); |
| 732 | } |
| 733 | return NULL; |
| 734 | } |
| 735 | |
| 736 | // ---------------------------------------------------------------------------- |
| 737 | // GUI helpers |
| 738 | // ---------------------------------------------------------------------------- |
| 739 | |
| 740 | /* |
| 741 | * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp |
| 742 | * since otherwise the generic code may be pulled in unnecessarily. |
| 743 | */ |
| 744 | |
| 745 | #if wxUSE_MSGDLG |
| 746 | |
| 747 | int wxMessageBox(const wxString& message, const wxString& caption, long style, |
| 748 | wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) ) |
| 749 | { |
| 750 | wxMessageDialog dialog(parent, message, caption, style); |
| 751 | |
| 752 | int ans = dialog.ShowModal(); |
| 753 | switch ( ans ) |
| 754 | { |
| 755 | case wxID_OK: |
| 756 | return wxOK; |
| 757 | case wxID_YES: |
| 758 | return wxYES; |
| 759 | case wxID_NO: |
| 760 | return wxNO; |
| 761 | case wxID_CANCEL: |
| 762 | return wxCANCEL; |
| 763 | } |
| 764 | |
| 765 | wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") ); |
| 766 | |
| 767 | return wxCANCEL; |
| 768 | } |
| 769 | |
| 770 | #endif // wxUSE_MSGDLG |
| 771 | |
| 772 | #if wxUSE_TEXTDLG |
| 773 | |
| 774 | wxString wxGetTextFromUser(const wxString& message, const wxString& caption, |
| 775 | const wxString& defaultValue, wxWindow *parent, |
| 776 | int x, int y, bool WXUNUSED(centre) ) |
| 777 | { |
| 778 | wxString str; |
| 779 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y)); |
| 780 | if (dialog.ShowModal() == wxID_OK) |
| 781 | { |
| 782 | str = dialog.GetValue(); |
| 783 | } |
| 784 | |
| 785 | return str; |
| 786 | } |
| 787 | |
| 788 | wxString wxGetPasswordFromUser(const wxString& message, |
| 789 | const wxString& caption, |
| 790 | const wxString& defaultValue, |
| 791 | wxWindow *parent) |
| 792 | { |
| 793 | wxString str; |
| 794 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, |
| 795 | wxOK | wxCANCEL | wxTE_PASSWORD); |
| 796 | if ( dialog.ShowModal() == wxID_OK ) |
| 797 | { |
| 798 | str = dialog.GetValue(); |
| 799 | } |
| 800 | |
| 801 | return str; |
| 802 | } |
| 803 | |
| 804 | #endif // wxUSE_TEXTDLG |
| 805 | |
| 806 | #if wxUSE_COLOURDLG |
| 807 | |
| 808 | wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit) |
| 809 | { |
| 810 | wxColourData data; |
| 811 | data.SetChooseFull(TRUE); |
| 812 | if ( colInit.Ok() ) |
| 813 | { |
| 814 | data.SetColour((wxColour &)colInit); // const_cast |
| 815 | } |
| 816 | |
| 817 | wxColour colRet; |
| 818 | wxColourDialog dialog(parent, &data); |
| 819 | if ( dialog.ShowModal() == wxID_OK ) |
| 820 | { |
| 821 | colRet = dialog.GetColourData().GetColour(); |
| 822 | } |
| 823 | //else: leave it invalid |
| 824 | |
| 825 | return colRet; |
| 826 | } |
| 827 | |
| 828 | #endif // wxUSE_COLOURDLG |
| 829 | |
| 830 | #if wxUSE_FONTDLG |
| 831 | |
| 832 | wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit) |
| 833 | { |
| 834 | wxFontData data; |
| 835 | if ( fontInit.Ok() ) |
| 836 | { |
| 837 | data.SetInitialFont(fontInit); |
| 838 | } |
| 839 | |
| 840 | wxFont fontRet; |
| 841 | wxFontDialog dialog(parent, data); |
| 842 | if ( dialog.ShowModal() == wxID_OK ) |
| 843 | { |
| 844 | fontRet = dialog.GetFontData().GetChosenFont(); |
| 845 | } |
| 846 | //else: leave it invalid |
| 847 | |
| 848 | return fontRet; |
| 849 | } |
| 850 | |
| 851 | #endif // wxUSE_FONTDLG |
| 852 | |
| 853 | // ---------------------------------------------------------------------------- |
| 854 | // wxSafeYield and supporting functions |
| 855 | // ---------------------------------------------------------------------------- |
| 856 | |
| 857 | void wxEnableTopLevelWindows(bool enable) |
| 858 | { |
| 859 | wxWindowList::compatibility_iterator node; |
| 860 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
| 861 | node->GetData()->Enable(enable); |
| 862 | } |
| 863 | |
| 864 | wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip) |
| 865 | { |
| 866 | // remember the top level windows which were already disabled, so that we |
| 867 | // don't reenable them later |
| 868 | m_winDisabled = NULL; |
| 869 | |
| 870 | wxWindowList::compatibility_iterator node; |
| 871 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
| 872 | { |
| 873 | wxWindow *winTop = node->GetData(); |
| 874 | if ( winTop == winToSkip ) |
| 875 | continue; |
| 876 | |
| 877 | // we don't need to disable the hidden or already disabled windows |
| 878 | if ( winTop->IsEnabled() && winTop->IsShown() ) |
| 879 | { |
| 880 | winTop->Disable(); |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | if ( !m_winDisabled ) |
| 885 | { |
| 886 | m_winDisabled = new wxWindowList; |
| 887 | } |
| 888 | |
| 889 | m_winDisabled->Append(winTop); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | wxWindowDisabler::~wxWindowDisabler() |
| 895 | { |
| 896 | wxWindowList::compatibility_iterator node; |
| 897 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
| 898 | { |
| 899 | wxWindow *winTop = node->GetData(); |
| 900 | if ( !m_winDisabled || !m_winDisabled->Find(winTop) ) |
| 901 | { |
| 902 | winTop->Enable(); |
| 903 | } |
| 904 | //else: had been already disabled, don't reenable |
| 905 | } |
| 906 | |
| 907 | delete m_winDisabled; |
| 908 | } |
| 909 | |
| 910 | // Yield to other apps/messages and disable user input to all windows except |
| 911 | // the given one |
| 912 | bool wxSafeYield(wxWindow *win, bool onlyIfNeeded) |
| 913 | { |
| 914 | wxWindowDisabler wd(win); |
| 915 | |
| 916 | bool rc; |
| 917 | if (onlyIfNeeded) |
| 918 | rc = wxYieldIfNeeded(); |
| 919 | else |
| 920 | rc = wxYield(); |
| 921 | |
| 922 | return rc; |
| 923 | } |
| 924 | |
| 925 | // Don't synthesize KeyUp events holding down a key and producing KeyDown |
| 926 | // events with autorepeat. On by default and always on in wxMSW. wxGTK version |
| 927 | // in utilsgtk.cpp. |
| 928 | #ifndef __WXGTK__ |
| 929 | bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) |
| 930 | { |
| 931 | return TRUE; // detectable auto-repeat is the only mode MSW supports |
| 932 | } |
| 933 | #endif // !wxGTK |
| 934 | |
| 935 | #endif // wxUSE_GUI |
| 936 | |