home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number5 / tvtsr / tvtest.pas < prev   
Pascal/Delphi Source File  |  1991-10-27  |  7KB  |  237 lines

  1. PROGRAM TVTEST;
  2. { This is a simple Turbo Vision TSR. The OPINT and OPTSR units 
  3.   are from TurboPower Software's Object Professional or TSRs 
  4.   Made Easy libraries, which are commercial libraries and may 
  5.   not be distributed. This file, the TVSCREEN unit, and associated
  6.   text may be distributed freely.}
  7. {$M 8192, 0, 655360}
  8. {$S-,R-,I-,V-,X+}
  9.  
  10. USES
  11.   Dos,
  12.   TvScreen,                                 { Refer to Listing 2}
  13.   Objects, Drivers, Memory, Views, Menus, Dialogs, MsgBox, App,
  14.   OpInt, OpTsr;           {From TurboPower's Object Professional}
  15.  
  16. CONST
  17.   WinCount: Integer =   0;
  18.   cmFileOpen        = 100;
  19.   cmNewWin          = 101;
  20.  
  21. TYPE
  22.   TMyApp = OBJECT(TApplication)
  23.     CONSTRUCTOR Init;                              {added for TSR}
  24.     DESTRUCTOR Done; Virtual;                      {added for TSR}
  25.     PROCEDURE HandleEvent(VAR Event: TEvent); virtual;
  26.     PROCEDURE InitMenuBar; virtual;
  27.     PROCEDURE InitStatusLine; virtual;
  28.     PROCEDURE NewWindow;
  29.   END;
  30.   PDemoWindow = ^TDemoWindow;
  31.   TDemoWindow = OBJECT(TWindow)
  32.   END;
  33.  
  34. CONST GoneResident : Boolean = False;              { Flag for TSR}
  35.  
  36. CONSTRUCTOR TMyApp.Init;
  37. CONST
  38.   TvTestStr = ^C'TVTEST 1.0'^M + ^C'Installing as a TSR'^M +
  39.               ^C'Press Alt-TAB to popup';
  40. VAR Control : Word;
  41. BEGIN
  42.   TApplication.Init;                   { Dialog box added for TSR}
  43.   Control := MessageBox(TvTestStr, Nil, mfInformation+mfOKCancel);
  44.   IF Control = cmCancel THEN BEGIN
  45.     Done;
  46.     Halt;
  47.   END;
  48. END;
  49.  
  50. DESTRUCTOR TMyApp.Done;
  51. VAR Control : Word;
  52. BEGIN                                  { Dialog box added for TSR}
  53.   IF GoneResident THEN
  54.     Control := MessageBox(^C'Unloading resident copy of TVTEST',
  55.                  Nil, mfInformation + mfOKButton);
  56.   TApplication.Done;
  57. END;
  58.  
  59. PROCEDURE TMyApp.HandleEvent(VAR Event: TEvent);
  60. BEGIN
  61.   TApplication.HandleEvent(Event);
  62.   IF Event.What = evCommand THEN BEGIN
  63.     CASE Event.Command of
  64.       cmNewWin: NewWindow;
  65.     ELSE
  66.       Exit;
  67.     END;
  68.     ClearEvent(Event);
  69.   END;
  70. END;
  71.  
  72. PROCEDURE TMyApp.InitMenuBar;
  73. VAR R: TRect;
  74. BEGIN
  75.   GetExtent(R);
  76.   R.B.Y := R.A.Y + 1;
  77.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  78.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  79.       NewItem('~O~pen', 'F3', kbF3, cmFileOpen, hcNoContext,
  80.       NewItem('~N~ew', 'F4', kbF4, cmNewWin, hcNoContext,
  81.       NewLine(
  82.       NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext,
  83.       nil))))),
  84.     NewSubMenu('~W~indow', hcNoContext, NewMenu(
  85.       NewItem('~N~ext', 'F6', kbF6, cmNext, hcNoContext,
  86.       NewItem('~Z~oom', 'F5', kbF5, cmZoom, hcNoContext,
  87.       nil))),
  88.     nil))
  89.   )));
  90. END;
  91.  
  92. PROCEDURE TMyApp.InitStatusLine;
  93. VAR R: TRect;
  94. BEGIN
  95.   GetExtent(R);
  96.   R.A.Y := R.B.Y - 1;
  97.   StatusLine := New(PStatusLine, Init(R,
  98.     NewStatusDef(0, $FFFF,
  99.       NewStatusKey('', kbF10, cmMenu,
  100.       NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,
  101.       NewStatusKey('~F4~ New', kbF4, cmNewWin,
  102.       NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,
  103.       nil)))),
  104.     nil)
  105.   ));
  106. END;
  107.  
  108. PROCEDURE TMyApp.NewWindow;
  109. VAR
  110.   Window: PDemoWindow;
  111.   R: TRect;
  112. BEGIN
  113.   Inc(WinCount);
  114.   R.Assign(0, 0, 26, 7);
  115.   R.Move(Random(58), Random(16));
  116.   Window := New(PDemoWindow, Init(R, 'Demo Window', WinCount));
  117.   DeskTop^.Insert(Window);
  118. END;
  119.  
  120. VAR
  121.   MyApp: TMyApp;
  122.  
  123. CONST                       {== Changes for making a TSR below ==}
  124.   HotKey = $080F;                                        {Alt-Tab}
  125.   ExtraHeapParas = (48 * 1024) div 16;    {48K extra heap for TSR}
  126.   OurModuleName : String[8] = 'TVTEST10';
  127.  
  128. PROCEDURE ShutTVDownForUnload;
  129. BEGIN                                       { Reinit Turbo Vision}
  130.   InitVideo;
  131.   InitMemory;
  132.   InitEvents;
  133.   MyApp.Redraw;
  134.   DRIVERS.ShowMouse;
  135.   MyApp.Done;
  136. END;
  137.  
  138. PROCEDURE CmdEntryPoint(BP : Word); Interrupt;
  139. VAR
  140.   Regs : IntRegisters absolute BP;
  141.   SavePSP : Word;
  142.  
  143. BEGIN
  144.   SavePSP := GetPSP;
  145.   SetPSP(PrefixSeg);
  146.   Regs.AL := 0;
  147.   IF SafeToDisable THEN BEGIN
  148.     ShutTVDownForUnload;
  149.     IF DisableTSR THEN Regs.AL := 1;
  150.   END;
  151.   SetPSP(SavePSP);
  152. END;
  153.  
  154. PROCEDURE UnloadFromCommandLine;
  155. VAR
  156.   Regs : IntRegisters;
  157.   P : IfcPtr;
  158. BEGIN
  159.   P := ModulePtrByName(OurModuleName);
  160.   IF (P <> Nil) and (P^.CmdEntryPtr <> Nil) THEN BEGIN
  161.     RestoreAllVectors;
  162.     EmulateInt(Regs, P^.CmdEntryPtr);
  163.     IF Boolean(Regs.AL) THEN
  164.       WriteLn('TVTEST successfully unloaded')
  165.     ELSE
  166.       WriteLn('Unable to unload TVTEST');
  167.   END;
  168. END;
  169.  
  170. PROCEDURE PopupEntryPoint(VAR Regs : Registers); far;
  171. VAR
  172.   Covers : pointer;
  173.   MSP : MouseStatePtr;
  174.   MStateSize : Word;
  175.   XY : Word;
  176.   ScanLines : Word;
  177. BEGIN               { Reset video VARs in case video mode changed}
  178.   ReinitVideo;
  179.   IN NOT InTextMode THEN Exit;        { Can't popup over graphics}
  180.   IF MouseInstalled THEN BEGIN
  181.     MStateSize := MouseStateBufferSize;
  182.     IF (MStateSize = 0) or (MStateSize > MaxAvail) THEN Exit;
  183.     SaveMouseState(MSP); { Save mouse, cursor and screen state...}
  184.   END;                             {...for underlying application}
  185.   GetCursorState(XY, ScanLines);
  186.   IN NOT SaveScreen(Covers) THEN BEGIN
  187.     RestoreMouseState(MSP);        { Done here to release heap...}
  188.     Exit;                                      { ...space for MSP}
  189.   END;
  190.   InitVideo;                                { Reinit Turbo Vision}
  191.   InitMemory;
  192.   InitEvents;
  193.   (* InitSysError; *)          {!! DO NOT CALL THIS IN A POPUP !!}
  194.   MyApp.Redraw;
  195.   DRIVERS.ShowMouse;
  196.   MyApp.Run;
  197.   DRIVERS.HideMouse;
  198.   DoneVideo;                            { Shut down Turbo Vision }
  199.   DoneEvents;
  200.   DoneMemory;
  201.   RestoreScreen(Covers);                     { Restore screen,...}
  202.   RestoreCursorState(XY, ScanLines);               {...cursor,...}
  203.   IF MouseInstalled THEN RestoreMouseState(MSP);    {...and mouse}
  204. END;
  205. {========================== MAIN ================================}
  206. VAR Parameter : String[128];
  207.  
  208. BEGIN
  209.   IF ParamCount > 0 THEN BEGIN
  210.     Parameter := ParamStr(1);
  211.     IF (Length(Parameter) = 2) and (Parameter[1] in ['/','-']) THEN
  212.       IF UpCase(Parameter[2]) = 'U' THEN BEGIN
  213.         UnloadFromCommandLine;
  214.         Halt;
  215.       END;
  216.   END;
  217.   IF ModuleInstalled(OurModuleName) THEN BEGIN
  218.     WriteLn('TVTEST already loaded.');
  219.     Halt;
  220.   END;
  221.   InstallModule(OurModuleName, @CmdEntryPoint);
  222.   IN NOT DefinePop(HotKey, PopupEntryPoint, Ptr(SSeg, SPtr), True)
  223.   THEN BEGIN
  224.     WriteLn('unable to define popup');
  225.     Halt;
  226.   END;
  227.   MyApp.Init;                        { Initialize the application}
  228.   DoneSysError;                           { Shutdown Turbo Vision}
  229.   DoneEvents;
  230.   DoneVideo;
  231.   DoneMemory;
  232.   PopupsOn;
  233.   GoneResident := True;
  234.   StayRes(ParagraphsToKeep+ExtraHeapParas, 0);
  235.   WriteLn('unable to go resident');
  236. END.
  237.