home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 19.ddi / OWLINC.PAK / APPLICAT.H next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  8.1 KB  |  283 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //   include\owl\applicat.h
  4. //   Defines class TApplication. This defines the basic behavior
  5. //   for OWL applications.
  6. //----------------------------------------------------------------------------
  7. #if !defined(__OWL_APPLICAT_H)
  8. #define __OWL_APPLICAT_H
  9.  
  10. #if !defined(__OWL_MODULE_H)
  11.   #include <owl\module.h>
  12. #endif
  13. #if defined(__WIN32__) && !defined(__CLASSLIB_THREAD_H)
  14.   #include <classlib\thread.h>
  15. #endif
  16.  
  17. class _OWLCLASS TFrameWindow;
  18. class _OWLCLASS TDocManager;
  19.  
  20. //
  21. //  class TApplication
  22. //  ----- ------------
  23. //
  24. class _OWLCLASS TApplication : public TModule {
  25.   public:
  26.     class _OWLCLASS_RTL TXInvalidMainWindow : public TXOwl {
  27.       public:
  28.         TXInvalidMainWindow();
  29.         TXOwl* Clone();
  30.         void Throw();
  31.     };
  32.  
  33.     HINSTANCE     hPrevInstance;
  34.     int           nCmdShow;
  35.  
  36.     TDocManager*  DocManager;  // will become private, use Get/SetDocManager()
  37.     TFrameWindow* MainWindow;  // will become private, use Get/SetMainWindow()
  38.     HACCEL        HAccTable;
  39.  
  40.     TApplication(const char far* name = 0, TModule*& gModule = ::Module);
  41.     TApplication(const char far* name,
  42.                  HINSTANCE       hInstance,
  43.                  HINSTANCE       hPrevInstance,
  44.                  const char far* cmdLine,
  45.                  int             cmdShow,
  46.                  TModule*&       gModule = ::Module);
  47.  
  48.    ~TApplication();
  49.  
  50.     TFrameWindow*    GetMainWindow() {return MainWindow;}
  51.     TDocManager*     GetDocManager() {return DocManager;}
  52.  
  53.     static void      SetWinMainParams(HINSTANCE       hInstance,
  54.                                       HINSTANCE       hPrevInstance,
  55.                                       const char far* cmdLine,
  56.                                       int             cmdShow);
  57.  
  58.     void             GetWinMainParams();
  59.  
  60.     virtual BOOL     CanClose();
  61.     virtual int      Run();
  62.  
  63. #if defined(__WIN32__)
  64.     TMutex *GetMutex();
  65.  
  66.     class _OWLCLASS TAppMutex {
  67.       public:
  68.         TAppMutex();
  69.         ~TAppMutex();
  70.         TMutex *GetMutex();
  71.         static int HasMutex();
  72.       private:
  73.         char Mutex[sizeof(TMutex)];
  74.         static int NotWIN32s;
  75.     };
  76.  
  77.     class _OWLCLASS TAppLock {
  78.       public:
  79.         TAppLock(TApplication &app);
  80.         ~TAppLock();
  81.         void Release();
  82.       private:
  83.         char AppLock[sizeof(TMutex::Lock)];
  84.     };
  85.  
  86.     //
  87.     // override TEventHandler::Dispatch() to handle multi-thread
  88.     // synchronization
  89.     //
  90.     virtual LRESULT  Dispatch(TEventInfo& info, WPARAM wp, LPARAM lp = 0);
  91. #endif
  92.  
  93.     BOOL             PumpWaitingMessages();  // pumps all waiting msgs
  94.     virtual int      MessageLoop();          // Loops until break or WM_QUIT
  95.     virtual BOOL     ProcessAppMsg(MSG& msg);
  96.     void             SuspendThrow(xalloc& x); // saves xalloc exception info
  97.     void             SuspendThrow(xmsg& x);   // saves xmsg exception info
  98.     void             SuspendThrow(TXOwl& x);  // saves copy of TXOwl exception
  99.     void             SuspendThrow(int);       // set bit flag to log exception
  100.     void             ResumeThrow(); // checks and rethrows suspended exceptions
  101.     int              QueryThrow() {return XState;}  // return suspend flags
  102.     enum {
  103.       xsUnknown   = 1,
  104.       xsBadCast   = 2,
  105.       xsBadTypeid = 4,
  106.       xsMsg       = 8,
  107.       xsAlloc     = 16,
  108.       xsOwl       = 32,
  109.     };
  110.  
  111.     //
  112.     // begin and end of a modal window's modal message loop
  113.     //
  114.     int              BeginModal(TWindow* window, int flags=MB_APPLMODAL);
  115.     void             EndModal(int result);
  116.     virtual void     PreProcessMenu(HMENU);  // called from MainWindow
  117.  
  118.     //
  119.     // Dead TWindow garbage collection
  120.     //
  121.     void             Condemn(TWindow* win);
  122.  
  123.     //
  124.     // Call this function after each msg dispatch if TApplication's message
  125.     // loop is not used.
  126.     //
  127.     void             PostDispatchAction();
  128.   
  129.     //
  130.     // TApplication has no event table itself, defers event handling to 
  131.     // DocManager if it has been installed.
  132.     //
  133.     BOOL  Find(TEventInfo&, TEqualOperator = 0);
  134.  
  135.     void             EnableBWCC(BOOL enable = TRUE, UINT language = 0);
  136.     BOOL             BWCCEnabled() const {return BWCCOn;}
  137.     TModule*         GetBWCCModule() const {return BWCCModule;}
  138.  
  139.     void             EnableCtl3d(BOOL enable = TRUE);
  140.     void             EnableCtl3dAutosubclass(BOOL enable);
  141.     BOOL             Ctl3dEnabled() const {return Ctl3dOn;}
  142.     TModule*         GetCtl3dModule() const {return Ctl3dModule;}
  143.  
  144.   protected:
  145.     BOOL        BreakMessageLoop;
  146.     int         MessageLoopResult;
  147.  
  148.     virtual void     InitApplication();  // "first"-instance initialization
  149.     virtual void     InitInstance();     // each-instance initialization
  150.     virtual void     InitMainWindow();   // init application main window
  151.     virtual int      TermInstance(int status); // each-instance termination
  152.  
  153.     //
  154.     // (re)set a new main window either at construction or sometime later
  155.     //
  156.     TFrameWindow*    SetMainWindow(TFrameWindow* window);
  157.  
  158.     TDocManager*     SetDocManager(TDocManager* docManager);
  159.  
  160.     //
  161.     // called each time there are no messages in the queue. idle count is
  162.     // incremented each time, & zeroed when messages are pumped. Return
  163.     // whether or not more processing needs to be done.
  164.     //
  165.     // default behavior is to give the main window an opportunity to do idle
  166.     // processing by invoking its IdleAction() member function when
  167.     // "idleCount" is 0
  168.     //
  169.     virtual BOOL     IdleAction(long idleCount);
  170.  
  171.   private:
  172.     BOOL          BWCCOn;
  173.     TModule*      BWCCModule;
  174.  
  175.     BOOL          Ctl3dOn;
  176.     TModule*      Ctl3dModule;
  177.     
  178. #if defined(__WIN32__)
  179.     TAppMutex     Mutex;
  180. #endif
  181.     
  182.     static HINSTANCE       InitHInstance;
  183.     static HINSTANCE       InitHPrevInstance;
  184.     static const char far* InitCmdLine;
  185.     static int             InitCmdShow;
  186.  
  187.     //
  188.     // exception handling state
  189.     //
  190.     int    XState;
  191.     string XString;
  192.     size_t XSize;
  193.     TXOwl* XOwl;
  194.  
  195.     //
  196.     // Condemned TWindow garbage collection
  197.     //
  198.     void             DeleteCondemned();
  199.     TWindow*         CondemnedWindows;
  200.  
  201.     //
  202.     // hidden to prevent accidental copying or assignment
  203.     //
  204.     TApplication(const TApplication&);
  205.     TApplication& operator =(const TApplication&);
  206.  
  207.   DECLARE_STREAMABLE(_OWLCLASS, TApplication, 1);
  208. };
  209.  
  210.  
  211. #if defined(__WIN32__)
  212.  
  213. inline TMutex *TApplication::GetMutex()
  214. {
  215.   return Mutex.GetMutex();
  216. }
  217.  
  218. inline TApplication::TAppMutex::TAppMutex()
  219. {
  220.   if (HasMutex())
  221.     new(Mutex)TMutex;
  222. }
  223.  
  224. inline TApplication::TAppMutex::~TAppMutex()
  225. {
  226.   if (HasMutex())
  227.     REINTERPRET_CAST(TMutex *,Mutex)->TMutex::~TMutex();
  228. }
  229.  
  230. inline TMutex *TApplication::TAppMutex::GetMutex()
  231. {
  232.   if (!HasMutex())
  233.     return 0;
  234.   else
  235.     return REINTERPRET_CAST(TMutex *,(char *)Mutex);
  236. }
  237.  
  238. inline int TApplication::TAppMutex::HasMutex()
  239. {
  240.     return NotWIN32s;
  241. }
  242.  
  243. inline TApplication::TAppLock::TAppLock(TApplication &app)
  244. {
  245.   if (TAppMutex::HasMutex())
  246.     new(AppLock)TMutex::Lock(*app.GetMutex());
  247. }
  248.  
  249. inline TApplication::TAppLock::~TAppLock()
  250. {
  251.   if (TAppMutex::HasMutex())
  252.     REINTERPRET_CAST(TMutex::Lock*,AppLock)->TMutex::Lock::~Lock();
  253. }
  254.  
  255. inline void TApplication::TAppLock::Release()
  256. {
  257.   if (TAppMutex::HasMutex())
  258.     REINTERPRET_CAST(TMutex::Lock*,AppLock)->Release();
  259. }
  260.  
  261. #endif
  262.  
  263. inline void TApplication::SetWinMainParams(HINSTANCE       hInstance,
  264.                                            HINSTANCE       hPrevInstance,
  265.                                            const char far* cmdLine,
  266.                                            int             cmdShow)
  267. {
  268.   InitHInstance = hInstance;
  269.   InitHPrevInstance = hPrevInstance;
  270.   InitCmdLine = cmdLine;
  271.   InitCmdShow = cmdShow;
  272. }
  273.  
  274. inline void TApplication::GetWinMainParams()
  275. {
  276.   InitModule(InitHInstance, InitCmdLine);
  277.   hPrevInstance = InitHPrevInstance;
  278.   nCmdShow = InitCmdShow;
  279. }
  280.  
  281. #endif  // __OWL_APPLICAT_H
  282.  
  283.