]>
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 | ||
36 | #if wxUSE_GUI | |
37 | #include "wx/window.h" | |
38 | #include "wx/menu.h" | |
39 | #include "wx/frame.h" | |
40 | #include "wx/msgdlg.h" | |
41 | #include "wx/textdlg.h" | |
42 | #endif // wxUSE_GUI | |
43 | #endif // WX_PRECOMP | |
44 | ||
45 | #include <ctype.h> | |
46 | #include <stdio.h> | |
47 | #include <stdlib.h> | |
48 | #include <string.h> | |
49 | ||
50 | #if !defined(__WATCOMC__) | |
51 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) | |
52 | #include <errno.h> | |
53 | #endif | |
54 | #endif | |
55 | ||
56 | #include <time.h> | |
57 | ||
58 | #ifndef __MWERKS__ | |
59 | #include <sys/types.h> | |
60 | #include <sys/stat.h> | |
61 | #endif | |
62 | ||
63 | #ifdef __SALFORDC__ | |
64 | #include <clib.h> | |
65 | #endif | |
66 | ||
67 | // Pattern matching code. (FIXME) | |
68 | // Yes, this path is deliberate (for Borland compilation) | |
69 | #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */ | |
70 | #include "glob.inc" | |
71 | #else | |
72 | #include "../common/glob.inc" | |
73 | #endif | |
74 | ||
75 | #ifdef __WXMSW__ | |
76 | #include "windows.h" | |
77 | #endif | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // function protoypes | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | #if wxUSE_GUI | |
84 | static wxWindow *wxFindWindowByLabel1(const wxString& title, wxWindow *parent); | |
85 | static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow *parent); | |
86 | #endif // wxUSE_GUI | |
87 | ||
88 | // ============================================================================ | |
89 | // implementation | |
90 | // ============================================================================ | |
91 | ||
92 | // ---------------------------------------------------------------------------- | |
93 | // string functions | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | #ifdef __WXMAC__ | |
97 | int strcasecmp(const char *str_1, const char *str_2) | |
98 | { | |
99 | register char c1, c2; | |
100 | do { | |
101 | c1 = tolower(*str_1++); | |
102 | c2 = tolower(*str_2++); | |
103 | } while ( c1 && (c1 == c2) ); | |
104 | ||
105 | return c1 - c2; | |
106 | } | |
107 | ||
108 | int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) | |
109 | { | |
110 | ||
111 | register char c1, c2; | |
112 | while( maxchar--) | |
113 | { | |
114 | c1 = tolower(*str_1++); | |
115 | c2 = tolower(*str_2++); | |
116 | ||
117 | if ( !c1 || c1!=c2 ) | |
118 | return c1 - c2; | |
119 | ||
120 | } ; | |
121 | ||
122 | return 0 ; | |
123 | ||
124 | } | |
125 | #endif // wxMAC | |
126 | ||
127 | #ifdef __VMS__ | |
128 | // we have no strI functions under VMS, therefore I have implemented | |
129 | // an inefficient but portable version: convert copies of strings to lowercase | |
130 | // and then use the normal comparison | |
131 | static void myLowerString(char *s) | |
132 | { | |
133 | while(*s){ | |
134 | if(isalpha(*s)) *s = (char)tolower(*s); | |
135 | s++; | |
136 | } | |
137 | } | |
138 | ||
139 | int strcasecmp(const char *str_1, const char *str_2) | |
140 | { | |
141 | char *temp1 = new char[strlen(str_1)+1]; | |
142 | char *temp2 = new char[strlen(str_2)+1]; | |
143 | strcpy(temp1,str_1); | |
144 | strcpy(temp2,str_2); | |
145 | myLowerString(temp1); | |
146 | myLowerString(temp2); | |
147 | ||
148 | int result = strcmp(temp1,temp2); | |
149 | delete[] temp1; | |
150 | delete[] temp2; | |
151 | ||
152 | return(result); | |
153 | } | |
154 | ||
155 | int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) | |
156 | { | |
157 | char *temp1 = new char[strlen(str_1)+1]; | |
158 | char *temp2 = new char[strlen(str_2)+1]; | |
159 | strcpy(temp1,str_1); | |
160 | strcpy(temp2,str_2); | |
161 | myLowerString(temp1); | |
162 | myLowerString(temp2); | |
163 | ||
164 | int result = strncmp(temp1,temp2,maxchar); | |
165 | delete[] temp1; | |
166 | delete[] temp2; | |
167 | ||
168 | return(result); | |
169 | } | |
170 | #endif // __VMS__ | |
171 | ||
172 | #ifdef __WINDOWS__ | |
173 | ||
174 | #ifndef __GNUWIN32__ | |
175 | #ifndef __MWERKS__ | |
176 | #define strcasecmp stricmp | |
177 | #define strncasecmp strnicmp | |
178 | #else | |
179 | #define strcasecmp _stricmp | |
180 | #define strncasecmp _strnicmp | |
181 | #endif | |
182 | #endif | |
183 | ||
184 | #else | |
185 | ||
186 | #ifdef __EMX__ | |
187 | #define strcasecmp stricmp | |
188 | #define strncasecmp strnicmp | |
189 | #endif | |
190 | ||
191 | // This declaration is missing in SunOS! | |
192 | // (Yes, I know it is NOT ANSI-C but its in BSD libc) | |
193 | #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__) | |
194 | extern "C" | |
195 | { | |
196 | int strcasecmp (const char *, const char *); | |
197 | int strncasecmp (const char *, const char *, size_t); | |
198 | } | |
199 | #endif | |
200 | #endif /* __WXMSW__ */ | |
201 | ||
202 | #ifdef __WXPM__ | |
203 | #define strcasecmp stricmp | |
204 | #define strncasecmp strnicmp | |
205 | #endif | |
206 | ||
207 | wxChar * | |
208 | copystring (const wxChar *s) | |
209 | { | |
210 | if (s == NULL) s = wxT(""); | |
211 | size_t len = wxStrlen (s) + 1; | |
212 | ||
213 | wxChar *news = new wxChar[len]; | |
214 | memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest | |
215 | ||
216 | return news; | |
217 | } | |
218 | ||
219 | // Id generation | |
220 | static long wxCurrentId = 100; | |
221 | ||
222 | long | |
223 | wxNewId (void) | |
224 | { | |
225 | return wxCurrentId++; | |
226 | } | |
227 | ||
228 | long | |
229 | wxGetCurrentId(void) { return wxCurrentId; } | |
230 | ||
231 | void | |
232 | wxRegisterId (long id) | |
233 | { | |
234 | if (id >= wxCurrentId) | |
235 | wxCurrentId = id + 1; | |
236 | } | |
237 | ||
238 | void | |
239 | StringToFloat (wxChar *s, float *number) | |
240 | { | |
241 | if (s && *s && number) | |
242 | *number = (float) wxStrtod (s, (wxChar **) NULL); | |
243 | } | |
244 | ||
245 | void | |
246 | StringToDouble (wxChar *s, double *number) | |
247 | { | |
248 | if (s && *s && number) | |
249 | *number = wxStrtod (s, (wxChar **) NULL); | |
250 | } | |
251 | ||
252 | wxChar * | |
253 | FloatToString (float number, const wxChar *fmt) | |
254 | { | |
255 | static wxChar buf[256]; | |
256 | ||
257 | // sprintf (buf, "%.2f", number); | |
258 | wxSprintf (buf, fmt, number); | |
259 | return buf; | |
260 | } | |
261 | ||
262 | wxChar * | |
263 | DoubleToString (double number, const wxChar *fmt) | |
264 | { | |
265 | static wxChar buf[256]; | |
266 | ||
267 | wxSprintf (buf, fmt, number); | |
268 | return buf; | |
269 | } | |
270 | ||
271 | void | |
272 | StringToInt (wxChar *s, int *number) | |
273 | { | |
274 | if (s && *s && number) | |
275 | *number = (int) wxStrtol (s, (wxChar **) NULL, 10); | |
276 | } | |
277 | ||
278 | void | |
279 | StringToLong (wxChar *s, long *number) | |
280 | { | |
281 | if (s && *s && number) | |
282 | *number = wxStrtol (s, (wxChar **) NULL, 10); | |
283 | } | |
284 | ||
285 | wxChar * | |
286 | IntToString (int number) | |
287 | { | |
288 | static wxChar buf[20]; | |
289 | ||
290 | wxSprintf (buf, wxT("%d"), number); | |
291 | return buf; | |
292 | } | |
293 | ||
294 | wxChar * | |
295 | LongToString (long number) | |
296 | { | |
297 | static wxChar buf[20]; | |
298 | ||
299 | wxSprintf (buf, wxT("%ld"), number); | |
300 | return buf; | |
301 | } | |
302 | ||
303 | // Array used in DecToHex conversion routine. | |
304 | static wxChar hexArray[] = wxT("0123456789ABCDEF"); | |
305 | ||
306 | // Convert 2-digit hex number to decimal | |
307 | int wxHexToDec(const wxString& buf) | |
308 | { | |
309 | int firstDigit, secondDigit; | |
310 | ||
311 | if (buf.GetChar(0) >= wxT('A')) | |
312 | firstDigit = buf.GetChar(0) - wxT('A') + 10; | |
313 | else | |
314 | firstDigit = buf.GetChar(0) - wxT('0'); | |
315 | ||
316 | if (buf.GetChar(1) >= wxT('A')) | |
317 | secondDigit = buf.GetChar(1) - wxT('A') + 10; | |
318 | else | |
319 | secondDigit = buf.GetChar(1) - wxT('0'); | |
320 | ||
321 | return firstDigit * 16 + secondDigit; | |
322 | } | |
323 | ||
324 | // Convert decimal integer to 2-character hex string | |
325 | void wxDecToHex(int dec, wxChar *buf) | |
326 | { | |
327 | int firstDigit = (int)(dec/16.0); | |
328 | int secondDigit = (int)(dec - (firstDigit*16.0)); | |
329 | buf[0] = hexArray[firstDigit]; | |
330 | buf[1] = hexArray[secondDigit]; | |
331 | buf[2] = 0; | |
332 | } | |
333 | ||
334 | // Convert decimal integer to 2-character hex string | |
335 | wxString wxDecToHex(int dec) | |
336 | { | |
337 | wxChar buf[3]; | |
338 | wxDecToHex(dec, buf); | |
339 | return wxString(buf); | |
340 | } | |
341 | ||
342 | // Match a string INDEPENDENT OF CASE | |
343 | bool | |
344 | StringMatch (char *str1, char *str2, bool subString, bool exact) | |
345 | { | |
346 | if (str1 == NULL || str2 == NULL) | |
347 | return FALSE; | |
348 | if (str1 == str2) | |
349 | return TRUE; | |
350 | ||
351 | if (subString) | |
352 | { | |
353 | int len1 = strlen (str1); | |
354 | int len2 = strlen (str2); | |
355 | int i; | |
356 | ||
357 | // Search for str1 in str2 | |
358 | // Slow .... but acceptable for short strings | |
359 | for (i = 0; i <= len2 - len1; i++) | |
360 | { | |
361 | if (strncasecmp (str1, str2 + i, len1) == 0) | |
362 | return TRUE; | |
363 | } | |
364 | } | |
365 | else if (exact) | |
366 | { | |
367 | if (strcasecmp (str1, str2) == 0) | |
368 | return TRUE; | |
369 | } | |
370 | else | |
371 | { | |
372 | int len1 = strlen (str1); | |
373 | int len2 = strlen (str2); | |
374 | ||
375 | if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0) | |
376 | return TRUE; | |
377 | } | |
378 | ||
379 | return FALSE; | |
380 | } | |
381 | ||
382 | // Return the current date/time | |
383 | // [volatile] | |
384 | wxString wxNow() | |
385 | { | |
386 | time_t now = time((time_t *) NULL); | |
387 | char *date = ctime(&now); | |
388 | date[24] = '\0'; | |
389 | return wxString(date); | |
390 | } | |
391 | ||
392 | #if wxUSE_GUI | |
393 | ||
394 | // ---------------------------------------------------------------------------- | |
395 | // Strip out any menu codes | |
396 | // ---------------------------------------------------------------------------- | |
397 | ||
398 | wxChar *wxStripMenuCodes (wxChar *in, wxChar *out) | |
399 | { | |
400 | if (!in) | |
401 | return (wxChar *) NULL; | |
402 | ||
403 | if (!out) | |
404 | out = copystring(in); | |
405 | ||
406 | wxChar *tmpOut = out; | |
407 | ||
408 | while (*in) | |
409 | { | |
410 | if (*in == wxT('&')) | |
411 | { | |
412 | // Check && -> &, &x -> x | |
413 | if (*++in == wxT('&')) | |
414 | *out++ = *in++; | |
415 | } | |
416 | else if (*in == wxT('\t')) | |
417 | { | |
418 | // Remove all stuff after \t in X mode, and let the stuff as is | |
419 | // in Windows mode. | |
420 | // Accelerators are handled in wx_item.cc for Motif, and are not | |
421 | // YET supported in XView | |
422 | break; | |
423 | } | |
424 | else | |
425 | *out++ = *in++; | |
426 | } // while | |
427 | ||
428 | *out = wxT('\0'); | |
429 | ||
430 | return tmpOut; | |
431 | } | |
432 | ||
433 | wxString wxStripMenuCodes(const wxString& str) | |
434 | { | |
435 | wxChar *buf = new wxChar[str.Length() + 1]; | |
436 | wxStripMenuCodes(WXSTRINGCAST str, buf); | |
437 | wxString str1(buf); | |
438 | delete[] buf; | |
439 | return str1; | |
440 | } | |
441 | ||
442 | // ---------------------------------------------------------------------------- | |
443 | // Window search functions | |
444 | // ---------------------------------------------------------------------------- | |
445 | ||
446 | /* | |
447 | * If parent is non-NULL, look through children for a label or title | |
448 | * matching the specified string. If NULL, look through all top-level windows. | |
449 | * | |
450 | */ | |
451 | ||
452 | wxWindow * | |
453 | wxFindWindowByLabel (const wxString& title, wxWindow * parent) | |
454 | { | |
455 | if (parent) | |
456 | { | |
457 | return wxFindWindowByLabel1(title, parent); | |
458 | } | |
459 | else | |
460 | { | |
461 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); | |
462 | node; | |
463 | node = node->GetNext() ) | |
464 | { | |
465 | wxWindow *win = node->GetData(); | |
466 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
467 | if (retwin) | |
468 | return retwin; | |
469 | } // for() | |
470 | ||
471 | } | |
472 | return (wxWindow *) NULL; | |
473 | } | |
474 | ||
475 | // Recursive | |
476 | static wxWindow * | |
477 | wxFindWindowByLabel1 (const wxString& title, wxWindow * parent) | |
478 | { | |
479 | if (parent) | |
480 | { | |
481 | if (parent->GetLabel() == title) | |
482 | return parent; | |
483 | } | |
484 | ||
485 | if (parent) | |
486 | { | |
487 | for ( wxWindowList::Node * node = parent->GetChildren().GetFirst(); | |
488 | node; | |
489 | node = node->GetNext() ) | |
490 | { | |
491 | wxWindow *win = (wxWindow *)node->GetData(); | |
492 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
493 | if (retwin) | |
494 | return retwin; | |
495 | } | |
496 | ||
497 | } | |
498 | ||
499 | return (wxWindow *) NULL; // Not found | |
500 | } | |
501 | ||
502 | /* | |
503 | * If parent is non-NULL, look through children for a name | |
504 | * matching the specified string. If NULL, look through all top-level windows. | |
505 | * | |
506 | */ | |
507 | ||
508 | wxWindow * | |
509 | wxFindWindowByName (const wxString& title, wxWindow * parent) | |
510 | { | |
511 | if (parent) | |
512 | { | |
513 | return wxFindWindowByName1 (title, parent); | |
514 | } | |
515 | else | |
516 | { | |
517 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); | |
518 | node; | |
519 | node = node->GetNext() ) | |
520 | { | |
521 | wxWindow *win = node->GetData(); | |
522 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
523 | if (retwin) | |
524 | return retwin; | |
525 | } | |
526 | ||
527 | } | |
528 | ||
529 | // Failed? Try by label instead. | |
530 | return wxFindWindowByLabel(title, parent); | |
531 | } | |
532 | ||
533 | // Recursive | |
534 | static wxWindow * | |
535 | wxFindWindowByName1 (const wxString& title, wxWindow * parent) | |
536 | { | |
537 | if (parent) | |
538 | { | |
539 | if ( parent->GetName() == title ) | |
540 | return parent; | |
541 | } | |
542 | ||
543 | if (parent) | |
544 | { | |
545 | for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ()) | |
546 | { | |
547 | wxWindow *win = (wxWindow *) node->Data (); | |
548 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
549 | if (retwin) | |
550 | return retwin; | |
551 | } // for() | |
552 | ||
553 | } | |
554 | ||
555 | return (wxWindow *) NULL; // Not found | |
556 | ||
557 | } | |
558 | ||
559 | // Returns menu item id or -1 if none. | |
560 | int | |
561 | wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString) | |
562 | { | |
563 | wxMenuBar *menuBar = frame->GetMenuBar (); | |
564 | if (!menuBar) | |
565 | return -1; | |
566 | return menuBar->FindMenuItem (menuString, itemString); | |
567 | } | |
568 | ||
569 | #endif // wxUSE_GUI | |
570 | ||
571 | /* | |
572 | On Fri, 21 Jul 1995, Paul Craven wrote: | |
573 | ||
574 | > Is there a way to find the path of running program's executable? I can get | |
575 | > my home directory, and the current directory, but I don't know how to get the | |
576 | > executable directory. | |
577 | > | |
578 | ||
579 | The code below (warty as it is), does what you want on most Unix, | |
580 | DOS, and Mac platforms (it's from the ALS Prolog main). | |
581 | ||
582 | || Ken Bowen Applied Logic Systems, Inc. PO Box 180, | |
583 | ||==== Voice: +1 (617)965-9191 Newton Centre, | |
584 | || FAX: +1 (617)965-1636 MA 02159 USA | |
585 | Email: ken@als.com WWW: http://www.als.com | |
586 | ------------------------------------------------------------------------ | |
587 | */ | |
588 | ||
589 | // This code is commented out but it may be integrated with wxWin at | |
590 | // a later date, after testing. Thanks Ken! | |
591 | #if 0 | |
592 | ||
593 | /*--------------------------------------------------------------------* | |
594 | | whereami is given a filename f in the form: whereami(argv[0]) | |
595 | | It returns the directory in which the executable file (containing | |
596 | | this code [main.c] ) may be found. A dot will be returned to indicate | |
597 | | the current directory. | |
598 | *--------------------------------------------------------------------*/ | |
599 | ||
600 | static void | |
601 | whereami(name) | |
602 | char *name; | |
603 | { | |
604 | register char *cutoff = NULL; /* stifle -Wall */ | |
605 | register char *s; | |
606 | register char *t; | |
607 | int cc; | |
608 | char ebuf[4096]; | |
609 | ||
610 | /* | |
611 | * See if the file is accessible either through the current directory | |
612 | * or through an absolute path. | |
613 | */ | |
614 | ||
615 | if (access(name, R_OK) == 0) { | |
616 | ||
617 | /*-------------------------------------------------------------* | |
618 | * The file was accessible without any other work. But the current | |
619 | * working directory might change on us, so if it was accessible | |
620 | * through the cwd, then we should get it for later accesses. | |
621 | *-------------------------------------------------------------*/ | |
622 | ||
623 | t = imagedir; | |
624 | if (!absolute_pathname(name)) { | |
625 | #if defined(DOS) || defined(__WIN32__) | |
626 | int drive; | |
627 | char *newrbuf; | |
628 | ||
629 | newrbuf = imagedir; | |
630 | #ifndef __DJGPP__ | |
631 | if (*(name + 1) == ':') { | |
632 | if (*name >= 'a' && *name <= 'z') | |
633 | drive = (int) (*name - 'a' + 1); | |
634 | else | |
635 | drive = (int) (*name - 'A' + 1); | |
636 | *newrbuf++ = *name; | |
637 | *newrbuf++ = *(name + 1); | |
638 | *newrbuf++ = DIR_SEPARATOR; | |
639 | } | |
640 | else { | |
641 | drive = 0; | |
642 | *newrbuf++ = DIR_SEPARATOR; | |
643 | } | |
644 | if (getcwd(newrbuf, drive) == 0) { /* } */ | |
645 | #else | |
646 | if (getcwd(newrbuf, 1024) == 0) { /* } */ | |
647 | #endif | |
648 | #else /* DOS */ | |
649 | #ifdef HAVE_GETWD | |
650 | if (getwd(imagedir) == 0) { /* } */ | |
651 | #else /* !HAVE_GETWD */ | |
652 | if (getcwd(imagedir, 1024) == 0) { | |
653 | #endif /* !HAVE_GETWD */ | |
654 | #endif /* DOS */ | |
655 | fatal_error(FE_GETCWD, 0); | |
656 | } | |
657 | for (; *t; t++) /* Set t to end of buffer */ | |
658 | ; | |
659 | if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already | |
660 | * last char | |
661 | */ | |
662 | cutoff = t - 1; | |
663 | else { | |
664 | cutoff = t; /* otherwise put one in */ | |
665 | *t++ = DIR_SEPARATOR; | |
666 | } | |
667 | } | |
668 | #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__)) | |
669 | else | |
670 | (*t++ = DIR_SEPARATOR); | |
671 | #endif | |
672 | ||
673 | /*-------------------------------------------------------------* | |
674 | * Copy the rest of the string and set the cutoff if it was not | |
675 | * already set. If the first character of name is a slash, cutoff | |
676 | * is not presently set but will be on the first iteration of the | |
677 | * loop below. | |
678 | *-------------------------------------------------------------*/ | |
679 | ||
680 | for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) { | |
681 | if (*s == DIR_SEPARATOR) | |
682 | cutoff = t; | |
683 | if (!(*t++ = *s++)) | |
684 | break; | |
685 | } | |
686 | ||
687 | } | |
688 | else { | |
689 | ||
690 | /*-------------------------------------------------------------* | |
691 | * Get the path list from the environment. If the path list is | |
692 | * inaccessible for any reason, leave with fatal error. | |
693 | *-------------------------------------------------------------*/ | |
694 | ||
695 | #ifdef __MAC__ | |
696 | if ((s = getenv("Commands")) == (char *) 0) | |
697 | #else | |
698 | if ((s = getenv("PATH")) == (char *) 0) | |
699 | #endif | |
700 | fatal_error(FE_PATH, 0); | |
701 | ||
702 | /* | |
703 | * Copy path list into ebuf and set the source pointer to the | |
704 | * beginning of this buffer. | |
705 | */ | |
706 | ||
707 | strcpy(ebuf, s); | |
708 | s = ebuf; | |
709 | ||
710 | for (;;) { | |
711 | t = imagedir; | |
712 | while (*s && *s != PATH_SEPARATOR) | |
713 | *t++ = *s++; | |
714 | if (t > imagedir && *(t - 1) == DIR_SEPARATOR) | |
715 | ; /* do nothing -- slash already is in place */ | |
716 | else | |
717 | *t++ = DIR_SEPARATOR; /* put in the slash */ | |
718 | cutoff = t - 1; /* set cutoff */ | |
719 | strcpy(t, name); | |
720 | if (access(imagedir, R_OK) == 0) | |
721 | break; | |
722 | ||
723 | if (*s) | |
724 | s++; /* advance source pointer */ | |
725 | else | |
726 | fatal_error(FE_INFND, 0); | |
727 | } | |
728 | ||
729 | } | |
730 | ||
731 | /*-------------------------------------------------------------* | |
732 | | At this point the full pathname should exist in imagedir and | |
733 | | cutoff should be set to the final slash. We must now determine | |
734 | | whether the file name is a symbolic link or not and chase it down | |
735 | | if it is. Note that we reuse ebuf for getting the link. | |
736 | *-------------------------------------------------------------*/ | |
737 | ||
738 | #ifdef HAVE_SYMLINK | |
739 | while ((cc = readlink(imagedir, ebuf, 512)) != -1) { | |
740 | ebuf[cc] = 0; | |
741 | s = ebuf; | |
742 | if (*s == DIR_SEPARATOR) { | |
743 | t = imagedir; | |
744 | } | |
745 | else { | |
746 | t = cutoff + 1; | |
747 | } | |
748 | for (;;) { | |
749 | if (*s == DIR_SEPARATOR) | |
750 | cutoff = t; /* mark the last slash seen */ | |
751 | if (!(*t++ = *s++)) /* copy the character */ | |
752 | break; | |
753 | } | |
754 | } | |
755 | ||
756 | #endif /* HAVE_SYMLINK */ | |
757 | ||
758 | strcpy(imagename, cutoff + 1); /* keep the image name */ | |
759 | *(cutoff + 1) = 0; /* chop off the filename part */ | |
760 | } | |
761 | ||
762 | #endif | |
763 | ||
764 | #if wxUSE_GUI | |
765 | ||
766 | // ---------------------------------------------------------------------------- | |
767 | // GUI helpers | |
768 | // ---------------------------------------------------------------------------- | |
769 | ||
770 | /* | |
771 | * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp | |
772 | * since otherwise the generic code may be pulled in unnecessarily. | |
773 | */ | |
774 | ||
775 | int wxMessageBox(const wxString& message, const wxString& caption, long style, | |
776 | wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) ) | |
777 | { | |
778 | wxMessageDialog dialog(parent, message, caption, style); | |
779 | ||
780 | int ans = dialog.ShowModal(); | |
781 | switch ( ans ) | |
782 | { | |
783 | case wxID_OK: | |
784 | return wxOK; | |
785 | break; | |
786 | case wxID_YES: | |
787 | return wxYES; | |
788 | break; | |
789 | case wxID_NO: | |
790 | return wxNO; | |
791 | break; | |
792 | default: | |
793 | case wxID_CANCEL: | |
794 | return wxCANCEL; | |
795 | break; | |
796 | } | |
797 | return ans; | |
798 | } | |
799 | ||
800 | #if wxUSE_TEXTDLG | |
801 | wxString wxGetTextFromUser(const wxString& message, const wxString& caption, | |
802 | const wxString& defaultValue, wxWindow *parent, | |
803 | int x, int y, bool WXUNUSED(centre) ) | |
804 | { | |
805 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y)); | |
806 | if (dialog.ShowModal() == wxID_OK) | |
807 | return dialog.GetValue(); | |
808 | else | |
809 | return wxString(""); | |
810 | } | |
811 | #endif // wxUSE_TEXTDLG | |
812 | ||
813 | #ifdef __MWERKS__ | |
814 | char *strdup(const char *s) | |
815 | { | |
816 | return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ; | |
817 | } | |
818 | ||
819 | int isascii( int c ) | |
820 | { | |
821 | return ( c >= 0 && c < 128 ) ; | |
822 | } | |
823 | #endif // __MWERKS__ | |
824 | ||
825 | // ---------------------------------------------------------------------------- | |
826 | // misc functions | |
827 | // ---------------------------------------------------------------------------- | |
828 | ||
829 | void wxEnableTopLevelWindows(bool enable) | |
830 | { | |
831 | wxWindowList::Node *node; | |
832 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
833 | node->GetData()->Enable(enable); | |
834 | } | |
835 | ||
836 | // Yield to other apps/messages and disable user input | |
837 | bool wxSafeYield(wxWindow *win) | |
838 | { | |
839 | wxEnableTopLevelWindows(FALSE); | |
840 | // always enable ourselves | |
841 | if ( win ) | |
842 | win->Enable(TRUE); | |
843 | bool rc = wxYield(); | |
844 | wxEnableTopLevelWindows(TRUE); | |
845 | return rc; | |
846 | } | |
847 | ||
848 | // Don't synthesize KeyUp events holding down a key and producing KeyDown | |
849 | // events with autorepeat. On by default and always on in wxMSW. wxGTK version | |
850 | // in utilsgtk.cpp. | |
851 | #ifndef __WXGTK__ | |
852 | bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) | |
853 | { | |
854 | return TRUE; // detectable auto-repeat is the only mode MSW supports | |
855 | } | |
856 | #endif // !wxGTK | |
857 | ||
858 | #endif // wxUSE_GUI | |
859 | ||
860 | // ---------------------------------------------------------------------------- | |
861 | // network and user id functions | |
862 | // ---------------------------------------------------------------------------- | |
863 | ||
864 | // Get Full RFC822 style email address | |
865 | bool wxGetEmailAddress(wxChar *address, int maxSize) | |
866 | { | |
867 | wxString email = wxGetEmailAddress(); | |
868 | if ( !email ) | |
869 | return FALSE; | |
870 | ||
871 | wxStrncpy(address, email, maxSize - 1); | |
872 | address[maxSize - 1] = wxT('\0'); | |
873 | ||
874 | return TRUE; | |
875 | } | |
876 | ||
877 | wxString wxGetEmailAddress() | |
878 | { | |
879 | wxString email; | |
880 | ||
881 | wxString host = wxGetHostName(); | |
882 | if ( !!host ) | |
883 | { | |
884 | wxString user = wxGetUserId(); | |
885 | if ( !!user ) | |
886 | { | |
887 | wxString email(user); | |
888 | email << wxT('@') << host; | |
889 | } | |
890 | } | |
891 | ||
892 | return email; | |
893 | } | |
894 | ||
895 | wxString wxGetUserId() | |
896 | { | |
897 | static const int maxLoginLen = 256; // FIXME arbitrary number | |
898 | ||
899 | wxString buf; | |
900 | bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen); | |
901 | buf.UngetWriteBuf(); | |
902 | ||
903 | if ( !ok ) | |
904 | buf.Empty(); | |
905 | ||
906 | return buf; | |
907 | } | |
908 | ||
909 | wxString wxGetUserName() | |
910 | { | |
911 | static const int maxUserNameLen = 1024; // FIXME arbitrary number | |
912 | ||
913 | wxString buf; | |
914 | bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen); | |
915 | buf.UngetWriteBuf(); | |
916 | ||
917 | if ( !ok ) | |
918 | buf.Empty(); | |
919 | ||
920 | return buf; | |
921 | } | |
922 | ||
923 | wxString wxGetHostName() | |
924 | { | |
925 | static const size_t hostnameSize = 257; | |
926 | ||
927 | wxString buf; | |
928 | bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); | |
929 | ||
930 | buf.UngetWriteBuf(); | |
931 | ||
932 | if ( !ok ) | |
933 | buf.Empty(); | |
934 | ||
935 | return buf; | |
936 | } | |
937 | ||
938 | wxString wxGetFullHostName() | |
939 | { | |
940 | static const size_t hostnameSize = 257; | |
941 | ||
942 | wxString buf; | |
943 | bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); | |
944 | ||
945 | buf.UngetWriteBuf(); | |
946 | ||
947 | if ( !ok ) | |
948 | buf.Empty(); | |
949 | ||
950 | return buf; | |
951 | } | |
952 |