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