]>
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"
39 #include "wx/window.h"
42 #include "wx/msgdlg.h"
43 #include "wx/textdlg.h"
45 #include "wx/menuitem.h"
56 #if !defined(__WATCOMC__)
57 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
63 #include "wx/colordlg.h"
69 #include <sys/types.h>
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 static wxWindow
*wxFindWindowByLabel1(const wxString
& title
, wxWindow
*parent
);
87 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
*parent
);
90 // ============================================================================
92 // ============================================================================
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
99 int strcasecmp(const char *str_1
, const char *str_2
)
101 register char c1
, c2
;
103 c1
= tolower(*str_1
++);
104 c2
= tolower(*str_2
++);
105 } while ( c1
&& (c1
== c2
) );
110 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
113 register char c1
, c2
;
116 c1
= tolower(*str_1
++);
117 c2
= tolower(*str_2
++);
129 #if defined( __VMS__ ) && ( __VMS_VER < 70000000 )
130 // we have no strI functions under VMS, therefore I have implemented
131 // an inefficient but portable version: convert copies of strings to lowercase
132 // and then use the normal comparison
133 static void myLowerString(char *s
)
136 if(isalpha(*s
)) *s
= (char)tolower(*s
);
141 int strcasecmp(const char *str_1
, const char *str_2
)
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
= wxStrcmp(temp1
,temp2
);
157 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
159 char *temp1
= new char[strlen(str_1
)+1];
160 char *temp2
= new char[strlen(str_2
)+1];
163 myLowerString(temp1
);
164 myLowerString(temp2
);
166 int result
= strncmp(temp1
,temp2
,maxchar
);
178 #define strcasecmp stricmp
179 #define strncasecmp strnicmp
181 #define strcasecmp _stricmp
182 #define strncasecmp _strnicmp
189 #define strcasecmp stricmp
190 #define strncasecmp strnicmp
193 // This declaration is missing in SunOS!
194 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
195 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
198 int strcasecmp (const char *, const char *);
199 int strncasecmp (const char *, const char *, size_t);
202 #endif /* __WXMSW__ */
205 #define strcasecmp stricmp
206 #define strncasecmp strnicmp
210 copystring (const wxChar
*s
)
212 if (s
== NULL
) s
= wxT("");
213 size_t len
= wxStrlen (s
) + 1;
215 wxChar
*news
= new wxChar
[len
];
216 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
222 static long wxCurrentId
= 100;
227 return wxCurrentId
++;
231 wxGetCurrentId(void) { return wxCurrentId
; }
234 wxRegisterId (long id
)
236 if (id
>= wxCurrentId
)
237 wxCurrentId
= id
+ 1;
241 StringToFloat (wxChar
*s
, float *number
)
243 if (s
&& *s
&& number
)
244 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
248 StringToDouble (wxChar
*s
, double *number
)
250 if (s
&& *s
&& number
)
251 *number
= wxStrtod (s
, (wxChar
**) NULL
);
255 FloatToString (float number
, const wxChar
*fmt
)
257 static wxChar buf
[256];
259 // sprintf (buf, "%.2f", number);
260 wxSprintf (buf
, fmt
, number
);
265 DoubleToString (double number
, const wxChar
*fmt
)
267 static wxChar buf
[256];
269 wxSprintf (buf
, fmt
, number
);
274 StringToInt (wxChar
*s
, int *number
)
276 if (s
&& *s
&& number
)
277 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
281 StringToLong (wxChar
*s
, long *number
)
283 if (s
&& *s
&& number
)
284 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
288 IntToString (int number
)
290 static wxChar buf
[20];
292 wxSprintf (buf
, wxT("%d"), number
);
297 LongToString (long number
)
299 static wxChar buf
[20];
301 wxSprintf (buf
, wxT("%ld"), number
);
305 // Array used in DecToHex conversion routine.
306 static wxChar hexArray
[] = wxT("0123456789ABCDEF");
308 // Convert 2-digit hex number to decimal
309 int wxHexToDec(const wxString
& buf
)
311 int firstDigit
, secondDigit
;
313 if (buf
.GetChar(0) >= wxT('A'))
314 firstDigit
= buf
.GetChar(0) - wxT('A') + 10;
316 firstDigit
= buf
.GetChar(0) - wxT('0');
318 if (buf
.GetChar(1) >= wxT('A'))
319 secondDigit
= buf
.GetChar(1) - wxT('A') + 10;
321 secondDigit
= buf
.GetChar(1) - wxT('0');
323 return firstDigit
* 16 + secondDigit
;
326 // Convert decimal integer to 2-character hex string
327 void wxDecToHex(int dec
, wxChar
*buf
)
329 int firstDigit
= (int)(dec
/16.0);
330 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
331 buf
[0] = hexArray
[firstDigit
];
332 buf
[1] = hexArray
[secondDigit
];
336 // Convert decimal integer to 2-character hex string
337 wxString
wxDecToHex(int dec
)
340 wxDecToHex(dec
, buf
);
341 return wxString(buf
);
344 // Match a string INDEPENDENT OF CASE
346 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
348 if (str1
== NULL
|| str2
== NULL
)
355 int len1
= strlen (str1
);
356 int len2
= strlen (str2
);
359 // Search for str1 in str2
360 // Slow .... but acceptable for short strings
361 for (i
= 0; i
<= len2
- len1
; i
++)
363 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
369 if (strcasecmp (str1
, str2
) == 0)
374 int len1
= strlen (str1
);
375 int len2
= strlen (str2
);
377 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
384 // Return the current date/time
388 time_t now
= time((time_t *) NULL
);
389 char *date
= ctime(&now
);
391 return wxString(date
);
396 // ----------------------------------------------------------------------------
397 // Menu accelerators related functions
398 // ----------------------------------------------------------------------------
400 wxChar
*wxStripMenuCodes (wxChar
*in
, wxChar
*out
)
403 return (wxChar
*) NULL
;
406 out
= copystring(in
);
408 wxChar
*tmpOut
= out
;
414 // Check && -> &, &x -> x
415 if (*++in
== wxT('&'))
418 else if (*in
== wxT('\t'))
420 // Remove all stuff after \t in X mode, and let the stuff as is
422 // Accelerators are handled in wx_item.cc for Motif, and are not
423 // YET supported in XView
435 wxString
wxStripMenuCodes(const wxString
& str
)
437 wxChar
*buf
= new wxChar
[str
.Length() + 1];
438 wxStripMenuCodes(WXSTRINGCAST str
, buf
);
446 // return wxAcceleratorEntry for the given menu string or NULL if none
448 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
450 // check for accelerators: they are given after '\t'
451 int posTab
= label
.Find(wxT('\t'));
452 if ( posTab
!= wxNOT_FOUND
) {
453 // parse the accelerator string
455 int accelFlags
= wxACCEL_NORMAL
;
457 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.Len(); n
++ ) {
458 if ( (label
[n
] == '+') || (label
[n
] == '-') ) {
459 if ( current
== _("ctrl") )
460 accelFlags
|= wxACCEL_CTRL
;
461 else if ( current
== _("alt") )
462 accelFlags
|= wxACCEL_ALT
;
463 else if ( current
== _("shift") )
464 accelFlags
|= wxACCEL_SHIFT
;
466 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
473 current
+= wxTolower(label
[n
]);
477 if ( current
.IsEmpty() ) {
478 wxLogDebug(wxT("No accel key found, accel string ignored."));
481 if ( current
.Len() == 1 ) {
483 keyCode
= wxToupper(current
[0U]);
486 // is it a function key?
487 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
488 (current
.Len() == 2 ||
489 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
491 wxSscanf(current
.c_str() + 1, wxT("%d"), &n
);
493 keyCode
= WXK_F1
+ n
- 1;
496 #if 0 // this is not supported by GTK+, apparently
497 // several special cases
499 if ( current
== wxT("DEL") ) {
502 else if ( current
== wxT("PGUP") ) {
505 else if ( current
== wxT("PGDN") ) {
511 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
512 "string ignored."), current
.c_str());
519 // we do have something
520 return new wxAcceleratorEntry(accelFlags
, keyCode
);
524 return (wxAcceleratorEntry
*)NULL
;
527 #endif // wxUSE_ACCEL
529 // ----------------------------------------------------------------------------
530 // Window search functions
531 // ----------------------------------------------------------------------------
534 * If parent is non-NULL, look through children for a label or title
535 * matching the specified string. If NULL, look through all top-level windows.
540 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
544 return wxFindWindowByLabel1(title
, parent
);
548 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
550 node
= node
->GetNext() )
552 wxWindow
*win
= node
->GetData();
553 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
559 return (wxWindow
*) NULL
;
564 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
568 if (parent
->GetLabel() == title
)
574 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
576 node
= node
->GetNext() )
578 wxWindow
*win
= (wxWindow
*)node
->GetData();
579 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
586 return (wxWindow
*) NULL
; // Not found
590 * If parent is non-NULL, look through children for a name
591 * matching the specified string. If NULL, look through all top-level windows.
596 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
600 return wxFindWindowByName1 (title
, parent
);
604 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
606 node
= node
->GetNext() )
608 wxWindow
*win
= node
->GetData();
609 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
616 // Failed? Try by label instead.
617 return wxFindWindowByLabel(title
, parent
);
622 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
626 if ( parent
->GetName() == title
)
632 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
634 wxWindow
*win
= (wxWindow
*) node
->Data ();
635 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
642 return (wxWindow
*) NULL
; // Not found
646 // Returns menu item id or -1 if none.
648 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
650 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
653 return menuBar
->FindMenuItem (menuString
, itemString
);
659 On Fri, 21 Jul 1995, Paul Craven wrote:
661 > Is there a way to find the path of running program's executable? I can get
662 > my home directory, and the current directory, but I don't know how to get the
663 > executable directory.
666 The code below (warty as it is), does what you want on most Unix,
667 DOS, and Mac platforms (it's from the ALS Prolog main).
669 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
670 ||==== Voice: +1 (617)965-9191 Newton Centre,
671 || FAX: +1 (617)965-1636 MA 02159 USA
672 Email: ken@als.com WWW: http://www.als.com
673 ------------------------------------------------------------------------
676 // This code is commented out but it may be integrated with wxWin at
677 // a later date, after testing. Thanks Ken!
680 /*--------------------------------------------------------------------*
681 | whereami is given a filename f in the form: whereami(argv[0])
682 | It returns the directory in which the executable file (containing
683 | this code [main.c] ) may be found. A dot will be returned to indicate
684 | the current directory.
685 *--------------------------------------------------------------------*/
691 register char *cutoff
= NULL
; /* stifle -Wall */
698 * See if the file is accessible either through the current directory
699 * or through an absolute path.
702 if (access(name
, R_OK
) == 0) {
704 /*-------------------------------------------------------------*
705 * The file was accessible without any other work. But the current
706 * working directory might change on us, so if it was accessible
707 * through the cwd, then we should get it for later accesses.
708 *-------------------------------------------------------------*/
711 if (!absolute_pathname(name
)) {
712 #if defined(DOS) || defined(__WIN32__)
718 if (*(name
+ 1) == ':') {
719 if (*name
>= 'a' && *name
<= 'z')
720 drive
= (int) (*name
- 'a' + 1);
722 drive
= (int) (*name
- 'A' + 1);
724 *newrbuf
++ = *(name
+ 1);
725 *newrbuf
++ = DIR_SEPARATOR
;
729 *newrbuf
++ = DIR_SEPARATOR
;
731 if (getcwd(newrbuf
, drive
) == 0) { /* } */
733 if (getcwd(newrbuf
, 1024) == 0) { /* } */
737 if (getwd(imagedir
) == 0) { /* } */
738 #else /* !HAVE_GETWD */
739 if (getcwd(imagedir
, 1024) == 0) {
740 #endif /* !HAVE_GETWD */
742 fatal_error(FE_GETCWD
, 0);
744 for (; *t
; t
++) /* Set t to end of buffer */
746 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
751 cutoff
= t
; /* otherwise put one in */
752 *t
++ = DIR_SEPARATOR
;
755 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
757 (*t
++ = DIR_SEPARATOR
);
760 /*-------------------------------------------------------------*
761 * Copy the rest of the string and set the cutoff if it was not
762 * already set. If the first character of name is a slash, cutoff
763 * is not presently set but will be on the first iteration of the
765 *-------------------------------------------------------------*/
767 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
768 if (*s
== DIR_SEPARATOR
)
777 /*-------------------------------------------------------------*
778 * Get the path list from the environment. If the path list is
779 * inaccessible for any reason, leave with fatal error.
780 *-------------------------------------------------------------*/
783 if ((s
= getenv("Commands")) == (char *) 0)
785 if ((s
= getenv("PATH")) == (char *) 0)
787 fatal_error(FE_PATH
, 0);
790 * Copy path list into ebuf and set the source pointer to the
791 * beginning of this buffer.
799 while (*s
&& *s
!= PATH_SEPARATOR
)
801 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
802 ; /* do nothing -- slash already is in place */
804 *t
++ = DIR_SEPARATOR
; /* put in the slash */
805 cutoff
= t
- 1; /* set cutoff */
807 if (access(imagedir
, R_OK
) == 0)
811 s
++; /* advance source pointer */
813 fatal_error(FE_INFND
, 0);
818 /*-------------------------------------------------------------*
819 | At this point the full pathname should exist in imagedir and
820 | cutoff should be set to the final slash. We must now determine
821 | whether the file name is a symbolic link or not and chase it down
822 | if it is. Note that we reuse ebuf for getting the link.
823 *-------------------------------------------------------------*/
826 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
829 if (*s
== DIR_SEPARATOR
) {
836 if (*s
== DIR_SEPARATOR
)
837 cutoff
= t
; /* mark the last slash seen */
838 if (!(*t
++ = *s
++)) /* copy the character */
843 #endif /* HAVE_SYMLINK */
845 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
846 *(cutoff
+ 1) = 0; /* chop off the filename part */
853 // ----------------------------------------------------------------------------
855 // ----------------------------------------------------------------------------
858 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
859 * since otherwise the generic code may be pulled in unnecessarily.
862 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
863 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
865 wxMessageDialog
dialog(parent
, message
, caption
, style
);
867 int ans
= dialog
.ShowModal();
883 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
884 const wxString
& defaultValue
, wxWindow
*parent
,
885 int x
, int y
, bool WXUNUSED(centre
) )
888 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
889 if (dialog
.ShowModal() == wxID_OK
)
891 str
= dialog
.GetValue();
897 wxString
wxGetPasswordFromUser(const wxString
& message
,
898 const wxString
& caption
,
899 const wxString
& defaultValue
,
903 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
,
904 wxOK
| wxCANCEL
| wxTE_PASSWORD
);
905 if ( dialog
.ShowModal() == wxID_OK
)
907 str
= dialog
.GetValue();
913 #endif // wxUSE_TEXTDLG
915 wxColour
wxGetColourFromUser(wxWindow
*parent
, const wxColour
& colInit
)
918 data
.SetChooseFull(TRUE
);
921 data
.SetColour((wxColour
&)colInit
); // const_cast
925 wxColourDialog
dialog(parent
, &data
);
926 if ( dialog
.ShowModal() == wxID_OK
)
928 colRet
= dialog
.GetColourData().GetColour();
930 //else: leave it invalid
935 // ----------------------------------------------------------------------------
936 // missing C RTL functions (FIXME shouldn't be here at all)
937 // ----------------------------------------------------------------------------
940 char *strdup(const char *s
)
942 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
947 return ( c
>= 0 && c
< 128 ) ;
951 // ----------------------------------------------------------------------------
953 // ----------------------------------------------------------------------------
955 void wxEnableTopLevelWindows(bool enable
)
957 wxWindowList::Node
*node
;
958 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
959 node
->GetData()->Enable(enable
);
962 // Yield to other apps/messages and disable user input
963 bool wxSafeYield(wxWindow
*win
)
965 wxEnableTopLevelWindows(FALSE
);
966 // always enable ourselves
970 wxEnableTopLevelWindows(TRUE
);
974 // Don't synthesize KeyUp events holding down a key and producing KeyDown
975 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
978 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
980 return TRUE
; // detectable auto-repeat is the only mode MSW supports
986 // ----------------------------------------------------------------------------
987 // network and user id functions
988 // ----------------------------------------------------------------------------
990 // Get Full RFC822 style email address
991 bool wxGetEmailAddress(wxChar
*address
, int maxSize
)
993 wxString email
= wxGetEmailAddress();
997 wxStrncpy(address
, email
, maxSize
- 1);
998 address
[maxSize
- 1] = wxT('\0');
1003 wxString
wxGetEmailAddress()
1007 wxString host
= wxGetHostName();
1010 wxString user
= wxGetUserId();
1013 wxString
email(user
);
1014 email
<< wxT('@') << host
;
1021 wxString
wxGetUserId()
1023 static const int maxLoginLen
= 256; // FIXME arbitrary number
1026 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
1027 buf
.UngetWriteBuf();
1035 wxString
wxGetUserName()
1037 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
1040 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
1041 buf
.UngetWriteBuf();
1049 wxString
wxGetHostName()
1051 static const size_t hostnameSize
= 257;
1054 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
1056 buf
.UngetWriteBuf();
1064 wxString
wxGetFullHostName()
1066 static const size_t hostnameSize
= 257;
1069 bool ok
= wxGetFullHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
1071 buf
.UngetWriteBuf();
1079 wxString
wxGetHomeDir()
1082 wxGetHomeDir(&home
);
1089 wxString
wxGetCurrentDir()
1096 ok
= getcwd(dir
.GetWriteBuf(len
+ 1), len
) != NULL
;
1097 dir
.UngetWriteBuf();
1101 if ( errno
!= ERANGE
)
1103 wxLogSysError(_T("Failed to get current directory"));
1105 return wxEmptyString
;
1109 // buffer was too small, retry with a larger one