If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Ole Nielsby
In need of some credit
In need of some credit
Posts: 3
Joined: Sat May 26, 2007 9:08 pm
Location: Aarhus, Denmark


Post by Ole Nielsby » Wed Feb 06, 2008 10:16 am

This is a class for writing single instance applications with command line forwarding.

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:
doc (not compiled, may have tag errors):

I don't know if it should go in as a contrib or in the common

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


#include "wx/app.h"
// ----------------------------------------------------------------------
// wxSingleInstanceApp: passing command line to already running instance
// ----------------------------------------------------------------------

#if wxUSE_GUI

struct PrivateSingleInstanceApp;

class WXDLLEXPORT wxSingleInstanceApp : public wxApp
    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();
        // Implementation method for OnExecuteCmdLine/OnValidateCmdLine
    bool ParseCmdLine(int argc, wxChar **argv, bool validateOnly = false);
    PrivateSingleInstanceApp *m_private;

#endif wxUSE_GUI
(8.2 KiB) Downloaded 156 times

Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am

Post by Auria » Wed Feb 06, 2008 2:21 pm

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.
The problem I see is that mac uses a very different paradigm from linux (and windows? no clue) to open file. On linux, the file is given as argument to the program, that can only be given at program start-up time (well, unless you jump through hoops like GIMP does and use stuff like gimp-remote - I think dbus can make it easier now but still...) on mac open file is instead an event, that can be sent at any time. This is why I think it'd be hard to merge the 2 behaviours

(I have no clue about the print document function though)

Post Reply