]>
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 | |
3f4a0c5b | 9 | // Licence: wxWindows license |
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 | |
e90c1d2a VZ |
32 | #include "wx/defs.h" |
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" | |
974e8d94 VZ |
44 | #if wxUSE_ACCEL |
45 | #include "wx/menuitem.h" | |
46 | #include "wx/accel.h" | |
47 | #endif // wxUSE_ACCEL | |
e90c1d2a VZ |
48 | #endif // wxUSE_GUI |
49 | #endif // WX_PRECOMP | |
c801d85f | 50 | |
669f7a11 | 51 | #ifndef __WIN16__ |
cd6ce4a9 VZ |
52 | #include "wx/process.h" |
53 | #include "wx/txtstrm.h" | |
669f7a11 | 54 | #endif |
cd6ce4a9 | 55 | |
c801d85f KB |
56 | #include <ctype.h> |
57 | #include <stdio.h> | |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
e90c1d2a | 60 | |
c801d85f | 61 | #if !defined(__WATCOMC__) |
3f4a0c5b VZ |
62 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
63 | #include <errno.h> | |
64 | #endif | |
c801d85f | 65 | #endif |
e90c1d2a | 66 | |
91b4c08d VZ |
67 | #if wxUSE_GUI |
68 | #include "wx/colordlg.h" | |
d1c8aaa3 JS |
69 | #include "wx/notebook.h" |
70 | #include "wx/frame.h" | |
71 | #include "wx/statusbr.h" | |
72 | #include "wx/toolbar.h" | |
91b4c08d VZ |
73 | #endif // wxUSE_GUI |
74 | ||
c801d85f | 75 | #include <time.h> |
e90c1d2a | 76 | |
469e1e5c | 77 | #ifndef __MWERKS__ |
e90c1d2a VZ |
78 | #include <sys/types.h> |
79 | #include <sys/stat.h> | |
469e1e5c | 80 | #endif |
c801d85f | 81 | |
ce3ed50d | 82 | #ifdef __SALFORDC__ |
e90c1d2a | 83 | #include <clib.h> |
ce3ed50d JS |
84 | #endif |
85 | ||
2049ba38 | 86 | #ifdef __WXMSW__ |
5e1febfa | 87 | #include "wx/msw/private.h" |
c801d85f KB |
88 | #endif |
89 | ||
e90c1d2a VZ |
90 | // ---------------------------------------------------------------------------- |
91 | // function protoypes | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | #if wxUSE_GUI | |
95 | static wxWindow *wxFindWindowByLabel1(const wxString& title, wxWindow *parent); | |
96 | static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow *parent); | |
97 | #endif // wxUSE_GUI | |
c801d85f | 98 | |
e90c1d2a VZ |
99 | // ============================================================================ |
100 | // implementation | |
101 | // ============================================================================ | |
c801d85f | 102 | |
e146b8c8 | 103 | // ---------------------------------------------------------------------------- |
e90c1d2a | 104 | // string functions |
e146b8c8 VZ |
105 | // ---------------------------------------------------------------------------- |
106 | ||
17dff81c SC |
107 | #ifdef __WXMAC__ |
108 | int strcasecmp(const char *str_1, const char *str_2) | |
109 | { | |
110 | register char c1, c2; | |
111 | do { | |
112 | c1 = tolower(*str_1++); | |
113 | c2 = tolower(*str_2++); | |
114 | } while ( c1 && (c1 == c2) ); | |
115 | ||
116 | return c1 - c2; | |
117 | } | |
118 | ||
119 | int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) | |
120 | { | |
121 | ||
122 | register char c1, c2; | |
3f4a0c5b | 123 | while( maxchar--) |
17dff81c SC |
124 | { |
125 | c1 = tolower(*str_1++); | |
126 | c2 = tolower(*str_2++); | |
3f4a0c5b | 127 | |
17dff81c | 128 | if ( !c1 || c1!=c2 ) |
3f4a0c5b VZ |
129 | return c1 - c2; |
130 | ||
17dff81c SC |
131 | } ; |
132 | ||
133 | return 0 ; | |
134 | ||
135 | } | |
e90c1d2a VZ |
136 | #endif // wxMAC |
137 | ||
7e72d7aa | 138 | #if defined( __VMS__ ) && ( __VMS_VER < 70000000 ) |
c801d85f KB |
139 | // we have no strI functions under VMS, therefore I have implemented |
140 | // an inefficient but portable version: convert copies of strings to lowercase | |
141 | // and then use the normal comparison | |
142 | static void myLowerString(char *s) | |
143 | { | |
144 | while(*s){ | |
145 | if(isalpha(*s)) *s = (char)tolower(*s); | |
146 | s++; | |
147 | } | |
148 | } | |
149 | ||
150 | int strcasecmp(const char *str_1, const char *str_2) | |
151 | { | |
152 | char *temp1 = new char[strlen(str_1)+1]; | |
153 | char *temp2 = new char[strlen(str_2)+1]; | |
154 | strcpy(temp1,str_1); | |
155 | strcpy(temp2,str_2); | |
156 | myLowerString(temp1); | |
157 | myLowerString(temp2); | |
158 | ||
81c67e27 | 159 | int result = wxStrcmp(temp1,temp2); |
c801d85f KB |
160 | delete[] temp1; |
161 | delete[] temp2; | |
162 | ||
163 | return(result); | |
164 | } | |
165 | ||
166 | int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) | |
167 | { | |
168 | char *temp1 = new char[strlen(str_1)+1]; | |
169 | char *temp2 = new char[strlen(str_2)+1]; | |
170 | strcpy(temp1,str_1); | |
171 | strcpy(temp2,str_2); | |
172 | myLowerString(temp1); | |
173 | myLowerString(temp2); | |
174 | ||
175 | int result = strncmp(temp1,temp2,maxchar); | |
176 | delete[] temp1; | |
177 | delete[] temp2; | |
178 | ||
179 | return(result); | |
180 | } | |
e90c1d2a | 181 | #endif // __VMS__ |
c801d85f | 182 | |
34138703 | 183 | #ifdef __WINDOWS__ |
c801d85f KB |
184 | |
185 | #ifndef __GNUWIN32__ | |
469e1e5c | 186 | #ifndef __MWERKS__ |
c801d85f KB |
187 | #define strcasecmp stricmp |
188 | #define strncasecmp strnicmp | |
469e1e5c SC |
189 | #else |
190 | #define strcasecmp _stricmp | |
191 | #define strncasecmp _strnicmp | |
192 | #endif | |
c801d85f KB |
193 | #endif |
194 | ||
c801d85f | 195 | #else |
91b8de8d RR |
196 | |
197 | #ifdef __EMX__ | |
198 | #define strcasecmp stricmp | |
199 | #define strncasecmp strnicmp | |
200 | #endif | |
201 | ||
c801d85f KB |
202 | // This declaration is missing in SunOS! |
203 | // (Yes, I know it is NOT ANSI-C but its in BSD libc) | |
204 | #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__) | |
205 | extern "C" | |
206 | { | |
207 | int strcasecmp (const char *, const char *); | |
208 | int strncasecmp (const char *, const char *, size_t); | |
209 | } | |
210 | #endif | |
3f4a0c5b | 211 | #endif /* __WXMSW__ */ |
c801d85f | 212 | |
717b9bf2 DW |
213 | #ifdef __WXPM__ |
214 | #define strcasecmp stricmp | |
215 | #define strncasecmp strnicmp | |
216 | #endif | |
c801d85f | 217 | |
0080691b OK |
218 | wxChar * |
219 | copystring (const wxChar *s) | |
c801d85f | 220 | { |
223d09f6 | 221 | if (s == NULL) s = wxT(""); |
0080691b | 222 | size_t len = wxStrlen (s) + 1; |
c801d85f | 223 | |
0080691b OK |
224 | wxChar *news = new wxChar[len]; |
225 | memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest | |
c801d85f KB |
226 | |
227 | return news; | |
228 | } | |
229 | ||
230 | // Id generation | |
231 | static long wxCurrentId = 100; | |
232 | ||
3f4a0c5b | 233 | long |
c801d85f KB |
234 | wxNewId (void) |
235 | { | |
236 | return wxCurrentId++; | |
237 | } | |
238 | ||
239 | long | |
240 | wxGetCurrentId(void) { return wxCurrentId; } | |
241 | ||
3f4a0c5b | 242 | void |
c801d85f KB |
243 | wxRegisterId (long id) |
244 | { | |
245 | if (id >= wxCurrentId) | |
246 | wxCurrentId = id + 1; | |
247 | } | |
248 | ||
3f4a0c5b | 249 | void |
0080691b | 250 | StringToFloat (wxChar *s, float *number) |
c801d85f KB |
251 | { |
252 | if (s && *s && number) | |
0080691b | 253 | *number = (float) wxStrtod (s, (wxChar **) NULL); |
c801d85f KB |
254 | } |
255 | ||
3f4a0c5b | 256 | void |
0080691b | 257 | StringToDouble (wxChar *s, double *number) |
c801d85f KB |
258 | { |
259 | if (s && *s && number) | |
0080691b | 260 | *number = wxStrtod (s, (wxChar **) NULL); |
c801d85f KB |
261 | } |
262 | ||
0080691b OK |
263 | wxChar * |
264 | FloatToString (float number, const wxChar *fmt) | |
c801d85f | 265 | { |
0080691b | 266 | static wxChar buf[256]; |
c801d85f | 267 | |
0080691b | 268 | wxSprintf (buf, fmt, number); |
c801d85f KB |
269 | return buf; |
270 | } | |
271 | ||
0080691b OK |
272 | wxChar * |
273 | DoubleToString (double number, const wxChar *fmt) | |
c801d85f | 274 | { |
0080691b | 275 | static wxChar buf[256]; |
c801d85f | 276 | |
0080691b | 277 | wxSprintf (buf, fmt, number); |
c801d85f KB |
278 | return buf; |
279 | } | |
280 | ||
3f4a0c5b | 281 | void |
0080691b | 282 | StringToInt (wxChar *s, int *number) |
c801d85f KB |
283 | { |
284 | if (s && *s && number) | |
0080691b | 285 | *number = (int) wxStrtol (s, (wxChar **) NULL, 10); |
c801d85f KB |
286 | } |
287 | ||
3f4a0c5b | 288 | void |
0080691b | 289 | StringToLong (wxChar *s, long *number) |
c801d85f KB |
290 | { |
291 | if (s && *s && number) | |
0080691b | 292 | *number = wxStrtol (s, (wxChar **) NULL, 10); |
c801d85f KB |
293 | } |
294 | ||
84fff0b3 | 295 | wxChar * |
c801d85f KB |
296 | IntToString (int number) |
297 | { | |
84fff0b3 | 298 | static wxChar buf[20]; |
c801d85f | 299 | |
223d09f6 | 300 | wxSprintf (buf, wxT("%d"), number); |
c801d85f KB |
301 | return buf; |
302 | } | |
303 | ||
84fff0b3 | 304 | wxChar * |
c801d85f KB |
305 | LongToString (long number) |
306 | { | |
84fff0b3 | 307 | static wxChar buf[20]; |
c801d85f | 308 | |
223d09f6 | 309 | wxSprintf (buf, wxT("%ld"), number); |
c801d85f KB |
310 | return buf; |
311 | } | |
312 | ||
313 | // Array used in DecToHex conversion routine. | |
223d09f6 | 314 | static wxChar hexArray[] = wxT("0123456789ABCDEF"); |
c801d85f KB |
315 | |
316 | // Convert 2-digit hex number to decimal | |
fd71308f | 317 | int wxHexToDec(const wxString& buf) |
c801d85f KB |
318 | { |
319 | int firstDigit, secondDigit; | |
3f4a0c5b | 320 | |
223d09f6 KB |
321 | if (buf.GetChar(0) >= wxT('A')) |
322 | firstDigit = buf.GetChar(0) - wxT('A') + 10; | |
c801d85f | 323 | else |
223d09f6 | 324 | firstDigit = buf.GetChar(0) - wxT('0'); |
c801d85f | 325 | |
223d09f6 KB |
326 | if (buf.GetChar(1) >= wxT('A')) |
327 | secondDigit = buf.GetChar(1) - wxT('A') + 10; | |
c801d85f | 328 | else |
223d09f6 | 329 | secondDigit = buf.GetChar(1) - wxT('0'); |
3f4a0c5b | 330 | |
4b1f6faa | 331 | return (firstDigit & 0xF) * 16 + (secondDigit & 0xF ); |
c801d85f KB |
332 | } |
333 | ||
334 | // Convert decimal integer to 2-character hex string | |
84fff0b3 | 335 | void wxDecToHex(int dec, wxChar *buf) |
c801d85f KB |
336 | { |
337 | int firstDigit = (int)(dec/16.0); | |
338 | int secondDigit = (int)(dec - (firstDigit*16.0)); | |
339 | buf[0] = hexArray[firstDigit]; | |
340 | buf[1] = hexArray[secondDigit]; | |
341 | buf[2] = 0; | |
342 | } | |
343 | ||
fd71308f JS |
344 | // Convert decimal integer to 2-character hex string |
345 | wxString wxDecToHex(int dec) | |
346 | { | |
84fff0b3 | 347 | wxChar buf[3]; |
fd71308f JS |
348 | wxDecToHex(dec, buf); |
349 | return wxString(buf); | |
350 | } | |
351 | ||
c801d85f | 352 | // Match a string INDEPENDENT OF CASE |
3f4a0c5b | 353 | bool |
c801d85f KB |
354 | StringMatch (char *str1, char *str2, bool subString, bool exact) |
355 | { | |
356 | if (str1 == NULL || str2 == NULL) | |
357 | return FALSE; | |
358 | if (str1 == str2) | |
359 | return TRUE; | |
360 | ||
361 | if (subString) | |
362 | { | |
363 | int len1 = strlen (str1); | |
364 | int len2 = strlen (str2); | |
365 | int i; | |
366 | ||
367 | // Search for str1 in str2 | |
368 | // Slow .... but acceptable for short strings | |
369 | for (i = 0; i <= len2 - len1; i++) | |
3f4a0c5b VZ |
370 | { |
371 | if (strncasecmp (str1, str2 + i, len1) == 0) | |
372 | return TRUE; | |
373 | } | |
c801d85f KB |
374 | } |
375 | else if (exact) | |
376 | { | |
377 | if (strcasecmp (str1, str2) == 0) | |
3f4a0c5b | 378 | return TRUE; |
c801d85f KB |
379 | } |
380 | else | |
381 | { | |
382 | int len1 = strlen (str1); | |
383 | int len2 = strlen (str2); | |
384 | ||
385 | if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0) | |
3f4a0c5b | 386 | return TRUE; |
c801d85f KB |
387 | } |
388 | ||
389 | return FALSE; | |
390 | } | |
391 | ||
392 | // Return the current date/time | |
393 | // [volatile] | |
e90c1d2a | 394 | wxString wxNow() |
c801d85f | 395 | { |
c67daf87 | 396 | time_t now = time((time_t *) NULL); |
3f4a0c5b | 397 | char *date = ctime(&now); |
c801d85f KB |
398 | date[24] = '\0'; |
399 | return wxString(date); | |
400 | } | |
401 | ||
e90c1d2a VZ |
402 | #if wxUSE_GUI |
403 | ||
1e6feb95 VZ |
404 | #if wxUSE_MENUS |
405 | ||
e90c1d2a | 406 | // ---------------------------------------------------------------------------- |
974e8d94 | 407 | // Menu accelerators related functions |
e90c1d2a | 408 | // ---------------------------------------------------------------------------- |
c801d85f | 409 | |
1e6feb95 | 410 | wxChar *wxStripMenuCodes(wxChar *in, wxChar *out) |
c801d85f | 411 | { |
1e6feb95 VZ |
412 | wxString s = wxMenuItem::GetLabelFromText(in); |
413 | if ( out ) | |
c801d85f | 414 | { |
1e6feb95 VZ |
415 | // go smash their buffer if it's not big enough - I love char * params |
416 | memcpy(out, s.c_str(), s.length() * sizeof(wxChar)); | |
417 | } | |
418 | else | |
419 | { | |
420 | out = copystring(s); | |
421 | } | |
c801d85f | 422 | |
1e6feb95 | 423 | return out; |
c801d85f KB |
424 | } |
425 | ||
1e6feb95 | 426 | wxString wxStripMenuCodes(const wxString& in) |
47bc1060 | 427 | { |
1e6feb95 | 428 | wxString out; |
c801d85f | 429 | |
1e6feb95 VZ |
430 | size_t len = in.length(); |
431 | out.reserve(len); | |
974e8d94 | 432 | |
1e6feb95 VZ |
433 | for ( size_t n = 0; n < len; n++ ) |
434 | { | |
435 | wxChar ch = in[n]; | |
436 | if ( ch == _T('&') ) | |
437 | { | |
438 | // skip it, it is used to introduce the accel char (or to quote | |
439 | // itself in which case it should still be skipped): note that it | |
440 | // can't be the last character of the string | |
441 | if ( ++n == len ) | |
442 | { | |
443 | wxLogDebug(_T("Invalid menu string '%s'"), in.c_str()); | |
974e8d94 | 444 | } |
1e6feb95 VZ |
445 | else |
446 | { | |
447 | // use the next char instead | |
448 | ch = in[n]; | |
974e8d94 VZ |
449 | } |
450 | } | |
1e6feb95 VZ |
451 | else if ( ch == _T('\t') ) |
452 | { | |
453 | // everything after TAB is accel string, exit the loop | |
454 | break; | |
974e8d94 VZ |
455 | } |
456 | ||
1e6feb95 | 457 | out += ch; |
974e8d94 VZ |
458 | } |
459 | ||
1e6feb95 | 460 | return out; |
974e8d94 VZ |
461 | } |
462 | ||
1e6feb95 | 463 | #endif // wxUSE_MENUS |
974e8d94 | 464 | |
e90c1d2a VZ |
465 | // ---------------------------------------------------------------------------- |
466 | // Window search functions | |
467 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
468 | |
469 | /* | |
470 | * If parent is non-NULL, look through children for a label or title | |
471 | * matching the specified string. If NULL, look through all top-level windows. | |
472 | * | |
473 | */ | |
474 | ||
c801d85f KB |
475 | wxWindow * |
476 | wxFindWindowByLabel (const wxString& title, wxWindow * parent) | |
477 | { | |
e146b8c8 | 478 | if (parent) |
c801d85f | 479 | { |
e146b8c8 | 480 | return wxFindWindowByLabel1(title, parent); |
c801d85f | 481 | } |
e146b8c8 | 482 | else |
c801d85f | 483 | { |
e146b8c8 VZ |
484 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); |
485 | node; | |
486 | node = node->GetNext() ) | |
3f4a0c5b | 487 | { |
e146b8c8 VZ |
488 | wxWindow *win = node->GetData(); |
489 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
490 | if (retwin) | |
491 | return retwin; | |
3f4a0c5b | 492 | } // for() |
c801d85f KB |
493 | |
494 | } | |
e146b8c8 | 495 | return (wxWindow *) NULL; |
c801d85f KB |
496 | } |
497 | ||
498 | // Recursive | |
499 | static wxWindow * | |
500 | wxFindWindowByLabel1 (const wxString& title, wxWindow * parent) | |
501 | { | |
e146b8c8 | 502 | if (parent) |
c801d85f | 503 | { |
e146b8c8 VZ |
504 | if (parent->GetLabel() == title) |
505 | return parent; | |
c801d85f KB |
506 | } |
507 | ||
e146b8c8 | 508 | if (parent) |
c801d85f | 509 | { |
f03fc89f | 510 | for ( wxWindowList::Node * node = parent->GetChildren().GetFirst(); |
e146b8c8 VZ |
511 | node; |
512 | node = node->GetNext() ) | |
3f4a0c5b | 513 | { |
e146b8c8 VZ |
514 | wxWindow *win = (wxWindow *)node->GetData(); |
515 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
516 | if (retwin) | |
517 | return retwin; | |
518 | } | |
c801d85f KB |
519 | |
520 | } | |
521 | ||
e146b8c8 | 522 | return (wxWindow *) NULL; // Not found |
c801d85f KB |
523 | } |
524 | ||
525 | /* | |
526 | * If parent is non-NULL, look through children for a name | |
527 | * matching the specified string. If NULL, look through all top-level windows. | |
528 | * | |
529 | */ | |
530 | ||
c801d85f KB |
531 | wxWindow * |
532 | wxFindWindowByName (const wxString& title, wxWindow * parent) | |
533 | { | |
e146b8c8 | 534 | if (parent) |
c801d85f | 535 | { |
e146b8c8 | 536 | return wxFindWindowByName1 (title, parent); |
c801d85f | 537 | } |
e146b8c8 | 538 | else |
c801d85f | 539 | { |
e146b8c8 VZ |
540 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); |
541 | node; | |
542 | node = node->GetNext() ) | |
3f4a0c5b | 543 | { |
e146b8c8 VZ |
544 | wxWindow *win = node->GetData(); |
545 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
546 | if (retwin) | |
547 | return retwin; | |
548 | } | |
c801d85f KB |
549 | |
550 | } | |
e146b8c8 VZ |
551 | |
552 | // Failed? Try by label instead. | |
553 | return wxFindWindowByLabel(title, parent); | |
c801d85f KB |
554 | } |
555 | ||
556 | // Recursive | |
557 | static wxWindow * | |
558 | wxFindWindowByName1 (const wxString& title, wxWindow * parent) | |
559 | { | |
560 | if (parent) | |
561 | { | |
3f4a0c5b VZ |
562 | if ( parent->GetName() == title ) |
563 | return parent; | |
c801d85f KB |
564 | } |
565 | ||
566 | if (parent) | |
567 | { | |
c0ed460c | 568 | for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ()) |
3f4a0c5b VZ |
569 | { |
570 | wxWindow *win = (wxWindow *) node->Data (); | |
571 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
572 | if (retwin) | |
573 | return retwin; | |
574 | } // for() | |
c801d85f KB |
575 | |
576 | } | |
577 | ||
3f4a0c5b | 578 | return (wxWindow *) NULL; // Not found |
c801d85f KB |
579 | |
580 | } | |
581 | ||
582 | // Returns menu item id or -1 if none. | |
3f4a0c5b | 583 | int |
c801d85f KB |
584 | wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString) |
585 | { | |
1e6feb95 | 586 | #if wxUSE_MENUS |
c801d85f | 587 | wxMenuBar *menuBar = frame->GetMenuBar (); |
1e6feb95 VZ |
588 | if ( menuBar ) |
589 | return menuBar->FindMenuItem (menuString, itemString); | |
590 | #endif // wxUSE_MENUS | |
591 | ||
592 | return -1; | |
59a12e90 JS |
593 | } |
594 | ||
d1c8aaa3 JS |
595 | // Try to find the deepest child that contains 'pt'. |
596 | // We go backwards, to try to allow for controls that are spacially | |
597 | // within other controls, but are still siblings (e.g. buttons within | |
598 | // static boxes). Static boxes are likely to be created _before_ controls | |
599 | // that sit inside them. | |
59a12e90 JS |
600 | wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt) |
601 | { | |
d1c8aaa3 JS |
602 | if (!win->IsShown()) |
603 | return NULL; | |
604 | ||
605 | // Hack for wxNotebook case: at least in wxGTK, all pages | |
606 | // claim to be shown, so we must only deal with the selected one. | |
607 | if (win->IsKindOf(CLASSINFO(wxNotebook))) | |
608 | { | |
609 | wxNotebook* nb = (wxNotebook*) win; | |
610 | int sel = nb->GetSelection(); | |
611 | if (sel >= 0) | |
612 | { | |
613 | wxWindow* child = nb->GetPage(sel); | |
614 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); | |
615 | if (foundWin) | |
616 | return foundWin; | |
617 | } | |
618 | } | |
619 | /* Doesn't work | |
620 | // Frame case | |
621 | else if (win->IsKindOf(CLASSINFO(wxFrame))) | |
622 | { | |
623 | // Pseudo-children that may not be mentioned in the child list | |
624 | wxWindowList extraChildren; | |
625 | wxFrame* frame = (wxFrame*) win; | |
626 | if (frame->GetStatusBar()) | |
627 | extraChildren.Append(frame->GetStatusBar()); | |
628 | if (frame->GetToolBar()) | |
629 | extraChildren.Append(frame->GetToolBar()); | |
630 | ||
631 | wxNode* node = extraChildren.First(); | |
632 | while (node) | |
633 | { | |
634 | wxWindow* child = (wxWindow*) node->Data(); | |
635 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); | |
636 | if (foundWin) | |
637 | return foundWin; | |
638 | node = node->Next(); | |
639 | } | |
640 | } | |
641 | */ | |
642 | ||
643 | wxNode* node = win->GetChildren().Last(); | |
59a12e90 JS |
644 | while (node) |
645 | { | |
646 | wxWindow* child = (wxWindow*) node->Data(); | |
647 | wxWindow* foundWin = wxFindWindowAtPoint(child, pt); | |
648 | if (foundWin) | |
d1c8aaa3 JS |
649 | return foundWin; |
650 | node = node->Previous(); | |
59a12e90 JS |
651 | } |
652 | ||
653 | wxPoint pos = win->GetPosition(); | |
654 | wxSize sz = win->GetSize(); | |
655 | if (win->GetParent()) | |
656 | { | |
657 | pos = win->GetParent()->ClientToScreen(pos); | |
658 | } | |
659 | ||
660 | wxRect rect(pos, sz); | |
661 | if (rect.Inside(pt)) | |
662 | return win; | |
663 | else | |
664 | return NULL; | |
665 | } | |
666 | ||
57591e0e | 667 | wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt) |
59a12e90 | 668 | { |
57591e0e JS |
669 | // Go backwards through the list since windows |
670 | // on top are likely to have been appended most | |
671 | // recently. | |
672 | wxNode* node = wxTopLevelWindows.Last(); | |
59a12e90 JS |
673 | while (node) |
674 | { | |
675 | wxWindow* win = (wxWindow*) node->Data(); | |
676 | wxWindow* found = wxFindWindowAtPoint(win, pt); | |
677 | if (found) | |
678 | return found; | |
57591e0e | 679 | node = node->Previous(); |
59a12e90 JS |
680 | } |
681 | return NULL; | |
c801d85f KB |
682 | } |
683 | ||
e90c1d2a VZ |
684 | #endif // wxUSE_GUI |
685 | ||
c801d85f KB |
686 | /* |
687 | On Fri, 21 Jul 1995, Paul Craven wrote: | |
688 | ||
689 | > Is there a way to find the path of running program's executable? I can get | |
690 | > my home directory, and the current directory, but I don't know how to get the | |
691 | > executable directory. | |
3f4a0c5b | 692 | > |
c801d85f KB |
693 | |
694 | The code below (warty as it is), does what you want on most Unix, | |
695 | DOS, and Mac platforms (it's from the ALS Prolog main). | |
696 | ||
3f4a0c5b | 697 | || Ken Bowen Applied Logic Systems, Inc. PO Box 180, |
c801d85f KB |
698 | ||==== Voice: +1 (617)965-9191 Newton Centre, |
699 | || FAX: +1 (617)965-1636 MA 02159 USA | |
700 | Email: ken@als.com WWW: http://www.als.com | |
701 | ------------------------------------------------------------------------ | |
702 | */ | |
703 | ||
704 | // This code is commented out but it may be integrated with wxWin at | |
705 | // a later date, after testing. Thanks Ken! | |
706 | #if 0 | |
707 | ||
708 | /*--------------------------------------------------------------------* | |
709 | | whereami is given a filename f in the form: whereami(argv[0]) | |
3f4a0c5b VZ |
710 | | It returns the directory in which the executable file (containing |
711 | | this code [main.c] ) may be found. A dot will be returned to indicate | |
c801d85f KB |
712 | | the current directory. |
713 | *--------------------------------------------------------------------*/ | |
714 | ||
715 | static void | |
716 | whereami(name) | |
717 | char *name; | |
718 | { | |
3f4a0c5b | 719 | register char *cutoff = NULL; /* stifle -Wall */ |
c801d85f KB |
720 | register char *s; |
721 | register char *t; | |
722 | int cc; | |
723 | char ebuf[4096]; | |
724 | ||
725 | /* | |
726 | * See if the file is accessible either through the current directory | |
727 | * or through an absolute path. | |
728 | */ | |
729 | ||
730 | if (access(name, R_OK) == 0) { | |
731 | ||
3f4a0c5b VZ |
732 | /*-------------------------------------------------------------* |
733 | * The file was accessible without any other work. But the current | |
734 | * working directory might change on us, so if it was accessible | |
735 | * through the cwd, then we should get it for later accesses. | |
736 | *-------------------------------------------------------------*/ | |
c801d85f | 737 | |
3f4a0c5b VZ |
738 | t = imagedir; |
739 | if (!absolute_pathname(name)) { | |
c801d85f | 740 | #if defined(DOS) || defined(__WIN32__) |
3f4a0c5b VZ |
741 | int drive; |
742 | char *newrbuf; | |
c801d85f | 743 | |
3f4a0c5b | 744 | newrbuf = imagedir; |
c801d85f | 745 | #ifndef __DJGPP__ |
3f4a0c5b VZ |
746 | if (*(name + 1) == ':') { |
747 | if (*name >= 'a' && *name <= 'z') | |
748 | drive = (int) (*name - 'a' + 1); | |
749 | else | |
750 | drive = (int) (*name - 'A' + 1); | |
751 | *newrbuf++ = *name; | |
752 | *newrbuf++ = *(name + 1); | |
753 | *newrbuf++ = DIR_SEPARATOR; | |
754 | } | |
755 | else { | |
756 | drive = 0; | |
757 | *newrbuf++ = DIR_SEPARATOR; | |
758 | } | |
759 | if (getcwd(newrbuf, drive) == 0) { /* } */ | |
c801d85f | 760 | #else |
3f4a0c5b | 761 | if (getcwd(newrbuf, 1024) == 0) { /* } */ |
c801d85f KB |
762 | #endif |
763 | #else /* DOS */ | |
764 | #ifdef HAVE_GETWD | |
3f4a0c5b | 765 | if (getwd(imagedir) == 0) { /* } */ |
c801d85f | 766 | #else /* !HAVE_GETWD */ |
3f4a0c5b | 767 | if (getcwd(imagedir, 1024) == 0) { |
c801d85f KB |
768 | #endif /* !HAVE_GETWD */ |
769 | #endif /* DOS */ | |
3f4a0c5b VZ |
770 | fatal_error(FE_GETCWD, 0); |
771 | } | |
772 | for (; *t; t++) /* Set t to end of buffer */ | |
773 | ; | |
774 | if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already | |
775 | * last char | |
776 | */ | |
777 | cutoff = t - 1; | |
778 | else { | |
779 | cutoff = t; /* otherwise put one in */ | |
780 | *t++ = DIR_SEPARATOR; | |
781 | } | |
782 | } | |
c801d85f | 783 | #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__)) |
3f4a0c5b VZ |
784 | else |
785 | (*t++ = DIR_SEPARATOR); | |
c801d85f KB |
786 | #endif |
787 | ||
3f4a0c5b VZ |
788 | /*-------------------------------------------------------------* |
789 | * Copy the rest of the string and set the cutoff if it was not | |
790 | * already set. If the first character of name is a slash, cutoff | |
791 | * is not presently set but will be on the first iteration of the | |
792 | * loop below. | |
793 | *-------------------------------------------------------------*/ | |
c801d85f | 794 | |
3f4a0c5b VZ |
795 | for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) { |
796 | if (*s == DIR_SEPARATOR) | |
797 | cutoff = t; | |
798 | if (!(*t++ = *s++)) | |
799 | break; | |
800 | } | |
c801d85f KB |
801 | |
802 | } | |
803 | else { | |
804 | ||
3f4a0c5b VZ |
805 | /*-------------------------------------------------------------* |
806 | * Get the path list from the environment. If the path list is | |
807 | * inaccessible for any reason, leave with fatal error. | |
808 | *-------------------------------------------------------------*/ | |
c801d85f KB |
809 | |
810 | #ifdef __MAC__ | |
3f4a0c5b | 811 | if ((s = getenv("Commands")) == (char *) 0) |
c801d85f | 812 | #else |
3f4a0c5b | 813 | if ((s = getenv("PATH")) == (char *) 0) |
c801d85f | 814 | #endif |
3f4a0c5b VZ |
815 | fatal_error(FE_PATH, 0); |
816 | ||
817 | /* | |
818 | * Copy path list into ebuf and set the source pointer to the | |
819 | * beginning of this buffer. | |
820 | */ | |
821 | ||
822 | strcpy(ebuf, s); | |
823 | s = ebuf; | |
824 | ||
825 | for (;;) { | |
826 | t = imagedir; | |
827 | while (*s && *s != PATH_SEPARATOR) | |
828 | *t++ = *s++; | |
829 | if (t > imagedir && *(t - 1) == DIR_SEPARATOR) | |
830 | ; /* do nothing -- slash already is in place */ | |
831 | else | |
832 | *t++ = DIR_SEPARATOR; /* put in the slash */ | |
833 | cutoff = t - 1; /* set cutoff */ | |
834 | strcpy(t, name); | |
835 | if (access(imagedir, R_OK) == 0) | |
836 | break; | |
837 | ||
838 | if (*s) | |
839 | s++; /* advance source pointer */ | |
840 | else | |
841 | fatal_error(FE_INFND, 0); | |
842 | } | |
c801d85f KB |
843 | |
844 | } | |
845 | ||
846 | /*-------------------------------------------------------------* | |
847 | | At this point the full pathname should exist in imagedir and | |
848 | | cutoff should be set to the final slash. We must now determine | |
849 | | whether the file name is a symbolic link or not and chase it down | |
850 | | if it is. Note that we reuse ebuf for getting the link. | |
851 | *-------------------------------------------------------------*/ | |
852 | ||
853 | #ifdef HAVE_SYMLINK | |
854 | while ((cc = readlink(imagedir, ebuf, 512)) != -1) { | |
3f4a0c5b VZ |
855 | ebuf[cc] = 0; |
856 | s = ebuf; | |
857 | if (*s == DIR_SEPARATOR) { | |
858 | t = imagedir; | |
859 | } | |
860 | else { | |
861 | t = cutoff + 1; | |
862 | } | |
863 | for (;;) { | |
864 | if (*s == DIR_SEPARATOR) | |
865 | cutoff = t; /* mark the last slash seen */ | |
866 | if (!(*t++ = *s++)) /* copy the character */ | |
867 | break; | |
868 | } | |
c801d85f KB |
869 | } |
870 | ||
871 | #endif /* HAVE_SYMLINK */ | |
872 | ||
3f4a0c5b VZ |
873 | strcpy(imagename, cutoff + 1); /* keep the image name */ |
874 | *(cutoff + 1) = 0; /* chop off the filename part */ | |
c801d85f KB |
875 | } |
876 | ||
877 | #endif | |
ead7ce10 | 878 | |
e90c1d2a VZ |
879 | #if wxUSE_GUI |
880 | ||
881 | // ---------------------------------------------------------------------------- | |
882 | // GUI helpers | |
883 | // ---------------------------------------------------------------------------- | |
ead7ce10 | 884 | |
dfad0599 JS |
885 | /* |
886 | * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp | |
887 | * since otherwise the generic code may be pulled in unnecessarily. | |
888 | */ | |
889 | ||
1e6feb95 VZ |
890 | #if wxUSE_MSGDLG |
891 | ||
dfad0599 JS |
892 | int wxMessageBox(const wxString& message, const wxString& caption, long style, |
893 | wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) ) | |
894 | { | |
895 | wxMessageDialog dialog(parent, message, caption, style); | |
896 | ||
897 | int ans = dialog.ShowModal(); | |
898 | switch ( ans ) | |
899 | { | |
900 | case wxID_OK: | |
901 | return wxOK; | |
dfad0599 JS |
902 | case wxID_YES: |
903 | return wxYES; | |
dfad0599 JS |
904 | case wxID_NO: |
905 | return wxNO; | |
dfad0599 JS |
906 | case wxID_CANCEL: |
907 | return wxCANCEL; | |
dfad0599 | 908 | } |
3ca6a5f0 BP |
909 | |
910 | wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") ); | |
911 | ||
912 | return wxCANCEL; | |
dfad0599 JS |
913 | } |
914 | ||
1e6feb95 VZ |
915 | #endif // wxUSE_MSGDLG |
916 | ||
88ac883a | 917 | #if wxUSE_TEXTDLG |
1e6feb95 | 918 | |
dfad0599 JS |
919 | wxString wxGetTextFromUser(const wxString& message, const wxString& caption, |
920 | const wxString& defaultValue, wxWindow *parent, | |
921 | int x, int y, bool WXUNUSED(centre) ) | |
922 | { | |
d2f50933 | 923 | wxString str; |
dfad0599 JS |
924 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y)); |
925 | if (dialog.ShowModal() == wxID_OK) | |
d2f50933 VZ |
926 | { |
927 | str = dialog.GetValue(); | |
928 | } | |
929 | ||
930 | return str; | |
dfad0599 | 931 | } |
d2f50933 VZ |
932 | |
933 | wxString wxGetPasswordFromUser(const wxString& message, | |
934 | const wxString& caption, | |
935 | const wxString& defaultValue, | |
936 | wxWindow *parent) | |
937 | { | |
938 | wxString str; | |
939 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, | |
940 | wxOK | wxCANCEL | wxTE_PASSWORD); | |
941 | if ( dialog.ShowModal() == wxID_OK ) | |
942 | { | |
943 | str = dialog.GetValue(); | |
944 | } | |
945 | ||
946 | return str; | |
947 | } | |
948 | ||
88ac883a | 949 | #endif // wxUSE_TEXTDLG |
dfad0599 | 950 | |
1e6feb95 VZ |
951 | #if wxUSE_COLOURDLG |
952 | ||
91b4c08d VZ |
953 | wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit) |
954 | { | |
955 | wxColourData data; | |
956 | data.SetChooseFull(TRUE); | |
957 | if ( colInit.Ok() ) | |
958 | { | |
959 | data.SetColour((wxColour &)colInit); // const_cast | |
960 | } | |
961 | ||
962 | wxColour colRet; | |
963 | wxColourDialog dialog(parent, &data); | |
964 | if ( dialog.ShowModal() == wxID_OK ) | |
965 | { | |
966 | colRet = dialog.GetColourData().GetColour(); | |
967 | } | |
968 | //else: leave it invalid | |
969 | ||
970 | return colRet; | |
971 | } | |
972 | ||
1e6feb95 VZ |
973 | #endif // wxUSE_COLOURDLG |
974 | ||
91b4c08d VZ |
975 | // ---------------------------------------------------------------------------- |
976 | // missing C RTL functions (FIXME shouldn't be here at all) | |
977 | // ---------------------------------------------------------------------------- | |
978 | ||
469e1e5c | 979 | #ifdef __MWERKS__ |
3f4a0c5b | 980 | char *strdup(const char *s) |
469e1e5c | 981 | { |
3f4a0c5b | 982 | return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ; |
469e1e5c SC |
983 | } |
984 | ||
3f4a0c5b | 985 | int isascii( int c ) |
469e1e5c | 986 | { |
3f4a0c5b | 987 | return ( c >= 0 && c < 128 ) ; |
469e1e5c | 988 | } |
e90c1d2a VZ |
989 | #endif // __MWERKS__ |
990 | ||
991 | // ---------------------------------------------------------------------------- | |
cbc66a27 | 992 | // wxSafeYield and supporting functions |
e90c1d2a VZ |
993 | // ---------------------------------------------------------------------------- |
994 | ||
995 | void wxEnableTopLevelWindows(bool enable) | |
996 | { | |
225fe9d6 VZ |
997 | wxWindowList::Node *node; |
998 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
999 | node->GetData()->Enable(enable); | |
e90c1d2a VZ |
1000 | } |
1001 | ||
cd6ce4a9 | 1002 | wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip) |
e90c1d2a | 1003 | { |
79f585d9 VZ |
1004 | // remember the top level windows which were already disabled, so that we |
1005 | // don't reenable them later | |
1006 | m_winDisabled = NULL; | |
1007 | ||
225fe9d6 VZ |
1008 | wxWindowList::Node *node; |
1009 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
1010 | { | |
1011 | wxWindow *winTop = node->GetData(); | |
79f585d9 VZ |
1012 | if ( winTop == winToSkip ) |
1013 | continue; | |
1014 | ||
1015 | if ( winTop->IsEnabled() ) | |
1016 | { | |
1017 | winTop->Disable(); | |
1018 | } | |
1019 | else | |
cd6ce4a9 | 1020 | { |
79f585d9 VZ |
1021 | if ( !m_winDisabled ) |
1022 | { | |
1023 | m_winDisabled = new wxWindowList; | |
1024 | } | |
225fe9d6 | 1025 | |
cd6ce4a9 | 1026 | m_winDisabled->Append(winTop); |
cd6ce4a9 | 1027 | } |
225fe9d6 | 1028 | } |
cd6ce4a9 | 1029 | } |
225fe9d6 | 1030 | |
cd6ce4a9 VZ |
1031 | wxWindowDisabler::~wxWindowDisabler() |
1032 | { | |
1033 | wxWindowList::Node *node; | |
79f585d9 | 1034 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) |
225fe9d6 | 1035 | { |
79f585d9 VZ |
1036 | wxWindow *winTop = node->GetData(); |
1037 | if ( !m_winDisabled || !m_winDisabled->Find(winTop) ) | |
1038 | { | |
1039 | winTop->Enable(); | |
1040 | } | |
1041 | //else: had been already disabled, don't reenable | |
225fe9d6 VZ |
1042 | } |
1043 | ||
cd6ce4a9 VZ |
1044 | delete m_winDisabled; |
1045 | } | |
1046 | ||
1047 | // Yield to other apps/messages and disable user input to all windows except | |
1048 | // the given one | |
1049 | bool wxSafeYield(wxWindow *win) | |
1050 | { | |
bc1dcfc1 | 1051 | wxWindowDisabler wd(win); |
cd6ce4a9 VZ |
1052 | |
1053 | bool rc = wxYield(); | |
1054 | ||
225fe9d6 | 1055 | return rc; |
e90c1d2a VZ |
1056 | } |
1057 | ||
cbc66a27 VZ |
1058 | // ---------------------------------------------------------------------------- |
1059 | // misc functions | |
1060 | // ---------------------------------------------------------------------------- | |
1061 | ||
e90c1d2a VZ |
1062 | // Don't synthesize KeyUp events holding down a key and producing KeyDown |
1063 | // events with autorepeat. On by default and always on in wxMSW. wxGTK version | |
1064 | // in utilsgtk.cpp. | |
1065 | #ifndef __WXGTK__ | |
1066 | bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) | |
1067 | { | |
225fe9d6 | 1068 | return TRUE; // detectable auto-repeat is the only mode MSW supports |
e90c1d2a VZ |
1069 | } |
1070 | #endif // !wxGTK | |
1071 | ||
1072 | #endif // wxUSE_GUI | |
e2a6f233 | 1073 | |
0fb67cd1 VZ |
1074 | // ---------------------------------------------------------------------------- |
1075 | // network and user id functions | |
1076 | // ---------------------------------------------------------------------------- | |
1077 | ||
1078 | // Get Full RFC822 style email address | |
84fff0b3 | 1079 | bool wxGetEmailAddress(wxChar *address, int maxSize) |
e2a6f233 | 1080 | { |
0fb67cd1 VZ |
1081 | wxString email = wxGetEmailAddress(); |
1082 | if ( !email ) | |
e2a6f233 | 1083 | return FALSE; |
0fb67cd1 | 1084 | |
84fff0b3 | 1085 | wxStrncpy(address, email, maxSize - 1); |
223d09f6 | 1086 | address[maxSize - 1] = wxT('\0'); |
0fb67cd1 VZ |
1087 | |
1088 | return TRUE; | |
e2a6f233 JS |
1089 | } |
1090 | ||
0fb67cd1 | 1091 | wxString wxGetEmailAddress() |
e2a6f233 | 1092 | { |
0fb67cd1 VZ |
1093 | wxString email; |
1094 | ||
1f0500b3 | 1095 | wxString host = wxGetFullHostName(); |
0fb67cd1 | 1096 | if ( !!host ) |
e2a6f233 | 1097 | { |
0fb67cd1 VZ |
1098 | wxString user = wxGetUserId(); |
1099 | if ( !!user ) | |
1100 | { | |
1f0500b3 | 1101 | email << user << wxT('@') << host; |
0fb67cd1 | 1102 | } |
e2a6f233 | 1103 | } |
0fb67cd1 VZ |
1104 | |
1105 | return email; | |
1106 | } | |
1107 | ||
1108 | wxString wxGetUserId() | |
1109 | { | |
1110 | static const int maxLoginLen = 256; // FIXME arbitrary number | |
1111 | ||
1112 | wxString buf; | |
1113 | bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen); | |
1114 | buf.UngetWriteBuf(); | |
1115 | ||
1116 | if ( !ok ) | |
1117 | buf.Empty(); | |
1118 | ||
1119 | return buf; | |
1120 | } | |
1121 | ||
1122 | wxString wxGetUserName() | |
1123 | { | |
1124 | static const int maxUserNameLen = 1024; // FIXME arbitrary number | |
1125 | ||
1126 | wxString buf; | |
1127 | bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen); | |
1128 | buf.UngetWriteBuf(); | |
1129 | ||
1130 | if ( !ok ) | |
1131 | buf.Empty(); | |
1132 | ||
1133 | return buf; | |
e2a6f233 JS |
1134 | } |
1135 | ||
0fb67cd1 | 1136 | wxString wxGetHostName() |
518b5d2f VZ |
1137 | { |
1138 | static const size_t hostnameSize = 257; | |
0fb67cd1 VZ |
1139 | |
1140 | wxString buf; | |
518b5d2f VZ |
1141 | bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); |
1142 | ||
1143 | buf.UngetWriteBuf(); | |
1144 | ||
0fb67cd1 VZ |
1145 | if ( !ok ) |
1146 | buf.Empty(); | |
1147 | ||
1148 | return buf; | |
518b5d2f VZ |
1149 | } |
1150 | ||
96c5bd7f KB |
1151 | wxString wxGetFullHostName() |
1152 | { | |
1153 | static const size_t hostnameSize = 257; | |
1154 | ||
1155 | wxString buf; | |
1156 | bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); | |
1157 | ||
1158 | buf.UngetWriteBuf(); | |
1159 | ||
1160 | if ( !ok ) | |
1161 | buf.Empty(); | |
1162 | ||
1163 | return buf; | |
1164 | } | |
1165 | ||
c51deffc VZ |
1166 | wxString wxGetHomeDir() |
1167 | { | |
1168 | wxString home; | |
1169 | wxGetHomeDir(&home); | |
1170 | ||
1171 | return home; | |
1172 | } | |
bc385ba9 VZ |
1173 | |
1174 | #if 0 | |
1175 | ||
1176 | wxString wxGetCurrentDir() | |
1177 | { | |
1178 | wxString dir; | |
1179 | size_t len = 1024; | |
1180 | bool ok; | |
1181 | do | |
1182 | { | |
1183 | ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL; | |
1184 | dir.UngetWriteBuf(); | |
1185 | ||
1186 | if ( !ok ) | |
1187 | { | |
1188 | if ( errno != ERANGE ) | |
1189 | { | |
1190 | wxLogSysError(_T("Failed to get current directory")); | |
1191 | ||
1192 | return wxEmptyString; | |
1193 | } | |
1194 | else | |
1195 | { | |
1196 | // buffer was too small, retry with a larger one | |
1197 | len *= 2; | |
1198 | } | |
1199 | } | |
1200 | //else: ok | |
1201 | } while ( !ok ); | |
1202 | ||
1203 | return dir; | |
1204 | } | |
1205 | ||
1206 | #endif // 0 | |
cd6ce4a9 VZ |
1207 | |
1208 | // ---------------------------------------------------------------------------- | |
1209 | // wxExecute | |
1210 | // ---------------------------------------------------------------------------- | |
1211 | ||
f6bcfd97 BP |
1212 | // this is a private function because it hasn't a clean interface: the first |
1213 | // array is passed by reference, the second by pointer - instead we have 2 | |
1214 | // public versions of wxExecute() below | |
1215 | static long wxDoExecuteWithCapture(const wxString& command, | |
1216 | wxArrayString& output, | |
1217 | wxArrayString* error) | |
cd6ce4a9 | 1218 | { |
669f7a11 JS |
1219 | #ifdef __WIN16__ |
1220 | wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16."); | |
f6bcfd97 | 1221 | |
669f7a11 | 1222 | return 0; |
f6bcfd97 | 1223 | #else // !Win16 |
cd6ce4a9 VZ |
1224 | // create a wxProcess which will capture the output |
1225 | wxProcess *process = new wxProcess; | |
1226 | process->Redirect(); | |
1227 | ||
1228 | long rc = wxExecute(command, TRUE /* sync */, process); | |
f6bcfd97 BP |
1229 | |
1230 | #if wxUSE_STREAMS | |
cd6ce4a9 VZ |
1231 | if ( rc != -1 ) |
1232 | { | |
f6bcfd97 BP |
1233 | wxInputStream* is = process->GetInputStream(); |
1234 | wxCHECK_MSG( is, -1, _T("if wxExecute() succeded, stream can't be NULL") ); | |
1235 | wxTextInputStream tis(*is); | |
1236 | ||
1237 | wxTextInputStream *tes = NULL; | |
1238 | wxInputStream *es = NULL; | |
1239 | if ( error ) | |
cd6ce4a9 | 1240 | { |
f6bcfd97 BP |
1241 | es = process->GetErrorStream(); |
1242 | ||
1243 | wxCHECK_MSG( es, -1, _T("stderr can't be NULL") ); | |
1244 | ||
1245 | tes = new wxTextInputStream(*es); | |
1246 | } | |
1247 | ||
1248 | bool cont; | |
1249 | do | |
1250 | { | |
1251 | cont = FALSE; | |
1252 | ||
1253 | if ( !is->Eof() && is->IsOk() ) | |
1254 | { | |
1255 | wxString line = tis.ReadLine(); | |
1256 | if ( is->LastError() ) | |
1257 | break; | |
1258 | ||
1259 | cont = TRUE; | |
1260 | ||
1261 | output.Add(line); | |
1262 | } | |
1263 | ||
1264 | if ( error && !es->Eof() && es->IsOk() ) | |
1265 | { | |
1266 | wxString line = tes->ReadLine(); | |
1267 | if ( es->LastError() ) | |
1268 | break; | |
1269 | ||
1270 | cont = TRUE; | |
cd6ce4a9 | 1271 | |
f6bcfd97 BP |
1272 | error->Add(line); |
1273 | } | |
cd6ce4a9 | 1274 | } |
f6bcfd97 BP |
1275 | while ( cont ); |
1276 | ||
1277 | delete tes; | |
cd6ce4a9 | 1278 | } |
f6bcfd97 BP |
1279 | #endif // wxUSE_STREAMS |
1280 | ||
1281 | delete process; | |
cd6ce4a9 VZ |
1282 | |
1283 | return rc; | |
f6bcfd97 BP |
1284 | #endif // IO redirection supoprted |
1285 | } | |
1286 | ||
1287 | long wxExecute(const wxString& command, wxArrayString& output) | |
1288 | { | |
1289 | return wxDoExecuteWithCapture(command, output, NULL); | |
1290 | } | |
1291 | ||
1292 | long wxExecute(const wxString& command, | |
1293 | wxArrayString& output, | |
1294 | wxArrayString& error) | |
1295 | { | |
1296 | return wxDoExecuteWithCapture(command, output, &error); | |
cd6ce4a9 | 1297 | } |