]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
Removed an include from windres flags since it can't cope with a long command line
[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 #if 0 // this is not supported by GTK+, apparently
502 // several special cases
503 current.MakeUpper();
504 if ( current == wxT("DEL") ) {
505 keyCode = VK_DELETE;
506 }
507 else if ( current == wxT("PGUP") ) {
508 keyCode = VK_PRIOR;
509 }
510 else if ( current == wxT("PGDN") ) {
511 keyCode = VK_NEXT;
512 }
513 else
514 #endif // 0
515 {
516 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
517 "string ignored."), current.c_str());
518 }
519 }
520 }
521 }
522
523 if ( keyCode ) {
524 // we do have something
525 return new wxAcceleratorEntry(accelFlags, keyCode);
526 }
527 }
528
529 return (wxAcceleratorEntry *)NULL;
530 }
531
532 #endif // wxUSE_ACCEL
533
534 // ----------------------------------------------------------------------------
535 // Window search functions
536 // ----------------------------------------------------------------------------
537
538 /*
539 * If parent is non-NULL, look through children for a label or title
540 * matching the specified string. If NULL, look through all top-level windows.
541 *
542 */
543
544 wxWindow *
545 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
546 {
547 if (parent)
548 {
549 return wxFindWindowByLabel1(title, parent);
550 }
551 else
552 {
553 for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst();
554 node;
555 node = node->GetNext() )
556 {
557 wxWindow *win = node->GetData();
558 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
559 if (retwin)
560 return retwin;
561 } // for()
562
563 }
564 return (wxWindow *) NULL;
565 }
566
567 // Recursive
568 static wxWindow *
569 wxFindWindowByLabel1 (const wxString& title, wxWindow * parent)
570 {
571 if (parent)
572 {
573 if (parent->GetLabel() == title)
574 return parent;
575 }
576
577 if (parent)
578 {
579 for ( wxWindowList::Node * node = parent->GetChildren().GetFirst();
580 node;
581 node = node->GetNext() )
582 {
583 wxWindow *win = (wxWindow *)node->GetData();
584 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
585 if (retwin)
586 return retwin;
587 }
588
589 }
590
591 return (wxWindow *) NULL; // Not found
592 }
593
594 /*
595 * If parent is non-NULL, look through children for a name
596 * matching the specified string. If NULL, look through all top-level windows.
597 *
598 */
599
600 wxWindow *
601 wxFindWindowByName (const wxString& title, wxWindow * parent)
602 {
603 if (parent)
604 {
605 return wxFindWindowByName1 (title, parent);
606 }
607 else
608 {
609 for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst();
610 node;
611 node = node->GetNext() )
612 {
613 wxWindow *win = node->GetData();
614 wxWindow *retwin = wxFindWindowByName1 (title, win);
615 if (retwin)
616 return retwin;
617 }
618
619 }
620
621 // Failed? Try by label instead.
622 return wxFindWindowByLabel(title, parent);
623 }
624
625 // Recursive
626 static wxWindow *
627 wxFindWindowByName1 (const wxString& title, wxWindow * parent)
628 {
629 if (parent)
630 {
631 if ( parent->GetName() == title )
632 return parent;
633 }
634
635 if (parent)
636 {
637 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
638 {
639 wxWindow *win = (wxWindow *) node->Data ();
640 wxWindow *retwin = wxFindWindowByName1 (title, win);
641 if (retwin)
642 return retwin;
643 } // for()
644
645 }
646
647 return (wxWindow *) NULL; // Not found
648
649 }
650
651 // Returns menu item id or -1 if none.
652 int
653 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
654 {
655 wxMenuBar *menuBar = frame->GetMenuBar ();
656 if (!menuBar)
657 return -1;
658 return menuBar->FindMenuItem (menuString, itemString);
659 }
660
661 #endif // wxUSE_GUI
662
663 /*
664 On Fri, 21 Jul 1995, Paul Craven wrote:
665
666 > Is there a way to find the path of running program's executable? I can get
667 > my home directory, and the current directory, but I don't know how to get the
668 > executable directory.
669 >
670
671 The code below (warty as it is), does what you want on most Unix,
672 DOS, and Mac platforms (it's from the ALS Prolog main).
673
674 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
675 ||==== Voice: +1 (617)965-9191 Newton Centre,
676 || FAX: +1 (617)965-1636 MA 02159 USA
677 Email: ken@als.com WWW: http://www.als.com
678 ------------------------------------------------------------------------
679 */
680
681 // This code is commented out but it may be integrated with wxWin at
682 // a later date, after testing. Thanks Ken!
683 #if 0
684
685 /*--------------------------------------------------------------------*
686 | whereami is given a filename f in the form: whereami(argv[0])
687 | It returns the directory in which the executable file (containing
688 | this code [main.c] ) may be found. A dot will be returned to indicate
689 | the current directory.
690 *--------------------------------------------------------------------*/
691
692 static void
693 whereami(name)
694 char *name;
695 {
696 register char *cutoff = NULL; /* stifle -Wall */
697 register char *s;
698 register char *t;
699 int cc;
700 char ebuf[4096];
701
702 /*
703 * See if the file is accessible either through the current directory
704 * or through an absolute path.
705 */
706
707 if (access(name, R_OK) == 0) {
708
709 /*-------------------------------------------------------------*
710 * The file was accessible without any other work. But the current
711 * working directory might change on us, so if it was accessible
712 * through the cwd, then we should get it for later accesses.
713 *-------------------------------------------------------------*/
714
715 t = imagedir;
716 if (!absolute_pathname(name)) {
717 #if defined(DOS) || defined(__WIN32__)
718 int drive;
719 char *newrbuf;
720
721 newrbuf = imagedir;
722 #ifndef __DJGPP__
723 if (*(name + 1) == ':') {
724 if (*name >= 'a' && *name <= 'z')
725 drive = (int) (*name - 'a' + 1);
726 else
727 drive = (int) (*name - 'A' + 1);
728 *newrbuf++ = *name;
729 *newrbuf++ = *(name + 1);
730 *newrbuf++ = DIR_SEPARATOR;
731 }
732 else {
733 drive = 0;
734 *newrbuf++ = DIR_SEPARATOR;
735 }
736 if (getcwd(newrbuf, drive) == 0) { /* } */
737 #else
738 if (getcwd(newrbuf, 1024) == 0) { /* } */
739 #endif
740 #else /* DOS */
741 #ifdef HAVE_GETWD
742 if (getwd(imagedir) == 0) { /* } */
743 #else /* !HAVE_GETWD */
744 if (getcwd(imagedir, 1024) == 0) {
745 #endif /* !HAVE_GETWD */
746 #endif /* DOS */
747 fatal_error(FE_GETCWD, 0);
748 }
749 for (; *t; t++) /* Set t to end of buffer */
750 ;
751 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
752 * last char
753 */
754 cutoff = t - 1;
755 else {
756 cutoff = t; /* otherwise put one in */
757 *t++ = DIR_SEPARATOR;
758 }
759 }
760 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
761 else
762 (*t++ = DIR_SEPARATOR);
763 #endif
764
765 /*-------------------------------------------------------------*
766 * Copy the rest of the string and set the cutoff if it was not
767 * already set. If the first character of name is a slash, cutoff
768 * is not presently set but will be on the first iteration of the
769 * loop below.
770 *-------------------------------------------------------------*/
771
772 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
773 if (*s == DIR_SEPARATOR)
774 cutoff = t;
775 if (!(*t++ = *s++))
776 break;
777 }
778
779 }
780 else {
781
782 /*-------------------------------------------------------------*
783 * Get the path list from the environment. If the path list is
784 * inaccessible for any reason, leave with fatal error.
785 *-------------------------------------------------------------*/
786
787 #ifdef __MAC__
788 if ((s = getenv("Commands")) == (char *) 0)
789 #else
790 if ((s = getenv("PATH")) == (char *) 0)
791 #endif
792 fatal_error(FE_PATH, 0);
793
794 /*
795 * Copy path list into ebuf and set the source pointer to the
796 * beginning of this buffer.
797 */
798
799 strcpy(ebuf, s);
800 s = ebuf;
801
802 for (;;) {
803 t = imagedir;
804 while (*s && *s != PATH_SEPARATOR)
805 *t++ = *s++;
806 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
807 ; /* do nothing -- slash already is in place */
808 else
809 *t++ = DIR_SEPARATOR; /* put in the slash */
810 cutoff = t - 1; /* set cutoff */
811 strcpy(t, name);
812 if (access(imagedir, R_OK) == 0)
813 break;
814
815 if (*s)
816 s++; /* advance source pointer */
817 else
818 fatal_error(FE_INFND, 0);
819 }
820
821 }
822
823 /*-------------------------------------------------------------*
824 | At this point the full pathname should exist in imagedir and
825 | cutoff should be set to the final slash. We must now determine
826 | whether the file name is a symbolic link or not and chase it down
827 | if it is. Note that we reuse ebuf for getting the link.
828 *-------------------------------------------------------------*/
829
830 #ifdef HAVE_SYMLINK
831 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
832 ebuf[cc] = 0;
833 s = ebuf;
834 if (*s == DIR_SEPARATOR) {
835 t = imagedir;
836 }
837 else {
838 t = cutoff + 1;
839 }
840 for (;;) {
841 if (*s == DIR_SEPARATOR)
842 cutoff = t; /* mark the last slash seen */
843 if (!(*t++ = *s++)) /* copy the character */
844 break;
845 }
846 }
847
848 #endif /* HAVE_SYMLINK */
849
850 strcpy(imagename, cutoff + 1); /* keep the image name */
851 *(cutoff + 1) = 0; /* chop off the filename part */
852 }
853
854 #endif
855
856 #if wxUSE_GUI
857
858 // ----------------------------------------------------------------------------
859 // GUI helpers
860 // ----------------------------------------------------------------------------
861
862 /*
863 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
864 * since otherwise the generic code may be pulled in unnecessarily.
865 */
866
867 int wxMessageBox(const wxString& message, const wxString& caption, long style,
868 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
869 {
870 wxMessageDialog dialog(parent, message, caption, style);
871
872 int ans = dialog.ShowModal();
873 switch ( ans )
874 {
875 case wxID_OK:
876 return wxOK;
877 case wxID_YES:
878 return wxYES;
879 case wxID_NO:
880 return wxNO;
881 default:
882 case wxID_CANCEL:
883 return wxCANCEL;
884 }
885 }
886
887 #if wxUSE_TEXTDLG
888 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
889 const wxString& defaultValue, wxWindow *parent,
890 int x, int y, bool WXUNUSED(centre) )
891 {
892 wxString str;
893 wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y));
894 if (dialog.ShowModal() == wxID_OK)
895 {
896 str = dialog.GetValue();
897 }
898
899 return str;
900 }
901
902 wxString wxGetPasswordFromUser(const wxString& message,
903 const wxString& caption,
904 const wxString& defaultValue,
905 wxWindow *parent)
906 {
907 wxString str;
908 wxTextEntryDialog dialog(parent, message, caption, defaultValue,
909 wxOK | wxCANCEL | wxTE_PASSWORD);
910 if ( dialog.ShowModal() == wxID_OK )
911 {
912 str = dialog.GetValue();
913 }
914
915 return str;
916 }
917
918 #endif // wxUSE_TEXTDLG
919
920 wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit)
921 {
922 wxColourData data;
923 data.SetChooseFull(TRUE);
924 if ( colInit.Ok() )
925 {
926 data.SetColour((wxColour &)colInit); // const_cast
927 }
928
929 wxColour colRet;
930 wxColourDialog dialog(parent, &data);
931 if ( dialog.ShowModal() == wxID_OK )
932 {
933 colRet = dialog.GetColourData().GetColour();
934 }
935 //else: leave it invalid
936
937 return colRet;
938 }
939
940 // ----------------------------------------------------------------------------
941 // missing C RTL functions (FIXME shouldn't be here at all)
942 // ----------------------------------------------------------------------------
943
944 #ifdef __MWERKS__
945 char *strdup(const char *s)
946 {
947 return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ;
948 }
949
950 int isascii( int c )
951 {
952 return ( c >= 0 && c < 128 ) ;
953 }
954 #endif // __MWERKS__
955
956 // ----------------------------------------------------------------------------
957 // wxSafeYield and supporting functions
958 // ----------------------------------------------------------------------------
959
960 void wxEnableTopLevelWindows(bool enable)
961 {
962 wxWindowList::Node *node;
963 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
964 node->GetData()->Enable(enable);
965 }
966
967 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
968 {
969 #ifdef __WXMSW__
970 #ifdef __WIN32__
971 // and the top level window too
972 HWND hwndFG = ::GetForegroundWindow();
973 m_winTop = hwndFG ? wxFindWinFromHandle((WXHWND)hwndFG) : (wxWindow *)NULL;
974 #else
975 HWND hwndFG = ::GetTopWindow(0);
976 m_winTop = hwndFG ? wxFindWinFromHandle((WXHWND)hwndFG) : (wxWindow *)NULL;
977 #endif
978 #endif // MSW
979
980 // remember the top level windows which were already disabled, so that we
981 // don't reenable them later
982 m_winDisabled = NULL;
983
984 wxWindowList::Node *node;
985 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
986 {
987 wxWindow *winTop = node->GetData();
988 if ( winTop == winToSkip )
989 continue;
990
991 if ( winTop->IsEnabled() )
992 {
993 winTop->Disable();
994 }
995 else
996 {
997 if ( !m_winDisabled )
998 {
999 m_winDisabled = new wxWindowList;
1000 }
1001
1002 m_winDisabled->Append(winTop);
1003 }
1004 }
1005 }
1006
1007 wxWindowDisabler::~wxWindowDisabler()
1008 {
1009 wxWindowList::Node *node;
1010 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
1011 {
1012 wxWindow *winTop = node->GetData();
1013 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
1014 {
1015 winTop->Enable();
1016 }
1017 //else: had been already disabled, don't reenable
1018 }
1019
1020 delete m_winDisabled;
1021
1022 #ifdef __WXMSW__
1023 #ifdef __WIN32__
1024 if ( m_winTop )
1025 {
1026 if ( !::SetForegroundWindow(GetHwndOf(m_winTop)) )
1027 {
1028 wxLogLastError("SetForegroundWindow");
1029 }
1030 }
1031 #else
1032 if ( m_winTop )
1033 {
1034 // 16-bit SetForegroundWindow() replacement
1035 RECT reWin;
1036 GetWindowRect((HWND) m_winTop, &reWin);
1037 SetWindowPos ((HWND) m_winTop, HWND_TOP,
1038 reWin.left, reWin.top,
1039 reWin.right - reWin.left, reWin.bottom,
1040 SWP_SHOWWINDOW);
1041 }
1042 #endif
1043 #endif // MSW
1044 }
1045
1046 // Yield to other apps/messages and disable user input to all windows except
1047 // the given one
1048 bool wxSafeYield(wxWindow *win)
1049 {
1050 wxWindowDisabler wd;
1051
1052 bool rc = wxYield();
1053
1054 return rc;
1055 }
1056
1057 // ----------------------------------------------------------------------------
1058 // misc functions
1059 // ----------------------------------------------------------------------------
1060
1061 // Don't synthesize KeyUp events holding down a key and producing KeyDown
1062 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
1063 // in utilsgtk.cpp.
1064 #ifndef __WXGTK__
1065 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
1066 {
1067 return TRUE; // detectable auto-repeat is the only mode MSW supports
1068 }
1069 #endif // !wxGTK
1070
1071 #endif // wxUSE_GUI
1072
1073 // ----------------------------------------------------------------------------
1074 // network and user id functions
1075 // ----------------------------------------------------------------------------
1076
1077 // Get Full RFC822 style email address
1078 bool wxGetEmailAddress(wxChar *address, int maxSize)
1079 {
1080 wxString email = wxGetEmailAddress();
1081 if ( !email )
1082 return FALSE;
1083
1084 wxStrncpy(address, email, maxSize - 1);
1085 address[maxSize - 1] = wxT('\0');
1086
1087 return TRUE;
1088 }
1089
1090 wxString wxGetEmailAddress()
1091 {
1092 wxString email;
1093
1094 wxString host = wxGetHostName();
1095 if ( !!host )
1096 {
1097 wxString user = wxGetUserId();
1098 if ( !!user )
1099 {
1100 wxString email(user);
1101 email << wxT('@') << host;
1102 }
1103 }
1104
1105 return email;
1106 }
1107
1108 wxString wxGetUserId()
1109 {
1110 static const int maxLoginLen = 256; // FIXME arbitrary number
1111
1112 wxString buf;
1113 bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen);
1114 buf.UngetWriteBuf();
1115
1116 if ( !ok )
1117 buf.Empty();
1118
1119 return buf;
1120 }
1121
1122 wxString wxGetUserName()
1123 {
1124 static const int maxUserNameLen = 1024; // FIXME arbitrary number
1125
1126 wxString buf;
1127 bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen);
1128 buf.UngetWriteBuf();
1129
1130 if ( !ok )
1131 buf.Empty();
1132
1133 return buf;
1134 }
1135
1136 wxString wxGetHostName()
1137 {
1138 static const size_t hostnameSize = 257;
1139
1140 wxString buf;
1141 bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1142
1143 buf.UngetWriteBuf();
1144
1145 if ( !ok )
1146 buf.Empty();
1147
1148 return buf;
1149 }
1150
1151 wxString wxGetFullHostName()
1152 {
1153 static const size_t hostnameSize = 257;
1154
1155 wxString buf;
1156 bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1157
1158 buf.UngetWriteBuf();
1159
1160 if ( !ok )
1161 buf.Empty();
1162
1163 return buf;
1164 }
1165
1166 wxString wxGetHomeDir()
1167 {
1168 wxString home;
1169 wxGetHomeDir(&home);
1170
1171 return home;
1172 }
1173
1174 #if 0
1175
1176 wxString wxGetCurrentDir()
1177 {
1178 wxString dir;
1179 size_t len = 1024;
1180 bool ok;
1181 do
1182 {
1183 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
1184 dir.UngetWriteBuf();
1185
1186 if ( !ok )
1187 {
1188 if ( errno != ERANGE )
1189 {
1190 wxLogSysError(_T("Failed to get current directory"));
1191
1192 return wxEmptyString;
1193 }
1194 else
1195 {
1196 // buffer was too small, retry with a larger one
1197 len *= 2;
1198 }
1199 }
1200 //else: ok
1201 } while ( !ok );
1202
1203 return dir;
1204 }
1205
1206 #endif // 0
1207
1208 // ----------------------------------------------------------------------------
1209 // wxExecute
1210 // ----------------------------------------------------------------------------
1211
1212 long wxExecute(const wxString& command, wxArrayString& output)
1213 {
1214 #ifdef __WIN16__
1215 wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16.");
1216 return 0;
1217 #else
1218 // create a wxProcess which will capture the output
1219 wxProcess *process = new wxProcess;
1220 process->Redirect();
1221
1222 long rc = wxExecute(command, TRUE /* sync */, process);
1223 if ( rc != -1 )
1224 {
1225 wxInputStream& is = *process->GetInputStream();
1226 wxTextInputStream tis(is);
1227 while ( !is.Eof() && is.IsOk() )
1228 {
1229 wxString line = tis.ReadLine();
1230 if ( is.LastError() )
1231 break;
1232
1233 output.Add(line);
1234 }
1235 }
1236
1237 return rc;
1238 #endif
1239 }