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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   include\owl\docmanag.h
  4. //   Defines classes TDocManager, TDocTemplate, TDocTemplateT<D,V>
  5. //----------------------------------------------------------------------------
  6. #if !defined(__OWL_DOCMANAG_H)
  7. #define __OWL_DOCMANAG_H
  8.  
  9. #if !defined(__OWL_DOCVIEW_H)
  10.   #include <owl\docview.h>
  11. #endif
  12. #if !defined(__OWL_APPLICAT_H)
  13.   #include <owl\applicat.h>
  14. #endif
  15.  
  16. #if defined(__DLL__)            // if implementing documents & views in a DLL
  17. # define _DOCVIEWCLASS _export  // force data segment load on method entry
  18. #else
  19. # define _DOCVIEWCLASS
  20. #endif
  21.  
  22. #if defined(__DLL__) && !defined(__WIN32__) // templates in a DLL
  23. # define _DOCVIEWENTRY _export
  24. #else
  25. # define _DOCVIEWENTRY
  26. #endif
  27.  
  28. extern TDocTemplate* DocTemplateStaticHead;// templates constructed before app
  29.  
  30. //
  31. // definitions of dm??? document manager operational mode flags
  32. //
  33. const int dmSDI        = 0x0001; // does not support multiple open documents
  34. const int dmMDI        = 0x0002; // supports multiple open documents
  35. const int dmMenu       = 0x0004; // set IDs for file menu
  36. const int dmSaveEnable = 0x0010; // enable FileSave even if doc is unmodified
  37. const int dmNoRevert   = 0x0020; // disable FileRevert menu item
  38.  
  39. //
  40. // definitions of dt??? document/view flags for templates and CreateDoc()
  41. //
  42. const long dtNewDoc     = 0x80000000L; // create new document, no path used
  43. const long dtAutoDelete = 0x40000000L; // delete doc when last view is deleted
  44. const long dtNoAutoView = 0x20000000L; // no automatic create of default view
  45. const long dtSingleView = 0x10000000L; // only a single view per document
  46. const long dtAutoOpen   = 0x08000000L; // open document upon creation
  47. const long dtUpdateDir  = 0x04000000L; // syncronize directory with dialog dir
  48. const long dtHidden     = 0x02000000L; // hide template from user selection
  49. const long dtSelected   = 0x01000000L; // indicates template last selected
  50.  
  51. //
  52. // flags defined by Windows in <commdlg.h> - use the low order 17 bits
  53. //
  54. const long dtReadOnly       = OFN_READONLY;      // initial check readonly box
  55. const long dtOverwritePrompt= OFN_OVERWRITEPROMPT;  // prompt before overwrite
  56. const long dtHideReadOnly   = OFN_HIDEREADONLY;     // no show readonly box
  57. const long dtPathMustExist  = OFN_PATHMUSTEXIST;    // path must be valid
  58. const long dtFileMustExist  = OFN_FILEMUSTEXIST;    // file must exist
  59. const long dtCreatePrompt   = OFN_CREATEPROMPT;     // prompt before create
  60. const long dtNoReadOnly     = OFN_NOREADONLYRETURN; // return writable file
  61. const long dtNoTestCreate   = OFN_NOTESTFILECREATE; // skip create tests
  62.  
  63. const long dtProhibited = OFN_ALLOWMULTISELECT | OFN_ENABLEHOOK
  64.                           | OFN_ENABLETEMPLATE | OFN_ENABLETEMPLATEHANDLE;
  65.  
  66. //
  67. // definitions of dn??? document/view message notifications
  68. //
  69. enum{
  70.   dnCreate,                 // new document or view has been created
  71.   dnClose                   // document or view has been closed
  72. };
  73.  
  74. class _OWLCLASS TDocManager;
  75.  
  76. //
  77. //  class TDocTemplate
  78. //  ----- ------------
  79. //
  80. class _OWLCLASS TDocTemplate : public TStreamableBase {
  81.   public:
  82.     virtual TDocument* CreateDoc(const char far* path, long flags = 0) = 0;
  83.     virtual TView*     CreateView(TDocument& doc, long flags = 0) = 0;
  84.     virtual TDocument* IsMyKindOfDoc(TDocument& doc) = 0;
  85.     virtual TView*     IsMyKindOfView(TView& view) = 0;
  86.     virtual const char far* GetViewName() = 0;
  87.     BOOL               SelectSave(TDocument& doc);
  88.     BOOL               IsVisible();     // displayable in file select dialogs
  89.     virtual TDocTemplate* Clone(TModule* module,
  90.                                 TDocTemplate*& phead=DocTemplateStaticHead)=0;
  91.     TDocManager*    GetDocManager() const {return DocManager;}
  92.     void            SetDocManager(TDocManager* dm) {DocManager = dm;}
  93.     const char far* GetFileFilter() const {return FileFilter;}
  94.     void            SetFileFilter(const char far*);
  95.     const char far* GetDescription() const {return Description;}
  96.     void            SetDescription(const char far*);
  97.     const char far* GetDirectory() const {return Directory;}
  98.     void            SetDirectory(const char far*);
  99.     void            SetDirectory(const char far*, int len);
  100.     const char far* GetDefaultExt() const {return DefaultExt;}
  101.     void            SetDefaultExt(const char far*);
  102.     long            GetFlags() const {return Flags;}
  103.     BOOL            IsFlagSet(long flag) {return (Flags & flag) != 0;}
  104.     void            SetFlag(long flag)  {Flags |= flag;}
  105.     void            ClearFlag(long flag){Flags &= ~flag;}
  106.     BOOL            IsStatic() {return (RefCnt & 0x8000) != 0;}
  107.     TModule*&       GetModule() {return *ModulePtr;}
  108.     void            SetModule(TModule* module){ModuleDeref = module;
  109.                                               ModulePtr = &ModuleDeref;}
  110.  
  111.   protected:                // called only from parameterized subclass
  112.     TDocTemplate(const char far* desc, const char far* filt,
  113.                  const char far* dir,  const char far* ext, long flags,
  114.                  TModule*& module, TDocTemplate*& phead);
  115.    ~TDocTemplate();
  116.     TDocument* InitDoc(TDocument* doc, const char far* path, long flags);
  117.     TView*     InitView(TView* view);
  118.  
  119.   private:
  120.     char far* FileFilter;         // file matching pattern
  121.     char far* Description;        // decription to put in list box
  122.     char far* Directory;          // directory path
  123.     char far* DefaultExt;         // default extension
  124.     long  Flags;                  // dt??? bits for dialogs and creation modes
  125.     TDocManager*  DocManager;     // pointer back to document manager
  126.     TDocTemplate* NextTemplate;   // doc template chain link
  127.     int  RefCnt;               // documents attached + 1 if attached to docmgr
  128.     TModule** ModulePtr;          // would be TModule*&, except for streaming
  129.     TModule*  ModuleDeref;        // used only when setting a new module
  130.  
  131.  
  132.   DECLARE_ABSTRACT_STREAMABLE (_OWLCLASS,TDocTemplate,1);
  133.   friend class TDocument;   // access to RefCnt
  134.   friend class TDocManager;
  135. };
  136.  
  137. inline BOOL
  138. TDocTemplate::IsVisible() {
  139.   return (!(Flags & dtHidden) && Description != 0);
  140. }
  141.  
  142. //
  143. //  class TDocTemplateT<D,V>
  144. //  ----- ------------------
  145. //
  146. template<class D, class V>
  147. class _DOCVIEWENTRY TDocTemplateT : public TDocTemplate {
  148.   public:
  149.     TDocTemplateT(const char far* filt, const char far* desc,
  150.                   const char far* dir, const char far* ext, long flags = 0,
  151.                   TModule*& module = ::Module,
  152.                   TDocTemplate*& phead = DocTemplateStaticHead);
  153.     TDocTemplateT* Clone(TModule* module,
  154.                          TDocTemplate*& phead = DocTemplateStaticHead);
  155.     D* CreateDoc(const char far* path, long flags=0);
  156.     TView* CreateView(TDocument& doc, long flags=0);
  157.     D* IsMyKindOfDoc(TDocument& doc);  // returns 0 if template can't support
  158.     V* IsMyKindOfView(TView& view);    // returns 0 if template incompatible
  159.     virtual const char far* GetViewName() {return V::StaticName();}
  160.  
  161.   // explicit expansion of DECLARE_STREAMABLE for use with parameterized class
  162.   // DECLARE_STREAMABLE_FROM_BASE(TDocTemplateT<D,V>);
  163.   #define TEMPLATECLASS TDocTemplateT<D,V>
  164.   DECLARE_CASTABLE;
  165.   DECLARE_STREAMABLE_CTOR(TDocTemplateT);
  166.   DECLARE_STREAMER_FROM_BASE(_OWLCLASS, TEMPLATECLASS, TDocTemplate);
  167.   DECLARE_STREAMABLE_OPS(TEMPLATECLASS);
  168.   #undef TEMPLATECLASS
  169. };
  170.  
  171. //
  172. //  class TDocManager
  173. //  ----- -----------
  174. //
  175. class _OWLCLASS TDocManager : public TEventHandler, public TStreamableBase {
  176.   public:
  177.     TDocument::List DocList;      // list of attached documents
  178.     TDocManager(int mode,TDocTemplate*& templateHead = DocTemplateStaticHead);
  179.     virtual ~TDocManager();
  180.     virtual TDocument*  CreateAnyDoc(const char far* path, long flags = 0);
  181.     virtual TView*      CreateAnyView(TDocument& doc,long flags = 0);
  182.     virtual TDocTemplate* SelectAnySave(TDocument& doc, BOOL samedoc = TRUE);
  183.     virtual TDocTemplate* MatchTemplate(const char far* path);
  184.     virtual TDocument*  GetCurrentDoc();  // return doc with focus, else 0
  185.     virtual BOOL        FlushDoc(TDocument& doc); // attempt to update changes
  186.     TDocument*          FindDocument(const char far* path); // 0 if not found
  187.     TApplication*       GetApplication() {return Application;}
  188.     BOOL                IsFlagSet(int flag) {return (Mode & flag) != 0;}
  189.     void                RefTemplate(TDocTemplate&);    // add template ref    
  190.     void                UnRefTemplate(TDocTemplate&);  // drop template ref    
  191.     void                DeleteTemplate(TDocTemplate&); // remove from list
  192.     void                AttachTemplate(TDocTemplate&); // append to list
  193.     TDocTemplate*       GetNextTemplate(TDocTemplate* tpl)
  194.                         {return tpl ? tpl->NextTemplate : TemplateList;}
  195.  
  196.     // primary event handlers, public to allow direct invocation from app
  197.     //
  198.     virtual void CmFileOpen();
  199.     virtual void CmFileNew();
  200.     virtual void CmFileClose();
  201.     virtual void CmFileSave();
  202.     virtual void CmFileSaveAs();
  203.     virtual void CmFileRevert();
  204.     virtual void CmViewCreate();
  205.  
  206.     // overrideable document manager UI functions
  207.     //
  208.     virtual UINT  PostDocError(TDocument& doc, UINT sid, UINT choice = MB_OK);
  209.     virtual void  PostEvent(int id, TDocument& doc); // changed doc status
  210.     virtual void  PostEvent(int id, TView& view);    // changed view status
  211.  
  212.     // delegated methods from TApplication
  213.     //
  214.     void EvPreProcessMenu(HMENU hMenu);
  215.     BOOL EvCanClose();
  216.  
  217.   protected:
  218.  
  219.     // overrideable document manager UI functions
  220.     //
  221.     virtual int   SelectDocPath(TDocTemplate** tpllist, int tplcount,
  222.                      char far* path, int buflen, long flags, BOOL save=FALSE);
  223.     virtual int   SelectDocType(TDocTemplate** tpllist, int tplcount);
  224.     virtual int   SelectViewType(TDocTemplate** tpllist, int tplcount);
  225.  
  226.   private:
  227.     //
  228.     // event enabling handlers
  229.     //
  230.     virtual void CmEnableNew(TCommandEnabler& hndlr);
  231.     virtual void CmEnableOpen(TCommandEnabler& hndlr);
  232.     virtual void CmEnableSave(TCommandEnabler& hndlr);
  233.     virtual void CmEnableSaveAs(TCommandEnabler& hndlr);
  234.     virtual void CmEnableRevert(TCommandEnabler& hndlr);
  235.     virtual void CmEnableClose(TCommandEnabler& hndlr);
  236.     virtual void CmEnableCreate(TCommandEnabler& hndlr);
  237.  
  238.     int           Mode;           // mode flags: dmxxx
  239.     TDocTemplate* TemplateList;   // chained list of doc templates, 0 if none
  240.     TApplication* Application;    // current application, set by constructor
  241.     TDocTemplate** TemplateHead;  // saved pointer to DocTemplateStaticHead
  242.  
  243.   DECLARE_RESPONSE_TABLE(TDocManager);
  244.   DECLARE_STREAMABLE(_OWLCLASS, TDocManager, 1);
  245.   friend class TDocTemplate;  // access to template list, PostEvent()
  246.   friend class TDocument;     // access to Application
  247. };
  248.  
  249. //
  250. // inline implementations
  251. //
  252.  
  253. inline void
  254. TDocManager::RefTemplate(TDocTemplate& tpl)
  255. {
  256.   ++tpl.RefCnt;
  257. }
  258.  
  259. inline void
  260. TDocManager::UnRefTemplate(TDocTemplate& tpl)
  261. {
  262.   if (--tpl.RefCnt == 0)
  263.     delete &tpl;
  264. }
  265.  
  266. template<class D, class V> inline
  267. TDocTemplateT<D,V>::TDocTemplateT(const char far* desc, const char far* filt,
  268.                         const char far* dir,  const char far* ext, long flags,
  269.                         TModule*& module, TDocTemplate*& phead)
  270.                   : TDocTemplate(desc, filt, dir, ext, flags, module, phead){}
  271.  
  272. template<class D, class V> inline TDocTemplateT<D,V>*
  273. TDocTemplateT<D,V>::Clone(TModule* module, TDocTemplate*& phead) {
  274.   TDocTemplateT<D,V>* tpl = new TDocTemplateT<D,V>(GetDescription(), GetFileFilter(),
  275.                                 GetDirectory(), GetDefaultExt(),
  276.                                 GetFlags(), GetModule(), phead);
  277.   tpl->SetModule(module);
  278.   return tpl;
  279. }
  280.  
  281. template<class D, class V> inline D*
  282. TDocTemplateT<D,V>::CreateDoc(const char far* path, long flags) {
  283.   return (D*)InitDoc(new D(0), path, flags);
  284. }
  285.  
  286. template<class D, class V> inline TView*
  287. TDocTemplateT<D,V>::CreateView(TDocument& doc, long flags) {
  288.   return (V*)InitView(new V((D&)doc));
  289. }
  290.  
  291. #define DEFINE_DOC_TEMPLATE_CLASS(docClass, viewClass, tplClass) \
  292.   typedef TDocTemplateT<docClass, viewClass> tplClass;           \
  293.   IMPLEMENT_STREAMABLE_FROM_BASE(tplClass, TDocTemplate);        \
  294.   docClass* tplClass::IsMyKindOfDoc(TDocument& doc)              \
  295.   {                                                              \
  296.     return TYPESAFE_DOWNCAST(&doc, docClass);                    \
  297.   }                                                              \
  298.   viewClass* tplClass::IsMyKindOfView(TView& view)               \
  299.   {                                                              \
  300.     return TYPESAFE_DOWNCAST(&view, viewClass);                  \
  301.   }
  302.  
  303. #define EV_WM_PREPROCMENU\
  304.   {WM_OWLPREPROCMENU, 0, (TAnyDispatcher)::v_WPARAM_Dispatch,\
  305.    (TMyPMF)v_HMENU_Sig(&TMyClass::EvPreProcessMenu)}
  306.  
  307. #define EV_WM_CANCLOSE\
  308.   {WM_OWLCANCLOSE, 0, (TAnyDispatcher)::U_Dispatch,\
  309.    (TMyPMF)B_Sig(&TMyClass::EvCanClose)}
  310.  
  311. #endif  // __OWL_DOCMANAG_H
  312.