Works on win Xp/MSVC9, not tested on other platforms.
Comes with a small demo app which simply lists "opened" files.
Cmd line parsing is supported, with a validation stage that catches bad command lines and --help requests before forwarding them.
I put the files in a zip but the header comes as plaintext
also, for discussion.
There is a problem with the type of the argv argument - in this version I assumed wxChar** but siapp.cpp uses a macro to control it, so it should be easy to adapt.
Files are:
the wxSingleInstanceApp class:
siapp.h
siapp.cpp
doc (not compiled, may have tag errors):
siapp.tex
demo:
sidemo.h
sidemo.cpp
I don't know if it should go in as a contrib or in the common
folder.
In longer terms, I think it might be a candidate for integrating in the wxApp class with a flag for disabling/enabling the feature (compile-time as well as runtime). This would make the features available with
derived classes like wxGLApp.
It might be desirable to let the default implementation of the CmdLine handling do what the Mac does, renaming the MacOpenDocument/MacPrintDocument or whatever they are called, to platform neutral names. This would simplify
the writing of cross-platform document-centric apps.
Now the header
======= siapp.h ========
Code: Select all
#ifndef _SINGLE_INSTANCE_APP_H_
#define _SINGLE_INSTANCE_APP_H_
#include "wx/app.h"
// ----------------------------------------------------------------------
// wxSingleInstanceApp: passing command line to already running instance
// ----------------------------------------------------------------------
#if wxUSE_GUI
struct PrivateSingleInstanceApp;
class WXDLLEXPORT wxSingleInstanceApp : public wxApp
{
public:
wxSingleInstanceApp();
virtual ~wxSingleInstanceApp();
// the virtual functions which may/must be overridden in the derived class
// -----------------------------------------------------------------------
// Overrides wxApp::OnInit() to provide IPC command line passing.
// Delegates to OnInitFirstInstance() and OnInitCmdLine().
//
// Override: rarely
virtual bool CallOnInit();
// Initialise the first instance
// Called when the first instance starts, or when later instances
// are allowed to proceed after communication errors.
//
// Override to perform initialisation that should only be performed
// once, and only in the first running instance - such as setting up
// the document view framework.
// The default version does nothing and returns true.
//
// Override: often
virtual bool OnInit();
// Valdiate the command line.
// This is done before checking for previous instances,
// to allow --help display and command line error messaging
// to be processed without involving a previous instance.
//
// (You probably want the help text where you typed the
// command line, not in the console of a previous instance.)
//
// Return true to proceed, false to quit.
//
// Override: when not using wxCmdLineParser
virtual bool OnValidateCmdLine(int argc, wxChar **argv);
// Notify the app that a command line should be executed.
// Called in the first instance whenever an instance starts.
// The first instance simply calls it, later instances try
// to forward it to the first instances via IPC.
//
// Override to open documents or whatever your app should do
// in response to a command line.
//
// Return true on succesful initialisation, false to silently
// close the application by returning false from OnInit()
//
// Override: when not using wxCmdLineParser
virtual bool OnExecuteCmdLine(int argc, wxChar **argv);
// Notify the app about a failed communication
//
// Override to warn the user about possible multiple instances.
// Return true to allow a newly started instance to proceeed
// even though another instance may be running.
//
// The default implementation issues a wxLogWarning(message)
// and allows the app to proceed. The message is in English.
//
// Override: rarely
virtual bool OnInstanceCheckFailure(const wxChar *message);
// Combine app name and username to a unique name used to
// identify the first instance.
//
// Override if app names clash.
//
// The default implementation concatenates wxApp::GetAppName()
// and wxGetUserId with a separaing hyphen.
//
// Override: rarely
virtual wxString GetUniqueAppUserName() const;
// Directory name for lock and socket file handles
//
// The default implementation returns "/tmp"
//
// Override to use an application or user specific directory.
// Note: the directory must exist and be accessible. No ending "/"
// should be added.
//
// Override: rarely
virtual wxString GetLockfileDirectoryName() const {return wxT("/tmp");}
void CleanUp();
protected:
// Implementation method for OnExecuteCmdLine/OnValidateCmdLine
bool ParseCmdLine(int argc, wxChar **argv, bool validateOnly = false);
private:
PrivateSingleInstanceApp *m_private;
DECLARE_CLASS(wxSingleInstanceApp)
};
#endif wxUSE_GUI
#endif