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