]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
removed wxDialog::SetSize() which just called wxWindow::SetSize()
[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 #ifdef __GNUG__
13 #pragma implementation "utils.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/utils.h"
26 #include "wx/window.h"
27 #include "wx/menu.h"
28 #include "wx/frame.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textdlg.h"
31 #endif
32
33 #if wxUSE_IOSTREAMH
34 #include <iostream.h>
35 #include <fstream.h>
36 #else
37 #include <iostream>
38 #include <fstream>
39 # ifdef _MSC_VER
40 using namespace std;
41 # endif
42 #endif
43
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #if !defined(__WATCOMC__)
49 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
50 #include <errno.h>
51 #endif
52 #endif
53 #include <time.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56
57 // Pattern matching code.
58 // Yes, this path is deliberate (for Borland compilation)
59 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
60 #include "glob.inc"
61 #else
62 #include "../common/glob.inc"
63 #endif
64
65 #ifdef __WXMSW__
66 #include "windows.h"
67 #endif
68
69 #define _MAXPATHLEN 500
70
71 extern char *wxBuffer;
72
73 #ifdef __VMS__
74 // we have no strI functions under VMS, therefore I have implemented
75 // an inefficient but portable version: convert copies of strings to lowercase
76 // and then use the normal comparison
77 static void myLowerString(char *s)
78 {
79 while(*s){
80 if(isalpha(*s)) *s = (char)tolower(*s);
81 s++;
82 }
83 }
84
85 int strcasecmp(const char *str_1, const char *str_2)
86 {
87 char *temp1 = new char[strlen(str_1)+1];
88 char *temp2 = new char[strlen(str_2)+1];
89 strcpy(temp1,str_1);
90 strcpy(temp2,str_2);
91 myLowerString(temp1);
92 myLowerString(temp2);
93
94 int result = strcmp(temp1,temp2);
95 delete[] temp1;
96 delete[] temp2;
97
98 return(result);
99 }
100
101 int strncasecmp(const char *str_1, const char *str_2, size_t maxchar)
102 {
103 char *temp1 = new char[strlen(str_1)+1];
104 char *temp2 = new char[strlen(str_2)+1];
105 strcpy(temp1,str_1);
106 strcpy(temp2,str_2);
107 myLowerString(temp1);
108 myLowerString(temp2);
109
110 int result = strncmp(temp1,temp2,maxchar);
111 delete[] temp1;
112 delete[] temp2;
113
114 return(result);
115 }
116 #endif
117
118 #ifdef __WINDOWS__
119
120 #ifndef __GNUWIN32__
121 #define strcasecmp stricmp
122 #define strncasecmp strnicmp
123 #endif
124
125 #ifdef _MSC_VER
126 #pragma warning (disable : 4245)
127 #endif
128
129 #ifdef _MSC_VER
130 #pragma warning (default : 4245)
131 #endif
132
133 #else
134 // This declaration is missing in SunOS!
135 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
136 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
137 extern "C"
138 {
139 int strcasecmp (const char *, const char *);
140 int strncasecmp (const char *, const char *, size_t);
141 }
142 #endif
143 #endif /* __WXMSW__ */
144
145
146 char *
147 copystring (const char *s)
148 {
149 if (s == NULL) s = "";
150 size_t len = strlen (s) + 1;
151
152 char *news = new char[len];
153 memcpy (news, s, len); // Should be the fastest
154
155 return news;
156 }
157
158 // Id generation
159 static long wxCurrentId = 100;
160
161 long
162 wxNewId (void)
163 {
164 return wxCurrentId++;
165 }
166
167 long
168 wxGetCurrentId(void) { return wxCurrentId; }
169
170 void
171 wxRegisterId (long id)
172 {
173 if (id >= wxCurrentId)
174 wxCurrentId = id + 1;
175 }
176
177 void
178 StringToFloat (char *s, float *number)
179 {
180 if (s && *s && number)
181 *number = (float) strtod (s, (char **) NULL);
182 }
183
184 void
185 StringToDouble (char *s, double *number)
186 {
187 if (s && *s && number)
188 *number = strtod (s, (char **) NULL);
189 }
190
191 char *
192 FloatToString (float number, const char *fmt)
193 {
194 static char buf[256];
195
196 // sprintf (buf, "%.2f", number);
197 sprintf (buf, fmt, number);
198 return buf;
199 }
200
201 char *
202 DoubleToString (double number, const char *fmt)
203 {
204 static char buf[256];
205
206 sprintf (buf, fmt, number);
207 return buf;
208 }
209
210 void
211 StringToInt (char *s, int *number)
212 {
213 if (s && *s && number)
214 *number = (int) strtol (s, (char **) NULL, 10);
215 }
216
217 void
218 StringToLong (char *s, long *number)
219 {
220 if (s && *s && number)
221 *number = strtol (s, (char **) NULL, 10);
222 }
223
224 char *
225 IntToString (int number)
226 {
227 static char buf[20];
228
229 sprintf (buf, "%d", number);
230 return buf;
231 }
232
233 char *
234 LongToString (long number)
235 {
236 static char buf[20];
237
238 sprintf (buf, "%ld", number);
239 return buf;
240 }
241
242 // Array used in DecToHex conversion routine.
243 static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
244 'C', 'D', 'E', 'F' };
245
246 // Convert 2-digit hex number to decimal
247 int wxHexToDec(const wxString& buf)
248 {
249 int firstDigit, secondDigit;
250
251 if (buf.GetChar(0) >= 'A')
252 firstDigit = buf.GetChar(0) - 'A' + 10;
253 else
254 firstDigit = buf.GetChar(0) - '0';
255
256 if (buf.GetChar(1) >= 'A')
257 secondDigit = buf.GetChar(1) - 'A' + 10;
258 else
259 secondDigit = buf.GetChar(1) - '0';
260
261 return firstDigit * 16 + secondDigit;
262 }
263
264 // Convert decimal integer to 2-character hex string
265 void wxDecToHex(int dec, char *buf)
266 {
267 int firstDigit = (int)(dec/16.0);
268 int secondDigit = (int)(dec - (firstDigit*16.0));
269 buf[0] = hexArray[firstDigit];
270 buf[1] = hexArray[secondDigit];
271 buf[2] = 0;
272 }
273
274 // Convert decimal integer to 2-character hex string
275 wxString wxDecToHex(int dec)
276 {
277 char buf[3];
278 wxDecToHex(dec, buf);
279 return wxString(buf);
280 }
281
282 // Match a string INDEPENDENT OF CASE
283 bool
284 StringMatch (char *str1, char *str2, bool subString, bool exact)
285 {
286 if (str1 == NULL || str2 == NULL)
287 return FALSE;
288 if (str1 == str2)
289 return TRUE;
290
291 if (subString)
292 {
293 int len1 = strlen (str1);
294 int len2 = strlen (str2);
295 int i;
296
297 // Search for str1 in str2
298 // Slow .... but acceptable for short strings
299 for (i = 0; i <= len2 - len1; i++)
300 {
301 if (strncasecmp (str1, str2 + i, len1) == 0)
302 return TRUE;
303 }
304 }
305 else if (exact)
306 {
307 if (strcasecmp (str1, str2) == 0)
308 return TRUE;
309 }
310 else
311 {
312 int len1 = strlen (str1);
313 int len2 = strlen (str2);
314
315 if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0)
316 return TRUE;
317 }
318
319 return FALSE;
320 }
321
322 // Return the current date/time
323 // [volatile]
324 wxString wxNow( void )
325 {
326 time_t now = time((time_t *) NULL);
327 char *date = ctime(&now);
328 date[24] = '\0';
329 return wxString(date);
330 }
331
332 /* Get Full RFC822 style email address */
333 bool
334 wxGetEmailAddress (char *address, int maxSize)
335 {
336 char host[65];
337 char user[65];
338
339 if (wxGetHostName(host, 64) == FALSE)
340 return FALSE;
341 if (wxGetUserId(user, 64) == FALSE)
342 return FALSE;
343
344 char tmp[130];
345 strcpy(tmp, user);
346 strcat(tmp, "@");
347 strcat(tmp, host);
348
349 strncpy(address, tmp, maxSize - 1);
350 address[maxSize-1] = '\0';
351 return TRUE;
352 }
353
354 /*
355 * Strip out any menu codes
356 */
357
358 char *wxStripMenuCodes (char *in, char *out)
359 {
360 if (!in)
361 return (char *) NULL;
362
363 if (!out)
364 out = copystring(in);
365
366 char *tmpOut = out;
367
368 while (*in)
369 {
370 if (*in == '&')
371 {
372 // Check && -> &, &x -> x
373 if (*++in == '&')
374 *out++ = *in++;
375 }
376 else if (*in == '\t')
377 {
378 // Remove all stuff after \t in X mode, and let the stuff as is
379 // in Windows mode.
380 // Accelerators are handled in wx_item.cc for Motif, and are not
381 // YET supported in XView
382 break;
383 }
384 else
385 *out++ = *in++;
386 } // while
387
388 *out = '\0';
389
390 return tmpOut;
391 }
392
393 wxString wxStripMenuCodes(const wxString& str)
394 {
395 char *buf = new char[str.Length() + 1];
396 wxStripMenuCodes((char*) (const char*) str, buf);
397 wxString str1(buf);
398 delete[] buf;
399 return str1;
400 }
401
402 /*
403 * Window search functions
404 *
405 */
406
407 /*
408 * If parent is non-NULL, look through children for a label or title
409 * matching the specified string. If NULL, look through all top-level windows.
410 *
411 */
412
413 static wxWindow *wxFindWindowByLabel1 (const wxString& title, wxWindow * parent);
414
415 wxWindow *
416 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
417 {
418 if (parent)
419 {
420 return wxFindWindowByLabel1 (title, parent);
421 }
422 else
423 {
424 for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ())
425 {
426 wxWindow *win = (wxWindow *) node->Data ();
427 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
428 if (retwin)
429 return retwin;
430 } // for()
431
432 }
433 return (wxWindow *) NULL;
434 }
435
436 // Recursive
437 static wxWindow *
438 wxFindWindowByLabel1 (const wxString& title, wxWindow * parent)
439 {
440 if (parent)
441 {
442 if (parent->GetLabel() == title)
443 return parent;
444 }
445
446 if (parent)
447 {
448 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
449 {
450 wxWindow *win = (wxWindow *) node->Data ();
451 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
452 if (retwin)
453 return retwin;
454 } // for()
455
456 }
457
458 return (wxWindow *) NULL; // Not found
459
460 }
461
462 /*
463 * If parent is non-NULL, look through children for a name
464 * matching the specified string. If NULL, look through all top-level windows.
465 *
466 */
467
468 static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow * parent);
469
470 wxWindow *
471 wxFindWindowByName (const wxString& title, wxWindow * parent)
472 {
473 if (parent)
474 {
475 return wxFindWindowByName1 (title, parent);
476 }
477 else
478 {
479 for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ())
480 {
481 wxWindow *win = (wxWindow *) node->Data ();
482 wxWindow *retwin = wxFindWindowByName1 (title, win);
483 if (retwin)
484 return retwin;
485 } // for()
486
487 }
488 // Failed? Try by label instead.
489 return wxFindWindowByLabel(title, parent);
490 }
491
492 // Recursive
493 static wxWindow *
494 wxFindWindowByName1 (const wxString& title, wxWindow * parent)
495 {
496 if (parent)
497 {
498 if ( parent->GetName() == title )
499 return parent;
500 }
501
502 if (parent)
503 {
504 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
505 {
506 wxWindow *win = (wxWindow *) node->Data ();
507 wxWindow *retwin = wxFindWindowByName1 (title, win);
508 if (retwin)
509 return retwin;
510 } // for()
511
512 }
513
514 return (wxWindow *) NULL; // Not found
515
516 }
517
518 // Returns menu item id or -1 if none.
519 int
520 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
521 {
522 wxMenuBar *menuBar = frame->GetMenuBar ();
523 if (!menuBar)
524 return -1;
525 return menuBar->FindMenuItem (menuString, itemString);
526 }
527
528 /*
529 * wxDebugStreamBuf
530 */
531 #if !defined(_WINDLL)
532
533 wxDebugStreamBuf::wxDebugStreamBuf(void)
534 {
535 // <iostream> usage doesn't need this, and i have no idea how to simulate it.
536 #if wxUSE_IOSTREAMH
537 if (allocate())
538 setp(base(),ebuf());
539 #endif
540 }
541
542 int wxDebugStreamBuf::overflow(int WXUNUSED(i))
543 {
544 int len = pptr() - pbase();
545 char *txt = new char[len+1];
546 strncpy(txt, pbase(), len);
547 txt[len] = '\0';
548 #ifdef __WXMSW__
549 OutputDebugString((LPCSTR)txt);
550 #else
551 fprintf(stderr, txt);
552 #endif
553 setp(pbase(), epptr());
554 delete[] txt;
555 return EOF;
556 }
557
558 int wxDebugStreamBuf::sync(void)
559 {
560 int len = pptr() - pbase();
561 char *txt = new char[len+1];
562 strncpy(txt, pbase(), len);
563 txt[len] = '\0';
564 #ifdef __WXMSW__
565 OutputDebugString((LPCSTR)txt);
566 #else
567 fprintf(stderr, txt);
568 #endif
569 setp(pbase(), epptr());
570 delete[] txt;
571 return 0;
572 }
573
574 #endif
575
576 /*
577 On Fri, 21 Jul 1995, Paul Craven wrote:
578
579 > Is there a way to find the path of running program's executable? I can get
580 > my home directory, and the current directory, but I don't know how to get the
581 > executable directory.
582 >
583
584 The code below (warty as it is), does what you want on most Unix,
585 DOS, and Mac platforms (it's from the ALS Prolog main).
586
587 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
588 ||==== Voice: +1 (617)965-9191 Newton Centre,
589 || FAX: +1 (617)965-1636 MA 02159 USA
590 Email: ken@als.com WWW: http://www.als.com
591 ------------------------------------------------------------------------
592 */
593
594 // This code is commented out but it may be integrated with wxWin at
595 // a later date, after testing. Thanks Ken!
596 #if 0
597
598 /*--------------------------------------------------------------------*
599 | whereami is given a filename f in the form: whereami(argv[0])
600 | It returns the directory in which the executable file (containing
601 | this code [main.c] ) may be found. A dot will be returned to indicate
602 | the current directory.
603 *--------------------------------------------------------------------*/
604
605 static void
606 whereami(name)
607 char *name;
608 {
609 register char *cutoff = NULL; /* stifle -Wall */
610 register char *s;
611 register char *t;
612 int cc;
613 char ebuf[4096];
614
615 /*
616 * See if the file is accessible either through the current directory
617 * or through an absolute path.
618 */
619
620 if (access(name, R_OK) == 0) {
621
622 /*-------------------------------------------------------------*
623 * The file was accessible without any other work. But the current
624 * working directory might change on us, so if it was accessible
625 * through the cwd, then we should get it for later accesses.
626 *-------------------------------------------------------------*/
627
628 t = imagedir;
629 if (!absolute_pathname(name)) {
630 #if defined(DOS) || defined(__WIN32__)
631 int drive;
632 char *newrbuf;
633
634 newrbuf = imagedir;
635 #ifndef __DJGPP__
636 if (*(name + 1) == ':') {
637 if (*name >= 'a' && *name <= 'z')
638 drive = (int) (*name - 'a' + 1);
639 else
640 drive = (int) (*name - 'A' + 1);
641 *newrbuf++ = *name;
642 *newrbuf++ = *(name + 1);
643 *newrbuf++ = DIR_SEPARATOR;
644 }
645 else {
646 drive = 0;
647 *newrbuf++ = DIR_SEPARATOR;
648 }
649 if (getcwd(newrbuf, drive) == 0) { /* } */
650 #else
651 if (getcwd(newrbuf, 1024) == 0) { /* } */
652 #endif
653 #else /* DOS */
654 #ifdef HAVE_GETWD
655 if (getwd(imagedir) == 0) { /* } */
656 #else /* !HAVE_GETWD */
657 if (getcwd(imagedir, 1024) == 0) {
658 #endif /* !HAVE_GETWD */
659 #endif /* DOS */
660 fatal_error(FE_GETCWD, 0);
661 }
662 for (; *t; t++) /* Set t to end of buffer */
663 ;
664 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
665 * last char
666 */
667 cutoff = t - 1;
668 else {
669 cutoff = t; /* otherwise put one in */
670 *t++ = DIR_SEPARATOR;
671 }
672 }
673 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
674 else
675 (*t++ = DIR_SEPARATOR);
676 #endif
677
678 /*-------------------------------------------------------------*
679 * Copy the rest of the string and set the cutoff if it was not
680 * already set. If the first character of name is a slash, cutoff
681 * is not presently set but will be on the first iteration of the
682 * loop below.
683 *-------------------------------------------------------------*/
684
685 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
686 if (*s == DIR_SEPARATOR)
687 cutoff = t;
688 if (!(*t++ = *s++))
689 break;
690 }
691
692 }
693 else {
694
695 /*-------------------------------------------------------------*
696 * Get the path list from the environment. If the path list is
697 * inaccessible for any reason, leave with fatal error.
698 *-------------------------------------------------------------*/
699
700 #ifdef __MAC__
701 if ((s = getenv("Commands")) == (char *) 0)
702 #else
703 if ((s = getenv("PATH")) == (char *) 0)
704 #endif
705 fatal_error(FE_PATH, 0);
706
707 /*
708 * Copy path list into ebuf and set the source pointer to the
709 * beginning of this buffer.
710 */
711
712 strcpy(ebuf, s);
713 s = ebuf;
714
715 for (;;) {
716 t = imagedir;
717 while (*s && *s != PATH_SEPARATOR)
718 *t++ = *s++;
719 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
720 ; /* do nothing -- slash already is in place */
721 else
722 *t++ = DIR_SEPARATOR; /* put in the slash */
723 cutoff = t - 1; /* set cutoff */
724 strcpy(t, name);
725 if (access(imagedir, R_OK) == 0)
726 break;
727
728 if (*s)
729 s++; /* advance source pointer */
730 else
731 fatal_error(FE_INFND, 0);
732 }
733
734 }
735
736 /*-------------------------------------------------------------*
737 | At this point the full pathname should exist in imagedir and
738 | cutoff should be set to the final slash. We must now determine
739 | whether the file name is a symbolic link or not and chase it down
740 | if it is. Note that we reuse ebuf for getting the link.
741 *-------------------------------------------------------------*/
742
743 #ifdef HAVE_SYMLINK
744 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
745 ebuf[cc] = 0;
746 s = ebuf;
747 if (*s == DIR_SEPARATOR) {
748 t = imagedir;
749 }
750 else {
751 t = cutoff + 1;
752 }
753 for (;;) {
754 if (*s == DIR_SEPARATOR)
755 cutoff = t; /* mark the last slash seen */
756 if (!(*t++ = *s++)) /* copy the character */
757 break;
758 }
759 }
760
761 #endif /* HAVE_SYMLINK */
762
763 strcpy(imagename, cutoff + 1); /* keep the image name */
764 *(cutoff + 1) = 0; /* chop off the filename part */
765 }
766
767 #endif
768
769 /*
770 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
771 * since otherwise the generic code may be pulled in unnecessarily.
772 */
773
774 int wxMessageBox(const wxString& message, const wxString& caption, long style,
775 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
776 {
777 wxMessageDialog dialog(parent, message, caption, style);
778
779 int ans = dialog.ShowModal();
780 switch ( ans )
781 {
782 case wxID_OK:
783 return wxOK;
784 break;
785 case wxID_YES:
786 return wxYES;
787 break;
788 case wxID_NO:
789 return wxNO;
790 break;
791 default:
792 case wxID_CANCEL:
793 return wxCANCEL;
794 break;
795 }
796 return ans;
797 }
798
799 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
800 const wxString& defaultValue, wxWindow *parent,
801 int x, int y, bool WXUNUSED(centre) )
802 {
803 wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y));
804 if (dialog.ShowModal() == wxID_OK)
805 return dialog.GetValue();
806 else
807 return wxString("");
808 }
809