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