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