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