]>
git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "utils.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/window.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textdlg.h"
37 #if !defined(__WATCOMC__)
38 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
44 #include <sys/types.h>
52 // Pattern matching code.
53 // Yes, this path is deliberate (for Borland compilation)
54 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
57 #include "../common/glob.inc"
64 #define _MAXPATHLEN 500
66 extern wxChar
*wxBuffer
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 static wxWindow
*wxFindWindowByLabel1(const wxString
& title
, wxWindow
* parent
);
73 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
76 int strcasecmp(const char *str_1
, const char *str_2
)
80 c1
= tolower(*str_1
++);
81 c2
= tolower(*str_2
++);
82 } while ( c1
&& (c1
== c2
) );
87 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
93 c1
= tolower(*str_1
++);
94 c2
= tolower(*str_2
++);
106 // we have no strI functions under VMS, therefore I have implemented
107 // an inefficient but portable version: convert copies of strings to lowercase
108 // and then use the normal comparison
109 static void myLowerString(char *s
)
112 if(isalpha(*s
)) *s
= (char)tolower(*s
);
117 int strcasecmp(const char *str_1
, const char *str_2
)
119 char *temp1
= new char[strlen(str_1
)+1];
120 char *temp2
= new char[strlen(str_2
)+1];
123 myLowerString(temp1
);
124 myLowerString(temp2
);
126 int result
= strcmp(temp1
,temp2
);
133 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
135 char *temp1
= new char[strlen(str_1
)+1];
136 char *temp2
= new char[strlen(str_2
)+1];
139 myLowerString(temp1
);
140 myLowerString(temp2
);
142 int result
= strncmp(temp1
,temp2
,maxchar
);
154 #define strcasecmp stricmp
155 #define strncasecmp strnicmp
157 #define strcasecmp _stricmp
158 #define strncasecmp _strnicmp
165 #define strcasecmp stricmp
166 #define strncasecmp strnicmp
169 // This declaration is missing in SunOS!
170 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
171 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
174 int strcasecmp (const char *, const char *);
175 int strncasecmp (const char *, const char *, size_t);
178 #endif /* __WXMSW__ */
182 copystring (const wxChar
*s
)
184 if (s
== NULL
) s
= _T("");
185 size_t len
= wxStrlen (s
) + 1;
187 wxChar
*news
= new wxChar
[len
];
188 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
194 static long wxCurrentId
= 100;
199 return wxCurrentId
++;
203 wxGetCurrentId(void) { return wxCurrentId
; }
206 wxRegisterId (long id
)
208 if (id
>= wxCurrentId
)
209 wxCurrentId
= id
+ 1;
213 StringToFloat (wxChar
*s
, float *number
)
215 if (s
&& *s
&& number
)
216 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
220 StringToDouble (wxChar
*s
, double *number
)
222 if (s
&& *s
&& number
)
223 *number
= wxStrtod (s
, (wxChar
**) NULL
);
227 FloatToString (float number
, const wxChar
*fmt
)
229 static wxChar buf
[256];
231 // sprintf (buf, "%.2f", number);
232 wxSprintf (buf
, fmt
, number
);
237 DoubleToString (double number
, const wxChar
*fmt
)
239 static wxChar buf
[256];
241 wxSprintf (buf
, fmt
, number
);
246 StringToInt (wxChar
*s
, int *number
)
248 if (s
&& *s
&& number
)
249 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
253 StringToLong (wxChar
*s
, long *number
)
255 if (s
&& *s
&& number
)
256 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
260 IntToString (int number
)
262 static wxChar buf
[20];
264 wxSprintf (buf
, _T("%d"), number
);
269 LongToString (long number
)
271 static wxChar buf
[20];
273 wxSprintf (buf
, _T("%ld"), number
);
277 // Array used in DecToHex conversion routine.
278 static wxChar hexArray
[] = _T("0123456789ABCDEF");
280 // Convert 2-digit hex number to decimal
281 int wxHexToDec(const wxString
& buf
)
283 int firstDigit
, secondDigit
;
285 if (buf
.GetChar(0) >= _T('A'))
286 firstDigit
= buf
.GetChar(0) - _T('A') + 10;
288 firstDigit
= buf
.GetChar(0) - _T('0');
290 if (buf
.GetChar(1) >= _T('A'))
291 secondDigit
= buf
.GetChar(1) - _T('A') + 10;
293 secondDigit
= buf
.GetChar(1) - _T('0');
295 return firstDigit
* 16 + secondDigit
;
298 // Convert decimal integer to 2-character hex string
299 void wxDecToHex(int dec
, wxChar
*buf
)
301 int firstDigit
= (int)(dec
/16.0);
302 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
303 buf
[0] = hexArray
[firstDigit
];
304 buf
[1] = hexArray
[secondDigit
];
308 // Convert decimal integer to 2-character hex string
309 wxString
wxDecToHex(int dec
)
312 wxDecToHex(dec
, buf
);
313 return wxString(buf
);
316 // Match a string INDEPENDENT OF CASE
318 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
320 if (str1
== NULL
|| str2
== NULL
)
327 int len1
= strlen (str1
);
328 int len2
= strlen (str2
);
331 // Search for str1 in str2
332 // Slow .... but acceptable for short strings
333 for (i
= 0; i
<= len2
- len1
; i
++)
335 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
341 if (strcasecmp (str1
, str2
) == 0)
346 int len1
= strlen (str1
);
347 int len2
= strlen (str2
);
349 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
356 // Don't synthesize KeyUp events holding down a key and producing
357 // KeyDown events with autorepeat. On by default and always on
358 // on in wxMSW. wxGTK version in utilsgtk.cpp.
360 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
362 return TRUE
; // detectable auto-repeat is the only mode MSW supports
366 // Return the current date/time
368 wxString
wxNow( void )
370 time_t now
= time((time_t *) NULL
);
371 char *date
= ctime(&now
);
373 return wxString(date
);
377 * Strip out any menu codes
380 wxChar
*wxStripMenuCodes (wxChar
*in
, wxChar
*out
)
383 return (wxChar
*) NULL
;
386 out
= copystring(in
);
388 wxChar
*tmpOut
= out
;
394 // Check && -> &, &x -> x
395 if (*++in
== _T('&'))
398 else if (*in
== _T('\t'))
400 // Remove all stuff after \t in X mode, and let the stuff as is
402 // Accelerators are handled in wx_item.cc for Motif, and are not
403 // YET supported in XView
415 wxString
wxStripMenuCodes(const wxString
& str
)
417 wxChar
*buf
= new wxChar
[str
.Length() + 1];
418 wxStripMenuCodes(WXSTRINGCAST str
, buf
);
425 * Window search functions
430 * If parent is non-NULL, look through children for a label or title
431 * matching the specified string. If NULL, look through all top-level windows.
436 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
440 return wxFindWindowByLabel1(title
, parent
);
444 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
446 node
= node
->GetNext() )
448 wxWindow
*win
= node
->GetData();
449 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
455 return (wxWindow
*) NULL
;
460 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
464 if (parent
->GetLabel() == title
)
470 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
472 node
= node
->GetNext() )
474 wxWindow
*win
= (wxWindow
*)node
->GetData();
475 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
482 return (wxWindow
*) NULL
; // Not found
486 * If parent is non-NULL, look through children for a name
487 * matching the specified string. If NULL, look through all top-level windows.
492 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
496 return wxFindWindowByName1 (title
, parent
);
500 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
502 node
= node
->GetNext() )
504 wxWindow
*win
= node
->GetData();
505 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
512 // Failed? Try by label instead.
513 return wxFindWindowByLabel(title
, parent
);
518 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
522 if ( parent
->GetName() == title
)
528 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
530 wxWindow
*win
= (wxWindow
*) node
->Data ();
531 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
538 return (wxWindow
*) NULL
; // Not found
542 // Returns menu item id or -1 if none.
544 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
546 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
549 return menuBar
->FindMenuItem (menuString
, itemString
);
553 On Fri, 21 Jul 1995, Paul Craven wrote:
555 > Is there a way to find the path of running program's executable? I can get
556 > my home directory, and the current directory, but I don't know how to get the
557 > executable directory.
560 The code below (warty as it is), does what you want on most Unix,
561 DOS, and Mac platforms (it's from the ALS Prolog main).
563 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
564 ||==== Voice: +1 (617)965-9191 Newton Centre,
565 || FAX: +1 (617)965-1636 MA 02159 USA
566 Email: ken@als.com WWW: http://www.als.com
567 ------------------------------------------------------------------------
570 // This code is commented out but it may be integrated with wxWin at
571 // a later date, after testing. Thanks Ken!
574 /*--------------------------------------------------------------------*
575 | whereami is given a filename f in the form: whereami(argv[0])
576 | It returns the directory in which the executable file (containing
577 | this code [main.c] ) may be found. A dot will be returned to indicate
578 | the current directory.
579 *--------------------------------------------------------------------*/
585 register char *cutoff
= NULL
; /* stifle -Wall */
592 * See if the file is accessible either through the current directory
593 * or through an absolute path.
596 if (access(name
, R_OK
) == 0) {
598 /*-------------------------------------------------------------*
599 * The file was accessible without any other work. But the current
600 * working directory might change on us, so if it was accessible
601 * through the cwd, then we should get it for later accesses.
602 *-------------------------------------------------------------*/
605 if (!absolute_pathname(name
)) {
606 #if defined(DOS) || defined(__WIN32__)
612 if (*(name
+ 1) == ':') {
613 if (*name
>= 'a' && *name
<= 'z')
614 drive
= (int) (*name
- 'a' + 1);
616 drive
= (int) (*name
- 'A' + 1);
618 *newrbuf
++ = *(name
+ 1);
619 *newrbuf
++ = DIR_SEPARATOR
;
623 *newrbuf
++ = DIR_SEPARATOR
;
625 if (getcwd(newrbuf
, drive
) == 0) { /* } */
627 if (getcwd(newrbuf
, 1024) == 0) { /* } */
631 if (getwd(imagedir
) == 0) { /* } */
632 #else /* !HAVE_GETWD */
633 if (getcwd(imagedir
, 1024) == 0) {
634 #endif /* !HAVE_GETWD */
636 fatal_error(FE_GETCWD
, 0);
638 for (; *t
; t
++) /* Set t to end of buffer */
640 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
645 cutoff
= t
; /* otherwise put one in */
646 *t
++ = DIR_SEPARATOR
;
649 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
651 (*t
++ = DIR_SEPARATOR
);
654 /*-------------------------------------------------------------*
655 * Copy the rest of the string and set the cutoff if it was not
656 * already set. If the first character of name is a slash, cutoff
657 * is not presently set but will be on the first iteration of the
659 *-------------------------------------------------------------*/
661 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
662 if (*s
== DIR_SEPARATOR
)
671 /*-------------------------------------------------------------*
672 * Get the path list from the environment. If the path list is
673 * inaccessible for any reason, leave with fatal error.
674 *-------------------------------------------------------------*/
677 if ((s
= getenv("Commands")) == (char *) 0)
679 if ((s
= getenv("PATH")) == (char *) 0)
681 fatal_error(FE_PATH
, 0);
684 * Copy path list into ebuf and set the source pointer to the
685 * beginning of this buffer.
693 while (*s
&& *s
!= PATH_SEPARATOR
)
695 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
696 ; /* do nothing -- slash already is in place */
698 *t
++ = DIR_SEPARATOR
; /* put in the slash */
699 cutoff
= t
- 1; /* set cutoff */
701 if (access(imagedir
, R_OK
) == 0)
705 s
++; /* advance source pointer */
707 fatal_error(FE_INFND
, 0);
712 /*-------------------------------------------------------------*
713 | At this point the full pathname should exist in imagedir and
714 | cutoff should be set to the final slash. We must now determine
715 | whether the file name is a symbolic link or not and chase it down
716 | if it is. Note that we reuse ebuf for getting the link.
717 *-------------------------------------------------------------*/
720 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
723 if (*s
== DIR_SEPARATOR
) {
730 if (*s
== DIR_SEPARATOR
)
731 cutoff
= t
; /* mark the last slash seen */
732 if (!(*t
++ = *s
++)) /* copy the character */
737 #endif /* HAVE_SYMLINK */
739 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
740 *(cutoff
+ 1) = 0; /* chop off the filename part */
744 void wxEnableTopLevelWindows(bool enable
)
746 wxWindowList::Node
*node
;
747 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
748 node
->GetData()->Enable(enable
);
751 // Yield to other apps/messages and disable user input
752 bool wxSafeYield(wxWindow
*win
)
754 wxEnableTopLevelWindows(FALSE
);
755 // always enable ourselves
759 wxEnableTopLevelWindows(TRUE
);
764 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
765 * since otherwise the generic code may be pulled in unnecessarily.
768 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
769 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
771 wxMessageDialog
dialog(parent
, message
, caption
, style
);
773 int ans
= dialog
.ShowModal();
794 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
795 const wxString
& defaultValue
, wxWindow
*parent
,
796 int x
, int y
, bool WXUNUSED(centre
) )
798 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
799 if (dialog
.ShowModal() == wxID_OK
)
800 return dialog
.GetValue();
804 #endif // wxUSE_TEXTDLG
807 char *strdup(const char *s
)
809 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
814 return ( c
>= 0 && c
< 128 ) ;
818 // ----------------------------------------------------------------------------
819 // network and user id functions
820 // ----------------------------------------------------------------------------
822 // Get Full RFC822 style email address
823 bool wxGetEmailAddress(wxChar
*address
, int maxSize
)
825 wxString email
= wxGetEmailAddress();
829 wxStrncpy(address
, email
, maxSize
- 1);
830 address
[maxSize
- 1] = _T('\0');
835 wxString
wxGetEmailAddress()
839 wxString host
= wxGetHostName();
842 wxString user
= wxGetUserId();
845 wxString
email(user
);
846 email
<< _T('@') << host
;
853 wxString
wxGetUserId()
855 static const int maxLoginLen
= 256; // FIXME arbitrary number
858 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
867 wxString
wxGetUserName()
869 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
872 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
881 wxString
wxGetHostName()
883 static const size_t hostnameSize
= 257;
886 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
896 wxString
wxGetFullHostName()
898 static const size_t hostnameSize
= 257;
901 bool ok
= wxGetFullHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);