]>
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"
33 #include "wx/ioswrap.h"
45 #if !defined(__WATCOMC__)
46 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
52 #include <sys/types.h>
60 // Pattern matching code.
61 // Yes, this path is deliberate (for Borland compilation)
62 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
65 #include "../common/glob.inc"
72 #define _MAXPATHLEN 500
74 extern wxChar
*wxBuffer
;
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 static wxWindow
*wxFindWindowByLabel1(const wxString
& title
, wxWindow
* parent
);
81 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
84 int strcasecmp(const char *str_1
, const char *str_2
)
88 c1
= tolower(*str_1
++);
89 c2
= tolower(*str_2
++);
90 } while ( c1
&& (c1
== c2
) );
95 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
101 c1
= tolower(*str_1
++);
102 c2
= tolower(*str_2
++);
114 // we have no strI functions under VMS, therefore I have implemented
115 // an inefficient but portable version: convert copies of strings to lowercase
116 // and then use the normal comparison
117 static void myLowerString(char *s
)
120 if(isalpha(*s
)) *s
= (char)tolower(*s
);
125 int strcasecmp(const char *str_1
, const char *str_2
)
127 char *temp1
= new char[strlen(str_1
)+1];
128 char *temp2
= new char[strlen(str_2
)+1];
131 myLowerString(temp1
);
132 myLowerString(temp2
);
134 int result
= strcmp(temp1
,temp2
);
141 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
143 char *temp1
= new char[strlen(str_1
)+1];
144 char *temp2
= new char[strlen(str_2
)+1];
147 myLowerString(temp1
);
148 myLowerString(temp2
);
150 int result
= strncmp(temp1
,temp2
,maxchar
);
162 #define strcasecmp stricmp
163 #define strncasecmp strnicmp
165 #define strcasecmp _stricmp
166 #define strncasecmp _strnicmp
171 // This declaration is missing in SunOS!
172 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
173 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
176 int strcasecmp (const char *, const char *);
177 int strncasecmp (const char *, const char *, size_t);
180 #endif /* __WXMSW__ */
184 copystring (const wxChar
*s
)
186 if (s
== NULL
) s
= _T("");
187 size_t len
= wxStrlen (s
) + 1;
189 wxChar
*news
= new wxChar
[len
];
190 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
196 static long wxCurrentId
= 100;
201 return wxCurrentId
++;
205 wxGetCurrentId(void) { return wxCurrentId
; }
208 wxRegisterId (long id
)
210 if (id
>= wxCurrentId
)
211 wxCurrentId
= id
+ 1;
215 StringToFloat (wxChar
*s
, float *number
)
217 if (s
&& *s
&& number
)
218 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
222 StringToDouble (wxChar
*s
, double *number
)
224 if (s
&& *s
&& number
)
225 *number
= wxStrtod (s
, (wxChar
**) NULL
);
229 FloatToString (float number
, const wxChar
*fmt
)
231 static wxChar buf
[256];
233 // sprintf (buf, "%.2f", number);
234 wxSprintf (buf
, fmt
, number
);
239 DoubleToString (double number
, const wxChar
*fmt
)
241 static wxChar buf
[256];
243 wxSprintf (buf
, fmt
, number
);
248 StringToInt (wxChar
*s
, int *number
)
250 if (s
&& *s
&& number
)
251 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
255 StringToLong (wxChar
*s
, long *number
)
257 if (s
&& *s
&& number
)
258 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
262 IntToString (int number
)
264 static wxChar buf
[20];
266 wxSprintf (buf
, _T("%d"), number
);
271 LongToString (long number
)
273 static wxChar buf
[20];
275 wxSprintf (buf
, _T("%ld"), number
);
279 // Array used in DecToHex conversion routine.
280 static wxChar hexArray
[] = _T("0123456789ABCDEF");
282 // Convert 2-digit hex number to decimal
283 int wxHexToDec(const wxString
& buf
)
285 int firstDigit
, secondDigit
;
287 if (buf
.GetChar(0) >= _T('A'))
288 firstDigit
= buf
.GetChar(0) - _T('A') + 10;
290 firstDigit
= buf
.GetChar(0) - _T('0');
292 if (buf
.GetChar(1) >= _T('A'))
293 secondDigit
= buf
.GetChar(1) - _T('A') + 10;
295 secondDigit
= buf
.GetChar(1) - _T('0');
297 return firstDigit
* 16 + secondDigit
;
300 // Convert decimal integer to 2-character hex string
301 void wxDecToHex(int dec
, wxChar
*buf
)
303 int firstDigit
= (int)(dec
/16.0);
304 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
305 buf
[0] = hexArray
[firstDigit
];
306 buf
[1] = hexArray
[secondDigit
];
310 // Convert decimal integer to 2-character hex string
311 wxString
wxDecToHex(int dec
)
314 wxDecToHex(dec
, buf
);
315 return wxString(buf
);
318 // Match a string INDEPENDENT OF CASE
320 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
322 if (str1
== NULL
|| str2
== NULL
)
329 int len1
= strlen (str1
);
330 int len2
= strlen (str2
);
333 // Search for str1 in str2
334 // Slow .... but acceptable for short strings
335 for (i
= 0; i
<= len2
- len1
; i
++)
337 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
343 if (strcasecmp (str1
, str2
) == 0)
348 int len1
= strlen (str1
);
349 int len2
= strlen (str2
);
351 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
358 // Don't synthesize KeyUp events holding down a key and producing
359 // KeyDown events with autorepeat. On by default and always on
360 // on in wxMSW. wxGTK version in utilsgtk.cpp.
362 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
364 return TRUE
; // detectable auto-repeat is the only mode MSW supports
368 // Return the current date/time
370 wxString
wxNow( void )
372 time_t now
= time((time_t *) NULL
);
373 char *date
= ctime(&now
);
375 return wxString(date
);
379 * Strip out any menu codes
382 wxChar
*wxStripMenuCodes (wxChar
*in
, wxChar
*out
)
385 return (wxChar
*) NULL
;
388 out
= copystring(in
);
390 wxChar
*tmpOut
= out
;
396 // Check && -> &, &x -> x
397 if (*++in
== _T('&'))
400 else if (*in
== _T('\t'))
402 // Remove all stuff after \t in X mode, and let the stuff as is
404 // Accelerators are handled in wx_item.cc for Motif, and are not
405 // YET supported in XView
417 wxString
wxStripMenuCodes(const wxString
& str
)
419 wxChar
*buf
= new wxChar
[str
.Length() + 1];
420 wxStripMenuCodes(WXSTRINGCAST str
, buf
);
427 * Window search functions
432 * If parent is non-NULL, look through children for a label or title
433 * matching the specified string. If NULL, look through all top-level windows.
438 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
442 return wxFindWindowByLabel1(title
, parent
);
446 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
448 node
= node
->GetNext() )
450 wxWindow
*win
= node
->GetData();
451 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
457 return (wxWindow
*) NULL
;
462 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
466 if (parent
->GetLabel() == title
)
472 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
474 node
= node
->GetNext() )
476 wxWindow
*win
= (wxWindow
*)node
->GetData();
477 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
484 return (wxWindow
*) NULL
; // Not found
488 * If parent is non-NULL, look through children for a name
489 * matching the specified string. If NULL, look through all top-level windows.
494 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
498 return wxFindWindowByName1 (title
, parent
);
502 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
504 node
= node
->GetNext() )
506 wxWindow
*win
= node
->GetData();
507 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
514 // Failed? Try by label instead.
515 return wxFindWindowByLabel(title
, parent
);
520 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
524 if ( parent
->GetName() == title
)
530 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
532 wxWindow
*win
= (wxWindow
*) node
->Data ();
533 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
540 return (wxWindow
*) NULL
; // Not found
544 // Returns menu item id or -1 if none.
546 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
548 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
551 return menuBar
->FindMenuItem (menuString
, itemString
);
555 On Fri, 21 Jul 1995, Paul Craven wrote:
557 > Is there a way to find the path of running program's executable? I can get
558 > my home directory, and the current directory, but I don't know how to get the
559 > executable directory.
562 The code below (warty as it is), does what you want on most Unix,
563 DOS, and Mac platforms (it's from the ALS Prolog main).
565 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
566 ||==== Voice: +1 (617)965-9191 Newton Centre,
567 || FAX: +1 (617)965-1636 MA 02159 USA
568 Email: ken@als.com WWW: http://www.als.com
569 ------------------------------------------------------------------------
572 // This code is commented out but it may be integrated with wxWin at
573 // a later date, after testing. Thanks Ken!
576 /*--------------------------------------------------------------------*
577 | whereami is given a filename f in the form: whereami(argv[0])
578 | It returns the directory in which the executable file (containing
579 | this code [main.c] ) may be found. A dot will be returned to indicate
580 | the current directory.
581 *--------------------------------------------------------------------*/
587 register char *cutoff
= NULL
; /* stifle -Wall */
594 * See if the file is accessible either through the current directory
595 * or through an absolute path.
598 if (access(name
, R_OK
) == 0) {
600 /*-------------------------------------------------------------*
601 * The file was accessible without any other work. But the current
602 * working directory might change on us, so if it was accessible
603 * through the cwd, then we should get it for later accesses.
604 *-------------------------------------------------------------*/
607 if (!absolute_pathname(name
)) {
608 #if defined(DOS) || defined(__WIN32__)
614 if (*(name
+ 1) == ':') {
615 if (*name
>= 'a' && *name
<= 'z')
616 drive
= (int) (*name
- 'a' + 1);
618 drive
= (int) (*name
- 'A' + 1);
620 *newrbuf
++ = *(name
+ 1);
621 *newrbuf
++ = DIR_SEPARATOR
;
625 *newrbuf
++ = DIR_SEPARATOR
;
627 if (getcwd(newrbuf
, drive
) == 0) { /* } */
629 if (getcwd(newrbuf
, 1024) == 0) { /* } */
633 if (getwd(imagedir
) == 0) { /* } */
634 #else /* !HAVE_GETWD */
635 if (getcwd(imagedir
, 1024) == 0) {
636 #endif /* !HAVE_GETWD */
638 fatal_error(FE_GETCWD
, 0);
640 for (; *t
; t
++) /* Set t to end of buffer */
642 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
647 cutoff
= t
; /* otherwise put one in */
648 *t
++ = DIR_SEPARATOR
;
651 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
653 (*t
++ = DIR_SEPARATOR
);
656 /*-------------------------------------------------------------*
657 * Copy the rest of the string and set the cutoff if it was not
658 * already set. If the first character of name is a slash, cutoff
659 * is not presently set but will be on the first iteration of the
661 *-------------------------------------------------------------*/
663 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
664 if (*s
== DIR_SEPARATOR
)
673 /*-------------------------------------------------------------*
674 * Get the path list from the environment. If the path list is
675 * inaccessible for any reason, leave with fatal error.
676 *-------------------------------------------------------------*/
679 if ((s
= getenv("Commands")) == (char *) 0)
681 if ((s
= getenv("PATH")) == (char *) 0)
683 fatal_error(FE_PATH
, 0);
686 * Copy path list into ebuf and set the source pointer to the
687 * beginning of this buffer.
695 while (*s
&& *s
!= PATH_SEPARATOR
)
697 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
698 ; /* do nothing -- slash already is in place */
700 *t
++ = DIR_SEPARATOR
; /* put in the slash */
701 cutoff
= t
- 1; /* set cutoff */
703 if (access(imagedir
, R_OK
) == 0)
707 s
++; /* advance source pointer */
709 fatal_error(FE_INFND
, 0);
714 /*-------------------------------------------------------------*
715 | At this point the full pathname should exist in imagedir and
716 | cutoff should be set to the final slash. We must now determine
717 | whether the file name is a symbolic link or not and chase it down
718 | if it is. Note that we reuse ebuf for getting the link.
719 *-------------------------------------------------------------*/
722 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
725 if (*s
== DIR_SEPARATOR
) {
732 if (*s
== DIR_SEPARATOR
)
733 cutoff
= t
; /* mark the last slash seen */
734 if (!(*t
++ = *s
++)) /* copy the character */
739 #endif /* HAVE_SYMLINK */
741 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
742 *(cutoff
+ 1) = 0; /* chop off the filename part */
746 void wxEnableTopLevelWindows(bool enable
)
748 wxWindowList::Node
*node
;
749 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
750 node
->GetData()->Enable(enable
);
753 // Yield to other apps/messages and disable user input
754 bool wxSafeYield(wxWindow
*win
)
756 wxEnableTopLevelWindows(FALSE
);
757 // always enable ourselves
761 wxEnableTopLevelWindows(TRUE
);
766 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
767 * since otherwise the generic code may be pulled in unnecessarily.
770 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
771 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
773 wxMessageDialog
dialog(parent
, message
, caption
, style
);
775 int ans
= dialog
.ShowModal();
795 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
796 const wxString
& defaultValue
, wxWindow
*parent
,
797 int x
, int y
, bool WXUNUSED(centre
) )
799 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
800 if (dialog
.ShowModal() == wxID_OK
)
801 return dialog
.GetValue();
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
);