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