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