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