]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
1. small fix in wxDirDialog: SHBrowseForFolder() doesn't like '/'s
[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 #include <ctype.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #if !defined(__WATCOMC__)
57 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
58 #include <errno.h>
59 #endif
60 #endif
61
62 #include <time.h>
63
64 #ifndef __MWERKS__
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #endif
68
69 #ifdef __SALFORDC__
70 #include <clib.h>
71 #endif
72
73 #ifdef __WXMSW__
74 #include "windows.h"
75 #endif
76
77 // ----------------------------------------------------------------------------
78 // function protoypes
79 // ----------------------------------------------------------------------------
80
81 #if wxUSE_GUI
82 static wxWindow *wxFindWindowByLabel1(const wxString& title, wxWindow *parent);
83 static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow *parent);
84 #endif // wxUSE_GUI
85
86 // ============================================================================
87 // implementation
88 // ============================================================================
89
90 // ----------------------------------------------------------------------------
91 // string functions
92 // ----------------------------------------------------------------------------
93
94 #ifdef __WXMAC__
95 int strcasecmp(const char *str_1, const char *str_2)
96 {
97 register char c1, c2;
98 do {
99 c1 = tolower(*str_1++);
100 c2 = tolower(*str_2++);
101 } while ( c1 && (c1 == c2) );
102
103 return c1 - c2;
104 }
105
106 int strncasecmp(const char *str_1, const char *str_2, size_t maxchar)
107 {
108
109 register char c1, c2;
110 while( maxchar--)
111 {
112 c1 = tolower(*str_1++);
113 c2 = tolower(*str_2++);
114
115 if ( !c1 || c1!=c2 )
116 return c1 - c2;
117
118 } ;
119
120 return 0 ;
121
122 }
123 #endif // wxMAC
124
125 #if defined( __VMS__ ) && ( __VMS_VER < 70000000 )
126 // we have no strI functions under VMS, therefore I have implemented
127 // an inefficient but portable version: convert copies of strings to lowercase
128 // and then use the normal comparison
129 static void myLowerString(char *s)
130 {
131 while(*s){
132 if(isalpha(*s)) *s = (char)tolower(*s);
133 s++;
134 }
135 }
136
137 int strcasecmp(const char *str_1, const char *str_2)
138 {
139 char *temp1 = new char[strlen(str_1)+1];
140 char *temp2 = new char[strlen(str_2)+1];
141 strcpy(temp1,str_1);
142 strcpy(temp2,str_2);
143 myLowerString(temp1);
144 myLowerString(temp2);
145
146 int result = wxStrcmp(temp1,temp2);
147 delete[] temp1;
148 delete[] temp2;
149
150 return(result);
151 }
152
153 int strncasecmp(const char *str_1, const char *str_2, size_t maxchar)
154 {
155 char *temp1 = new char[strlen(str_1)+1];
156 char *temp2 = new char[strlen(str_2)+1];
157 strcpy(temp1,str_1);
158 strcpy(temp2,str_2);
159 myLowerString(temp1);
160 myLowerString(temp2);
161
162 int result = strncmp(temp1,temp2,maxchar);
163 delete[] temp1;
164 delete[] temp2;
165
166 return(result);
167 }
168 #endif // __VMS__
169
170 #ifdef __WINDOWS__
171
172 #ifndef __GNUWIN32__
173 #ifndef __MWERKS__
174 #define strcasecmp stricmp
175 #define strncasecmp strnicmp
176 #else
177 #define strcasecmp _stricmp
178 #define strncasecmp _strnicmp
179 #endif
180 #endif
181
182 #else
183
184 #ifdef __EMX__
185 #define strcasecmp stricmp
186 #define strncasecmp strnicmp
187 #endif
188
189 // This declaration is missing in SunOS!
190 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
191 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
192 extern "C"
193 {
194 int strcasecmp (const char *, const char *);
195 int strncasecmp (const char *, const char *, size_t);
196 }
197 #endif
198 #endif /* __WXMSW__ */
199
200 #ifdef __WXPM__
201 #define strcasecmp stricmp
202 #define strncasecmp strnicmp
203 #endif
204
205 wxChar *
206 copystring (const wxChar *s)
207 {
208 if (s == NULL) s = wxT("");
209 size_t len = wxStrlen (s) + 1;
210
211 wxChar *news = new wxChar[len];
212 memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest
213
214 return news;
215 }
216
217 // Id generation
218 static long wxCurrentId = 100;
219
220 long
221 wxNewId (void)
222 {
223 return wxCurrentId++;
224 }
225
226 long
227 wxGetCurrentId(void) { return wxCurrentId; }
228
229 void
230 wxRegisterId (long id)
231 {
232 if (id >= wxCurrentId)
233 wxCurrentId = id + 1;
234 }
235
236 void
237 StringToFloat (wxChar *s, float *number)
238 {
239 if (s && *s && number)
240 *number = (float) wxStrtod (s, (wxChar **) NULL);
241 }
242
243 void
244 StringToDouble (wxChar *s, double *number)
245 {
246 if (s && *s && number)
247 *number = wxStrtod (s, (wxChar **) NULL);
248 }
249
250 wxChar *
251 FloatToString (float number, const wxChar *fmt)
252 {
253 static wxChar buf[256];
254
255 // sprintf (buf, "%.2f", number);
256 wxSprintf (buf, fmt, number);
257 return buf;
258 }
259
260 wxChar *
261 DoubleToString (double number, const wxChar *fmt)
262 {
263 static wxChar buf[256];
264
265 wxSprintf (buf, fmt, number);
266 return buf;
267 }
268
269 void
270 StringToInt (wxChar *s, int *number)
271 {
272 if (s && *s && number)
273 *number = (int) wxStrtol (s, (wxChar **) NULL, 10);
274 }
275
276 void
277 StringToLong (wxChar *s, long *number)
278 {
279 if (s && *s && number)
280 *number = wxStrtol (s, (wxChar **) NULL, 10);
281 }
282
283 wxChar *
284 IntToString (int number)
285 {
286 static wxChar buf[20];
287
288 wxSprintf (buf, wxT("%d"), number);
289 return buf;
290 }
291
292 wxChar *
293 LongToString (long number)
294 {
295 static wxChar buf[20];
296
297 wxSprintf (buf, wxT("%ld"), number);
298 return buf;
299 }
300
301 // Array used in DecToHex conversion routine.
302 static wxChar hexArray[] = wxT("0123456789ABCDEF");
303
304 // Convert 2-digit hex number to decimal
305 int wxHexToDec(const wxString& buf)
306 {
307 int firstDigit, secondDigit;
308
309 if (buf.GetChar(0) >= wxT('A'))
310 firstDigit = buf.GetChar(0) - wxT('A') + 10;
311 else
312 firstDigit = buf.GetChar(0) - wxT('0');
313
314 if (buf.GetChar(1) >= wxT('A'))
315 secondDigit = buf.GetChar(1) - wxT('A') + 10;
316 else
317 secondDigit = buf.GetChar(1) - wxT('0');
318
319 return firstDigit * 16 + secondDigit;
320 }
321
322 // Convert decimal integer to 2-character hex string
323 void wxDecToHex(int dec, wxChar *buf)
324 {
325 int firstDigit = (int)(dec/16.0);
326 int secondDigit = (int)(dec - (firstDigit*16.0));
327 buf[0] = hexArray[firstDigit];
328 buf[1] = hexArray[secondDigit];
329 buf[2] = 0;
330 }
331
332 // Convert decimal integer to 2-character hex string
333 wxString wxDecToHex(int dec)
334 {
335 wxChar buf[3];
336 wxDecToHex(dec, buf);
337 return wxString(buf);
338 }
339
340 // Match a string INDEPENDENT OF CASE
341 bool
342 StringMatch (char *str1, char *str2, bool subString, bool exact)
343 {
344 if (str1 == NULL || str2 == NULL)
345 return FALSE;
346 if (str1 == str2)
347 return TRUE;
348
349 if (subString)
350 {
351 int len1 = strlen (str1);
352 int len2 = strlen (str2);
353 int i;
354
355 // Search for str1 in str2
356 // Slow .... but acceptable for short strings
357 for (i = 0; i <= len2 - len1; i++)
358 {
359 if (strncasecmp (str1, str2 + i, len1) == 0)
360 return TRUE;
361 }
362 }
363 else if (exact)
364 {
365 if (strcasecmp (str1, str2) == 0)
366 return TRUE;
367 }
368 else
369 {
370 int len1 = strlen (str1);
371 int len2 = strlen (str2);
372
373 if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0)
374 return TRUE;
375 }
376
377 return FALSE;
378 }
379
380 // Return the current date/time
381 // [volatile]
382 wxString wxNow()
383 {
384 time_t now = time((time_t *) NULL);
385 char *date = ctime(&now);
386 date[24] = '\0';
387 return wxString(date);
388 }
389
390 #if wxUSE_GUI
391
392 // ----------------------------------------------------------------------------
393 // Menu accelerators related functions
394 // ----------------------------------------------------------------------------
395
396 wxChar *wxStripMenuCodes (wxChar *in, wxChar *out)
397 {
398 if (!in)
399 return (wxChar *) NULL;
400
401 if (!out)
402 out = copystring(in);
403
404 wxChar *tmpOut = out;
405
406 while (*in)
407 {
408 if (*in == wxT('&'))
409 {
410 // Check && -> &, &x -> x
411 if (*++in == wxT('&'))
412 *out++ = *in++;
413 }
414 else if (*in == wxT('\t'))
415 {
416 // Remove all stuff after \t in X mode, and let the stuff as is
417 // in Windows mode.
418 // Accelerators are handled in wx_item.cc for Motif, and are not
419 // YET supported in XView
420 break;
421 }
422 else
423 *out++ = *in++;
424 } // while
425
426 *out = wxT('\0');
427
428 return tmpOut;
429 }
430
431 wxString wxStripMenuCodes(const wxString& str)
432 {
433 wxChar *buf = new wxChar[str.Length() + 1];
434 wxStripMenuCodes(WXSTRINGCAST str, buf);
435 wxString str1(buf);
436 delete[] buf;
437 return str1;
438 }
439
440 #if wxUSE_ACCEL
441
442 // return wxAcceleratorEntry for the given menu string or NULL if none
443 // specified
444 wxAcceleratorEntry *wxGetAccelFromString(const wxString& label)
445 {
446 // check for accelerators: they are given after '\t'
447 int posTab = label.Find(wxT('\t'));
448 if ( posTab != wxNOT_FOUND ) {
449 // parse the accelerator string
450 int keyCode = 0;
451 int accelFlags = wxACCEL_NORMAL;
452 wxString current;
453 for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) {
454 if ( (label[n] == '+') || (label[n] == '-') ) {
455 if ( current == _("ctrl") )
456 accelFlags |= wxACCEL_CTRL;
457 else if ( current == _("alt") )
458 accelFlags |= wxACCEL_ALT;
459 else if ( current == _("shift") )
460 accelFlags |= wxACCEL_SHIFT;
461 else {
462 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
463 current.c_str());
464 }
465
466 current.Empty();
467 }
468 else {
469 current += wxTolower(label[n]);
470 }
471 }
472
473 if ( current.IsEmpty() ) {
474 wxLogDebug(wxT("No accel key found, accel string ignored."));
475 }
476 else {
477 if ( current.Len() == 1 ) {
478 // it's a letter
479 keyCode = wxToupper(current[0U]);
480 }
481 else {
482 // is it a function key?
483 if ( current[0U] == 'f' && isdigit(current[1U]) &&
484 (current.Len() == 2 ||
485 (current.Len() == 3 && isdigit(current[2U]))) ) {
486 int n;
487 wxSscanf(current.c_str() + 1, wxT("%d"), &n);
488
489 keyCode = WXK_F1 + n - 1;
490 }
491 else {
492 #if 0 // this is not supported by GTK+, apparently
493 // several special cases
494 current.MakeUpper();
495 if ( current == wxT("DEL") ) {
496 keyCode = VK_DELETE;
497 }
498 else if ( current == wxT("PGUP") ) {
499 keyCode = VK_PRIOR;
500 }
501 else if ( current == wxT("PGDN") ) {
502 keyCode = VK_NEXT;
503 }
504 else
505 #endif // 0
506 {
507 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
508 "string ignored."), current.c_str());
509 }
510 }
511 }
512 }
513
514 if ( keyCode ) {
515 // we do have something
516 return new wxAcceleratorEntry(accelFlags, keyCode);
517 }
518 }
519
520 return (wxAcceleratorEntry *)NULL;
521 }
522
523 #endif // wxUSE_ACCEL
524
525 // ----------------------------------------------------------------------------
526 // Window search functions
527 // ----------------------------------------------------------------------------
528
529 /*
530 * If parent is non-NULL, look through children for a label or title
531 * matching the specified string. If NULL, look through all top-level windows.
532 *
533 */
534
535 wxWindow *
536 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
537 {
538 if (parent)
539 {
540 return wxFindWindowByLabel1(title, parent);
541 }
542 else
543 {
544 for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst();
545 node;
546 node = node->GetNext() )
547 {
548 wxWindow *win = node->GetData();
549 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
550 if (retwin)
551 return retwin;
552 } // for()
553
554 }
555 return (wxWindow *) NULL;
556 }
557
558 // Recursive
559 static wxWindow *
560 wxFindWindowByLabel1 (const wxString& title, wxWindow * parent)
561 {
562 if (parent)
563 {
564 if (parent->GetLabel() == title)
565 return parent;
566 }
567
568 if (parent)
569 {
570 for ( wxWindowList::Node * node = parent->GetChildren().GetFirst();
571 node;
572 node = node->GetNext() )
573 {
574 wxWindow *win = (wxWindow *)node->GetData();
575 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
576 if (retwin)
577 return retwin;
578 }
579
580 }
581
582 return (wxWindow *) NULL; // Not found
583 }
584
585 /*
586 * If parent is non-NULL, look through children for a name
587 * matching the specified string. If NULL, look through all top-level windows.
588 *
589 */
590
591 wxWindow *
592 wxFindWindowByName (const wxString& title, wxWindow * parent)
593 {
594 if (parent)
595 {
596 return wxFindWindowByName1 (title, parent);
597 }
598 else
599 {
600 for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst();
601 node;
602 node = node->GetNext() )
603 {
604 wxWindow *win = node->GetData();
605 wxWindow *retwin = wxFindWindowByName1 (title, win);
606 if (retwin)
607 return retwin;
608 }
609
610 }
611
612 // Failed? Try by label instead.
613 return wxFindWindowByLabel(title, parent);
614 }
615
616 // Recursive
617 static wxWindow *
618 wxFindWindowByName1 (const wxString& title, wxWindow * parent)
619 {
620 if (parent)
621 {
622 if ( parent->GetName() == title )
623 return parent;
624 }
625
626 if (parent)
627 {
628 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
629 {
630 wxWindow *win = (wxWindow *) node->Data ();
631 wxWindow *retwin = wxFindWindowByName1 (title, win);
632 if (retwin)
633 return retwin;
634 } // for()
635
636 }
637
638 return (wxWindow *) NULL; // Not found
639
640 }
641
642 // Returns menu item id or -1 if none.
643 int
644 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
645 {
646 wxMenuBar *menuBar = frame->GetMenuBar ();
647 if (!menuBar)
648 return -1;
649 return menuBar->FindMenuItem (menuString, itemString);
650 }
651
652 #endif // wxUSE_GUI
653
654 /*
655 On Fri, 21 Jul 1995, Paul Craven wrote:
656
657 > Is there a way to find the path of running program's executable? I can get
658 > my home directory, and the current directory, but I don't know how to get the
659 > executable directory.
660 >
661
662 The code below (warty as it is), does what you want on most Unix,
663 DOS, and Mac platforms (it's from the ALS Prolog main).
664
665 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
666 ||==== Voice: +1 (617)965-9191 Newton Centre,
667 || FAX: +1 (617)965-1636 MA 02159 USA
668 Email: ken@als.com WWW: http://www.als.com
669 ------------------------------------------------------------------------
670 */
671
672 // This code is commented out but it may be integrated with wxWin at
673 // a later date, after testing. Thanks Ken!
674 #if 0
675
676 /*--------------------------------------------------------------------*
677 | whereami is given a filename f in the form: whereami(argv[0])
678 | It returns the directory in which the executable file (containing
679 | this code [main.c] ) may be found. A dot will be returned to indicate
680 | the current directory.
681 *--------------------------------------------------------------------*/
682
683 static void
684 whereami(name)
685 char *name;
686 {
687 register char *cutoff = NULL; /* stifle -Wall */
688 register char *s;
689 register char *t;
690 int cc;
691 char ebuf[4096];
692
693 /*
694 * See if the file is accessible either through the current directory
695 * or through an absolute path.
696 */
697
698 if (access(name, R_OK) == 0) {
699
700 /*-------------------------------------------------------------*
701 * The file was accessible without any other work. But the current
702 * working directory might change on us, so if it was accessible
703 * through the cwd, then we should get it for later accesses.
704 *-------------------------------------------------------------*/
705
706 t = imagedir;
707 if (!absolute_pathname(name)) {
708 #if defined(DOS) || defined(__WIN32__)
709 int drive;
710 char *newrbuf;
711
712 newrbuf = imagedir;
713 #ifndef __DJGPP__
714 if (*(name + 1) == ':') {
715 if (*name >= 'a' && *name <= 'z')
716 drive = (int) (*name - 'a' + 1);
717 else
718 drive = (int) (*name - 'A' + 1);
719 *newrbuf++ = *name;
720 *newrbuf++ = *(name + 1);
721 *newrbuf++ = DIR_SEPARATOR;
722 }
723 else {
724 drive = 0;
725 *newrbuf++ = DIR_SEPARATOR;
726 }
727 if (getcwd(newrbuf, drive) == 0) { /* } */
728 #else
729 if (getcwd(newrbuf, 1024) == 0) { /* } */
730 #endif
731 #else /* DOS */
732 #ifdef HAVE_GETWD
733 if (getwd(imagedir) == 0) { /* } */
734 #else /* !HAVE_GETWD */
735 if (getcwd(imagedir, 1024) == 0) {
736 #endif /* !HAVE_GETWD */
737 #endif /* DOS */
738 fatal_error(FE_GETCWD, 0);
739 }
740 for (; *t; t++) /* Set t to end of buffer */
741 ;
742 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
743 * last char
744 */
745 cutoff = t - 1;
746 else {
747 cutoff = t; /* otherwise put one in */
748 *t++ = DIR_SEPARATOR;
749 }
750 }
751 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
752 else
753 (*t++ = DIR_SEPARATOR);
754 #endif
755
756 /*-------------------------------------------------------------*
757 * Copy the rest of the string and set the cutoff if it was not
758 * already set. If the first character of name is a slash, cutoff
759 * is not presently set but will be on the first iteration of the
760 * loop below.
761 *-------------------------------------------------------------*/
762
763 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
764 if (*s == DIR_SEPARATOR)
765 cutoff = t;
766 if (!(*t++ = *s++))
767 break;
768 }
769
770 }
771 else {
772
773 /*-------------------------------------------------------------*
774 * Get the path list from the environment. If the path list is
775 * inaccessible for any reason, leave with fatal error.
776 *-------------------------------------------------------------*/
777
778 #ifdef __MAC__
779 if ((s = getenv("Commands")) == (char *) 0)
780 #else
781 if ((s = getenv("PATH")) == (char *) 0)
782 #endif
783 fatal_error(FE_PATH, 0);
784
785 /*
786 * Copy path list into ebuf and set the source pointer to the
787 * beginning of this buffer.
788 */
789
790 strcpy(ebuf, s);
791 s = ebuf;
792
793 for (;;) {
794 t = imagedir;
795 while (*s && *s != PATH_SEPARATOR)
796 *t++ = *s++;
797 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
798 ; /* do nothing -- slash already is in place */
799 else
800 *t++ = DIR_SEPARATOR; /* put in the slash */
801 cutoff = t - 1; /* set cutoff */
802 strcpy(t, name);
803 if (access(imagedir, R_OK) == 0)
804 break;
805
806 if (*s)
807 s++; /* advance source pointer */
808 else
809 fatal_error(FE_INFND, 0);
810 }
811
812 }
813
814 /*-------------------------------------------------------------*
815 | At this point the full pathname should exist in imagedir and
816 | cutoff should be set to the final slash. We must now determine
817 | whether the file name is a symbolic link or not and chase it down
818 | if it is. Note that we reuse ebuf for getting the link.
819 *-------------------------------------------------------------*/
820
821 #ifdef HAVE_SYMLINK
822 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
823 ebuf[cc] = 0;
824 s = ebuf;
825 if (*s == DIR_SEPARATOR) {
826 t = imagedir;
827 }
828 else {
829 t = cutoff + 1;
830 }
831 for (;;) {
832 if (*s == DIR_SEPARATOR)
833 cutoff = t; /* mark the last slash seen */
834 if (!(*t++ = *s++)) /* copy the character */
835 break;
836 }
837 }
838
839 #endif /* HAVE_SYMLINK */
840
841 strcpy(imagename, cutoff + 1); /* keep the image name */
842 *(cutoff + 1) = 0; /* chop off the filename part */
843 }
844
845 #endif
846
847 #if wxUSE_GUI
848
849 // ----------------------------------------------------------------------------
850 // GUI helpers
851 // ----------------------------------------------------------------------------
852
853 /*
854 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
855 * since otherwise the generic code may be pulled in unnecessarily.
856 */
857
858 int wxMessageBox(const wxString& message, const wxString& caption, long style,
859 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
860 {
861 wxMessageDialog dialog(parent, message, caption, style);
862
863 int ans = dialog.ShowModal();
864 switch ( ans )
865 {
866 case wxID_OK:
867 return wxOK;
868 case wxID_YES:
869 return wxYES;
870 case wxID_NO:
871 return wxNO;
872 default:
873 case wxID_CANCEL:
874 return wxCANCEL;
875 }
876 }
877
878 #if wxUSE_TEXTDLG
879 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
880 const wxString& defaultValue, wxWindow *parent,
881 int x, int y, bool WXUNUSED(centre) )
882 {
883 wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y));
884 if (dialog.ShowModal() == wxID_OK)
885 return dialog.GetValue();
886 else
887 return wxString("");
888 }
889 #endif // wxUSE_TEXTDLG
890
891 #ifdef __MWERKS__
892 char *strdup(const char *s)
893 {
894 return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ;
895 }
896
897 int isascii( int c )
898 {
899 return ( c >= 0 && c < 128 ) ;
900 }
901 #endif // __MWERKS__
902
903 // ----------------------------------------------------------------------------
904 // misc functions
905 // ----------------------------------------------------------------------------
906
907 void wxEnableTopLevelWindows(bool enable)
908 {
909 wxWindowList::Node *node;
910 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
911 node->GetData()->Enable(enable);
912 }
913
914 // Yield to other apps/messages and disable user input
915 bool wxSafeYield(wxWindow *win)
916 {
917 wxEnableTopLevelWindows(FALSE);
918 // always enable ourselves
919 if ( win )
920 win->Enable(TRUE);
921 bool rc = wxYield();
922 wxEnableTopLevelWindows(TRUE);
923 return rc;
924 }
925
926 // Don't synthesize KeyUp events holding down a key and producing KeyDown
927 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
928 // in utilsgtk.cpp.
929 #ifndef __WXGTK__
930 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
931 {
932 return TRUE; // detectable auto-repeat is the only mode MSW supports
933 }
934 #endif // !wxGTK
935
936 #endif // wxUSE_GUI
937
938 // ----------------------------------------------------------------------------
939 // network and user id functions
940 // ----------------------------------------------------------------------------
941
942 // Get Full RFC822 style email address
943 bool wxGetEmailAddress(wxChar *address, int maxSize)
944 {
945 wxString email = wxGetEmailAddress();
946 if ( !email )
947 return FALSE;
948
949 wxStrncpy(address, email, maxSize - 1);
950 address[maxSize - 1] = wxT('\0');
951
952 return TRUE;
953 }
954
955 wxString wxGetEmailAddress()
956 {
957 wxString email;
958
959 wxString host = wxGetHostName();
960 if ( !!host )
961 {
962 wxString user = wxGetUserId();
963 if ( !!user )
964 {
965 wxString email(user);
966 email << wxT('@') << host;
967 }
968 }
969
970 return email;
971 }
972
973 wxString wxGetUserId()
974 {
975 static const int maxLoginLen = 256; // FIXME arbitrary number
976
977 wxString buf;
978 bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen);
979 buf.UngetWriteBuf();
980
981 if ( !ok )
982 buf.Empty();
983
984 return buf;
985 }
986
987 wxString wxGetUserName()
988 {
989 static const int maxUserNameLen = 1024; // FIXME arbitrary number
990
991 wxString buf;
992 bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen);
993 buf.UngetWriteBuf();
994
995 if ( !ok )
996 buf.Empty();
997
998 return buf;
999 }
1000
1001 wxString wxGetHostName()
1002 {
1003 static const size_t hostnameSize = 257;
1004
1005 wxString buf;
1006 bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1007
1008 buf.UngetWriteBuf();
1009
1010 if ( !ok )
1011 buf.Empty();
1012
1013 return buf;
1014 }
1015
1016 wxString wxGetFullHostName()
1017 {
1018 static const size_t hostnameSize = 257;
1019
1020 wxString buf;
1021 bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1022
1023 buf.UngetWriteBuf();
1024
1025 if ( !ok )
1026 buf.Empty();
1027
1028 return buf;
1029 }
1030
1031 wxString wxGetHomeDir()
1032 {
1033 wxString home;
1034 wxGetHomeDir(&home);
1035
1036 return home;
1037 }
1038
1039 #if 0
1040
1041 wxString wxGetCurrentDir()
1042 {
1043 wxString dir;
1044 size_t len = 1024;
1045 bool ok;
1046 do
1047 {
1048 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
1049 dir.UngetWriteBuf();
1050
1051 if ( !ok )
1052 {
1053 if ( errno != ERANGE )
1054 {
1055 wxLogSysError(_T("Failed to get current directory"));
1056
1057 return wxEmptyString;
1058 }
1059 else
1060 {
1061 // buffer was too small, retry with a larger one
1062 len *= 2;
1063 }
1064 }
1065 //else: ok
1066 } while ( !ok );
1067
1068 return dir;
1069 }
1070
1071 #endif // 0