home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D11 / WDOCDEMO.ZIP / GRAFFITI.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  9.0 KB  |  341 lines

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