home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / STREAM2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.9 KB  |  283 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Demo                           }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. { Load and display a collection of graphical objects from a stream:
  9.   ellipses, rectangles and pie slices. This collection was created
  10.   and put on a stream by another program (STREAM1.PAS). }
  11.  
  12. program Stream2;
  13.  
  14. uses
  15.   Objects, WinTypes, WinProcs, Strings, OWindows;
  16.  
  17. const
  18.   em_Stream = 100;
  19.  
  20. { ********************************** }
  21. { ******  Graphical Objects  ******* }
  22. { ********************************** }
  23.  
  24. type
  25.   PGraphObject = ^TGraphObject;
  26.   TGraphObject = object(TObject)
  27.     Rect: TRect;
  28.     constructor Init(Bounds: TRect);
  29.     constructor Load(var S: TStream);
  30.     procedure Draw(DC: HDC); virtual;
  31.     procedure Store(var S: TStream); virtual;
  32.   end;
  33.  
  34.   PGraphEllipse = ^TGraphEllipse;
  35.   TGraphEllipse = object(TGraphObject)
  36.     procedure Draw(DC: HDC); virtual;
  37.   end;
  38.  
  39.   PGraphRect = ^TGraphRect;
  40.   TGraphRect = object(TGraphObject)
  41.     procedure Draw(DC: HDC); virtual;
  42.   end;
  43.  
  44.   PGraphPie = ^TGraphPie;
  45.   TGraphPie = object(TGraphObject)
  46.     ArcStart, ArcEnd: TPoint;
  47.     constructor Init(Bounds: TRect);
  48.     constructor Load(var S: TStream);
  49.     procedure Draw(DC: HDC); virtual;
  50.     procedure Store(var S: TStream); virtual;
  51.   end;
  52.  
  53. { TGraphObject }
  54. constructor TGraphObject.Init(Bounds: TRect);
  55. var
  56.   Height, Width: Word;
  57. begin
  58.   inherited Init;
  59.   with Bounds do
  60.   begin
  61.     Height := Random(Bottom - Top) div 2 + 10;
  62.     Width := Random(Right - Left) div 3 + 15;
  63.   end;
  64.   with Rect do
  65.   begin
  66.     Left := Random(Bounds.Right - Bounds.Left - Width);
  67.     Right := Left + Width;
  68.     Top := Random(Bounds.Bottom - Bounds.Top - Height);
  69.     Bottom := Top + Height;
  70.   end;
  71. end;
  72.  
  73. constructor TGraphObject.Load(var S: TStream);
  74. begin
  75.   S.Read(Rect, SizeOf(Rect));
  76. end;
  77.  
  78. procedure TGraphObject.Draw(DC: HDC);
  79. begin
  80.   Abstract;
  81. end;
  82.  
  83. procedure TGraphObject.Store(var S: TStream);
  84. begin
  85.   S.Write(Rect, SizeOf(Rect));
  86. end;
  87.  
  88. { TGraphEllipse }
  89. procedure TGraphEllipse.Draw(DC: HDC);
  90. begin
  91.   with Rect do
  92.     Ellipse(DC, Left, Top, Right, Bottom);
  93. end;
  94.  
  95. { TGraphRect }
  96. procedure TGraphRect.Draw(DC: HDC);
  97. begin
  98.   with Rect do
  99.     Rectangle(DC, Left, Top, Right, Bottom);
  100. end;
  101.  
  102. { TGraphPie }
  103. constructor TGraphPie.Init(Bounds: TRect);
  104. var Height, Width: Word;
  105. begin
  106.   inherited Init(Bounds);
  107.   with Bounds do
  108.   begin
  109.     Height := Random(Bottom - Top);
  110.     Width := Random(Right - Left);
  111.  
  112.     ArcStart.X := Random(Right - Left - Width);
  113.     ArcEnd.X := ArcStart.X + Width;
  114.     ArcStart.Y := Random(Bottom - Top - Height);
  115.     ArcEnd.Y := ArcStart.Y + Height;
  116.   end;
  117. end;
  118.  
  119. constructor TGraphPie.Load(var S: TStream);
  120. begin
  121.   inherited Load(S);
  122.   S.Read(ArcStart, SizeOf(ArcStart));
  123.   S.Read(ArcEnd, SizeOf(ArcEnd));
  124. end;
  125.  
  126. procedure TGraphPie.Draw;
  127. begin
  128.   with Rect do
  129.     Pie(DC, Left, Top, Right, Bottom,
  130.       ArcStart.X, ArcStart.Y, ArcEnd.X, ArcEnd.Y);
  131. end;
  132.  
  133. procedure TGraphPie.Store(var S: TStream);
  134. begin
  135.   inherited Store(S);
  136.   S.Write(ArcStart, SizeOf(ArcStart));
  137.   S.Write(ArcEnd, SizeOf(ArcEnd));
  138. end;
  139.  
  140.  
  141. { ********************************** }
  142. { **  Stream Registration Records ** }
  143. { ********************************** }
  144.  
  145. const
  146.   RGraphEllipse: TStreamRec = (
  147.     ObjType: 150;
  148.     VmtLink: Ofs(TypeOf(TGraphEllipse)^);
  149.     Load: @TGraphEllipse.Load;
  150.     Store: @TGraphEllipse.Store);
  151.  
  152.   RGraphRect: TStreamRec = (
  153.     ObjType: 151;
  154.     VmtLink: Ofs(TypeOf(TGraphRect)^);
  155.     Load: @TGraphRect.Load;
  156.     Store: @TGraphRect.Store);
  157.  
  158.   RGraphPie: TStreamRec = (
  159.     ObjType: 152;
  160.     VmtLink: Ofs(TypeOf(TGraphPie)^);
  161.     Load: @TGraphPie.Load;
  162.     Store: @TGraphPie.Store);
  163.  
  164. procedure StreamRegistration;
  165. begin
  166.   RegisterType(RCollection);
  167.   RegisterType(RGraphEllipse);
  168.   RegisterType(RGraphRect);
  169.   RegisterType(RGraphPie);
  170. end;
  171.  
  172. { ********************************** }
  173. { *********  Graph Window  ********* }
  174. { ********************************** }
  175. type
  176.   { Define a TApplication descendant }
  177.   TGraphApp = object(TApplication)
  178.     procedure InitMainWindow; virtual;
  179.     procedure Error(ErrorCode: Integer); virtual;
  180.   end;
  181.  
  182.   PGraphWindow = ^TGraphWindow;
  183.   TGraphWindow = object(TWindow)
  184.     GraphicsList: PCollection;
  185.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  186.     destructor Done; virtual;
  187.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  188.     procedure SetupWindow; virtual;
  189.   end;
  190.  
  191.  
  192. { TGraphApp }
  193. procedure TGraphApp.InitMainWindow;
  194. begin
  195.   MainWindow := New(PGraphWindow,
  196.     Init(nil, 'Collection of Graphical Objects'));
  197. end;
  198.  
  199. { TGraphWindow }
  200. constructor TGraphWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  201. begin
  202.   inherited Init(AParent, ATitle);
  203.   GraphicsList := nil;
  204. end;
  205.  
  206. procedure TGraphWindow.SetupWindow;
  207. var
  208.   Bounds: TRect;
  209.   I: Integer;
  210.   P: PGraphObject;
  211.   GraphicsStream: TBufStream;
  212. begin
  213.   inherited SetupWindow;
  214.   StreamRegistration;                        { Register all streams }
  215.  
  216.   { Load collection from stream }
  217.   GraphicsStream.Init('GRAPH.STM', stOpen, 1024);         { Open stream }
  218.   GraphicsList := PCollection(GraphicsStream.Get);        { Load collection }
  219.   if GraphicsStream.Status <> 0 then
  220.     Status := em_Stream;
  221.   GraphicsStream.Done;                                    { Shut down stream }
  222. end;
  223.  
  224. destructor TGraphWindow.Done;
  225. begin
  226.   if GraphicsList <> nil then
  227.     Dispose(GraphicsList, Done);         { Delete collection }
  228.   inherited Done;
  229. end;
  230.  
  231. procedure TGraphWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  232.  
  233. { Nest the iterator method inside Paint so it can access the DC }
  234. procedure DrawAll(C: PCollection); far;
  235.  
  236. { Nested, far procedure. Receives one
  237.   collection element--a GraphObject, and
  238.   calls that elements Draw method.
  239. }
  240.  
  241. procedure CallDraw(P : PGraphObject); far;
  242. begin
  243.   P^.Draw(PaintDC);                            { Call Draw method }
  244. end;
  245.  
  246. begin { DrawAll }
  247.   C^.ForEach(@CallDraw);              { Draw each object }
  248. end;
  249.  
  250. begin
  251.   DrawAll(GraphicsList)
  252. end;
  253.  
  254. procedure TGraphApp.Error(ErrorCode: Integer);
  255. var
  256.   ErrorString: array[0..25] of Char;
  257. begin
  258.   case ErrorCode of
  259.     em_Stream:
  260.       MessageBox(0, 'Error loading GRAPHICS.STM (run STREAM1.PAS first).',
  261.         'Application Error', mb_Ok);
  262.   else
  263.     WVSPrintF(ErrorString, 'Error code = %d', ErrorCode);
  264.     MessageBox(0, ErrorString, 'Application Error', mb_Ok);
  265.   end;
  266. end;
  267.  
  268.  
  269. { ********************************** }
  270. { **********  Main Program ********* }
  271. { ********************************** }
  272.  
  273. { Declare a variable of type TGraphApp }
  274. var
  275.   GraphApp: TGraphApp;
  276.  
  277. { Run the GraphApp }
  278. begin
  279.   GraphApp.Init('GraphApp');
  280.   GraphApp.Run;
  281.   GraphApp.Done;
  282. end.
  283.