home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / STEP08B.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.2 KB  |  244 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Demo                           }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. {$R-} { Turn off range check because Windows message parameters
  9.         don't distinguish between Integer and Word. }
  10.  
  11. program Step08b;
  12.  
  13. uses WinDos, Strings, WinTypes, WinProcs, Objects, OWindows, ODialogs, OStdDlgs,
  14.   Pen, DrawLine;
  15.  
  16. {$R STEPS.RES}
  17.  
  18. {$I STEPS.INC}
  19.  
  20. type
  21.   PStepWindow = ^TStepWIndow;
  22.   TStepWindow = object(TWindow)
  23.     DragDC: HDC;
  24.     ButtonDown, HasChanged, IsNewFile: Boolean;
  25.     CurrentPen: PPen;
  26.     FileName: array[0..fsPathName] of Char;
  27.     Drawing: PCollection;
  28.     CurrentLine: PLine;
  29.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  30.     destructor Done; virtual;
  31.     function CanClose: Boolean; virtual;
  32.     procedure CMAbout(var Msg: TMessage);
  33.       virtual cm_First + cm_About;
  34.     procedure CMFileNew(var Msg: TMessage);
  35.       virtual cm_First + cm_FileNew;
  36.     procedure CMFileOpen(var Msg: TMessage);
  37.       virtual cm_First + cm_FileOpen;
  38.     procedure CMFileSave(var Msg: TMessage);
  39.       virtual cm_First + cm_FileSave;
  40.     procedure CMFileSaveAs(var Msg: TMessage);
  41.       virtual cm_First + cm_FileSaveAs;
  42.     procedure CMFilePrint(var Msg: TMessage);
  43.       virtual cm_First + cm_FilePrint;
  44.     procedure CMFileSetup(var Msg: TMessage);
  45.       virtual cm_First + cm_FileSetup;
  46.     procedure CMPen(var Msg: TMessage);
  47.       virtual cm_First + cm_Pen;
  48.     procedure LoadFile;
  49.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  50.     procedure SaveFile;
  51.     procedure WMLButtonDown(var Msg: TMessage);
  52.       virtual wm_First + wm_LButtonDown;
  53.     procedure WMLButtonUp(var Msg: TMessage);
  54.       virtual wm_First + wm_LButtonUp;
  55.     procedure WMMouseMove(var Msg: TMessage);
  56.       virtual wm_First + wm_MouseMove;
  57.     procedure WMRButtonDown(var Msg: TMessage);
  58.       virtual wm_First + wm_RButtonDown;
  59.   end;
  60.   TMyApplication = object(TApplication)
  61.     procedure InitMainWindow; virtual;
  62.   end;
  63.  
  64. procedure StreamRegistration;
  65. begin
  66.   RegisterType(RCollection);
  67. end;
  68.  
  69. constructor TStepWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  70. begin
  71.   inherited Init(AParent, ATitle);
  72.   Attr.Menu := LoadMenu(HInstance, MakeIntResource(100));
  73.   HasChanged := False;
  74.   IsNewFile := True;
  75.   ButtonDown := False;
  76.   StrCopy(FileName, '*.PTS');
  77.   CurrentPen := New(PPen, Init(ps_Solid, 1, 0));
  78.   Drawing := New(PCollection, Init(50, 50));
  79.   CurrentLine := nil;
  80.   StreamRegistration;
  81. end;
  82.  
  83. destructor TStepWindow.Done;
  84. begin
  85.   Dispose(CurrentPen, Done);
  86.   Dispose(Drawing, DOne);
  87.   inherited Done;
  88. end;
  89.  
  90. function TStepWindow.CanClose: Boolean;
  91. var
  92.   Reply: Integer;
  93. begin
  94.   CanClose := True;
  95.   if HasChanged then
  96.   begin
  97.     Reply := MessageBox(HWindow, 'Do you want to save?',
  98.       'Drawing has changed', mb_YesNo or mb_IconQuestion);
  99.     if Reply = id_Yes then CanClose := False;
  100.   end;
  101. end;
  102.  
  103. procedure TStepWindow.CMAbout(var Msg: TMessage);
  104. begin
  105.   Application^.ExecDialog(New(PDialog, Init(@Self, 'ABOUTBOX')));
  106. end;
  107.  
  108. procedure TStepWindow.CMFileNew(var Msg: TMessage);
  109. begin
  110.   Drawing^.FreeAll;
  111.   InvalidateRect(HWindow, nil, True);
  112.   HasChanged := False;
  113.   IsNewFile := True;
  114. end;
  115.  
  116. procedure TStepWindow.CMFileOpen(var Msg: TMessage);
  117. begin
  118.   if CanClose then
  119.     if Application^.ExecDialog(New(PFileDialog,
  120.       Init(@Self, PChar(sd_FileOpen), FileName))) = id_OK then
  121.       LoadFile;
  122. end;
  123.  
  124. procedure TStepWindow.CMFileSave(var Msg: TMessage);
  125. begin
  126.   if IsNewFile then CMFileSaveAs(Msg) else SaveFile;
  127. end;
  128.  
  129. procedure TStepWindow.CMFileSaveAs(var Msg: TMessage);
  130. begin
  131.   if IsNewFile then StrCopy(FileName, '*.pts');
  132.   if Application^.ExecDialog(New(PFileDialog,
  133.     Init(@Self, PChar(sd_FileSave), FileName))) = id_OK then
  134.     SaveFile;
  135. end;
  136.  
  137. procedure TStepWindow.CMFilePrint(var Msg: TMessage);
  138. begin
  139.   MessageBox(HWindow, 'Feature not implemented.', 'File Print', mb_OK);
  140. end;
  141.  
  142. procedure TStepWindow.CMFileSetup(var Msg: TMessage);
  143. begin
  144.   MessageBox(HWindow, 'Feature not implemented.', 'Printer Setup', mb_OK);
  145. end;
  146.  
  147. procedure TStepWindow.CMPen(var Msg: TMessage);
  148. begin
  149.   CurrentPen^.ChangePen;
  150. end;
  151.  
  152. procedure TStepWindow.LoadFile;
  153. var
  154.   TempColl: PCollection;
  155.   TheFile: TDosStream;
  156. begin
  157.   TheFile.Init(FileName, stOpen);
  158.   TempColl := PCollection(TheFile.Get);
  159.   TheFile.Done;
  160.   if TempColl <> nil then
  161.   begin
  162.     Dispose(Drawing, Done);
  163.     Drawing := TempColl;
  164.     InvalidateRect(HWindow, nil, True);
  165.   end;
  166.   HasChanged := False;
  167.   IsNewFile := False;
  168. end;
  169.  
  170. procedure TStepWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  171.  
  172.   procedure DrawLine(P: PLine); far;
  173.   begin
  174.     P^.Draw(PaintDC);
  175.   end;
  176.  
  177. begin
  178.   Drawing^.ForEach(@DrawLine);
  179. end;
  180.  
  181. procedure TStepWindow.SaveFile;
  182. var
  183.   TheFile: TDosStream;
  184. begin
  185.   TheFile.Init(FileName, stCreate);
  186.   TheFile.Put(Drawing);
  187.   TheFile.Done;
  188.   IsNewFile := False;
  189.   HasChanged := False;
  190. end;
  191.  
  192. procedure TStepWindow.WMLButtonDown(var Msg: TMessage);
  193. begin
  194.   if not ButtonDown then
  195.   begin
  196.     ButtonDown := True;
  197.     SetCapture(HWindow);
  198.     DragDC := GetDC(HWindow);
  199.     CurrentPen^.Select(DragDC);
  200.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  201.     CurrentLine := New(PLine, Init(CurrentPen));
  202.     Drawing^.Insert(CurrentLine);
  203.     HasChanged := True;
  204.   end;
  205. end;
  206.  
  207. procedure TStepWindow.WMLButtonUp(var Msg: TMessage);
  208. begin
  209.   if ButtonDown then
  210.   begin
  211.     ButtonDown := False;
  212.     ReleaseCapture;
  213.     CurrentPen^.Delete;
  214.     ReleaseDC(HWindow, DragDC);
  215.   end;
  216. end;
  217.  
  218. procedure TStepWindow.WMMouseMove(var Msg: TMessage);
  219. begin
  220.   if ButtonDown then
  221.   begin
  222.     LineTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  223.     CurrentLine^.AddPoint(Msg.LParamLo, Msg.LParamHi);
  224.   end;
  225. end;
  226.  
  227. procedure TStepWindow.WMRButtonDown(var Msg: TMessage);
  228. begin
  229.   if not ButtonDown then CurrentPen^.ChangePen;
  230. end;
  231.  
  232. procedure TMyApplication.InitMainWindow;
  233. begin
  234.   MainWindow := New(PStepWindow, Init(nil, 'Steps'));
  235. end;
  236.  
  237. var
  238.   MyApp: TMyApplication;
  239.  
  240. begin
  241.   MyApp.Init('Steps');
  242.   MyApp.Run;
  243.   MyApp.Done;
  244. end.