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