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