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