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