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