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