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