]> git.saurik.com Git - wxWidgets.git/blame - src/common/utilscmn.cpp
don't use implicit parent for the progress dialogs
[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.
c67d6888 609#if wxUSE_NOTEBOOK
d1c8aaa3
JS
610 if (win->IsKindOf(CLASSINFO(wxNotebook)))
611 {
612 wxNotebook* nb = (wxNotebook*) win;
613 int sel = nb->GetSelection();
614 if (sel >= 0)
615 {
616 wxWindow* child = nb->GetPage(sel);
617 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
618 if (foundWin)
619 return foundWin;
620 }
621 }
c67d6888
JS
622#endif
623
d1c8aaa3
JS
624 /* Doesn't work
625 // Frame case
626 else if (win->IsKindOf(CLASSINFO(wxFrame)))
627 {
628 // Pseudo-children that may not be mentioned in the child list
629 wxWindowList extraChildren;
630 wxFrame* frame = (wxFrame*) win;
631 if (frame->GetStatusBar())
632 extraChildren.Append(frame->GetStatusBar());
633 if (frame->GetToolBar())
634 extraChildren.Append(frame->GetToolBar());
635
636 wxNode* node = extraChildren.First();
637 while (node)
638 {
639 wxWindow* child = (wxWindow*) node->Data();
640 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
641 if (foundWin)
642 return foundWin;
643 node = node->Next();
644 }
645 }
646 */
647
648 wxNode* node = win->GetChildren().Last();
59a12e90
JS
649 while (node)
650 {
651 wxWindow* child = (wxWindow*) node->Data();
652 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
653 if (foundWin)
d1c8aaa3
JS
654 return foundWin;
655 node = node->Previous();
59a12e90
JS
656 }
657
658 wxPoint pos = win->GetPosition();
659 wxSize sz = win->GetSize();
660 if (win->GetParent())
661 {
662 pos = win->GetParent()->ClientToScreen(pos);
663 }
664
665 wxRect rect(pos, sz);
666 if (rect.Inside(pt))
667 return win;
668 else
669 return NULL;
670}
671
57591e0e 672wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
59a12e90 673{
57591e0e
JS
674 // Go backwards through the list since windows
675 // on top are likely to have been appended most
676 // recently.
677 wxNode* node = wxTopLevelWindows.Last();
59a12e90
JS
678 while (node)
679 {
680 wxWindow* win = (wxWindow*) node->Data();
681 wxWindow* found = wxFindWindowAtPoint(win, pt);
682 if (found)
683 return found;
57591e0e 684 node = node->Previous();
59a12e90
JS
685 }
686 return NULL;
c801d85f
KB
687}
688
e90c1d2a
VZ
689#endif // wxUSE_GUI
690
c801d85f
KB
691/*
692On Fri, 21 Jul 1995, Paul Craven wrote:
693
694> Is there a way to find the path of running program's executable? I can get
695> my home directory, and the current directory, but I don't know how to get the
696> executable directory.
3f4a0c5b 697>
c801d85f
KB
698
699The code below (warty as it is), does what you want on most Unix,
700DOS, and Mac platforms (it's from the ALS Prolog main).
701
3f4a0c5b 702|| Ken Bowen Applied Logic Systems, Inc. PO Box 180,
c801d85f
KB
703||==== Voice: +1 (617)965-9191 Newton Centre,
704|| FAX: +1 (617)965-1636 MA 02159 USA
705 Email: ken@als.com WWW: http://www.als.com
706------------------------------------------------------------------------
707*/
708
709// This code is commented out but it may be integrated with wxWin at
710// a later date, after testing. Thanks Ken!
711#if 0
712
713/*--------------------------------------------------------------------*
714 | whereami is given a filename f in the form: whereami(argv[0])
3f4a0c5b
VZ
715 | It returns the directory in which the executable file (containing
716 | this code [main.c] ) may be found. A dot will be returned to indicate
c801d85f
KB
717 | the current directory.
718 *--------------------------------------------------------------------*/
719
720static void
721whereami(name)
722 char *name;
723{
3f4a0c5b 724 register char *cutoff = NULL; /* stifle -Wall */
c801d85f
KB
725 register char *s;
726 register char *t;
727 int cc;
728 char ebuf[4096];
729
730 /*
731 * See if the file is accessible either through the current directory
732 * or through an absolute path.
733 */
734
735 if (access(name, R_OK) == 0) {
736
3f4a0c5b
VZ
737 /*-------------------------------------------------------------*
738 * The file was accessible without any other work. But the current
739 * working directory might change on us, so if it was accessible
740 * through the cwd, then we should get it for later accesses.
741 *-------------------------------------------------------------*/
c801d85f 742
3f4a0c5b
VZ
743 t = imagedir;
744 if (!absolute_pathname(name)) {
c801d85f 745#if defined(DOS) || defined(__WIN32__)
3f4a0c5b
VZ
746 int drive;
747 char *newrbuf;
c801d85f 748
3f4a0c5b 749 newrbuf = imagedir;
c801d85f 750#ifndef __DJGPP__
3f4a0c5b
VZ
751 if (*(name + 1) == ':') {
752 if (*name >= 'a' && *name <= 'z')
753 drive = (int) (*name - 'a' + 1);
754 else
755 drive = (int) (*name - 'A' + 1);
756 *newrbuf++ = *name;
757 *newrbuf++ = *(name + 1);
758 *newrbuf++ = DIR_SEPARATOR;
759 }
760 else {
761 drive = 0;
762 *newrbuf++ = DIR_SEPARATOR;
763 }
764 if (getcwd(newrbuf, drive) == 0) { /* } */
c801d85f 765#else
3f4a0c5b 766 if (getcwd(newrbuf, 1024) == 0) { /* } */
c801d85f
KB
767#endif
768#else /* DOS */
769#ifdef HAVE_GETWD
3f4a0c5b 770 if (getwd(imagedir) == 0) { /* } */
c801d85f 771#else /* !HAVE_GETWD */
3f4a0c5b 772 if (getcwd(imagedir, 1024) == 0) {
c801d85f
KB
773#endif /* !HAVE_GETWD */
774#endif /* DOS */
3f4a0c5b
VZ
775 fatal_error(FE_GETCWD, 0);
776 }
777 for (; *t; t++) /* Set t to end of buffer */
778 ;
779 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
780 * last char
781 */
782 cutoff = t - 1;
783 else {
784 cutoff = t; /* otherwise put one in */
785 *t++ = DIR_SEPARATOR;
786 }
787 }
c801d85f 788#if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
3f4a0c5b
VZ
789 else
790 (*t++ = DIR_SEPARATOR);
c801d85f
KB
791#endif
792
3f4a0c5b
VZ
793 /*-------------------------------------------------------------*
794 * Copy the rest of the string and set the cutoff if it was not
795 * already set. If the first character of name is a slash, cutoff
796 * is not presently set but will be on the first iteration of the
797 * loop below.
798 *-------------------------------------------------------------*/
c801d85f 799
3f4a0c5b
VZ
800 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
801 if (*s == DIR_SEPARATOR)
802 cutoff = t;
803 if (!(*t++ = *s++))
804 break;
805 }
c801d85f
KB
806
807 }
808 else {
809
3f4a0c5b
VZ
810 /*-------------------------------------------------------------*
811 * Get the path list from the environment. If the path list is
812 * inaccessible for any reason, leave with fatal error.
813 *-------------------------------------------------------------*/
c801d85f
KB
814
815#ifdef __MAC__
3f4a0c5b 816 if ((s = getenv("Commands")) == (char *) 0)
c801d85f 817#else
3f4a0c5b 818 if ((s = getenv("PATH")) == (char *) 0)
c801d85f 819#endif
3f4a0c5b
VZ
820 fatal_error(FE_PATH, 0);
821
822 /*
823 * Copy path list into ebuf and set the source pointer to the
824 * beginning of this buffer.
825 */
826
827 strcpy(ebuf, s);
828 s = ebuf;
829
830 for (;;) {
831 t = imagedir;
832 while (*s && *s != PATH_SEPARATOR)
833 *t++ = *s++;
834 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
835 ; /* do nothing -- slash already is in place */
836 else
837 *t++ = DIR_SEPARATOR; /* put in the slash */
838 cutoff = t - 1; /* set cutoff */
839 strcpy(t, name);
840 if (access(imagedir, R_OK) == 0)
841 break;
842
843 if (*s)
844 s++; /* advance source pointer */
845 else
846 fatal_error(FE_INFND, 0);
847 }
c801d85f
KB
848
849 }
850
851 /*-------------------------------------------------------------*
852 | At this point the full pathname should exist in imagedir and
853 | cutoff should be set to the final slash. We must now determine
854 | whether the file name is a symbolic link or not and chase it down
855 | if it is. Note that we reuse ebuf for getting the link.
856 *-------------------------------------------------------------*/
857
858#ifdef HAVE_SYMLINK
859 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
3f4a0c5b
VZ
860 ebuf[cc] = 0;
861 s = ebuf;
862 if (*s == DIR_SEPARATOR) {
863 t = imagedir;
864 }
865 else {
866 t = cutoff + 1;
867 }
868 for (;;) {
869 if (*s == DIR_SEPARATOR)
870 cutoff = t; /* mark the last slash seen */
871 if (!(*t++ = *s++)) /* copy the character */
872 break;
873 }
c801d85f
KB
874 }
875
876#endif /* HAVE_SYMLINK */
877
3f4a0c5b
VZ
878 strcpy(imagename, cutoff + 1); /* keep the image name */
879 *(cutoff + 1) = 0; /* chop off the filename part */
c801d85f
KB
880}
881
882#endif
ead7ce10 883
e90c1d2a
VZ
884#if wxUSE_GUI
885
886// ----------------------------------------------------------------------------
887// GUI helpers
888// ----------------------------------------------------------------------------
ead7ce10 889
dfad0599
JS
890/*
891 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
892 * since otherwise the generic code may be pulled in unnecessarily.
893 */
894
1e6feb95
VZ
895#if wxUSE_MSGDLG
896
dfad0599
JS
897int wxMessageBox(const wxString& message, const wxString& caption, long style,
898 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
899{
900 wxMessageDialog dialog(parent, message, caption, style);
901
902 int ans = dialog.ShowModal();
903 switch ( ans )
904 {
905 case wxID_OK:
906 return wxOK;
dfad0599
JS
907 case wxID_YES:
908 return wxYES;
dfad0599
JS
909 case wxID_NO:
910 return wxNO;
dfad0599
JS
911 case wxID_CANCEL:
912 return wxCANCEL;
dfad0599 913 }
3ca6a5f0
BP
914
915 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
916
917 return wxCANCEL;
dfad0599
JS
918}
919
1e6feb95
VZ
920#endif // wxUSE_MSGDLG
921
88ac883a 922#if wxUSE_TEXTDLG
1e6feb95 923
dfad0599
JS
924wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
925 const wxString& defaultValue, wxWindow *parent,
926 int x, int y, bool WXUNUSED(centre) )
927{
d2f50933 928 wxString str;
dfad0599
JS
929 wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y));
930 if (dialog.ShowModal() == wxID_OK)
d2f50933
VZ
931 {
932 str = dialog.GetValue();
933 }
934
935 return str;
dfad0599 936}
d2f50933
VZ
937
938wxString wxGetPasswordFromUser(const wxString& message,
939 const wxString& caption,
940 const wxString& defaultValue,
941 wxWindow *parent)
942{
943 wxString str;
944 wxTextEntryDialog dialog(parent, message, caption, defaultValue,
945 wxOK | wxCANCEL | wxTE_PASSWORD);
946 if ( dialog.ShowModal() == wxID_OK )
947 {
948 str = dialog.GetValue();
949 }
950
951 return str;
952}
953
88ac883a 954#endif // wxUSE_TEXTDLG
dfad0599 955
1e6feb95
VZ
956#if wxUSE_COLOURDLG
957
91b4c08d
VZ
958wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit)
959{
960 wxColourData data;
961 data.SetChooseFull(TRUE);
962 if ( colInit.Ok() )
963 {
964 data.SetColour((wxColour &)colInit); // const_cast
965 }
966
967 wxColour colRet;
968 wxColourDialog dialog(parent, &data);
969 if ( dialog.ShowModal() == wxID_OK )
970 {
971 colRet = dialog.GetColourData().GetColour();
972 }
973 //else: leave it invalid
974
975 return colRet;
976}
977
1e6feb95
VZ
978#endif // wxUSE_COLOURDLG
979
91b4c08d
VZ
980// ----------------------------------------------------------------------------
981// missing C RTL functions (FIXME shouldn't be here at all)
982// ----------------------------------------------------------------------------
983
469e1e5c 984#ifdef __MWERKS__
3f4a0c5b 985char *strdup(const char *s)
469e1e5c 986{
3f4a0c5b 987 return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ;
469e1e5c
SC
988}
989
3f4a0c5b 990int isascii( int c )
469e1e5c 991{
3f4a0c5b 992 return ( c >= 0 && c < 128 ) ;
469e1e5c 993}
e90c1d2a
VZ
994#endif // __MWERKS__
995
996// ----------------------------------------------------------------------------
cbc66a27 997// wxSafeYield and supporting functions
e90c1d2a
VZ
998// ----------------------------------------------------------------------------
999
1000void wxEnableTopLevelWindows(bool enable)
1001{
225fe9d6
VZ
1002 wxWindowList::Node *node;
1003 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1004 node->GetData()->Enable(enable);
e90c1d2a
VZ
1005}
1006
cd6ce4a9 1007wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
e90c1d2a 1008{
79f585d9
VZ
1009 // remember the top level windows which were already disabled, so that we
1010 // don't reenable them later
1011 m_winDisabled = NULL;
1012
225fe9d6
VZ
1013 wxWindowList::Node *node;
1014 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1015 {
1016 wxWindow *winTop = node->GetData();
79f585d9
VZ
1017 if ( winTop == winToSkip )
1018 continue;
1019
1020 if ( winTop->IsEnabled() )
1021 {
1022 winTop->Disable();
1023 }
1024 else
cd6ce4a9 1025 {
79f585d9
VZ
1026 if ( !m_winDisabled )
1027 {
1028 m_winDisabled = new wxWindowList;
1029 }
225fe9d6 1030
cd6ce4a9 1031 m_winDisabled->Append(winTop);
cd6ce4a9 1032 }
225fe9d6 1033 }
cd6ce4a9 1034}
225fe9d6 1035
cd6ce4a9
VZ
1036wxWindowDisabler::~wxWindowDisabler()
1037{
1038 wxWindowList::Node *node;
79f585d9 1039 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
225fe9d6 1040 {
79f585d9
VZ
1041 wxWindow *winTop = node->GetData();
1042 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
1043 {
1044 winTop->Enable();
1045 }
1046 //else: had been already disabled, don't reenable
225fe9d6
VZ
1047 }
1048
cd6ce4a9
VZ
1049 delete m_winDisabled;
1050}
1051
1052// Yield to other apps/messages and disable user input to all windows except
1053// the given one
1054bool wxSafeYield(wxWindow *win)
1055{
bc1dcfc1 1056 wxWindowDisabler wd(win);
cd6ce4a9
VZ
1057
1058 bool rc = wxYield();
1059
225fe9d6 1060 return rc;
e90c1d2a
VZ
1061}
1062
cbc66a27
VZ
1063// ----------------------------------------------------------------------------
1064// misc functions
1065// ----------------------------------------------------------------------------
1066
e90c1d2a
VZ
1067// Don't synthesize KeyUp events holding down a key and producing KeyDown
1068// events with autorepeat. On by default and always on in wxMSW. wxGTK version
1069// in utilsgtk.cpp.
1070#ifndef __WXGTK__
1071bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
1072{
225fe9d6 1073 return TRUE; // detectable auto-repeat is the only mode MSW supports
e90c1d2a
VZ
1074}
1075#endif // !wxGTK
1076
1077#endif // wxUSE_GUI
e2a6f233 1078
134677bd
VS
1079const wxChar *wxGetInstallPrefix()
1080{
1081 wxString prefix;
1082
1083 if ( wxGetEnv(wxT("WX_PREFIX"), &prefix) )
1084 return prefix.c_str();
1085
1086#ifdef wxINSTALL_PREFIX
1087 return wxT(wxINSTALL_PREFIX);
1088#else
1089 return wxT("");
1090#endif
1091}
1092
2c18f21d
VS
1093wxString wxGetDataDir()
1094{
1095 wxString format = wxGetInstallPrefix();
1096 format << wxFILE_SEP_PATH
1097 << wxT("share") << wxFILE_SEP_PATH
1098 << wxT("wx") << wxFILE_SEP_PATH
1099 << wxT("%i.%i");
1100 wxString dir;
1101 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
1102 return dir;
1103}
1104
134677bd 1105
0fb67cd1
VZ
1106// ----------------------------------------------------------------------------
1107// network and user id functions
1108// ----------------------------------------------------------------------------
1109
1110// Get Full RFC822 style email address
84fff0b3 1111bool wxGetEmailAddress(wxChar *address, int maxSize)
e2a6f233 1112{
0fb67cd1
VZ
1113 wxString email = wxGetEmailAddress();
1114 if ( !email )
e2a6f233 1115 return FALSE;
0fb67cd1 1116
84fff0b3 1117 wxStrncpy(address, email, maxSize - 1);
223d09f6 1118 address[maxSize - 1] = wxT('\0');
0fb67cd1
VZ
1119
1120 return TRUE;
e2a6f233
JS
1121}
1122
0fb67cd1 1123wxString wxGetEmailAddress()
e2a6f233 1124{
0fb67cd1
VZ
1125 wxString email;
1126
1f0500b3 1127 wxString host = wxGetFullHostName();
0fb67cd1 1128 if ( !!host )
e2a6f233 1129 {
0fb67cd1
VZ
1130 wxString user = wxGetUserId();
1131 if ( !!user )
1132 {
1f0500b3 1133 email << user << wxT('@') << host;
0fb67cd1 1134 }
e2a6f233 1135 }
0fb67cd1
VZ
1136
1137 return email;
1138}
1139
1140wxString wxGetUserId()
1141{
1142 static const int maxLoginLen = 256; // FIXME arbitrary number
1143
1144 wxString buf;
1145 bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen);
1146 buf.UngetWriteBuf();
1147
1148 if ( !ok )
1149 buf.Empty();
1150
1151 return buf;
1152}
1153
1154wxString wxGetUserName()
1155{
1156 static const int maxUserNameLen = 1024; // FIXME arbitrary number
1157
1158 wxString buf;
1159 bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen);
1160 buf.UngetWriteBuf();
1161
1162 if ( !ok )
1163 buf.Empty();
1164
1165 return buf;
e2a6f233
JS
1166}
1167
0fb67cd1 1168wxString wxGetHostName()
518b5d2f
VZ
1169{
1170 static const size_t hostnameSize = 257;
0fb67cd1
VZ
1171
1172 wxString buf;
518b5d2f
VZ
1173 bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1174
1175 buf.UngetWriteBuf();
1176
0fb67cd1
VZ
1177 if ( !ok )
1178 buf.Empty();
1179
1180 return buf;
518b5d2f
VZ
1181}
1182
96c5bd7f
KB
1183wxString wxGetFullHostName()
1184{
1185 static const size_t hostnameSize = 257;
1186
1187 wxString buf;
1188 bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1189
1190 buf.UngetWriteBuf();
1191
1192 if ( !ok )
1193 buf.Empty();
1194
1195 return buf;
1196}
1197
c51deffc
VZ
1198wxString wxGetHomeDir()
1199{
1200 wxString home;
1201 wxGetHomeDir(&home);
1202
1203 return home;
1204}
bc385ba9
VZ
1205
1206#if 0
1207
1208wxString wxGetCurrentDir()
1209{
1210 wxString dir;
1211 size_t len = 1024;
1212 bool ok;
1213 do
1214 {
1215 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
1216 dir.UngetWriteBuf();
1217
1218 if ( !ok )
1219 {
1220 if ( errno != ERANGE )
1221 {
1222 wxLogSysError(_T("Failed to get current directory"));
1223
1224 return wxEmptyString;
1225 }
1226 else
1227 {
1228 // buffer was too small, retry with a larger one
1229 len *= 2;
1230 }
1231 }
1232 //else: ok
1233 } while ( !ok );
1234
1235 return dir;
1236}
1237
1238#endif // 0
cd6ce4a9
VZ
1239
1240// ----------------------------------------------------------------------------
1241// wxExecute
1242// ----------------------------------------------------------------------------
1243
f6bcfd97
BP
1244// this is a private function because it hasn't a clean interface: the first
1245// array is passed by reference, the second by pointer - instead we have 2
1246// public versions of wxExecute() below
1247static long wxDoExecuteWithCapture(const wxString& command,
1248 wxArrayString& output,
1249 wxArrayString* error)
cd6ce4a9 1250{
669f7a11
JS
1251#ifdef __WIN16__
1252 wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16.");
f6bcfd97 1253
669f7a11 1254 return 0;
f6bcfd97 1255#else // !Win16
cd6ce4a9
VZ
1256 // create a wxProcess which will capture the output
1257 wxProcess *process = new wxProcess;
1258 process->Redirect();
1259
1260 long rc = wxExecute(command, TRUE /* sync */, process);
f6bcfd97
BP
1261
1262#if wxUSE_STREAMS
cd6ce4a9
VZ
1263 if ( rc != -1 )
1264 {
f6bcfd97
BP
1265 wxInputStream* is = process->GetInputStream();
1266 wxCHECK_MSG( is, -1, _T("if wxExecute() succeded, stream can't be NULL") );
1267 wxTextInputStream tis(*is);
1268
1269 wxTextInputStream *tes = NULL;
1270 wxInputStream *es = NULL;
1271 if ( error )
cd6ce4a9 1272 {
f6bcfd97
BP
1273 es = process->GetErrorStream();
1274
1275 wxCHECK_MSG( es, -1, _T("stderr can't be NULL") );
1276
1277 tes = new wxTextInputStream(*es);
1278 }
1279
1280 bool cont;
1281 do
1282 {
1283 cont = FALSE;
1284
1285 if ( !is->Eof() && is->IsOk() )
1286 {
1287 wxString line = tis.ReadLine();
1288 if ( is->LastError() )
1289 break;
1290
1291 cont = TRUE;
1292
1293 output.Add(line);
1294 }
1295
1296 if ( error && !es->Eof() && es->IsOk() )
1297 {
1298 wxString line = tes->ReadLine();
1299 if ( es->LastError() )
1300 break;
1301
1302 cont = TRUE;
cd6ce4a9 1303
f6bcfd97
BP
1304 error->Add(line);
1305 }
cd6ce4a9 1306 }
f6bcfd97
BP
1307 while ( cont );
1308
1309 delete tes;
cd6ce4a9 1310 }
f6bcfd97
BP
1311#endif // wxUSE_STREAMS
1312
1313 delete process;
cd6ce4a9
VZ
1314
1315 return rc;
f6bcfd97
BP
1316#endif // IO redirection supoprted
1317}
1318
1319long wxExecute(const wxString& command, wxArrayString& output)
1320{
1321 return wxDoExecuteWithCapture(command, output, NULL);
1322}
1323
1324long wxExecute(const wxString& command,
1325 wxArrayString& output,
1326 wxArrayString& error)
1327{
1328 return wxDoExecuteWithCapture(command, output, &error);
cd6ce4a9 1329}
21709999 1330
8461e4c2
VZ
1331// ----------------------------------------------------------------------------
1332// wxApp::Yield() wrappers for backwards compatibility
1333// ----------------------------------------------------------------------------
1334
1335bool wxYield()
1336{
1337#if wxUSE_GUI
1338 return wxTheApp && wxTheApp->Yield();
1339#else
1340 return FALSE;
1341#endif
1342}
1343
1344bool wxYieldIfNeeded()
1345{
1346#if wxUSE_GUI
1347 return wxTheApp && wxTheApp->Yield(TRUE);
1348#else
1349 return FALSE;
1350#endif
1351}
21709999 1352