home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / docview.pak / DVLOADER.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  3KB  |  109 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   Provides TModuleDoc class to dynamically load new document/view classes
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include <owl\docmanag.h>
  7. #include "dvloader.rc"
  8.  
  9. class TModuleDocument : public TDocument {
  10.   public:
  11.     TModuleDocument(TDocument* parent=0) : TDocument(parent), Module(0) {}
  12.    ~TModuleDocument() { Close(); }
  13.     BOOL   Open(int mode, const char far* path=0);
  14.     BOOL   Close();
  15.     BOOL   IsOpen() { return Module != 0; }
  16.   protected:
  17.     TModule* Module;
  18.  
  19.   DECLARE_STREAMABLE(, TModuleDocument,1);
  20. };
  21.  
  22. class TModuleView : public TView {
  23.   public:
  24.     TModuleView(TDocument& doc, TWindow* parent = 0) : TView(doc) {
  25.       doc.GetDocManager().PostDocError(doc, IDS_DVLOADED);
  26.       NotOK();
  27.     }
  28.     static LPCSTR StaticName() {return "Module View";}
  29.     LPCSTR GetViewName() {return StaticName();}
  30. };
  31.  
  32. BOOL TModuleDocument::Open(int /*mode*/, const char far* path)
  33. {
  34.   if (IsOpen())
  35.     return TRUE;
  36.   if (path)
  37.     SetDocPath(path);
  38.   if (!GetDocPath())
  39.     return FALSE;
  40.   try {
  41.     Module = new TModule(GetDocPath());
  42.   }
  43.   catch(TXOwl&) {
  44.     return FALSE;
  45.   }
  46.   TDocTemplate** tplhead;
  47.   TDocTemplate** PASCAL(*entry)(int version);
  48.   (FARPROC)entry = Module->GetProcAddress("GetDocTemplateHead");
  49.   if (!entry || (tplhead = entry(OWLVersion)) == 0) {
  50.     delete Module;
  51.     Module = 0;
  52.     return FALSE;
  53.   }
  54.   TDocTemplate* tpl = *tplhead;
  55.   for (; tpl; tpl = GetDocManager().GetNextTemplate(tpl)) 
  56.     tpl->Clone(Module)->SetDocManager(&GetDocManager());
  57.   return TRUE;
  58. }
  59.  
  60. BOOL TModuleDocument::Close()
  61. {
  62.   if (!IsOpen())
  63.     return TRUE;
  64.   TDocTemplate* tpl;
  65.   TDocTemplate* next;
  66.   for (tpl = GetDocManager().GetNextTemplate(0); tpl; tpl = next) {
  67.     next = GetDocManager().GetNextTemplate(tpl);
  68.     if (*tpl->GetModule() == *Module) {
  69.       // must remove any remaining associated documents before freeing module
  70.       TDocument* doc;
  71.       TDocument* next;
  72.       for (doc = GetDocManager().DocList.Next(0); doc; doc = next) {
  73.         next = GetDocManager().DocList.Next(doc);
  74.         if (doc->GetTemplate() == tpl){
  75.           doc->Close();
  76.           delete doc;
  77.         }
  78.       }
  79.       GetDocManager().DeleteTemplate(*tpl);  // RefCnt should go to 0 now
  80.     }
  81.   }
  82.   delete Module;
  83.   Module = 0;
  84.   return TRUE;  
  85. }
  86.  
  87. IMPLEMENT_STREAMABLE1(TModuleDocument, TDocument);
  88.  
  89. void*
  90. TModuleDocument::Streamer::Read(ipstream& is, uint32 /*version*/) const
  91. {
  92.   ReadBaseObject((TDocument*)GetObject(), is);
  93.   is >> GetObject()->Module;
  94.   return GetObject();
  95. }
  96.  
  97. void
  98. TModuleDocument::Streamer::Write(opstream& os) const
  99. {
  100.   WriteBaseObject((TDocument*)GetObject(), os);
  101.   os << GetObject()->Module;
  102. }
  103.  
  104. DEFINE_DOC_TEMPLATE_CLASS(TModuleDocument, TModuleView, LoadTemplate);
  105. LoadTemplate loadTpl("DocView Components (*.DVC)","*.dvc",0,0,
  106.                      dtAutoOpen|dtUpdateDir|dtReadOnly|dtHideReadOnly);
  107.  
  108.  
  109.