/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/utilscmn.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/utilscmn.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22