home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / drawex / drawcnv.cpp < prev    next >
C/C++ Source or Header  |  1998-07-03  |  5KB  |  157 lines

  1. //================================================================
  2. //  drawcnv.cxx -- myCanvasPane class defintion
  3. //  Copyright (C) 1995  Bruce E. Wampler
  4. //
  5. //  This SIMPLE canvas class just draws lines using the pen that
  6. //  goes with this canvas. It doesn't handle any scrolling or
  7. //  paging. It also only handles up to 200 lines.
  8. //================================================================
  9.  
  10. #include "drawcnv.h"
  11.  
  12. //================>>> myCanvasPane::myCanvasPane <<<==============
  13.   myCanvasPane::myCanvasPane()
  14.   {
  15.     // Initialize to known values
  16.     _mouseDown = 0;
  17.     _begx = -1;  _begy = -1;  _curx = -1;  _cury = -1;
  18.     _pt = new point[200];    // Just 200 lines for this example
  19.     _nextpt = 0;
  20.   }
  21.  
  22. //==============>>> myCanvasPane::myCanvasPane <<<================
  23.   myCanvasPane::~myCanvasPane()
  24.   {
  25.     delete [] _pt;        // free the point array
  26.   }
  27.  
  28. //=================>>> myCanvasPane::Clear <<<====================
  29.   void myCanvasPane::Clear()
  30.   {
  31.     vCanvasPane::Clear();    // clear the canvas
  32.     _nextpt = 0;        // and all the points
  33.   }
  34.  
  35. //================>>> myCanvasPane::MouseDown <<<=================
  36.   void myCanvasPane::MouseDown(int X, int Y, int button)
  37.   {
  38.     // Note beginning of line on mouse down
  39.     _mouseDown = 1;
  40.     _begx = _curx = X;    _begy = _cury = Y;
  41.  
  42.     _pt[_nextpt].x = X;  _pt[_nextpt].y = Y;
  43.     _pt[_nextpt].pPen = GetPen();
  44.     ++_nextpt;
  45.     if (_nextpt >= 200)        // really dumb, but SIMPLE!
  46.     _nextpt = 0;
  47.   }
  48.  
  49. //==================>>> myCanvasPane::MouseUp <<<=================
  50.   void myCanvasPane::MouseUp(int X, int Y, int button)
  51.   {
  52.     // Finish drawing line on mouse up
  53.     _mouseDown = 0;
  54.     if (_begx != X || _begy != Y)    // First time?
  55.     DrawLine(_begx, _begy, X, Y);
  56.  
  57.     _pt[_nextpt].x = X;    _pt[_nextpt].y = Y;
  58.     _pt[_nextpt].pPen = GetPen();
  59.     ++_nextpt;
  60.  
  61.     if (_nextpt >= 200)        // really dumb, but SIMPLE!
  62.     _nextpt = 0;
  63.  
  64.     _mouseDown = 0;        // ready for next line
  65.     _begx = -1;  _begy = -1;  _curx = -1;  _cury = -1;
  66.   }
  67.  
  68. //==================>>> myCanvasPane::MouseMove <<<===============
  69.   void myCanvasPane::MouseMove(int x, int y, int button)
  70.   {
  71.     // Draw rubber band line on mouse move
  72.     if (_begx != _curx || _begy != _cury)    // old line to clear?
  73.     DrawRubberLine(_begx, _begy, _curx, _cury);
  74.  
  75.     if (_begx != x || _begy != y)    // draw new line
  76.     DrawRubberLine(_begx, _begy, x, y);
  77.     
  78.     _curx = x;    _cury = y;        // update
  79.   }
  80.  
  81. //===================>>> myCanvasPane::Redraw <<<=================
  82.   void myCanvasPane::Redraw(int x, int y, int w, int h)
  83.   {
  84.     // This is a stupid Redraw that just redraws everything.
  85.     // It also starts losing things after 200 points. This
  86.     // is just sample code, remember!
  87.  
  88.     int x1, y1, x2, y2;        // work variables
  89.  
  90.     for (int ix = 0 ; ix < _nextpt ; ix += 2)
  91.       {
  92.     if (ix == 0 || _pt[ix].pPen != _pt[ix-2].pPen)
  93.         SetPen(_pt[ix].pPen);
  94.     x1 = _pt[ix].x;      y1 = _pt[ix].y;
  95.     x2 = _pt[ix+1].x;    y2 = _pt[ix+1].y;
  96.     DrawLine(x1, y1, x2, y2);
  97.       }
  98.   }
  99.  
  100. //==================>>> myCanvasPane::Resize <<<==================
  101.   void myCanvasPane::Resize(int w, int h)
  102.   {
  103.     // This simple example doesn't need to do anything special for
  104.     // resize. The default will cause a Redraw.
  105.     vCanvasPane::Resize(w,h);
  106.   }
  107.  
  108. #include <fstream.h>        // to save/restore drawings
  109.  
  110. //==================>>> myCanvasPane::Read <<<====================
  111.   int myCanvasPane::Read(char* name)
  112.   {
  113.     // Read in a file of points
  114.     int r,g,b,ps,pw;
  115.  
  116.     if (!name || !*name) return 0;    // sanity check
  117.     ifstream inFile(name);        // open the file to read
  118.  
  119.     if (!inFile) return 0;        // OK?
  120.     
  121.     inFile >> _nextpt;            // number of points
  122.     for (int ix = 0 ; ix < _nextpt ; ++ix)  // read the points
  123.       {
  124.     inFile >> _pt[ix].x >> _pt[ix].y >> r >> g >> b
  125.         >> pw >> ps;
  126.       _pt[ix].pPen.SetColor(r,g,b); // can't read directly
  127.       _pt[ix].pPen.SetWidth(pw);
  128.       _pt[ix].pPen.SetStyle(ps);
  129.       }
  130.     inFile.close();            // finished with file
  131.     return 1;
  132.   }
  133.  
  134. //==================>>> myCanvasPane::Save <<<====================
  135.   int myCanvasPane::Save(char* name)
  136.   {
  137.     // Save drawing to a file of points
  138.  
  139.     if (!name || !*name) return 0;    // sanity check
  140.     ofstream outFile(name);        // open output file
  141.  
  142.     if (!outFile) return 0;        // OK?
  143.  
  144.     outFile << _nextpt << endl;        // number of points
  145.     for (int ix = 0 ; ix < _nextpt ; ++ix) // write the points
  146.       {
  147.     outFile << _pt[ix].x << " " << _pt[ix].y << " "
  148.         << _pt[ix].pPen.GetColor().r() << " "
  149.         << _pt[ix].pPen.GetColor().g() << " "
  150.         << _pt[ix].pPen.GetColor().b() << " "
  151.         << _pt[ix].pPen.GetWidth() << " "
  152.         << _pt[ix].pPen.GetStyle() << endl;
  153.       }
  154.     outFile.close();            // done with file
  155.     return 1;
  156.   }
  157.