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

  1. // ObjectWindows - (C) Copyright 1992 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, HINSTANCE hInstance, HINSTANCE 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.   virtual classType isA() const { return __firstUserClass + 1; }
  42.   virtual Pchar nameOf() const { return "TPointArray"; }
  43. };
  44.  
  45. _CLASSDEF(TMyWindow)
  46. class TMyWindow : public TWindow
  47. {
  48. public:
  49.   HDC DragDC;
  50.   BOOL ButtonDown;
  51.   HPEN ThePen;
  52.   int PenSize;
  53.   PTPointArray Points;
  54.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle);
  55.   ~TMyWindow();
  56.   virtual BOOL CanClose();
  57.   void SetPenSize(int NewSize);
  58.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  59.   virtual void WMLButtonDown(RTMessage Msg)
  60.     = [WM_FIRST + WM_LBUTTONDOWN];
  61.   virtual void WMLButtonUp(RTMessage Msg)
  62.     = [WM_FIRST + WM_LBUTTONUP];
  63.   virtual void WMMouseMove(RTMessage Msg)
  64.     = [WM_FIRST + WM_MOUSEMOVE];
  65.   virtual void WMRButtonDown(RTMessage Msg)
  66.     = [WM_FIRST + WM_RBUTTONDOWN];
  67.   virtual void CMFileNew(RTMessage Msg)
  68.     = [CM_FIRST + CM_FILENEW];
  69.   virtual void CMFileOpen(RTMessage Msg)
  70.     = [CM_FIRST + CM_FILEOPEN];
  71.   virtual void CMFileSave(RTMessage Msg)
  72.     = [CM_FIRST + CM_FILESAVE];
  73.   virtual void CMFileSaveAs(RTMessage Msg)
  74.     = [CM_FIRST + CM_FILESAVEAS];
  75.   virtual void CMHelp(RTMessage Msg)
  76.     = [CM_FIRST + CM_HELP];
  77. };
  78.  
  79. TMyWindow::TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  80.   : TWindow(AParent, ATitle)
  81. {
  82.   AssignMenu("COMMANDS");
  83.   ButtonDown = FALSE;
  84.   PenSize = 1;
  85.   ThePen = CreatePen(PS_SOLID, PenSize, 0);
  86.   Points = new TPointArray(50, 0, 50);
  87. }
  88.  
  89. TMyWindow::~TMyWindow()
  90. {
  91.   delete Points;
  92.   DeleteObject(ThePen);
  93. }
  94.  
  95. void TMyWindow::SetPenSize(int NewSize)
  96. {
  97.   DeleteObject(ThePen);
  98.   ThePen = CreatePen(PS_SOLID, NewSize, 0);
  99.   PenSize = NewSize;
  100. }
  101.  
  102. void TMyWindow::Paint(HDC DC, PAINTSTRUCT&)
  103. {
  104.   RArrayIterator PointIterator = (RArrayIterator)(Points->initIterator());
  105.   BOOL First = TRUE;
  106.  
  107.   SelectObject(DC, ThePen);
  108.   while ( int(PointIterator) != 0 )
  109.   {
  110.     RObject AnObject = PointIterator++;
  111.     if ( AnObject != NOOBJECT )
  112.     {
  113.       if ( First )
  114.       {
  115.         MoveTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  116.         First = FALSE;
  117.       }
  118.       else
  119.         LineTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  120.     }
  121.   }
  122.   delete &PointIterator;
  123. }
  124.  
  125. BOOL TMyWindow::CanClose()
  126. {
  127.   return MessageBox(HWindow, "Do you want to save?",
  128.     "Drawing has changed", MB_YESNO | MB_ICONQUESTION) == IDNO;
  129. }
  130.  
  131. void TMyWindow::WMLButtonDown(RTMessage Msg)
  132. {
  133.   Points->flush();
  134.   InvalidateRect(HWindow, NULL, TRUE);
  135.   if ( !ButtonDown )
  136.   {
  137.     ButtonDown = TRUE;
  138.     SetCapture(HWindow);
  139.     DragDC = GetDC(HWindow);
  140.     SelectObject(DragDC, ThePen);
  141.     MoveTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  142.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  143.   }
  144. }
  145.  
  146. void TMyWindow::WMMouseMove(RTMessage Msg)
  147. {
  148.   if ( ButtonDown )
  149.   {
  150.     LineTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  151.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  152.   }
  153. }
  154.  
  155. void TMyWindow::WMLButtonUp(RTMessage)
  156. {
  157.   if ( ButtonDown )
  158.   {
  159.     ButtonDown = FALSE;
  160.     ReleaseCapture();
  161.     ReleaseDC(HWindow, DragDC);
  162.   }
  163. }
  164.  
  165. void TMyWindow::WMRButtonDown(RTMessage)
  166. {
  167.   char InputText[6];
  168.   int NewPenSize;
  169.  
  170.   sprintf(InputText, "%d", PenSize);
  171.   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Line Thickness",
  172.     "Input a new thickness:", InputText, sizeof InputText)) == IDOK )
  173.   {
  174.       NewPenSize = atoi(InputText);
  175.       if ( NewPenSize < 0 )
  176.         NewPenSize = 1;
  177.       SetPenSize(NewPenSize);
  178.   }
  179. }
  180.  
  181. void TMyWindow::CMFileNew(RTMessage)
  182. {
  183.   Points->flush();
  184.   InvalidateRect(HWindow, NULL, TRUE);
  185. }
  186.  
  187. void TMyWindow::CMFileOpen(RTMessage)
  188. {
  189.   MessageBox(HWindow, "Feature not implemented", "File Open", MB_OK);
  190. }
  191.  
  192. void TMyWindow::CMFileSave(RTMessage)
  193. {
  194.   MessageBox(HWindow, "Feature not implemented", "File Save", MB_OK);
  195. }
  196.  
  197. void TMyWindow::CMFileSaveAs(RTMessage)
  198. {
  199.   MessageBox(HWindow, "Feature not implemented", "File Save As", MB_OK);
  200. }
  201.  
  202. void TMyWindow::CMHelp(RTMessage)
  203. {
  204.   MessageBox(HWindow, "Feature not implemented", "Help", MB_OK);
  205. }
  206.  
  207. void TMyApp::InitMainWindow()
  208. {
  209.   MainWindow = new TMyWindow(NULL, Name);
  210. }
  211.  
  212. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  213.   LPSTR lpCmdLine, int nCmdShow)
  214. {
  215.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  216.                lpCmdLine, nCmdShow);
  217.   MyApp.Run();
  218.   return MyApp.Status;
  219. }
  220.