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

  1. // ObjectWindows - (C) Copyright 1991 by Borland International
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <array.h>
  6. #include <abstarry.h>
  7. #include <owl.h>
  8. #include <inputdia.h>
  9.  
  10. class TMyApp : public TApplication
  11. {
  12. public:
  13.   TMyApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  14.     LPSTR lpCmdLine, int nCmdShow)
  15.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  16.   virtual void InitMainWindow();
  17. };
  18.  
  19. _CLASSDEF(TPoint)
  20. class TPoint: public Object
  21. {
  22. public:
  23.   int X, Y;
  24.   TPoint(int AX, int AY) {X = AX, Y = AY;}
  25.   virtual classType isA() const { return __firstUserClass; }
  26.   virtual Pchar nameOf() const { return "TPoint"; }
  27.   virtual hashValueType hashValue() const { return 0; } 
  28.   virtual int isEqual(RCObject APoint) const
  29.     { return X == ((RTPoint)APoint).X && Y == ((RTPoint)APoint).Y; }
  30.   virtual void printOn(Rostream outputStream) const
  31.     { outputStream << "(" << X << "," << Y << ")"; }
  32. };
  33.  
  34. _CLASSDEF(TPointArray)
  35. class TPointArray : public Array
  36. {
  37. public:
  38.   TPointArray(int upper, int lower = 0, sizeType aDelta = 0)
  39.     : Array(upper, lower, aDelta){};
  40.   void DeleteAll();
  41.   virtual classType isA() const { return __firstUserClass + 1; }
  42.   virtual Pchar nameOf() const { return "TPointArray"; }
  43. };
  44.  
  45. void TPointArray::DeleteAll()
  46. {
  47.   RArrayIterator PointIterator = (RArrayIterator)initIterator();
  48.  
  49.   while ( int(PointIterator) != 0 )
  50.   {
  51.     RObject AnObject = PointIterator++;
  52.     if ( AnObject != NOOBJECT )
  53.       detach(AnObject, 1);  // detach and delete
  54.   }
  55.   delete &PointIterator;
  56. }
  57.  
  58. _CLASSDEF(TMyWindow)
  59. class TMyWindow : public TWindow
  60. {
  61. public:
  62.   HDC DragDC;
  63.   BOOL ButtonDown;
  64.   HPEN ThePen;
  65.   int PenSize;
  66.   PTPointArray Points;
  67.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle);
  68.   ~TMyWindow();
  69.   virtual BOOL CanClose();
  70.   void SetPenSize(int NewSize);
  71.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  72.   virtual void WMLButtonDown(RTMessage Msg)
  73.     = [WM_FIRST + WM_LBUTTONDOWN];
  74.   virtual void WMLButtonUp(RTMessage Msg)
  75.     = [WM_FIRST + WM_LBUTTONUP];
  76.   virtual void WMMouseMove(RTMessage Msg)
  77.     = [WM_FIRST + WM_MOUSEMOVE];
  78.   virtual void WMRButtonDown(RTMessage Msg)
  79.     = [WM_FIRST + WM_RBUTTONDOWN];
  80. };
  81.  
  82. TMyWindow::TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  83.   : TWindow(AParent, ATitle)
  84. {
  85.   ButtonDown = FALSE;
  86.   PenSize = 1;
  87.   ThePen = CreatePen(PS_SOLID, PenSize, 0);
  88.   Points = new TPointArray(50, 0, 50);
  89. }
  90.  
  91. TMyWindow::~TMyWindow()
  92. {
  93.   delete Points;
  94.   DeleteObject(ThePen);
  95. }
  96.  
  97. BOOL TMyWindow::CanClose()
  98. {
  99.   return MessageBox(HWindow, "Do you want to save?",
  100.     "Drawing has changed", MB_YESNO | MB_ICONQUESTION) == IDNO;
  101. }
  102.  
  103. void TMyWindow::WMLButtonDown(RTMessage Msg)
  104. {
  105.   Points->DeleteAll();
  106.   InvalidateRect(HWindow, NULL, TRUE);
  107.   if ( !ButtonDown )
  108.   {
  109.     ButtonDown = TRUE;
  110.     SetCapture(HWindow);
  111.     DragDC = GetDC(HWindow);
  112.     SelectObject(DragDC, ThePen);
  113.     MoveTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  114.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  115.   }
  116. }
  117.  
  118. void TMyWindow::WMMouseMove(RTMessage Msg)
  119. {
  120.   if ( ButtonDown )
  121.   {
  122.     LineTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  123.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  124.   }
  125. }
  126.  
  127. void TMyWindow::WMLButtonUp(RTMessage)
  128. {
  129.   if ( ButtonDown )
  130.   {
  131.     ButtonDown = FALSE;
  132.     ReleaseCapture();
  133.     ReleaseDC(HWindow, DragDC);
  134.   }
  135. }
  136.  
  137. void TMyWindow::WMRButtonDown(RTMessage)
  138. {
  139.   char InputText[6];
  140.   int NewPenSize;
  141.  
  142.   sprintf(InputText, "%d", PenSize);
  143.   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Line Thickness",
  144.     "Input a new thickness:", InputText, sizeof InputText)) == IDOK )
  145.   {
  146.       NewPenSize = atoi(InputText);
  147.       if ( NewPenSize < 0 )
  148.         NewPenSize = 1;
  149.       SetPenSize(NewPenSize);
  150.   }
  151. }
  152.  
  153. void TMyWindow::SetPenSize(int NewSize)
  154. {
  155.   DeleteObject(ThePen);
  156.   ThePen = CreatePen(PS_SOLID, NewSize, 0);
  157.   PenSize = NewSize;
  158. }
  159.  
  160. void TMyWindow::Paint(HDC DC, PAINTSTRUCT&)
  161. {
  162.   RArrayIterator PointIterator = (RArrayIterator)(Points->initIterator());
  163.   BOOL First = TRUE;
  164.  
  165.   SelectObject(DC, ThePen);
  166.   while ( int(PointIterator) != 0 )
  167.   {
  168.     RObject AnObject = PointIterator++;
  169.     if ( AnObject != NOOBJECT )
  170.     {
  171.       if ( First )
  172.       {
  173.         MoveTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  174.         First = FALSE;
  175.       }
  176.       else
  177.         LineTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  178.     }   
  179.   }
  180.   delete &PointIterator;
  181. }
  182.  
  183. void TMyApp::InitMainWindow()
  184. {
  185.   MainWindow = new TMyWindow(NULL, Name);
  186. }
  187.  
  188. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  189.   LPSTR lpCmdLine, int nCmdShow)
  190. {
  191.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  192.                lpCmdLine, nCmdShow);
  193.   MyApp.Run();
  194.   return MyApp.Status;
  195. }
  196.  
  197.  
  198.