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