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

Generated on Tue Dec 14 13:18:15 2004 for TNTsdk by  doxygen 1.3.8-20040913