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