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