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