NIREP

wxNirepApp.cpp

Go to the documentation of this file.
00001 
00002 // Name:        wxnirepapp.cpp
00003 // Purpose:     The main NIREP application
00004 // Author:      James Harris II
00005 // Modified by: Gary E. Christensen
00006 // Created:     25/01/2008 15:55:45
00007 // RCS-ID:
00008 // Copyright:   All rights reserved
00009 // Licence:
00011 
00012 // For compilers that support precompilation, includes "wx/wx.h".
00013 #include "wx/wxprec.h"
00014 
00015 #ifdef __BORLANDC__
00016 #pragma hdrstop
00017 #endif
00018 
00019 #ifndef WX_PRECOMP
00020 #include "wx/wx.h"
00021 #endif
00022 
00023 #include "wxNirepApp.h"
00024 #include "NIREPGenericXMLParser.h"
00025 #include <wx/splash.h>
00026 #include <wx/evtloop.h>
00027 
00028 IMPLEMENT_APP( WxNIREPApp )
00029 
00030 IMPLEMENT_CLASS( WxNIREPApp, wxApp )
00031 
00032 BEGIN_EVENT_TABLE( WxNIREPApp, wxApp )
00033 END_EVENT_TABLE()
00034 
00035 // Added 6/7/2010 GEC for single instance
00036 // Accepts a connection from another instance
00037 wxConnectionBase * nirepServer::OnAcceptConnection( const wxString & topic )
00038 {
00039     if ( topic.Lower() == wxT( "nirepapp" ) ) {
00040         // Check that there are no modal dialogs active
00041         wxWindowList::Node * node = wxTopLevelWindows.GetFirst();
00042         while( node ) {
00043             wxDialog * dialog = wxDynamicCast( node->GetData(), wxDialog );
00044             if ( dialog && dialog->IsModal() ) {
00045                 return NULL;
00046             }
00047 
00048             node = node->GetNext();
00049         }
00050         return new nirepConnection();
00051     } else {
00052         return NULL;
00053     }
00054 }
00055 
00056 // Opens a file passed from another instance
00057 bool nirepConnection::OnExecute( const wxString & WXUNUSED( topic ), wxChar * data,
00058                                  int WXUNUSED( size ), wxIPCFormat WXUNUSED( format ) )
00059 {
00060 
00061     //   GEC:  The following 3 commented lines were used for debugging the single instance ICP code.
00062     //   wxString msg;
00063     //   msg.Printf(wxT("Message received to open %s"), (const wxChar*) data);
00064     //   wxMessageBox(msg,wxT("NIREP"), wxICON_INFORMATION|wxOK);
00065 
00066     std::string filename( wxString( data ).mb_str() );
00067     Interface * frame = wxDynamicCast( wxGetApp().GetTopWindow(), Interface );
00068     frame->CreateNIREPDisplay( filename );
00069 
00070     //  GEC: The following code was taken from the wxWidgets book.  It will not work
00071     //  unless the DocManager object and associated framework are added to the program.
00072     /*      wxString filename(data);
00073             if (filename.IsEmpty()){
00074                 // Just raise the main window
00075                 if (frame){
00076                     frame->Raise();
00077                 }
00078             } else {
00079                 // Check if the filename is already open, and raise that instead.
00080                 wxNode * node = wxGetApp().GetDocManager()->GetDocuments().GetFirst();
00081                 while (node) {
00082                     NIREPDisplay * doc = wxDynamicCast(node->GetData(),NIREPDisplay);
00083                     if (doc && doc->GetFileName() == filename){
00084                         if (doc->GetFrame()){
00085                             doc->GetFrame()->Raise();
00086                         }
00087                     }
00088                     node = node->GetNext();
00089                 }
00090                 mainWindow->CreateDisplay(filename);
00091             }
00092     */
00093     return true;
00094 }
00095 
00100 WxNIREPApp::WxNIREPApp()
00101 {
00102 
00103 // Added 6/7/2010 GEC for command line and server
00104 #if wxUSE_IPC
00105     m_server = NULL;
00106     m_singleInstanceChecker = NULL;
00107 #endif
00108 
00109     Init();
00110 }
00111 
00112 WxNIREPApp::~WxNIREPApp()
00113 {
00114     // Delete mainWindow;
00115     if( mainWindow ) {
00116         delete mainWindow;
00117     }
00118     mainWindow = NULL;
00119 
00120 }
00121 
00126 void WxNIREPApp::Init()
00127 {
00128     mainWindow = NULL;
00129 }
00130 
00135 bool WxNIREPApp::OnInit()
00136 {
00137 
00138 #if wxUSE_XPM
00139     wxImage::AddHandler( new wxXPMHandler );
00140 #endif
00141 #if wxUSE_LIBPNG
00142     wxImage::AddHandler( new wxPNGHandler );
00143 #endif
00144 #if wxUSE_LIBJPEG
00145     wxImage::AddHandler( new wxJPEGHandler );
00146 #endif
00147 #if wxUSE_GIF
00148     wxImage::AddHandler( new wxGIFHandler );
00149 #endif
00150 
00151     //NIREPGenericXMLParser * xml = NIREPGenericXMLParser::New();
00152     //xml->SetFileName("resource.xml");
00153     //xml->Parse();
00154     //Destroy(xml->root);
00155     //xml->Delete();
00156 
00157 #ifndef wxUSE_IPC
00158     wxBitmap bitmap;
00159     wxSplashScreen* splash = NULL;
00160     if( bitmap.LoadFile( "../MenuOptions/Logo2skull.png", wxBITMAP_TYPE_PNG ) ) {
00161 
00162         splash = new wxSplashScreen( bitmap,
00163                                      wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT,
00164                                      3000, NULL, -1, wxDefaultPosition, wxDefaultSize,
00165                                      wxSIMPLE_BORDER | wxSTAY_ON_TOP );
00166     }
00167 
00168     wxYield();
00169 #endif
00170 
00171     // Added 6/7/2010 GEC for parsing the command line arguments
00172     wxString cmdFilename;
00173     wxCmdLineParser cmdParser( g_cmdLineDesc, argc, argv );
00174     int res;
00175 
00176     // Pass false to suppress auto Usage() message
00177     res = cmdParser.Parse( false );
00178 
00179     // check if the user asked for command-line help
00180     if ( res == -1 || res > 0 || cmdParser.Found( wxT( "h" ) ) ) {
00181         cmdParser.Usage();
00182         return false;
00183     }
00184 
00185     // check if teh user asked for the version
00186     if ( cmdParser.Found( wxT( "v" ) ) ) {
00187         wxString msg;
00188         wxString date( wxString::FromAscii( __DATE__ ) );
00189         msg.Printf( wxT( "NIREP, (c) Gary E. Christensen, 2010 %s, %s\n" ), wxVERSION_STRING, date );
00190         std::cout << msg.mb_str();
00191         return false;
00192     }
00193 
00194     // Check for a project filename
00195     if ( cmdParser.GetParamCount() > 0 ) {
00196         cmdFilename = cmdParser.GetParam( 0 );
00197 
00198         // Under Windows when invoking via a document
00199         // in Explorer, we are passed the short form.
00200         // So normalize and make the long form.
00201         wxFileName fName( cmdFilename );
00202         fName.Normalize( wxPATH_NORM_LONG | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE );
00203         cmdFilename = fName.GetFullPath();
00204     }
00205 
00207     // Instantiate the main NIREP application window.
00208     // The main application window also instantiates the evaluator and data manager.
00210     Interface* mainWindow = new Interface( NULL, ID_FRAME );
00211     mainWindow->SetCmdLine( argc, argv );  
00212     //bitmap.FreeResource(true);
00213 
00214 #if wxUSE_IPC
00215     // Create a single instance checker to see if there is another NIREP application running or not
00216     m_singleInstanceChecker = new wxSingleInstanceChecker( wxT( "nirepapp" ) );
00217 
00218     // Create a server object if no other instance of NIREP is running
00219     // Otherwise create a client object and set up IPC to communicate with the server
00220     if ( !m_singleInstanceChecker->IsAnotherRunning() ) {
00221 
00222         // Show splash screen for first instance only
00223         wxBitmap bitmap;
00224         wxSplashScreen* splash = NULL;
00225         if( bitmap.LoadFile( wxT( "../MenuOptions/Logo2skull.png" ), wxBITMAP_TYPE_PNG ) ) {
00226 
00227             splash = new wxSplashScreen( bitmap,
00228                                          wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT,
00229                                          3000, NULL, -1, wxDefaultPosition, wxDefaultSize,
00230                                          wxSIMPLE_BORDER | wxSTAY_ON_TOP );
00231         }
00232         wxYield();
00233 
00234         // Create a new server to signify that first instance of program is running
00235         m_server = new nirepServer;
00236 
00237         if ( !m_server->Create( wxT( "nirepapp" ) ) ) {
00238             wxLogDebug( wxT( "Failed to create an IPC service." ) );
00239         }
00240 
00241         // // Open new display description if called with command line argument
00242         if ( cmdParser.GetParamCount() > 0 ) {
00243             std::string filename( cmdFilename.mb_str() );
00244             Interface * frame = wxDynamicCast( wxGetApp().GetTopWindow(), Interface );
00245             
00246             //Jeffrey Hawley
00247             //Found out that there is a major problem with wxProgressDialog and needing an 
00248             //event loop.  For some reason the loop gets created after this function, so
00249             //we have to create a temporary event loop.  See http://old.nabble.com/wxProgressDialog-initialization-td29617194.html
00250             //for a deeper insight.
00251             //
00252             wxEventLoopGuarantor ensureEventLoop;
00253             frame->CreateNIREPDisplay( filename );
00254         }
00255 
00256         // GEC: This seems a little silly to kill the splash screen already.  Fix in future.
00257         if( splash != NULL ) {
00258             delete splash;
00259         }
00260         splash = NULL;
00261     } else {
00262         wxLogNull logNull;
00263 
00264         // OK, there IS another one running, so try to connect to it
00265         // and send it any filename before exiting
00266         nirepClient * client = new nirepClient;
00267 
00268         // ignored under DDE, host name in TCP/IP based classes
00269         wxString hostName = wxT( "localhost" );
00270 
00271         // Create the connection
00272         wxConnectionBase * connection =  client->MakeConnection( hostName, wxT( "nirepapp" ), wxT( "nirepapp" ) );
00273 
00274         if ( connection ) {
00275             // Ask the other instance to open a file or raise itself
00276             connection->Execute( cmdFilename );
00277             connection->Disconnect();
00278 
00279             if ( connection != NULL ) {
00280                 delete connection;
00281             }
00282             connection = NULL;
00283 
00284         } else {
00285             wxMessageBox( wxT( "Sorry, the existing instance may be too busy to respond.\nPlease close any open dialogs and retry." ), wxT( "NIREP" ), wxICON_INFORMATION | wxOK );
00286 
00287         }
00288 
00289         if ( client != NULL ) {
00290             delete client;
00291         }
00292         client = NULL;
00293 
00294         return false;
00295     }
00296 
00297 #endif
00298 
00299 #ifndef wxUSE_IPC
00300     if( splash != NULL ) {
00301         delete splash;
00302     }
00303     splash = NULL;
00304 #endif
00305 
00306     mainWindow->Show( true );
00307 
00308     return true;
00309 }
00310 
00311 
00316 int WxNIREPApp::OnExit()
00317 {
00318 
00319 #if wxUSE_IPC
00320     if ( m_singleInstanceChecker != NULL ) {
00321         delete m_singleInstanceChecker;
00322     }
00323     m_singleInstanceChecker = NULL;
00324 
00325     if ( m_server != NULL ) {
00326         delete m_server;
00327     }
00328     m_server = NULL;
00329 #endif
00330 
00331     return wxApp::OnExit();
00332 }
00333 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines