]> git.saurik.com Git - wxWidgets.git/blame - src/common/utilscmn.cpp
i18n files are installed as part of wxBase (and should be ideally part of separate...
[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 74 #include "wx/notebook.h"
d1c8aaa3 75 #include "wx/statusbr.h"
91b4c08d
VZ
76#endif // wxUSE_GUI
77
1c193821 78#ifndef __WXWINCE__
c801d85f 79#include <time.h>
1c193821
JS
80#else
81#include "wx/msw/wince/time.h"
82#endif
e90c1d2a 83
f14d6dd1
JS
84#ifdef __WXMAC__
85#include "wx/mac/private.h"
86#ifndef __DARWIN__
87#include "InternetConfig.h"
88#endif
89#endif
90
4676948b 91#if !defined(__MWERKS__) && !defined(__WXWINCE__)
e90c1d2a
VZ
92 #include <sys/types.h>
93 #include <sys/stat.h>
469e1e5c 94#endif
c801d85f 95
82ef81ed 96#if defined(__WXMSW__)
5e1febfa 97 #include "wx/msw/private.h"
3452f00b 98 #include "wx/msw/registry.h"
b21d68c6 99 #include <shellapi.h> // needed for SHELLEXECUTEINFO
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
230c9077
JS
306/*
307 * Class to make it easier to specify platform-dependent values
308 */
309
310wxArrayInt* wxPlatform::sm_customPlatforms = NULL;
311
4bfec179 312void wxPlatform::Copy(const wxPlatform& platform)
230c9077 313{
4bfec179
JS
314 m_longValue = platform.m_longValue;
315 m_doubleValue = platform.m_doubleValue;
316 m_stringValue = platform.m_stringValue;
317}
318
319wxPlatform wxPlatform::If(int platform, long value)
320{
321 if (Is(platform))
322 return wxPlatform(value);
323 else
324 return wxPlatform();
325}
326
327wxPlatform wxPlatform::IfNot(int platform, long value)
328{
329 if (!Is(platform))
330 return wxPlatform(value);
331 else
332 return wxPlatform();
333}
334
335wxPlatform& wxPlatform::ElseIf(int platform, long value)
336{
337 if (Is(platform))
230c9077
JS
338 m_longValue = value;
339 return *this;
340}
341
4bfec179 342wxPlatform& wxPlatform::ElseIfNot(int platform, long value)
230c9077 343{
4bfec179 344 if (!Is(platform))
230c9077
JS
345 m_longValue = value;
346 return *this;
347}
348
4bfec179 349wxPlatform wxPlatform::If(int platform, double value)
230c9077 350{
4bfec179
JS
351 if (Is(platform))
352 return wxPlatform(value);
353 else
354 return wxPlatform();
355}
356
357wxPlatform wxPlatform::IfNot(int platform, double value)
358{
359 if (!Is(platform))
360 return wxPlatform(value);
361 else
362 return wxPlatform();
363}
364
365wxPlatform& wxPlatform::ElseIf(int platform, double value)
366{
367 if (Is(platform))
230c9077
JS
368 m_doubleValue = value;
369 return *this;
370}
371
4bfec179 372wxPlatform& wxPlatform::ElseIfNot(int platform, double value)
230c9077 373{
4bfec179 374 if (!Is(platform))
230c9077
JS
375 m_doubleValue = value;
376 return *this;
377}
378
4bfec179
JS
379wxPlatform wxPlatform::If(int platform, const wxString& value)
380{
381 if (Is(platform))
382 return wxPlatform(value);
383 else
384 return wxPlatform();
385}
386
387wxPlatform wxPlatform::IfNot(int platform, const wxString& value)
388{
389 if (!Is(platform))
390 return wxPlatform(value);
391 else
392 return wxPlatform();
393}
394
395wxPlatform& wxPlatform::ElseIf(int platform, const wxString& value)
230c9077 396{
4bfec179 397 if (Is(platform))
230c9077
JS
398 m_stringValue = value;
399 return *this;
400}
401
4bfec179 402wxPlatform& wxPlatform::ElseIfNot(int platform, const wxString& value)
230c9077 403{
4bfec179 404 if (!Is(platform))
230c9077
JS
405 m_stringValue = value;
406 return *this;
407}
408
4bfec179 409wxPlatform& wxPlatform::Else(long value)
230c9077
JS
410{
411 m_longValue = value;
412 return *this;
413}
414
4bfec179 415wxPlatform& wxPlatform::Else(double value)
230c9077
JS
416{
417 m_doubleValue = value;
418 return *this;
419}
420
4bfec179 421wxPlatform& wxPlatform::Else(const wxString& value)
230c9077
JS
422{
423 m_stringValue = value;
424 return *this;
425}
426
427void wxPlatform::AddPlatform(int platform)
428{
429 if (!sm_customPlatforms)
430 sm_customPlatforms = new wxArrayInt;
431 sm_customPlatforms->Add(platform);
432}
433
434void wxPlatform::ClearPlatforms()
435{
436 delete sm_customPlatforms;
437 sm_customPlatforms = NULL;
438}
439
440/// Function for testing current platform
441
4bfec179 442bool wxPlatform::Is(int platform)
230c9077
JS
443{
444#ifdef __WXMSW__
445 if (platform == wxMSW)
446 return true;
447#endif
448#ifdef __WXWINCE__
449 if (platform == wxWinCE)
450 return true;
451#endif
452#if defined(__WXWINCE__) && defined(__POCKETPC__)
453 if (platform == wxWinPocketPC)
454 return true;
455#endif
456#if defined(__WXWINCE__) && defined(__SMARTPHONE__)
6aa68c25 457 if (platform == wxWinSmartPhone)
230c9077
JS
458 return true;
459#endif
460#ifdef __WXGTK__
461 if (platform == wxGTK)
462 return true;
463#endif
464#ifdef __WXMAC__
465 if (platform == wxMac)
466 return true;
467#endif
468#ifdef __WXX11__
469 if (platform == wxX11)
470 return true;
471#endif
472#ifdef __UNIX__
473 if (platform == wxUnix)
474 return true;
475#endif
476#ifdef __WXMGL__
477 if (platform == wxMGL)
478 return true;
479#endif
480#ifdef __WXOS2__
481 if (platform == wxOS2)
482 return true;
483#endif
55d452c6 484#ifdef __WXCOCOA__
230c9077
JS
485 if (platform == wxCocoa)
486 return true;
487#endif
488
489 if (sm_customPlatforms && sm_customPlatforms->Index(platform) != wxNOT_FOUND)
490 return true;
491
492 return false;
493}
494
e90c1d2a 495// ----------------------------------------------------------------------------
7c072018 496// network and user id functions
e90c1d2a 497// ----------------------------------------------------------------------------
c801d85f 498
7c072018
VZ
499// Get Full RFC822 style email address
500bool wxGetEmailAddress(wxChar *address, int maxSize)
c801d85f 501{
7c072018
VZ
502 wxString email = wxGetEmailAddress();
503 if ( !email )
cb719f2e 504 return false;
c801d85f 505
7c072018
VZ
506 wxStrncpy(address, email, maxSize - 1);
507 address[maxSize - 1] = wxT('\0');
508
cb719f2e 509 return true;
c801d85f
KB
510}
511
7c072018 512wxString wxGetEmailAddress()
47bc1060 513{
7c072018 514 wxString email;
974e8d94 515
7c072018 516 wxString host = wxGetFullHostName();
4055ed82 517 if ( !host.empty() )
1e6feb95 518 {
7c072018 519 wxString user = wxGetUserId();
4055ed82 520 if ( !user.empty() )
1e6feb95 521 {
7c072018 522 email << user << wxT('@') << host;
974e8d94 523 }
974e8d94
VZ
524 }
525
7c072018 526 return email;
974e8d94
VZ
527}
528
7c072018 529wxString wxGetUserId()
c801d85f 530{
7c072018 531 static const int maxLoginLen = 256; // FIXME arbitrary number
c801d85f 532
7c072018 533 wxString buf;
4c3ebca9 534 bool ok = wxGetUserId(wxStringBuffer(buf, maxLoginLen), maxLoginLen);
c801d85f 535
7c072018
VZ
536 if ( !ok )
537 buf.Empty();
c801d85f 538
7c072018 539 return buf;
c801d85f
KB
540}
541
7c072018 542wxString wxGetUserName()
c801d85f 543{
7c072018 544 static const int maxUserNameLen = 1024; // FIXME arbitrary number
1e6feb95 545
7c072018 546 wxString buf;
4c3ebca9 547 bool ok = wxGetUserName(wxStringBuffer(buf, maxUserNameLen), maxUserNameLen);
7c072018
VZ
548
549 if ( !ok )
550 buf.Empty();
551
552 return buf;
59a12e90
JS
553}
554
7c072018 555wxString wxGetHostName()
59a12e90 556{
7c072018 557 static const size_t hostnameSize = 257;
c67d6888 558
7c072018 559 wxString buf;
4c3ebca9 560 bool ok = wxGetHostName(wxStringBuffer(buf, hostnameSize), hostnameSize);
59a12e90 561
7c072018
VZ
562 if ( !ok )
563 buf.Empty();
59a12e90 564
7c072018 565 return buf;
59a12e90
JS
566}
567
7c072018 568wxString wxGetFullHostName()
59a12e90 569{
7c072018 570 static const size_t hostnameSize = 257;
c801d85f 571
7c072018 572 wxString buf;
4c3ebca9 573 bool ok = wxGetFullHostName(wxStringBuffer(buf, hostnameSize), hostnameSize);
7c072018
VZ
574
575 if ( !ok )
576 buf.Empty();
c801d85f 577
7c072018
VZ
578 return buf;
579}
c801d85f 580
7c072018
VZ
581wxString wxGetHomeDir()
582{
583 wxString home;
584 wxGetHomeDir(&home);
c801d85f 585
7c072018
VZ
586 return home;
587}
c801d85f 588
c801d85f
KB
589#if 0
590
7c072018 591wxString wxGetCurrentDir()
c801d85f 592{
7c072018
VZ
593 wxString dir;
594 size_t len = 1024;
595 bool ok;
596 do
597 {
598 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
599 dir.UngetWriteBuf();
c801d85f 600
7c072018
VZ
601 if ( !ok )
602 {
603 if ( errno != ERANGE )
604 {
605 wxLogSysError(_T("Failed to get current directory"));
c801d85f 606
7c072018
VZ
607 return wxEmptyString;
608 }
3f4a0c5b 609 else
7c072018
VZ
610 {
611 // buffer was too small, retry with a larger one
612 len *= 2;
613 }
3f4a0c5b 614 }
7c072018
VZ
615 //else: ok
616 } while ( !ok );
c801d85f 617
7c072018 618 return dir;
c801d85f
KB
619}
620
7c072018 621#endif // 0
e90c1d2a
VZ
622
623// ----------------------------------------------------------------------------
7c072018 624// wxExecute
e90c1d2a 625// ----------------------------------------------------------------------------
ead7ce10 626
7c072018
VZ
627// wxDoExecuteWithCapture() helper: reads an entire stream into one array
628//
cb719f2e 629// returns true if ok, false if error
7c072018
VZ
630#if wxUSE_STREAMS
631static bool ReadAll(wxInputStream *is, wxArrayString& output)
dfad0599 632{
cb719f2e 633 wxCHECK_MSG( is, false, _T("NULL stream in wxExecute()?") );
dfad0599 634
7c072018
VZ
635 // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state
636 is->Reset();
1e6feb95 637
7c072018 638 wxTextInputStream tis(*is);
1e6feb95 639
cb719f2e 640 bool cont = true;
7c072018 641 while ( cont )
d2f50933 642 {
7c072018
VZ
643 wxString line = tis.ReadLine();
644 if ( is->Eof() )
645 break;
646
647 if ( !*is )
648 {
cb719f2e 649 cont = false;
7c072018
VZ
650 }
651 else
652 {
653 output.Add(line);
654 }
d2f50933
VZ
655 }
656
7c072018 657 return cont;
dfad0599 658}
7c072018 659#endif // wxUSE_STREAMS
d2f50933 660
7c072018
VZ
661// this is a private function because it hasn't a clean interface: the first
662// array is passed by reference, the second by pointer - instead we have 2
663// public versions of wxExecute() below
664static long wxDoExecuteWithCapture(const wxString& command,
665 wxArrayString& output,
4d172154
VZ
666 wxArrayString* error,
667 int flags)
d2f50933 668{
7c072018
VZ
669 // create a wxProcess which will capture the output
670 wxProcess *process = new wxProcess;
671 process->Redirect();
dfad0599 672
4d172154 673 long rc = wxExecute(command, wxEXEC_SYNC | flags, process);
1e6feb95 674
7c072018
VZ
675#if wxUSE_STREAMS
676 if ( rc != -1 )
bf31fa26 677 {
7c072018
VZ
678 if ( !ReadAll(process->GetInputStream(), output) )
679 rc = -1;
91b4c08d 680
7c072018
VZ
681 if ( error )
682 {
683 if ( !ReadAll(process->GetErrorStream(), *error) )
684 rc = -1;
685 }
91b4c08d 686
7c072018 687 }
e70ba80d
WS
688#else
689 wxUnusedVar(output);
690 wxUnusedVar(error);
691#endif // wxUSE_STREAMS/!wxUSE_STREAMS
91b4c08d 692
7c072018 693 delete process;
1e6feb95 694
7c072018 695 return rc;
7c072018 696}
bf31fa26 697
4d172154 698long wxExecute(const wxString& command, wxArrayString& output, int flags)
bf31fa26 699{
4d172154 700 return wxDoExecuteWithCapture(command, output, NULL, flags);
7c072018 701}
bf31fa26 702
7c072018
VZ
703long wxExecute(const wxString& command,
704 wxArrayString& output,
4d172154
VZ
705 wxArrayString& error,
706 int flags)
7c072018 707{
4d172154 708 return wxDoExecuteWithCapture(command, output, &error, flags);
bf31fa26
VZ
709}
710
498a1eeb
RN
711// ----------------------------------------------------------------------------
712// Launch default browser
713// ----------------------------------------------------------------------------
714
42d0df00 715bool wxLaunchDefaultBrowser(const wxString& urlOrig, int flags)
498a1eeb 716{
42d0df00
VZ
717 wxUnusedVar(flags);
718
7999124f
VZ
719 // set the scheme of url to http if it does not have one
720 wxString url(urlOrig);
721 if ( !wxURI(url).HasScheme() )
722 url.Prepend(wxT("http://"));
532d575b 723
7999124f 724#if defined(__WXMSW__)
5ccb95f6
WS
725
726#if wxUSE_IPC
42d0df00
VZ
727 if ( flags & wxBROWSER_NEW_WINDOW )
728 {
729 // ShellExecuteEx() opens the URL in an existing window by default so
730 // we can't use it if we need a new window
731 wxRegKey key(wxRegKey::HKCR, url.BeforeFirst(':') + _T("\\shell\\open"));
732 if ( key.Exists() )
733 {
734 wxRegKey keyDDE(key, wxT("DDEExec"));
735 if ( keyDDE.Exists() )
736 {
737 const wxString ddeTopic = wxRegKey(keyDDE, wxT("topic"));
738
739 // we only know the syntax of WWW_OpenURL DDE request for IE,
740 // optimistically assume that all other browsers are compatible
741 // with it
742 wxString ddeCmd;
743 bool ok = ddeTopic == wxT("WWW_OpenURL");
744 if ( ok )
745 {
588c80de 746 ddeCmd = keyDDE.QueryDefaultValue();
42d0df00
VZ
747 ok = !ddeCmd.empty();
748 }
749
750 if ( ok )
751 {
752 // for WWW_OpenURL, the index of the window to open the URL
753 // in is -1 (meaning "current") by default, replace it with
754 // 0 which means "new" (see KB article 160957)
755 ok = ddeCmd.Replace(wxT("-1"), wxT("0"),
756 false /* only first occurence */) == 1;
757 }
758
759 if ( ok )
760 {
761 // and also replace the parameters: the topic should
762 // contain a placeholder for the URL
763 ok = ddeCmd.Replace(wxT("%1"), url, false) == 1;
764 }
765
766 if ( ok )
767 {
768 // try to send it the DDE request now but ignore the errors
769 wxLogNull noLog;
770
771 const wxString ddeServer = wxRegKey(keyDDE, wxT("application"));
772 if ( wxExecuteDDE(ddeServer, ddeTopic, ddeCmd) )
773 return true;
774
775 // this is not necessarily an error: maybe browser is
776 // simply not running, but no matter, in any case we're
777 // going to launch it using ShellExecuteEx() below now and
778 // we shouldn't try to open a new window if we open a new
779 // browser anyhow
780 }
781 }
782 }
783 }
5ccb95f6 784#endif // wxUSE_IPC
42d0df00 785
7999124f
VZ
786 WinStruct<SHELLEXECUTEINFO> sei;
787 sei.lpFile = url.c_str();
788 sei.lpVerb = _T("open");
789 sei.nShow = SW_SHOWNORMAL;
498a1eeb 790
7999124f 791 ::ShellExecuteEx(&sei);
657a7545 792
7999124f 793 const int nResult = (int) sei.hInstApp;
498a1eeb 794
7999124f
VZ
795 // Firefox returns file not found for some reason, so make an exception
796 // for it
797 if ( nResult > 32 || nResult == SE_ERR_FNF )
498a1eeb 798 {
657a7545 799#ifdef __WXDEBUG__
498a1eeb 800 // Log something if SE_ERR_FNF happens
7999124f
VZ
801 if ( nResult == SE_ERR_FNF )
802 wxLogDebug(wxT("SE_ERR_FNF from ShellExecute -- maybe FireFox?"));
803#endif // __WXDEBUG__
804 return true;
657a7545 805 }
f14d6dd1
JS
806#elif defined(__WXMAC__)
807 OSStatus err;
808 ICInstance inst;
809 SInt32 startSel;
810 SInt32 endSel;
811
812 err = ICStart(&inst, 'STKA'); // put your app creator code here
13a1e96f
DS
813 if (err == noErr)
814 {
f14d6dd1 815#if !TARGET_CARBON
13a1e96f 816 err = ICFindConfigFile(inst, 0, NULL);
f14d6dd1
JS
817#endif
818 if (err == noErr)
819 {
820 ConstStr255Param hint = 0;
821 startSel = 0;
76b49cf4 822 endSel = url.length();
f14d6dd1
JS
823 err = ICLaunchURL(inst, hint, url.fn_str(), endSel, &startSel, &endSel);
824 if (err != noErr)
825 wxLogDebug(wxT("ICLaunchURL error %d"), (int) err);
826 }
827 ICStop(inst);
828 return true;
829 }
830 else
831 {
832 wxLogDebug(wxT("ICStart error %d"), (int) err);
833 return false;
834 }
657a7545 835#elif wxUSE_MIMETYPE
498a1eeb 836 // Non-windows way
13a1e96f
DS
837 bool ok = false;
838 wxString cmd;
839
840 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(_T("html"));
7999124f 841 if ( ft )
657a7545 842 {
7999124f
VZ
843 wxString mt;
844 ft->GetMimeType(&mt);
498a1eeb 845
13a1e96f 846 ok = ft->GetOpenCommand(&cmd, wxFileType::MessageParameters(url));
7999124f 847 delete ft;
498a1eeb 848 }
13a1e96f
DS
849
850 if ( !ok || cmd.empty() )
2830b4a4 851 {
13a1e96f
DS
852 // fallback to checking for the BROWSER environment variable
853 cmd = wxGetenv(wxT("BROWSER"));
854 if ( !cmd.empty() )
855 cmd << _T(' ') << url;
2830b4a4 856 }
13a1e96f
DS
857
858 ok = ( !cmd.empty() && wxExecute(cmd) );
859 if (ok)
860 return ok;
861
862 // no file type for HTML extension
863 wxLogError(_T("No default application configured for HTML files."));
864
7999124f 865#endif // !wxUSE_MIMETYPE && !__WXMSW__
532d575b 866
7999124f
VZ
867 wxLogSysError(_T("Failed to open URL \"%s\" in default browser."),
868 url.c_str());
657a7545 869
7999124f 870 return false;
498a1eeb
RN
871}
872
91b4c08d 873// ----------------------------------------------------------------------------
7c072018 874// wxApp::Yield() wrappers for backwards compatibility
91b4c08d
VZ
875// ----------------------------------------------------------------------------
876
7c072018 877bool wxYield()
469e1e5c 878{
7c072018 879 return wxTheApp && wxTheApp->Yield();
469e1e5c 880}
7c072018
VZ
881
882bool wxYieldIfNeeded()
469e1e5c 883{
cb719f2e 884 return wxTheApp && wxTheApp->Yield(true);
469e1e5c 885}
7c072018 886
ec67cff1 887#endif // wxUSE_BASE
7c072018
VZ
888
889// ============================================================================
890// GUI-only functions from now on
891// ============================================================================
892
893#if wxUSE_GUI
894
4c3ebca9
MB
895// Id generation
896static long wxCurrentId = 100;
897
c838b68e 898long wxNewId()
4c3ebca9 899{
c838b68e
VS
900 // skip the part of IDs space that contains hard-coded values:
901 if (wxCurrentId == wxID_LOWEST)
902 wxCurrentId = wxID_HIGHEST + 1;
903
904 return wxCurrentId++;
4c3ebca9
MB
905}
906
907long
908wxGetCurrentId(void) { return wxCurrentId; }
909
910void
911wxRegisterId (long id)
912{
913 if (id >= wxCurrentId)
914 wxCurrentId = id + 1;
915}
916
e90c1d2a 917// ----------------------------------------------------------------------------
7c072018 918// Menu accelerators related functions
e90c1d2a
VZ
919// ----------------------------------------------------------------------------
920
7c072018 921wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out)
e90c1d2a 922{
9a6384ca 923#if wxUSE_MENUS
7c072018 924 wxString s = wxMenuItem::GetLabelFromText(in);
9a6384ca
WS
925#else
926 wxString str(in);
927 wxString s = wxStripMenuCodes(str);
928#endif // wxUSE_MENUS
7c072018
VZ
929 if ( out )
930 {
931 // go smash their buffer if it's not big enough - I love char * params
932 memcpy(out, s.c_str(), s.length() * sizeof(wxChar));
933 }
934 else
935 {
f526f752
MB
936 // MYcopystring - for easier search...
937 out = new wxChar[s.length() + 1];
938 wxStrcpy(out, s.c_str());
7c072018
VZ
939 }
940
941 return out;
e90c1d2a
VZ
942}
943
74639764 944wxString wxStripMenuCodes(const wxString& in, int flags)
e90c1d2a 945{
74639764
VZ
946 wxASSERT_MSG( flags, _T("this is useless to call without any flags") );
947
7c072018 948 wxString out;
79f585d9 949
7c072018
VZ
950 size_t len = in.length();
951 out.reserve(len);
79f585d9 952
7c072018
VZ
953 for ( size_t n = 0; n < len; n++ )
954 {
955 wxChar ch = in[n];
74639764 956 if ( (flags & wxStrip_Mnemonics) && ch == _T('&') )
cd6ce4a9 957 {
7c072018
VZ
958 // skip it, it is used to introduce the accel char (or to quote
959 // itself in which case it should still be skipped): note that it
960 // can't be the last character of the string
961 if ( ++n == len )
79f585d9 962 {
7c072018
VZ
963 wxLogDebug(_T("Invalid menu string '%s'"), in.c_str());
964 }
965 else
966 {
967 // use the next char instead
968 ch = in[n];
79f585d9 969 }
cd6ce4a9 970 }
74639764 971 else if ( (flags & wxStrip_Accel) && ch == _T('\t') )
79f585d9 972 {
7c072018
VZ
973 // everything after TAB is accel string, exit the loop
974 break;
79f585d9 975 }
7c072018
VZ
976
977 out += ch;
225fe9d6
VZ
978 }
979
7c072018 980 return out;
cd6ce4a9
VZ
981}
982
cbc66a27 983// ----------------------------------------------------------------------------
7c072018 984// Window search functions
cbc66a27
VZ
985// ----------------------------------------------------------------------------
986
7c072018
VZ
987/*
988 * If parent is non-NULL, look through children for a label or title
989 * matching the specified string. If NULL, look through all top-level windows.
990 *
991 */
e2a6f233 992
7c072018
VZ
993wxWindow *
994wxFindWindowByLabel (const wxString& title, wxWindow * parent)
134677bd 995{
7c072018
VZ
996 return wxWindow::FindWindowByLabel( title, parent );
997}
b829bf55 998
b829bf55 999
7c072018
VZ
1000/*
1001 * If parent is non-NULL, look through children for a name
1002 * matching the specified string. If NULL, look through all top-level windows.
1003 *
1004 */
134677bd 1005
7c072018
VZ
1006wxWindow *
1007wxFindWindowByName (const wxString& name, wxWindow * parent)
2c18f21d 1008{
7c072018 1009 return wxWindow::FindWindowByName( name, parent );
2c18f21d
VS
1010}
1011
cb719f2e 1012// Returns menu item id or wxNOT_FOUND if none.
7c072018
VZ
1013int
1014wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
e2a6f233 1015{
7c072018 1016#if wxUSE_MENUS
9a6384ca
WS
1017 wxMenuBar *menuBar = frame->GetMenuBar ();
1018 if ( menuBar )
1019 return menuBar->FindMenuItem (menuString, itemString);
7c072018 1020#endif // wxUSE_MENUS
0fb67cd1 1021
9a6384ca 1022 return wxNOT_FOUND;
e2a6f233
JS
1023}
1024
7c072018
VZ
1025// Try to find the deepest child that contains 'pt'.
1026// We go backwards, to try to allow for controls that are spacially
1027// within other controls, but are still siblings (e.g. buttons within
1028// static boxes). Static boxes are likely to be created _before_ controls
1029// that sit inside them.
1030wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt)
e2a6f233 1031{
7c072018
VZ
1032 if (!win->IsShown())
1033 return NULL;
0fb67cd1 1034
7c072018
VZ
1035 // Hack for wxNotebook case: at least in wxGTK, all pages
1036 // claim to be shown, so we must only deal with the selected one.
1037#if wxUSE_NOTEBOOK
1038 if (win->IsKindOf(CLASSINFO(wxNotebook)))
e2a6f233 1039 {
7c072018
VZ
1040 wxNotebook* nb = (wxNotebook*) win;
1041 int sel = nb->GetSelection();
1042 if (sel >= 0)
1043 {
1044 wxWindow* child = nb->GetPage(sel);
1045 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
1046 if (foundWin)
1047 return foundWin;
1048 }
e2a6f233 1049 }
7c072018 1050#endif
0fb67cd1 1051
df5168c4 1052 wxWindowList::compatibility_iterator node = win->GetChildren().GetLast();
7c072018
VZ
1053 while (node)
1054 {
1055 wxWindow* child = node->GetData();
1056 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
1057 if (foundWin)
1058 return foundWin;
1059 node = node->GetPrevious();
1060 }
1061
1062 wxPoint pos = win->GetPosition();
1063 wxSize sz = win->GetSize();
7aa7d2d4 1064 if ( !win->IsTopLevel() && win->GetParent() )
7c072018
VZ
1065 {
1066 pos = win->GetParent()->ClientToScreen(pos);
1067 }
1068
1069 wxRect rect(pos, sz);
1070 if (rect.Inside(pt))
1071 return win;
622eb786
VZ
1072
1073 return NULL;
0fb67cd1
VZ
1074}
1075
7c072018 1076wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
0fb67cd1 1077{
7c072018
VZ
1078 // Go backwards through the list since windows
1079 // on top are likely to have been appended most
1080 // recently.
df5168c4 1081 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast();
7c072018
VZ
1082 while (node)
1083 {
1084 wxWindow* win = node->GetData();
1085 wxWindow* found = wxFindWindowAtPoint(win, pt);
1086 if (found)
1087 return found;
1088 node = node->GetPrevious();
1089 }
1090 return NULL;
1091}
0fb67cd1 1092
7c072018
VZ
1093// ----------------------------------------------------------------------------
1094// GUI helpers
1095// ----------------------------------------------------------------------------
0fb67cd1 1096
7c072018
VZ
1097/*
1098 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
1099 * since otherwise the generic code may be pulled in unnecessarily.
1100 */
0fb67cd1 1101
7c072018 1102#if wxUSE_MSGDLG
0fb67cd1 1103
7c072018
VZ
1104int wxMessageBox(const wxString& message, const wxString& caption, long style,
1105 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
0fb67cd1 1106{
53a6ac21
DS
1107 long decorated_style = style;
1108
1109 if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION | wxICON_QUESTION ) ) == 0 )
1110 {
1111 decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ;
1112 }
1113
1114 wxMessageDialog dialog(parent, message, caption, decorated_style);
0fb67cd1 1115
7c072018
VZ
1116 int ans = dialog.ShowModal();
1117 switch ( ans )
1118 {
1119 case wxID_OK:
1120 return wxOK;
1121 case wxID_YES:
1122 return wxYES;
1123 case wxID_NO:
1124 return wxNO;
1125 case wxID_CANCEL:
1126 return wxCANCEL;
1127 }
0fb67cd1 1128
7c072018 1129 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
0fb67cd1 1130
7c072018 1131 return wxCANCEL;
e2a6f233
JS
1132}
1133
7c072018 1134#endif // wxUSE_MSGDLG
518b5d2f 1135
7c072018 1136#if wxUSE_TEXTDLG
518b5d2f 1137
7c072018
VZ
1138wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
1139 const wxString& defaultValue, wxWindow *parent,
13d13a9e 1140 wxCoord x, wxCoord y, bool centre )
7c072018
VZ
1141{
1142 wxString str;
13d13a9e
WS
1143 long style = wxTextEntryDialogStyle;
1144
1145 if (centre)
1146 style |= wxCENTRE;
1147 else
1148 style &= ~wxCENTRE;
1149
1150 wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y));
1151
7c072018
VZ
1152 if (dialog.ShowModal() == wxID_OK)
1153 {
1154 str = dialog.GetValue();
1155 }
0fb67cd1 1156
7c072018 1157 return str;
518b5d2f
VZ
1158}
1159
7c072018
VZ
1160wxString wxGetPasswordFromUser(const wxString& message,
1161 const wxString& caption,
1162 const wxString& defaultValue,
b3bb2a74
KH
1163 wxWindow *parent,
1164 wxCoord x, wxCoord y, bool centre )
96c5bd7f 1165{
7c072018 1166 wxString str;
b3bb2a74
KH
1167 long style = wxTextEntryDialogStyle;
1168
1169 if (centre)
1170 style |= wxCENTRE;
1171 else
1172 style &= ~wxCENTRE;
1173
12cfa304
KH
1174 wxPasswordEntryDialog dialog(parent, message, caption, defaultValue,
1175 style, wxPoint(x, y));
7c072018
VZ
1176 if ( dialog.ShowModal() == wxID_OK )
1177 {
1178 str = dialog.GetValue();
1179 }
96c5bd7f 1180
7c072018
VZ
1181 return str;
1182}
96c5bd7f 1183
7c072018 1184#endif // wxUSE_TEXTDLG
96c5bd7f 1185
7c072018 1186#if wxUSE_COLOURDLG
96c5bd7f 1187
f14d6dd1 1188wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit, const wxString& caption)
c51deffc 1189{
7c072018 1190 wxColourData data;
cb719f2e 1191 data.SetChooseFull(true);
7c072018
VZ
1192 if ( colInit.Ok() )
1193 {
1194 data.SetColour((wxColour &)colInit); // const_cast
1195 }
c51deffc 1196
7c072018
VZ
1197 wxColour colRet;
1198 wxColourDialog dialog(parent, &data);
76b49cf4 1199 if (!caption.empty())
f14d6dd1 1200 dialog.SetTitle(caption);
7c072018
VZ
1201 if ( dialog.ShowModal() == wxID_OK )
1202 {
1203 colRet = dialog.GetColourData().GetColour();
1204 }
1205 //else: leave it invalid
1206
1207 return colRet;
c51deffc 1208}
bc385ba9 1209
7c072018 1210#endif // wxUSE_COLOURDLG
bc385ba9 1211
7c072018
VZ
1212#if wxUSE_FONTDLG
1213
f14d6dd1 1214wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit, const wxString& caption)
bc385ba9 1215{
7c072018
VZ
1216 wxFontData data;
1217 if ( fontInit.Ok() )
bc385ba9 1218 {
7c072018
VZ
1219 data.SetInitialFont(fontInit);
1220 }
bc385ba9 1221
7c072018
VZ
1222 wxFont fontRet;
1223 wxFontDialog dialog(parent, data);
76b49cf4 1224 if (!caption.empty())
f14d6dd1 1225 dialog.SetTitle(caption);
7c072018
VZ
1226 if ( dialog.ShowModal() == wxID_OK )
1227 {
1228 fontRet = dialog.GetFontData().GetChosenFont();
1229 }
1230 //else: leave it invalid
bc385ba9 1231
7c072018 1232 return fontRet;
bc385ba9
VZ
1233}
1234
7c072018 1235#endif // wxUSE_FONTDLG
2b5f62a0 1236
7c072018
VZ
1237// ----------------------------------------------------------------------------
1238// wxSafeYield and supporting functions
1239// ----------------------------------------------------------------------------
2b5f62a0 1240
7c072018
VZ
1241void wxEnableTopLevelWindows(bool enable)
1242{
df5168c4 1243 wxWindowList::compatibility_iterator node;
7c072018
VZ
1244 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1245 node->GetData()->Enable(enable);
1246}
2b5f62a0 1247
7c072018
VZ
1248wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
1249{
1250 // remember the top level windows which were already disabled, so that we
1251 // don't reenable them later
1252 m_winDisabled = NULL;
1253
df5168c4 1254 wxWindowList::compatibility_iterator node;
7c072018 1255 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
2b5f62a0 1256 {
7c072018
VZ
1257 wxWindow *winTop = node->GetData();
1258 if ( winTop == winToSkip )
1259 continue;
2b5f62a0 1260
7c072018
VZ
1261 // we don't need to disable the hidden or already disabled windows
1262 if ( winTop->IsEnabled() && winTop->IsShown() )
2b5f62a0 1263 {
7c072018 1264 winTop->Disable();
2b5f62a0
VZ
1265 }
1266 else
1267 {
7c072018
VZ
1268 if ( !m_winDisabled )
1269 {
1270 m_winDisabled = new wxWindowList;
1271 }
1272
1273 m_winDisabled->Append(winTop);
2b5f62a0
VZ
1274 }
1275 }
2b5f62a0
VZ
1276}
1277
7c072018 1278wxWindowDisabler::~wxWindowDisabler()
cd6ce4a9 1279{
df5168c4 1280 wxWindowList::compatibility_iterator node;
7c072018 1281 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
cd6ce4a9 1282 {
7c072018
VZ
1283 wxWindow *winTop = node->GetData();
1284 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
cd6ce4a9 1285 {
7c072018 1286 winTop->Enable();
cd6ce4a9 1287 }
7c072018 1288 //else: had been already disabled, don't reenable
cd6ce4a9 1289 }
f6bcfd97 1290
7c072018 1291 delete m_winDisabled;
f6bcfd97
BP
1292}
1293
7c072018
VZ
1294// Yield to other apps/messages and disable user input to all windows except
1295// the given one
1296bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)
f6bcfd97 1297{
7c072018 1298 wxWindowDisabler wd(win);
21709999 1299
7c072018
VZ
1300 bool rc;
1301 if (onlyIfNeeded)
1302 rc = wxYieldIfNeeded();
1303 else
1304 rc = wxYield();
8461e4c2 1305
7c072018 1306 return rc;
8461e4c2
VZ
1307}
1308
7c072018
VZ
1309// Don't synthesize KeyUp events holding down a key and producing KeyDown
1310// events with autorepeat. On by default and always on in wxMSW. wxGTK version
1311// in utilsgtk.cpp.
1312#ifndef __WXGTK__
1313bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
8461e4c2 1314{
cb719f2e 1315 return true; // detectable auto-repeat is the only mode MSW supports
8461e4c2 1316}
7c072018
VZ
1317#endif // !wxGTK
1318
1319#endif // wxUSE_GUI