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