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