home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / STEP12B.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  11.6 KB  |  462 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 Step12b;
  12.  
  13. uses WinDos, Strings, Objects, WinTypes, WinProcs, OWindows, ODialogs,
  14.   OStdDlgs, 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.   MaxPens = 9;
  25.  
  26. type
  27.   PPenPic = ^TPenPic;
  28.   TPenPic = object(TWindow)
  29.     PenSet: PCollection;
  30.     SelectedPen: Integer;
  31.     constructor Init(AParent: PWindowsObject);
  32.     destructor Done; virtual;
  33.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  34.     procedure AddPen(APen: PPen);
  35.     procedure DeletePen;
  36.     procedure SetupWindow; virtual;
  37.     procedure WMLButtonDown(var Msg: TMessage);
  38.       virtual wm_First + wm_LButtonDown;
  39.   private
  40.     UpPic, DownPic: HBitmap;
  41.   end;
  42.  
  43.   PPenPalette = ^TPenPalette;
  44.   TPenPalette = object(TWindow)
  45.     AddBtn, DelBtn: PButton;
  46.     Pens: PPenPic;
  47.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  48.     function CanClose: Boolean; virtual;
  49.     procedure IDAdd(var Msg: TMessage); virtual id_First + id_Add;
  50.     procedure IDDel(var Msg: TMessage); virtual id_First + id_Del;
  51.     procedure Grow;
  52.     procedure Shrink;
  53.     procedure WMNCActivate(var Msg: TMessage);
  54.       virtual wm_First + wm_NCActivate;
  55.   end;
  56.  
  57.   PStepWindow = ^TStepWindow;
  58.   TStepWindow = object(TWindow)
  59.     DragDC: HDC;
  60.     ButtonDown, HasChanged, IsNewFile: Boolean;
  61.     FileName: array[0..fsPathName] of Char;
  62.     Drawing: PCollection;
  63.     CurrentLine: PLine;
  64.     CurrentPen: PPen;
  65.     Printer: PPrinter;
  66.     PenPalette: PPenPalette;
  67.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  68.     destructor Done; virtual;
  69.     function CanClose: Boolean; virtual;
  70.     procedure CMAbout(var Msg: TMessage);
  71.       virtual cm_First + cm_About;
  72.     procedure CMFileNew(var Msg: TMessage);
  73.       virtual cm_First + cm_FileNew;
  74.     procedure CMFileOpen(var Msg: TMessage);
  75.       virtual cm_First + cm_FileOpen;
  76.     procedure CMFileSave(var Msg: TMessage);
  77.       virtual cm_First + cm_FileSave;
  78.     procedure CMFileSaveAs(var Msg: TMessage);
  79.       virtual cm_First + cm_FileSaveAs;
  80.     procedure CMFilePrint(var Msg: TMessage);
  81.       virtual cm_First + cm_FilePrint;
  82.     procedure CMFileSetup(var Msg: TMessage);
  83.       virtual cm_First + cm_FileSetup;
  84.     procedure CMPalShow(var Msg: TMessage);
  85.       virtual cm_First + cm_PalShow;
  86.     procedure CMPalHide(var Msg: TMessage);
  87.       virtual cm_First + cm_PalHide;
  88.     procedure CMPen(var Msg: TMessage);
  89.       virtual cm_First + cm_Pen;
  90.     procedure LoadFile;
  91.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  92.     procedure SaveFile;
  93.     procedure WMLButtonDown(var Msg: TMessage);
  94.       virtual wm_First + wm_LButtonDown;
  95.     procedure WMLButtonUp(var Msg: TMessage);
  96.       virtual wm_First + wm_LButtonUp;
  97.     procedure WMMouseMove(var Msg: TMessage);
  98.       virtual wm_First + wm_MouseMove;
  99.     procedure WMNCActivate(var Msg: TMessage);
  100.       virtual wm_First + wm_NCActivate;
  101.     procedure WMRButtonDown(var Msg: TMessage);
  102.       virtual wm_First + wm_RButtonDown;
  103.   end;
  104.  
  105.   TMyApplication = object(TApplication)
  106.     procedure InitMainWindow; virtual;
  107.   end;
  108.  
  109. procedure StreamRegistration;
  110. begin
  111.   RegisterType(RCollection);
  112. end;
  113.  
  114. constructor TStepWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  115. begin
  116.   inherited Init(AParent, ATitle);
  117.   Attr.Menu := LoadMenu(HInstance, MakeIntResource(100));
  118.   HasChanged := False;
  119.   IsNewFile := True;
  120.   ButtonDown := False;
  121.   StrCopy(FileName, '*.PTS');
  122.   CurrentPen := New(PPen, Init(ps_Solid, 1, 0));
  123.   Drawing := New(PCollection, Init(50, 50));
  124.   CurrentLine := nil;
  125.   Printer := New(PPrinter, Init);
  126.   PenPalette := New(PPenPalette, Init(@Self, 'PenPalette'));
  127.   StreamRegistration;
  128. end;
  129.  
  130. destructor TStepWindow.Done;
  131. begin
  132.   Dispose(CurrentPen, Done);
  133.   Dispose(Drawing, Done);
  134.   inherited Done;
  135. end;
  136.  
  137. function TStepWindow.CanClose: Boolean;
  138. var
  139.   Reply: Integer;
  140. begin
  141.   CanClose := True;
  142.   if HasChanged then
  143.   begin
  144.     Reply := MessageBox(HWindow, 'Do you want to save?',
  145.       'Drawing has changed', mb_YesNo or mb_IconQuestion);
  146.     if Reply = id_Yes then CanClose := False;
  147.   end;
  148. end;
  149.  
  150. procedure TStepWindow.CMAbout(var Msg: TMessage);
  151. begin
  152.   Application^.ExecDialog(New(PDialog, Init(@Self, 'ABOUTBOX')));
  153. end;
  154.  
  155. procedure TStepWindow.CMFileNew(var Msg: TMessage);
  156. begin
  157.   Drawing^.FreeAll;
  158.   InvalidateRect(HWindow, nil, True);
  159.   HasChanged := False;
  160.   IsNewFile := True;
  161. end;
  162.  
  163. procedure TStepWindow.CMFileOpen(var Msg: TMessage);
  164. begin
  165.   if CanClose then
  166.     if Application^.ExecDialog(New(PFileDialog,
  167.       Init(@Self, MakeIntResource(sd_FileOpen), FileName))) = id_OK then
  168.       LoadFile;
  169. end;
  170.  
  171. procedure TStepWindow.CMFileSave(var Msg: TMessage);
  172. begin
  173.   if IsNewFile then CMFileSaveAs(Msg) else SaveFile;
  174. end;
  175.  
  176. procedure TStepWindow.CMFileSaveAs(var Msg: TMessage);
  177. begin
  178.   if IsNewFile then StrCopy(FileName, '*.pts');
  179.   if Application^.ExecDialog(New(PFileDialog,
  180.     Init(@Self, MakeIntResource(sd_FileSave), FileName))) = id_OK then
  181.     SaveFile;
  182. end;
  183.  
  184. procedure TStepWindow.CMFilePrint(var Msg: TMessage);
  185. var
  186.   P: PPrintout;
  187. begin
  188.   if IsNewFile then StrCopy(FileName, 'Untitled');
  189.   P := New(PWindowPrintout, Init(FileName, @Self));
  190.   Printer^.Print(@Self, P);
  191.   Dispose(P, Done);
  192. end;
  193.  
  194. procedure TStepWindow.CMFileSetup(var Msg: TMessage);
  195. begin
  196.   Printer^.Setup(@Self);
  197. end;
  198.  
  199. procedure TStepWindow.CMPalShow(var Msg: TMessage);
  200. begin
  201.   PenPalette^.Show(sw_ShowNA);
  202. end;
  203.  
  204. procedure TStepWindow.CMPalHide(var Msg: TMessage);
  205. begin
  206.   PenPalette^.Show(sw_Hide);
  207. end;
  208.  
  209. procedure TStepWindow.CMPen(var Msg: TMessage);
  210. begin
  211.   CurrentPen^.ChangePen;
  212. end;
  213.  
  214. procedure TStepWindow.LoadFile;
  215. var
  216.   TempColl: PCollection;
  217.   TheFile: TDosStream;
  218. begin
  219.   TheFile.Init(FileName, stOpen);
  220.   TempColl := PCollection(TheFile.Get);
  221.   TheFile.Done;
  222.   if TempColl <> nil then
  223.   begin
  224.     Dispose(Drawing, Done);
  225.     Drawing := TempColl;
  226.     InvalidateRect(HWindow, nil, True);
  227.   end;
  228.   HasChanged := False;
  229.   IsNewFile := False;
  230. end;
  231.  
  232. procedure TStepWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  233.  
  234.   procedure DrawLine(P: PLine); far;
  235.   begin
  236.     P^.Draw(PaintDC);
  237.   end;
  238.  
  239. begin
  240.   Drawing^.ForEach(@DrawLine);
  241. end;
  242.  
  243. procedure TStepWindow.SaveFile;
  244. var
  245.   TheFile: TDosStream;
  246. begin
  247.   TheFile.Init(FileName, stCreate);
  248.   TheFile.Put(Drawing);
  249.   TheFile.Done;
  250.   IsNewFile := False;
  251.   HasChanged := False;
  252. end;
  253.  
  254. procedure TStepWindow.WMLButtonDown(var Msg: TMessage);
  255. begin
  256.   if not ButtonDown then
  257.   begin
  258.     ButtonDown := True;
  259.     SetCapture(HWindow);
  260.     DragDC := GetDC(HWindow);
  261.     CurrentPen^.Select(DragDC);
  262.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  263.     CurrentLine := New(PLine, Init(CurrentPen));
  264.     Drawing^.Insert(CurrentLine);
  265.     HasChanged := True;
  266.   end;
  267. end;
  268.  
  269. procedure TStepWindow.WMLButtonUp(var Msg: TMessage);
  270. begin
  271.   if ButtonDown then
  272.   begin
  273.     ButtonDown := False;
  274.     ReleaseCapture;
  275.     CurrentPen^.Delete;
  276.     ReleaseDC(HWindow, DragDC);
  277.   end;
  278. end;
  279.  
  280. procedure TStepWindow.WMMouseMove(var Msg: TMessage);
  281. begin
  282.   if ButtonDown then
  283.   begin
  284.     LineTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  285.     CurrentLine^.AddPoint(Msg.LParamLo, Msg.LParamHi);
  286.   end;
  287. end;
  288.  
  289. procedure TStepWindow.WMNCActivate(var Msg: TMessage);
  290. begin
  291.   if Msg.WParam = 0 then Msg.WParam := 1;
  292.   DefWndProc(Msg);
  293. end;
  294.  
  295. procedure TStepWindow.WMRButtonDown(var Msg: TMessage);
  296. begin
  297.   if not ButtonDown then CurrentPen^.ChangePen;
  298. end;
  299.  
  300. procedure TMyApplication.InitMainWindow;
  301. begin
  302.   MainWindow := New(PStepWindow, Init(nil, 'Steps'));
  303. end;
  304.  
  305. constructor TPenPalette.Init(AParent: PWindowsObject; ATitle: PChar);
  306. begin
  307.   inherited Init(AParent, ATitle);
  308.   with Attr do
  309.   begin
  310.     Style := Style or ws_Tiled or ws_SysMenu or ws_Visible;
  311.     Y := sw_ShowNA;
  312.     W := 132;
  313.     H := GetSystemMetrics(sm_CYCaption) + 42;
  314.   end;
  315.   AddBtn := New(PButton, Init(@Self, id_Add, 'Add Pen', 0, 0, 65, 40, True));
  316.   DelBtn := New(PButton, Init(@Self, id_Del, 'Del Pen', 65, 0, 65, 40, False));
  317.   Pens := New(PPenPic, Init(@Self));
  318. end;
  319.  
  320. function TPenPalette.CanClose: Boolean;
  321. begin
  322.   Show(sw_Hide);
  323.   CanClose := False;
  324. end;
  325.  
  326. procedure TPenPalette.IDAdd(var Msg: TMessage);
  327. begin
  328.   Pens^.AddPen(PStepWindow(Parent)^.CurrentPen);
  329. end;
  330.  
  331. procedure TPenPalette.IDDel(var Msg: TMessage);
  332. begin
  333.   Pens^.DeletePen;
  334. end;
  335.  
  336. procedure TPenPalette.Grow;
  337. var
  338.   WindowRect: TRect;
  339. begin
  340.   GetWindowRect(HWindow, WindowRect);
  341.   with WindowRect do
  342.     MoveWindow(HWindow, left, top, right - left,
  343.       bottom - top + 40, True);
  344. end;
  345.  
  346. procedure TPenPalette.Shrink;
  347. var
  348.   WindowRect: TRect;
  349. begin
  350.   GetWindowRect(HWindow, WindowRect);
  351.   with WindowRect do
  352.     MoveWindow(HWindow, left, top, right - left,
  353.       bottom - top - 40, True);
  354. end;
  355.  
  356. procedure TPenPalette.WMNCActivate(var Msg: TMessage);
  357. begin
  358.   if Msg.WParam = 0 then Msg.WParam := 1;
  359.   DefWndProc(Msg);
  360. end;
  361.  
  362. constructor TPenPic.Init(AParent: PWindowsObject);
  363. begin
  364.   TWindow.Init(AParent, nil);
  365.   Attr.Style := ws_Child or ws_Visible;
  366.   PenSet := New(PCollection, Init(MaxPens, 0));
  367.   SelectedPen := -1;
  368.   UpPic := LoadBitmap(HInstance, 'PAL_UP');
  369.   DownPic := LoadBitmap(HInstance, 'PAL_DOWN');
  370. end;
  371.  
  372. destructor TPenPic.Done;
  373. begin
  374.   DeleteObject(UpPic);
  375.   DeleteObject(DownPic);
  376.   Dispose(PenSet, Done);
  377.   inherited Done;
  378. end;
  379.  
  380. procedure TPenPic.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  381. var
  382.   PenCount: Integer;
  383.  
  384.   procedure ShowPen(P: PPen); far;
  385.   var
  386.     MemDC: HDC;
  387.     TheBitmap: HBitmap;
  388.   begin
  389.     MemDC := CreateCompatibleDC(PaintDC);
  390.     Inc(PenCount);
  391.     if PenCount = SelectedPen then TheBitmap := DownPic
  392.     else TheBitmap := UpPic;
  393.     SelectObject(MemDC, TheBitmap);
  394.     BitBlt(PaintDC, 0, PenCount * 40, 128, 40, MemDC, 0, 0, SrcCopy);
  395.     DeleteDC(MemDC);
  396.  
  397.     P^.Select(PaintDC);
  398.     MoveTo(PaintDC, 15, PenCount * 40 + 20);
  399.     LineTo(PaintDC, 115, PenCount * 40 + 20);
  400.     P^.Delete;
  401.   end;
  402.  
  403. begin
  404.   PenCount := -1;
  405.   PenSet^.ForEach(@ShowPen);
  406. end;
  407.  
  408. procedure TPenPic.AddPen(APen: PPen);
  409. begin
  410.   SelectedPen := PenSet^.Count;
  411.   with APen^ do PenSet^.Insert(New(PPen, Init(Style, Width, Color)));
  412.   with PPenPalette(Parent)^ do
  413.   begin
  414.     DelBtn^.Enable;
  415.     if PenSet^.Count >= MaxPens then AddBtn^.Disable;
  416.     Grow;
  417.   end;
  418. end;
  419.  
  420. procedure TPenPic.DeletePen;
  421. begin
  422.   if SelectedPen > -1 then
  423.   begin
  424.     PenSet^.AtFree(SelectedPen);
  425.     PenSet^.Pack;
  426.     SelectedPen := -1;
  427.     with PPenPalette(Parent)^ do
  428.     begin
  429.       AddBtn^.Enable;
  430.       DelBtn^.Disable;
  431.       Shrink;
  432.     end;
  433.   end;
  434. end;
  435.  
  436. procedure TPenPic.SetupWindow;
  437. var
  438.   ClientRect: TRect;
  439. begin
  440.   inherited SetupWindow;
  441.   GetClientRect(Parent^.HWindow, ClientRect);
  442.   with ClientRect do
  443.     MoveWindow(HWindow, 1, bottom - top + 1, 128, 40 * MaxPens, False);
  444. end;
  445.  
  446. procedure TPenPic.WMLButtonDown(var Msg: TMessage);
  447. begin
  448.   SelectedPen := Msg.LParamHi div 40;
  449.   with PPen(PenSet^.At(SelectedPen))^ do
  450.     PStepWindow(Parent^.Parent)^.CurrentPen^.SetAttributes(Style, Width, Color);
  451.   PPenPalette(Parent)^.DelBtn^.Enable;
  452.   InvalidateRect(HWindow, nil, False);
  453. end;
  454.  
  455. var
  456.   MyApp: TMyApplication;
  457.  
  458. begin
  459.   MyApp.Init('Steps');
  460.   MyApp.Run;
  461.   MyApp.Done;
  462. end.