home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / STEPS.ZIP / STEP10.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  9.1 KB  |  385 lines

  1. // ObjectWindows - (C) Copyright 1992 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, HINSTANCE hInstance, HINSTANCE 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.   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->flush();
  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. {
  238.   Points->flush();
  239.   InvalidateRect(HWindow, NULL, TRUE);
  240.   IsDirty = FALSE;
  241.   IsNewFile = TRUE;
  242. }
  243.  
  244. void TMyWindow::CMFileOpen(RTMessage)
  245. {
  246.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILEOPEN,
  247.     strcpy(FileName, "*.PTS"))) == IDOK )
  248.       OpenFile();
  249. }
  250.  
  251. void TMyWindow::CMFileSave(RTMessage)
  252. {
  253.   if ( IsNewFile )
  254.     SaveFileAs();
  255.   else SaveFile();
  256. }
  257.  
  258. void TMyWindow::SaveFileAs()
  259. {
  260.   if ( IsNewFile )
  261.     strcpy(FileName, "");
  262.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILESAVE,
  263.     strcpy(FileName, "*.PTS"))) == IDOK )
  264.       SaveFile();
  265. }
  266.  
  267. void TMyWindow::CMFileSaveAs(RTMessage)
  268. {
  269.   SaveFileAs();
  270. }
  271.  
  272. void TMyWindow::SaveFile()
  273. {
  274.    ofpstream os(FileName);
  275.  
  276.    os << Points;
  277.    os.close();
  278.    IsNewFile = IsDirty = FALSE;
  279. }
  280.  
  281. void TMyWindow::OpenFile()
  282. {
  283.    ifpstream is(FileName);
  284.    if ( is.bad() )
  285.      MessageBox(HWindow, "Unable to open file", "File Error",
  286.        MB_OK | MB_ICONEXCLAMATION);
  287.    else
  288.    {
  289.      Points->flush();
  290.      is >> Points;
  291.      is.close();
  292.      IsNewFile = IsDirty = FALSE;
  293.      InvalidateRect(HWindow, NULL, 1);
  294.    }
  295. }
  296.  
  297.  
  298. void TMyWindow::CMHelp(RTMessage)
  299. {
  300.   PTWindow HelpWindow;
  301.  
  302.   HelpWindow = new THelpWindow(this);
  303.   HelpWindow->Attr.Style |= WS_POPUPWINDOW | WS_CAPTION;
  304.   HelpWindow->Attr.X = 100;
  305.   HelpWindow->Attr.Y = 100;
  306.   HelpWindow->Attr.W = 300;
  307.   HelpWindow->Attr.H = 300;
  308.   GetApplication()->MakeWindow(HelpWindow);
  309. }
  310.  
  311. Pvoid TMyWindow::read(Ripstream is)
  312. {
  313.   TWindow::read(is);
  314.   is >> Points;
  315.   return this;
  316. }
  317.  
  318. void TMyWindow::write(Ropstream os)
  319. {
  320.   TWindow::write(os);
  321.   os << Points;
  322. }
  323.  
  324. PTStreamable TMyWindow::build()
  325. {
  326.   return new TMyWindow(streamableInit);
  327. }
  328.  
  329. TStreamableClass RegMyWindow("TMyWindow", TMyWindow::build, __DELTA(TMyWindow));
  330.  
  331. Pvoid TPointArray::read(Ripstream is)
  332. {
  333.   sizeType NumPoints;
  334.   PTPoint APoint;
  335.  
  336.   is >> NumPoints;
  337.   for ( int i = 0; i < NumPoints; ++i )
  338.   {
  339.      APoint = new TPoint(0, 0);
  340.      is >> APoint->X;
  341.      is >> APoint->Y;
  342.      add(* (APoint));
  343.   };
  344.   return this;
  345. }
  346.  
  347. void TPointArray::write(Ropstream os)
  348. {
  349.   RContainerIterator PointIterator = initIterator();
  350.  
  351.   os << getItemsInContainer();
  352.   while ( int(PointIterator) != 0 )
  353.   {
  354.     RObject PointObject = PointIterator++;
  355.     if ( PointObject != NOOBJECT )
  356.     {
  357.       os << ((PTPoint)(&PointObject))->X;
  358.       os << ((PTPoint)(&PointObject))->Y;
  359.     }
  360.   }
  361.   delete &PointIterator;
  362. }
  363.  
  364. PTStreamable TPointArray::build()
  365. {
  366.   return new TPointArray(streamableInit);
  367. }
  368.  
  369. TStreamableClass RegPointArray("TPointArray", TPointArray::build,
  370.   __DELTA(TPointArray));
  371.  
  372. void TMyApp::InitMainWindow()
  373. {
  374.   MainWindow = new TMyWindow(NULL, Name);
  375. }
  376.  
  377. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  378.   LPSTR lpCmdLine, int nCmdShow)
  379. {
  380.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  381.                lpCmdLine, nCmdShow);
  382.   MyApp.Run();
  383.   return MyApp.Status;
  384. }
  385.