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