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