]> git.saurik.com Git - wxWidgets.git/blame - src/common/utilscmn.cpp
bitmap button size was too small when uxtheme functions were used to get the margins
[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
e90c1d2a 916// ----------------------------------------------------------------------------
7c072018 917// Menu accelerators related functions
e90c1d2a
VZ
918// ----------------------------------------------------------------------------
919
7c072018 920wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out)
e90c1d2a 921{
9a6384ca 922#if wxUSE_MENUS
7c072018 923 wxString s = wxMenuItem::GetLabelFromText(in);
9a6384ca
WS
924#else
925 wxString str(in);
926 wxString s = wxStripMenuCodes(str);
927#endif // wxUSE_MENUS
7c072018
VZ
928 if ( out )
929 {
930 // go smash their buffer if it's not big enough - I love char * params
931 memcpy(out, s.c_str(), s.length() * sizeof(wxChar));
932 }
933 else
934 {
f526f752
MB
935 // MYcopystring - for easier search...
936 out = new wxChar[s.length() + 1];
937 wxStrcpy(out, s.c_str());
7c072018
VZ
938 }
939
940 return out;
e90c1d2a
VZ
941}
942
7c072018 943wxString wxStripMenuCodes(const wxString& in)
e90c1d2a 944{
7c072018 945 wxString out;
79f585d9 946
7c072018
VZ
947 size_t len = in.length();
948 out.reserve(len);
79f585d9 949
7c072018
VZ
950 for ( size_t n = 0; n < len; n++ )
951 {
952 wxChar ch = in[n];
953 if ( ch == _T('&') )
cd6ce4a9 954 {
7c072018
VZ
955 // skip it, it is used to introduce the accel char (or to quote
956 // itself in which case it should still be skipped): note that it
957 // can't be the last character of the string
958 if ( ++n == len )
79f585d9 959 {
7c072018
VZ
960 wxLogDebug(_T("Invalid menu string '%s'"), in.c_str());
961 }
962 else
963 {
964 // use the next char instead
965 ch = in[n];
79f585d9 966 }
cd6ce4a9 967 }
7c072018 968 else if ( ch == _T('\t') )
79f585d9 969 {
7c072018
VZ
970 // everything after TAB is accel string, exit the loop
971 break;
79f585d9 972 }
7c072018
VZ
973
974 out += ch;
225fe9d6
VZ
975 }
976
7c072018 977 return out;
cd6ce4a9
VZ
978}
979
cbc66a27 980// ----------------------------------------------------------------------------
7c072018 981// Window search functions
cbc66a27
VZ
982// ----------------------------------------------------------------------------
983
7c072018
VZ
984/*
985 * If parent is non-NULL, look through children for a label or title
986 * matching the specified string. If NULL, look through all top-level windows.
987 *
988 */
e2a6f233 989
7c072018
VZ
990wxWindow *
991wxFindWindowByLabel (const wxString& title, wxWindow * parent)
134677bd 992{
7c072018
VZ
993 return wxWindow::FindWindowByLabel( title, parent );
994}
b829bf55 995
b829bf55 996
7c072018
VZ
997/*
998 * If parent is non-NULL, look through children for a name
999 * matching the specified string. If NULL, look through all top-level windows.
1000 *
1001 */
134677bd 1002
7c072018
VZ
1003wxWindow *
1004wxFindWindowByName (const wxString& name, wxWindow * parent)
2c18f21d 1005{
7c072018 1006 return wxWindow::FindWindowByName( name, parent );
2c18f21d
VS
1007}
1008
cb719f2e 1009// Returns menu item id or wxNOT_FOUND if none.
7c072018
VZ
1010int
1011wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
e2a6f233 1012{
7c072018 1013#if wxUSE_MENUS
9a6384ca
WS
1014 wxMenuBar *menuBar = frame->GetMenuBar ();
1015 if ( menuBar )
1016 return menuBar->FindMenuItem (menuString, itemString);
7c072018 1017#endif // wxUSE_MENUS
0fb67cd1 1018
9a6384ca 1019 return wxNOT_FOUND;
e2a6f233
JS
1020}
1021
7c072018
VZ
1022// Try to find the deepest child that contains 'pt'.
1023// We go backwards, to try to allow for controls that are spacially
1024// within other controls, but are still siblings (e.g. buttons within
1025// static boxes). Static boxes are likely to be created _before_ controls
1026// that sit inside them.
1027wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt)
e2a6f233 1028{
7c072018
VZ
1029 if (!win->IsShown())
1030 return NULL;
0fb67cd1 1031
7c072018
VZ
1032 // Hack for wxNotebook case: at least in wxGTK, all pages
1033 // claim to be shown, so we must only deal with the selected one.
1034#if wxUSE_NOTEBOOK
1035 if (win->IsKindOf(CLASSINFO(wxNotebook)))
e2a6f233 1036 {
7c072018
VZ
1037 wxNotebook* nb = (wxNotebook*) win;
1038 int sel = nb->GetSelection();
1039 if (sel >= 0)
1040 {
1041 wxWindow* child = nb->GetPage(sel);
1042 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
1043 if (foundWin)
1044 return foundWin;
1045 }
e2a6f233 1046 }
7c072018 1047#endif
0fb67cd1 1048
df5168c4 1049 wxWindowList::compatibility_iterator node = win->GetChildren().GetLast();
7c072018
VZ
1050 while (node)
1051 {
1052 wxWindow* child = node->GetData();
1053 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
1054 if (foundWin)
1055 return foundWin;
1056 node = node->GetPrevious();
1057 }
1058
1059 wxPoint pos = win->GetPosition();
1060 wxSize sz = win->GetSize();
7aa7d2d4 1061 if ( !win->IsTopLevel() && win->GetParent() )
7c072018
VZ
1062 {
1063 pos = win->GetParent()->ClientToScreen(pos);
1064 }
1065
1066 wxRect rect(pos, sz);
1067 if (rect.Inside(pt))
1068 return win;
622eb786
VZ
1069
1070 return NULL;
0fb67cd1
VZ
1071}
1072
7c072018 1073wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
0fb67cd1 1074{
7c072018
VZ
1075 // Go backwards through the list since windows
1076 // on top are likely to have been appended most
1077 // recently.
df5168c4 1078 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast();
7c072018
VZ
1079 while (node)
1080 {
1081 wxWindow* win = node->GetData();
1082 wxWindow* found = wxFindWindowAtPoint(win, pt);
1083 if (found)
1084 return found;
1085 node = node->GetPrevious();
1086 }
1087 return NULL;
1088}
0fb67cd1 1089
7c072018
VZ
1090// ----------------------------------------------------------------------------
1091// GUI helpers
1092// ----------------------------------------------------------------------------
0fb67cd1 1093
7c072018
VZ
1094/*
1095 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
1096 * since otherwise the generic code may be pulled in unnecessarily.
1097 */
0fb67cd1 1098
7c072018 1099#if wxUSE_MSGDLG
0fb67cd1 1100
7c072018
VZ
1101int wxMessageBox(const wxString& message, const wxString& caption, long style,
1102 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
0fb67cd1 1103{
53a6ac21
DS
1104 long decorated_style = style;
1105
1106 if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION | wxICON_QUESTION ) ) == 0 )
1107 {
1108 decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ;
1109 }
1110
1111 wxMessageDialog dialog(parent, message, caption, decorated_style);
0fb67cd1 1112
7c072018
VZ
1113 int ans = dialog.ShowModal();
1114 switch ( ans )
1115 {
1116 case wxID_OK:
1117 return wxOK;
1118 case wxID_YES:
1119 return wxYES;
1120 case wxID_NO:
1121 return wxNO;
1122 case wxID_CANCEL:
1123 return wxCANCEL;
1124 }
0fb67cd1 1125
7c072018 1126 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
0fb67cd1 1127
7c072018 1128 return wxCANCEL;
e2a6f233
JS
1129}
1130
7c072018 1131#endif // wxUSE_MSGDLG
518b5d2f 1132
7c072018 1133#if wxUSE_TEXTDLG
518b5d2f 1134
7c072018
VZ
1135wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
1136 const wxString& defaultValue, wxWindow *parent,
13d13a9e 1137 wxCoord x, wxCoord y, bool centre )
7c072018
VZ
1138{
1139 wxString str;
13d13a9e
WS
1140 long style = wxTextEntryDialogStyle;
1141
1142 if (centre)
1143 style |= wxCENTRE;
1144 else
1145 style &= ~wxCENTRE;
1146
1147 wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y));
1148
7c072018
VZ
1149 if (dialog.ShowModal() == wxID_OK)
1150 {
1151 str = dialog.GetValue();
1152 }
0fb67cd1 1153
7c072018 1154 return str;
518b5d2f
VZ
1155}
1156
7c072018
VZ
1157wxString wxGetPasswordFromUser(const wxString& message,
1158 const wxString& caption,
1159 const wxString& defaultValue,
b3bb2a74
KH
1160 wxWindow *parent,
1161 wxCoord x, wxCoord y, bool centre )
96c5bd7f 1162{
7c072018 1163 wxString str;
b3bb2a74
KH
1164 long style = wxTextEntryDialogStyle;
1165
1166 if (centre)
1167 style |= wxCENTRE;
1168 else
1169 style &= ~wxCENTRE;
1170
12cfa304
KH
1171 wxPasswordEntryDialog dialog(parent, message, caption, defaultValue,
1172 style, wxPoint(x, y));
7c072018
VZ
1173 if ( dialog.ShowModal() == wxID_OK )
1174 {
1175 str = dialog.GetValue();
1176 }
96c5bd7f 1177
7c072018
VZ
1178 return str;
1179}
96c5bd7f 1180
7c072018 1181#endif // wxUSE_TEXTDLG
96c5bd7f 1182
7c072018 1183#if wxUSE_COLOURDLG
96c5bd7f 1184
f14d6dd1 1185wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit, const wxString& caption)
c51deffc 1186{
7c072018 1187 wxColourData data;
cb719f2e 1188 data.SetChooseFull(true);
7c072018
VZ
1189 if ( colInit.Ok() )
1190 {
1191 data.SetColour((wxColour &)colInit); // const_cast
1192 }
c51deffc 1193
7c072018
VZ
1194 wxColour colRet;
1195 wxColourDialog dialog(parent, &data);
76b49cf4 1196 if (!caption.empty())
f14d6dd1 1197 dialog.SetTitle(caption);
7c072018
VZ
1198 if ( dialog.ShowModal() == wxID_OK )
1199 {
1200 colRet = dialog.GetColourData().GetColour();
1201 }
1202 //else: leave it invalid
1203
1204 return colRet;
c51deffc 1205}
bc385ba9 1206
7c072018 1207#endif // wxUSE_COLOURDLG
bc385ba9 1208
7c072018
VZ
1209#if wxUSE_FONTDLG
1210
f14d6dd1 1211wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit, const wxString& caption)
bc385ba9 1212{
7c072018
VZ
1213 wxFontData data;
1214 if ( fontInit.Ok() )
bc385ba9 1215 {
7c072018
VZ
1216 data.SetInitialFont(fontInit);
1217 }
bc385ba9 1218
7c072018
VZ
1219 wxFont fontRet;
1220 wxFontDialog dialog(parent, data);
76b49cf4 1221 if (!caption.empty())
f14d6dd1 1222 dialog.SetTitle(caption);
7c072018
VZ
1223 if ( dialog.ShowModal() == wxID_OK )
1224 {
1225 fontRet = dialog.GetFontData().GetChosenFont();
1226 }
1227 //else: leave it invalid
bc385ba9 1228
7c072018 1229 return fontRet;
bc385ba9
VZ
1230}
1231
7c072018 1232#endif // wxUSE_FONTDLG
2b5f62a0 1233
7c072018
VZ
1234// ----------------------------------------------------------------------------
1235// wxSafeYield and supporting functions
1236// ----------------------------------------------------------------------------
2b5f62a0 1237
7c072018
VZ
1238void wxEnableTopLevelWindows(bool enable)
1239{
df5168c4 1240 wxWindowList::compatibility_iterator node;
7c072018
VZ
1241 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1242 node->GetData()->Enable(enable);
1243}
2b5f62a0 1244
7c072018
VZ
1245wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
1246{
1247 // remember the top level windows which were already disabled, so that we
1248 // don't reenable them later
1249 m_winDisabled = NULL;
1250
df5168c4 1251 wxWindowList::compatibility_iterator node;
7c072018 1252 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
2b5f62a0 1253 {
7c072018
VZ
1254 wxWindow *winTop = node->GetData();
1255 if ( winTop == winToSkip )
1256 continue;
2b5f62a0 1257
7c072018
VZ
1258 // we don't need to disable the hidden or already disabled windows
1259 if ( winTop->IsEnabled() && winTop->IsShown() )
2b5f62a0 1260 {
7c072018 1261 winTop->Disable();
2b5f62a0
VZ
1262 }
1263 else
1264 {
7c072018
VZ
1265 if ( !m_winDisabled )
1266 {
1267 m_winDisabled = new wxWindowList;
1268 }
1269
1270 m_winDisabled->Append(winTop);
2b5f62a0
VZ
1271 }
1272 }
2b5f62a0
VZ
1273}
1274
7c072018 1275wxWindowDisabler::~wxWindowDisabler()
cd6ce4a9 1276{
df5168c4 1277 wxWindowList::compatibility_iterator node;
7c072018 1278 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
cd6ce4a9 1279 {
7c072018
VZ
1280 wxWindow *winTop = node->GetData();
1281 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
cd6ce4a9 1282 {
7c072018 1283 winTop->Enable();
cd6ce4a9 1284 }
7c072018 1285 //else: had been already disabled, don't reenable
cd6ce4a9 1286 }
f6bcfd97 1287
7c072018 1288 delete m_winDisabled;
f6bcfd97
BP
1289}
1290
7c072018
VZ
1291// Yield to other apps/messages and disable user input to all windows except
1292// the given one
1293bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)
f6bcfd97 1294{
7c072018 1295 wxWindowDisabler wd(win);
21709999 1296
7c072018
VZ
1297 bool rc;
1298 if (onlyIfNeeded)
1299 rc = wxYieldIfNeeded();
1300 else
1301 rc = wxYield();
8461e4c2 1302
7c072018 1303 return rc;
8461e4c2
VZ
1304}
1305
7c072018
VZ
1306// Don't synthesize KeyUp events holding down a key and producing KeyDown
1307// events with autorepeat. On by default and always on in wxMSW. wxGTK version
1308// in utilsgtk.cpp.
1309#ifndef __WXGTK__
1310bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
8461e4c2 1311{
cb719f2e 1312 return true; // detectable auto-repeat is the only mode MSW supports
8461e4c2 1313}
7c072018
VZ
1314#endif // !wxGTK
1315
1316#endif // wxUSE_GUI