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