]>
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__ */
181 #define strcasecmp stricmp
182 #define strncasecmp strnicmp
186 copystring (const wxChar
*s
)
188 if (s
== NULL
) s
= _T("");
189 size_t len
= wxStrlen (s
) + 1;
191 wxChar
*news
= new wxChar
[len
];
192 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
198 static long wxCurrentId
= 100;
203 return wxCurrentId
++;
207 wxGetCurrentId(void) { return wxCurrentId
; }
210 wxRegisterId (long id
)
212 if (id
>= wxCurrentId
)
213 wxCurrentId
= id
+ 1;
217 StringToFloat (wxChar
*s
, float *number
)
219 if (s
&& *s
&& number
)
220 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
224 StringToDouble (wxChar
*s
, double *number
)
226 if (s
&& *s
&& number
)
227 *number
= wxStrtod (s
, (wxChar
**) NULL
);
231 FloatToString (float number
, const wxChar
*fmt
)
233 static wxChar buf
[256];
235 // sprintf (buf, "%.2f", number);
236 wxSprintf (buf
, fmt
, number
);
241 DoubleToString (double number
, const wxChar
*fmt
)
243 static wxChar buf
[256];
245 wxSprintf (buf
, fmt
, number
);
250 StringToInt (wxChar
*s
, int *number
)
252 if (s
&& *s
&& number
)
253 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
257 StringToLong (wxChar
*s
, long *number
)
259 if (s
&& *s
&& number
)
260 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
264 IntToString (int number
)
266 static wxChar buf
[20];
268 wxSprintf (buf
, _T("%d"), number
);
273 LongToString (long number
)
275 static wxChar buf
[20];
277 wxSprintf (buf
, _T("%ld"), number
);
281 // Array used in DecToHex conversion routine.
282 static wxChar hexArray
[] = _T("0123456789ABCDEF");
284 // Convert 2-digit hex number to decimal
285 int wxHexToDec(const wxString
& buf
)
287 int firstDigit
, secondDigit
;
289 if (buf
.GetChar(0) >= _T('A'))
290 firstDigit
= buf
.GetChar(0) - _T('A') + 10;
292 firstDigit
= buf
.GetChar(0) - _T('0');
294 if (buf
.GetChar(1) >= _T('A'))
295 secondDigit
= buf
.GetChar(1) - _T('A') + 10;
297 secondDigit
= buf
.GetChar(1) - _T('0');
299 return firstDigit
* 16 + secondDigit
;
302 // Convert decimal integer to 2-character hex string
303 void wxDecToHex(int dec
, wxChar
*buf
)
305 int firstDigit
= (int)(dec
/16.0);
306 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
307 buf
[0] = hexArray
[firstDigit
];
308 buf
[1] = hexArray
[secondDigit
];
312 // Convert decimal integer to 2-character hex string
313 wxString
wxDecToHex(int dec
)
316 wxDecToHex(dec
, buf
);
317 return wxString(buf
);
320 // Match a string INDEPENDENT OF CASE
322 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
324 if (str1
== NULL
|| str2
== NULL
)
331 int len1
= strlen (str1
);
332 int len2
= strlen (str2
);
335 // Search for str1 in str2
336 // Slow .... but acceptable for short strings
337 for (i
= 0; i
<= len2
- len1
; i
++)
339 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
345 if (strcasecmp (str1
, str2
) == 0)
350 int len1
= strlen (str1
);
351 int len2
= strlen (str2
);
353 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
360 // Don't synthesize KeyUp events holding down a key and producing
361 // KeyDown events with autorepeat. On by default and always on
362 // on in wxMSW. wxGTK version in utilsgtk.cpp.
364 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
366 return TRUE
; // detectable auto-repeat is the only mode MSW supports
370 // Return the current date/time
372 wxString
wxNow( void )
374 time_t now
= time((time_t *) NULL
);
375 char *date
= ctime(&now
);
377 return wxString(date
);
381 * Strip out any menu codes
384 wxChar
*wxStripMenuCodes (wxChar
*in
, wxChar
*out
)
387 return (wxChar
*) NULL
;
390 out
= copystring(in
);
392 wxChar
*tmpOut
= out
;
398 // Check && -> &, &x -> x
399 if (*++in
== _T('&'))
402 else if (*in
== _T('\t'))
404 // Remove all stuff after \t in X mode, and let the stuff as is
406 // Accelerators are handled in wx_item.cc for Motif, and are not
407 // YET supported in XView
419 wxString
wxStripMenuCodes(const wxString
& str
)
421 wxChar
*buf
= new wxChar
[str
.Length() + 1];
422 wxStripMenuCodes(WXSTRINGCAST str
, buf
);
429 * Window search functions
434 * If parent is non-NULL, look through children for a label or title
435 * matching the specified string. If NULL, look through all top-level windows.
440 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
444 return wxFindWindowByLabel1(title
, parent
);
448 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
450 node
= node
->GetNext() )
452 wxWindow
*win
= node
->GetData();
453 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
459 return (wxWindow
*) NULL
;
464 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
468 if (parent
->GetLabel() == title
)
474 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
476 node
= node
->GetNext() )
478 wxWindow
*win
= (wxWindow
*)node
->GetData();
479 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
486 return (wxWindow
*) NULL
; // Not found
490 * If parent is non-NULL, look through children for a name
491 * matching the specified string. If NULL, look through all top-level windows.
496 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
500 return wxFindWindowByName1 (title
, parent
);
504 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
506 node
= node
->GetNext() )
508 wxWindow
*win
= node
->GetData();
509 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
516 // Failed? Try by label instead.
517 return wxFindWindowByLabel(title
, parent
);
522 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
526 if ( parent
->GetName() == title
)
532 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
534 wxWindow
*win
= (wxWindow
*) node
->Data ();
535 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
542 return (wxWindow
*) NULL
; // Not found
546 // Returns menu item id or -1 if none.
548 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
550 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
553 return menuBar
->FindMenuItem (menuString
, itemString
);
557 On Fri, 21 Jul 1995, Paul Craven wrote:
559 > Is there a way to find the path of running program's executable? I can get
560 > my home directory, and the current directory, but I don't know how to get the
561 > executable directory.
564 The code below (warty as it is), does what you want on most Unix,
565 DOS, and Mac platforms (it's from the ALS Prolog main).
567 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
568 ||==== Voice: +1 (617)965-9191 Newton Centre,
569 || FAX: +1 (617)965-1636 MA 02159 USA
570 Email: ken@als.com WWW: http://www.als.com
571 ------------------------------------------------------------------------
574 // This code is commented out but it may be integrated with wxWin at
575 // a later date, after testing. Thanks Ken!
578 /*--------------------------------------------------------------------*
579 | whereami is given a filename f in the form: whereami(argv[0])
580 | It returns the directory in which the executable file (containing
581 | this code [main.c] ) may be found. A dot will be returned to indicate
582 | the current directory.
583 *--------------------------------------------------------------------*/
589 register char *cutoff
= NULL
; /* stifle -Wall */
596 * See if the file is accessible either through the current directory
597 * or through an absolute path.
600 if (access(name
, R_OK
) == 0) {
602 /*-------------------------------------------------------------*
603 * The file was accessible without any other work. But the current
604 * working directory might change on us, so if it was accessible
605 * through the cwd, then we should get it for later accesses.
606 *-------------------------------------------------------------*/
609 if (!absolute_pathname(name
)) {
610 #if defined(DOS) || defined(__WIN32__)
616 if (*(name
+ 1) == ':') {
617 if (*name
>= 'a' && *name
<= 'z')
618 drive
= (int) (*name
- 'a' + 1);
620 drive
= (int) (*name
- 'A' + 1);
622 *newrbuf
++ = *(name
+ 1);
623 *newrbuf
++ = DIR_SEPARATOR
;
627 *newrbuf
++ = DIR_SEPARATOR
;
629 if (getcwd(newrbuf
, drive
) == 0) { /* } */
631 if (getcwd(newrbuf
, 1024) == 0) { /* } */
635 if (getwd(imagedir
) == 0) { /* } */
636 #else /* !HAVE_GETWD */
637 if (getcwd(imagedir
, 1024) == 0) {
638 #endif /* !HAVE_GETWD */
640 fatal_error(FE_GETCWD
, 0);
642 for (; *t
; t
++) /* Set t to end of buffer */
644 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
649 cutoff
= t
; /* otherwise put one in */
650 *t
++ = DIR_SEPARATOR
;
653 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
655 (*t
++ = DIR_SEPARATOR
);
658 /*-------------------------------------------------------------*
659 * Copy the rest of the string and set the cutoff if it was not
660 * already set. If the first character of name is a slash, cutoff
661 * is not presently set but will be on the first iteration of the
663 *-------------------------------------------------------------*/
665 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
666 if (*s
== DIR_SEPARATOR
)
675 /*-------------------------------------------------------------*
676 * Get the path list from the environment. If the path list is
677 * inaccessible for any reason, leave with fatal error.
678 *-------------------------------------------------------------*/
681 if ((s
= getenv("Commands")) == (char *) 0)
683 if ((s
= getenv("PATH")) == (char *) 0)
685 fatal_error(FE_PATH
, 0);
688 * Copy path list into ebuf and set the source pointer to the
689 * beginning of this buffer.
697 while (*s
&& *s
!= PATH_SEPARATOR
)
699 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
700 ; /* do nothing -- slash already is in place */
702 *t
++ = DIR_SEPARATOR
; /* put in the slash */
703 cutoff
= t
- 1; /* set cutoff */
705 if (access(imagedir
, R_OK
) == 0)
709 s
++; /* advance source pointer */
711 fatal_error(FE_INFND
, 0);
716 /*-------------------------------------------------------------*
717 | At this point the full pathname should exist in imagedir and
718 | cutoff should be set to the final slash. We must now determine
719 | whether the file name is a symbolic link or not and chase it down
720 | if it is. Note that we reuse ebuf for getting the link.
721 *-------------------------------------------------------------*/
724 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
727 if (*s
== DIR_SEPARATOR
) {
734 if (*s
== DIR_SEPARATOR
)
735 cutoff
= t
; /* mark the last slash seen */
736 if (!(*t
++ = *s
++)) /* copy the character */
741 #endif /* HAVE_SYMLINK */
743 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
744 *(cutoff
+ 1) = 0; /* chop off the filename part */
748 void wxEnableTopLevelWindows(bool enable
)
750 wxWindowList::Node
*node
;
751 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
752 node
->GetData()->Enable(enable
);
755 // Yield to other apps/messages and disable user input
756 bool wxSafeYield(wxWindow
*win
)
758 wxEnableTopLevelWindows(FALSE
);
759 // always enable ourselves
763 wxEnableTopLevelWindows(TRUE
);
768 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
769 * since otherwise the generic code may be pulled in unnecessarily.
772 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
773 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
775 wxMessageDialog
dialog(parent
, message
, caption
, style
);
777 int ans
= dialog
.ShowModal();
798 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
799 const wxString
& defaultValue
, wxWindow
*parent
,
800 int x
, int y
, bool WXUNUSED(centre
) )
802 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
803 if (dialog
.ShowModal() == wxID_OK
)
804 return dialog
.GetValue();
808 #endif // wxUSE_TEXTDLG
811 char *strdup(const char *s
)
813 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
818 return ( c
>= 0 && c
< 128 ) ;
822 // ----------------------------------------------------------------------------
823 // network and user id functions
824 // ----------------------------------------------------------------------------
826 // Get Full RFC822 style email address
827 bool wxGetEmailAddress(wxChar
*address
, int maxSize
)
829 wxString email
= wxGetEmailAddress();
833 wxStrncpy(address
, email
, maxSize
- 1);
834 address
[maxSize
- 1] = _T('\0');
839 wxString
wxGetEmailAddress()
843 wxString host
= wxGetHostName();
846 wxString user
= wxGetUserId();
849 wxString
email(user
);
850 email
<< _T('@') << host
;
857 wxString
wxGetUserId()
859 static const int maxLoginLen
= 256; // FIXME arbitrary number
862 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
871 wxString
wxGetUserName()
873 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
876 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
885 wxString
wxGetHostName()
887 static const size_t hostnameSize
= 257;
890 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
900 wxString
wxGetFullHostName()
902 static const size_t hostnameSize
= 257;
905 bool ok
= wxGetFullHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);