X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0fb67cd1968707f15341792406da62045e11c5f0..d54598dd855e64cf2086b73cb3b8e3b99c513bbc:/src/common/utilscmn.cpp diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index 3520a5f0f5..1fcc2b7346 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -9,55 +9,62 @@ // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + #ifdef __GNUG__ -#pragma implementation "utils.h" + #pragma implementation "utils.h" #endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif #ifndef WX_PRECOMP -#include "wx/defs.h" -#include "wx/utils.h" -#include "wx/window.h" -#include "wx/menu.h" -#include "wx/frame.h" -#include "wx/msgdlg.h" -#include "wx/textdlg.h" -#endif - -#include "wx/ioswrap.h" - -#if wxUSE_IOSTREAMH - #include -#else - #include -#endif + #include "wx/defs.h" + #include "wx/string.h" + #include "wx/utils.h" + + #if wxUSE_GUI + #include "wx/window.h" + #include "wx/menu.h" + #include "wx/frame.h" + #include "wx/msgdlg.h" + #include "wx/textdlg.h" + #endif // wxUSE_GUI +#endif // WX_PRECOMP #include #include #include #include + #if !defined(__WATCOMC__) #if !(defined(_MSC_VER) && (_MSC_VER > 800)) #include #endif #endif + #include + #ifndef __MWERKS__ -#include -#include + #include + #include #endif #ifdef __SALFORDC__ -#include + #include #endif -// Pattern matching code. +// Pattern matching code. (FIXME) // Yes, this path is deliberate (for Borland compilation) #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */ #include "glob.inc" @@ -66,12 +73,25 @@ #endif #ifdef __WXMSW__ -#include "windows.h" + #include "windows.h" #endif -#define _MAXPATHLEN 500 +// ---------------------------------------------------------------------------- +// function protoypes +// ---------------------------------------------------------------------------- + +#if wxUSE_GUI + static wxWindow *wxFindWindowByLabel1(const wxString& title, wxWindow *parent); + static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow *parent); +#endif // wxUSE_GUI + +// ============================================================================ +// implementation +// ============================================================================ -extern char *wxBuffer; +// ---------------------------------------------------------------------------- +// string functions +// ---------------------------------------------------------------------------- #ifdef __WXMAC__ int strcasecmp(const char *str_1, const char *str_2) @@ -102,7 +122,8 @@ int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) return 0 ; } -#endif +#endif // wxMAC + #ifdef __VMS__ // we have no strI functions under VMS, therefore I have implemented // an inefficient but portable version: convert copies of strings to lowercase @@ -146,7 +167,7 @@ int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) return(result); } -#endif +#endif // __VMS__ #ifdef __WINDOWS__ @@ -161,6 +182,12 @@ int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) #endif #else + +#ifdef __EMX__ +#define strcasecmp stricmp +#define strncasecmp strnicmp +#endif + // This declaration is missing in SunOS! // (Yes, I know it is NOT ANSI-C but its in BSD libc) #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__) @@ -172,15 +199,19 @@ extern "C" #endif #endif /* __WXMSW__ */ +#ifdef __WXPM__ +#define strcasecmp stricmp +#define strncasecmp strnicmp +#endif -char * -copystring (const char *s) +wxChar * +copystring (const wxChar *s) { - if (s == NULL) s = ""; - size_t len = strlen (s) + 1; + if (s == NULL) s = wxT(""); + size_t len = wxStrlen (s) + 1; - char *news = new char[len]; - memcpy (news, s, len); // Should be the fastest + wxChar *news = new wxChar[len]; + memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest return news; } @@ -205,94 +236,93 @@ wxRegisterId (long id) } void -StringToFloat (char *s, float *number) +StringToFloat (wxChar *s, float *number) { if (s && *s && number) - *number = (float) strtod (s, (char **) NULL); + *number = (float) wxStrtod (s, (wxChar **) NULL); } void -StringToDouble (char *s, double *number) +StringToDouble (wxChar *s, double *number) { if (s && *s && number) - *number = strtod (s, (char **) NULL); + *number = wxStrtod (s, (wxChar **) NULL); } -char * -FloatToString (float number, const char *fmt) +wxChar * +FloatToString (float number, const wxChar *fmt) { - static char buf[256]; + static wxChar buf[256]; // sprintf (buf, "%.2f", number); - sprintf (buf, fmt, number); + wxSprintf (buf, fmt, number); return buf; } -char * -DoubleToString (double number, const char *fmt) +wxChar * +DoubleToString (double number, const wxChar *fmt) { - static char buf[256]; + static wxChar buf[256]; - sprintf (buf, fmt, number); + wxSprintf (buf, fmt, number); return buf; } void -StringToInt (char *s, int *number) +StringToInt (wxChar *s, int *number) { if (s && *s && number) - *number = (int) strtol (s, (char **) NULL, 10); + *number = (int) wxStrtol (s, (wxChar **) NULL, 10); } void -StringToLong (char *s, long *number) +StringToLong (wxChar *s, long *number) { if (s && *s && number) - *number = strtol (s, (char **) NULL, 10); + *number = wxStrtol (s, (wxChar **) NULL, 10); } -char * +wxChar * IntToString (int number) { - static char buf[20]; + static wxChar buf[20]; - sprintf (buf, "%d", number); + wxSprintf (buf, wxT("%d"), number); return buf; } -char * +wxChar * LongToString (long number) { - static char buf[20]; + static wxChar buf[20]; - sprintf (buf, "%ld", number); + wxSprintf (buf, wxT("%ld"), number); return buf; } // Array used in DecToHex conversion routine. -static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', - 'C', 'D', 'E', 'F' }; +static wxChar hexArray[] = wxT("0123456789ABCDEF"); // Convert 2-digit hex number to decimal int wxHexToDec(const wxString& buf) { int firstDigit, secondDigit; - if (buf.GetChar(0) >= 'A') - firstDigit = buf.GetChar(0) - 'A' + 10; + if (buf.GetChar(0) >= wxT('A')) + firstDigit = buf.GetChar(0) - wxT('A') + 10; else - firstDigit = buf.GetChar(0) - '0'; + firstDigit = buf.GetChar(0) - wxT('0'); - if (buf.GetChar(1) >= 'A') - secondDigit = buf.GetChar(1) - 'A' + 10; + if (buf.GetChar(1) >= wxT('A')) + secondDigit = buf.GetChar(1) - wxT('A') + 10; else - secondDigit = buf.GetChar(1) - '0'; + secondDigit = buf.GetChar(1) - wxT('0'); return firstDigit * 16 + secondDigit; } // Convert decimal integer to 2-character hex string -void wxDecToHex(int dec, char *buf) +void wxDecToHex(int dec, wxChar *buf) { int firstDigit = (int)(dec/16.0); int secondDigit = (int)(dec - (firstDigit*16.0)); @@ -304,7 +334,7 @@ void wxDecToHex(int dec, char *buf) // Convert decimal integer to 2-character hex string wxString wxDecToHex(int dec) { - char buf[3]; + wxChar buf[3]; wxDecToHex(dec, buf); return wxString(buf); } @@ -351,7 +381,7 @@ StringMatch (char *str1, char *str2, bool subString, bool exact) // Return the current date/time // [volatile] -wxString wxNow( void ) +wxString wxNow() { time_t now = time((time_t *) NULL); char *date = ctime(&now); @@ -359,29 +389,31 @@ wxString wxNow( void ) return wxString(date); } -/* - * Strip out any menu codes - */ +#if wxUSE_GUI -char *wxStripMenuCodes (char *in, char *out) +// ---------------------------------------------------------------------------- +// Strip out any menu codes +// ---------------------------------------------------------------------------- + +wxChar *wxStripMenuCodes (wxChar *in, wxChar *out) { if (!in) - return (char *) NULL; + return (wxChar *) NULL; if (!out) out = copystring(in); - char *tmpOut = out; + wxChar *tmpOut = out; while (*in) { - if (*in == '&') + if (*in == wxT('&')) { // Check && -> &, &x -> x - if (*++in == '&') + if (*++in == wxT('&')) *out++ = *in++; } - else if (*in == '\t') + else if (*in == wxT('\t')) { // Remove all stuff after \t in X mode, and let the stuff as is // in Windows mode. @@ -393,24 +425,23 @@ char *wxStripMenuCodes (char *in, char *out) *out++ = *in++; } // while - *out = '\0'; + *out = wxT('\0'); return tmpOut; } wxString wxStripMenuCodes(const wxString& str) { - char *buf = new char[str.Length() + 1]; - wxStripMenuCodes((char*) (const char*) str, buf); + wxChar *buf = new wxChar[str.Length() + 1]; + wxStripMenuCodes(WXSTRINGCAST str, buf); wxString str1(buf); delete[] buf; return str1; } -/* - * Window search functions - * - */ +// ---------------------------------------------------------------------------- +// Window search functions +// ---------------------------------------------------------------------------- /* * If parent is non-NULL, look through children for a label or title @@ -418,53 +449,54 @@ wxString wxStripMenuCodes(const wxString& str) * */ -static wxWindow *wxFindWindowByLabel1 (const wxString& title, wxWindow * parent); - wxWindow * wxFindWindowByLabel (const wxString& title, wxWindow * parent) { - if (parent) + if (parent) { - return wxFindWindowByLabel1 (title, parent); + return wxFindWindowByLabel1(title, parent); } - else + else { - for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ()) + for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); + node; + node = node->GetNext() ) { - wxWindow *win = (wxWindow *) node->Data (); - wxWindow *retwin = wxFindWindowByLabel1 (title, win); - if (retwin) - return retwin; + wxWindow *win = node->GetData(); + wxWindow *retwin = wxFindWindowByLabel1 (title, win); + if (retwin) + return retwin; } // for() } - return (wxWindow *) NULL; + return (wxWindow *) NULL; } // Recursive static wxWindow * wxFindWindowByLabel1 (const wxString& title, wxWindow * parent) { - if (parent) + if (parent) { - if (parent->GetLabel() == title) - return parent; + if (parent->GetLabel() == title) + return parent; } - if (parent) + if (parent) { - for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ()) + for ( wxWindowList::Node * node = parent->GetChildren().GetFirst(); + node; + node = node->GetNext() ) { - wxWindow *win = (wxWindow *) node->Data (); - wxWindow *retwin = wxFindWindowByLabel1 (title, win); - if (retwin) - return retwin; - } // for() + wxWindow *win = (wxWindow *)node->GetData(); + wxWindow *retwin = wxFindWindowByLabel1 (title, win); + if (retwin) + return retwin; + } } - return (wxWindow *) NULL; // Not found - + return (wxWindow *) NULL; // Not found } /* @@ -473,28 +505,29 @@ wxFindWindowByLabel1 (const wxString& title, wxWindow * parent) * */ -static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow * parent); - wxWindow * wxFindWindowByName (const wxString& title, wxWindow * parent) { - if (parent) + if (parent) { - return wxFindWindowByName1 (title, parent); + return wxFindWindowByName1 (title, parent); } - else + else { - for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ()) + for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); + node; + node = node->GetNext() ) { - wxWindow *win = (wxWindow *) node->Data (); - wxWindow *retwin = wxFindWindowByName1 (title, win); - if (retwin) - return retwin; - } // for() + wxWindow *win = node->GetData(); + wxWindow *retwin = wxFindWindowByName1 (title, win); + if (retwin) + return retwin; + } } - // Failed? Try by label instead. - return wxFindWindowByLabel(title, parent); + + // Failed? Try by label instead. + return wxFindWindowByLabel(title, parent); } // Recursive @@ -533,6 +566,8 @@ wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& i return menuBar->FindMenuItem (menuString, itemString); } +#endif // wxUSE_GUI + /* On Fri, 21 Jul 1995, Paul Craven wrote: @@ -726,26 +761,11 @@ whereami(name) #endif +#if wxUSE_GUI -// Yield to other apps/messages and disable user input -bool wxSafeYield(wxWindow *win) -{ - wxNode *node; - for ( node = wxTopLevelWindows.GetFirst(); - node; - node = node->GetNext() ) - ((wxWindow*)node->GetData())->Enable(FALSE); - - // always enable ourselves - if(win) win->Enable(true); - bool rc = wxYield(); - - for ( node = wxTopLevelWindows.GetFirst(); - node; - node = node->GetNext() ) - ((wxWindow*)node->GetData())->Enable(TRUE); - return rc; -} +// ---------------------------------------------------------------------------- +// GUI helpers +// ---------------------------------------------------------------------------- /* * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp @@ -777,6 +797,7 @@ int wxMessageBox(const wxString& message, const wxString& caption, long style, return ans; } +#if wxUSE_TEXTDLG wxString wxGetTextFromUser(const wxString& message, const wxString& caption, const wxString& defaultValue, wxWindow *parent, int x, int y, bool WXUNUSED(centre) ) @@ -787,6 +808,7 @@ wxString wxGetTextFromUser(const wxString& message, const wxString& caption, else return wxString(""); } +#endif // wxUSE_TEXTDLG #ifdef __MWERKS__ char *strdup(const char *s) @@ -798,21 +820,56 @@ int isascii( int c ) { return ( c >= 0 && c < 128 ) ; } -#endif +#endif // __MWERKS__ + +// ---------------------------------------------------------------------------- +// misc functions +// ---------------------------------------------------------------------------- + +void wxEnableTopLevelWindows(bool enable) +{ + wxWindowList::Node *node; + for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) + node->GetData()->Enable(enable); +} + +// Yield to other apps/messages and disable user input +bool wxSafeYield(wxWindow *win) +{ + wxEnableTopLevelWindows(FALSE); + // always enable ourselves + if ( win ) + win->Enable(TRUE); + bool rc = wxYield(); + wxEnableTopLevelWindows(TRUE); + return rc; +} + +// Don't synthesize KeyUp events holding down a key and producing KeyDown +// events with autorepeat. On by default and always on in wxMSW. wxGTK version +// in utilsgtk.cpp. +#ifndef __WXGTK__ +bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) +{ + return TRUE; // detectable auto-repeat is the only mode MSW supports +} +#endif // !wxGTK + +#endif // wxUSE_GUI // ---------------------------------------------------------------------------- // network and user id functions // ---------------------------------------------------------------------------- // Get Full RFC822 style email address -bool wxGetEmailAddress(char *address, int maxSize) +bool wxGetEmailAddress(wxChar *address, int maxSize) { wxString email = wxGetEmailAddress(); if ( !email ) return FALSE; - strncpy(address, email, maxSize - 1); - address[maxSize - 1] = '\0'; + wxStrncpy(address, email, maxSize - 1); + address[maxSize - 1] = wxT('\0'); return TRUE; } @@ -828,7 +885,7 @@ wxString wxGetEmailAddress() if ( !!user ) { wxString email(user); - email << '@' << host; + email << wxT('@') << host; } } @@ -878,3 +935,18 @@ wxString wxGetHostName() return buf; } +wxString wxGetFullHostName() +{ + static const size_t hostnameSize = 257; + + wxString buf; + bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); + + buf.UngetWriteBuf(); + + if ( !ok ) + buf.Empty(); + + return buf; +} +