]>
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 | |
13d13a9e | 22 | // #pragma interface/implementation correctly, iff |
e4844687 SN |
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 | |
ffecfa5a | 98 | #if defined(__WXMSW__) && !defined(__PALMOS__) |
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 | { | |
10f206ad RL |
290 | wxString dir = wxGetInstallPrefix(); |
291 | dir << wxFILE_SEP_PATH << wxT("share") << wxFILE_SEP_PATH << wxT("wx"); | |
7c072018 VZ |
292 | return dir; |
293 | } | |
e90c1d2a | 294 | |
2739d4f0 VZ |
295 | int wxGetOsVersion(int *verMaj, int *verMin) |
296 | { | |
297 | // we want this function to work even if there is no wxApp | |
298 | wxConsoleAppTraits traitsConsole; | |
299 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
300 | if ( ! traits ) | |
301 | traits = &traitsConsole; | |
302 | ||
324899f6 | 303 | wxToolkitInfo& info = traits->GetToolkitInfo(); |
a8eaaeb2 | 304 | if ( verMaj ) |
324899f6 | 305 | *verMaj = info.versionMajor; |
a8eaaeb2 | 306 | if ( verMin ) |
324899f6 VS |
307 | *verMin = info.versionMinor; |
308 | return info.os; | |
2739d4f0 | 309 | } |
1e6feb95 | 310 | |
e90c1d2a | 311 | // ---------------------------------------------------------------------------- |
7c072018 | 312 | // network and user id functions |
e90c1d2a | 313 | // ---------------------------------------------------------------------------- |
c801d85f | 314 | |
7c072018 VZ |
315 | // Get Full RFC822 style email address |
316 | bool wxGetEmailAddress(wxChar *address, int maxSize) | |
c801d85f | 317 | { |
7c072018 VZ |
318 | wxString email = wxGetEmailAddress(); |
319 | if ( !email ) | |
cb719f2e | 320 | return false; |
c801d85f | 321 | |
7c072018 VZ |
322 | wxStrncpy(address, email, maxSize - 1); |
323 | address[maxSize - 1] = wxT('\0'); | |
324 | ||
cb719f2e | 325 | return true; |
c801d85f KB |
326 | } |
327 | ||
7c072018 | 328 | wxString wxGetEmailAddress() |
47bc1060 | 329 | { |
7c072018 | 330 | wxString email; |
974e8d94 | 331 | |
7c072018 | 332 | wxString host = wxGetFullHostName(); |
cb719f2e | 333 | if ( !host.IsEmpty() ) |
1e6feb95 | 334 | { |
7c072018 | 335 | wxString user = wxGetUserId(); |
cb719f2e | 336 | if ( !user.IsEmpty() ) |
1e6feb95 | 337 | { |
7c072018 | 338 | email << user << wxT('@') << host; |
974e8d94 | 339 | } |
974e8d94 VZ |
340 | } |
341 | ||
7c072018 | 342 | return email; |
974e8d94 VZ |
343 | } |
344 | ||
7c072018 | 345 | wxString wxGetUserId() |
c801d85f | 346 | { |
7c072018 | 347 | static const int maxLoginLen = 256; // FIXME arbitrary number |
c801d85f | 348 | |
7c072018 | 349 | wxString buf; |
4c3ebca9 | 350 | bool ok = wxGetUserId(wxStringBuffer(buf, maxLoginLen), maxLoginLen); |
c801d85f | 351 | |
7c072018 VZ |
352 | if ( !ok ) |
353 | buf.Empty(); | |
c801d85f | 354 | |
7c072018 | 355 | return buf; |
c801d85f KB |
356 | } |
357 | ||
7c072018 | 358 | wxString wxGetUserName() |
c801d85f | 359 | { |
7c072018 | 360 | static const int maxUserNameLen = 1024; // FIXME arbitrary number |
1e6feb95 | 361 | |
7c072018 | 362 | wxString buf; |
4c3ebca9 | 363 | bool ok = wxGetUserName(wxStringBuffer(buf, maxUserNameLen), maxUserNameLen); |
7c072018 VZ |
364 | |
365 | if ( !ok ) | |
366 | buf.Empty(); | |
367 | ||
368 | return buf; | |
59a12e90 JS |
369 | } |
370 | ||
7c072018 | 371 | wxString wxGetHostName() |
59a12e90 | 372 | { |
7c072018 | 373 | static const size_t hostnameSize = 257; |
c67d6888 | 374 | |
7c072018 | 375 | wxString buf; |
4c3ebca9 | 376 | bool ok = wxGetHostName(wxStringBuffer(buf, hostnameSize), hostnameSize); |
59a12e90 | 377 | |
7c072018 VZ |
378 | if ( !ok ) |
379 | buf.Empty(); | |
59a12e90 | 380 | |
7c072018 | 381 | return buf; |
59a12e90 JS |
382 | } |
383 | ||
7c072018 | 384 | wxString wxGetFullHostName() |
59a12e90 | 385 | { |
7c072018 | 386 | static const size_t hostnameSize = 257; |
c801d85f | 387 | |
7c072018 | 388 | wxString buf; |
4c3ebca9 | 389 | bool ok = wxGetFullHostName(wxStringBuffer(buf, hostnameSize), hostnameSize); |
7c072018 VZ |
390 | |
391 | if ( !ok ) | |
392 | buf.Empty(); | |
c801d85f | 393 | |
7c072018 VZ |
394 | return buf; |
395 | } | |
c801d85f | 396 | |
7c072018 VZ |
397 | wxString wxGetHomeDir() |
398 | { | |
399 | wxString home; | |
400 | wxGetHomeDir(&home); | |
c801d85f | 401 | |
7c072018 VZ |
402 | return home; |
403 | } | |
c801d85f | 404 | |
c801d85f KB |
405 | #if 0 |
406 | ||
7c072018 | 407 | wxString wxGetCurrentDir() |
c801d85f | 408 | { |
7c072018 VZ |
409 | wxString dir; |
410 | size_t len = 1024; | |
411 | bool ok; | |
412 | do | |
413 | { | |
414 | ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL; | |
415 | dir.UngetWriteBuf(); | |
c801d85f | 416 | |
7c072018 VZ |
417 | if ( !ok ) |
418 | { | |
419 | if ( errno != ERANGE ) | |
420 | { | |
421 | wxLogSysError(_T("Failed to get current directory")); | |
c801d85f | 422 | |
7c072018 VZ |
423 | return wxEmptyString; |
424 | } | |
3f4a0c5b | 425 | else |
7c072018 VZ |
426 | { |
427 | // buffer was too small, retry with a larger one | |
428 | len *= 2; | |
429 | } | |
3f4a0c5b | 430 | } |
7c072018 VZ |
431 | //else: ok |
432 | } while ( !ok ); | |
c801d85f | 433 | |
7c072018 | 434 | return dir; |
c801d85f KB |
435 | } |
436 | ||
7c072018 | 437 | #endif // 0 |
e90c1d2a VZ |
438 | |
439 | // ---------------------------------------------------------------------------- | |
7c072018 | 440 | // wxExecute |
e90c1d2a | 441 | // ---------------------------------------------------------------------------- |
ead7ce10 | 442 | |
7c072018 VZ |
443 | // wxDoExecuteWithCapture() helper: reads an entire stream into one array |
444 | // | |
cb719f2e | 445 | // returns true if ok, false if error |
7c072018 VZ |
446 | #if wxUSE_STREAMS |
447 | static bool ReadAll(wxInputStream *is, wxArrayString& output) | |
dfad0599 | 448 | { |
cb719f2e | 449 | wxCHECK_MSG( is, false, _T("NULL stream in wxExecute()?") ); |
dfad0599 | 450 | |
7c072018 VZ |
451 | // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state |
452 | is->Reset(); | |
1e6feb95 | 453 | |
7c072018 | 454 | wxTextInputStream tis(*is); |
1e6feb95 | 455 | |
cb719f2e | 456 | bool cont = true; |
7c072018 | 457 | while ( cont ) |
d2f50933 | 458 | { |
7c072018 VZ |
459 | wxString line = tis.ReadLine(); |
460 | if ( is->Eof() ) | |
461 | break; | |
462 | ||
463 | if ( !*is ) | |
464 | { | |
cb719f2e | 465 | cont = false; |
7c072018 VZ |
466 | } |
467 | else | |
468 | { | |
469 | output.Add(line); | |
470 | } | |
d2f50933 VZ |
471 | } |
472 | ||
7c072018 | 473 | return cont; |
dfad0599 | 474 | } |
7c072018 | 475 | #endif // wxUSE_STREAMS |
d2f50933 | 476 | |
7c072018 VZ |
477 | // this is a private function because it hasn't a clean interface: the first |
478 | // array is passed by reference, the second by pointer - instead we have 2 | |
479 | // public versions of wxExecute() below | |
480 | static long wxDoExecuteWithCapture(const wxString& command, | |
481 | wxArrayString& output, | |
482 | wxArrayString* error) | |
d2f50933 | 483 | { |
7c072018 VZ |
484 | // create a wxProcess which will capture the output |
485 | wxProcess *process = new wxProcess; | |
486 | process->Redirect(); | |
dfad0599 | 487 | |
7c072018 | 488 | long rc = wxExecute(command, wxEXEC_SYNC, process); |
1e6feb95 | 489 | |
7c072018 VZ |
490 | #if wxUSE_STREAMS |
491 | if ( rc != -1 ) | |
bf31fa26 | 492 | { |
7c072018 VZ |
493 | if ( !ReadAll(process->GetInputStream(), output) ) |
494 | rc = -1; | |
91b4c08d | 495 | |
7c072018 VZ |
496 | if ( error ) |
497 | { | |
498 | if ( !ReadAll(process->GetErrorStream(), *error) ) | |
499 | rc = -1; | |
500 | } | |
91b4c08d | 501 | |
7c072018 | 502 | } |
e70ba80d WS |
503 | #else |
504 | wxUnusedVar(output); | |
505 | wxUnusedVar(error); | |
506 | #endif // wxUSE_STREAMS/!wxUSE_STREAMS | |
91b4c08d | 507 | |
7c072018 | 508 | delete process; |
1e6feb95 | 509 | |
7c072018 | 510 | return rc; |
7c072018 | 511 | } |
bf31fa26 | 512 | |
7c072018 | 513 | long wxExecute(const wxString& command, wxArrayString& output) |
bf31fa26 | 514 | { |
7c072018 VZ |
515 | return wxDoExecuteWithCapture(command, output, NULL); |
516 | } | |
bf31fa26 | 517 | |
7c072018 VZ |
518 | long wxExecute(const wxString& command, |
519 | wxArrayString& output, | |
520 | wxArrayString& error) | |
521 | { | |
522 | return wxDoExecuteWithCapture(command, output, &error); | |
bf31fa26 VZ |
523 | } |
524 | ||
91b4c08d | 525 | // ---------------------------------------------------------------------------- |
7c072018 | 526 | // wxApp::Yield() wrappers for backwards compatibility |
91b4c08d VZ |
527 | // ---------------------------------------------------------------------------- |
528 | ||
7c072018 | 529 | bool wxYield() |
469e1e5c | 530 | { |
7c072018 | 531 | return wxTheApp && wxTheApp->Yield(); |
469e1e5c | 532 | } |
7c072018 VZ |
533 | |
534 | bool wxYieldIfNeeded() | |
469e1e5c | 535 | { |
cb719f2e | 536 | return wxTheApp && wxTheApp->Yield(true); |
469e1e5c | 537 | } |
7c072018 | 538 | |
ec67cff1 | 539 | #endif // wxUSE_BASE |
7c072018 VZ |
540 | |
541 | // ============================================================================ | |
542 | // GUI-only functions from now on | |
543 | // ============================================================================ | |
544 | ||
545 | #if wxUSE_GUI | |
546 | ||
4c3ebca9 MB |
547 | // Id generation |
548 | static long wxCurrentId = 100; | |
549 | ||
c838b68e | 550 | long wxNewId() |
4c3ebca9 | 551 | { |
c838b68e VS |
552 | // skip the part of IDs space that contains hard-coded values: |
553 | if (wxCurrentId == wxID_LOWEST) | |
554 | wxCurrentId = wxID_HIGHEST + 1; | |
555 | ||
556 | return wxCurrentId++; | |
4c3ebca9 MB |
557 | } |
558 | ||
559 | long | |
560 | wxGetCurrentId(void) { return wxCurrentId; } | |
561 | ||
562 | void | |
563 | wxRegisterId (long id) | |
564 | { | |
565 | if (id >= wxCurrentId) | |
566 | wxCurrentId = id + 1; | |
567 | } | |
568 | ||
7c072018 | 569 | #if wxUSE_MENUS |
e90c1d2a VZ |
570 | |
571 | // ---------------------------------------------------------------------------- | |
7c072018 | 572 | // Menu accelerators related functions |
e90c1d2a VZ |
573 | // ---------------------------------------------------------------------------- |
574 | ||
7c072018 | 575 | wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out) |
e90c1d2a | 576 | { |
7c072018 VZ |
577 | wxString s = wxMenuItem::GetLabelFromText(in); |
578 | if ( out ) | |
579 | { | |
580 | // go smash their buffer if it's not big enough - I love char * params | |
581 | memcpy(out, s.c_str(), s.length() * sizeof(wxChar)); | |
582 | } | |
583 | else | |
584 | { | |
f526f752 MB |
585 | // MYcopystring - for easier search... |
586 | out = new wxChar[s.length() + 1]; | |
587 | wxStrcpy(out, s.c_str()); | |
7c072018 VZ |
588 | } |
589 | ||
590 | return out; | |
e90c1d2a VZ |
591 | } |
592 | ||
7c072018 | 593 | wxString wxStripMenuCodes(const wxString& in) |
e90c1d2a | 594 | { |
7c072018 | 595 | wxString out; |
79f585d9 | 596 | |
7c072018 VZ |
597 | size_t len = in.length(); |
598 | out.reserve(len); | |
79f585d9 | 599 | |
7c072018 VZ |
600 | for ( size_t n = 0; n < len; n++ ) |
601 | { | |
602 | wxChar ch = in[n]; | |
603 | if ( ch == _T('&') ) | |
cd6ce4a9 | 604 | { |
7c072018 VZ |
605 | // skip it, it is used to introduce the accel char (or to quote |
606 | // itself in which case it should still be skipped): note that it | |
607 | // can't be the last character of the string | |
608 | if ( ++n == len ) | |
79f585d9 | 609 | { |
7c072018 VZ |
610 | wxLogDebug(_T("Invalid menu string '%s'"), in.c_str()); |
611 | } | |
612 | else | |
613 | { | |
614 | // use the next char instead | |
615 | ch = in[n]; | |
79f585d9 | 616 | } |
cd6ce4a9 | 617 | } |
7c072018 | 618 | else if ( ch == _T('\t') ) |
79f585d9 | 619 | { |
7c072018 VZ |
620 | // everything after TAB is accel string, exit the loop |
621 | break; | |
79f585d9 | 622 | } |
7c072018 VZ |
623 | |
624 | out += ch; | |
225fe9d6 VZ |
625 | } |
626 | ||
7c072018 | 627 | return out; |
cd6ce4a9 VZ |
628 | } |
629 | ||
7c072018 | 630 | #endif // wxUSE_MENUS |
e90c1d2a | 631 | |
cbc66a27 | 632 | // ---------------------------------------------------------------------------- |
7c072018 | 633 | // Window search functions |
cbc66a27 VZ |
634 | // ---------------------------------------------------------------------------- |
635 | ||
7c072018 VZ |
636 | /* |
637 | * If parent is non-NULL, look through children for a label or title | |
638 | * matching the specified string. If NULL, look through all top-level windows. | |
639 | * | |
640 | */ | |
e2a6f233 | 641 | |
7c072018 VZ |
642 | wxWindow * |
643 | wxFindWindowByLabel (const wxString& title, wxWindow * parent) | |
134677bd | 644 | { |
7c072018 VZ |
645 | return wxWindow::FindWindowByLabel( title, parent ); |
646 | } | |
b829bf55 | 647 | |
b829bf55 | 648 | |
7c072018 VZ |
649 | /* |
650 | * If parent is non-NULL, look through children for a name | |
651 | * matching the specified string. If NULL, look through all top-level windows. | |
652 | * | |
653 | */ | |
134677bd | 654 | |
7c072018 VZ |
655 | wxWindow * |
656 | wxFindWindowByName (const wxString& name, wxWindow * parent) | |
2c18f21d | 657 | { |
7c072018 | 658 | return wxWindow::FindWindowByName( name, parent ); |
2c18f21d VS |
659 | } |
660 | ||
cb719f2e | 661 | // Returns menu item id or wxNOT_FOUND if none. |
7c072018 VZ |
662 | int |
663 | wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString) | |
e2a6f233 | 664 | { |
7c072018 VZ |
665 | #if wxUSE_MENUS |
666 | wxMenuBar *menuBar = frame->GetMenuBar (); | |
667 | if ( menuBar ) | |
668 | return menuBar->FindMenuItem (menuString, itemString); | |
669 | #endif // wxUSE_MENUS | |
0fb67cd1 | 670 | |
cb719f2e | 671 | return wxNOT_FOUND; |
e2a6f233 JS |
672 | } |
673 | ||
7c072018 VZ |
674 | // Try to find the deepest child that contains 'pt'. |
675 | // We go backwards, to try to allow for controls that are spacially | |
676 | // within other controls, but are still siblings (e.g. buttons within | |
677 | // static boxes). Static boxes are likely to be created _before_ controls | |
678 | // that sit inside them. | |
679 | wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt) | |
e2a6f233 | 680 | { |
7c072018 VZ |
681 | if (!win->IsShown()) |
682 | return NULL; | |
0fb67cd1 | 683 | |
7c072018 VZ |
684 | // Hack for wxNotebook case: at least in wxGTK, all pages |
685 | // claim to be shown, so we must only deal with the selected one. | |
686 | #if wxUSE_NOTEBOOK | |
687 | if (win->IsKindOf(CLASSINFO(wxNotebook))) | |
e2a6f233 | 688 | { |
7c072018 VZ |
689 | wxNotebook* nb = (wxNotebook*) win; |
690 | int sel = nb->GetSelection(); | |
691 | if (sel >= 0) | |
692 | { | |
693 | wxWindow* child = nb->GetPage(sel); | |
694 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); | |
695 | if (foundWin) | |
696 | return foundWin; | |
697 | } | |
e2a6f233 | 698 | } |
7c072018 | 699 | #endif |
0fb67cd1 | 700 | |
df5168c4 | 701 | wxWindowList::compatibility_iterator node = win->GetChildren().GetLast(); |
7c072018 VZ |
702 | while (node) |
703 | { | |
704 | wxWindow* child = node->GetData(); | |
705 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); | |
706 | if (foundWin) | |
707 | return foundWin; | |
708 | node = node->GetPrevious(); | |
709 | } | |
710 | ||
711 | wxPoint pos = win->GetPosition(); | |
712 | wxSize sz = win->GetSize(); | |
713 | if (win->GetParent()) | |
714 | { | |
715 | pos = win->GetParent()->ClientToScreen(pos); | |
716 | } | |
717 | ||
718 | wxRect rect(pos, sz); | |
719 | if (rect.Inside(pt)) | |
720 | return win; | |
721 | else | |
722 | return NULL; | |
0fb67cd1 VZ |
723 | } |
724 | ||
7c072018 | 725 | wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt) |
0fb67cd1 | 726 | { |
7c072018 VZ |
727 | // Go backwards through the list since windows |
728 | // on top are likely to have been appended most | |
729 | // recently. | |
df5168c4 | 730 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetLast(); |
7c072018 VZ |
731 | while (node) |
732 | { | |
733 | wxWindow* win = node->GetData(); | |
734 | wxWindow* found = wxFindWindowAtPoint(win, pt); | |
735 | if (found) | |
736 | return found; | |
737 | node = node->GetPrevious(); | |
738 | } | |
739 | return NULL; | |
740 | } | |
0fb67cd1 | 741 | |
7c072018 VZ |
742 | // ---------------------------------------------------------------------------- |
743 | // GUI helpers | |
744 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 745 | |
7c072018 VZ |
746 | /* |
747 | * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp | |
748 | * since otherwise the generic code may be pulled in unnecessarily. | |
749 | */ | |
0fb67cd1 | 750 | |
7c072018 | 751 | #if wxUSE_MSGDLG |
0fb67cd1 | 752 | |
7c072018 VZ |
753 | int wxMessageBox(const wxString& message, const wxString& caption, long style, |
754 | wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) ) | |
0fb67cd1 | 755 | { |
53a6ac21 DS |
756 | long decorated_style = style; |
757 | ||
758 | if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION | wxICON_QUESTION ) ) == 0 ) | |
759 | { | |
760 | decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ; | |
761 | } | |
762 | ||
763 | wxMessageDialog dialog(parent, message, caption, decorated_style); | |
0fb67cd1 | 764 | |
7c072018 VZ |
765 | int ans = dialog.ShowModal(); |
766 | switch ( ans ) | |
767 | { | |
768 | case wxID_OK: | |
769 | return wxOK; | |
770 | case wxID_YES: | |
771 | return wxYES; | |
772 | case wxID_NO: | |
773 | return wxNO; | |
774 | case wxID_CANCEL: | |
775 | return wxCANCEL; | |
776 | } | |
0fb67cd1 | 777 | |
7c072018 | 778 | wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") ); |
0fb67cd1 | 779 | |
7c072018 | 780 | return wxCANCEL; |
e2a6f233 JS |
781 | } |
782 | ||
7c072018 | 783 | #endif // wxUSE_MSGDLG |
518b5d2f | 784 | |
7c072018 | 785 | #if wxUSE_TEXTDLG |
518b5d2f | 786 | |
7c072018 VZ |
787 | wxString wxGetTextFromUser(const wxString& message, const wxString& caption, |
788 | const wxString& defaultValue, wxWindow *parent, | |
13d13a9e | 789 | wxCoord x, wxCoord y, bool centre ) |
7c072018 VZ |
790 | { |
791 | wxString str; | |
13d13a9e WS |
792 | long style = wxTextEntryDialogStyle; |
793 | ||
794 | if (centre) | |
795 | style |= wxCENTRE; | |
796 | else | |
797 | style &= ~wxCENTRE; | |
798 | ||
799 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y)); | |
800 | ||
7c072018 VZ |
801 | if (dialog.ShowModal() == wxID_OK) |
802 | { | |
803 | str = dialog.GetValue(); | |
804 | } | |
0fb67cd1 | 805 | |
7c072018 | 806 | return str; |
518b5d2f VZ |
807 | } |
808 | ||
7c072018 VZ |
809 | wxString wxGetPasswordFromUser(const wxString& message, |
810 | const wxString& caption, | |
811 | const wxString& defaultValue, | |
b3bb2a74 KH |
812 | wxWindow *parent, |
813 | wxCoord x, wxCoord y, bool centre ) | |
96c5bd7f | 814 | { |
7c072018 | 815 | wxString str; |
b3bb2a74 KH |
816 | long style = wxTextEntryDialogStyle; |
817 | ||
818 | if (centre) | |
819 | style |= wxCENTRE; | |
820 | else | |
821 | style &= ~wxCENTRE; | |
822 | ||
12cfa304 KH |
823 | wxPasswordEntryDialog dialog(parent, message, caption, defaultValue, |
824 | style, wxPoint(x, y)); | |
7c072018 VZ |
825 | if ( dialog.ShowModal() == wxID_OK ) |
826 | { | |
827 | str = dialog.GetValue(); | |
828 | } | |
96c5bd7f | 829 | |
7c072018 VZ |
830 | return str; |
831 | } | |
96c5bd7f | 832 | |
7c072018 | 833 | #endif // wxUSE_TEXTDLG |
96c5bd7f | 834 | |
7c072018 | 835 | #if wxUSE_COLOURDLG |
96c5bd7f | 836 | |
7c072018 | 837 | wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit) |
c51deffc | 838 | { |
7c072018 | 839 | wxColourData data; |
cb719f2e | 840 | data.SetChooseFull(true); |
7c072018 VZ |
841 | if ( colInit.Ok() ) |
842 | { | |
843 | data.SetColour((wxColour &)colInit); // const_cast | |
844 | } | |
c51deffc | 845 | |
7c072018 VZ |
846 | wxColour colRet; |
847 | wxColourDialog dialog(parent, &data); | |
848 | if ( dialog.ShowModal() == wxID_OK ) | |
849 | { | |
850 | colRet = dialog.GetColourData().GetColour(); | |
851 | } | |
852 | //else: leave it invalid | |
853 | ||
854 | return colRet; | |
c51deffc | 855 | } |
bc385ba9 | 856 | |
7c072018 | 857 | #endif // wxUSE_COLOURDLG |
bc385ba9 | 858 | |
7c072018 VZ |
859 | #if wxUSE_FONTDLG |
860 | ||
861 | wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit) | |
bc385ba9 | 862 | { |
7c072018 VZ |
863 | wxFontData data; |
864 | if ( fontInit.Ok() ) | |
bc385ba9 | 865 | { |
7c072018 VZ |
866 | data.SetInitialFont(fontInit); |
867 | } | |
bc385ba9 | 868 | |
7c072018 VZ |
869 | wxFont fontRet; |
870 | wxFontDialog dialog(parent, data); | |
871 | if ( dialog.ShowModal() == wxID_OK ) | |
872 | { | |
873 | fontRet = dialog.GetFontData().GetChosenFont(); | |
874 | } | |
875 | //else: leave it invalid | |
bc385ba9 | 876 | |
7c072018 | 877 | return fontRet; |
bc385ba9 VZ |
878 | } |
879 | ||
7c072018 | 880 | #endif // wxUSE_FONTDLG |
2b5f62a0 | 881 | |
7c072018 VZ |
882 | // ---------------------------------------------------------------------------- |
883 | // wxSafeYield and supporting functions | |
884 | // ---------------------------------------------------------------------------- | |
2b5f62a0 | 885 | |
7c072018 VZ |
886 | void wxEnableTopLevelWindows(bool enable) |
887 | { | |
df5168c4 | 888 | wxWindowList::compatibility_iterator node; |
7c072018 VZ |
889 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
890 | node->GetData()->Enable(enable); | |
891 | } | |
2b5f62a0 | 892 | |
7c072018 VZ |
893 | wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip) |
894 | { | |
895 | // remember the top level windows which were already disabled, so that we | |
896 | // don't reenable them later | |
897 | m_winDisabled = NULL; | |
898 | ||
df5168c4 | 899 | wxWindowList::compatibility_iterator node; |
7c072018 | 900 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
2b5f62a0 | 901 | { |
7c072018 VZ |
902 | wxWindow *winTop = node->GetData(); |
903 | if ( winTop == winToSkip ) | |
904 | continue; | |
2b5f62a0 | 905 | |
7c072018 VZ |
906 | // we don't need to disable the hidden or already disabled windows |
907 | if ( winTop->IsEnabled() && winTop->IsShown() ) | |
2b5f62a0 | 908 | { |
7c072018 | 909 | winTop->Disable(); |
2b5f62a0 VZ |
910 | } |
911 | else | |
912 | { | |
7c072018 VZ |
913 | if ( !m_winDisabled ) |
914 | { | |
915 | m_winDisabled = new wxWindowList; | |
916 | } | |
917 | ||
918 | m_winDisabled->Append(winTop); | |
2b5f62a0 VZ |
919 | } |
920 | } | |
2b5f62a0 VZ |
921 | } |
922 | ||
7c072018 | 923 | wxWindowDisabler::~wxWindowDisabler() |
cd6ce4a9 | 924 | { |
df5168c4 | 925 | wxWindowList::compatibility_iterator node; |
7c072018 | 926 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
cd6ce4a9 | 927 | { |
7c072018 VZ |
928 | wxWindow *winTop = node->GetData(); |
929 | if ( !m_winDisabled || !m_winDisabled->Find(winTop) ) | |
cd6ce4a9 | 930 | { |
7c072018 | 931 | winTop->Enable(); |
cd6ce4a9 | 932 | } |
7c072018 | 933 | //else: had been already disabled, don't reenable |
cd6ce4a9 | 934 | } |
f6bcfd97 | 935 | |
7c072018 | 936 | delete m_winDisabled; |
f6bcfd97 BP |
937 | } |
938 | ||
7c072018 VZ |
939 | // Yield to other apps/messages and disable user input to all windows except |
940 | // the given one | |
941 | bool wxSafeYield(wxWindow *win, bool onlyIfNeeded) | |
f6bcfd97 | 942 | { |
7c072018 | 943 | wxWindowDisabler wd(win); |
21709999 | 944 | |
7c072018 VZ |
945 | bool rc; |
946 | if (onlyIfNeeded) | |
947 | rc = wxYieldIfNeeded(); | |
948 | else | |
949 | rc = wxYield(); | |
8461e4c2 | 950 | |
7c072018 | 951 | return rc; |
8461e4c2 VZ |
952 | } |
953 | ||
7c072018 VZ |
954 | // Don't synthesize KeyUp events holding down a key and producing KeyDown |
955 | // events with autorepeat. On by default and always on in wxMSW. wxGTK version | |
956 | // in utilsgtk.cpp. | |
957 | #ifndef __WXGTK__ | |
958 | bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) | |
8461e4c2 | 959 | { |
cb719f2e | 960 | return true; // detectable auto-repeat is the only mode MSW supports |
8461e4c2 | 961 | } |
7c072018 VZ |
962 | #endif // !wxGTK |
963 | ||
964 | #endif // wxUSE_GUI | |
21709999 | 965 |