home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TOTDOC.ZIP / CHAPT17.TXT < prev    next >
Encoding:
Text File  |  1991-02-11  |  14.3 KB  |  301 lines

  1.                                                                        Replacing
  2.                                                                              the
  3.                                                                           Screen
  4.                                                                           Writer
  5.  
  6.  
  7.  
  8.          "I don't want to achieve immortality through my work, I want to achieve
  9.          immortality by not dying."
  10.  
  11.                                                                      Woody Allen
  12.  
  13.  
  14.  
  15.          The Toolkit uses some assembly language routines for writing to the
  16.          screen, as well as for moving blocks of data to and from the screen.
  17.          The routines are optimized for maximum speed, and by-pass BIOS, i.e.
  18.          they write directly to the video area of memory. If you are using a
  19.          non-standard display device, such as a palmtop computer, or you don't
  20.          like the (1..80,1..25) coordinate scheme, or for whatever other reason,
  21.          you can develop your own screen writing code to meet your custom needs.
  22.  
  23.          In this chapter you will learn how to create an object to replace the
  24.          Toolkit's screen writing routines.
  25.  
  26.  
  27.  
  28. Understanding ScreenOBJ and WriteOBJ
  29.  
  30.          Before discussing how to replace the Toolkit's methods, you need to
  31.          understand how those methods work. Bear in mind that your replacement
  32.          algorithms may need to write to virtual screens as well as to the
  33.          physical screen.
  34.  
  35.          The ScreenOBJ object is the primary Toolkit object for managing screen
  36.          and video related activity. The global instance Screen is automatically
  37.          initialized when you use the totFAST unit, and you should use the
  38.          Screen instance for all writing to the visible display. Additional
  39.          ScreenOBJ instances can be used to manage virtual (or non-visible)
  40.          screens. The method SAVE is used to save a copy of the visible screen,
  41.          and the method CREATE is used to create an empty virtual screen with
  42.          maximum dimensions of 255 rows by 255 columns. All or part of a virtual
  43.          screen can subsequently be displayed on the visible screen.
  44.  
  45.          Each ScreenOBJ instance includes a pointer to a WriteOBJ object. Wri-
  46.          teOBJ actually performs the core screen writing functions. For example,
  47.          when you call ScreenOBJ's method WriteAT, behind the scenes the Toolkit
  48.          calls the WriteOBJ method WriteAT. By instructing ScreenOBJ to point to
  49.          a different WriteOBJ object you can change how screen tasks are per-
  50.          formed. In other words, by creating a new WriteOBJ object, you change
  51.          how the Toolkit accesses the video functions.
  52.  
  53.          The two step process is to create a descendant of WriteOBJ, and then
  54.          instruct all ScreenOBJ instances to use this new object.
  55.  
  56.  
  57.  
  58. 17-2                                                       Extending the Toolkit
  59.  
  60. --------------------------------------------------------------------------------
  61.  
  62. Replacing WriteOBJ
  63.  
  64.          The primary functions of WriteOBJ are to write to the screen, to move
  65.          blocks of data to and from the screen, to position the cursor, to
  66.          manage window settings, and to change the display attribute of a rect-
  67.          angular area of the screen.
  68.  
  69.          The declaration of WriteOBJ is as follows:
  70.  
  71.          WritePtr = ^WriteOBJ;
  72.          WriteOBJ = object
  73.             vWidth: byte;           {how wide is screen}
  74.             vScreenPtr: pointer;    {memory location of screen data}
  75.             vWindow: tByteCoords;   {active screen area}
  76.             vWindowOn: boolean;     {is window area active}
  77.             vWindowIgnore: boolean; {ignore window settings}
  78.             {methods...}
  79.             Constructor Init;
  80.             procedure   SetScreen(var P:Pointer; W:byte);
  81.             function    WindowOff: boolean;
  82.             procedure   SetWinIgnore(On:Boolean);
  83.             procedure   WindowOn;
  84.             procedure   WindowCoords(var Coords: tByteCoords);
  85.             function    WindowActive: boolean;
  86.             function    WinX: byte;
  87.             function    WinY: byte;
  88.             procedure   GetWinCoords(var X1,Y1,X2,Y2:byte);
  89.             procedure   WriteAT(X,Y,attr:byte;Str:string);             VIRTUAL;
  90.             procedure   WritePlain(X,Y:byte;Str:string);               VIRTUAL;
  91.             procedure   Write(Str:string);                             VIRTUAL;
  92.             procedure   WriteLn(Str:string);                           VIRTUAL;
  93.             procedure   GotoXY(X,Y: word);                             VIRTUAL;
  94.             function    WhereX: word;                                  VIRTUAL;
  95.             function    WhereY: word;                                  VIRTUAL;
  96.             procedure   SetWindow(X1,Y1,X2,Y2: byte);                  VIRTUAL;
  97.             procedure   ResetWindow;                                   VIRTUAL;
  98.             procedure   ChangeAttr(Col,Row,Att:byte;Len:word);         VIRTUAL;
  99.             procedure   MoveFromScreen(var Source,Dest;Length:Word);   VIRTUAL;
  100.             procedure   MoveToScreen(var Source,Dest; Length:Word);    VIRTUAL;
  101.             procedure   Clear(Att:byte;Ch:char);                       VIRTUAL;
  102.             destructor  Done;                                          VIRTUAL;
  103.          end; {WriteOBJ}
  104.  
  105.  
  106.          WriteOBJ's data describes the size and memory location of the screen,
  107.          and the status of the window settings.
  108.  
  109.  
  110.  
  111. Replacing the Screen Writer                                                 17-3
  112.  
  113. --------------------------------------------------------------------------------
  114.  
  115.          vWidth is a byte variable which identifies the width of the screen.
  116.          vScreenPtr is a pointer to the start of the screen display memory area.
  117.          The memory is organized into byte pairs, like actual video display
  118.          memory - the first byte is the attribute of a character and the second
  119.          byte is the ASCII code of the character. The associated ScreenOBJ
  120.          instance allocates the necessary memory and calls the WriteOBJ method
  121.          SetScreen to update the vWidth and vScreenPtr variables.
  122.  
  123.          The other variables provide information about the screen's active win-
  124.          dow. vWindow is a record describing the (X1,Y1) and (X2,Y2) active
  125.          window coordinates. vWindowOn and vWindowIgnore are two boolean
  126.          variables which indicate whether the window settings are active. If
  127.          vWindowOn is false or vWindowIgnore is true, the WriteOBJ screen writ-
  128.          ing methods should ignore the current window settings.
  129.  
  130.          The virtual methods shown in the WriteOBJ declaration represent the
  131.          heart of the screen writing routines. The table below describes their
  132.          specific purpose.
  133.  
  134.  
  135.  
  136.                         Method                           Description
  137.  
  138.           WriteAT(X,Y,attr:byte;Str:string);  Writes to the screen at (X,Y)
  139.                                               using the specified attribute.
  140.           WritePlain(X,Y:byte;Str:string);    Writes to the screen at (X,Y)
  141.                                               using that region's attribute.
  142.           Write(Str:string);                  Writes at the current cursor
  143.                                               position like Turbo's Write.
  144.           WriteLn(Str:string);                Write at the current cursor posi-
  145.                                               tion like Turbo's WriteLn.
  146.           GotoXY(X,Y: word);                  Positions the cursor at (X,Y).
  147.  
  148.           WhereX: word;                       Returns the cursor's X coordi-
  149.                                               nate.
  150.           WhereY: word;
  151.                                               Returns the cursor's Y coordi-
  152.           SetWindow(X1,Y1,X2,Y2: byte);       nate.
  153.  
  154.           ResetWindow;                        Sets the window coordinates to
  155.                                               specified coordinates.
  156.           ChangeAttr(Col,Row,Att:byte;        Sets the window coordinates to
  157.                      Len:word);               entire screen.
  158.           MoveFromScreen(var Source,Dest;     Changes the display attribute to
  159.                              Len:Word);       Att starting at (X,Y) for Len
  160.                                               bytes.
  161.                                               Moves a block of memory from
  162.           MoveToScreen(var Source,Dest;       Source to Dest, assuming Source
  163.                            Len:Word);         is the visible screen. Len repre-
  164.           Clear(Att:byte;Ch:char);            sents the number of words (not
  165.  
  166.  
  167.  
  168. 17-4                                                       Extending the Toolkit
  169.  
  170. --------------------------------------------------------------------------------
  171.  
  172.                                               bytes) to move.
  173.                                               As above, but assumes Dest is the
  174.           Done;                               visible screen.
  175.                                               Clears a rectangular area of the
  176.                                               display to the specified attrib-
  177.                                               ute and replaces all characters
  178.                                               with Ch.
  179.                                               Disposes of the object.
  180.  
  181.  
  182.          To develop your own WriteOBJ object, you should create a descendant of
  183.          WriteOBJ, and thanks to inheritance, you can replace as little as one
  184.          procedure or as many as all of them.
  185.  
  186.          The file EXTSCR1.PAS is a demonstration unit which creates a new
  187.          object. The new object is called MonoWriteOBJ, and the objective is to
  188.          force the Toolkit to use the attribute white-on-black, regardless of
  189.          the attribute specified. The declaration of MonoWriteOBJ is as follows:
  190.  
  191.          Unit ExtFast;
  192.          {$I TOTFLAGS.INC}
  193.          INTERFACE
  194.          uses DOS, CRT, totFAST;
  195.  
  196.  
  197.          TYPE
  198.          MonoWriteOBJ = object (WriteOBJ)
  199.             constructor Init;
  200.             procedure   WriteAT(X,Y,attr:byte;Str:string);       VIRTUAL;
  201.             procedure   ChangeAttr(X,Y,Att:byte;Len:word);       VIRTUAL;
  202.             procedure   Clear(Att:byte;Ch:char);                 VIRTUAL;
  203.             destructor  Done;                                    VIRTUAL;
  204.          end; {MonoWriteOBJ}
  205.  
  206.          IMPLEMENTATION
  207.  
  208.          constructor MonoWriteOBJ.Init;
  209.          {}
  210.          begin
  211.             WriteOBJ.Init;
  212.             TextColor(white);
  213.             Textbackground(black);
  214.          end; {MonoWriteOBJ.Init}
  215.  
  216.          procedure MonoWriteOBJ.WriteAT(X,Y,attr:byte;Str:string);
  217.          {}
  218.          begin
  219.             WriteOBJ.WriteAT(X,Y,white,Str);
  220.          end; {MonoWriteOBJ.WriteAT}
  221.  
  222.  
  223.  
  224.  
  225. Replacing the Screen Writer                                                 17-5
  226.  
  227. --------------------------------------------------------------------------------
  228.  
  229.          procedure MonoWriteOBJ.ChangeAttr(X,Y,Att:byte;Len:word);
  230.          {}
  231.          begin
  232.             WriteOBJ.ChangeAttr(X,Y,white,Len);
  233.          end; {MonoWriteOBJ.ChangeAttr}
  234.  
  235.          procedure MonoWriteOBJ.Clear(Att:byte;Ch:char);
  236.          {}
  237.          begin
  238.             WriteOBJ.Clear(white,Ch);
  239.          end; {MonoWriteOBJ.Clear}
  240.  
  241.          destructor MonoWriteOBJ.Done;
  242.          {}
  243.          begin
  244.             WriteOBJ.Done;
  245.          end; {MonoWriteOBJ.Done}
  246.  
  247.          end.
  248.  
  249.  
  250.          MonoWriteOBJ redefines the three primary methods which control display
  251.          attributes. It intercepts the requested attribute and always substi-
  252.          tutes the attribute white.
  253.  
  254.          If you want to modify the way the Toolkit writes to the physical
  255.          screen, but are satisfied with the virtual screen routines, then your
  256.          unit should use the totSYS unit, and the revised methods should use the
  257.          following statement to determine whether the instance is pointing to
  258.          the visible screen:
  259.  
  260.          if vScreenPtr = Monitor^.BaseOfScreen then
  261.             {new visible screen routines}
  262.          else {virtual screen}
  263.             {call equivalent WriteOBJ method};
  264.  
  265.  
  266.  
  267. Using the Method AssignWriteOBJ
  268.  
  269.          Having created your new object, you must instruct the Toolkit to use
  270.          it. This is achieved by initializing your object, and then calling the
  271.          ScreenOBJ method AssignWriteOBJ method. For example:
  272.  
  273.                   Screen.AssignWriteOBJ(WhiteWrite);
  274.  
  275.          Any virtual screens you create must be assigned the new WriteOBJ object
  276.          individually in the same manner. For example:
  277.  
  278.                   Save3.AssignWriteOBJ(WhiteWrite);
  279.  
  280.  
  281.  
  282.  
  283. 17-6                                                       Extending the Toolkit
  284.  
  285. --------------------------------------------------------------------------------
  286.  
  287.          The demo program, EXTDEM3.PAS, listed below, shows the new ExtFast unit
  288.          in use. This program is actually an adaptation of DEMBR1, which uses
  289.          MonoWriteOBJ in preference to WriteOBJ. If you run it, you will see
  290.          that the entire display uses white on black, even though other attrib-
  291.          utes are specified.
  292.  
  293.  
  294.  
  295.          Program ExtendedDemoThree;
  296.          {EXTDEM3 - this program shows how to use the MonoWriteOBJ object
  297.           developed in the ExtFast unit. The demo is actually a simple
  298.           adaptation of the browse demo file DEMBR1}
  299.  
  300.          Uses DOS,CRT,
  301.               totINPUT, totFAST,totLIST, totSTR, ExtFast;
  302.  
  303.          var
  304.             WhiteWrite: MonoWriteOBJ;
  305.             BWin: BrowseArrayOBJ;
  306.             StringList: array[1..26] of string[100];
  307.             I : integer;
  308.  
  309.          begin
  310.             WhiteWrite.Init;
  311.             Screen.AssignWriteOBJ(WhiteWrite);
  312.             for I := 1 to 26 do  {first assign something to the string array}
  313.                StringList[I] := 'Line '+IntToStr(I)+': '+replica-
  314.          te(80,char(I+64));
  315.             ShadowTot^.SetShadowStyle(downright,red,chr(219));
  316.             Screen.Clear(green,' '); {paint the screen}
  317.             Key.SetFast;
  318.             Key.SetClick(true);
  319.             with BWin do
  320.             begin
  321.                Init;
  322.                AssignList(StringList,26,100);
  323.                Go;
  324.                Done;
  325.             end;
  326.          end.
  327.