X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5276b0a53cef4815230e39b54d2ecda14f72cbd1..a4ea083bbdf710d3234dcbd2eafb5d0e078f8348:/src/common/utilscmn.cpp diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index 53791d386f..6c597072a9 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -4,7 +4,6 @@ // Author: Julian Smart // Modified by: // Created: 29/01/98 -// RCS-ID: $Id$ // Copyright: (c) 1998 Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -52,6 +51,7 @@ #include "wx/uri.h" #include "wx/mimetype.h" #include "wx/config.h" +#include "wx/versioninfo.h" #if defined(__WXWINCE__) && wxUSE_DATETIME #include "wx/datetime.h" @@ -69,32 +69,26 @@ #endif #if wxUSE_GUI - #include "wx/colordlg.h" - #include "wx/fontdlg.h" #include "wx/notebook.h" #include "wx/statusbr.h" #endif // wxUSE_GUI -#ifndef __WXPALMOS5__ #ifndef __WXWINCE__ #include #else #include "wx/msw/wince/time.h" #endif -#endif // ! __WXPALMOS5__ #ifdef __WXMAC__ #include "wx/osx/private.h" #endif -#ifndef __WXPALMOS5__ -#if !defined(__MWERKS__) && !defined(__WXWINCE__) +#if !defined(__WXWINCE__) #include #include #endif -#endif // ! __WXPALMOS5__ -#if defined(__WXMSW__) +#if defined(__WINDOWS__) #include "wx/msw/private.h" #include "wx/filesys.h" #endif @@ -113,21 +107,12 @@ static const wxChar hexArray[] = wxT("0123456789ABCDEF"); // Convert 2-digit hex number to decimal -int wxHexToDec(const wxString& buf) +int wxHexToDec(const wxString& str) { - int firstDigit, secondDigit; - - if (buf.GetChar(0) >= wxT('A')) - firstDigit = buf.GetChar(0) - wxT('A') + 10; - else - firstDigit = buf.GetChar(0) - wxT('0'); - - if (buf.GetChar(1) >= wxT('A')) - secondDigit = buf.GetChar(1) - wxT('A') + 10; - else - secondDigit = buf.GetChar(1) - wxT('0'); - - return (firstDigit & 0xF) * 16 + (secondDigit & 0xF ); + char buf[2]; + buf[0] = str.GetChar(0); + buf[1] = str.GetChar(1); + return wxHexToDec((const char*) buf); } // Convert decimal integer to 2-character hex string @@ -362,7 +347,7 @@ void wxPlatform::ClearPlatforms() bool wxPlatform::Is(int platform) { -#ifdef __WXMSW__ +#ifdef __WINDOWS__ if (platform == wxOS_WINDOWS) return true; #endif @@ -402,10 +387,6 @@ bool wxPlatform::Is(int platform) if (platform == wxOS_UNIX) return true; #endif -#ifdef __WXMGL__ - if (platform == wxPORT_MGL) - return true; -#endif #ifdef __OS2__ if (platform == wxOS_OS2) return true; @@ -552,17 +533,99 @@ wxString wxGetCurrentDir() #endif // 0 +// ---------------------------------------------------------------------------- +// Environment +// ---------------------------------------------------------------------------- + +#ifdef __WXOSX__ +#if wxOSX_USE_COCOA_OR_CARBON + #include +#endif +#endif + +bool wxGetEnvMap(wxEnvVariableHashMap *map) +{ + wxCHECK_MSG( map, false, wxS("output pointer can't be NULL") ); + +#if defined(__VISUALC__) + // This variable only exists to force the CRT to fill the wide char array, + // it might only have it in narrow char version until now as we use main() + // (and not _wmain()) as our entry point. + static wxChar* s_dummyEnvVar = _tgetenv(wxT("TEMP")); + + wxChar **env = _tenviron; +#elif defined(__VMS) + // Now this routine wil give false for OpenVMS + // TODO : should we do something with logicals? + char **env=NULL; +#elif defined(__DARWIN__) +#if wxOSX_USE_COCOA_OR_CARBON + // Under Mac shared libraries don't have access to the global environ + // variable so use this Mac-specific function instead as advised by + // environ(7) under Darwin + char ***penv = _NSGetEnviron(); + if ( !penv ) + return false; + char **env = *penv; +#else + char **env=NULL; + // todo translate NSProcessInfo environment into map +#endif +#else // non-MSVC non-Mac + // Not sure if other compilers have _tenviron so use the (more standard) + // ANSI version only for them. + + // Both POSIX and Single UNIX Specification say that this variable must + // exist but not that it must be declared anywhere and, indeed, it's not + // declared in several common systems (some BSDs, Solaris with native CC) + // so we (re)declare it ourselves to deal with these cases. However we do + // not do this under MSW where there can be DLL-related complications, i.e. + // the variable might be DLL-imported or not. Luckily we don't have to + // worry about this as all MSW compilers do seem to define it in their + // standard headers anyhow so we can just rely on already having the + // correct declaration. And if this turns out to be wrong, we can always + // add a configure test checking whether it is declared later. +#ifndef __WINDOWS__ + extern char **environ; +#endif // !__WINDOWS__ + + char **env = environ; +#endif + + if ( env ) + { + wxString name, + value; + while ( *env ) + { + const wxString var(*env); + + name = var.BeforeFirst(wxS('='), &value); + + (*map)[name] = value; + + env++; + } + + return true; + } + + return false; +} + // ---------------------------------------------------------------------------- // wxExecute // ---------------------------------------------------------------------------- -// wxDoExecuteWithCapture() helper: reads an entire stream into one array +// wxDoExecuteWithCapture() helper: reads an entire stream into one array if +// the stream is non-NULL (it doesn't do anything if it's NULL). // // returns true if ok, false if error #if wxUSE_STREAMS static bool ReadAll(wxInputStream *is, wxArrayString& output) { - wxCHECK_MSG( is, false, wxT("NULL stream in wxExecute()?") ); + if ( !is ) + return true; // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state is->Reset(); @@ -599,26 +662,26 @@ static bool ReadAll(wxInputStream *is, wxArrayString& output) static long wxDoExecuteWithCapture(const wxString& command, wxArrayString& output, wxArrayString* error, - int flags) + int flags, + const wxExecuteEnv *env) { // create a wxProcess which will capture the output wxProcess *process = new wxProcess; process->Redirect(); - long rc = wxExecute(command, wxEXEC_SYNC | flags, process); + long rc = wxExecute(command, wxEXEC_SYNC | flags, process, env); #if wxUSE_STREAMS - if ( rc != -1 ) + // Notice that while -1 indicates an error exit code for us, a program + // exiting with this code could still have written something to its stdout + // and, especially, stderr, so we still need to read from them. + if ( !ReadAll(process->GetInputStream(), output) ) + rc = -1; + + if ( error ) { - if ( !ReadAll(process->GetInputStream(), output) ) + if ( !ReadAll(process->GetErrorStream(), *error) ) rc = -1; - - if ( error ) - { - if ( !ReadAll(process->GetErrorStream(), *error) ) - rc = -1; - } - } #else wxUnusedVar(output); @@ -630,17 +693,19 @@ static long wxDoExecuteWithCapture(const wxString& command, return rc; } -long wxExecute(const wxString& command, wxArrayString& output, int flags) +long wxExecute(const wxString& command, wxArrayString& output, int flags, + const wxExecuteEnv *env) { - return wxDoExecuteWithCapture(command, output, NULL, flags); + return wxDoExecuteWithCapture(command, output, NULL, flags, env); } long wxExecute(const wxString& command, wxArrayString& output, wxArrayString& error, - int flags) + int flags, + const wxExecuteEnv *env) { - return wxDoExecuteWithCapture(command, output, &error, flags); + return wxDoExecuteWithCapture(command, output, &error, flags, env); } // ---------------------------------------------------------------------------- @@ -648,9 +713,9 @@ long wxExecute(const wxString& command, // ---------------------------------------------------------------------------- // Id generation -static long wxCurrentId = 100; +static int wxCurrentId = 100; -long wxNewId() +int wxNewId() { // skip the part of IDs space that contains hard-coded values: if (wxCurrentId == wxID_LOWEST) @@ -659,11 +724,11 @@ long wxNewId() return wxCurrentId++; } -long +int wxGetCurrentId(void) { return wxCurrentId; } void -wxRegisterId (long id) +wxRegisterId (int id) { if (id >= wxCurrentId) wxCurrentId = id + 1; @@ -753,8 +818,8 @@ typedef struct smaller partition. This *guarantees* no more than log (n) stack size is needed (actually O(1) in this case)! */ -void wxQsort(void *const pbase, size_t total_elems, - size_t size, CMPFUNCDATA cmp, const void* user_data) +void wxQsort(void* pbase, size_t total_elems, + size_t size, wxSortCallback cmp, const void* user_data) { register char *base_ptr = (char *) pbase; const size_t max_thresh = MAX_THRESH * size; @@ -936,7 +1001,7 @@ bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) // Launch default browser // ---------------------------------------------------------------------------- -#if defined(__WXMSW__) +#if defined(__WINDOWS__) // implemented in a port-specific utils source file: bool wxDoLaunchDefaultBrowser(const wxString& url, const wxString& scheme, int flags); @@ -1002,7 +1067,7 @@ static bool DoLaunchDefaultBrowserHelper(const wxString& urlOrig, int flags) // (e.g. "C:\\test.txt" when parsed by wxURI reports a scheme == "C") bool hasValidScheme = uri.HasScheme() && uri.GetScheme().length() > 1; -#if defined(__WXMSW__) +#if defined(__WINDOWS__) // NOTE: when testing wxMSW's wxLaunchDefaultBrowser all possible forms // of the URL/flags should be tested; e.g.: @@ -1122,22 +1187,22 @@ wxString wxStripMenuCodes(const wxString& in, int flags) size_t len = in.length(); out.reserve(len); - for ( size_t n = 0; n < len; n++ ) + for ( wxString::const_iterator it = in.begin(); it != in.end(); ++it ) { - wxChar ch = in[n]; + wxChar ch = *it; if ( (flags & wxStrip_Mnemonics) && ch == wxT('&') ) { // skip it, it is used to introduce the accel char (or to quote // itself in which case it should still be skipped): note that it // can't be the last character of the string - if ( ++n == len ) + if ( ++it == in.end() ) { wxLogDebug(wxT("Invalid menu string '%s'"), in.c_str()); } else { // use the next char instead - ch = in[n]; + ch = *it; } } else if ( (flags & wxStrip_Accel) && ch == wxT('\t') ) @@ -1213,7 +1278,7 @@ wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt) // Hack for wxNotebook case: at least in wxGTK, all pages // claim to be shown, so we must only deal with the selected one. #if wxUSE_NOTEBOOK - if (win->IsKindOf(CLASSINFO(wxNotebook))) + if (wxDynamicCast(win, wxNotebook)) { wxNotebook* nb = (wxNotebook*) win; int sel = nb->GetSelection(); @@ -1302,6 +1367,8 @@ int wxMessageBox(const wxString& message, const wxString& caption, long style, return wxNO; case wxID_CANCEL: return wxCANCEL; + case wxID_HELP: + return wxHELP; } wxFAIL_MSG( wxT("unexpected return code from wxMessageDialog") ); @@ -1309,7 +1376,7 @@ int wxMessageBox(const wxString& message, const wxString& caption, long style, return wxCANCEL; } -void wxInfoMessageBox(wxWindow* parent) +wxVersionInfo wxGetLibraryVersionInfo() { // don't translate these strings, they're for diagnostics purposes only wxString msg; @@ -1342,7 +1409,20 @@ void wxInfoMessageBox(wxWindow* parent) GTK_MICRO_VERSION); #endif // __WXGTK__ - msg += wxS("\nCopyright (c) 1995-2010 wxWidgets team"); + return wxVersionInfo(wxS("wxWidgets"), + wxMAJOR_VERSION, + wxMINOR_VERSION, + wxRELEASE_NUMBER, + msg, + wxS("Copyright (c) 1995-2011 wxWidgets team")); +} + +void wxInfoMessageBox(wxWindow* parent) +{ + wxVersionInfo info = wxGetLibraryVersionInfo(); + wxString msg = info.ToString(); + + msg << wxS("\n") << info.GetCopyright(); wxMessageBox(msg, wxT("wxWidgets information"), wxICON_INFORMATION | wxOK, @@ -1401,89 +1481,6 @@ wxString wxGetPasswordFromUser(const wxString& message, #endif // wxUSE_TEXTDLG -#if wxUSE_COLOURDLG - -wxColour wxGetColourFromUser(wxWindow *parent, - const wxColour& colInit, - const wxString& caption, - wxColourData *ptrData) -{ - // contains serialized representation of wxColourData used the last time - // the dialog was shown: we want to reuse it the next time in order to show - // the same custom colours to the user (and we can't just have static - // wxColourData itself because it's a GUI object and so should be destroyed - // before GUI shutdown and doing it during static cleanup is too late) - static wxString s_strColourData; - - wxColourData data; - if ( !ptrData ) - { - ptrData = &data; - if ( !s_strColourData.empty() ) - { - if ( !data.FromString(s_strColourData) ) - { - wxFAIL_MSG( "bug in wxColourData::FromString()?" ); - } - -#ifdef __WXMSW__ - // we don't get back the "choose full" flag value from the native - // dialog and so we can't preserve it between runs, so we decide to - // always use it as it seems better than not using it (user can - // just ignore the extra controls in the dialog but having to click - // a button each time to show them would be very annoying - data.SetChooseFull(true); -#endif // __WXMSW__ - } - } - - if ( colInit.IsOk() ) - { - ptrData->SetColour(colInit); - } - - wxColour colRet; - wxColourDialog dialog(parent, ptrData); - if (!caption.empty()) - dialog.SetTitle(caption); - if ( dialog.ShowModal() == wxID_OK ) - { - *ptrData = dialog.GetColourData(); - colRet = ptrData->GetColour(); - s_strColourData = ptrData->ToString(); - } - //else: leave colRet invalid - - return colRet; -} - -#endif // wxUSE_COLOURDLG - -#if wxUSE_FONTDLG - -wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit, const wxString& caption) -{ - wxFontData data; - if ( fontInit.Ok() ) - { - data.SetInitialFont(fontInit); - } - - wxFont fontRet; - wxFontDialog dialog(parent, data); - if (!caption.empty()) - dialog.SetTitle(caption); - if ( dialog.ShowModal() == wxID_OK ) - { - fontRet = dialog.GetFontData().GetChosenFont(); - } - //else: leave it invalid - - return fontRet; -} - -#endif // wxUSE_FONTDLG - // ---------------------------------------------------------------------------- // wxSafeYield and supporting functions // ---------------------------------------------------------------------------- @@ -1495,6 +1492,12 @@ void wxEnableTopLevelWindows(bool enable) node->GetData()->Enable(enable); } +#if defined(__WXOSX__) && wxOSX_USE_COCOA + +// defined in evtloop.mm + +#else + wxWindowDisabler::wxWindowDisabler(bool disable) { m_disabled = disable; @@ -1557,6 +1560,8 @@ wxWindowDisabler::~wxWindowDisabler() delete m_winDisabled; } +#endif + // Yield to other apps/messages and disable user input to all windows except // the given one bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)