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