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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //  Defines classes TDocument, TView,TWindowView, TStream,TInStream,TOutStream
  6. //----------------------------------------------------------------------------
  7. #if !defined(OWL_DOCVIEW_H)
  8. #define OWL_DOCVIEW_H
  9.  
  10. #if !defined(OWL_APPLICAT_H)
  11. # include <owl/applicat.h>
  12. #endif
  13. #if !defined(OWL_FRAMEWIN_H)
  14. # include <owl/framewin.h>
  15. #endif
  16. #if !defined(__COMMDLG_H)
  17. # include <commdlg.h>
  18. #endif
  19. class _OWLCLASS TDocManager;
  20. class _OWLCLASS TDocTemplate;
  21. class _OWLCLASS TMenuDescr;
  22.  
  23. //
  24. // Some types & functions available in Win32/Ole2 that are emulated under Win16
  25. // NOTE: This must always be in synch with Win32/Ole2 headers 
  26. //
  27. #if defined(BI_PLAT_WIN16)
  28. # if !defined(_FILETIME_)
  29.     struct far tagFILETIME {uint32 dwLowDateTime; uint32 dwHighDateTime;};
  30.     typedef tagFILETIME FILETIME;
  31. #   define _FILETIME_
  32. # endif
  33. # include <io.h>
  34.   void DateTimeToFileTime(struct date* dosd, struct time* dost, FILETIME far* pft);
  35.   bool FileTimeToDateTime(FILETIME far* pft, struct date* dosd, struct time* dost);
  36.   int  FormatDateTime(struct date, struct time, void far* dest, int textlen);
  37. #endif
  38. int _OWLFUNC FormatFileTime(FILETIME* pft, void far* dest, int textlen);
  39.  
  40. //
  41. // document open and sharing modes - used in storage and stream constructors
  42. //   note: the bits values are those of file streams, not same as RTL or OLE
  43. //
  44. enum {
  45.   ofParent    = 0,      // use open mode of parent storage
  46.   ofRead      = 0x0001, // ios::in,  open for reading
  47.   ofWrite     = 0x0002, // ios::out, open for writing
  48.   ofReadWrite = (ofRead|ofWrite),
  49.   ofAtEnd     = 0x0004, // ios::ate, seek to eof upon original open
  50.   ofAppend    = 0x0008, // ios::app, append mode: all additions at eof
  51.   ofTruncate  = 0x0010, // ios::trunc, truncate file if already exists
  52.   ofNoCreate  = 0x0020, // ios::nocreate,  open fails if file doesn't exist
  53.   ofNoReplace = 0x0040, // ios::noreplace, open fails if file already exists
  54.   ofBinary    = 0x0080, // ios::binary, binary (not text) file, no CR stripping
  55.   ofIosMask   = 0x00FF, // all of the above bits as used by class ios
  56.  
  57.   ofTransacted= 0x1000, // STGM_TRANSACTED, supports commit and revert
  58.   ofPreserve  = 0x2000, // STGM_CONVERT, backup old data of same name
  59.   ofPriority  = 0x4000, // STGM_PRIORITY, temporary efficient peeking
  60.   ofTemporary = 0x8000, // STGM_DELETEONRELEASE, delete when destructed
  61.  
  62.   shCompat    = 0x0600, // for non-compliant applications, avoid if possible
  63.   shNone      = 0x0800, // EXCLUSIVE functionality
  64.   shRead      = 0x0A00, // DENY_WRITE functionality
  65.   shWrite     = 0x0C00, // DENY_READ functionality
  66.   shReadWrite = 0x0E00, // DENY_NONE functionality
  67.   shDefault   = 0,      // use stream implementation default value
  68.   shMask      = (shCompat|shNone|shRead|shWrite)
  69. };
  70. #define PREV_OPEN           (ofNoCreate|ofNoReplace)
  71. #define IS_PREV_OPEN(omode) ((omode & PREV_OPEN)==PREV_OPEN)
  72.  
  73. //
  74. // Definitions of vnXxxx view notification event IDs
  75. // event ID's up to vnCustomBase reserved for general doc-view notifications
  76. //
  77. const int vnViewOpened  = 1;   // a new view has just been constructed
  78. const int vnViewClosed  = 2;   // another view is about to be destructed
  79. const int vnDocOpened   = 3;   // document has just been opened
  80. const int vnDocClosed   = 4;   // document has just been closed
  81. const int vnCommit      = 5;   // document is committing, flush cached changes
  82. const int vnRevert      = 6;   // document has reverted, reload data from doc
  83. const int vnIsDirty     = 7;   // respond true if uncommitted changes present
  84. const int vnIsWindow    = 8;   // respond true if passed HWND is that of view
  85. const int vnCustomBase = 100;  // base of document class specific notifications
  86.  
  87. //
  88. // Document and view property access flags
  89. //
  90. const int pfGetText   =  1;   // property accessible as text format
  91. const int pfGetBinary =  2;   // property accessible as native non-text format
  92. const int pfConstant  =  4;   // property is invariant for object instance
  93. const int pfSettable  =  8;   // property settable as native format
  94. const int pfUnknown   = 16;   // property defined but unavailable in object  
  95. const int pfHidden    = 32;   // property should be hidden from normal browse
  96. const int pfUserDef   =128;   // property has been user-defined at run time
  97.  
  98. //
  99. // Classes defined later in this file
  100. //
  101. class _OWLCLASS_RTL TStream;
  102. class _OWLCLASS_RTL TInStream;
  103. class _OWLCLASS_RTL TOutStream;
  104. class _OWLCLASS TDocument;
  105. class _OWLCLASS TView;
  106.  
  107. //
  108. //  class TDocument - abstract base class for document/view management
  109. //  ----- ---------
  110. //
  111. class _OWLCLASS TDocument : public TStreamableBase {
  112.   public:
  113.     enum TDocProp {
  114.       PrevProperty = 0,
  115.       DocumentClass,     // text
  116.       TemplateName,      // text
  117.       ViewCount,         // int
  118.       StoragePath,       // text
  119.       DocTitle,          // text
  120.       NextProperty,
  121.     };
  122.  
  123.     class _OWLCLASS List {
  124.       public:
  125.         List() : DocList(0) {}
  126.        ~List() {Destroy();}
  127.         bool Insert(TDocument* doc);  // insert new document, fails if there
  128.         bool Remove(TDocument* doc);  // remove document, fails if not there
  129.         TDocument* Next(const TDocument* doc); // returns first if doc=0
  130.         void Destroy();               // deletes all documents
  131.       private:
  132.         TDocument* DocList;
  133.     };
  134.  
  135.     void far*     Tag;          // application hook, not used internally
  136.     List          ChildDoc;     // linked child document chain
  137.  
  138.     TDocument(TDocument* parent = 0);
  139.     virtual ~TDocument();
  140.     virtual TInStream*  InStream(int mode, const char far* strmId=0) {return 0;}
  141.     virtual TOutStream* OutStream(int mode, const char far* strmId=0) {return 0;}
  142.     virtual bool   Open(int mode, const char far* path=0) {return true;}
  143.     virtual bool   Close();      // close document, does not delete or detach
  144.     virtual bool   Commit(bool force=false); // save current data, force write
  145.     virtual bool   Revert(bool clear=false); // abort changes, no reload if true
  146.     virtual TDocument& RootDocument();
  147.     TDocManager&   GetDocManager() {return *DocManager;}
  148.     void           SetDocManager(TDocManager& dm);
  149.     TDocument*     GetParentDoc() {return ParentDoc;}
  150.  
  151.     TDocTemplate*  GetTemplate() {return Template;}
  152.     bool           SetTemplate(TDocTemplate* tpl);
  153.     virtual bool   SetDocPath(const char far* path);
  154.     const char far* GetDocPath() {return DocPath;}
  155.     virtual void   SetTitle(const char far* title);
  156.     const char far* GetTitle() {return Title;}
  157.     virtual bool   IsDirty();           // also queries doc and view hierarchy
  158.     void           SetDirty(bool dirty = true){DirtyFlag = dirty;}
  159.     virtual bool   IsOpen()                   {return StreamList != 0;}
  160.     virtual bool   CanClose();           // returns false if unable to close
  161.     virtual bool   HasFocus(HWND hwnd);
  162.     bool           NotifyViews(int event, long item=0, TView* exclude=0);
  163.     TView*         QueryViews(int event, long item=0, TView* exclude=0);
  164.     virtual uint   PostError(uint sid, uint choice = MB_OK);
  165.  
  166.     virtual int    PropertyCount() {return NextProperty-1;}
  167.     virtual int    FindProperty(const char far* name);// return property index
  168.     virtual int    PropertyFlags(int index);          // pfXxxxx bit array
  169.     virtual const char*  PropertyName(int index);     // locale invariant name
  170.     virtual int    GetProperty(int index, void far* dest, int textlen=0);
  171.     virtual bool   SetProperty(int index, const void far* src); // native type
  172.     TStream*       NextStream(const TStream* strm);
  173.     TView*         NextView(const TView* view);
  174.     int            GetOpenMode();
  175.     void           SetOpenMode(int mode);
  176.     TView*         InitView(TView* view);    // called from template InitView
  177.     bool           IsEmbedded() {return Embedded;}
  178.     void           SetEmbedded(bool embed) {Embedded = embed;}
  179.     virtual bool   InitDoc() {return true;}
  180.     TView*         GetViewList() {return ViewList;}
  181.  
  182.   protected:
  183.     bool          DirtyFlag;    // document changed, might not represent views
  184.     bool          Embedded;     // document is an embedding
  185.     static int    UntitledIndex;// last used index for Untitled document
  186.     virtual void  AttachStream(TStream& strm);// called from TStream constructor
  187.     virtual void  DetachStream(TStream& strm);// called from TStream destructor
  188.  
  189.   private:
  190.     TDocManager*  DocManager;   // pointer back to document manager
  191.     TDocument*    NextDoc;      // next in linked chain of active documents
  192.     TView*        ViewList;     // head of linked view chain, 0 if no views
  193.     TStream*      StreamList;   // head of linked stream chain, 0 if no streams
  194.     int           OpenMode;     // mode and protection flags
  195.     char far*     DocPath;      // path used to open/save document
  196.     char far*     Title;        // current document title, 0 if untitled
  197.     TDocument*    ParentDoc;    // parent document, 0 if this is root document
  198.     TDocTemplate* Template;     // template associated with this document
  199.  
  200.     void   ReindexFrames();          // force view title and index update
  201.     void   AttachView(TView& view);  // called from TView constructor
  202.     bool   DetachView(TView& view);  // called from TView destructor
  203.     TDocument(TDocManager* docMan) : // create a dummy document
  204.       ParentDoc(this), DocManager(docMan){}
  205.  
  206.  
  207.   DECLARE_ABSTRACT_STREAMABLE(_OWLCLASS,TDocument,1);
  208.   friend class _OWLCLASS TDocTemplate;  // access to InitView()
  209.   friend class _OWLCLASS TView;         // access to AttachView(), DetatchView()
  210.   friend class _OWLCLASS_RTL TStream;   // access to AttachStream(), DetachStream()
  211.   friend class _OWLCLASS TDocManager;
  212.   friend class _OWLCLASS List;          // access to NextDoc
  213. };
  214.  
  215. //
  216. //  class TStream - abstract base class for storage stream access
  217. //  ----- -------
  218. //
  219.  
  220. class _OWLCLASS_RTL TStream {
  221.   public:
  222.     TDocument& GetDocument() {return Doc;}
  223.    ~TStream()                {Doc.DetachStream(*this);}
  224.     int        GetOpenMode();
  225.     const char far* GetStreamName();
  226.  
  227.   protected:
  228.     TDocument& Doc;        // document owning this stream
  229.     TStream* NextStream;   // next stream in linked list of active streams
  230.  
  231.     TStream(TDocument& doc, const char far* name, int mode)
  232.                  : Doc(doc), StreamName(name), OpenMode(mode)
  233.               {Doc.AttachStream(*this);}
  234.  
  235.   private:
  236.     int             OpenMode;
  237.     const char far* StreamName;
  238.  
  239.   friend class TDocument;
  240. };
  241.  
  242. //
  243. //  class TInStream - base class for input streams
  244. //  ----- ---------
  245. //
  246. class _OWLCLASS_RTL TInStream : public TStream, public istream {
  247.   public:
  248.     TInStream(TDocument& doc, const char far* name, int mode)
  249.                : TStream(doc, name, mode), istream() {}
  250. };
  251.  
  252. //
  253. //  class TOutStream - base class for output streams
  254. //  ----- ---------
  255. //
  256. class _OWLCLASS_RTL TOutStream : public TStream, public ostream {
  257.   public:
  258.     TOutStream(TDocument& doc, const char far* name, int mode)
  259.                 : TStream(doc, name, mode), ostream() {}
  260. };
  261.  
  262. //
  263. //  class TView - abstract base class for view access from document
  264. //  ----- -----
  265. //
  266. class _OWLCLASS TView : virtual public TEventHandler,
  267.                         virtual public TStreamableBase {
  268.   public:
  269.     enum {
  270.       PrevProperty = 0,
  271.       ViewClass,                  // text
  272.       ViewName,                   // text
  273.       NextProperty,
  274.     };
  275.     void far*     Tag;        // application hook, not used internally
  276.  
  277.     TView(TDocument& doc);
  278.     virtual ~TView();
  279.     TDocument&  GetDocument() {return *Doc;}
  280.     unsigned    GetViewId()   {return ViewId;}
  281.     virtual TMenuDescr* GetViewMenu() {return ViewMenu;} 
  282.     void        SetViewMenu(TMenuDescr* menu);
  283.     bool        IsOK() {return ViewId != 0;}  // true if successfully created
  284.     static unsigned GetNextViewId() {return NextViewId;} // next ID to assign
  285.     TView*      GetNextView() {return NextView;}
  286.  
  287.     // must implement, used by template manager for selection
  288.     // static const char far* StaticName() {return "name of view";}
  289.     virtual const char far* GetViewName()=0;         // return static name of view
  290.  
  291.     virtual TWindow* GetWindow() {return 0;} // if not derived from TWindow
  292.     virtual bool   SetDocTitle(const char far* docname, int index) {return false;}
  293.  
  294.     virtual int    PropertyCount() {return NextProperty - 1;}
  295.     virtual int    FindProperty(const char far* name);// return property index
  296.     virtual int    PropertyFlags(int index);          // pfXxxxx bit array
  297.     virtual const char*  PropertyName(int index);     // locale invariant name
  298.     virtual int    GetProperty(int index, void far* dest, int textlen=0);
  299.     virtual bool   SetProperty(int index, const void far* src) {return false;}
  300.  
  301.   protected:
  302.     TDocument*   Doc;
  303.     void         NotOK() {ViewId = 0;}  // to flag errors in creation
  304.  
  305.   private:
  306.     TView*       NextView;      // linked view chain, 0 if no more views
  307.     unsigned     ViewId;        // unique ID for this view, used for controls
  308.     TMenuDescr*  ViewMenu;      // menu descriptor specific for this view or 0
  309.     static unsigned NextViewId; // next view ID to be assigned
  310.  
  311.   DECLARE_ABSTRACT_STREAMABLE(_OWLCLASS,TView,1);
  312.   friend class _OWLCLASS TDocument;   // needs access to NextView
  313.   friend class TDocument::Streamer;   // needs access to NextView
  314. };
  315.  
  316. //
  317. //  class TWindowView
  318. //  ----- -----------
  319. //
  320. class _OWLCLASS TWindowView : public TWindow, public TView {
  321.   public:
  322.     TWindowView(TDocument& doc, TWindow* parent = 0);
  323.    ~TWindowView() {}
  324.     static const char far* StaticName() {return "Window View";}  // put in resource
  325.  
  326.     // inherited virtuals from TWindow
  327.     //
  328.     bool     CanClose()  {return TWindow::CanClose() && Doc->CanClose();}
  329.  
  330.     // inherited virtuals from TView
  331.     //
  332.     const char far* GetViewName() {return StaticName();}
  333.     TWindow* GetWindow() {return (TWindow*)this;}
  334.     bool     SetDocTitle(const char far* docname, int index)
  335.              { return TWindow::SetDocTitle(docname, index); }
  336.   private:
  337.     // event handlers
  338.     //
  339.     bool     VnIsWindow(HWND hWnd);
  340.  
  341.   DECLARE_RESPONSE_TABLE(TWindowView);
  342.   DECLARE_STREAMABLE (_OWLCLASS, TWindowView,1);
  343. };
  344.  
  345. //----------------------------------------------------------------------------
  346. // View Notification Handler Definitions
  347. //
  348.  
  349. //
  350. // DocView aliases to actual dispatchers
  351. //
  352. #define U_void_Dispatch     U_Dispatch
  353. #define U_int_Dispatch      U_U_Dispatch
  354. #define U_pointer_Dispatch  U_POINTER_Dispatch
  355. #define U_long_Dispatch     U_LPARAM_Dispatch
  356.  
  357. //
  358. // Define a DocView notification signature
  359. //   'id' is the vnXxxXxx name of the notification
  360. //   'arg' is the type of the arg passed to the handler
  361. //
  362. #define NOTIFY_SIG(id, arg) \
  363.   template <class T> \
  364.   inline bool (T::*id##_Sig(bool (T::*pmf)(arg)))(arg) {return pmf;}
  365.  
  366. NOTIFY_SIG(vnViewOpened,TView*)
  367. NOTIFY_SIG(vnViewClosed,TView*)
  368. NOTIFY_SIG(vnDocOpened, int)
  369. NOTIFY_SIG(vnDocClosed, int)
  370. NOTIFY_SIG(vnCommit, bool)
  371. NOTIFY_SIG(vnRevert, bool)
  372. NOTIFY_SIG(vnIsDirty, void)
  373. NOTIFY_SIG(vnIsWindow, HWND)
  374.  
  375. //
  376. // Define a DocView notification response entry
  377. //   'id' is the id from NOTIFY_SIG above
  378. //   'method' is the method name called by the notification
  379. //   'disp' is the type of the lParam dispacher to use, and can be:
  380. //     'void'    lParam not passed
  381. //     'int'     integer size (16bit for win16, or 32bit on win32)
  382. //     'pointer' pointer size (16bit for small & medium, or 32bit all other)
  383. //     'long'    32 bit passed always
  384. //
  385. #define VN_DEFINE(id, method, disp) \
  386.   {WM_OWLNOTIFY, id, \
  387.   (TAnyDispatcher) ::U_##disp##_Dispatch, \
  388.   (TMyPMF)id##_Sig(&TMyClass::method)}
  389.  
  390. #define EV_VN_VIEWOPENED  VN_DEFINE(vnViewOpened,  VnViewOpened,  pointer)
  391. #define EV_VN_VIEWCLOSED  VN_DEFINE(vnViewClosed,  VnViewClosed,  pointer)
  392. #define EV_VN_DOCOPENED   VN_DEFINE(vnDocOpened,   VnDocOpened,   int)
  393. #define EV_VN_DOCCLOSED   VN_DEFINE(vnDocClosed,   VnDocClosed,   int)
  394. #define EV_VN_COMMIT      VN_DEFINE(vnCommit,      VnCommit,      int)
  395. #define EV_VN_REVERT      VN_DEFINE(vnRevert,      VnRevert,      int)
  396. #define EV_VN_ISDIRTY     VN_DEFINE(vnIsDirty,     VnIsDirty,     void)
  397. #define EV_VN_ISWINDOW    VN_DEFINE(vnIsWindow,    VnIsWindow,    int)
  398.  
  399. //----------------------------------------------------------------------------
  400. // inline implementations
  401. //
  402.  
  403. inline int
  404. TDocument::GetOpenMode()
  405. {
  406.   return OpenMode;
  407. }
  408.  
  409. inline void
  410. TDocument::SetOpenMode(int mode)
  411. {
  412.   OpenMode = mode;
  413. }
  414.  
  415. inline const char far*
  416. TStream::GetStreamName()
  417. {
  418.   return StreamName;
  419. }
  420.  
  421. inline int
  422. TStream::GetOpenMode()
  423. {
  424.   return OpenMode;
  425. }
  426.  
  427. #endif  // OWL_DOCVIEW_H
  428.