home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number5 / tvtsr / tvstestm.pas < prev    next >
Pascal/Delphi Source File  |  1991-04-12  |  7KB  |  272 lines

  1. {
  2.   This is a very simple Turbo Vision TSR. It uses the unit TVSCREEN provided
  3.   by TurboPower Software, and the OPINT and OPTSR units from TurboPower's
  4.   Object Professional or TSRs Made Easy libraries. Object Professional and
  5.   TSRs Made Easy are commercial libraries and may not be distributed. This
  6.   file, the TVSCREEN unit, and associated text may be distributed freely.
  7. }
  8. {$S-,R-,I-,V-,X+}
  9. unit TVSTESTM;
  10. interface
  11. uses
  12.   Dos,
  13.   TvScreen,
  14.   Objects, Drivers, Memory, Views, Menus, MsgBox, App,
  15.   OpSwap1;
  16.  
  17. procedure InitTvTest;
  18.  
  19. implementation
  20.  
  21. const
  22.   WinCount: Integer =   0;
  23.   cmFileOpen        = 100;
  24.   cmNewWin          = 101;
  25.  
  26. type
  27.   TMyApp = object(TApplication)
  28.     constructor Init;                    {added for TSR}
  29.     destructor Done; Virtual;            {added for TSR}
  30.     procedure HandleEvent(var Event: TEvent); virtual;
  31.     procedure InitMenuBar; virtual;
  32.     procedure InitStatusLine; virtual;
  33.     procedure NewWindow;
  34.   end;
  35.  
  36.   PDemoWindow = ^TDemoWindow;
  37.   TDemoWindow = object(TWindow)
  38.   end;
  39.  
  40. {Added for TSR. Flag indicating whether program has gone resident yet}
  41. const
  42.   GoneResident      : Boolean = False;
  43.  
  44. { TMyApp }
  45. constructor TMyApp.Init;
  46. const
  47.   TvTestStr = ^C'TVSTEST 1.0'^M +
  48.               ^C'Installing as a swappable TSR'^M +
  49.               ^C'Press Alt-TAB to popup';
  50. var
  51.   Control : Word;
  52. begin
  53.   TApplication.Init;
  54.  
  55.   {dialog box added for TSR}
  56.   Control := MessageBox(TvTestStr, Nil, mfInformation + mfOKCancel);
  57.   if Control = cmCancel then begin
  58.     Done;
  59.     Halt;
  60.   end;
  61. end;
  62.  
  63. destructor TMyApp.Done;
  64. var
  65.   Control : Word;
  66. begin
  67.   {dialog box added for TSR}
  68.   if GoneResident then
  69.     Control := MessageBox(^C'Unloading resident copy of TVSTEST', Nil,
  70.                           mfInformation + mfOKButton);
  71.   TApplication.Done;
  72. end;
  73.  
  74. procedure TMyApp.HandleEvent(var Event: TEvent);
  75. begin
  76.   TApplication.HandleEvent(Event);
  77.   if Event.What = evCommand then
  78.   begin
  79.     case Event.Command of
  80.       cmNewWin: NewWindow;
  81.     else
  82.       Exit;
  83.     end;
  84.     ClearEvent(Event);
  85.   end;
  86. end;
  87.  
  88. procedure TMyApp.InitMenuBar;
  89. var R: TRect;
  90. begin
  91.   GetExtent(R);
  92.   R.B.Y := R.A.Y + 1;
  93.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  94.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  95.       NewItem('~O~pen', 'F3', kbF3, cmFileOpen, hcNoContext,
  96.       NewItem('~N~ew', 'F4', kbF4, cmNewWin, hcNoContext,
  97.       NewLine(
  98.       NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext,
  99.       nil))))),
  100.     NewSubMenu('~W~indow', hcNoContext, NewMenu(
  101.       NewItem('~N~ext', 'F6', kbF6, cmNext, hcNoContext,
  102.       NewItem('~Z~oom', 'F5', kbF5, cmZoom, hcNoContext,
  103.       nil))),
  104.     nil))
  105.   )));
  106. end;
  107.  
  108. procedure TMyApp.InitStatusLine;
  109. var R: TRect;
  110. begin
  111.   GetExtent(R);
  112.   R.A.Y := R.B.Y - 1;
  113.   StatusLine := New(PStatusLine, Init(R,
  114.     NewStatusDef(0, $FFFF,
  115.       NewStatusKey('', kbF10, cmMenu,
  116.       NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,
  117.       NewStatusKey('~F4~ New', kbF4, cmNewWin,
  118.       NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,
  119.       nil)))),
  120.     nil)
  121.   ));
  122. end;
  123.  
  124. procedure TMyApp.NewWindow;
  125. var
  126.   Window: PDemoWindow;
  127.   R: TRect;
  128. begin
  129.   Inc(WinCount);
  130.   R.Assign(0, 0, 26, 7);
  131.   R.Move(Random(58), Random(16));
  132.   Window := New(PDemoWindow, Init(R, 'Demo Window', WinCount));
  133.   DeskTop^.Insert(Window);
  134. end;
  135.  
  136. var
  137.   MyApp: TMyApp;
  138.  
  139. {=========================================================================}
  140.  
  141. const
  142.   HotKey = $080F; {alt-tab}
  143.   ExtraHeapParas = (48 * 1024) div 16;    {48K of extra heap for TSR}
  144.   OurModuleName : String[8] = 'TVSTEST1';
  145.  
  146. procedure ShutTVDownForUnload;
  147. begin
  148.   {reinit Turbo Vision}
  149.   InitVideo;
  150.   InitMemory;
  151.   InitEvents;
  152.  
  153.   MyApp.Redraw;
  154.   DRIVERS.ShowMouse;
  155.   MyApp.Done;
  156. end;
  157.  
  158. procedure CmdEntryPoint; Far;
  159.  
  160. begin
  161.   if SafeToDisable then begin
  162.     ShutTVDownForUnload;
  163.     LongInt(CSSwapData^.ThisIFC.UserData) := LongInt(Ord(DisableTSR));
  164.   end;
  165. end;
  166.  
  167. procedure UnloadFromCommandLine;
  168. var
  169.   P : IfcPtr;
  170. begin
  171.   P := ModulePtrByName(OurModuleName);
  172.   if (P <> Nil) then begin
  173.     RestoreAllVectors;
  174.     P^.CmdEntryPtr;
  175.     if Boolean(P^.UserData) then
  176.       WriteLn('TVSTEST successfully unloaded')
  177.     else
  178.       WriteLn('Unable to unload TVSTEST');
  179.   end;
  180. end;
  181.  
  182. procedure PopupEntryPoint; far;
  183. var
  184.   Covers : pointer;
  185.   MSP : MouseStatePtr;
  186.   MStateSize : Word;
  187.   XY : Word;
  188.   ScanLines : Word;
  189. begin
  190.   ReinitVideo;                    {reset video vars in case video mode changed}
  191.   if not InTextMode then          {can't popup over graphics}
  192.     Exit;
  193.   if MouseInstalled then begin
  194.     MStateSize := MouseStateBufferSize;
  195.     {check to see if mouse driver supports mouse state calls, and enough mem}
  196.     if (MStateSize = 0) or (MStateSize > MaxAvail) then
  197.       Exit;
  198.     {save mouse, cursor and screen state for underlying application}
  199.     SaveMouseState(MSP);
  200.   end;
  201.   GetCursorState(XY, ScanLines);
  202.   if not SaveScreen(Covers) then begin
  203.     RestoreMouseState(MSP);       {done here to release heap space for MSP}
  204.     Exit;
  205.   end;
  206.  
  207.  
  208.   {reinit Turbo Vision}
  209.   InitVideo;
  210.   InitMemory;
  211.   InitEvents;
  212.   (* InitSysError; *)  {!! do not call this in a popup !!}
  213.  
  214.   MyApp.Redraw;
  215.   DRIVERS.ShowMouse;
  216.   MyApp.Run;
  217.   DRIVERS.HideMouse;
  218.  
  219.   {shut down Turbo Vision}
  220.   DoneVideo;
  221.   DoneEvents;
  222.   DoneMemory;
  223.  
  224.   {restore screen, cursor, and mouse states}
  225.   RestoreScreen(Covers);
  226.   RestoreCursorState(XY, ScanLines);
  227.   if MouseInstalled then
  228.     RestoreMouseState(MSP);
  229. end;
  230.  
  231. procedure InitTvTest;
  232.  
  233. var
  234.   Parameter : String[128];
  235.  
  236. begin
  237.   if ParamCount > 0 then begin
  238.     Parameter := ParamStr(1);
  239.     if (Length(Parameter) = 2) and (Parameter[1] in ['/','-']) then
  240.       if UpCase(Parameter[2]) = 'U' then begin
  241.         UnloadFromCommandLine;
  242.         Halt;
  243.       end;
  244.   end;
  245.   if ModuleInstalled(OurModuleName) then begin
  246.     WriteLn('TVSTEST already loaded.');
  247.     Halt;
  248.   end;
  249.   InstallModule(OurModuleName, CmdEntryPoint);
  250.  
  251.   if not DefinePop(HotKey, PopupEntryPoint, Ptr(SSeg, SPtr)) then begin
  252.     WriteLn('Unable to define popup');
  253.     Halt;
  254.   end;
  255.  
  256.   MyApp.Init;
  257.  
  258.   {Shutdown Turbo Vision}
  259.   DoneSysError;
  260.   DoneEvents;
  261.   DoneVideo;
  262.   DoneMemory;
  263.  
  264.   PopupsOn;
  265.   GoneResident := True;
  266.   StayResSwap(ParagraphsToKeep+ExtraHeapParas, 0, 'c:\tvstest1.$$$',
  267.               'c:\tvstest2.$$$', True);
  268.   WriteLn('unable to go resident');
  269. end;
  270.  
  271. end.
  272.