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