home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / DOCTPL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.8 KB  |  237 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.9  $
  6. //
  7. // Implementation of class TDocTemplate
  8. //----------------------------------------------------------------------------
  9. #pragma hdrignore SECTION
  10. #include <owl/pch.h>
  11. #if !defined(OWL_DEFS_H)
  12. # include <owl/defs.h>
  13. #endif
  14. #if !defined(OWL_DOCTPL_H)
  15. # include <owl/doctpl.h>
  16. #endif
  17. #include <stdio.h>
  18.  
  19. OWL_DIAGINFO;
  20. #if !defined(SECTION) || SECTION == 1
  21.  
  22. //
  23. // Construct a Doc/View template from the specified parameters.
  24. //
  25. TDocTemplate::TDocTemplate(TRegList& regList, TModule*& module,
  26.                            TDocTemplate*& rptpl)
  27. :
  28.   TRegLink(regList, (TRegLink*&)rptpl),
  29.   Directory(0),
  30.   ModulePtr(&module)
  31. {
  32.   RefCnt = module ? 1 : 0x8001;  // static if constructed before Module
  33.   Flags = atol((*RegList)["docflags"]);
  34. }
  35.  
  36. //
  37. //
  38. //
  39. void TDocTemplate::SetFlag(long flag)
  40. {
  41.   Flags = GetFlags() | flag;
  42. }
  43.  
  44. //
  45. //
  46. //
  47. void TDocTemplate::ClearFlag(long flag)
  48. {
  49.   Flags = GetFlags() & ~flag;
  50. }
  51.  
  52. //
  53. //
  54. //
  55. const char far* TDocTemplate::GetDirectory() const
  56. {
  57.   if (Directory)
  58.     return Directory;
  59.   return (*RegList)["directory"];
  60. }
  61.  
  62. //
  63. //
  64. //
  65. void TDocTemplate::SetDirectory(const char far* txt)
  66. {
  67.   delete[] Directory;
  68.   Directory = 0;
  69.   if (txt)
  70.     Directory = strnewdup(txt);
  71. }
  72.  
  73. //
  74. //
  75. //
  76. void TDocTemplate::SetDirectory(const char far* txt, int len)
  77. {
  78.   delete[] Directory;
  79.   Directory = 0;
  80.   if (txt && len > 0) {
  81.     Directory = strnewdup(txt, len);
  82.     Directory[len] = 0;
  83.   }
  84. }
  85.  
  86. //
  87. // Called only when RefCnt goes to 0
  88. //
  89. TDocTemplate::~TDocTemplate()
  90. {
  91.   if (GetFlags() & dtDynRegInfo) {
  92.     delete RegList;
  93.     RegList = 0;
  94.   }
  95.   delete[] Directory;
  96. }
  97.  
  98. //
  99. const char far* TDocTemplate::GetFileFilter() const
  100. {
  101.   return (*RegList)["docfilter"];
  102. }
  103.  
  104. //
  105. const char far* TDocTemplate::GetDescription() const
  106. {
  107.   return (*RegList)["description"];
  108. }
  109.  
  110. //
  111. const char far* TDocTemplate::GetDefaultExt() const
  112. {
  113.   return (*RegList)["extension"];
  114. }
  115.  
  116. //----------------------------------------------------------------------------
  117. // Backward compatibility with old style doc templates
  118. //
  119. #if defined(OWL2_COMPAT)
  120.  
  121. //
  122. // The following three function vectors get reset by TDocManager constructor
  123. //
  124. static bool       _CALLCNVN SelectSaveX(TDocTemplate*, TDocument&) {return false;}
  125. static TView*     _CALLCNVN InitViewX(TView*) {return 0;}
  126. static TDocument* _CALLCNVN InitDocX(TDocTemplate&, TDocument*, const char far*, long)
  127.                                     {return 0;}
  128.  
  129. bool       _CALLCNVN (*TDocTemplate::SelectSave_)(TDocTemplate*,TDocument&) = SelectSaveX;
  130. TView*     _CALLCNVN (*TDocTemplate::InitView_)(TView*) = InitViewX;
  131. TDocument* _CALLCNVN (*TDocTemplate::InitDoc_)(TDocTemplate&, TDocument*,
  132.                                      const char far*, long) = InitDocX;
  133.  
  134. //
  135. // private class for backward compatibility
  136. //
  137. class TRegListOldDocTemplate : public TRegList {
  138.   public:
  139.     TRegListOldDocTemplate(const char* desc, const char* filt,
  140.                            const char* dir,  const char* ext, long flags);
  141.     TRegItem List[6];  // 4 strings, flags, terminator
  142.     char FlagBuf[12];  // for string representation of doc template flags
  143. };
  144.  
  145. //
  146. //
  147. //
  148. TRegListOldDocTemplate::TRegListOldDocTemplate(const char* desc,
  149.                                                const char* filt,
  150.                                                const char* dir,
  151.                                                const char* ext,
  152.                                                long        flags)
  153. :
  154.   TRegList(List)
  155. {
  156.   sprintf(FlagBuf,"0x%lX",flags);
  157.   List[0].Key = "description";
  158.   List[0].Value = desc;
  159.   List[1].Key = "docfilter";
  160.   List[1].Value = filt;
  161.   List[2].Key = "directory";
  162.   List[2].Value = dir;
  163.   List[3].Key = "extension";
  164.   List[3].Value = ext;
  165.   List[4].Key = "docflags";
  166.   List[4].Value = FlagBuf;
  167.   List[5].Key = 0;
  168. }
  169.  
  170. //
  171. // Construct a Doc/View template from the description, filter, directory, 
  172. // file extension, 'dt' flags, module and template head parameters.
  173. // This constructor is primarily for backward compatibility with earlier
  174. // implementation of ObjectWindows' Doc/View subsystem.
  175. //
  176. TDocTemplate::TDocTemplate(const char* desc, const char* filt,
  177.                            const char* dir, const char* ext,
  178.                            long flags, TModule*& module,
  179.                            TDocTemplate*& rphead)
  180. :
  181.   TRegLink(),
  182.   Directory(0),
  183.   ModulePtr(&module),
  184.   Flags(flags | dtDynRegInfo)
  185. {
  186.   AddLink((TRegLink*&)rphead, *this);
  187.   RefCnt = module ? 1 : 0x8001;  // static if contructed before Module
  188. //  NextTemplate = 0;
  189.   RegList = new TRegListOldDocTemplate(desc, filt, dir, ext, flags);
  190. }
  191.  
  192. #endif  //  defined(OWL2_COMPAT)
  193.  
  194. #endif
  195. //----------------------------------------------------------------------------
  196. #if !defined(SECTION) || SECTION == 2
  197.  
  198. IMPLEMENT_ABSTRACT_STREAMABLE(TDocTemplate);
  199.  
  200. #if !defined(BI_NO_OBJ_STREAMING)
  201.  
  202. //
  203. //
  204. //
  205. void*
  206. TDocTemplate::Streamer::Read(ipstream& is, uint32 /*version*/) const
  207. {
  208.   TDocTemplate* o = GetObject();
  209.   bool wasStatic = o->IsStatic();  // test in case dummy template passed
  210.   is >> o->RefCnt;  // need to set back to 1 if doc attach increments!!?
  211.   is >> o->Flags;
  212.   if (o->IsStatic()) {
  213.     delete[] o->Directory;
  214.   }
  215.   o->Directory   = is.freadString();
  216.   if (o->IsStatic() && !wasStatic) {  // dummy template passed as sink
  217.     delete[] o->Directory;
  218.   }
  219.   return o;
  220. }
  221.  
  222. //
  223. //
  224. //
  225. void
  226. TDocTemplate::Streamer::Write(opstream& os) const
  227. {
  228.   TDocTemplate* o = GetObject();
  229.   os << o->RefCnt;
  230.   os << o->GetFlags();
  231.   os.fwriteString(o->Directory);
  232. }
  233.  
  234. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  235.  
  236. #endif
  237.