home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / TOTDOC.ZIP / CHAPT18.TXT < prev    next >
Encoding:
Text File  |  1991-02-10  |  15.1 KB  |  334 lines

  1.                                                                        Extending
  2.                                                                          Windows
  3.  
  4.  
  5.  
  6.          "The success hasn't gone to our price."          Peugeot Motor Company
  7.  
  8.  
  9.  
  10.  
  11.  
  12.          Earlier, in chapter 7: Using Windows (page 7-12) a template for
  13.          stretchable windows was presented. This template illustrated how, with
  14.          just a few procedures, you could build customized windows. In this
  15.          chapter you will learn how to use OOP techniques to extend window
  16.          objects, and gain even more flexibility. To illustrate the concepts,
  17.          the StretchWinOBJ object will be extended to provide a generic object
  18.          for scrolling virtual screens.
  19.  
  20.  
  21.  
  22. Window Object Structure
  23.  
  24.          Before trying to extend the window objects, you need to have an under-
  25.          standing of how the window objects function. The table below describes
  26.          the data declared in each window object.
  27.  
  28.  
  29.  
  30.                                        WinOBJ
  31.                  Variable                         Description
  32.            vBorder: tCoords     The (X1,Y1,X2,Y2) coordinates of the window
  33.                                 border.
  34.            vOuter: tCoords      The (X1,Y1,X2,Y2) coordinates of the saved
  35.                                 screen area, i.e. window and shadow.
  36.            vUnderneathPtr:      A pointer to the saved image overlaid by the
  37.            pointer              window.
  38.            vSavedSize: longint  The size, in bytes, of the saved area.
  39.            vTitle: string       The window title.
  40.            vBorderAttr          The window display attributes.
  41.            vTitleAttr
  42.            vBodyAttr
  43.            vIconsAttr: byte
  44.            vStyle:byte          The window style.
  45.            vRemove: boolean     A boolean to indicate whether the window will
  46.                                 be removed when the object is Done.
  47.            vCursX,vCursY,       The cursor settings at the time the window was
  48.            vCursTop,            displayed.
  49.            vCursBot: byte
  50.            vOldWin: tByteCoords The window settings at the time the window was
  51.                                 displayed.
  52.            vOldWinConfine:      A boolean to indicate whether window settings
  53.            boolean              were active when the window was displayed.
  54.            vMVisible: boolean   A boolean to indicate whether the mouse was
  55.                                 visible when the window was displayed.
  56.  
  57. 18-2                                                       Extending the Toolkit
  58.  
  59. --------------------------------------------------------------------------------
  60.                                        MoveWinOBJ
  61.                  Variable                         Description
  62.  
  63.  
  64.            vBoundary:tCoords    The (X1,Y1,X2,Y2) coordinates of the area in
  65.                                 which the window can be moved.
  66.            vMoveKey: word       The key to press to invoke a manual (non-mouse)
  67.                                 move.
  68.            vAllowMove:boolean   A boolean to indicate whether the user is
  69.                                 allowed to move the window.
  70.  
  71.  
  72.                                        ScrollWinOBJ
  73.                  Variable                         Description
  74.            vScrollV:boolean;    A boolean to indicate whether the vertical
  75.                                 scroll bar is visible.
  76.            vScrollH:boolean;    A boolean to indicate whether the horizontal
  77.                                 scroll bar is visible.
  78.  
  79.  
  80.  
  81.  
  82.                                        StretchWinOBJ
  83.                  Variable                         Description
  84.            vZoomed:boolean      A boolean to indicate whether the window is
  85.                                 currently zoomed.
  86.            vPreZoom:tCoords     The coordinates of the window border prior to
  87.                                 zooming.
  88.            vMinWidth:byte       The minimum width of the window.
  89.            vMinDepth:byte       The minimum depth of the window.
  90.            vStretchKey:word     The key to press to invoke a manual (non-mouse)
  91.                                 stretch.
  92.            vZoomKey:word        The key to press to invoke a manual (non-mouse)
  93.                                 zoom.
  94.            vAllowStretch:       A boolean to indicate whether the user is
  95.            boolean              allowed to stretch the window.
  96.            vSmartStretch:       A boolean to indicate whether the window will
  97.            boolean              be updated during a stretch operation.
  98.  
  99.  
  100.          Note that the type tCoords is defined as follows:
  101.  
  102.                   tCoords = record
  103.                      X1,Y1,X2,Y2: shortint;
  104.                   end;
  105.  
  106.  
  107.          By now, you should already know most of the window methods in the file
  108.          TOTWIN.PAS. Review the declaration of all four windows objects for a
  109.          complete list of the methods. From an object extension perspective, the
  110.          following methods are important:
  111.  
  112.          constructor Init;
  113.  
  114.          Initializes the object.
  115.  
  116. Extending Windows                                                           18-3
  117.  
  118. --------------------------------------------------------------------------------
  119.  
  120.          procedure WinKey(var K:word; var X,Y: byte); VIRTUAL;
  121.  
  122.          WinKey is passed keystroke details as variable parameters. If the key-
  123.          stroke is window specific, e.g. a click on the close icon, a zoom, a
  124.          window stretch etc., the method will process the key and modify the
  125.          relevant window characteristic. The value of K is then updated to indi-
  126.          cate the type of activity, e.g. set to 602 to indicate that the window
  127.          has been re-sized.
  128.  
  129.          procedure StretchRefresh; VIRTUAL;
  130.  
  131.          While the user is stretching the lower right window, the method Stret-
  132.          chRefresh is continuously called. This is the mechanism for dynamically
  133.          showing the user how the window will appear while it is being
  134.          stretched. The method is only called if the boolean vSmartStretch is
  135.          set to true. There are two important items to consider. Firstly, this
  136.          method will only be called while the window settings are suspended, so
  137.          any screen writing statements in the procedure must use the full-screen
  138.          coordinate system. Secondly, this method must complete its display
  139.          update very quickly. It will be called continuously during a stretch
  140.          operation, and if the procedure takes too long, the user will notice a
  141.          sluggish response.
  142.  
  143.          procedure Draw; VIRTUAL;
  144.  
  145.          Draw saves the contents of the portion of the screen which will be
  146.          overlaid by the window, and then draws the window and shadow.
  147.  
  148.          destructor Done; VIRTUAL;
  149.  
  150.          Disposes of the window object memory, and removes the window (if it is
  151.          still visible and the vRemove variable is set to true).
  152.  
  153.  
  154.          If you extend any window object, you will probably modify most of these
  155.          methods, since they control the window display and input processing
  156.          routines.
  157.  
  158.  
  159.  
  160. Extending StretchWinOBJ
  161.  
  162.          To illustrate the principles of window extension, a new window object
  163.          for displaying and scrolling virtual screens will be created. You may
  164.          recall that a virtual screen is a screen which has all the properties
  165.          of the physical screen, but is not visible. A virtual screen can be up
  166.          to 255 characters wide and 255 lines deep - quite a screen! The Screen-
  167.          OBJ method PartDisplay can be used to transfer a portion of the virtual
  168.          screen to the visible screen.
  169.  
  170.  
  171.  
  172. 18-4                                                       Extending the Toolkit
  173.  
  174. --------------------------------------------------------------------------------
  175.  
  176.          The first task in designing the new object is to decide what additional
  177.          data will be needed. Obviously, the object will need to know the
  178.          address of the virtual screen, i.e. a pointer to a ScreenOBJ instance.
  179.          Two additional variables will be required to keep track of which part
  180.          of the virtual screen is on display. In other words, the coordinates of
  181.          the top-left corner of the visible part of the virtual screen. To make
  182.          sure that the user doesn't scroll beyond the bottom-edge or past the
  183.          right-edge of the virtual screen, the object will need to know the
  184.          width and depth of the virtual screen. The preliminary object declara-
  185.          tion might therefore be as follows:
  186.  
  187.          VirtualWinOBJ = object (StretchWinOBJ)
  188.             vScreen: ScreenPtr;
  189.             vTopLine: integer;   {line number of first visible line}
  190.             vFirstChar: integer; {number of left-most visible character}
  191.             vScreenWidth: byte;
  192.             vScreenDepth: byte;
  193.             {Methods...}
  194.             constructor Init;
  195.             procedure   AssignVirtualScreen(var Scr:ScreenOBJ);
  196.             destructor  Done;                                    VIRTUAL;
  197.          end; {VirtualWinOBJ}
  198.  
  199.  
  200.          So far so good, but the method Draw, inherited from StretchWinOBJ, will
  201.          only draw the window border and then clear the central area of the
  202.          window. VirtualWinOBJ needs to have its own Draw method. This method
  203.          will first call StretchWinOBJ.Draw to display the basic window, and
  204.          will then draw the visible part of the virtual window by calling the
  205.          virtual window's method PartDisplay. The new Draw method will then need
  206.          to update the vertical and horizontal scroll bars to accurately reflect
  207.          which portion of the virtual screen is visible.
  208.  
  209.          The inherited methods DrawHorizbar and DrawVertBar can be used to draw
  210.          the scroll bars. These methods are passed two longint parameters repre-
  211.          senting the current position and maximum possible position. For exam-
  212.          ple, the following statements will correctly update the StretchWinOBJ
  213.          scroll bars:
  214.  
  215.                   DrawHorizBar(vFirstChar,vScreenWidth);
  216.                   DrawVertBar(vTopLine,vScreenDepth
  217.  
  218.          Since the virtual screen method PartDisplay is mega-fast, we should be
  219.          able to refresh the window display during a stretch. This is achieved
  220.          by adding the method StretchRefresh. All StretchRefresh needs to do is
  221.          restore the visible portion of the virtual screen, and paint any area
  222.          of the window which may be beyond the virtual window boundary.
  223.  
  224.  
  225.  
  226. Extending Windows                                                           18-5
  227.  
  228. --------------------------------------------------------------------------------
  229.  
  230.          The inherit WinKey method will only process keystrokes which close,
  231.          move or stretch the window. StretchWinOBJ needs to have its own WinKey
  232.          method which calls the inherited one, and then processes any of the
  233.          scrolling keys like End, Home, PgUp, PgDn, etc. as well as any mouse
  234.          clicks on the scroll bars.
  235.  
  236.          Finally, a Go method could be added. This method would continuously get
  237.          user input and call WinKey until the user escaped or clicked on the
  238.          close icon.
  239.  
  240.          After all these enhancements, the declaration of the full-blown Virtu-
  241.          alWinOBJ would be as follows:
  242.  
  243.          VirtualWinOBJ = object (StretchWinOBJ)
  244.             vScreen: ScreenPtr;
  245.             vTopLine: integer;
  246.             vFirstChar: integer;
  247.             vScreenWidth: byte;
  248.             vScreenDepth: byte;
  249.             {Methods...}
  250.             constructor Init;
  251.             procedure   SetScreenXY(X,Y:byte);
  252.             procedure   AssignVirtualScreen(var Scr:ScreenOBJ);
  253.             procedure   RefreshWindow;
  254.             procedure   ScrollDown;
  255.             procedure   ScrollUp;
  256.             procedure   ScrollLeft;
  257.             procedure   ScrollRight;
  258.             procedure   ScrollTop;
  259.             procedure   ScrollBottom;
  260.             procedure   ScrollHome;
  261.             procedure   ScrollEnd;
  262.             procedure   ScrollJump(Vert:boolean; X,Y:byte);
  263.             procedure   ScrollPgUp;
  264.             procedure   ScrollPgDn;
  265.             procedure   Go;
  266.             procedure   StretchRefresh;                       VIRTUAL;
  267.             procedure   Winkey(var K:word;var X,Y:byte);      VIRTUAL;
  268.             procedure   Draw;                                 VIRTUAL;
  269.             destructor  Done;                                 VIRTUAL;
  270.          end; {VirtualWinOBJ}
  271.  
  272.  
  273.          The on-disk unit file EXTWIN.PAS contains the complete implementation
  274.          of the VirtualWinOBJ method. You might print out this unit and review
  275.          the detailed statements to gain a thorough understanding of how Virtu-
  276.          alWinOBJ functions. Since all the work has been done for you, you might
  277.          consider creating a descendant of VirtualWinOBJ which includes on-line
  278.          help, printing, etc.
  279.  
  280.  
  281.  
  282.  
  283. 18-6                                                       Extending the Toolkit
  284.  
  285. --------------------------------------------------------------------------------
  286.  
  287. Using VirtualWinOBJ
  288.  
  289.          Using the new VirtualWinOBJ object is a snap. The demo program EXT-
  290.          DEM4.PAS, listed below, shows how the object could be used to display
  291.          the good 'ole ASCII table. Figure 18.1 shows the display generated by
  292.          the demo program.
  293.  
  294.  
  295.  
  296.          Program ExtendedDemoFour;
  297.          {EXTDEM4}
  298.  
  299.          Uses DOS,CRT,
  300.               totINPUT, totFAST, totSTR, extWIN;
  301.  
  302.          var
  303.             ASCIIdata: ScreenOBJ;
  304.             AscWin: VirtualWinOBJ;
  305.  
  306.          procedure BuildASCIIscreen;
  307.          {creates the virtual screen}
  308.          var
  309.             I,J : integer;
  310.             Str:string;
  311.          begin
  312.             with ASCIIdata do
  313.             begin
  314.                Init;
  315.                Create(65,32,71);
  316.                for I := 0 to 31 do
  317.                begin
  318.                   Str := '';
  319.                   for J := 0 to 7 do
  320.                   begin
  321.                      Str := Str+' '+
  322.                             padright(inttostr(I+32*J),3,'0')+
  323.                             ' '+char(I+32*J)+' ';
  324.                      if J <> 7 then
  325.                         Str := Str + '│';
  326.                   end;
  327.                   WritePlain(1,succ(I),Str);
  328.                end;
  329.                for J := 0 to 7 do
  330.                    Attrib(6+J*8,1,6+J*8,32,78);
  331.             end;
  332.          end; {BuildASCIIscreen}
  333.  
  334.          begin
  335.             Screen.Clear(white,'░'); {paint the screen}
  336.             Key.SetFast;
  337.             BuildASCIIscreen;
  338.             with AscWin do
  339.  
  340.  
  341.  
  342. Extending Windows                                                           18-7
  343.  
  344. --------------------------------------------------------------------------------
  345.  
  346.             begin
  347.                Init;
  348.                SetTitle(' ASCII Table ');
  349.                AssignVirtualScreen(ASCIIdata);
  350.                Go;
  351.                ASCIIdata.Done;
  352.                Done;
  353.             end;
  354.          end.
  355.  
  356.  
  357.  
  358. Figure 18.1                                                             [SCREEN]
  359. Using
  360. VirtualWinOBJ
  361.