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