home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL3.ZIP / STEPS.ZIP / STEP8.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  6.2 KB  |  250 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
  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. };
  48.  
  49. void TPointArray::DeleteAll()
  50. {
  51.   RArrayIterator PointIterator = (RArrayIterator)initIterator();
  52.  
  53.   while ( int(PointIterator) != 0 )
  54.   {
  55.     RObject AnObject = PointIterator++;
  56.     if ( AnObject != NOOBJECT )
  57.       detach(AnObject, 1);  // detach and delete
  58.   }
  59.   delete &PointIterator;
  60. }
  61.  
  62. _CLASSDEF(TMyWindow)
  63. class TMyWindow : public TWindow
  64. {
  65. public:
  66.   HDC DragDC;
  67.   BOOL ButtonDown;
  68.   HPEN ThePen;
  69.   int PenSize;
  70.   PTPointArray Points;
  71.   char FileName[MAXPATH];
  72.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle);
  73.   ~TMyWindow();
  74.   virtual BOOL CanClose();
  75.   void SetPenSize(int NewSize);
  76.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  77.   virtual void WMLButtonDown(RTMessage Msg)
  78.     = [WM_FIRST + WM_LBUTTONDOWN];
  79.   virtual void WMLButtonUp(RTMessage Msg)
  80.     = [WM_FIRST + WM_LBUTTONUP];
  81.   virtual void WMMouseMove(RTMessage Msg)
  82.     = [WM_FIRST + WM_MOUSEMOVE];
  83.   virtual void WMRButtonDown(RTMessage Msg)
  84.     = [WM_FIRST + WM_RBUTTONDOWN];
  85.   virtual void CMFileNew(RTMessage Msg)
  86.     = [CM_FIRST + CM_FILENEW];
  87.   virtual void CMFileOpen(RTMessage Msg)
  88.     = [CM_FIRST + CM_FILEOPEN];
  89.   virtual void CMFileSave(RTMessage Msg)
  90.     = [CM_FIRST + CM_FILESAVE];
  91.   virtual void CMFileSaveAs(RTMessage Msg)
  92.     = [CM_FIRST + CM_FILESAVEAS];
  93.   virtual void CMHelp(RTMessage Msg)
  94.     = [CM_FIRST + CM_HELP];
  95. };
  96.  
  97. TMyWindow::TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  98.   : TWindow(AParent, ATitle)
  99. {
  100.   AssignMenu("COMMANDS");
  101.   ButtonDown = FALSE;
  102.   PenSize = 1;
  103.   ThePen = CreatePen(PS_SOLID, PenSize, 0);
  104.   Points = new TPointArray(50, 0, 50);
  105. }
  106.  
  107. TMyWindow::~TMyWindow()
  108. {
  109.   delete Points;
  110.   DeleteObject(ThePen);
  111. }
  112.  
  113. void TMyWindow::SetPenSize(int NewSize)
  114. {
  115.   DeleteObject(ThePen);
  116.   ThePen = CreatePen(PS_SOLID, NewSize, 0);
  117.   PenSize = NewSize;
  118. }
  119.  
  120. void TMyWindow::Paint(HDC DC, PAINTSTRUCT&)
  121. {
  122.   RArrayIterator PointIterator = (RArrayIterator)(Points->initIterator());
  123.   BOOL First = TRUE;
  124.  
  125.   SelectObject(DC, ThePen);
  126.   while ( int(PointIterator) != 0 )
  127.   {
  128.     RObject AnObject = PointIterator++;
  129.     if ( AnObject != NOOBJECT )
  130.     {
  131.       if ( First )
  132.       {
  133.         MoveTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  134.         First = FALSE;
  135.       }
  136.       else
  137.         LineTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  138.     }   
  139.   }
  140.   delete &PointIterator;
  141. }
  142.  
  143. BOOL TMyWindow::CanClose()
  144. {
  145.   return MessageBox(HWindow, "Do you want to save?",
  146.     "Drawing has changed", MB_YESNO | MB_ICONQUESTION) == IDNO;
  147. }
  148.  
  149. void TMyWindow::WMLButtonDown(RTMessage Msg)
  150. {
  151.   Points->DeleteAll();
  152.   InvalidateRect(HWindow, NULL, TRUE);
  153.   if ( !ButtonDown )
  154.   {
  155.     ButtonDown = TRUE;
  156.     SetCapture(HWindow);
  157.     DragDC = GetDC(HWindow);
  158.     SelectObject(DragDC, ThePen);
  159.     MoveTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  160.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  161.   }
  162. }
  163.  
  164. void TMyWindow::WMMouseMove(RTMessage Msg)
  165. {
  166.   if ( ButtonDown )
  167.   {
  168.     LineTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  169.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  170.   }
  171. }
  172.  
  173. void TMyWindow::WMLButtonUp(RTMessage)
  174. {
  175.   if ( ButtonDown )
  176.   {
  177.     ButtonDown = FALSE;
  178.     ReleaseCapture();
  179.     ReleaseDC(HWindow, DragDC);
  180.   }
  181. }
  182.  
  183. void TMyWindow::WMRButtonDown(RTMessage)
  184. {
  185.   char InputText[6];
  186.   int NewPenSize;
  187.  
  188.   sprintf(InputText, "%d", PenSize);
  189.   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Line Thickness",
  190.     "Input a new thickness:", InputText, sizeof InputText)) == IDOK )
  191.   {
  192.       NewPenSize = atoi(InputText);
  193.       if ( NewPenSize < 0 )
  194.         NewPenSize = 1;
  195.       SetPenSize(NewPenSize);
  196.   }
  197. }
  198.  
  199. void TMyWindow::CMFileNew(RTMessage)
  200.   Points->DeleteAll();
  201.   InvalidateRect(HWindow, NULL, TRUE);
  202. }
  203.  
  204. void TMyWindow::CMFileOpen(RTMessage)
  205.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILEOPEN,
  206.     strcpy(FileName, "*.PTS"))) == IDOK )
  207.      MessageBox(HWindow, FileName, "Open the file:", MB_OK);
  208. }
  209.  
  210. void TMyWindow::CMFileSave(RTMessage)
  211.   MessageBox(HWindow, "Feature not implemented", "File Save", MB_OK);
  212. }
  213.  
  214. void TMyWindow::CMFileSaveAs(RTMessage)
  215.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILESAVE,
  216.     FileName)) == IDOK )
  217.       MessageBox(HWindow, FileName, "Save the file:", MB_OK);
  218. }
  219.  
  220. void TMyWindow::CMHelp(RTMessage)
  221.   PTWindow HelpWindow;
  222.   
  223.   HelpWindow = new TWindow(this, "Help System");
  224.   HelpWindow->Attr.Style |= WS_POPUPWINDOW | WS_CAPTION;
  225.   HelpWindow->Attr.X = 100;
  226.   HelpWindow->Attr.Y = 100;
  227.   HelpWindow->Attr.W = 300;
  228.   HelpWindow->Attr.H = 300;
  229.   GetApplication()->MakeWindow(HelpWindow);
  230. }
  231.  
  232. void TMyApp::InitMainWindow()
  233. {
  234.   MainWindow = new TMyWindow(NULL, Name);
  235. }
  236.  
  237. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  238.   LPSTR lpCmdLine, int nCmdShow)
  239. {
  240.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  241.                lpCmdLine, nCmdShow);
  242.   MyApp.Run();
  243.   return MyApp.Status;
  244. }
  245.