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