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