appmain.h

Go to the documentation of this file.
00001 /**
00002  * \file mgui/appmain.h
00003  * \brief Definitions for MGUI::APP_MAIN and MGUI::APP_BASE classes
00004  *
00005  * \if NODOC
00006  * $Id: appmain.h_v 1.15 2005/09/19 13:38:33 mju Exp $
00007  *
00008  * $Log: appmain.h_v $
00009  * Revision 1.15  2005/09/19 13:38:33  mju
00010  * Rename getTopParent as getTopDlgParent.
00011  *
00012  * Revision 1.14  2005/06/02 15:15:31  mju
00013  * Add app_base::v_PreInitInstance.
00014  *
00015  * Revision 1.13  2005/03/30 23:58:02  scowan
00016  * Fixed for X.
00017  *
00018  * Revision 1.12  2005/03/30 23:36:55  scowan
00019  * Support mfc issues in appmain and appbase.
00020  *
00021  * Revision 1.11  2004/02/26 16:24:05  mju
00022  * Make app_base dtor virtual.
00023  *
00024  * Revision 1.10  2004/02/12 16:20:19  mju
00025  * Update comment for app_base relating to construction via 'new'.
00026  *
00027  * Revision 1.9  2004/01/08 17:58:40  mju
00028  * Remove bad comment.
00029  *
00030  * Revision 1.8  2003/10/03 19:58:19  linux32build!build
00031  * Doxygen
00032  *
00033  * Revision 1.7  2003/09/15 13:49:32  fileserver!dwilliss
00034  * Doxygen
00035  *
00036  * Revision 1.6  2003/09/02 17:41:39  scowan
00037  * Fixed for win32 native.
00038  *
00039  * Revision 1.5  2003/07/29 16:33:30  mju
00040  * Add SetLegacyAllowShellResize.
00041  *
00042  * Revision 1.4  2003/07/15 17:05:47  dwilliss
00043  * Fixed to compile on Unix
00044  *
00045  * Revision 1.3  2003/05/14 20:50:57  mju
00046  * Add GetBaseInstance.
00047  *
00048  * Revision 1.2  2003/04/28 16:52:17  mju
00049  * Add OnTerminate.
00050  *
00051  * Revision 1.1  2003/04/23 21:59:55  mju
00052  * Initial revision
00053  *
00054  * \endif
00055 **/
00056 
00057 #ifndef  INC_MGUI_APPMAIN_H
00058 #define  INC_MGUI_APPMAIN_H
00059 
00060 #ifndef INC_MI32_STDDEFNS_H
00061    #include <mi32/stddefns.h>
00062 #endif
00063 
00064 #if !defined(_XtIntrinsic_h) && !defined(WIN32_MFC)
00065    #include <X11/Intrinsic.h>
00066 #endif
00067 
00068 #if !defined(INC_MI32_STDAFX_H) && defined(WIN32_MFC)
00069 #include <mi32/stdafx.h>
00070 #endif
00071 
00072 struct McbAnyCallbackStruct;
00073 
00074 namespace MGUI {
00075 
00076 class APP_MAIN;
00077 
00078 //! Base class for application process instance.
00079 //! A class derived from this must be constructed by a class derived from APP_MAIN,
00080 //! either in v_InitInstance or in its constructor.  Failure to follow this
00081 //! convention will result in an exception during application startup or shutdown.
00082 //! Note that this is handled automatically for applications based on MGUI::APP_MULTI.
00083 class APP_BASE {
00084    public:
00085 
00086    #ifdef X_NATIVE
00087       enum LEGACYINIT {
00088          LEGACYINIT_Default =             0x00,
00089          LEGACYINIT_NoSetPosition =       0x01, //!< Don't automatically set position
00090          LEGACYINIT_NoSetTitle =          0x02, //!< Don't automatically set title
00091          LEGACYINIT_NoRealize =           0x04, //!< Don't 'realize' toplevel application shell after InitInstance
00092          LEGACYINIT_NoAutoSetExit =       0x08, //!< Don't do MxSetExitProcess() when toplevel shell 'closed' via Window Manager
00093          LEGACYINIT_LockShellSize =       0x10, //!< Lock toplevel shell size after realizing
00094          LEGACYINIT_NoDestroyTopOnExit =  0x20  //!< Don't destroy TopLevel shell on exit as application destroys it
00095          };
00096    #endif
00097 
00098       //! Get number of command-line parameters.
00099       inline int GetCmdParmCount () const;
00100 
00101       //! Get command-line parameter for specified index.
00102       inline const char* GetCmdParm (
00103          int idx                       //!< Command parameter index
00104          ) const;
00105 
00106    #if defined(WIN32_MFC)
00107       static CFont* GetDefaultFont (
00108          );
00109    #endif
00110    
00111    #ifdef USE_MGUI
00112       //! Get top level parent window for dialogs
00113       //! Intended for use by MGUI::SHELL, not usually called directly.
00114       static MDLGPARENT GetTopDlgParent (
00115          );
00116    #endif
00117    
00118    protected:
00119 
00120       //! Constructor.
00121       APP_BASE (
00122          UINT32 AllowedProduct1 = 0,      //!< Product code for license checking
00123          UINT32 AllowedProduct2 = 0,
00124          UINT32 AllowedProduct3 = 0,
00125          UINT32 AllowedProduct4 = 0
00126          );
00127 
00128       //! Destructor
00129       virtual ~APP_BASE ();
00130 
00131    #ifdef X_NATIVE
00132       //! Perform legacy initialization for processes using TopLevel shell (X only).
00133       //! Replaces MxInitProc(), should call from within v_InitInstance().
00134       //! @return Unrealized TopLevel shell.
00135       Widget MxInitLegacy (LEGACYINIT flags = LEGACYINIT_Default);
00136    #endif
00137 
00138    #ifdef X_NATIVE
00139       //! Set to allow resize of legacy toplevel shell.
00140       //! Must be done in derived class constructor.
00141       //! Required for some applications which need to dynamically grow the legacy toplevel shell.
00142       void SetLegacyAllowShellResize (
00143          ) { m_LegacyAllowShellResize = true; }
00144    #endif
00145 
00146    private:
00147 
00148       #ifndef GENERATING_DOXYGEN_OUTPUT
00149 
00150    #ifdef X_NATIVE
00151       LEGACYINIT m_LegacyInitFlags;
00152       bool m_LegacyAllowShellResize;
00153    #endif
00154    #if defined(X_NATIVE) || defined(WIN32_MFC)
00155       MDLGPARENT m_TopParent;
00156    #endif
00157    #if defined(WIN32_MFC)
00158       CFont m_DftFont;
00159       CWnd m_TopWnd;
00160    #endif
00161       UINT32 m_ProductCode;
00162       UINT32 m_AllowedProducts[4];
00163 
00164       UINT32 DetermineProductCode ()
00165          { return (v_DetermineProductCode()); }
00166       void ExitInstance ();
00167       const char* GetProcessName () const
00168          { return (v_GetProcessName()); }
00169       bool InitInstance ();
00170       bool IsProductAllowed (UINT32) const;
00171 
00172       friend class MGUI::APP_MAIN;
00173 
00174       APP_BASE (const APP_BASE&);
00175       APP_BASE& operator= (const APP_BASE&);
00176 
00177       #endif // GENERATING_DOXYGEN_OUTPUT
00178 
00179       // Overridables.
00180 
00181       //! Override to determine product code for license check.
00182       //! Default implementation determines product code based on command
00183       //! line parameters and initialization settings.  Not normally overridden.
00184       virtual UINT32 v_DetermineProductCode () const;
00185 
00186       //! Override to perform resource cleanup prior to application exit.
00187       virtual void v_ExitInstance ();
00188 
00189       //! Must be overridden to return 'process name' for use as 'INI group' and 'registry subkey'.
00190       //! Should return character string having no whitespace.
00191       virtual const char* v_GetProcessName () const = 0;
00192 
00193       //! Must be overridden to perform initialization of this application.
00194       //! Initialization normally includes creation of primary user interface
00195       //! windows prior to entering main message handling loop.
00196       //! @return 'true' if successful, 'false' if not (will exit if false).
00197       virtual bool v_InitInstance () = 0;
00198 
00199       //! Called at startup just prior to MsysAppInitialize.
00200       //! @return true to continue, false to terminate application.
00201       virtual bool v_PreInitInstance ();
00202 
00203    };
00204 #ifdef X_NATIVE
00205 DEFINE_ENUM_OP_BITWISE(APP_BASE::LEGACYINIT);
00206 #endif
00207 
00208 
00209 //! Base class for main application instance.
00210 //! A class must be derived from APP_MAIN and a single instance must be
00211 //! declared at file (static global) scope in the main application module.
00212 //! Only one instance of this class may be defined per executable.
00213 //! See also: APP_BASE.
00214 class APP_MAIN {
00215    public:
00216 
00217       //! Terminate the event loop and exit application normally.
00218       static void Exit () { s_pMainInstance->DoExit(); }
00219 
00220       //! Get reference to the single APP_BASE instance for this executable.
00221       static APP_BASE* GetBaseInstance ()
00222          { return (s_pBaseInstance); }
00223 
00224       //! Get reference to the single APP_MAIN instance for this executable.
00225       static APP_MAIN* GetMainInstance ()
00226          { return (s_pMainInstance); }
00227 
00228       //! Constructor.
00229       APP_MAIN ();
00230 
00231       //! Destructor.
00232       virtual ~APP_MAIN () = 0;
00233 
00234       //! Get number of command-line parameters.
00235       int GetCmdParmCount () const
00236          { return (m_CmdParmCount); }
00237 
00238       //! Get command-line parameter for specified index.
00239       const char* GetCmdParm (
00240          int idx                       //!< Command parameter index
00241          ) const { return ((idx < 0 || idx >= m_CmdParmCount) ? NULL : m_CmdParmArray[idx]); }
00242 
00243    #ifdef WIN32
00244       //! Get Windows application instance handle (Windows only).
00245       void *GetInstanceHandle () const
00246          { return (m_hInstance); }
00247    #endif
00248 
00249       //! Remove command-line parameter at specified index.
00250       //! Note, idx must be > 0 as first entry cannot be removed.
00251       void RemoveCmdParm (
00252          int idx
00253          );
00254 
00255       #ifndef GENERATING_DOXYGEN_OUTPUT
00256    #if defined(WIN32) && !defined(WIN32_NATIVE)
00257       void DoWinMain (void *hInstance, const char *cmdline);
00258    #else
00259       // These should be private, but then main() can't access them.  I tried
00260       // friend int main(int, char**), but it didn't help.
00261    #if !defined(WIN32_MFC)
00262       void Run();
00263    #endif
00264       void SetCmdParms (const char**, int);
00265    #endif
00266       #endif // GENERATING_DOXYGEN_OUTPUT
00267 
00268    private:
00269 
00270       #ifndef GENERATING_DOXYGEN_OUTPUT
00271 
00272       static APP_MAIN *s_pMainInstance;
00273       static APP_BASE *s_pBaseInstance;
00274    #ifdef WIN32
00275       void *m_hInstance;
00276       char *m_ModNameStr;
00277       char *m_CmdLineStr;
00278    #endif
00279 
00280       void DoExit ();
00281       void ExitInstance ();
00282       bool InitInstance ();
00283       static void OnExitXServer (McbAnyCallbackStruct*, void*);
00284       static void OnTerminate ();
00285    #if defined(WIN32) && !defined(WIN32_NATIVE)
00286    #if !defined(WIN32_MFC)
00287       void Run ();
00288    #endif
00289       void SetCmdLine (const char*);
00290    #endif
00291    #if defined(WIN32_MFC)
00292       class MyCWinApp : public CWinApp {
00293          public:
00294             MyCWinApp (
00295                APP_MAIN* AppMain
00296                ) : m_AppMain(AppMain) {}
00297          
00298          private:
00299             APP_MAIN* m_AppMain;
00300             
00301             virtual BOOL InitInstance ();
00302             virtual BOOL ExitInstance ();
00303 
00304             friend class APP_MAIN;
00305          };
00306       friend class MyCWinApp;
00307       
00308       MyCWinApp m_AppInst;
00309    #endif
00310       char **m_CmdParmArray;
00311       int m_CmdParmCount;
00312 
00313       friend class MGUI::APP_BASE;
00314 
00315       APP_MAIN (const APP_MAIN&);
00316       APP_MAIN& operator= (const APP_MAIN&);
00317 
00318       #endif // GENERATING_DOXYGEN_OUTPUT
00319 
00320       // Overridables.
00321 
00322       //! Called just prior to application exit.
00323       //! May be overridden to clean up allocated instance resource.
00324       virtual void v_ExitInstance ();
00325 
00326       //! Called at application startup.
00327       //! Normally overridden if multiple 'processes' are built into a single executable.
00328       //! In this case the command line parameters may be processed to determine which
00329       //! process to invoke and call a function or method to create a static instance of
00330       //! a derived APP_BASE class for that process.
00331       virtual bool v_InitInstance ();
00332 
00333    };
00334 
00335 
00336 #ifndef GENERATING_DOXYGEN_OUTPUT
00337 
00338 //! Get number of command-line parameters.
00339 inline int APP_BASE::GetCmdParmCount (
00340    ) const {
00341    APP_MAIN *pMain = APP_MAIN::GetMainInstance();
00342    return ((pMain == 0) ? 0 : pMain->GetCmdParmCount());
00343    }
00344 
00345 //! Get command-line parameter for specified index.
00346 inline const char* APP_BASE::GetCmdParm (
00347    int idx                       //!< Command parameter index
00348    ) const {
00349    APP_MAIN *pMain = APP_MAIN::GetMainInstance();
00350    return ((pMain == 0) ? 0 : pMain->GetCmdParm(idx));
00351    }
00352 
00353 #endif // GENERATING_DOXYGEN_OUTPUT
00354 
00355 }  // End namespace MGUI
00356 
00357 #endif   // INC_MGUI_APPMAIN_H

Generated on Wed May 31 15:26:43 2006 for TNTsdk by  doxygen 1.3.8-20040913