home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL3.ZIP / STEPS.ZIP / STEP10.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  9.5 KB  |  399 lines

  1. // ObjectWindows - (C) Copyright 1991 by Borland International
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <dir.h>
  6. #include <array.h>
  7. #include <abstarry.h>
  8. #include <string.h>
  9. #include <owl.h>
  10. #include <inputdia.h>
  11. #include <filedial.h>
  12. #include "helpwind.h"
  13. #include "steps.h"
  14.  
  15. class TMyApp : public TApplication
  16. {
  17. public:
  18.   TMyApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  19.     LPSTR lpCmdLine, int nCmdShow)
  20.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  21.   virtual void InitMainWindow();
  22. };
  23.  
  24. _CLASSDEF(TPoint)
  25. class TPoint: public Object
  26. {
  27. public:
  28.   int X, Y;
  29.   TPoint(int AX, int AY) {X = AX, Y = AY;}
  30.   virtual classType isA() const { return __firstUserClass; }
  31.   virtual Pchar nameOf() const { return "TPoint"; }
  32.   virtual hashValueType hashValue() const { return 0; } 
  33.   virtual int isEqual(RCObject APoint) const
  34.     { return X == ((RTPoint)APoint).X && Y == ((RTPoint)APoint).Y; }
  35.   virtual void printOn(Rostream outputStream) const
  36.     { outputStream << "(" << X << "," << Y << ")"; }
  37. };
  38.  
  39. _CLASSDEF(TPointArray)
  40. class TPointArray : public Array, public TStreamable
  41. {
  42. public:
  43.   TPointArray(int upper, int lower = 0, sizeType aDelta = 0)
  44.     : Array(upper, lower, aDelta){};
  45.   void DeleteAll();
  46.   virtual classType isA() const { return __firstUserClass + 1; }
  47.   virtual Pchar nameOf() const { return "TPointArray"; }
  48.   static PTStreamable build();
  49. protected:
  50.   TPointArray(StreamableInit) : Array(50, 0, 50) {};
  51.   virtual void write( Ropstream );
  52.   virtual Pvoid read( Ripstream );
  53. private:
  54.   virtual const Pchar streamableName() const
  55.     { return "TPointArray"; }
  56. };
  57.  
  58. inline Ripstream operator >> ( Ripstream is, RTPointArray cl )
  59.   { return is >> (RTStreamable)cl; }
  60.  
  61. inline Ripstream operator >> ( Ripstream is, RPTPointArray cl )
  62.   { return is >> (RPvoid)cl; }
  63.  
  64. inline Ropstream operator << ( Ropstream os, RTPointArray cl )
  65.   { return os << (RTStreamable)cl; }
  66.  
  67. inline Ropstream operator << ( Ropstream os, PTPointArray cl )
  68.   { return os << (PTStreamable)cl; }
  69.  
  70.  
  71. _CLASSDEF(TMyWindow)
  72. class TMyWindow : public TWindow
  73. {
  74. public:
  75.   HDC DragDC;
  76.   BOOL ButtonDown;
  77.   HPEN ThePen;
  78.   int PenSize;
  79.   PTPointArray Points;
  80.   char FileName[MAXPATH];
  81.   BOOL IsDirty, IsNewFile;
  82.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle);
  83.   ~TMyWindow();
  84.   virtual BOOL CanClose();
  85.   void SetPenSize(int NewSize);
  86.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  87.   virtual void OpenFile();
  88.   virtual void SaveFile();
  89.   virtual void SaveFileAs();
  90.   virtual void WMLButtonDown(RTMessage Msg)
  91.     = [WM_FIRST + WM_LBUTTONDOWN];
  92.   virtual void WMLButtonUp(RTMessage Msg)
  93.     = [WM_FIRST + WM_LBUTTONUP];
  94.   virtual void WMMouseMove(RTMessage Msg)
  95.     = [WM_FIRST + WM_MOUSEMOVE];
  96.   virtual void WMRButtonDown(RTMessage Msg)
  97.     = [WM_FIRST + WM_RBUTTONDOWN];
  98.   virtual void CMFileNew(RTMessage Msg)
  99.     = [CM_FIRST + CM_FILENEW];
  100.   virtual void CMFileOpen(RTMessage Msg)
  101.     = [CM_FIRST + CM_FILEOPEN];
  102.   virtual void CMFileSave(RTMessage Msg)
  103.     = [CM_FIRST + CM_FILESAVE];
  104.   virtual void CMFileSaveAs(RTMessage Msg)
  105.     = [CM_FIRST + CM_FILESAVEAS];
  106.   virtual void CMHelp(RTMessage Msg)
  107.     = [CM_FIRST + CM_HELP];
  108.   static PTStreamable build();
  109.  
  110. protected:
  111.   TMyWindow(StreamableInit) : TWindow(streamableInit) {};
  112.   virtual void write( Ropstream );
  113.   virtual Pvoid read( Ripstream );
  114.  
  115. private:
  116.   const Pchar streamableName() const { return "TMyWindow"; }
  117. };
  118.  
  119. inline Ripstream operator >> ( Ripstream is, RTMyWindow cl )
  120.   { return is >> (RTStreamable)cl; }
  121.  
  122. inline Ripstream operator >> ( Ripstream is, RPTMyWindow cl )
  123.   { return is >> (RPvoid)cl; }
  124.  
  125. inline Ropstream operator << ( Ropstream os, RTMyWindow cl )
  126.   { return os << (RTStreamable)cl; }
  127.  
  128. inline Ropstream operator << ( Ropstream os, PTMyWindow cl )
  129.   { return os << (PTStreamable)cl; }
  130.  
  131. TMyWindow::TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  132.   : TWindow(AParent, ATitle)
  133. {
  134.   AssignMenu("COMMANDS");
  135.   ButtonDown = FALSE;
  136.   PenSize = 1;
  137.   ThePen = CreatePen(PS_SOLID, PenSize, 0);
  138.   Points = new TPointArray(50, 0, 50);
  139.   IsNewFile = TRUE;
  140.   IsDirty = FALSE;
  141. }
  142.  
  143. TMyWindow::~TMyWindow()
  144. {
  145.   delete Points;
  146.   DeleteObject(ThePen);
  147. }
  148.  
  149. void TMyWindow::SetPenSize(int NewSize)
  150. {
  151.   DeleteObject(ThePen);
  152.   ThePen = CreatePen(PS_SOLID, NewSize, 0);
  153.   PenSize = NewSize;
  154. }
  155.  
  156. void TMyWindow::Paint(HDC DC, PAINTSTRUCT&)
  157. {
  158.   RArrayIterator PointIterator = (RArrayIterator)(Points->initIterator());
  159.   BOOL First = TRUE;
  160.  
  161.   SelectObject(DC, ThePen);
  162.   while ( int(PointIterator) != 0 )
  163.   {
  164.     RObject AnObject = PointIterator++;
  165.     if ( AnObject != NOOBJECT )
  166.     {
  167.       if ( First )
  168.       {
  169.         MoveTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  170.         First = FALSE;
  171.       }
  172.       else
  173.         LineTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  174.     }   
  175.   }
  176.   delete &PointIterator;
  177. }
  178.  
  179. BOOL TMyWindow::CanClose()
  180. {
  181.   if ( !IsDirty )
  182.     return TRUE;
  183.   return MessageBox(HWindow, "Do you want to save?",
  184.     "Drawing has changed", MB_YESNO | MB_ICONQUESTION) == IDNO;
  185. }
  186.  
  187. void TMyWindow::WMLButtonDown(RTMessage Msg)
  188. {
  189.   Points->DeleteAll();
  190.   InvalidateRect(HWindow, NULL, TRUE);
  191.   if ( !ButtonDown )
  192.   {
  193.     ButtonDown = TRUE;
  194.     SetCapture(HWindow);
  195.     DragDC = GetDC(HWindow);
  196.     SelectObject(DragDC, ThePen);
  197.     MoveTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  198.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  199.   }
  200. }
  201.  
  202. void TMyWindow::WMMouseMove(RTMessage Msg)
  203. {
  204.   if ( ButtonDown )
  205.   {
  206.     LineTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  207.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  208.   }
  209. }
  210.  
  211. void TMyWindow::WMLButtonUp(RTMessage)
  212. {
  213.   if ( ButtonDown )
  214.   {
  215.     ButtonDown = FALSE;
  216.     ReleaseCapture();
  217.     ReleaseDC(HWindow, DragDC);
  218.   }
  219. }
  220.  
  221. void TMyWindow::WMRButtonDown(RTMessage)
  222. {
  223.   char InputText[6];
  224.   int NewPenSize;
  225.  
  226.   sprintf(InputText, "%d", PenSize);
  227.   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Line Thickness",
  228.     "Input a new thickness:", InputText, sizeof InputText)) == IDOK )
  229.   {
  230.       NewPenSize = atoi(InputText);
  231.       if ( NewPenSize < 0 )
  232.         NewPenSize = 1;
  233.       SetPenSize(NewPenSize);
  234.   }
  235. }
  236.  
  237. void TMyWindow::CMFileNew(RTMessage)
  238.   Points->DeleteAll();
  239.   InvalidateRect(HWindow, NULL, TRUE);
  240.   IsDirty = FALSE;
  241.   IsNewFile = TRUE;
  242. }
  243.  
  244. void TMyWindow::CMFileOpen(RTMessage)
  245.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILEOPEN,
  246.     strcpy(FileName, "*.PTS"))) == IDOK )
  247.       OpenFile();
  248. }
  249.  
  250. void TMyWindow::CMFileSave(RTMessage)
  251.   if ( IsNewFile )
  252.     SaveFileAs();
  253.   else SaveFile();
  254. }
  255.  
  256. void TMyWindow::SaveFileAs()
  257.   if ( IsNewFile ) 
  258.     strcpy(FileName, "");
  259.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILESAVE,
  260.     strcpy(FileName, "*.PTS"))) == IDOK )
  261.       SaveFile();
  262. }
  263.  
  264. void TMyWindow::CMFileSaveAs(RTMessage)
  265.   SaveFileAs();
  266. }
  267.  
  268. void TMyWindow::SaveFile()
  269.    ofpstream os(FileName);
  270.  
  271.    os << Points;
  272.    os.close();
  273.    IsNewFile = IsDirty = FALSE;
  274. }
  275.  
  276. void TMyWindow::OpenFile()
  277. {
  278.    ifpstream is(FileName);
  279.    if ( is.bad() )
  280.      MessageBox(HWindow, "Unable to open file", "File Error", 
  281.        MB_OK | MB_ICONEXCLAMATION);
  282.    else
  283.    {
  284.      Points->DeleteAll();
  285.      is >> Points;
  286.      is.close();
  287.      IsNewFile = IsDirty = FALSE;
  288.      InvalidateRect(HWindow, NULL, 1);
  289.    }
  290. }
  291.  
  292.  
  293. void TMyWindow::CMHelp(RTMessage)
  294.   PTWindow HelpWindow;
  295.   
  296.   HelpWindow = new THelpWindow(this);
  297.   HelpWindow->Attr.Style |= WS_POPUPWINDOW | WS_CAPTION;
  298.   HelpWindow->Attr.X = 100;
  299.   HelpWindow->Attr.Y = 100;
  300.   HelpWindow->Attr.W = 300;
  301.   HelpWindow->Attr.H = 300;
  302.   GetApplication()->MakeWindow(HelpWindow);
  303. }
  304.  
  305. Pvoid TMyWindow::read(Ripstream is)
  306. {
  307.   TWindow::read(is);
  308.   is >> Points;
  309.   return this;
  310. }
  311.  
  312. void TMyWindow::write(Ropstream os)
  313. {
  314.   TWindow::write(os);
  315.   os << Points;
  316. }
  317.  
  318. PTStreamable TMyWindow::build()
  319. {
  320.   return new TMyWindow(streamableInit);
  321. }
  322.  
  323. TStreamableClass RegMyWindow("TMyWindow", TMyWindow::build, __DELTA(TMyWindow));
  324.  
  325. void TPointArray::DeleteAll()
  326. {
  327.   RArrayIterator PointIterator = (RArrayIterator)initIterator();
  328.  
  329.   while ( int(PointIterator) != 0 )
  330.   {
  331.     RObject AnObject = PointIterator++;
  332.     if ( AnObject != NOOBJECT )
  333.       detach(AnObject, 1);  // detach and delete
  334.   }
  335.   delete &PointIterator;
  336. }
  337.  
  338. Pvoid TPointArray::read(Ripstream is)
  339. {
  340.   sizeType NumPoints;
  341.   PTPoint APoint;
  342.  
  343.   is >> NumPoints;
  344.   for ( int i = 0; i < NumPoints; ++i )
  345.   {
  346.      APoint = new TPoint(0, 0);
  347.      is >> APoint->X;
  348.      is >> APoint->Y;
  349.      add(* (APoint));
  350.   };
  351.   return this;
  352. }
  353.  
  354. void TPointArray::write(Ropstream os)
  355. {
  356.   RContainerIterator PointIterator = initIterator();
  357.  
  358.   os << getItemsInContainer();
  359.   while ( int(PointIterator) != 0 )
  360.   {
  361.     RObject PointObject = PointIterator++;
  362.     if ( PointObject != NOOBJECT )
  363.     {
  364.       os << ((PTPoint)(&PointObject))->X;
  365.       os << ((PTPoint)(&PointObject))->Y;
  366.     }   
  367.   }
  368.   delete &PointIterator;
  369. }
  370.  
  371. PTStreamable TPointArray::build()
  372. {
  373.   return new TPointArray(streamableInit);
  374. }
  375.  
  376. TStreamableClass RegPointArray("TPointArray", TPointArray::build,
  377.   __DELTA(TPointArray));
  378.  
  379. void TMyApp::InitMainWindow()
  380. {
  381.   MainWindow = new TMyWindow(NULL, Name);
  382. }
  383.  
  384. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  385.   LPSTR lpCmdLine, int nCmdShow)
  386. {
  387.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  388.                lpCmdLine, nCmdShow);
  389.   MyApp.Run();
  390.   return MyApp.Status;
  391. }
  392.