home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 20.ddi / DOCVIEW.PAK / LINEDOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  19.7 KB  |  835 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //   Implements classes TDrawDocument, TDrawView, TDrawListView
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include <owl\dc.h>
  7. #include <owl\inputdia.h>
  8. #include <owl\chooseco.h>
  9. #include <owl\gdiobjec.h>
  10. #include <owl\docmanag.h>
  11. #include <owl\filedoc.h>
  12. #include <owl\listbox.h>
  13. #include <classlib\arrays.h>
  14. #include "linedoc.rc"
  15.  
  16. typedef TArray<TPoint> TPoints;
  17. typedef TArrayIterator<TPoint> TPointsIterator;
  18.  
  19. class TLine : public TPoints {
  20.   public:
  21.     // Constructor to allow construction from a color and a pen size.
  22.     // Also serves as default constructor.
  23.     TLine(const TColor &color = TColor(0), int penSize = 1)
  24.       : TPoints(10, 0, 10), PenSize(penSize), Color(color) {}
  25.  
  26.     // Functions to modify and query pen attributes.
  27.     int QueryPenSize() const { return PenSize; }
  28.     const TColor& QueryColor() const { return Color; }
  29.     void SetPen(TColor &newColor, int penSize = 0);
  30.     void SetPen(int penSize);
  31.     BOOL GetPenSize();
  32.     BOOL GetPenColor();
  33.  
  34.     // TLine draws itself.  Returns TRUE if everything went OK.
  35.     virtual BOOL Draw(TDC &) const;
  36.  
  37.     // The == operator must be defined for the container class, even if unused
  38.     BOOL operator ==(const TLine& other) const { return &other == this; }
  39.     friend ostream& operator <<(ostream& os, const TLine& line);
  40.     friend istream& operator >>(istream& is, TLine& line);
  41.  
  42.   protected:
  43.     int PenSize;
  44.     TColor Color;
  45. };
  46.  
  47. typedef TArray<TLine> TLines;
  48. typedef TArrayIterator<TLine> TLinesIterator;
  49.  
  50. class _DOCVIEWCLASS TDrawDocument : public TFileDocument
  51. {
  52.   public:
  53.     enum {
  54.       PrevProperty = TFileDocument::NextProperty-1,
  55.       LineCount,
  56.       Description,
  57.       NextProperty,
  58.     };
  59.  
  60.     enum {
  61.       UndoNone,
  62.       UndoDelete,
  63.       UndoAppend,
  64.       UndoModify
  65.     };
  66.  
  67.     TDrawDocument(TDocument* parent = 0)
  68.         : TFileDocument(parent), Lines(0), UndoLine(0), UndoState(UndoNone) {}
  69.    ~TDrawDocument() { delete Lines; delete UndoLine; }
  70.  
  71.     // implement virtual methods of TDocument
  72.     BOOL   Open(int mode, const char far* path=0);
  73.     BOOL   Close();
  74.     BOOL   IsOpen() { return Lines != 0; }
  75.     BOOL   Commit(BOOL force = FALSE);
  76.     BOOL   Revert(BOOL clear = FALSE);
  77.  
  78.     int         FindProperty(const char far* name);  // return index
  79.     int         PropertyFlags(int index);
  80.     const char* PropertyName(int index);
  81.     int         PropertyCount() {return NextProperty - 1;}
  82.     int         GetProperty(int index, void far* dest, int textlen=0);
  83.  
  84.     // data access functions
  85.     const TLine* GetLine(unsigned int index);
  86.     int    AddLine(TLine& line);
  87.     void   DeleteLine(unsigned int index);
  88.     void   ModifyLine(TLine& line, unsigned int index);
  89.     void   Clear();
  90.     void   Undo();
  91.  
  92.   protected:
  93.     TLines* Lines;
  94.     TLine* UndoLine;
  95.     int    UndoState;
  96.     int    UndoIndex;
  97.     string FileInfo;
  98.  
  99.   DECLARE_STREAMABLE(, TDrawDocument,1);
  100. };
  101.  
  102. class _DOCVIEWCLASS TDrawView : public TWindowView
  103. {
  104.   public:
  105.     TDrawView(TDrawDocument& doc, TWindow *parent = 0);
  106.    ~TDrawView() {delete DragDC; delete Line;}
  107.     static const char far* StaticName() {return "Draw View";}
  108.     const char far* GetViewName(){return StaticName();}
  109.  
  110.   protected:
  111.     TDrawDocument* DrawDoc;  // same as Doc member, but cast to derived class
  112.     TDC *DragDC;
  113.     TPen *Pen;
  114.     TLine *Line; // To hold a single line sent or received from document
  115.  
  116.     // Message response functions
  117.     void EvLButtonDown(UINT, TPoint&);
  118.     void EvRButtonDown(UINT, TPoint&);
  119.     void EvMouseMove(UINT, TPoint&);
  120.     void EvLButtonUp(UINT, TPoint&);
  121.     void Paint(TDC&, BOOL, TRect&);
  122.     void CmPenSize();
  123.     void CmPenColor();
  124.     void CmClear();
  125.     void CmUndo();
  126.  
  127.     // Document notifications
  128.     BOOL VnCommit(BOOL force);
  129.     BOOL VnRevert(BOOL clear);
  130.     BOOL VnAppend(unsigned int index);
  131.     BOOL VnDelete(unsigned int index);
  132.     BOOL VnModify(unsigned int index);
  133.  
  134.   DECLARE_RESPONSE_TABLE(TDrawView);
  135.   DECLARE_STREAMABLE(,TDrawView,1);
  136. };
  137.  
  138. class _DOCVIEWCLASS TDrawListView : public TListBox, public TView {
  139.   public:
  140.     TDrawListView(TDrawDocument& doc, TWindow* parent = 0);
  141.    ~TDrawListView(){}
  142.     static const char far* StaticName() {return "DrawList View";}
  143.  
  144.     // Overridden virtuals from TView
  145.     //
  146.     const char far* GetViewName(){return StaticName();}
  147.     TWindow* GetWindow()  {return (TWindow*)this;}
  148.     BOOL     SetDocTitle(const char far* docname, int index)
  149.                           {return TListBox::SetDocTitle(docname, index); }
  150.  
  151.     // Overridden virtuals from TWindow
  152.     //
  153.     BOOL CanClose()   {return TListBox::CanClose() && Doc->CanClose();}
  154.     BOOL Create();
  155.  
  156.   protected:
  157.     TDrawDocument* DrawDoc;  // same as Doc member, but cast to derived class
  158.     void LoadData();
  159.     void FormatData(const TLine* line, unsigned int index);
  160.  
  161.     // Message response functions
  162.     void CmPenSize();
  163.     void CmPenColor();
  164.     void CmClear();
  165.     void CmUndo();
  166.     void CmDelete();
  167.  
  168.     // Document notifications
  169.     BOOL VnIsWindow(HWND hWnd) {return HWindow == hWnd;}
  170.     BOOL VnCommit(BOOL force);
  171.     BOOL VnRevert(BOOL clear);
  172.     BOOL VnAppend(unsigned int index);
  173.     BOOL VnDelete(unsigned int index);
  174.     BOOL VnModify(unsigned int index);
  175.  
  176.   DECLARE_RESPONSE_TABLE(TDrawListView);
  177.   DECLARE_STREAMABLE(,TDrawListView,1);
  178. };
  179.  
  180. const int vnDrawAppend = vnCustomBase+0;
  181. const int vnDrawDelete = vnCustomBase+1;
  182. const int vnDrawModify = vnCustomBase+2;
  183.  
  184. NOTIFY_SIG(vnDrawAppend, unsigned int)
  185. NOTIFY_SIG(vnDrawDelete, unsigned int)
  186. NOTIFY_SIG(vnDrawModify, unsigned int)
  187.  
  188. #define EV_VN_DRAWAPPEND  VN_DEFINE(vnDrawAppend,  VnAppend,  int)
  189. #define EV_VN_DRAWDELETE  VN_DEFINE(vnDrawDelete,  VnDelete,  int)
  190. #define EV_VN_DRAWMODIFY  VN_DEFINE(vnDrawModify,  VnModify,  int)
  191.  
  192. DEFINE_DOC_TEMPLATE_CLASS(TDrawDocument, TDrawView,   DrawTemplate);
  193. DEFINE_DOC_TEMPLATE_CLASS(TDrawDocument, TDrawListView,   DrawListTemplate);
  194. DrawTemplate drawTpl("Draw Line Files (*.PTS)","*.pts",0,"PTS",dtAutoDelete|dtUpdateDir);
  195. DrawListTemplate drawListTpl("Line List","  *.pts",0,"PTS",dtAutoDelete|dtHidden);
  196.  
  197. void TLine::SetPen(int penSize)
  198. {
  199.   if (penSize < 1)
  200.     PenSize = 1;
  201.   else
  202.     PenSize = penSize;
  203. }
  204.  
  205. void TLine::SetPen(TColor &newColor, int penSize)
  206. {
  207.   // If penSize isn't the default (0), set PenSize to the new size.
  208.   if (penSize)
  209.     PenSize = penSize;
  210.  
  211.   Color = newColor;
  212. }
  213.  
  214. BOOL TLine::Draw(TDC &dc) const
  215. {
  216.   // Set pen for the dc to the values for this line
  217.   TPen pen(Color, PenSize);
  218.   dc.SelectObject(pen);
  219.  
  220.   // Iterates through the points in the line i.
  221.   TPointsIterator j(*this);
  222.   BOOL first = TRUE;
  223.  
  224.   while (j) {
  225.     TPoint p = j++;
  226.  
  227.     if (!first)
  228.       dc.LineTo(p);
  229.     else {
  230.       dc.MoveTo(p);
  231.       first = FALSE;
  232.     }
  233.   }
  234.   dc.RestorePen();
  235.   return TRUE;
  236. }
  237.  
  238. ostream& operator <<(ostream& os, const TLine& line)
  239. {
  240.   // Write the number of points in the line
  241.   os << line.GetItemsInContainer();
  242.  
  243.   // Get and write pen attributes.
  244.   os << ' ' << line.Color << ' ' << line.PenSize;
  245.  
  246.   // Get an iterator for the array of points
  247.   TPointsIterator j(line);
  248.  
  249.   // While the iterator is valid (i.e. we haven't run out of points)
  250.   while(j)
  251.     // Write the point from the iterator and increment the array.
  252.     os << j++;
  253.   os << '\n';
  254.  
  255.   // return the stream object
  256.   return os;
  257. }
  258.  
  259. istream& operator >>(istream& is, TLine& line)
  260. {
  261.   unsigned numPoints;
  262.   is >> numPoints;
  263.  
  264.   COLORREF color;
  265.   int penSize;
  266.   is >> color >> penSize;
  267.   line.SetPen(TColor(color), penSize);
  268.  
  269.   while (numPoints--) {
  270.     TPoint point;
  271.     is >> point;
  272.     line.Add(point);
  273.   }
  274.  
  275.   // return the stream object
  276.   return is;
  277. }
  278.  
  279. BOOL TDrawDocument::Commit(BOOL force)
  280. {
  281.   if (!IsDirty() && !force)
  282.     return TRUE;
  283.  
  284.   TOutStream* os = OutStream(ofWrite);
  285.   if (!os)
  286.     return FALSE;
  287.  
  288.   // Write the number of lines in the figure
  289.   *os << Lines->GetItemsInContainer();
  290.  
  291.   // Append a description using a resource string
  292.   *os << ' ' << FileInfo << '\n';
  293.  
  294.   // Get an iterator for the array of lines
  295.   TLinesIterator i(*Lines);
  296.  
  297.   // While the iterator is valid (i.e. we haven't run out of lines)
  298.   while (i) {
  299.     // Copy the current line from the iterator and increment the array.
  300.     *os << i++;
  301.   }
  302.   delete os;
  303.  
  304.   SetDirty(FALSE);
  305.   return TRUE;
  306. }
  307.  
  308. BOOL TDrawDocument::Revert(BOOL clear)
  309. {
  310.   if (!TFileDocument::Revert(clear))
  311.     return FALSE;
  312.   if (!clear)
  313.     Open(0);
  314.   return TRUE;
  315. }
  316.  
  317. BOOL TDrawDocument::Open(int /*mode*/, const char far* path)
  318. {
  319.   char fileinfo[100];
  320.   Lines = new TLines(5, 0, 5);
  321.   if (path)
  322.     SetDocPath(path);
  323.   if (GetDocPath()) {
  324.     TInStream* is = InStream(ofRead);
  325.     if (!is)
  326.       return FALSE;
  327.  
  328.     unsigned numLines;
  329.     *is >> numLines;
  330.     is->getline(fileinfo, sizeof(fileinfo));
  331.     while (numLines--) {
  332.       TLine line;
  333.       *is >> line;
  334.       Lines->Add(line);
  335.     }
  336.     delete is;
  337.     FileInfo = fileinfo;
  338.   } else {
  339.     FileInfo = string(*::Module,IDS_FILEINFO);
  340.   }  
  341.   SetDirty(FALSE);
  342.   UndoState = UndoNone;
  343.   return TRUE;
  344. }
  345.  
  346. BOOL TDrawDocument::Close()
  347. {
  348.   delete Lines;
  349.   Lines = 0;
  350.   return TRUE;
  351. }
  352.  
  353. const TLine* TDrawDocument::GetLine(unsigned int index)
  354. {
  355.   if (!IsOpen() && !Open(ofRead | ofWrite))
  356.     return 0;
  357.   return index < Lines->GetItemsInContainer() ? &(*Lines)[index] : 0;
  358. }
  359.  
  360. int TDrawDocument::AddLine(TLine& line)
  361. {
  362.   int index = Lines->GetItemsInContainer();
  363.   Lines->Add(line);
  364.   SetDirty(TRUE);
  365.   NotifyViews(vnDrawAppend, index); 
  366.   UndoState = UndoAppend;
  367.   return index;
  368. }
  369.  
  370. void TDrawDocument::DeleteLine(unsigned int index)
  371. {
  372.   const TLine* oldLine = GetLine(index);
  373.   if (!oldLine)
  374.     return;
  375.   delete UndoLine;
  376.   UndoLine = new TLine(*oldLine);
  377.   Lines->Detach(index);
  378.   SetDirty(TRUE);
  379.   NotifyViews(vnDrawDelete, index); 
  380.   UndoState = UndoDelete;
  381. }
  382.  
  383. void TDrawDocument::ModifyLine(TLine& line, unsigned int index)
  384. {
  385.   delete UndoLine;
  386.   UndoLine = new TLine((*Lines)[index]);
  387.   SetDirty(TRUE);
  388.   (*Lines)[index] = line;
  389.   NotifyViews(vnDrawModify, index);
  390.   UndoState = UndoModify;
  391.   UndoIndex = index;
  392. }
  393.  
  394. void TDrawDocument::Clear()
  395. {
  396.   Lines->Flush();
  397.   NotifyViews(vnRevert, TRUE); 
  398. }
  399.  
  400. void TDrawDocument::Undo()
  401. {
  402.   switch (UndoState) {
  403.     case UndoAppend:
  404.       DeleteLine(Lines->GetItemsInContainer()-1);
  405.       return;
  406.     case UndoDelete:
  407.       AddLine(*UndoLine);
  408.       delete UndoLine;
  409.       UndoLine = 0;
  410.       return;
  411.     case UndoModify:
  412.       TLine* temp = UndoLine;
  413.       UndoLine = 0;
  414.       ModifyLine(*temp, UndoIndex);
  415.       delete temp;
  416.   }
  417. }
  418.  
  419. BOOL TLine::GetPenSize()
  420. {
  421.   char inputText[6];
  422.  
  423.   wsprintf(inputText, "%d", PenSize);
  424.   if (TInputDialog(0, "Line Thickness",
  425.                         "Input a new thickness:",
  426.                         inputText,
  427.                         sizeof(inputText),::Module).Execute() != IDOK)
  428.     return FALSE;
  429.   PenSize = atoi(inputText);
  430.  
  431.   if (PenSize < 1)
  432.     PenSize = 1;
  433.  
  434.   return TRUE;
  435. }
  436.  
  437. BOOL TLine::GetPenColor()
  438. {
  439.   TChooseColorDialog::TData colors;
  440.   static TColor custColors[16] =
  441.   {
  442.     0x010101L, 0x101010L, 0x202020L, 0x303030L,
  443.     0x404040L, 0x505050L, 0x606060L, 0x707070L,
  444.     0x808080L, 0x909090L, 0xA0A0A0L, 0xB0B0B0L,
  445.     0xC0C0C0L, 0xD0D0D0L, 0xE0E0E0L, 0xF0F0F0L
  446.   };
  447.  
  448.   colors.Flags = CC_RGBINIT;
  449.   colors.Color = TColor(QueryColor());
  450.   colors.CustColors = custColors;
  451.   if (TChooseColorDialog(0, colors).Execute() != IDOK)
  452.     return FALSE;
  453.   SetPen(colors.Color);
  454.   return TRUE;
  455. }
  456.  
  457. DEFINE_RESPONSE_TABLE1(TDrawView, TWindowView)
  458.   EV_WM_LBUTTONDOWN,
  459.   EV_WM_RBUTTONDOWN,
  460.   EV_WM_MOUSEMOVE,
  461.   EV_WM_LBUTTONUP,
  462.   EV_COMMAND(CM_PENSIZE, CmPenSize),
  463.   EV_COMMAND(CM_PENCOLOR, CmPenColor),
  464.   EV_COMMAND(CM_CLEAR, CmClear),
  465.   EV_COMMAND(CM_UNDO, CmUndo),
  466.   EV_VN_COMMIT,
  467.   EV_VN_REVERT,
  468.   EV_VN_DRAWAPPEND,
  469.   EV_VN_DRAWDELETE,
  470.   EV_VN_DRAWMODIFY,
  471. END_RESPONSE_TABLE;
  472.  
  473. TDrawView::TDrawView(TDrawDocument& doc,TWindow *parent)
  474.          : TWindowView(doc,parent), DrawDoc(&doc)
  475. {
  476.   DragDC = 0;
  477.   Line = new TLine(TColor::Black, 1);
  478.   SetViewMenu(new TMenuDescr(IDM_DRAWVIEW,0,2,0,0,0,1));
  479. }
  480.  
  481. void TDrawView::EvLButtonDown(UINT, TPoint& point)
  482. {
  483.   if (!DragDC) {
  484.     SetCapture();
  485.     DragDC = new TClientDC(*this);
  486.     Pen = new TPen(Line->QueryColor(), Line->QueryPenSize());
  487.     DragDC->SelectObject(*Pen);
  488.     DragDC->MoveTo(point);
  489.     Line->Add(point);
  490.   }
  491. }
  492.  
  493. void TDrawView::EvRButtonDown(UINT, TPoint&)
  494. {
  495.   CmUndo();
  496. }
  497.  
  498. void TDrawView::EvMouseMove(UINT, TPoint& point)
  499. {
  500.   if (DragDC) {
  501.     DragDC->LineTo(point);
  502.     Line->Add(point);
  503.   }
  504. }
  505.  
  506. void TDrawView::EvLButtonUp(UINT, TPoint&)
  507. {
  508.   if (DragDC) {
  509.     ReleaseCapture();
  510.     if (Line->GetItemsInContainer() > 1)
  511.       DrawDoc->AddLine(*Line);
  512.     Line->Flush();
  513.     delete DragDC;
  514.     delete Pen;
  515.     DragDC = 0;
  516.   }
  517. }
  518.  
  519. void TDrawView::CmPenSize()
  520. {
  521.   Line->GetPenSize();
  522. }
  523.  
  524. void TDrawView::CmPenColor()
  525. {
  526.   Line->GetPenColor();
  527. }
  528.  
  529. void TDrawView::CmClear()
  530. {
  531.   DrawDoc->Clear();
  532. }
  533.  
  534. void TDrawView::CmUndo()
  535. {
  536.   DrawDoc->Undo();
  537. }
  538.  
  539. void TDrawView::Paint(TDC& dc, BOOL, TRect&)
  540. {
  541.   // Iterates through the array of line objects.
  542.   int i = 0;
  543.   const TLine* line;
  544.   while ((line = DrawDoc->GetLine(i++)) != 0)
  545.     line->Draw(dc);
  546. }
  547.  
  548. BOOL TDrawView::VnCommit(BOOL /*force*/)
  549. {
  550.   // nothing to do here, no data held in view
  551.   return TRUE;
  552. }
  553.  
  554. BOOL TDrawView::VnRevert(BOOL /*clear*/)
  555. {
  556.   Invalidate();  // force full repaint
  557.   return TRUE;
  558. }
  559.  
  560. BOOL TDrawView::VnAppend(unsigned int index)
  561. {
  562.   TClientDC dc(*this);
  563.   const TLine* line = DrawDoc->GetLine(index);
  564.   line->Draw(dc);
  565.   return TRUE;
  566. }
  567.  
  568. BOOL TDrawView::VnModify(unsigned int /*index*/)
  569. {
  570.   Invalidate();  // force full repaint
  571.   return TRUE;
  572. }
  573.  
  574. BOOL TDrawView::VnDelete(unsigned int /*index*/)
  575. {
  576.   Invalidate();  // force full repaint
  577.   return TRUE;
  578. }
  579.  
  580. DEFINE_RESPONSE_TABLE1(TDrawListView, TListBox)
  581.   EV_COMMAND(CM_PENSIZE, CmPenSize),
  582.   EV_COMMAND(CM_PENCOLOR, CmPenColor),
  583.   EV_COMMAND(CM_CLEAR, CmClear),
  584.   EV_COMMAND(CM_UNDO, CmUndo),
  585.   EV_COMMAND(CM_DELETE, CmDelete),
  586.   EV_VN_ISWINDOW,
  587.   EV_VN_COMMIT,
  588.   EV_VN_REVERT,
  589.   EV_VN_DRAWAPPEND,
  590.   EV_VN_DRAWDELETE,
  591.   EV_VN_DRAWMODIFY,
  592. END_RESPONSE_TABLE;
  593.  
  594. TDrawListView::TDrawListView(TDrawDocument& doc,TWindow *parent)
  595.        : TView(doc), TListBox(parent, GetNextViewId(), 0,0,0,0), DrawDoc(&doc)
  596. {
  597.   Attr.Style &= ~(WS_BORDER | LBS_SORT);
  598.   Attr.AccelTable = IDA_DRAWLISTVIEW;   
  599.   SetViewMenu(new TMenuDescr(IDM_DRAWLISTVIEW,0,1,0,0,0,1));
  600.  
  601. BOOL TDrawListView::Create()
  602. {
  603.   TListBox::Create();
  604.   LoadData();
  605.   return TRUE;
  606. }
  607.  
  608. void TDrawListView::LoadData()
  609. {
  610.   ClearList();
  611.   int i = 0;
  612.   const TLine* line;
  613.   while ((line = DrawDoc->GetLine(i)) != 0)
  614.     FormatData(line, i++);
  615.  
  616.   SetSelIndex(0);
  617. }
  618.  
  619. void TDrawListView::FormatData(const TLine* line, int unsigned index)
  620. {
  621.   char buf[80];
  622.   TColor color(line->QueryColor());
  623.   wsprintf(buf, "Color = R%d G%d B%d, Size = %d, Points = %d",
  624.            color.Red(), color.Green(), color.Blue(),
  625.            line->QueryPenSize(), line->GetItemsInContainer());
  626.   DeleteString(index);
  627.   InsertString(buf, index);
  628.   SetSelIndex(index);
  629. }
  630.   
  631. void TDrawListView::CmPenSize()
  632. {
  633.   int index = GetSelIndex();
  634.   const TLine* line = DrawDoc->GetLine(index);
  635.   if (line) {
  636.     TLine* newline = new TLine(*line);
  637.     if (newline->GetPenSize())
  638.       DrawDoc->ModifyLine(*newline, index);
  639.     delete newline;
  640.   }
  641. }
  642.  
  643. void TDrawListView::CmPenColor()
  644. {
  645.   int index = GetSelIndex();
  646.   const TLine* line = DrawDoc->GetLine(index);
  647.   if (line) {
  648.     TLine* newline = new TLine(*line);
  649.     if (newline->GetPenColor())
  650.       DrawDoc->ModifyLine(*newline, index);
  651.     delete newline;
  652.   }
  653. }
  654.  
  655. void TDrawListView::CmClear()
  656. {
  657.   DrawDoc->Clear();
  658. }
  659.  
  660. void TDrawListView::CmUndo()
  661. {
  662.   DrawDoc->Undo();
  663. }
  664.  
  665. void TDrawListView::CmDelete()
  666. {
  667.   DrawDoc->DeleteLine(GetSelIndex());
  668. }
  669.  
  670. BOOL TDrawListView::VnCommit(BOOL /*force*/)
  671. {
  672.   return TRUE;
  673. }
  674.  
  675. BOOL TDrawListView::VnRevert(BOOL /*clear*/)
  676. {
  677.   LoadData();
  678.   return TRUE;
  679. }
  680.  
  681. BOOL TDrawListView::VnAppend(unsigned int index)
  682. {
  683.   const TLine* line = DrawDoc->GetLine(index);
  684.   FormatData(line, index);
  685.   SetSelIndex(index);
  686.   return TRUE;
  687. }
  688.  
  689. BOOL TDrawListView::VnDelete(unsigned int index)
  690. {
  691.   DeleteString(index);
  692.   HandleMessage(WM_KEYDOWN,VK_DOWN); // force selection
  693.   return TRUE;
  694. }
  695.  
  696. BOOL TDrawListView::VnModify(unsigned int index)
  697. {
  698.   const TLine* line = DrawDoc->GetLine(index);
  699.   FormatData(line, index);
  700.   return TRUE;
  701. }
  702.  
  703. static char* PropNames[] = {
  704.   "Line Count",             // LineCount
  705.   "Description",             // Description
  706. };
  707.  
  708. static int PropFlags[] = {
  709.   pfGetBinary|pfGetText, // LineCount
  710.   pfGetText,             // Description
  711. };
  712.  
  713. const char*
  714. TDrawDocument::PropertyName(int index)
  715. {
  716.   if (index <= PrevProperty)
  717.     return TFileDocument::PropertyName(index);
  718.   else if (index < NextProperty)
  719.     return PropNames[index-PrevProperty-1];
  720.   else
  721.     return 0;
  722. }
  723.  
  724. int
  725. TDrawDocument::PropertyFlags(int index)
  726. {
  727.   if (index <= PrevProperty)
  728.     return TFileDocument::PropertyFlags(index);
  729.   else if (index < NextProperty)
  730.     return PropFlags[index-PrevProperty-1];
  731.   else
  732.     return 0;
  733. }
  734.  
  735. int
  736. TDrawDocument::FindProperty(const char far* name)
  737. {
  738.   for (int i=0; i < NextProperty-PrevProperty-1; i++)
  739.     if (strcmp(PropNames[i], name) == 0)
  740.       return i+PrevProperty+1;
  741.   return 0;
  742. }
  743.  
  744. int
  745. TDrawDocument::GetProperty(int prop, void far* dest, int textlen)
  746. {
  747.   if (IsOpen())
  748.     switch(prop)
  749.     {
  750.       case LineCount:
  751.       {
  752.         int count = Lines->GetItemsInContainer();
  753.         if (!textlen) {
  754.           *(int far*)dest = count;
  755.           return sizeof(int);
  756.         }
  757.         return wsprintf((char far*)dest, "%d", count);
  758.       }
  759.       case Description:
  760.         char* temp = new char[textlen]; // need local copy for medium model
  761.         int len = FileInfo.copy(temp, textlen);
  762.         strcpy((char far*)dest, temp);
  763.         return len;
  764.     }
  765.   return TFileDocument::GetProperty(prop, dest, textlen);
  766. }
  767.  
  768. IMPLEMENT_STREAMABLE1(TDrawDocument, TFileDocument);
  769.  
  770. void*
  771. TDrawDocument::Streamer::Read(ipstream& is, uint32 /*version*/) const
  772. {
  773.   TDrawDocument* o = GetObject();
  774.   o->UndoState = UndoNone;
  775.   o->UndoLine = 0;
  776.   o->Lines = 0;
  777.   ReadBaseObject((TFileDocument*)o, is);
  778.   return o;
  779. }
  780.  
  781. void
  782. TDrawDocument::Streamer::Write(opstream& os) const
  783. {
  784.   WriteBaseObject((TFileDocument*)GetObject(), os);
  785. }
  786.  
  787. IMPLEMENT_STREAMABLE1(TDrawView, TWindowView);
  788.  
  789. void*
  790. TDrawView::Streamer::Read(ipstream& is, uint32 /*version*/) const
  791. {
  792.   TDrawView* o = GetObject();
  793.   ReadBaseObject((TWindowView*)o, is);
  794.   is >> o->DrawDoc;
  795.   int width;
  796.   COLORREF color;  
  797.   is >> width >> color;
  798.   o->Line = new TLine(TColor(color), width);
  799.   o->DragDC = 0;
  800.   return o;
  801. }
  802.  
  803. void
  804. TDrawView::Streamer::Write(opstream &os) const
  805. {
  806.   TDrawView* o = GetObject();
  807.   WriteBaseObject((TWindowView*)o, os);
  808.   os << o->DrawDoc;
  809.   os << o->Line->QueryPenSize();
  810.   os << COLORREF(o->Line->QueryColor());
  811. }
  812.  
  813. IMPLEMENT_STREAMABLE2(TDrawListView, TListBox, TView);
  814.  
  815. void*
  816. TDrawListView::Streamer::Read(ipstream& is, uint32 /*version*/) const
  817. {
  818.   TDrawListView* o = GetObject();
  819.   ReadBaseObject((TListBox*)o, is);
  820.   ReadBaseObject((TView*)o, is);
  821.   is >> o->DrawDoc;
  822.   return o;
  823. }
  824.  
  825. void
  826. TDrawListView::Streamer::Write(opstream &os) const
  827. {
  828.   TDrawListView* o = GetObject();
  829.   WriteBaseObject((TListBox*)o, os);
  830.   WriteBaseObject((TView*)o, os);
  831.   os << o->DrawDoc;
  832. }
  833.  
  834.