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