]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
fixed wxString::resize() which was completely broken
[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 (wxChar *s, float *number)
253 {
254 if (s && *s && number)
255 *number = (float) wxStrtod (s, (wxChar **) NULL);
256 }
257
258 void
259 StringToDouble (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 (wxChar *s, int *number)
285 {
286 if (s && *s && number)
287 *number = (int) wxStrtol (s, (wxChar **) NULL, 10);
288 }
289
290 void
291 StringToLong (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 (char *str1, 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(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
1091 // ----------------------------------------------------------------------------
1092 // network and user id functions
1093 // ----------------------------------------------------------------------------
1094
1095 // Get Full RFC822 style email address
1096 bool wxGetEmailAddress(wxChar *address, int maxSize)
1097 {
1098 wxString email = wxGetEmailAddress();
1099 if ( !email )
1100 return FALSE;
1101
1102 wxStrncpy(address, email, maxSize - 1);
1103 address[maxSize - 1] = wxT('\0');
1104
1105 return TRUE;
1106 }
1107
1108 wxString wxGetEmailAddress()
1109 {
1110 wxString email;
1111
1112 wxString host = wxGetFullHostName();
1113 if ( !!host )
1114 {
1115 wxString user = wxGetUserId();
1116 if ( !!user )
1117 {
1118 email << user << wxT('@') << host;
1119 }
1120 }
1121
1122 return email;
1123 }
1124
1125 wxString wxGetUserId()
1126 {
1127 static const int maxLoginLen = 256; // FIXME arbitrary number
1128
1129 wxString buf;
1130 bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen);
1131 buf.UngetWriteBuf();
1132
1133 if ( !ok )
1134 buf.Empty();
1135
1136 return buf;
1137 }
1138
1139 wxString wxGetUserName()
1140 {
1141 static const int maxUserNameLen = 1024; // FIXME arbitrary number
1142
1143 wxString buf;
1144 bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen);
1145 buf.UngetWriteBuf();
1146
1147 if ( !ok )
1148 buf.Empty();
1149
1150 return buf;
1151 }
1152
1153 wxString wxGetHostName()
1154 {
1155 static const size_t hostnameSize = 257;
1156
1157 wxString buf;
1158 bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1159
1160 buf.UngetWriteBuf();
1161
1162 if ( !ok )
1163 buf.Empty();
1164
1165 return buf;
1166 }
1167
1168 wxString wxGetFullHostName()
1169 {
1170 static const size_t hostnameSize = 257;
1171
1172 wxString buf;
1173 bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1174
1175 buf.UngetWriteBuf();
1176
1177 if ( !ok )
1178 buf.Empty();
1179
1180 return buf;
1181 }
1182
1183 wxString wxGetHomeDir()
1184 {
1185 wxString home;
1186 wxGetHomeDir(&home);
1187
1188 return home;
1189 }
1190
1191 #if 0
1192
1193 wxString wxGetCurrentDir()
1194 {
1195 wxString dir;
1196 size_t len = 1024;
1197 bool ok;
1198 do
1199 {
1200 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
1201 dir.UngetWriteBuf();
1202
1203 if ( !ok )
1204 {
1205 if ( errno != ERANGE )
1206 {
1207 wxLogSysError(_T("Failed to get current directory"));
1208
1209 return wxEmptyString;
1210 }
1211 else
1212 {
1213 // buffer was too small, retry with a larger one
1214 len *= 2;
1215 }
1216 }
1217 //else: ok
1218 } while ( !ok );
1219
1220 return dir;
1221 }
1222
1223 #endif // 0
1224
1225 // ----------------------------------------------------------------------------
1226 // wxExecute
1227 // ----------------------------------------------------------------------------
1228
1229 // this is a private function because it hasn't a clean interface: the first
1230 // array is passed by reference, the second by pointer - instead we have 2
1231 // public versions of wxExecute() below
1232 static long wxDoExecuteWithCapture(const wxString& command,
1233 wxArrayString& output,
1234 wxArrayString* error)
1235 {
1236 #ifdef __WIN16__
1237 wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16.");
1238
1239 return 0;
1240 #else // !Win16
1241 // create a wxProcess which will capture the output
1242 wxProcess *process = new wxProcess;
1243 process->Redirect();
1244
1245 long rc = wxExecute(command, TRUE /* sync */, process);
1246
1247 #if wxUSE_STREAMS
1248 if ( rc != -1 )
1249 {
1250 wxInputStream* is = process->GetInputStream();
1251 wxCHECK_MSG( is, -1, _T("if wxExecute() succeded, stream can't be NULL") );
1252 wxTextInputStream tis(*is);
1253
1254 wxTextInputStream *tes = NULL;
1255 wxInputStream *es = NULL;
1256 if ( error )
1257 {
1258 es = process->GetErrorStream();
1259
1260 wxCHECK_MSG( es, -1, _T("stderr can't be NULL") );
1261
1262 tes = new wxTextInputStream(*es);
1263 }
1264
1265 bool cont;
1266 do
1267 {
1268 cont = FALSE;
1269
1270 if ( !is->Eof() && is->IsOk() )
1271 {
1272 wxString line = tis.ReadLine();
1273 if ( is->LastError() )
1274 break;
1275
1276 cont = TRUE;
1277
1278 output.Add(line);
1279 }
1280
1281 if ( error && !es->Eof() && es->IsOk() )
1282 {
1283 wxString line = tes->ReadLine();
1284 if ( es->LastError() )
1285 break;
1286
1287 cont = TRUE;
1288
1289 error->Add(line);
1290 }
1291 }
1292 while ( cont );
1293
1294 delete tes;
1295 }
1296 #endif // wxUSE_STREAMS
1297
1298 delete process;
1299
1300 return rc;
1301 #endif // IO redirection supoprted
1302 }
1303
1304 long wxExecute(const wxString& command, wxArrayString& output)
1305 {
1306 return wxDoExecuteWithCapture(command, output, NULL);
1307 }
1308
1309 long wxExecute(const wxString& command,
1310 wxArrayString& output,
1311 wxArrayString& error)
1312 {
1313 return wxDoExecuteWithCapture(command, output, &error);
1314 }
1315
1316 // ----------------------------------------------------------------------------
1317 // wxApp::Yield() wrappers for backwards compatibility
1318 // ----------------------------------------------------------------------------
1319
1320 bool wxYield()
1321 {
1322 #if wxUSE_GUI
1323 return wxTheApp && wxTheApp->Yield();
1324 #else
1325 return FALSE;
1326 #endif
1327 }
1328
1329 bool wxYieldIfNeeded()
1330 {
1331 #if wxUSE_GUI
1332 return wxTheApp && wxTheApp->Yield(TRUE);
1333 #else
1334 return FALSE;
1335 #endif
1336 }
1337