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