X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d08d71a8441b5a8d20ab1f71cc76d5486e667543..cb8ae61385f1c65552585cb55d8f7bd9f1f690b4:/include/wx/snglinst.h diff --git a/include/wx/snglinst.h b/include/wx/snglinst.h index 817e7d45fe..e62d6cfc01 100644 --- a/include/wx/snglinst.h +++ b/include/wx/snglinst.h @@ -13,17 +13,16 @@ #ifndef _WX_SNGLINST_H_ #define _WX_SNGLINST_H_ -#ifdef __GNUG__ - #pragma interface "snglinst.h" -#endif - #if wxUSE_SNGLINST_CHECKER +#include "wx/app.h" +#include "wx/utils.h" + // ---------------------------------------------------------------------------- // wxSingleInstanceChecker // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxSingleInstanceChecker +class WXDLLIMPEXP_BASE wxSingleInstanceChecker { public: // default ctor, use Create() after it @@ -37,29 +36,46 @@ public: Create(name, path); } - // name must be given and be as unique as possible, it is used as the mutex - // name under Win32 and the lock file name under Unix - - // wxTheApp->GetAppName() may be a good value for this parameter + // notice that calling Create() is optional now, if you don't do it before + // calling IsAnotherRunning(), CreateDefault() is used automatically + // + // name it is used as the mutex name under Win32 and the lock file name + // under Unix so it should be as unique as possible and must be non-empty // // path is optional and is ignored under Win32 and used as the directory to // create the lock file in under Unix (default is wxGetHomeDir()) // - // returns FALSE if initialization failed, it doesn't mean that another + // returns false if initialization failed, it doesn't mean that another // instance is running - use IsAnotherRunning() to check it bool Create(const wxString& name, const wxString& path = wxEmptyString); + // use the default name, which is a combination of wxTheApp->GetAppName() + // and wxGetUserId() for mutex/lock file + // + // this is called implicitly by IsAnotherRunning() if the checker hadn't + // been created until then + bool CreateDefault() + { + wxCHECK_MSG( wxTheApp, false, "must have application instance" ); + return Create(wxTheApp->GetAppName() + '-' + wxGetUserId()); + } + // is another copy of this program already running? - bool IsAnotherRunning() const; - -#ifdef __WXMSW__ - // Activates Previous Instance if a window whose Title contains the search string is found - bool ActivatePrevInstance(const wxString & sSearch); - - // Activates Previous Instance and passes CommandLine to wxCommandLineEvent - // if a window with matching Title is found - bool PassCommandLineToPrevInstance(const wxString & sSearch, const wxString & sCmdLine); -#endif - + bool IsAnotherRunning() const + { + if ( !m_impl ) + { + if ( !const_cast(this)->CreateDefault() ) + { + // if creation failed, return false as it's better to not + // prevent this instance from starting up if there is an error + return false; + } + } + + return DoIsAnotherRunning(); + } + // dtor is not virtual, this class is not meant to be used polymorphically ~wxSingleInstanceChecker(); @@ -67,8 +83,13 @@ private: // common part of all ctors void Init() { m_impl = NULL; } + // do check if another instance is running, called only if m_impl != NULL + bool DoIsAnotherRunning() const; + // the implementation details (platform specific) - class WXDLLEXPORT wxSingleInstanceCheckerImpl *m_impl; + class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl *m_impl; + + wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker); }; #endif // wxUSE_SNGLINST_CHECKER