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