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