home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWLINC.PAK / APPLICAT.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  10KB  |  320 lines

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