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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   Implements class TInfoView
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include <owl\docmanag.h>
  7. #include <owl\filedoc.h>
  8. #include <owl\dc.h>
  9. #include "infoview.rc"
  10.  
  11. class _DOCVIEWCLASS TInfoView : public TWindowView {
  12.   public:
  13.     struct Data {
  14.       const char*  PropName;
  15.       char*        PropData;
  16.       int          PropSize;
  17.       int          PropFlags;
  18.     };
  19.     TInfoView(TDocument& doc, TWindow* parent = 0);
  20.    ~TInfoView();
  21.     static LPCSTR StaticName() {return "Info View";}  // put in resource
  22.     LPCSTR   GetViewName() {return StaticName();}
  23.     BOOL     SetDocTitle(LPCSTR docname, int index)
  24.              { Invalidate(); return TWindow::SetDocTitle(docname, index); }
  25.     void Paint(TDC&, BOOL, TRect&);
  26.  
  27.   protected:
  28.     Data* PropList;
  29.     int   PropCount;
  30.     int   YLoc;
  31.     int   OpenCount;  //temp
  32.     int   CloseCount; //temp
  33.     void  GetInfo();
  34.     void  Init();
  35.  
  36.     //
  37.     // event handlers
  38.     //
  39.     void  CmInfoOpen();
  40.     BOOL  VnViewOpened(TView*);
  41.     BOOL  VnViewClosed(TView*);
  42.     BOOL  VnDocOpened(int omode);
  43.     BOOL  VnDocClosed(int omode);
  44.     BOOL  VnIsWindow(HWND hWnd) {return HWindow == hWnd;}
  45.  
  46.   DECLARE_RESPONSE_TABLE(TInfoView);
  47.   DECLARE_STREAMABLE(,TInfoView,1);
  48. };
  49.  
  50. DEFINE_RESPONSE_TABLE1(TInfoView, TWindow)
  51.   EV_COMMAND(CM_INFOOPEN, CmInfoOpen),
  52.   EV_VN_DOCOPENED,
  53.   EV_VN_DOCCLOSED,
  54.   EV_VN_VIEWOPENED,
  55.   EV_VN_VIEWCLOSED,
  56.   EV_VN_ISWINDOW,
  57. END_RESPONSE_TABLE;
  58.  
  59. TInfoView::TInfoView(TDocument& doc,TWindow* parent) : TWindowView(doc,parent)
  60. {
  61.   Attr.Style &= ~(WS_BORDER);
  62.   SetViewMenu(new TMenuDescr(IDM_INFOVIEW,0,1,0,0,0,1));
  63.   Init();
  64. }
  65.  
  66. void TInfoView::Init()
  67. {
  68.   PropCount = Doc->PropertyCount();
  69.   PropList = new Data[PropCount];
  70.   for (int i = 0; i < PropCount; i++) {
  71.     PropList[i].PropName = Doc->PropertyName(i+1);
  72.     PropList[i].PropFlags = Doc->PropertyFlags(i+1);
  73.     PropList[i].PropData = 0;
  74.   }
  75.   OpenCount = CloseCount = 0;  //temp
  76.   GetInfo();
  77. }
  78.  
  79. TInfoView::~TInfoView()
  80. {
  81.   for (int i = 0; i < PropCount; i++) {
  82.     delete PropList[i].PropData;
  83.   }
  84.   delete PropList;
  85. }
  86.  
  87. void TInfoView::GetInfo()
  88. {
  89.   BOOL changed = FALSE;
  90.   int prop;
  91.   int len;
  92.   char buf[256+1];
  93.  
  94.   for (prop = 0; prop < PropCount; prop++) {
  95.     int flags = PropList[prop].PropFlags;
  96.     if ((flags & pfGetText) && !(flags & pfHidden)
  97.     && (len = Doc->GetProperty(prop+1, buf, sizeof(buf)-1)) != 0) {
  98.       if (PropList[prop].PropData) {
  99.         if (flags & pfConstant)
  100.           continue;
  101.         if (len == PropList[prop].PropSize) {
  102.           if (strcmp(buf, PropList[prop].PropData) != 0) {
  103.             strcpy(PropList[prop].PropData, buf);
  104.             changed = TRUE;
  105.           }
  106.           continue;
  107.         }
  108.         delete PropList[prop].PropData;
  109.       
  110.       }
  111.       PropList[prop].PropData = strnewdup(buf);
  112.       PropList[prop].PropSize = len;
  113.       changed = TRUE;
  114.     }
  115.   }
  116.   if (changed)
  117.     Invalidate();
  118. }
  119.  
  120. //  len = wsprintf(buf, "Document State:  %s  (Opens=%d Closes=%d)", (char far*)(Doc->IsOpen() ? "Open" : "Closed"),OpenCount,CloseCount);
  121.  
  122. void TInfoView::Paint(TDC& dc, BOOL /*erase*/, TRect&)
  123. {
  124.   TSize size;
  125.   static char separator[] = " = ";
  126.   int sepsize;
  127.   int prop;
  128.  
  129.   YLoc = 0;
  130.   dc.GetTextExtent(separator, sizeof(separator)-1, size);
  131.   sepsize = size.cx;
  132.   for (prop = 0; prop < PropCount; prop++) {
  133.     if (!PropList[prop].PropData)
  134.       continue;
  135.     int len = strlen(PropList[prop].PropName);
  136.     dc.GetTextExtent(   PropList[prop].PropName, len, size);
  137.     dc.TextOut(0, YLoc, PropList[prop].PropName, len);
  138.     dc.TextOut(size.cx, YLoc, separator, sizeof(separator)-1);
  139.     dc.TextOut(size.cx+sepsize, YLoc, PropList[prop].PropData,
  140.                                strlen(PropList[prop].PropData));
  141.     YLoc += size.cy;
  142.   }
  143. }
  144.  
  145. void
  146. TInfoView::CmInfoOpen()
  147. {
  148.   if (!Doc->IsOpen()
  149.   && Doc->GetDocPath() != 0
  150.   && Doc->Open(ofRead | shReadWrite | ofNoCreate | ofPriority)) {
  151. //  GetInfo();
  152.     Doc->Close();
  153.     OpenCount--; CloseCount--;
  154.   }
  155. }
  156.  
  157. BOOL
  158. TInfoView::VnViewOpened(TView*)
  159. {
  160.   GetInfo();
  161.   return TRUE;
  162. }
  163.  
  164. BOOL
  165. TInfoView::VnViewClosed(TView*)
  166. {
  167.   GetInfo();
  168.   return TRUE;
  169. }
  170.  
  171. BOOL
  172. TInfoView::VnDocOpened(int)
  173. {
  174.   ++OpenCount;
  175.   GetInfo();
  176.   return TRUE;
  177. }
  178.  
  179. BOOL
  180. TInfoView::VnDocClosed(int)
  181. {
  182.   ++CloseCount;
  183.   GetInfo();
  184.   return TRUE;
  185. }
  186.  
  187. IMPLEMENT_STREAMABLE1(TInfoView, TWindowView);
  188.  
  189. void*
  190. TInfoView::Streamer::Read(ipstream& is, uint32 /*version*/) const
  191. {
  192.   ReadBaseObject((TWindowView*)GetObject(), is);
  193.   GetObject()->Init();
  194.   return GetObject();
  195. }
  196.  
  197. void
  198. TInfoView::Streamer::Write(opstream &os) const
  199. {
  200.   WriteBaseObject((TWindowView*)GetObject(), os);
  201. }
  202.  
  203. DEFINE_DOC_TEMPLATE_CLASS(TFileDocument, TInfoView,   InfoTemplate);
  204. InfoTemplate infoTpl("InfoView, All files", "*.*", 0, 0,dtAutoDelete|dtReadOnly);
  205.  
  206.