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