]> git.saurik.com Git - wxWidgets.git/blame - src/common/utilscmn.cpp
Move some OLE utilities to oleutils.cpp and simplified date conversion
[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
624 if (err == noErr) {
625#if !TARGET_CARBON
626 err = ICFindConfigFile(inst, 0, nil);
627#endif
628 if (err == noErr)
629 {
630 ConstStr255Param hint = 0;
631 startSel = 0;
632 endSel = url.Length();
633 err = ICLaunchURL(inst, hint, url.fn_str(), endSel, &startSel, &endSel);
634 if (err != noErr)
635 wxLogDebug(wxT("ICLaunchURL error %d"), (int) err);
636 }
637 ICStop(inst);
638 return true;
639 }
640 else
641 {
642 wxLogDebug(wxT("ICStart error %d"), (int) err);
643 return false;
644 }
657a7545 645#elif wxUSE_MIMETYPE
498a1eeb 646 // Non-windows way
657a7545 647 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension (_T("html"));
7999124f 648 if ( ft )
657a7545 649 {
7999124f
VZ
650 wxString mt;
651 ft->GetMimeType(&mt);
498a1eeb 652
7999124f
VZ
653 wxString cmd;
654 bool ok = ft->GetOpenCommand(&cmd, wxFileType::MessageParameters(url));
655 delete ft;
498a1eeb 656
7999124f 657 if ( !ok || cmd.empty() )
498a1eeb 658 {
7999124f
VZ
659 // fallback to checking for the BROWSER environment variable
660 cmd = wxGetenv(wxT("BROWSER"));
661 if ( !cmd.empty() )
662 cmd << _T(' ') << url;
498a1eeb 663 }
7999124f
VZ
664
665 if ( !cmd.empty() && wxExecute(cmd) )
666 return true;
498a1eeb 667 }
7999124f 668 else // no file type for html extension
2830b4a4 669 {
7999124f 670 wxLogError(_T("No default application configured for HTML files."));
2830b4a4 671 }
7999124f 672#endif // !wxUSE_MIMETYPE && !__WXMSW__
532d575b 673
7999124f
VZ
674 wxLogSysError(_T("Failed to open URL \"%s\" in default browser."),
675 url.c_str());
657a7545 676
7999124f 677 return false;
498a1eeb
RN
678}
679
91b4c08d 680// ----------------------------------------------------------------------------
7c072018 681// wxApp::Yield() wrappers for backwards compatibility
91b4c08d
VZ
682// ----------------------------------------------------------------------------
683
7c072018 684bool wxYield()
469e1e5c 685{
7c072018 686 return wxTheApp && wxTheApp->Yield();
469e1e5c 687}
7c072018
VZ
688
689bool wxYieldIfNeeded()
469e1e5c 690{
cb719f2e 691 return wxTheApp && wxTheApp->Yield(true);
469e1e5c 692}
7c072018 693
ec67cff1 694#endif // wxUSE_BASE
7c072018
VZ
695
696// ============================================================================
697// GUI-only functions from now on
698// ============================================================================
699
700#if wxUSE_GUI
701
4c3ebca9
MB
702// Id generation
703static long wxCurrentId = 100;
704
c838b68e 705long wxNewId()
4c3ebca9 706{
c838b68e
VS
707 // skip the part of IDs space that contains hard-coded values:
708 if (wxCurrentId == wxID_LOWEST)
709 wxCurrentId = wxID_HIGHEST + 1;
710
711 return wxCurrentId++;
4c3ebca9
MB
712}
713
714long
715wxGetCurrentId(void) { return wxCurrentId; }
716
717void
718wxRegisterId (long id)
719{
720 if (id >= wxCurrentId)
721 wxCurrentId = id + 1;
722}
723
7c072018 724#if wxUSE_MENUS
e90c1d2a
VZ
725
726// ----------------------------------------------------------------------------
7c072018 727// Menu accelerators related functions
e90c1d2a
VZ
728// ----------------------------------------------------------------------------
729
7c072018 730wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out)
e90c1d2a 731{
7c072018
VZ
732 wxString s = wxMenuItem::GetLabelFromText(in);
733 if ( out )
734 {
735 // go smash their buffer if it's not big enough - I love char * params
736 memcpy(out, s.c_str(), s.length() * sizeof(wxChar));
737 }
738 else
739 {
f526f752
MB
740 // MYcopystring - for easier search...
741 out = new wxChar[s.length() + 1];
742 wxStrcpy(out, s.c_str());
7c072018
VZ
743 }
744
745 return out;
e90c1d2a
VZ
746}
747
7c072018 748wxString wxStripMenuCodes(const wxString& in)
e90c1d2a 749{
7c072018 750 wxString out;
79f585d9 751
7c072018
VZ
752 size_t len = in.length();
753 out.reserve(len);
79f585d9 754
7c072018
VZ
755 for ( size_t n = 0; n < len; n++ )
756 {
757 wxChar ch = in[n];
758 if ( ch == _T('&') )
cd6ce4a9 759 {
7c072018
VZ
760 // skip it, it is used to introduce the accel char (or to quote
761 // itself in which case it should still be skipped): note that it
762 // can't be the last character of the string
763 if ( ++n == len )
79f585d9 764 {
7c072018
VZ
765 wxLogDebug(_T("Invalid menu string '%s'"), in.c_str());
766 }
767 else
768 {
769 // use the next char instead
770 ch = in[n];
79f585d9 771 }
cd6ce4a9 772 }
7c072018 773 else if ( ch == _T('\t') )
79f585d9 774 {
7c072018
VZ
775 // everything after TAB is accel string, exit the loop
776 break;
79f585d9 777 }
7c072018
VZ
778
779 out += ch;
225fe9d6
VZ
780 }
781
7c072018 782 return out;
cd6ce4a9
VZ
783}
784
7c072018 785#endif // wxUSE_MENUS
e90c1d2a 786
cbc66a27 787// ----------------------------------------------------------------------------
7c072018 788// Window search functions
cbc66a27
VZ
789// ----------------------------------------------------------------------------
790
7c072018
VZ
791/*
792 * If parent is non-NULL, look through children for a label or title
793 * matching the specified string. If NULL, look through all top-level windows.
794 *
795 */
e2a6f233 796
7c072018
VZ
797wxWindow *
798wxFindWindowByLabel (const wxString& title, wxWindow * parent)
134677bd 799{
7c072018
VZ
800 return wxWindow::FindWindowByLabel( title, parent );
801}
b829bf55 802
b829bf55 803
7c072018
VZ
804/*
805 * If parent is non-NULL, look through children for a name
806 * matching the specified string. If NULL, look through all top-level windows.
807 *
808 */
134677bd 809
7c072018
VZ
810wxWindow *
811wxFindWindowByName (const wxString& name, wxWindow * parent)
2c18f21d 812{
7c072018 813 return wxWindow::FindWindowByName( name, parent );
2c18f21d
VS
814}
815
cb719f2e 816// Returns menu item id or wxNOT_FOUND if none.
7c072018
VZ
817int
818wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
e2a6f233 819{
7c072018
VZ
820#if wxUSE_MENUS
821 wxMenuBar *menuBar = frame->GetMenuBar ();
822 if ( menuBar )
823 return menuBar->FindMenuItem (menuString, itemString);
824#endif // wxUSE_MENUS
0fb67cd1 825
cb719f2e 826 return wxNOT_FOUND;
e2a6f233
JS
827}
828
7c072018
VZ
829// Try to find the deepest child that contains 'pt'.
830// We go backwards, to try to allow for controls that are spacially
831// within other controls, but are still siblings (e.g. buttons within
832// static boxes). Static boxes are likely to be created _before_ controls
833// that sit inside them.
834wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt)
e2a6f233 835{
7c072018
VZ
836 if (!win->IsShown())
837 return NULL;
0fb67cd1 838
7c072018
VZ
839 // Hack for wxNotebook case: at least in wxGTK, all pages
840 // claim to be shown, so we must only deal with the selected one.
841#if wxUSE_NOTEBOOK
842 if (win->IsKindOf(CLASSINFO(wxNotebook)))
e2a6f233 843 {
7c072018
VZ
844 wxNotebook* nb = (wxNotebook*) win;
845 int sel = nb->GetSelection();
846 if (sel >= 0)
847 {
848 wxWindow* child = nb->GetPage(sel);
849 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
850 if (foundWin)
851 return foundWin;
852 }
e2a6f233 853 }
7c072018 854#endif
0fb67cd1 855
df5168c4 856 wxWindowList::compatibility_iterator node = win->GetChildren().GetLast();
7c072018
VZ
857 while (node)
858 {
859 wxWindow* child = node->GetData();
860 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
861 if (foundWin)
862 return foundWin;
863 node = node->GetPrevious();
864 }
865
866 wxPoint pos = win->GetPosition();
867 wxSize sz = win->GetSize();
868 if (win->GetParent())
869 {
870 pos = win->GetParent()->ClientToScreen(pos);
871 }
872
873 wxRect rect(pos, sz);
874 if (rect.Inside(pt))
875 return win;
876 else
877 return NULL;
0fb67cd1
VZ
878}
879
7c072018 880wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
0fb67cd1 881{
7c072018
VZ
882 // Go backwards through the list since windows
883 // on top are likely to have been appended most
884 // recently.
df5168c4 885 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast();
7c072018
VZ
886 while (node)
887 {
888 wxWindow* win = node->GetData();
889 wxWindow* found = wxFindWindowAtPoint(win, pt);
890 if (found)
891 return found;
892 node = node->GetPrevious();
893 }
894 return NULL;
895}
0fb67cd1 896
7c072018
VZ
897// ----------------------------------------------------------------------------
898// GUI helpers
899// ----------------------------------------------------------------------------
0fb67cd1 900
7c072018
VZ
901/*
902 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
903 * since otherwise the generic code may be pulled in unnecessarily.
904 */
0fb67cd1 905
7c072018 906#if wxUSE_MSGDLG
0fb67cd1 907
7c072018
VZ
908int wxMessageBox(const wxString& message, const wxString& caption, long style,
909 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
0fb67cd1 910{
53a6ac21
DS
911 long decorated_style = style;
912
913 if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION | wxICON_QUESTION ) ) == 0 )
914 {
915 decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ;
916 }
917
918 wxMessageDialog dialog(parent, message, caption, decorated_style);
0fb67cd1 919
7c072018
VZ
920 int ans = dialog.ShowModal();
921 switch ( ans )
922 {
923 case wxID_OK:
924 return wxOK;
925 case wxID_YES:
926 return wxYES;
927 case wxID_NO:
928 return wxNO;
929 case wxID_CANCEL:
930 return wxCANCEL;
931 }
0fb67cd1 932
7c072018 933 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
0fb67cd1 934
7c072018 935 return wxCANCEL;
e2a6f233
JS
936}
937
7c072018 938#endif // wxUSE_MSGDLG
518b5d2f 939
7c072018 940#if wxUSE_TEXTDLG
518b5d2f 941
7c072018
VZ
942wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
943 const wxString& defaultValue, wxWindow *parent,
13d13a9e 944 wxCoord x, wxCoord y, bool centre )
7c072018
VZ
945{
946 wxString str;
13d13a9e
WS
947 long style = wxTextEntryDialogStyle;
948
949 if (centre)
950 style |= wxCENTRE;
951 else
952 style &= ~wxCENTRE;
953
954 wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y));
955
7c072018
VZ
956 if (dialog.ShowModal() == wxID_OK)
957 {
958 str = dialog.GetValue();
959 }
0fb67cd1 960
7c072018 961 return str;
518b5d2f
VZ
962}
963
7c072018
VZ
964wxString wxGetPasswordFromUser(const wxString& message,
965 const wxString& caption,
966 const wxString& defaultValue,
b3bb2a74
KH
967 wxWindow *parent,
968 wxCoord x, wxCoord y, bool centre )
96c5bd7f 969{
7c072018 970 wxString str;
b3bb2a74
KH
971 long style = wxTextEntryDialogStyle;
972
973 if (centre)
974 style |= wxCENTRE;
975 else
976 style &= ~wxCENTRE;
977
12cfa304
KH
978 wxPasswordEntryDialog dialog(parent, message, caption, defaultValue,
979 style, wxPoint(x, y));
7c072018
VZ
980 if ( dialog.ShowModal() == wxID_OK )
981 {
982 str = dialog.GetValue();
983 }
96c5bd7f 984
7c072018
VZ
985 return str;
986}
96c5bd7f 987
7c072018 988#endif // wxUSE_TEXTDLG
96c5bd7f 989
7c072018 990#if wxUSE_COLOURDLG
96c5bd7f 991
f14d6dd1 992wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit, const wxString& caption)
c51deffc 993{
7c072018 994 wxColourData data;
cb719f2e 995 data.SetChooseFull(true);
7c072018
VZ
996 if ( colInit.Ok() )
997 {
998 data.SetColour((wxColour &)colInit); // const_cast
999 }
c51deffc 1000
7c072018
VZ
1001 wxColour colRet;
1002 wxColourDialog dialog(parent, &data);
f14d6dd1
JS
1003 if (!caption.IsEmpty())
1004 dialog.SetTitle(caption);
7c072018
VZ
1005 if ( dialog.ShowModal() == wxID_OK )
1006 {
1007 colRet = dialog.GetColourData().GetColour();
1008 }
1009 //else: leave it invalid
1010
1011 return colRet;
c51deffc 1012}
bc385ba9 1013
7c072018 1014#endif // wxUSE_COLOURDLG
bc385ba9 1015
7c072018
VZ
1016#if wxUSE_FONTDLG
1017
f14d6dd1 1018wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit, const wxString& caption)
bc385ba9 1019{
7c072018
VZ
1020 wxFontData data;
1021 if ( fontInit.Ok() )
bc385ba9 1022 {
7c072018
VZ
1023 data.SetInitialFont(fontInit);
1024 }
bc385ba9 1025
7c072018
VZ
1026 wxFont fontRet;
1027 wxFontDialog dialog(parent, data);
f14d6dd1
JS
1028 if (!caption.IsEmpty())
1029 dialog.SetTitle(caption);
7c072018
VZ
1030 if ( dialog.ShowModal() == wxID_OK )
1031 {
1032 fontRet = dialog.GetFontData().GetChosenFont();
1033 }
1034 //else: leave it invalid
bc385ba9 1035
7c072018 1036 return fontRet;
bc385ba9
VZ
1037}
1038
7c072018 1039#endif // wxUSE_FONTDLG
2b5f62a0 1040
7c072018
VZ
1041// ----------------------------------------------------------------------------
1042// wxSafeYield and supporting functions
1043// ----------------------------------------------------------------------------
2b5f62a0 1044
7c072018
VZ
1045void wxEnableTopLevelWindows(bool enable)
1046{
df5168c4 1047 wxWindowList::compatibility_iterator node;
7c072018
VZ
1048 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1049 node->GetData()->Enable(enable);
1050}
2b5f62a0 1051
7c072018
VZ
1052wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
1053{
1054 // remember the top level windows which were already disabled, so that we
1055 // don't reenable them later
1056 m_winDisabled = NULL;
1057
df5168c4 1058 wxWindowList::compatibility_iterator node;
7c072018 1059 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
2b5f62a0 1060 {
7c072018
VZ
1061 wxWindow *winTop = node->GetData();
1062 if ( winTop == winToSkip )
1063 continue;
2b5f62a0 1064
7c072018
VZ
1065 // we don't need to disable the hidden or already disabled windows
1066 if ( winTop->IsEnabled() && winTop->IsShown() )
2b5f62a0 1067 {
7c072018 1068 winTop->Disable();
2b5f62a0
VZ
1069 }
1070 else
1071 {
7c072018
VZ
1072 if ( !m_winDisabled )
1073 {
1074 m_winDisabled = new wxWindowList;
1075 }
1076
1077 m_winDisabled->Append(winTop);
2b5f62a0
VZ
1078 }
1079 }
2b5f62a0
VZ
1080}
1081
7c072018 1082wxWindowDisabler::~wxWindowDisabler()
cd6ce4a9 1083{
df5168c4 1084 wxWindowList::compatibility_iterator node;
7c072018 1085 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
cd6ce4a9 1086 {
7c072018
VZ
1087 wxWindow *winTop = node->GetData();
1088 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
cd6ce4a9 1089 {
7c072018 1090 winTop->Enable();
cd6ce4a9 1091 }
7c072018 1092 //else: had been already disabled, don't reenable
cd6ce4a9 1093 }
f6bcfd97 1094
7c072018 1095 delete m_winDisabled;
f6bcfd97
BP
1096}
1097
7c072018
VZ
1098// Yield to other apps/messages and disable user input to all windows except
1099// the given one
1100bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)
f6bcfd97 1101{
7c072018 1102 wxWindowDisabler wd(win);
21709999 1103
7c072018
VZ
1104 bool rc;
1105 if (onlyIfNeeded)
1106 rc = wxYieldIfNeeded();
1107 else
1108 rc = wxYield();
8461e4c2 1109
7c072018 1110 return rc;
8461e4c2
VZ
1111}
1112
7c072018
VZ
1113// Don't synthesize KeyUp events holding down a key and producing KeyDown
1114// events with autorepeat. On by default and always on in wxMSW. wxGTK version
1115// in utilsgtk.cpp.
1116#ifndef __WXGTK__
1117bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
8461e4c2 1118{
cb719f2e 1119 return true; // detectable auto-repeat is the only mode MSW supports
8461e4c2 1120}
7c072018
VZ
1121#endif // !wxGTK
1122
1123#endif // wxUSE_GUI