home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / STEP09.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.3 KB  |  252 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 Step09;
  12.  
  13. uses WinDos, Strings, WinTypes, WinProcs, Objects, OWindows, ODialogs, OStdDlgs,
  14.   Pen, DrawLine, OPrinter;
  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.     FileName: array[0..fsPathName] of Char;
  26.     Drawing: PCollection;
  27.     CurrentLine: PLine;
  28.     CurrentPen: PPen;
  29.     Printer: PPrinter;
  30.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  31.     destructor Done; virtual;
  32.     function CanClose: Boolean; virtual;
  33.     procedure CMAbout(var Msg: TMessage);
  34.       virtual cm_First + cm_About;
  35.     procedure CMFileNew(var Msg: TMessage);
  36.       virtual cm_First + cm_FileNew;
  37.     procedure CMFileOpen(var Msg: TMessage);
  38.       virtual cm_First + cm_FileOpen;
  39.     procedure CMFileSave(var Msg: TMessage);
  40.       virtual cm_First + cm_FileSave;
  41.     procedure CMFileSaveAs(var Msg: TMessage);
  42.       virtual cm_First + cm_FileSaveAs;
  43.     procedure CMFilePrint(var Msg: TMessage);
  44.       virtual cm_First + cm_FilePrint;
  45.     procedure CMFileSetup(var Msg: TMessage);
  46.       virtual cm_First + cm_FileSetup;
  47.     procedure CMPen(var Msg: TMessage);
  48.       virtual cm_First + cm_Pen;
  49.     procedure ReadFile;
  50.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  51.     procedure WriteFile;
  52.     procedure WMLButtonDown(var Msg: TMessage);
  53.       virtual wm_First + wm_LButtonDown;
  54.     procedure WMLButtonUp(var Msg: TMessage);
  55.       virtual wm_First + wm_LButtonUp;
  56.     procedure WMMouseMove(var Msg: TMessage);
  57.       virtual wm_First + wm_MouseMove;
  58.     procedure WMRButtonDown(var Msg: TMessage);
  59.       virtual wm_First + wm_RButtonDown;
  60.   end;
  61.  
  62.   TMyApplication = object(TApplication)
  63.     procedure InitMainWindow; virtual;
  64.   end;
  65.  
  66. procedure StreamRegistration;
  67. begin
  68.   RegisterType(RCollection);
  69. end;
  70.  
  71. constructor TStepWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  72. begin
  73.   inherited Init(AParent, ATitle);
  74.   Attr.Menu := LoadMenu(HInstance, MakeIntResource(100));
  75.   HasChanged := False;
  76.   IsNewFile := True;
  77.   ButtonDown := False;
  78.   StrCopy(FileName, '*.PTS');
  79.   CurrentPen := New(PPen, Init(ps_Solid, 1, 0));
  80.   Drawing := New(PCollection, Init(50, 50));
  81.   CurrentLine := nil;
  82.   Printer := New(PPrinter, Init);
  83.   StreamRegistration;
  84. end;
  85.  
  86. destructor TStepWindow.Done;
  87. begin
  88.   Dispose(CurrentPen, Done);
  89.   Dispose(Drawing, Done);
  90.   inherited Done;
  91. end;
  92.  
  93. function TStepWindow.CanClose: Boolean;
  94. var
  95.   Reply: Integer;
  96. begin
  97.   CanClose := True;
  98.   if HasChanged then
  99.   begin
  100.     Reply := MessageBox(HWindow, 'Do you want to save?',
  101.       'Drawing has changed', mb_YesNo or mb_IconQuestion);
  102.     if Reply = id_Yes then CanClose := False;
  103.   end;
  104. end;
  105.  
  106. procedure TStepWindow.CMAbout(var Msg: TMessage);
  107. begin
  108.   Application^.ExecDialog(New(PDialog, Init(@Self, 'ABOUTBOX')));
  109. end;
  110.  
  111. procedure TStepWindow.CMFileNew(var Msg: TMessage);
  112. begin
  113.   Drawing^.FreeAll;
  114.   InvalidateRect(HWindow, nil, True);
  115.   HasChanged := False;
  116.   IsNewFile := True;
  117. end;
  118.  
  119. procedure TStepWindow.CMFileOpen(var Msg: TMessage);
  120. begin
  121.   if CanClose then
  122.     if Application^.ExecDialog(New(PFileDialog,
  123.       Init(@Self, PChar(sd_FileOpen), FileName))) = id_OK then
  124.       ReadFile;
  125. end;
  126.  
  127. procedure TStepWindow.CMFileSave(var Msg: TMessage);
  128. begin
  129.   if IsNewFile then CMFileSaveAs(Msg) else WriteFile;
  130. end;
  131.  
  132. procedure TStepWindow.CMFileSaveAs(var Msg: TMessage);
  133. begin
  134.   if IsNewFile then StrCopy(FileName, '*.pts');
  135.   if Application^.ExecDialog(New(PFileDialog,
  136.     Init(@Self, PChar(sd_FileSave), FileName))) = id_OK then
  137.     WriteFile;
  138. end;
  139.  
  140. procedure TStepWindow.CMFilePrint(var Msg: TMessage);
  141. var
  142.   P: PPrintout;
  143. begin
  144.   if IsNewFile then StrCopy(FileName, 'Untitled');
  145.   P := New(PWindowPrintout, Init(FileName, @Self));
  146.   Printer^.Print(@Self, P);
  147.   Dispose(P, Done);
  148. end;
  149.  
  150. procedure TStepWindow.CMFileSetup(var Msg: TMessage);
  151. begin
  152.   Printer^.Setup(@Self);
  153. end;
  154.  
  155. procedure TStepWindow.CMPen(var Msg: TMessage);
  156. begin
  157.   CurrentPen^.ChangePen;
  158. end;
  159.  
  160. procedure TStepWindow.ReadFile;
  161. var
  162.   TempColl: PCollection;
  163.   TheFile: TDosStream;
  164. begin
  165.   TheFile.Init(FileName, stOpen);
  166.   TempColl := PCollection(TheFile.Get);
  167.   TheFile.Done;
  168.   if TempColl <> nil then
  169.   begin
  170.     Dispose(Drawing, Done);
  171.     Drawing := TempColl;
  172.     InvalidateRect(HWindow, nil, True);
  173.   end;
  174.   HasChanged := False;
  175.   IsNewFile := False;
  176. end;
  177.  
  178. procedure TStepWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  179.  
  180.   procedure DrawLine(P: PLine); far;
  181.   begin
  182.     P^.Draw(PaintDC);
  183.   end;
  184.  
  185. begin
  186.   Drawing^.ForEach(@DrawLine);
  187. end;
  188.  
  189. procedure TStepWindow.WriteFile;
  190. var
  191.   TheFile: TDosStream;
  192. begin
  193.   TheFile.Init(FileName, stCreate);
  194.   TheFile.Put(Drawing);
  195.   TheFile.Done;
  196.   IsNewFile := False;
  197.   HasChanged := False;
  198. end;
  199.  
  200. procedure TStepWindow.WMLButtonDown(var Msg: TMessage);
  201. begin
  202.   if not ButtonDown then
  203.   begin
  204.     ButtonDown := True;
  205.     SetCapture(HWindow);
  206.     DragDC := GetDC(HWindow);
  207.     CurrentPen^.Select(DragDC);
  208.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  209.     CurrentLine := New(PLine, Init(CurrentPen));
  210.     Drawing^.Insert(CurrentLine);
  211.     HasChanged := True;
  212.   end;
  213. end;
  214.  
  215. procedure TStepWindow.WMLButtonUp(var Msg: TMessage);
  216. begin
  217.   if ButtonDown then
  218.   begin
  219.     ButtonDown := False;
  220.     ReleaseCapture;
  221.     CurrentPen^.Delete;
  222.     ReleaseDC(HWindow, DragDC);
  223.   end;
  224. end;
  225.  
  226. procedure TStepWindow.WMMouseMove(var Msg: TMessage);
  227. begin
  228.   if ButtonDown then
  229.   begin
  230.     LineTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  231.     CurrentLine^.AddPoint(Msg.LParamLo, Msg.LParamHi);
  232.   end;
  233. end;
  234.  
  235. procedure TStepWindow.WMRButtonDown(var Msg: TMessage);
  236. begin
  237.   if not ButtonDown then CurrentPen^.ChangePen;
  238. end;
  239.  
  240. procedure TMyApplication.InitMainWindow;
  241. begin
  242.   MainWindow := New(PStepWindow, Init(nil, 'Steps'));
  243. end;
  244.  
  245. var
  246.   MyApp: TMyApplication;
  247.  
  248. begin
  249.   MyApp.Init('Steps');
  250.   MyApp.Run;
  251.   MyApp.Done;
  252. end.