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