]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
set error to GSOCK_TIMEOUT if the socket timed out (modified and extended patch 1303554)
[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& url)
524 {
525 bool success = true;
526
527 wxString finalurl = url;
528
529 //if it isn't a full url, try appending http:// to it
530 if(wxURI(url).IsReference())
531 finalurl = wxString(wxT("http://")) + url;
532
533 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
534
535 wxString command;
536
537 // ShellExecute() always opens in the same window,
538 // so do it manually for new window (from Mahogany)
539 wxRegKey key(wxRegKey::HKCR, url.BeforeFirst(':') + wxT("\\shell\\open"));
540 if ( key.Exists() )
541 {
542 wxRegKey keyDDE(key, wxT("DDEExec"));
543 if ( keyDDE.Exists() )
544 {
545 wxRegKey keyTopic(keyDDE, wxT("topic"));
546 wxString ddeTopic = keyTopic.QueryDefaultValue();
547
548 // we only know the syntax of WWW_OpenURL DDE request
549 if ( ddeTopic == wxT("WWW_OpenURL") )
550 {
551 wxString ddeCmd = keyDDE.QueryDefaultValue();
552
553 // this is a bit naive but should work as -1 can't appear
554 // elsewhere in the DDE topic, normally
555 if ( ddeCmd.Replace(wxT("-1"), wxT("0"),
556 false /* only first occurence */) == 1 )
557 {
558 // and also replace the parameters
559 if ( ddeCmd.Replace(wxT("%1"), url, false) == 1 )
560 {
561 // magic incantation understood by wxMSW
562 command << wxT("WX_DDE#")
563 << wxRegKey(key, wxT("command")).QueryDefaultValue() << wxT('#')
564 << wxRegKey(keyDDE, wxT("application")).QueryDefaultValue()
565 << wxT('#') << ddeTopic << wxT('#')
566 << ddeCmd;
567 }
568 }
569 }
570 }
571 }
572
573 //Try wxExecute - if it doesn't work or the regkey stuff
574 //above failed, fallback to opening the file in the same
575 //browser window
576 if ( command.empty() || wxExecute(command) == -1)
577 {
578 int nResult; //HINSTANCE error code
579
580 #if !defined(__WXWINCE__)
581 // CYGWIN and MINGW may have problems - so load ShellExecute
582 // dynamically
583 typedef HINSTANCE (WINAPI *LPShellExecute)(HWND hwnd, const wxChar* lpOperation,
584 const wxChar* lpFile,
585 const wxChar* lpParameters,
586 const wxChar* lpDirectory,
587 INT nShowCmd);
588
589 HINSTANCE hShellDll = ::LoadLibrary(wxT("shell32.dll"));
590 if(hShellDll == NULL)
591 return false;
592
593 LPShellExecute lpShellExecute =
594 (LPShellExecute) ::GetProcAddress(hShellDll,
595 wxString::Format(wxT("ShellExecute%s"),
596
597 #if wxUSE_UNICODE
598 wxT("W")
599 #else
600 wxT("A")
601 #endif
602 #ifdef __WXWINCE__
603 )
604 #else
605 ).mb_str(wxConvLocal)
606 #endif
607 );
608 if(lpShellExecute == NULL)
609 return false;
610
611 // Windows sometimes doesn't open the browser correctly when using mime
612 // types, so do ShellExecute - i.e. start <url> (from James Carroll)
613 nResult = (int) (*lpShellExecute)(NULL, NULL, finalurl.c_str(),
614 NULL, wxT(""), SW_SHOWNORMAL);
615 // Unload Shell32.dll
616 ::FreeLibrary(hShellDll);
617 #else
618 //Windows CE does not have normal ShellExecute - but it has
619 //ShellExecuteEx all the way back to version 1.0
620
621
622 //Set up the SHELLEXECUTEINFO structure to pass to ShellExecuteEx
623 SHELLEXECUTEINFO sei;
624 sei.cbSize = sizeof(SHELLEXECUTEINFO);
625 sei.dwHotKey = 0;
626 sei.fMask = 0;
627 sei.hIcon = NULL;
628 sei.hInstApp = NULL;
629 sei.hkeyClass = NULL;
630 // Not in WinCE
631 #if 0
632 sei.hMonitor = NULL;
633 #endif
634 sei.hProcess = NULL;
635 sei.hwnd = NULL;
636 sei.lpClass = NULL;
637 sei.lpDirectory = NULL;
638 sei.lpFile = finalurl.c_str();
639 sei.lpIDList = NULL;
640 sei.lpParameters = NULL;
641 sei.lpVerb = TEXT("open");
642 sei.nShow = SW_SHOWNORMAL;
643
644 //Call ShellExecuteEx
645 ShellExecuteEx(&sei);
646
647 //Get error code
648 nResult = (int) sei.hInstApp;
649 #endif
650
651 // Hack for Firefox (returns file not found for some reason)
652 // from Angelo Mandato's wxHyperlinksCtrl
653 // HINSTANCE_ERROR == 32 (HINSTANCE_ERROR does not exist on Windows CE)
654 if (nResult <= 32 && nResult != SE_ERR_FNF)
655 return false;
656
657 #ifdef __WXDEBUG__
658 // Log something if SE_ERR_FNF happens
659 if(nResult == SE_ERR_FNF)
660 wxLogDebug(wxT("Got SE_ERR_FNF from ShellExecute - maybe FireFox"));
661 #endif
662 }
663
664 #elif wxUSE_MIMETYPE
665
666 // Non-windows way
667 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension (_T("html"));
668 if (!ft)
669 {
670 wxLogError(_T("No default application can open .html extension"));
671 return false;
672 }
673
674 wxString mt;
675 ft->GetMimeType(&mt);
676
677 wxString cmd;
678 bool ok = ft->GetOpenCommand (&cmd, wxFileType::MessageParameters(finalurl));
679 delete ft;
680
681 if (ok)
682 {
683 if( wxExecute (cmd, wxEXEC_ASYNC) == -1 )
684 {
685 wxLogError(_T("Failed to launch application for wxLaunchDefaultBrowser"));
686 return false;
687 }
688 }
689 else
690 {
691 // fallback to checking for the BROWSER environment variable
692 cmd = wxGetenv(wxT("BROWSER"));
693 if ( cmd.empty() || wxExecute(cmd + wxT(" ") + finalurl) == -1)
694 return false;
695 }
696
697
698 #else // !wxUSE_MIMETYPE && !(WXMSW && wxUSE_NATIVE_CONFIG)
699
700 success = false;
701
702 #endif
703
704 //success - hopefully
705 return success;
706 }
707
708 // ----------------------------------------------------------------------------
709 // wxApp::Yield() wrappers for backwards compatibility
710 // ----------------------------------------------------------------------------
711
712 bool wxYield()
713 {
714 return wxTheApp && wxTheApp->Yield();
715 }
716
717 bool wxYieldIfNeeded()
718 {
719 return wxTheApp && wxTheApp->Yield(true);
720 }
721
722 #endif // wxUSE_BASE
723
724 // ============================================================================
725 // GUI-only functions from now on
726 // ============================================================================
727
728 #if wxUSE_GUI
729
730 // Id generation
731 static long wxCurrentId = 100;
732
733 long wxNewId()
734 {
735 // skip the part of IDs space that contains hard-coded values:
736 if (wxCurrentId == wxID_LOWEST)
737 wxCurrentId = wxID_HIGHEST + 1;
738
739 return wxCurrentId++;
740 }
741
742 long
743 wxGetCurrentId(void) { return wxCurrentId; }
744
745 void
746 wxRegisterId (long id)
747 {
748 if (id >= wxCurrentId)
749 wxCurrentId = id + 1;
750 }
751
752 #if wxUSE_MENUS
753
754 // ----------------------------------------------------------------------------
755 // Menu accelerators related functions
756 // ----------------------------------------------------------------------------
757
758 wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out)
759 {
760 wxString s = wxMenuItem::GetLabelFromText(in);
761 if ( out )
762 {
763 // go smash their buffer if it's not big enough - I love char * params
764 memcpy(out, s.c_str(), s.length() * sizeof(wxChar));
765 }
766 else
767 {
768 // MYcopystring - for easier search...
769 out = new wxChar[s.length() + 1];
770 wxStrcpy(out, s.c_str());
771 }
772
773 return out;
774 }
775
776 wxString wxStripMenuCodes(const wxString& in)
777 {
778 wxString out;
779
780 size_t len = in.length();
781 out.reserve(len);
782
783 for ( size_t n = 0; n < len; n++ )
784 {
785 wxChar ch = in[n];
786 if ( ch == _T('&') )
787 {
788 // skip it, it is used to introduce the accel char (or to quote
789 // itself in which case it should still be skipped): note that it
790 // can't be the last character of the string
791 if ( ++n == len )
792 {
793 wxLogDebug(_T("Invalid menu string '%s'"), in.c_str());
794 }
795 else
796 {
797 // use the next char instead
798 ch = in[n];
799 }
800 }
801 else if ( ch == _T('\t') )
802 {
803 // everything after TAB is accel string, exit the loop
804 break;
805 }
806
807 out += ch;
808 }
809
810 return out;
811 }
812
813 #endif // wxUSE_MENUS
814
815 // ----------------------------------------------------------------------------
816 // Window search functions
817 // ----------------------------------------------------------------------------
818
819 /*
820 * If parent is non-NULL, look through children for a label or title
821 * matching the specified string. If NULL, look through all top-level windows.
822 *
823 */
824
825 wxWindow *
826 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
827 {
828 return wxWindow::FindWindowByLabel( title, parent );
829 }
830
831
832 /*
833 * If parent is non-NULL, look through children for a name
834 * matching the specified string. If NULL, look through all top-level windows.
835 *
836 */
837
838 wxWindow *
839 wxFindWindowByName (const wxString& name, wxWindow * parent)
840 {
841 return wxWindow::FindWindowByName( name, parent );
842 }
843
844 // Returns menu item id or wxNOT_FOUND if none.
845 int
846 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
847 {
848 #if wxUSE_MENUS
849 wxMenuBar *menuBar = frame->GetMenuBar ();
850 if ( menuBar )
851 return menuBar->FindMenuItem (menuString, itemString);
852 #endif // wxUSE_MENUS
853
854 return wxNOT_FOUND;
855 }
856
857 // Try to find the deepest child that contains 'pt'.
858 // We go backwards, to try to allow for controls that are spacially
859 // within other controls, but are still siblings (e.g. buttons within
860 // static boxes). Static boxes are likely to be created _before_ controls
861 // that sit inside them.
862 wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt)
863 {
864 if (!win->IsShown())
865 return NULL;
866
867 // Hack for wxNotebook case: at least in wxGTK, all pages
868 // claim to be shown, so we must only deal with the selected one.
869 #if wxUSE_NOTEBOOK
870 if (win->IsKindOf(CLASSINFO(wxNotebook)))
871 {
872 wxNotebook* nb = (wxNotebook*) win;
873 int sel = nb->GetSelection();
874 if (sel >= 0)
875 {
876 wxWindow* child = nb->GetPage(sel);
877 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
878 if (foundWin)
879 return foundWin;
880 }
881 }
882 #endif
883
884 wxWindowList::compatibility_iterator node = win->GetChildren().GetLast();
885 while (node)
886 {
887 wxWindow* child = node->GetData();
888 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
889 if (foundWin)
890 return foundWin;
891 node = node->GetPrevious();
892 }
893
894 wxPoint pos = win->GetPosition();
895 wxSize sz = win->GetSize();
896 if (win->GetParent())
897 {
898 pos = win->GetParent()->ClientToScreen(pos);
899 }
900
901 wxRect rect(pos, sz);
902 if (rect.Inside(pt))
903 return win;
904 else
905 return NULL;
906 }
907
908 wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
909 {
910 // Go backwards through the list since windows
911 // on top are likely to have been appended most
912 // recently.
913 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast();
914 while (node)
915 {
916 wxWindow* win = node->GetData();
917 wxWindow* found = wxFindWindowAtPoint(win, pt);
918 if (found)
919 return found;
920 node = node->GetPrevious();
921 }
922 return NULL;
923 }
924
925 // ----------------------------------------------------------------------------
926 // GUI helpers
927 // ----------------------------------------------------------------------------
928
929 /*
930 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
931 * since otherwise the generic code may be pulled in unnecessarily.
932 */
933
934 #if wxUSE_MSGDLG
935
936 int wxMessageBox(const wxString& message, const wxString& caption, long style,
937 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
938 {
939 long decorated_style = style;
940
941 if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION | wxICON_QUESTION ) ) == 0 )
942 {
943 decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ;
944 }
945
946 wxMessageDialog dialog(parent, message, caption, decorated_style);
947
948 int ans = dialog.ShowModal();
949 switch ( ans )
950 {
951 case wxID_OK:
952 return wxOK;
953 case wxID_YES:
954 return wxYES;
955 case wxID_NO:
956 return wxNO;
957 case wxID_CANCEL:
958 return wxCANCEL;
959 }
960
961 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
962
963 return wxCANCEL;
964 }
965
966 #endif // wxUSE_MSGDLG
967
968 #if wxUSE_TEXTDLG
969
970 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
971 const wxString& defaultValue, wxWindow *parent,
972 wxCoord x, wxCoord y, bool centre )
973 {
974 wxString str;
975 long style = wxTextEntryDialogStyle;
976
977 if (centre)
978 style |= wxCENTRE;
979 else
980 style &= ~wxCENTRE;
981
982 wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y));
983
984 if (dialog.ShowModal() == wxID_OK)
985 {
986 str = dialog.GetValue();
987 }
988
989 return str;
990 }
991
992 wxString wxGetPasswordFromUser(const wxString& message,
993 const wxString& caption,
994 const wxString& defaultValue,
995 wxWindow *parent,
996 wxCoord x, wxCoord y, bool centre )
997 {
998 wxString str;
999 long style = wxTextEntryDialogStyle;
1000
1001 if (centre)
1002 style |= wxCENTRE;
1003 else
1004 style &= ~wxCENTRE;
1005
1006 wxPasswordEntryDialog dialog(parent, message, caption, defaultValue,
1007 style, wxPoint(x, y));
1008 if ( dialog.ShowModal() == wxID_OK )
1009 {
1010 str = dialog.GetValue();
1011 }
1012
1013 return str;
1014 }
1015
1016 #endif // wxUSE_TEXTDLG
1017
1018 #if wxUSE_COLOURDLG
1019
1020 wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit)
1021 {
1022 wxColourData data;
1023 data.SetChooseFull(true);
1024 if ( colInit.Ok() )
1025 {
1026 data.SetColour((wxColour &)colInit); // const_cast
1027 }
1028
1029 wxColour colRet;
1030 wxColourDialog dialog(parent, &data);
1031 if ( dialog.ShowModal() == wxID_OK )
1032 {
1033 colRet = dialog.GetColourData().GetColour();
1034 }
1035 //else: leave it invalid
1036
1037 return colRet;
1038 }
1039
1040 #endif // wxUSE_COLOURDLG
1041
1042 #if wxUSE_FONTDLG
1043
1044 wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit)
1045 {
1046 wxFontData data;
1047 if ( fontInit.Ok() )
1048 {
1049 data.SetInitialFont(fontInit);
1050 }
1051
1052 wxFont fontRet;
1053 wxFontDialog dialog(parent, data);
1054 if ( dialog.ShowModal() == wxID_OK )
1055 {
1056 fontRet = dialog.GetFontData().GetChosenFont();
1057 }
1058 //else: leave it invalid
1059
1060 return fontRet;
1061 }
1062
1063 #endif // wxUSE_FONTDLG
1064
1065 // ----------------------------------------------------------------------------
1066 // wxSafeYield and supporting functions
1067 // ----------------------------------------------------------------------------
1068
1069 void wxEnableTopLevelWindows(bool enable)
1070 {
1071 wxWindowList::compatibility_iterator node;
1072 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1073 node->GetData()->Enable(enable);
1074 }
1075
1076 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
1077 {
1078 // remember the top level windows which were already disabled, so that we
1079 // don't reenable them later
1080 m_winDisabled = NULL;
1081
1082 wxWindowList::compatibility_iterator node;
1083 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1084 {
1085 wxWindow *winTop = node->GetData();
1086 if ( winTop == winToSkip )
1087 continue;
1088
1089 // we don't need to disable the hidden or already disabled windows
1090 if ( winTop->IsEnabled() && winTop->IsShown() )
1091 {
1092 winTop->Disable();
1093 }
1094 else
1095 {
1096 if ( !m_winDisabled )
1097 {
1098 m_winDisabled = new wxWindowList;
1099 }
1100
1101 m_winDisabled->Append(winTop);
1102 }
1103 }
1104 }
1105
1106 wxWindowDisabler::~wxWindowDisabler()
1107 {
1108 wxWindowList::compatibility_iterator node;
1109 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1110 {
1111 wxWindow *winTop = node->GetData();
1112 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
1113 {
1114 winTop->Enable();
1115 }
1116 //else: had been already disabled, don't reenable
1117 }
1118
1119 delete m_winDisabled;
1120 }
1121
1122 // Yield to other apps/messages and disable user input to all windows except
1123 // the given one
1124 bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)
1125 {
1126 wxWindowDisabler wd(win);
1127
1128 bool rc;
1129 if (onlyIfNeeded)
1130 rc = wxYieldIfNeeded();
1131 else
1132 rc = wxYield();
1133
1134 return rc;
1135 }
1136
1137 // Don't synthesize KeyUp events holding down a key and producing KeyDown
1138 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
1139 // in utilsgtk.cpp.
1140 #ifndef __WXGTK__
1141 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
1142 {
1143 return true; // detectable auto-repeat is the only mode MSW supports
1144 }
1145 #endif // !wxGTK
1146
1147 #endif // wxUSE_GUI