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