home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DOCDEMOS.ZIP / TVGUID09.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  5.4 KB  |  217 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal 6.0                             }
  4. {   Demo program from the Turbo Vision Guide     }
  5. {                                                }
  6. {   Copyright (c) 1990 by Borland International  }
  7. {                                                }
  8. {************************************************}
  9.  
  10. program TVGUID09;
  11.  
  12. uses Objects, Drivers, Views, Menus, App;
  13.  
  14. const
  15.   FileToRead        = 'TVGUID09.PAS';
  16.   MaxLines          = 100;
  17.   WinCount: Integer =   0;
  18.   cmFileOpen        = 100;
  19.   cmNewWin          = 101;
  20.  
  21. var
  22.   LineCount: Integer;
  23.   Lines: array[0..MaxLines - 1] of PString;
  24.  
  25. type
  26.   TMyApp = object(TApplication)
  27.     procedure HandleEvent(var Event: TEvent); virtual;
  28.     procedure InitMenuBar; virtual;
  29.     procedure InitStatusLine; virtual;
  30.     procedure NewWindow;
  31.   end;
  32.  
  33.   PInterior = ^TInterior;
  34.   TInterior = object(TScroller)
  35.     constructor Init(var Bounds: TRect; AHScrollBar,
  36.       AVScrollBar: PScrollBar);
  37.     procedure Draw; virtual;
  38.   end;
  39.  
  40.   PDemoWindow = ^TDemoWindow;
  41.   TDemoWindow = object(TWindow)
  42.     constructor Init(Bounds: TRect; WinTitle: String; WindowNo: Word);
  43.     function MakeInterior(Bounds: TRect; Left: Boolean): PInterior;
  44.   end;
  45.  
  46. procedure ReadFile;
  47. var
  48.   F: Text;
  49.   S: String;
  50. begin
  51.   LineCount := 0;
  52.   Assign(F, FileToRead);
  53.   {$I-}
  54.   Reset(F);
  55.   {$I+}
  56.   if IOResult <> 0 then
  57.   begin
  58.     Writeln('Cannot open ', FileToRead);
  59.     Halt(1);
  60.   end;
  61.   while not Eof(F) and (LineCount < MaxLines) do
  62.   begin
  63.     Readln(F, S);
  64.     Lines[LineCount] := NewStr(S);
  65.     Inc(LineCount);
  66.   end;
  67.   Close(F);
  68. end;
  69.  
  70. procedure DoneFile;
  71. var
  72.   I: Integer;
  73. begin
  74.   for I := 0 to LineCount - 1 do
  75.     if Lines[I] <> nil then DisposeStr(Lines[i]);
  76. end;
  77.  
  78. { TInterior }
  79. constructor TInterior.Init(var Bounds: TRect; AHScrollBar,
  80.   AVScrollBar: PScrollBar);
  81. begin
  82.   TScroller.Init(Bounds, AHScrollBar, AVScrollBar);
  83.   Options := Options or ofFramed;
  84.   SetLimit(128, LineCount);
  85. end;
  86.  
  87. procedure TInterior.Draw;
  88. var
  89.   Color: Byte;
  90.   I, Y: Integer;
  91.   B: TDrawBuffer;
  92. begin
  93.   Color := GetColor(1);
  94.   for Y := 0 to Size.Y - 1 do
  95.   begin
  96.     MoveChar(B, ' ', Color, Size.X);
  97.     i := Delta.Y + Y;
  98.     if (I < LineCount) and (Lines[I] <> nil) then
  99.       MoveStr(B, Copy(Lines[I]^, Delta.X + 1, Size.X), Color);
  100.     WriteLine(0, Y, Size.X, 1, B);
  101.   end;
  102. end;
  103.  
  104. { TDemoWindow }
  105. constructor TDemoWindow.Init(Bounds: TRect; WinTitle: String;
  106.   WindowNo: Word);
  107. var
  108.   S: string[3];
  109.   R: TRect;
  110.   RInterior, LInterior: PInterior;
  111. begin
  112.   Str(WindowNo, S);
  113.   TWindow.Init(Bounds, WinTitle + ' ' + S, wnNoNumber);
  114.   GetExtent(Bounds);
  115.   R.Assign(Bounds.A.X, Bounds.A.Y, Bounds.B.X div 2 + 1, Bounds.B.Y);
  116.   LInterior := MakeInterior(R, True);
  117.   LInterior^.GrowMode := gfGrowHiY;
  118.   Insert(Linterior);
  119.   R.Assign(Bounds.B.X div 2, Bounds.A.Y, Bounds.B.X, Bounds.B.Y);
  120.   RInterior := MakeInterior(R,False);
  121.   RInterior^.GrowMode := gfGrowHiX + gfGrowHiY;
  122.   Insert(RInterior);
  123. end;
  124.  
  125. function TDemoWindow.MakeInterior(Bounds: TRect; Left: Boolean): PInterior;
  126. var
  127.   HScrollBar, VScrollBar: PScrollBar;
  128.   R: TRect;
  129. begin
  130.   R.Assign(Bounds.B.X-1, Bounds.A.Y+1, Bounds.B.X, Bounds.B.Y-1);
  131.   VScrollBar := New(PScrollBar, Init(R));
  132.   VScrollBar^.Options := VScrollBar^.Options or ofPostProcess;
  133.   if Left then VScrollBar^.GrowMode := gfGrowHiY;
  134.   Insert(VScrollBar);
  135.   R.Assign(Bounds.A.X+2, Bounds.B.Y-1, Bounds.B.X-2, Bounds.B.Y);
  136.   HScrollBar := New(PScrollBar, Init(R));
  137.   HScrollBar^.Options := HScrollBar^.Options or ofPostProcess;
  138.   if Left then HScrollBar^.GrowMode := gfGrowHiY + gfGrowLoY;
  139.   Insert(HScrollBar);
  140.   Bounds.Grow(-1,-1);
  141.   MakeInterior := New(PInterior, Init(Bounds, HScrollBar, VScrollBar));
  142. end;
  143.  
  144. { TMyApp }
  145. procedure TMyApp.HandleEvent(var Event: TEvent);
  146. begin
  147.   TApplication.HandleEvent(Event);
  148.   if Event.What = evCommand then
  149.   begin
  150.     case Event.Command of
  151.       cmNewWin: NewWindow;
  152.     else
  153.       Exit;
  154.     end;
  155.     ClearEvent(Event);
  156.   end;
  157. end;
  158.  
  159. procedure TMyApp.InitMenuBar;
  160. var R: TRect;
  161. begin
  162.   GetExtent(R);
  163.   R.B.Y := R.A.Y + 1;
  164.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  165.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  166.       NewItem('~O~pen', 'F3', kbF3, cmFileOpen, hcNoContext,
  167.       NewItem('~N~ew', 'F4', kbF4, cmNewWin, hcNoContext,
  168.       NewLine(
  169.       NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext,
  170.       nil))))),
  171.     NewSubMenu('~W~indow', hcNoContext, NewMenu(
  172.       NewItem('~N~ext', 'F6', kbF6, cmNext, hcNoContext,
  173.       NewItem('~Z~oom', 'F5', kbF5, cmZoom, hcNoContext,
  174.       nil))),
  175.     nil))
  176.   )));
  177. end;
  178.  
  179. procedure TMyApp.InitStatusLine;
  180. var R: TRect;
  181. begin
  182.   GetExtent(R);
  183.   R.A.Y := R.B.Y - 1;
  184.   StatusLine := New(PStatusLine, Init(R,
  185.     NewStatusDef(0, $FFFF,
  186.       NewStatusKey('', kbF10, cmMenu,
  187.       NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,
  188.       NewStatusKey('~F4~ New', kbF4, cmNewWin,
  189.       NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,
  190.       nil)))),
  191.     nil)
  192.   ));
  193. end;
  194.  
  195. procedure TMyApp.NewWindow;
  196. var
  197.   Window: PDemoWindow;
  198.   R: TRect;
  199. begin
  200.   Inc(WinCount);
  201.   R.Assign(0, 0, 45, 13);
  202.   R.Move(Random(34), Random(11));
  203.   Window := New(PDemoWindow, Init(R, 'Demo Window', WinCount));
  204.   DeskTop^.Insert(Window);
  205. end;
  206.  
  207. var
  208.   MyApp: TMyApp;
  209.  
  210. begin
  211.   ReadFile;
  212.   MyApp.Init;
  213.   MyApp.Run;
  214.   MyApp.Done;
  215.   DoneFile;
  216. end.
  217.