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