]>
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 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "utils.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
37 #include "wx/window.h"
40 #include "wx/msgdlg.h"
41 #include "wx/textdlg.h"
50 #if !defined(__WATCOMC__)
51 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
59 #include <sys/types.h>
67 // Pattern matching code. (FIXME)
68 // Yes, this path is deliberate (for Borland compilation)
69 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
72 #include "../common/glob.inc"
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
84 static wxWindow
*wxFindWindowByLabel1(const wxString
& title
, wxWindow
*parent
);
85 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
*parent
);
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
97 int strcasecmp(const char *str_1
, const char *str_2
)
101 c1
= tolower(*str_1
++);
102 c2
= tolower(*str_2
++);
103 } while ( c1
&& (c1
== c2
) );
108 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
111 register char c1
, c2
;
114 c1
= tolower(*str_1
++);
115 c2
= tolower(*str_2
++);
128 // we have no strI functions under VMS, therefore I have implemented
129 // an inefficient but portable version: convert copies of strings to lowercase
130 // and then use the normal comparison
131 static void myLowerString(char *s
)
134 if(isalpha(*s
)) *s
= (char)tolower(*s
);
139 int strcasecmp(const char *str_1
, const char *str_2
)
141 char *temp1
= new char[strlen(str_1
)+1];
142 char *temp2
= new char[strlen(str_2
)+1];
145 myLowerString(temp1
);
146 myLowerString(temp2
);
148 int result
= strcmp(temp1
,temp2
);
155 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
157 char *temp1
= new char[strlen(str_1
)+1];
158 char *temp2
= new char[strlen(str_2
)+1];
161 myLowerString(temp1
);
162 myLowerString(temp2
);
164 int result
= strncmp(temp1
,temp2
,maxchar
);
176 #define strcasecmp stricmp
177 #define strncasecmp strnicmp
179 #define strcasecmp _stricmp
180 #define strncasecmp _strnicmp
187 #define strcasecmp stricmp
188 #define strncasecmp strnicmp
191 // This declaration is missing in SunOS!
192 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
193 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
196 int strcasecmp (const char *, const char *);
197 int strncasecmp (const char *, const char *, size_t);
200 #endif /* __WXMSW__ */
203 #define strcasecmp stricmp
204 #define strncasecmp strnicmp
208 copystring (const wxChar
*s
)
210 if (s
== NULL
) s
= wxT("");
211 size_t len
= wxStrlen (s
) + 1;
213 wxChar
*news
= new wxChar
[len
];
214 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
220 static long wxCurrentId
= 100;
225 return wxCurrentId
++;
229 wxGetCurrentId(void) { return wxCurrentId
; }
232 wxRegisterId (long id
)
234 if (id
>= wxCurrentId
)
235 wxCurrentId
= id
+ 1;
239 StringToFloat (wxChar
*s
, float *number
)
241 if (s
&& *s
&& number
)
242 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
246 StringToDouble (wxChar
*s
, double *number
)
248 if (s
&& *s
&& number
)
249 *number
= wxStrtod (s
, (wxChar
**) NULL
);
253 FloatToString (float number
, const wxChar
*fmt
)
255 static wxChar buf
[256];
257 // sprintf (buf, "%.2f", number);
258 wxSprintf (buf
, fmt
, number
);
263 DoubleToString (double number
, const wxChar
*fmt
)
265 static wxChar buf
[256];
267 wxSprintf (buf
, fmt
, number
);
272 StringToInt (wxChar
*s
, int *number
)
274 if (s
&& *s
&& number
)
275 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
279 StringToLong (wxChar
*s
, long *number
)
281 if (s
&& *s
&& number
)
282 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
286 IntToString (int number
)
288 static wxChar buf
[20];
290 wxSprintf (buf
, wxT("%d"), number
);
295 LongToString (long number
)
297 static wxChar buf
[20];
299 wxSprintf (buf
, wxT("%ld"), number
);
303 // Array used in DecToHex conversion routine.
304 static wxChar hexArray
[] = wxT("0123456789ABCDEF");
306 // Convert 2-digit hex number to decimal
307 int wxHexToDec(const wxString
& buf
)
309 int firstDigit
, secondDigit
;
311 if (buf
.GetChar(0) >= wxT('A'))
312 firstDigit
= buf
.GetChar(0) - wxT('A') + 10;
314 firstDigit
= buf
.GetChar(0) - wxT('0');
316 if (buf
.GetChar(1) >= wxT('A'))
317 secondDigit
= buf
.GetChar(1) - wxT('A') + 10;
319 secondDigit
= buf
.GetChar(1) - wxT('0');
321 return firstDigit
* 16 + secondDigit
;
324 // Convert decimal integer to 2-character hex string
325 void wxDecToHex(int dec
, wxChar
*buf
)
327 int firstDigit
= (int)(dec
/16.0);
328 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
329 buf
[0] = hexArray
[firstDigit
];
330 buf
[1] = hexArray
[secondDigit
];
334 // Convert decimal integer to 2-character hex string
335 wxString
wxDecToHex(int dec
)
338 wxDecToHex(dec
, buf
);
339 return wxString(buf
);
342 // Match a string INDEPENDENT OF CASE
344 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
346 if (str1
== NULL
|| str2
== NULL
)
353 int len1
= strlen (str1
);
354 int len2
= strlen (str2
);
357 // Search for str1 in str2
358 // Slow .... but acceptable for short strings
359 for (i
= 0; i
<= len2
- len1
; i
++)
361 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
367 if (strcasecmp (str1
, str2
) == 0)
372 int len1
= strlen (str1
);
373 int len2
= strlen (str2
);
375 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
382 // Return the current date/time
386 time_t now
= time((time_t *) NULL
);
387 char *date
= ctime(&now
);
389 return wxString(date
);
394 // ----------------------------------------------------------------------------
395 // Strip out any menu codes
396 // ----------------------------------------------------------------------------
398 wxChar
*wxStripMenuCodes (wxChar
*in
, wxChar
*out
)
401 return (wxChar
*) NULL
;
404 out
= copystring(in
);
406 wxChar
*tmpOut
= out
;
412 // Check && -> &, &x -> x
413 if (*++in
== wxT('&'))
416 else if (*in
== wxT('\t'))
418 // Remove all stuff after \t in X mode, and let the stuff as is
420 // Accelerators are handled in wx_item.cc for Motif, and are not
421 // YET supported in XView
433 wxString
wxStripMenuCodes(const wxString
& str
)
435 wxChar
*buf
= new wxChar
[str
.Length() + 1];
436 wxStripMenuCodes(WXSTRINGCAST str
, buf
);
442 // ----------------------------------------------------------------------------
443 // Window search functions
444 // ----------------------------------------------------------------------------
447 * If parent is non-NULL, look through children for a label or title
448 * matching the specified string. If NULL, look through all top-level windows.
453 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
457 return wxFindWindowByLabel1(title
, parent
);
461 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
463 node
= node
->GetNext() )
465 wxWindow
*win
= node
->GetData();
466 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
472 return (wxWindow
*) NULL
;
477 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
481 if (parent
->GetLabel() == title
)
487 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
489 node
= node
->GetNext() )
491 wxWindow
*win
= (wxWindow
*)node
->GetData();
492 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
499 return (wxWindow
*) NULL
; // Not found
503 * If parent is non-NULL, look through children for a name
504 * matching the specified string. If NULL, look through all top-level windows.
509 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
513 return wxFindWindowByName1 (title
, parent
);
517 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
519 node
= node
->GetNext() )
521 wxWindow
*win
= node
->GetData();
522 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
529 // Failed? Try by label instead.
530 return wxFindWindowByLabel(title
, parent
);
535 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
539 if ( parent
->GetName() == title
)
545 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
547 wxWindow
*win
= (wxWindow
*) node
->Data ();
548 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
555 return (wxWindow
*) NULL
; // Not found
559 // Returns menu item id or -1 if none.
561 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
563 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
566 return menuBar
->FindMenuItem (menuString
, itemString
);
572 On Fri, 21 Jul 1995, Paul Craven wrote:
574 > Is there a way to find the path of running program's executable? I can get
575 > my home directory, and the current directory, but I don't know how to get the
576 > executable directory.
579 The code below (warty as it is), does what you want on most Unix,
580 DOS, and Mac platforms (it's from the ALS Prolog main).
582 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
583 ||==== Voice: +1 (617)965-9191 Newton Centre,
584 || FAX: +1 (617)965-1636 MA 02159 USA
585 Email: ken@als.com WWW: http://www.als.com
586 ------------------------------------------------------------------------
589 // This code is commented out but it may be integrated with wxWin at
590 // a later date, after testing. Thanks Ken!
593 /*--------------------------------------------------------------------*
594 | whereami is given a filename f in the form: whereami(argv[0])
595 | It returns the directory in which the executable file (containing
596 | this code [main.c] ) may be found. A dot will be returned to indicate
597 | the current directory.
598 *--------------------------------------------------------------------*/
604 register char *cutoff
= NULL
; /* stifle -Wall */
611 * See if the file is accessible either through the current directory
612 * or through an absolute path.
615 if (access(name
, R_OK
) == 0) {
617 /*-------------------------------------------------------------*
618 * The file was accessible without any other work. But the current
619 * working directory might change on us, so if it was accessible
620 * through the cwd, then we should get it for later accesses.
621 *-------------------------------------------------------------*/
624 if (!absolute_pathname(name
)) {
625 #if defined(DOS) || defined(__WIN32__)
631 if (*(name
+ 1) == ':') {
632 if (*name
>= 'a' && *name
<= 'z')
633 drive
= (int) (*name
- 'a' + 1);
635 drive
= (int) (*name
- 'A' + 1);
637 *newrbuf
++ = *(name
+ 1);
638 *newrbuf
++ = DIR_SEPARATOR
;
642 *newrbuf
++ = DIR_SEPARATOR
;
644 if (getcwd(newrbuf
, drive
) == 0) { /* } */
646 if (getcwd(newrbuf
, 1024) == 0) { /* } */
650 if (getwd(imagedir
) == 0) { /* } */
651 #else /* !HAVE_GETWD */
652 if (getcwd(imagedir
, 1024) == 0) {
653 #endif /* !HAVE_GETWD */
655 fatal_error(FE_GETCWD
, 0);
657 for (; *t
; t
++) /* Set t to end of buffer */
659 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
664 cutoff
= t
; /* otherwise put one in */
665 *t
++ = DIR_SEPARATOR
;
668 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
670 (*t
++ = DIR_SEPARATOR
);
673 /*-------------------------------------------------------------*
674 * Copy the rest of the string and set the cutoff if it was not
675 * already set. If the first character of name is a slash, cutoff
676 * is not presently set but will be on the first iteration of the
678 *-------------------------------------------------------------*/
680 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
681 if (*s
== DIR_SEPARATOR
)
690 /*-------------------------------------------------------------*
691 * Get the path list from the environment. If the path list is
692 * inaccessible for any reason, leave with fatal error.
693 *-------------------------------------------------------------*/
696 if ((s
= getenv("Commands")) == (char *) 0)
698 if ((s
= getenv("PATH")) == (char *) 0)
700 fatal_error(FE_PATH
, 0);
703 * Copy path list into ebuf and set the source pointer to the
704 * beginning of this buffer.
712 while (*s
&& *s
!= PATH_SEPARATOR
)
714 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
715 ; /* do nothing -- slash already is in place */
717 *t
++ = DIR_SEPARATOR
; /* put in the slash */
718 cutoff
= t
- 1; /* set cutoff */
720 if (access(imagedir
, R_OK
) == 0)
724 s
++; /* advance source pointer */
726 fatal_error(FE_INFND
, 0);
731 /*-------------------------------------------------------------*
732 | At this point the full pathname should exist in imagedir and
733 | cutoff should be set to the final slash. We must now determine
734 | whether the file name is a symbolic link or not and chase it down
735 | if it is. Note that we reuse ebuf for getting the link.
736 *-------------------------------------------------------------*/
739 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
742 if (*s
== DIR_SEPARATOR
) {
749 if (*s
== DIR_SEPARATOR
)
750 cutoff
= t
; /* mark the last slash seen */
751 if (!(*t
++ = *s
++)) /* copy the character */
756 #endif /* HAVE_SYMLINK */
758 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
759 *(cutoff
+ 1) = 0; /* chop off the filename part */
766 // ----------------------------------------------------------------------------
768 // ----------------------------------------------------------------------------
771 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
772 * since otherwise the generic code may be pulled in unnecessarily.
775 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
776 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
778 wxMessageDialog
dialog(parent
, message
, caption
, style
);
780 int ans
= dialog
.ShowModal();
801 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
802 const wxString
& defaultValue
, wxWindow
*parent
,
803 int x
, int y
, bool WXUNUSED(centre
) )
805 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
806 if (dialog
.ShowModal() == wxID_OK
)
807 return dialog
.GetValue();
811 #endif // wxUSE_TEXTDLG
814 char *strdup(const char *s
)
816 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
821 return ( c
>= 0 && c
< 128 ) ;
825 // ----------------------------------------------------------------------------
827 // ----------------------------------------------------------------------------
829 void wxEnableTopLevelWindows(bool enable
)
831 wxWindowList::Node
*node
;
832 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
833 node
->GetData()->Enable(enable
);
836 // Yield to other apps/messages and disable user input
837 bool wxSafeYield(wxWindow
*win
)
839 wxEnableTopLevelWindows(FALSE
);
840 // always enable ourselves
844 wxEnableTopLevelWindows(TRUE
);
848 // Don't synthesize KeyUp events holding down a key and producing KeyDown
849 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
852 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
854 return TRUE
; // detectable auto-repeat is the only mode MSW supports
860 // ----------------------------------------------------------------------------
861 // network and user id functions
862 // ----------------------------------------------------------------------------
864 // Get Full RFC822 style email address
865 bool wxGetEmailAddress(wxChar
*address
, int maxSize
)
867 wxString email
= wxGetEmailAddress();
871 wxStrncpy(address
, email
, maxSize
- 1);
872 address
[maxSize
- 1] = wxT('\0');
877 wxString
wxGetEmailAddress()
881 wxString host
= wxGetHostName();
884 wxString user
= wxGetUserId();
887 wxString
email(user
);
888 email
<< wxT('@') << host
;
895 wxString
wxGetUserId()
897 static const int maxLoginLen
= 256; // FIXME arbitrary number
900 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
909 wxString
wxGetUserName()
911 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
914 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
923 wxString
wxGetHostName()
925 static const size_t hostnameSize
= 257;
928 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
938 wxString
wxGetFullHostName()
940 static const size_t hostnameSize
= 257;
943 bool ok
= wxGetFullHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);