]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
fix wxExecute() return code checks and removed not working code to open URLs in new...
[wxWidgets.git] / src / common / utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilscmn.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/string.h"
30 #include "wx/utils.h"
31 #include "wx/intl.h"
32 #include "wx/log.h"
33
34 #if wxUSE_GUI
35 #include "wx/window.h"
36 #include "wx/frame.h"
37 #include "wx/menu.h"
38 #include "wx/msgdlg.h"
39 #include "wx/textdlg.h"
40 #include "wx/textctrl.h" // for wxTE_PASSWORD
41 #if wxUSE_ACCEL
42 #include "wx/menuitem.h"
43 #include "wx/accel.h"
44 #endif // wxUSE_ACCEL
45 #endif // wxUSE_GUI
46 #endif // WX_PRECOMP
47
48 #include "wx/apptrait.h"
49
50 #include "wx/process.h"
51 #include "wx/txtstrm.h"
52 #include "wx/uri.h"
53 #include "wx/mimetype.h"
54 #include "wx/config.h"
55
56 #if defined(__WXWINCE__) && wxUSE_DATETIME
57 #include "wx/datetime.h"
58 #endif
59
60 #include <ctype.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #if !defined(__WATCOMC__)
66 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
67 #include <errno.h>
68 #endif
69 #endif
70
71 #if wxUSE_GUI
72 #include "wx/colordlg.h"
73 #include "wx/fontdlg.h"
74 #include "wx/notebook.h"
75 #include "wx/frame.h"
76 #include "wx/statusbr.h"
77 #endif // wxUSE_GUI
78
79 #ifndef __WXWINCE__
80 #include <time.h>
81 #else
82 #include "wx/msw/wince/time.h"
83 #endif
84
85 #if !defined(__MWERKS__) && !defined(__WXWINCE__)
86 #include <sys/types.h>
87 #include <sys/stat.h>
88 #endif
89
90 #if defined(__WXMSW__)
91 #include "wx/msw/private.h"
92 #endif
93
94 #if wxUSE_BASE
95
96 // ----------------------------------------------------------------------------
97 // common data
98 // ----------------------------------------------------------------------------
99
100 #if WXWIN_COMPATIBILITY_2_2
101 const wxChar *wxInternalErrorStr = wxT("wxWidgets Internal Error");
102 const wxChar *wxFatalErrorStr = wxT("wxWidgets Fatal Error");
103 #endif // WXWIN_COMPATIBILITY_2_2
104
105 // ============================================================================
106 // implementation
107 // ============================================================================
108
109 #if WXWIN_COMPATIBILITY_2_4
110
111 wxChar *
112 copystring (const wxChar *s)
113 {
114 if (s == NULL) s = wxEmptyString;
115 size_t len = wxStrlen (s) + 1;
116
117 wxChar *news = new wxChar[len];
118 memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest
119
120 return news;
121 }
122
123 #endif // WXWIN_COMPATIBILITY_2_4
124
125 // ----------------------------------------------------------------------------
126 // String <-> Number conversions (deprecated)
127 // ----------------------------------------------------------------------------
128
129 #if WXWIN_COMPATIBILITY_2_4
130
131 WXDLLIMPEXP_DATA_BASE(const wxChar *) wxFloatToStringStr = wxT("%.2f");
132 WXDLLIMPEXP_DATA_BASE(const wxChar *) wxDoubleToStringStr = wxT("%.2f");
133
134 void
135 StringToFloat (const wxChar *s, float *number)
136 {
137 if (s && *s && number)
138 *number = (float) wxStrtod (s, (wxChar **) NULL);
139 }
140
141 void
142 StringToDouble (const wxChar *s, double *number)
143 {
144 if (s && *s && number)
145 *number = wxStrtod (s, (wxChar **) NULL);
146 }
147
148 wxChar *
149 FloatToString (float number, const wxChar *fmt)
150 {
151 static wxChar buf[256];
152
153 wxSprintf (buf, fmt, number);
154 return buf;
155 }
156
157 wxChar *
158 DoubleToString (double number, const wxChar *fmt)
159 {
160 static wxChar buf[256];
161
162 wxSprintf (buf, fmt, number);
163 return buf;
164 }
165
166 void
167 StringToInt (const wxChar *s, int *number)
168 {
169 if (s && *s && number)
170 *number = (int) wxStrtol (s, (wxChar **) NULL, 10);
171 }
172
173 void
174 StringToLong (const wxChar *s, long *number)
175 {
176 if (s && *s && number)
177 *number = wxStrtol (s, (wxChar **) NULL, 10);
178 }
179
180 wxChar *
181 IntToString (int number)
182 {
183 static wxChar buf[20];
184
185 wxSprintf (buf, wxT("%d"), number);
186 return buf;
187 }
188
189 wxChar *
190 LongToString (long number)
191 {
192 static wxChar buf[20];
193
194 wxSprintf (buf, wxT("%ld"), number);
195 return buf;
196 }
197
198 #endif // WXWIN_COMPATIBILITY_2_4
199
200 // Array used in DecToHex conversion routine.
201 static wxChar hexArray[] = wxT("0123456789ABCDEF");
202
203 // Convert 2-digit hex number to decimal
204 int wxHexToDec(const wxString& buf)
205 {
206 int firstDigit, secondDigit;
207
208 if (buf.GetChar(0) >= wxT('A'))
209 firstDigit = buf.GetChar(0) - wxT('A') + 10;
210 else
211 firstDigit = buf.GetChar(0) - wxT('0');
212
213 if (buf.GetChar(1) >= wxT('A'))
214 secondDigit = buf.GetChar(1) - wxT('A') + 10;
215 else
216 secondDigit = buf.GetChar(1) - wxT('0');
217
218 return (firstDigit & 0xF) * 16 + (secondDigit & 0xF );
219 }
220
221 // Convert decimal integer to 2-character hex string
222 void wxDecToHex(int dec, wxChar *buf)
223 {
224 int firstDigit = (int)(dec/16.0);
225 int secondDigit = (int)(dec - (firstDigit*16.0));
226 buf[0] = hexArray[firstDigit];
227 buf[1] = hexArray[secondDigit];
228 buf[2] = 0;
229 }
230
231 // Convert decimal integer to 2-character hex string
232 wxString wxDecToHex(int dec)
233 {
234 wxChar buf[3];
235 wxDecToHex(dec, buf);
236 return wxString(buf);
237 }
238
239 // ----------------------------------------------------------------------------
240 // misc functions
241 // ----------------------------------------------------------------------------
242
243 // Return the current date/time
244 wxString wxNow()
245 {
246 #ifdef __WXWINCE__
247 #if wxUSE_DATETIME
248 wxDateTime now = wxDateTime::Now();
249 return now.Format();
250 #else
251 return wxEmptyString;
252 #endif
253 #else
254 time_t now = time((time_t *) NULL);
255 char *date = ctime(&now);
256 date[24] = '\0';
257 return wxString::FromAscii(date);
258 #endif
259 }
260
261 void wxUsleep(unsigned long milliseconds)
262 {
263 wxMilliSleep(milliseconds);
264 }
265
266 const wxChar *wxGetInstallPrefix()
267 {
268 wxString prefix;
269
270 if ( wxGetEnv(wxT("WXPREFIX"), &prefix) )
271 return prefix.c_str();
272
273 #ifdef wxINSTALL_PREFIX
274 return wxT(wxINSTALL_PREFIX);
275 #else
276 return wxEmptyString;
277 #endif
278 }
279
280 wxString wxGetDataDir()
281 {
282 wxString dir = wxGetInstallPrefix();
283 dir << wxFILE_SEP_PATH << wxT("share") << wxFILE_SEP_PATH << wxT("wx");
284 return dir;
285 }
286
287 int wxGetOsVersion(int *verMaj, int *verMin)
288 {
289 // we want this function to work even if there is no wxApp
290 wxConsoleAppTraits traitsConsole;
291 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
292 if ( ! traits )
293 traits = &traitsConsole;
294
295 wxToolkitInfo& info = traits->GetToolkitInfo();
296 if ( verMaj )
297 *verMaj = info.versionMajor;
298 if ( verMin )
299 *verMin = info.versionMinor;
300 return info.os;
301 }
302
303 // ----------------------------------------------------------------------------
304 // network and user id functions
305 // ----------------------------------------------------------------------------
306
307 // Get Full RFC822 style email address
308 bool wxGetEmailAddress(wxChar *address, int maxSize)
309 {
310 wxString email = wxGetEmailAddress();
311 if ( !email )
312 return false;
313
314 wxStrncpy(address, email, maxSize - 1);
315 address[maxSize - 1] = wxT('\0');
316
317 return true;
318 }
319
320 wxString wxGetEmailAddress()
321 {
322 wxString email;
323
324 wxString host = wxGetFullHostName();
325 if ( !host.empty() )
326 {
327 wxString user = wxGetUserId();
328 if ( !user.empty() )
329 {
330 email << user << wxT('@') << host;
331 }
332 }
333
334 return email;
335 }
336
337 wxString wxGetUserId()
338 {
339 static const int maxLoginLen = 256; // FIXME arbitrary number
340
341 wxString buf;
342 bool ok = wxGetUserId(wxStringBuffer(buf, maxLoginLen), maxLoginLen);
343
344 if ( !ok )
345 buf.Empty();
346
347 return buf;
348 }
349
350 wxString wxGetUserName()
351 {
352 static const int maxUserNameLen = 1024; // FIXME arbitrary number
353
354 wxString buf;
355 bool ok = wxGetUserName(wxStringBuffer(buf, maxUserNameLen), maxUserNameLen);
356
357 if ( !ok )
358 buf.Empty();
359
360 return buf;
361 }
362
363 wxString wxGetHostName()
364 {
365 static const size_t hostnameSize = 257;
366
367 wxString buf;
368 bool ok = wxGetHostName(wxStringBuffer(buf, hostnameSize), hostnameSize);
369
370 if ( !ok )
371 buf.Empty();
372
373 return buf;
374 }
375
376 wxString wxGetFullHostName()
377 {
378 static const size_t hostnameSize = 257;
379
380 wxString buf;
381 bool ok = wxGetFullHostName(wxStringBuffer(buf, hostnameSize), hostnameSize);
382
383 if ( !ok )
384 buf.Empty();
385
386 return buf;
387 }
388
389 wxString wxGetHomeDir()
390 {
391 wxString home;
392 wxGetHomeDir(&home);
393
394 return home;
395 }
396
397 #if 0
398
399 wxString wxGetCurrentDir()
400 {
401 wxString dir;
402 size_t len = 1024;
403 bool ok;
404 do
405 {
406 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
407 dir.UngetWriteBuf();
408
409 if ( !ok )
410 {
411 if ( errno != ERANGE )
412 {
413 wxLogSysError(_T("Failed to get current directory"));
414
415 return wxEmptyString;
416 }
417 else
418 {
419 // buffer was too small, retry with a larger one
420 len *= 2;
421 }
422 }
423 //else: ok
424 } while ( !ok );
425
426 return dir;
427 }
428
429 #endif // 0
430
431 // ----------------------------------------------------------------------------
432 // wxExecute
433 // ----------------------------------------------------------------------------
434
435 // wxDoExecuteWithCapture() helper: reads an entire stream into one array
436 //
437 // returns true if ok, false if error
438 #if wxUSE_STREAMS
439 static bool ReadAll(wxInputStream *is, wxArrayString& output)
440 {
441 wxCHECK_MSG( is, false, _T("NULL stream in wxExecute()?") );
442
443 // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state
444 is->Reset();
445
446 wxTextInputStream tis(*is);
447
448 bool cont = true;
449 while ( cont )
450 {
451 wxString line = tis.ReadLine();
452 if ( is->Eof() )
453 break;
454
455 if ( !*is )
456 {
457 cont = false;
458 }
459 else
460 {
461 output.Add(line);
462 }
463 }
464
465 return cont;
466 }
467 #endif // wxUSE_STREAMS
468
469 // this is a private function because it hasn't a clean interface: the first
470 // array is passed by reference, the second by pointer - instead we have 2
471 // public versions of wxExecute() below
472 static long wxDoExecuteWithCapture(const wxString& command,
473 wxArrayString& output,
474 wxArrayString* error,
475 int flags)
476 {
477 // create a wxProcess which will capture the output
478 wxProcess *process = new wxProcess;
479 process->Redirect();
480
481 long rc = wxExecute(command, wxEXEC_SYNC | flags, process);
482
483 #if wxUSE_STREAMS
484 if ( rc != -1 )
485 {
486 if ( !ReadAll(process->GetInputStream(), output) )
487 rc = -1;
488
489 if ( error )
490 {
491 if ( !ReadAll(process->GetErrorStream(), *error) )
492 rc = -1;
493 }
494
495 }
496 #else
497 wxUnusedVar(output);
498 wxUnusedVar(error);
499 #endif // wxUSE_STREAMS/!wxUSE_STREAMS
500
501 delete process;
502
503 return rc;
504 }
505
506 long wxExecute(const wxString& command, wxArrayString& output, int flags)
507 {
508 return wxDoExecuteWithCapture(command, output, NULL, flags);
509 }
510
511 long wxExecute(const wxString& command,
512 wxArrayString& output,
513 wxArrayString& error,
514 int flags)
515 {
516 return wxDoExecuteWithCapture(command, output, &error, flags);
517 }
518
519 // ----------------------------------------------------------------------------
520 // Launch default browser
521 // ----------------------------------------------------------------------------
522
523 bool wxLaunchDefaultBrowser(const wxString& urlOrig)
524 {
525 // set the scheme of url to http if it does not have one
526 wxString url(urlOrig);
527 if ( !wxURI(url).HasScheme() )
528 url.Prepend(wxT("http://"));
529
530 #if defined(__WXMSW__)
531 WinStruct<SHELLEXECUTEINFO> sei;
532 sei.lpFile = url.c_str();
533 sei.lpVerb = _T("open");
534 sei.nShow = SW_SHOWNORMAL;
535
536 ::ShellExecuteEx(&sei);
537
538 const int nResult = (int) sei.hInstApp;
539
540 // Firefox returns file not found for some reason, so make an exception
541 // for it
542 if ( nResult > 32 || nResult == SE_ERR_FNF )
543 {
544 #ifdef __WXDEBUG__
545 // Log something if SE_ERR_FNF happens
546 if ( nResult == SE_ERR_FNF )
547 wxLogDebug(wxT("SE_ERR_FNF from ShellExecute -- maybe FireFox?"));
548 #endif // __WXDEBUG__
549 return true;
550 }
551 #elif wxUSE_MIMETYPE
552 // Non-windows way
553 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension (_T("html"));
554 if ( ft )
555 {
556 wxString mt;
557 ft->GetMimeType(&mt);
558
559 wxString cmd;
560 bool ok = ft->GetOpenCommand(&cmd, wxFileType::MessageParameters(url));
561 delete ft;
562
563 if ( !ok || cmd.empty() )
564 {
565 // fallback to checking for the BROWSER environment variable
566 cmd = wxGetenv(wxT("BROWSER"));
567 if ( !cmd.empty() )
568 cmd << _T(' ') << url;
569 }
570
571 if ( !cmd.empty() && wxExecute(cmd) )
572 return true;
573 }
574 else // no file type for html extension
575 {
576 wxLogError(_T("No default application configured for HTML files."));
577 }
578 #endif // !wxUSE_MIMETYPE && !__WXMSW__
579
580 wxLogSysError(_T("Failed to open URL \"%s\" in default browser."),
581 url.c_str());
582
583 return false;
584 }
585
586 // ----------------------------------------------------------------------------
587 // wxApp::Yield() wrappers for backwards compatibility
588 // ----------------------------------------------------------------------------
589
590 bool wxYield()
591 {
592 return wxTheApp && wxTheApp->Yield();
593 }
594
595 bool wxYieldIfNeeded()
596 {
597 return wxTheApp && wxTheApp->Yield(true);
598 }
599
600 #endif // wxUSE_BASE
601
602 // ============================================================================
603 // GUI-only functions from now on
604 // ============================================================================
605
606 #if wxUSE_GUI
607
608 // Id generation
609 static long wxCurrentId = 100;
610
611 long wxNewId()
612 {
613 // skip the part of IDs space that contains hard-coded values:
614 if (wxCurrentId == wxID_LOWEST)
615 wxCurrentId = wxID_HIGHEST + 1;
616
617 return wxCurrentId++;
618 }
619
620 long
621 wxGetCurrentId(void) { return wxCurrentId; }
622
623 void
624 wxRegisterId (long id)
625 {
626 if (id >= wxCurrentId)
627 wxCurrentId = id + 1;
628 }
629
630 #if wxUSE_MENUS
631
632 // ----------------------------------------------------------------------------
633 // Menu accelerators related functions
634 // ----------------------------------------------------------------------------
635
636 wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out)
637 {
638 wxString s = wxMenuItem::GetLabelFromText(in);
639 if ( out )
640 {
641 // go smash their buffer if it's not big enough - I love char * params
642 memcpy(out, s.c_str(), s.length() * sizeof(wxChar));
643 }
644 else
645 {
646 // MYcopystring - for easier search...
647 out = new wxChar[s.length() + 1];
648 wxStrcpy(out, s.c_str());
649 }
650
651 return out;
652 }
653
654 wxString wxStripMenuCodes(const wxString& in)
655 {
656 wxString out;
657
658 size_t len = in.length();
659 out.reserve(len);
660
661 for ( size_t n = 0; n < len; n++ )
662 {
663 wxChar ch = in[n];
664 if ( ch == _T('&') )
665 {
666 // skip it, it is used to introduce the accel char (or to quote
667 // itself in which case it should still be skipped): note that it
668 // can't be the last character of the string
669 if ( ++n == len )
670 {
671 wxLogDebug(_T("Invalid menu string '%s'"), in.c_str());
672 }
673 else
674 {
675 // use the next char instead
676 ch = in[n];
677 }
678 }
679 else if ( ch == _T('\t') )
680 {
681 // everything after TAB is accel string, exit the loop
682 break;
683 }
684
685 out += ch;
686 }
687
688 return out;
689 }
690
691 #endif // wxUSE_MENUS
692
693 // ----------------------------------------------------------------------------
694 // Window search functions
695 // ----------------------------------------------------------------------------
696
697 /*
698 * If parent is non-NULL, look through children for a label or title
699 * matching the specified string. If NULL, look through all top-level windows.
700 *
701 */
702
703 wxWindow *
704 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
705 {
706 return wxWindow::FindWindowByLabel( title, parent );
707 }
708
709
710 /*
711 * If parent is non-NULL, look through children for a name
712 * matching the specified string. If NULL, look through all top-level windows.
713 *
714 */
715
716 wxWindow *
717 wxFindWindowByName (const wxString& name, wxWindow * parent)
718 {
719 return wxWindow::FindWindowByName( name, parent );
720 }
721
722 // Returns menu item id or wxNOT_FOUND if none.
723 int
724 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
725 {
726 #if wxUSE_MENUS
727 wxMenuBar *menuBar = frame->GetMenuBar ();
728 if ( menuBar )
729 return menuBar->FindMenuItem (menuString, itemString);
730 #endif // wxUSE_MENUS
731
732 return wxNOT_FOUND;
733 }
734
735 // Try to find the deepest child that contains 'pt'.
736 // We go backwards, to try to allow for controls that are spacially
737 // within other controls, but are still siblings (e.g. buttons within
738 // static boxes). Static boxes are likely to be created _before_ controls
739 // that sit inside them.
740 wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt)
741 {
742 if (!win->IsShown())
743 return NULL;
744
745 // Hack for wxNotebook case: at least in wxGTK, all pages
746 // claim to be shown, so we must only deal with the selected one.
747 #if wxUSE_NOTEBOOK
748 if (win->IsKindOf(CLASSINFO(wxNotebook)))
749 {
750 wxNotebook* nb = (wxNotebook*) win;
751 int sel = nb->GetSelection();
752 if (sel >= 0)
753 {
754 wxWindow* child = nb->GetPage(sel);
755 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
756 if (foundWin)
757 return foundWin;
758 }
759 }
760 #endif
761
762 wxWindowList::compatibility_iterator node = win->GetChildren().GetLast();
763 while (node)
764 {
765 wxWindow* child = node->GetData();
766 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
767 if (foundWin)
768 return foundWin;
769 node = node->GetPrevious();
770 }
771
772 wxPoint pos = win->GetPosition();
773 wxSize sz = win->GetSize();
774 if (win->GetParent())
775 {
776 pos = win->GetParent()->ClientToScreen(pos);
777 }
778
779 wxRect rect(pos, sz);
780 if (rect.Inside(pt))
781 return win;
782 else
783 return NULL;
784 }
785
786 wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
787 {
788 // Go backwards through the list since windows
789 // on top are likely to have been appended most
790 // recently.
791 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast();
792 while (node)
793 {
794 wxWindow* win = node->GetData();
795 wxWindow* found = wxFindWindowAtPoint(win, pt);
796 if (found)
797 return found;
798 node = node->GetPrevious();
799 }
800 return NULL;
801 }
802
803 // ----------------------------------------------------------------------------
804 // GUI helpers
805 // ----------------------------------------------------------------------------
806
807 /*
808 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
809 * since otherwise the generic code may be pulled in unnecessarily.
810 */
811
812 #if wxUSE_MSGDLG
813
814 int wxMessageBox(const wxString& message, const wxString& caption, long style,
815 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
816 {
817 long decorated_style = style;
818
819 if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION | wxICON_QUESTION ) ) == 0 )
820 {
821 decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ;
822 }
823
824 wxMessageDialog dialog(parent, message, caption, decorated_style);
825
826 int ans = dialog.ShowModal();
827 switch ( ans )
828 {
829 case wxID_OK:
830 return wxOK;
831 case wxID_YES:
832 return wxYES;
833 case wxID_NO:
834 return wxNO;
835 case wxID_CANCEL:
836 return wxCANCEL;
837 }
838
839 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
840
841 return wxCANCEL;
842 }
843
844 #endif // wxUSE_MSGDLG
845
846 #if wxUSE_TEXTDLG
847
848 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
849 const wxString& defaultValue, wxWindow *parent,
850 wxCoord x, wxCoord y, bool centre )
851 {
852 wxString str;
853 long style = wxTextEntryDialogStyle;
854
855 if (centre)
856 style |= wxCENTRE;
857 else
858 style &= ~wxCENTRE;
859
860 wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y));
861
862 if (dialog.ShowModal() == wxID_OK)
863 {
864 str = dialog.GetValue();
865 }
866
867 return str;
868 }
869
870 wxString wxGetPasswordFromUser(const wxString& message,
871 const wxString& caption,
872 const wxString& defaultValue,
873 wxWindow *parent,
874 wxCoord x, wxCoord y, bool centre )
875 {
876 wxString str;
877 long style = wxTextEntryDialogStyle;
878
879 if (centre)
880 style |= wxCENTRE;
881 else
882 style &= ~wxCENTRE;
883
884 wxPasswordEntryDialog dialog(parent, message, caption, defaultValue,
885 style, wxPoint(x, y));
886 if ( dialog.ShowModal() == wxID_OK )
887 {
888 str = dialog.GetValue();
889 }
890
891 return str;
892 }
893
894 #endif // wxUSE_TEXTDLG
895
896 #if wxUSE_COLOURDLG
897
898 wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit)
899 {
900 wxColourData data;
901 data.SetChooseFull(true);
902 if ( colInit.Ok() )
903 {
904 data.SetColour((wxColour &)colInit); // const_cast
905 }
906
907 wxColour colRet;
908 wxColourDialog dialog(parent, &data);
909 if ( dialog.ShowModal() == wxID_OK )
910 {
911 colRet = dialog.GetColourData().GetColour();
912 }
913 //else: leave it invalid
914
915 return colRet;
916 }
917
918 #endif // wxUSE_COLOURDLG
919
920 #if wxUSE_FONTDLG
921
922 wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit)
923 {
924 wxFontData data;
925 if ( fontInit.Ok() )
926 {
927 data.SetInitialFont(fontInit);
928 }
929
930 wxFont fontRet;
931 wxFontDialog dialog(parent, data);
932 if ( dialog.ShowModal() == wxID_OK )
933 {
934 fontRet = dialog.GetFontData().GetChosenFont();
935 }
936 //else: leave it invalid
937
938 return fontRet;
939 }
940
941 #endif // wxUSE_FONTDLG
942
943 // ----------------------------------------------------------------------------
944 // wxSafeYield and supporting functions
945 // ----------------------------------------------------------------------------
946
947 void wxEnableTopLevelWindows(bool enable)
948 {
949 wxWindowList::compatibility_iterator node;
950 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
951 node->GetData()->Enable(enable);
952 }
953
954 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
955 {
956 // remember the top level windows which were already disabled, so that we
957 // don't reenable them later
958 m_winDisabled = NULL;
959
960 wxWindowList::compatibility_iterator node;
961 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
962 {
963 wxWindow *winTop = node->GetData();
964 if ( winTop == winToSkip )
965 continue;
966
967 // we don't need to disable the hidden or already disabled windows
968 if ( winTop->IsEnabled() && winTop->IsShown() )
969 {
970 winTop->Disable();
971 }
972 else
973 {
974 if ( !m_winDisabled )
975 {
976 m_winDisabled = new wxWindowList;
977 }
978
979 m_winDisabled->Append(winTop);
980 }
981 }
982 }
983
984 wxWindowDisabler::~wxWindowDisabler()
985 {
986 wxWindowList::compatibility_iterator node;
987 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
988 {
989 wxWindow *winTop = node->GetData();
990 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
991 {
992 winTop->Enable();
993 }
994 //else: had been already disabled, don't reenable
995 }
996
997 delete m_winDisabled;
998 }
999
1000 // Yield to other apps/messages and disable user input to all windows except
1001 // the given one
1002 bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)
1003 {
1004 wxWindowDisabler wd(win);
1005
1006 bool rc;
1007 if (onlyIfNeeded)
1008 rc = wxYieldIfNeeded();
1009 else
1010 rc = wxYield();
1011
1012 return rc;
1013 }
1014
1015 // Don't synthesize KeyUp events holding down a key and producing KeyDown
1016 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
1017 // in utilsgtk.cpp.
1018 #ifndef __WXGTK__
1019 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
1020 {
1021 return true; // detectable auto-repeat is the only mode MSW supports
1022 }
1023 #endif // !wxGTK
1024
1025 #endif // wxUSE_GUI