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