home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / GRAFFITI.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  9.1 KB  |  344 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 Graffiti;
  12.  
  13. {$M 8192, 16384}
  14.  
  15. uses Strings, WinTypes, WinProcs, WinDos, Objects, OWindows, ODialogs,
  16.   OStdDlgs, PenPal, GrafLine, Pen, OPrinter, BWCC;
  17.  
  18. {$R GRAFFITI.RES}
  19.  
  20. {$I GRAFFITI.INC}
  21.  
  22. type
  23.   TMyApplication = object(TApplication)
  24.     procedure InitMainWindow; virtual;
  25.   end;
  26.  
  27.   PGrafWindow = ^TGrafWindow;
  28.   TGrafWindow = object(TMDIWindow)
  29.     constructor Init(ATitle: PChar; AMenu: HMenu);
  30.     procedure CMAbout(var Msg: TMessage);
  31.       virtual cm_First + cm_About;
  32.     function InitChild: PWindowsObject; virtual;
  33.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  34.   end;
  35.  
  36.   PStepWindow = ^TStepWindow;
  37.   TStepWindow = object(TWindow)
  38.     DragDC: HDC;
  39.     ButtonDown: Boolean;
  40.     FileName: array[0..fsPathName] of Char;
  41.     HasChanged, IsNewFile: Boolean;
  42.     Drawing: PCollection;
  43.     CurrentLine: PLine;
  44.     ThePen: PPen;
  45.     PenPalette: PPenPalette;
  46.     Printer: PPrinter;
  47.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  48.     destructor Done; virtual;
  49.     function CanClose: Boolean; virtual;
  50.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  51.     procedure WMLButtonDown(var Msg: TMessage);
  52.       virtual wm_First + wm_LButtonDown;
  53.     procedure WMLButtonUp(var Msg: TMessage);
  54.       virtual wm_First + wm_LButtonUp;
  55.     procedure WMMouseMove(var Msg: TMessage);
  56.       virtual wm_First + wm_MouseMove;
  57.     procedure WMRButtonDown(var Msg: TMessage);
  58.       virtual wm_First + wm_RButtonDown;
  59.     procedure WMMDIActivate(var Msg: TMessage);
  60.       virtual wm_First + wm_MDIActivate;
  61.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  62.     procedure FileNew(var Msg: TMessage);
  63.       virtual cm_First + cm_New;
  64.     procedure FileOpen(var Msg: TMessage);
  65.       virtual cm_First + cm_Open;
  66.     procedure FileSave(var Msg: TMessage);
  67.       virtual cm_First + cm_Save;
  68.     procedure FileSaveAs(var Msg: TMessage);
  69.       virtual cm_First + cm_SaveAs;
  70.     procedure LoadFile;
  71.     procedure SaveFile;
  72.     procedure CMPen(var Msg: TMessage);
  73.       virtual cm_First + cm_Pen;
  74.     procedure CMPrint(var Msg: TMessage);
  75.       virtual cm_First + cm_Print;
  76.     procedure CMSetup(var Msg: TMessage);
  77.       virtual cm_First + cm_Setup;
  78.     procedure CMShowPal(var Msg: TMessage);
  79.       virtual cm_First + cm_ShowPal;
  80.     procedure CMHidePal(var Msg: TMessage);
  81.       virtual cm_First + cm_HidePal;
  82.     procedure CMUndo(var Msg: TMessage);
  83.       virtual cm_First + cm_Undo;
  84.   end;
  85.  
  86. procedure StreamRegistration;
  87. begin
  88.   RegisterType(RCollection);
  89. end;
  90.  
  91. {--------------------------------------------------}
  92. { TStepWindow's method implementations:            }
  93. {--------------------------------------------------}
  94.  
  95. constructor TStepWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  96. begin
  97.   inherited Init(AParent, ATitle);
  98.   EnableAutoCreate;
  99.   Attr.Menu := LoadMenu(HInstance, MakeIntResource(100));
  100.   ButtonDown := False;
  101.   ThePen := New(PPen, Init(ps_Solid, 1, RGB(0, 0, 0)));
  102.   Drawing := New(PCollection, Init(50, 50));
  103.   HasChanged := False;
  104.   IsNewFile := True;
  105.   PenPalette := New(PPenPalette, Init(@Self, 'Pen Palette', ThePen));
  106.   Printer := New(PPrinter, Init);
  107.   Scroller := New(PScroller, Init(@Self, 10, 10, 640, 480));
  108.   with Scroller^ do
  109.   begin
  110.     HasHScrollBar := True;
  111.     HasVScrollBar := True;
  112.   end;
  113. end;
  114.  
  115. destructor TStepWindow.Done;
  116. begin
  117.   Dispose(Drawing, Done);
  118.   Dispose(ThePen, Done);
  119.   inherited Done;
  120. end;
  121.  
  122. function TStepWindow.CanClose: Boolean;
  123. var
  124.   Reply: Integer;
  125. begin
  126.   CanClose := True;
  127.   if HasChanged then
  128.   begin
  129.     Reply := MessageBox(HWindow, 'Do you want to save?',
  130.       'Drawing has changed', mb_YesNo or mb_IconQuestion);
  131.     if Reply = id_Yes then CanClose := False;
  132.   end;
  133. end;
  134.  
  135. procedure TStepWindow.GetWindowClass(var AWndClass: TWndClass);
  136. begin
  137.   inherited GetWindowClass(AWndClass);
  138.   AWndClass.hIcon := LoadIcon(HInstance, 'STEPICON');
  139. end;
  140.  
  141. procedure TStepWindow.WMLButtonDown(var Msg: TMessage);
  142. begin
  143.   if not ButtonDown then
  144.   begin
  145.     HasChanged := True;
  146.     ButtonDown := True;
  147.     SetCapture(HWindow);
  148.     DragDC := GetDC(HWindow);
  149.     ThePen^.Select(DragDC);
  150.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  151.     CurrentLine := New(PLine, Init(ThePen));
  152.     Drawing^.Insert(CurrentLine);
  153.   end;
  154.   inherited WMLButtonDown(Msg);
  155. end;
  156.  
  157. procedure TStepWindow.WMMouseMove(var Msg: TMessage);
  158. begin
  159.   if ButtonDown then
  160.   begin
  161.     LineTo(DragDC, Integer(Msg.LParamLo), Integer(Msg.LParamHi));
  162.     CurrentLine^.AddPoint(Scroller^.XPos + Msg.LParamLo, Scroller^.YPos + Msg.LParamHi);
  163.   end;
  164. end;
  165.  
  166. procedure TStepWindow.WMLButtonUp(var Msg: TMessage);
  167. begin
  168.   if ButtonDown then
  169.   begin
  170.     CurrentLine^.AddPoint(Scroller^.XPos + Msg.LParamLo, Scroller^.YPos + Msg.LParamHi);
  171.     ButtonDown := False;
  172.     ReleaseCapture;
  173.     ReleaseDC(HWindow, DragDC);
  174.   end;
  175. end;
  176.  
  177. procedure TStepWindow.WMRButtonDown(var Msg: TMessage);
  178. begin
  179.   ThePen^.ChangePen;
  180. end;
  181.  
  182. procedure TStepWindow.WMMDIActivate(var Msg: TMessage);
  183. begin
  184.   if Msg.wParam = 0 then PenPalette^.Show(sw_Hide)
  185.   else PenPalette^.Show(sw_ShowNA);
  186. end;
  187.  
  188. procedure TStepWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  189.  
  190.   procedure DrawIt(P: PLine); far;
  191.   begin
  192.     P^.Draw(PaintDC);
  193.   end;
  194.  
  195. begin
  196.  Drawing^.ForEach(@DrawIt);
  197. end;
  198.  
  199. procedure TStepWindow.CMPen(var Msg: TMessage);
  200. begin
  201.   ThePen^.ChangePen;
  202. end;
  203.  
  204. procedure TStepWindow.FileNew(var Msg: TMessage);
  205. begin
  206.   Drawing^.FreeAll;
  207.   InvalidateRect(HWindow, nil, True);
  208.   HasChanged := False;
  209.   IsNewFile := True;
  210. end;
  211.  
  212. procedure TStepWindow.FileOpen(var Msg: TMessage);
  213. begin
  214.   if CanClose then
  215.     if Application^.ExecDialog(New(PFileDialog,
  216.         Init(@Self, MakeIntResource(sd_FileOpen),
  217.         StrCopy(FileName,'*.PTS')))) = id_Ok then
  218.       LoadFile;
  219. end;
  220.  
  221. procedure TStepWindow.FileSave(var Msg: TMessage);
  222. begin
  223.   if IsNewFile then FileSaveAs(Msg) else SaveFile;
  224. end;
  225.  
  226. procedure TStepWindow.FileSaveAs(var Msg: TMessage);
  227. var
  228.   FileDlg: PFileDialog;
  229. begin
  230.   if IsNewFile then StrCopy(FileName, '');
  231.   if Application^.ExecDialog(New(PFileDialog,
  232.     Init(@Self, MakeIntResource(sd_FileSave), FileName))) = id_Ok then SaveFile;
  233. end;
  234.  
  235. procedure TStepWindow.LoadFile;
  236. var
  237.   TempColl: PCollection;
  238.   TheFile: TDosStream;
  239. begin
  240.   TheFile.Init(FileName, stOpen);
  241.   TempColl := PCollection(TheFile.Get);
  242.   TheFile.Done;
  243.   if TempColl <> nil then
  244.   begin
  245.     Dispose(Drawing, Done);
  246.     Drawing := TempColl;
  247.     InvalidateRect(HWindow, nil, True);
  248.   end;
  249.   HasChanged := False;
  250.   IsNewFile := False;
  251. end;
  252.  
  253. procedure TStepWindow.SaveFile;
  254. var
  255.   TheFile: TDosStream;
  256. begin
  257.   TheFile.Init(FileName, stCreate);
  258.   TheFile.Put(Drawing);
  259.   TheFile.Done;
  260.   IsNewFile := False;
  261.   HasChanged := False;
  262. end;
  263.  
  264. procedure TStepWindow.CMPrint(var Msg: TMessage);
  265. var
  266.   P: PPrintOut;
  267. begin
  268.   if IsNewFile then StrCopy(FileName, 'Untitled');
  269.   P := New(PWindowPrintout, Init(FileName, @Self));
  270.   Printer^.Print(@Self, P);
  271.   Dispose(P, Done);
  272. end;
  273.  
  274. procedure TStepWindow.CMSetup(var Msg: TMessage);
  275. begin
  276.   Printer^.Setup(@Self);
  277. end;
  278.  
  279. procedure TStepWindow.CMShowPal(var Msg: TMessage);
  280. begin
  281.   PenPalette^.Show(sw_ShowNA);
  282. end;
  283.  
  284. procedure TStepWindow.CMHidePal(var Msg: TMessage);
  285. begin
  286.   PenPalette^.Show(sw_Hide);
  287. end;
  288.  
  289. procedure TStepWindow.CMUndo(var Msg: TMessage);
  290. begin
  291.   with Drawing^ do if Count > 0 then AtFree(Count - 1);
  292.   InvalidateRect(HWindow, nil, True);
  293. end;
  294.  
  295. {--------------------------------------------------}
  296. { TGrafWindow's method implementations:            }
  297. {--------------------------------------------------}
  298.  
  299. constructor TGrafWindow.Init(ATitle: PChar; AMenu: HMenu);
  300. begin
  301.   inherited Init(ATitle, AMenu);
  302.   ChildMenuPos := 3;
  303.   StreamRegistration;
  304. end;
  305.  
  306. procedure TGrafWindow.CMAbout(var Msg: TMessage);
  307. begin
  308.   Application^.ExecDialog(New(PDialog, Init(@Self, 'ABOUTBOX')));
  309. end;
  310.  
  311. function TGrafWindow.InitChild: PWindowsObject;
  312. begin
  313.   InitChild := New(PStepWindow, Init(@Self, 'Untitled'));
  314. end;
  315.  
  316. procedure TGrafWindow.GetWindowClass(var AWndClass: TWndClass);
  317. begin
  318.   inherited GetWindowClass(AWndClass);
  319.   AWndClass.hIcon := LoadIcon(HInstance, 'GrafIcon');
  320. end;
  321.  
  322. {--------------------------------------------------}
  323. { TMyApplication's method implementations:         }
  324. {--------------------------------------------------}
  325.  
  326. procedure TMyApplication.InitMainWindow;
  327. begin
  328.   MainWindow := New(PGrafWindow, Init('Graffiti',
  329.     LoadMenu(HInstance, MakeIntResource(100))));
  330. end;
  331.  
  332. {--------------------------------------------------}
  333. { Main program:                                    }
  334. {--------------------------------------------------}
  335.  
  336. var
  337.   MyApp: TMyApplication;
  338.  
  339. begin
  340.   MyApp.Init('Graffiti');
  341.   MyApp.Run;
  342.   MyApp.Done;
  343. end.
  344.