home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D11 / WDOCDEMO.ZIP / STEP11B.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  8.0 KB  |  315 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 Step11b;
  12.  
  13. uses WinDos, Strings, WinTypes, WinProcs, Objects, OWindows, ODialogs, OStdDlgs,
  14.   Pen, DrawLine, OPrinter, BWCC;
  15.  
  16. {$R STEPS.RES}
  17. {$R PENPAL.RES}
  18.  
  19. {$I STEPS.INC}
  20.  
  21. const
  22.   id_Add = 201;
  23.   id_Del = 202;
  24.  
  25. type
  26.   PPenPalette = ^TPenPalette;
  27.   TPenPalette = object(TWindow)
  28.     AddBtn, DelBtn: PButton;
  29.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  30.     function CanClose: Boolean; virtual;
  31.     procedure WMNCActivate(var Msg: TMessage);
  32.       virtual wm_First + wm_NCActivate;
  33.   end;
  34.  
  35.   PStepWindow = ^TStepWindow;
  36.   TStepWindow = object(TWindow)
  37.     DragDC: HDC;
  38.     ButtonDown, HasChanged, IsNewFile: Boolean;
  39.     FileName: array[0..fsPathName] of Char;
  40.     Drawing: PCollection;
  41.     CurrentLine: PLine;
  42.     CurrentPen: PPen;
  43.     Printer: PPrinter;
  44.     PenPalette: PPenPalette;
  45.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  46.     destructor Done; virtual;
  47.     function CanClose: Boolean; virtual;
  48.     procedure CMAbout(var Msg: TMessage);
  49.       virtual cm_First + cm_About;
  50.     procedure CMFileNew(var Msg: TMessage);
  51.       virtual cm_First + cm_FileNew;
  52.     procedure CMFileOpen(var Msg: TMessage);
  53.       virtual cm_First + cm_FileOpen;
  54.     procedure CMFileSave(var Msg: TMessage);
  55.       virtual cm_First + cm_FileSave;
  56.     procedure CMFileSaveAs(var Msg: TMessage);
  57.       virtual cm_First + cm_FileSaveAs;
  58.     procedure CMFilePrint(var Msg: TMessage);
  59.       virtual cm_First + cm_FilePrint;
  60.     procedure CMFileSetup(var Msg: TMessage);
  61.       virtual cm_First + cm_FileSetup;
  62.     procedure CMPalShow(var Msg: TMessage);
  63.       virtual cm_First + cm_PalShow;
  64.     procedure CMPalHide(var Msg: TMessage);
  65.       virtual cm_First + cm_PalHide;
  66.     procedure CMPen(var Msg: TMessage);
  67.       virtual cm_First + cm_Pen;
  68.     procedure LoadFile;
  69.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  70.     procedure SaveFile;
  71.     procedure WMLButtonDown(var Msg: TMessage);
  72.       virtual wm_First + wm_LButtonDown;
  73.     procedure WMLButtonUp(var Msg: TMessage);
  74.       virtual wm_First + wm_LButtonUp;
  75.     procedure WMMouseMove(var Msg: TMessage);
  76.       virtual wm_First + wm_MouseMove;
  77.     procedure WMNCActivate(var Msg: TMessage);
  78.       virtual wm_First + wm_NCActivate;
  79.     procedure WMRButtonDown(var Msg: TMessage);
  80.       virtual wm_First + wm_RButtonDown;
  81.   end;
  82.  
  83.   TMyApplication = object(TApplication)
  84.     procedure InitMainWindow; virtual;
  85.   end;
  86.  
  87. procedure StreamRegistration;
  88. begin
  89.   RegisterType(RCollection);
  90. end;
  91.  
  92. constructor TStepWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  93. begin
  94.   inherited Init(AParent, ATitle);
  95.   Attr.Menu := LoadMenu(HInstance, MakeIntResource(100));
  96.   HasChanged := False;
  97.   IsNewFile := True;
  98.   ButtonDown := False;
  99.   StrCopy(FileName, '*.PTS');
  100.   CurrentPen := New(PPen, Init(ps_Solid, 1, 0));
  101.   Drawing := New(PCollection, Init(50, 50));
  102.   CurrentLine := nil;
  103.   Printer := New(PPrinter, Init);
  104.   PenPalette := New(PPenPalette, Init(@Self, 'PenPalette'));
  105.   StreamRegistration;
  106. end;
  107.  
  108. destructor TStepWindow.Done;
  109. begin
  110.   Dispose(CurrentPen, Done);
  111.   Dispose(Drawing, DOne);
  112.   inherited Done;
  113. end;
  114.  
  115. function TStepWindow.CanClose: Boolean;
  116. var
  117.   Reply: Integer;
  118. begin
  119.   CanClose := True;
  120.   if HasChanged then
  121.   begin
  122.     Reply := MessageBox(HWindow, 'Do you want to save?',
  123.       'Drawing has changed', mb_YesNo or mb_IconQuestion);
  124.     if Reply = id_Yes then CanClose := False;
  125.   end;
  126. end;
  127.  
  128. procedure TStepWindow.CMAbout(var Msg: TMessage);
  129. begin
  130.   Application^.ExecDialog(New(PDialog, Init(@Self, 'ABOUTBOX')));
  131. end;
  132.  
  133. procedure TStepWindow.CMFileNew(var Msg: TMessage);
  134. begin
  135.   Drawing^.FreeAll;
  136.   InvalidateRect(HWindow, nil, True);
  137.   HasChanged := False;
  138.   IsNewFile := True;
  139. end;
  140.  
  141. procedure TStepWindow.CMFileOpen(var Msg: TMessage);
  142. begin
  143.   if CanClose then
  144.     if Application^.ExecDialog(New(PFileDialog,
  145.       Init(@Self, PChar(sd_FileOpen), FileName))) = id_OK then
  146.       LoadFile;
  147. end;
  148.  
  149. procedure TStepWindow.CMFileSave(var Msg: TMessage);
  150. begin
  151.   if IsNewFile then CMFileSaveAs(Msg) else SaveFile;
  152. end;
  153.  
  154. procedure TStepWindow.CMFileSaveAs(var Msg: TMessage);
  155. begin
  156.   if IsNewFile then StrCopy(FileName, '*.pts');
  157.   if Application^.ExecDialog(New(PFileDialog,
  158.     Init(@Self, PChar(sd_FileSave), FileName))) = id_OK then
  159.     SaveFile;
  160. end;
  161.  
  162. procedure TStepWindow.CMFilePrint(var Msg: TMessage);
  163. var
  164.   P: PPrintout;
  165. begin
  166.   if IsNewFile then StrCopy(FileName, 'Untitled');
  167.   P := New(PWindowPrintout, Init(FileName, @Self));
  168.   Printer^.Print(@Self, P);
  169.   Dispose(P, Done);
  170. end;
  171.  
  172. procedure TStepWindow.CMFileSetup(var Msg: TMessage);
  173. begin
  174.   Printer^.Setup(@Self);
  175. end;
  176.  
  177. procedure TStepWindow.CMPalShow(var Msg: TMessage);
  178. begin
  179.   PenPalette^.Show(sw_ShowNA);
  180. end;
  181.  
  182. procedure TStepWindow.CMPalHide(var Msg: TMessage);
  183. begin
  184.   PenPalette^.Show(sw_Hide);
  185. end;
  186.  
  187. procedure TStepWindow.CMPen(var Msg: TMessage);
  188. begin
  189.   CurrentPen^.ChangePen;
  190. end;
  191.  
  192. procedure TStepWindow.LoadFile;
  193. var
  194.   TempColl: PCollection;
  195.   TheFile: TDosStream;
  196. begin
  197.   TheFile.Init(FileName, stOpen);
  198.   TempColl := PCollection(TheFile.Get);
  199.   TheFile.Done;
  200.   if TempColl <> nil then
  201.   begin
  202.     Dispose(Drawing, Done);
  203.     Drawing := TempColl;
  204.     InvalidateRect(HWindow, nil, True);
  205.   end;
  206.   HasChanged := False;
  207.   IsNewFile := False;
  208. end;
  209.  
  210. procedure TStepWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  211.  
  212.   procedure DrawLine(P: PLine); far;
  213.   begin
  214.     P^.Draw(PaintDC);
  215.   end;
  216.  
  217. begin
  218.   Drawing^.ForEach(@DrawLine);
  219. end;
  220.  
  221. procedure TStepWindow.SaveFile;
  222. var
  223.   TheFile: TDosStream;
  224. begin
  225.   TheFile.Init(FileName, stCreate);
  226.   TheFile.Put(Drawing);
  227.   TheFile.Done;
  228.   IsNewFile := False;
  229.   HasChanged := False;
  230. end;
  231.  
  232. procedure TStepWindow.WMLButtonDown(var Msg: TMessage);
  233. begin
  234.   if not ButtonDown then
  235.   begin
  236.     ButtonDown := True;
  237.     SetCapture(HWindow);
  238.     DragDC := GetDC(HWindow);
  239.     CurrentPen^.Select(DragDC);
  240.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  241.     CurrentLine := New(PLine, Init(CurrentPen));
  242.     Drawing^.Insert(CurrentLine);
  243.     HasChanged := True;
  244.   end;
  245. end;
  246.  
  247. procedure TStepWindow.WMLButtonUp(var Msg: TMessage);
  248. begin
  249.   if ButtonDown then
  250.   begin
  251.     ButtonDown := False;
  252.     ReleaseCapture;
  253.     CurrentPen^.Delete;
  254.     ReleaseDC(HWindow, DragDC);
  255.   end;
  256. end;
  257.  
  258. procedure TStepWindow.WMMouseMove(var Msg: TMessage);
  259. begin
  260.   if ButtonDown then
  261.   begin
  262.     LineTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  263.     CurrentLine^.AddPoint(Msg.LParamLo, Msg.LParamHi);
  264.   end;
  265. end;
  266.  
  267. procedure TStepWindow.WMNCActivate(var Msg: TMessage);
  268. begin
  269.   if Msg.WParam = 0 then Msg.WParam := 1;
  270.   DefWndProc(Msg);
  271. end;
  272.  
  273. procedure TStepWindow.WMRButtonDown(var Msg: TMessage);
  274. begin
  275.   if not ButtonDown then CurrentPen^.ChangePen;
  276. end;
  277.  
  278. procedure TMyApplication.InitMainWindow;
  279. begin
  280.   MainWindow := New(PStepWindow, Init(nil, 'Steps'));
  281. end;
  282.  
  283. constructor TPenPalette.Init(AParent: PWindowsObject; ATitle: PChar);
  284. begin
  285.   inherited Init(AParent, ATitle);
  286.   with Attr do
  287.   begin
  288.     Style := Style or ws_Tiled or ws_SysMenu or ws_Visible;
  289.     W := 132;
  290.     H := GetSystemMetrics(sm_CYCaption) + 42;
  291.   end;
  292.   AddBtn := New(PButton, Init(@Self, id_Add, 'Add Pen', 0, 0, 65, 40, True));
  293.   DelBtn := New(PButton, Init(@Self, id_Del, 'Del Pen', 65, 0, 65, 40, False));
  294. end;
  295.  
  296. function TPenPalette.CanClose: Boolean;
  297. begin
  298.   Show(sw_Hide);
  299.   CanClose := False;
  300. end;
  301.  
  302. procedure TPenPalette.WMNCActivate(var Msg: TMessage);
  303. begin
  304.   if Msg.WParam = 0 then Msg.WParam := 1;
  305.   DefWndProc(Msg);
  306. end;
  307.  
  308. var
  309.   MyApp: TMyApplication;
  310.  
  311. begin
  312.   MyApp.Init('Steps');
  313.   MyApp.Run;
  314.   MyApp.Done;
  315. end.