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