home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL3.ZIP / STEPS.ZIP / STEP7.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  5.7 KB  |  234 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. #include "steps.h"
  10.  
  11. class TMyApp : public TApplication
  12. {
  13. public:
  14.   TMyApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  15.     LPSTR lpCmdLine, int nCmdShow)
  16.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  17.   virtual void InitMainWindow();
  18. };
  19.  
  20. _CLASSDEF(TPoint)
  21. class TPoint: public Object
  22. {
  23. public:
  24.   int X, Y;
  25.   TPoint(int AX, int AY) {X = AX, Y = AY;}
  26.   virtual classType isA() const { return __firstUserClass; }
  27.   virtual Pchar nameOf() const { return "TPoint"; }
  28.   virtual hashValueType hashValue() const { return 0; } 
  29.   virtual int isEqual(RCObject APoint) const
  30.     { return X == ((RTPoint)APoint).X && Y == ((RTPoint)APoint).Y; }
  31.   virtual void printOn(Rostream outputStream) const
  32.     { outputStream << "(" << X << "," << Y << ")"; }
  33. };
  34.  
  35. _CLASSDEF(TPointArray)
  36. class TPointArray : public Array
  37. {
  38. public:
  39.   TPointArray(int upper, int lower = 0, sizeType aDelta = 0)
  40.     : Array(upper, lower, aDelta){};
  41.   void DeleteAll();
  42.   virtual classType isA() const { return __firstUserClass + 1; }
  43.   virtual Pchar nameOf() const { return "TPointArray"; }
  44. };
  45.  
  46. void TPointArray::DeleteAll()
  47. {
  48.   RArrayIterator PointIterator = (RArrayIterator)initIterator();
  49.  
  50.   while ( int(PointIterator) != 0 )
  51.   {
  52.     RObject AnObject = PointIterator++;
  53.     if ( AnObject != NOOBJECT )
  54.       detach(AnObject, 1);  // detach and delete
  55.   }
  56.   delete &PointIterator;
  57. }
  58.  
  59. _CLASSDEF(TMyWindow)
  60. class TMyWindow : public TWindow
  61. {
  62. public:
  63.   HDC DragDC;
  64.   BOOL ButtonDown;
  65.   HPEN ThePen;
  66.   int PenSize;
  67.   PTPointArray Points;
  68.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle);
  69.   ~TMyWindow();
  70.   virtual BOOL CanClose();
  71.   void SetPenSize(int NewSize);
  72.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  73.   virtual void WMLButtonDown(RTMessage Msg)
  74.     = [WM_FIRST + WM_LBUTTONDOWN];
  75.   virtual void WMLButtonUp(RTMessage Msg)
  76.     = [WM_FIRST + WM_LBUTTONUP];
  77.   virtual void WMMouseMove(RTMessage Msg)
  78.     = [WM_FIRST + WM_MOUSEMOVE];
  79.   virtual void WMRButtonDown(RTMessage Msg)
  80.     = [WM_FIRST + WM_RBUTTONDOWN];
  81.   virtual void CMFileNew(RTMessage Msg)
  82.     = [CM_FIRST + CM_FILENEW];
  83.   virtual void CMFileOpen(RTMessage Msg)
  84.     = [CM_FIRST + CM_FILEOPEN];
  85.   virtual void CMFileSave(RTMessage Msg)
  86.     = [CM_FIRST + CM_FILESAVE];
  87.   virtual void CMFileSaveAs(RTMessage Msg)
  88.     = [CM_FIRST + CM_FILESAVEAS];
  89.   virtual void CMHelp(RTMessage Msg)
  90.     = [CM_FIRST + CM_HELP];
  91. };
  92.  
  93. TMyWindow::TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  94.   : TWindow(AParent, ATitle)
  95. {
  96.   AssignMenu("COMMANDS");
  97.   ButtonDown = FALSE;
  98.   PenSize = 1;
  99.   ThePen = CreatePen(PS_SOLID, PenSize, 0);
  100.   Points = new TPointArray(50, 0, 50);
  101. }
  102.  
  103. TMyWindow::~TMyWindow()
  104. {
  105.   delete Points;
  106.   DeleteObject(ThePen);
  107. }
  108.  
  109. void TMyWindow::SetPenSize(int NewSize)
  110. {
  111.   DeleteObject(ThePen);
  112.   ThePen = CreatePen(PS_SOLID, NewSize, 0);
  113.   PenSize = NewSize;
  114. }
  115.  
  116. void TMyWindow::Paint(HDC DC, PAINTSTRUCT&)
  117. {
  118.   RArrayIterator PointIterator = (RArrayIterator)(Points->initIterator());
  119.   BOOL First = TRUE;
  120.  
  121.   SelectObject(DC, ThePen);
  122.   while ( int(PointIterator) != 0 )
  123.   {
  124.     RObject AnObject = PointIterator++;
  125.     if ( AnObject != NOOBJECT )
  126.     {
  127.       if ( First )
  128.       {
  129.         MoveTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  130.         First = FALSE;
  131.       }
  132.       else
  133.         LineTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  134.     }   
  135.   }
  136.   delete &PointIterator;
  137. }
  138.  
  139. BOOL TMyWindow::CanClose()
  140. {
  141.   return MessageBox(HWindow, "Do you want to save?",
  142.     "Drawing has changed", MB_YESNO | MB_ICONQUESTION) == IDNO;
  143. }
  144.  
  145. void TMyWindow::WMLButtonDown(RTMessage Msg)
  146. {
  147.   Points->DeleteAll();
  148.   InvalidateRect(HWindow, NULL, TRUE);
  149.   if ( !ButtonDown )
  150.   {
  151.     ButtonDown = TRUE;
  152.     SetCapture(HWindow);
  153.     DragDC = GetDC(HWindow);
  154.     SelectObject(DragDC, ThePen);
  155.     MoveTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  156.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  157.   }
  158. }
  159.  
  160. void TMyWindow::WMMouseMove(RTMessage Msg)
  161. {
  162.   if ( ButtonDown )
  163.   {
  164.     LineTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  165.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  166.   }
  167. }
  168.  
  169. void TMyWindow::WMLButtonUp(RTMessage)
  170. {
  171.   if ( ButtonDown )
  172.   {
  173.     ButtonDown = FALSE;
  174.     ReleaseCapture();
  175.     ReleaseDC(HWindow, DragDC);
  176.   }
  177. }
  178.  
  179. void TMyWindow::WMRButtonDown(RTMessage)
  180. {
  181.   char InputText[6];
  182.   int NewPenSize;
  183.  
  184.   sprintf(InputText, "%d", PenSize);
  185.   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Line Thickness",
  186.     "Input a new thickness:", InputText, sizeof InputText)) == IDOK )
  187.   {
  188.       NewPenSize = atoi(InputText);
  189.       if ( NewPenSize < 0 )
  190.         NewPenSize = 1;
  191.       SetPenSize(NewPenSize);
  192.   }
  193. }
  194.  
  195. void TMyWindow::CMFileNew(RTMessage)
  196.   Points->DeleteAll();
  197.   InvalidateRect(HWindow, NULL, TRUE);
  198. }
  199.  
  200. void TMyWindow::CMFileOpen(RTMessage)
  201.   MessageBox(HWindow, "Feature not implemented", "File Open", MB_OK);
  202. }
  203.  
  204. void TMyWindow::CMFileSave(RTMessage)
  205.   MessageBox(HWindow, "Feature not implemented", "File Save", MB_OK);
  206. }
  207.  
  208. void TMyWindow::CMFileSaveAs(RTMessage)
  209.   MessageBox(HWindow, "Feature not implemented", "File Save As", MB_OK);
  210. }
  211.  
  212. void TMyWindow::CMHelp(RTMessage)
  213.   MessageBox(HWindow, "Feature not implemented", "Help", MB_OK);
  214. }
  215.  
  216. void TMyApp::InitMainWindow()
  217. {
  218.   MainWindow = new TMyWindow(NULL, Name);
  219. }
  220.  
  221. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  222.   LPSTR lpCmdLine, int nCmdShow)
  223. {
  224.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  225.                lpCmdLine, nCmdShow);
  226.   MyApp.Run();
  227.   return MyApp.Status;
  228. }
  229.