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