home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / packer / arc / arctool / tppick.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-09-29  |  7.2 KB  |  248 lines

  1. {
  2.                        F i l e    I n f o r m a t i o n
  3.  
  4. * DESCRIPTION
  5. General-purpose scrolling window unit.
  6.  
  7. * ASSOCIATED FILES
  8. TPDIR.PAS
  9. DEMO1.PAS
  10. DEMO1.EXE
  11. TPDIR.TPU
  12. TPPICK.PAS
  13. TPPICK.TPU
  14. }
  15. {
  16. Copyright (c) 1987 by TurboPower Software. May be freely used by and
  17. distributed to owners of Turbo Professional 4.0.
  18.  
  19. See TPDir for an example of using the TPPick unit.
  20. }
  21.  
  22. {$R-,I-,S-,V-}
  23.  
  24. unit TPPick;
  25.   {-Manage scrolling pick windows}
  26.  
  27. interface
  28.  
  29. uses
  30.   TPString,
  31.   TPCrt,
  32.   TPWindow;
  33.  
  34. type
  35.   CharSet = set of Char;
  36.  
  37. function PickWindow
  38.   (StringFunc : pointer;     {Pointer to function to return each item string}
  39.    NumItems : Word;          {Number of items to pick from}
  40.    XLow, YLow : Byte;        {Window coordinates, including frame if any}
  41.    XHigh, YHigh : Byte;      {Window coordinates, including frame if any}
  42.    DrawFrame : Boolean;      {True to draw a frame around window}
  43.    WindowAttr : Byte;        {Video attribute for body of window}
  44.    FrameAttr : Byte;         {Video attribute for frame}
  45.    HeaderAttr : Byte;        {Video attribute for header}
  46.    SelectAttr : Byte;        {Video attribute for selected item}
  47.    Header : string;          {Title for window}
  48.    PickSet : CharSet;        {Selection characters}
  49.    var Choice : Word;        {The item selected, in the range 1..NumItems}
  50.    var PickChar : Char       {Character used to perform selection}
  51.    ) : Boolean;              {True if PickWindow was successful}
  52.  
  53.   {-Display a window, let user scroll around in it, and return choice.
  54.     Choice returned is in the range 1..NumItems.
  55.     PickChar is an element of PickSet.
  56.     }
  57.  
  58.   {=========================================================================}
  59.  
  60. implementation
  61.  
  62. var
  63.   XSize : Byte;              {Active width of pick window (no frame)}
  64.   YSize : Byte;              {Active height of pick window}
  65.   W : WindowPtr;             {Window descriptor of pick window}
  66.   PickFunc : pointer;        {Pointer to function that returns each string}
  67.  
  68.   function GetString(Item : word) : string;
  69.     {-Return the name of each item}
  70.   inline($FF/$1E/>PickFunc); {CALL DWORD PTR [>PickFunc]}
  71.  
  72.   function PickWindow
  73.     (StringFunc : pointer;     {Pointer to function to return each item string}
  74.      NumItems : Word;          {Number of items in PickArray}
  75.      XLow, YLow : Byte;        {Window coordinates, including frame if any}
  76.      XHigh, YHigh : Byte;      {Window coordinates, including frame if any}
  77.      DrawFrame : Boolean;      {True to draw a frame around window}
  78.      WindowAttr : Byte;        {Video attribute for body of window}
  79.      FrameAttr : Byte;         {Video attribute for frame}
  80.      HeaderAttr : Byte;        {Video attribute for header}
  81.      SelectAttr : Byte;        {Video attribute for selected item}
  82.      Header : string;          {Title for window}
  83.      PickSet : CharSet;        {Selection characters}
  84.      var Choice : Word;        {The item selected, in the range 1..NumItems}
  85.      var PickChar : Char       {Character used to perform selection}
  86.      ) : Boolean;              {True if PickWindow was successful}
  87.   var
  88.     SaveBreak : Boolean;
  89.     Done : Boolean;
  90.     KW : Word;
  91.     Row : Byte;
  92.     Correction : integer;
  93.  
  94.     procedure DrawItem(ItemNum : Word; Row, Attr : Byte);
  95.       {-Draw the specified item}
  96.     begin
  97.       FastWriteWindow(pad(' '+GetString(ItemNum), XSize), Row, 1, Attr);
  98.     end;
  99.  
  100.     procedure DrawPage(Top : Word);
  101.       {-Draw a full page of items, starting with Top}
  102.     var
  103.       I : Word;
  104.     begin
  105.       for I := 1 to YSize do
  106.         DrawItem(Pred(Top+I), I, WindowAttr);
  107.     end;
  108.  
  109.   begin
  110.  
  111.     {Assume failure}
  112.     PickWindow := False;
  113.  
  114.     {Validate item information}
  115.     if (NumItems = 0) or (StringFunc = nil) then
  116.       Exit;
  117.  
  118.     {Initialize variables we'll use for display}
  119.     PickFunc := StringFunc;
  120.     if DrawFrame then
  121.       Correction := -1
  122.     else
  123.       Correction := +1;
  124.     XSize := XHigh-XLow+Correction;
  125.     YSize := YHigh-YLow+Correction;
  126.     if YSize > NumItems then begin
  127.       YSize := NumItems;
  128.       YHigh := YLow+NumItems-Correction;
  129.     end;
  130.  
  131.     {Initialize the window}
  132.     if not MakeWindow(W, XLow, YLow, XHigh, YHigh,
  133.                       DrawFrame, True, False,
  134.                       WindowAttr, FrameAttr, HeaderAttr,
  135.                       Header) then Exit;
  136.  
  137.     {Display the window and turn off the cursor}
  138.     if not DisplayWindow(W) then
  139.       Exit;
  140.     HiddenCursor;
  141.     SaveBreak := CheckBreak;
  142.     CheckBreak := False;
  143.  
  144.     {Initial item is the first one}
  145.     Choice := 1;
  146.     Row := 1;
  147.     DrawPage(Choice);
  148.  
  149.     {Loop getting characters}
  150.     Done := False;
  151.     repeat
  152.       {Highlight the selected entry}
  153.       DrawItem(Choice, Row, SelectAttr);
  154.       {Get a command}
  155.       KW := ReadKeyWord;
  156.  
  157.       case KW of
  158.         $4700 :              {Home}
  159.           if Choice > 1 then begin
  160.             Choice := 1;
  161.             Row := 1;
  162.             DrawPage(Choice);
  163.           end;
  164.  
  165.         $4800 :              {Up arrow}
  166.           if Choice > 1 then begin
  167.             {Erase the last choice}
  168.             DrawItem(Choice, Row, WindowAttr);
  169.             {Move to previous item}
  170.             Dec(Choice);
  171.             if Row = 1 then
  172.               {Scroll}
  173.               InsLine
  174.             else
  175.               {Move selection bar}
  176.               Dec(Row);
  177.           end else if YSize = NumItems then begin
  178.             {Wrap to end}
  179.             Choice := NumItems;
  180.             Row := YSize;
  181.             DrawPage(Succ(Choice-Row));
  182.           end;
  183.  
  184.         $4900 :              {PgUp}
  185.           if Choice > 1 then begin
  186.             if Choice > YSize then
  187.               Dec(Choice, YSize)
  188.             else
  189.               Choice := 1;
  190.             Row := 1;
  191.             DrawPage(Choice);
  192.           end;
  193.  
  194.         $4F00 :              {End}
  195.           if Choice < NumItems then begin
  196.             Choice := NumItems;
  197.             Row := YSize;
  198.             DrawPage(Succ(Choice-Row));
  199.           end;
  200.  
  201.         $5000 :              {Down arrow}
  202.           if Choice < NumItems then begin
  203.             {Erase the last choice}
  204.             DrawItem(Choice, Row, WindowAttr);
  205.             {Move to next item}
  206.             Inc(Choice);
  207.             if Row = YSize then
  208.               {Scroll}
  209.               DelLine
  210.             else
  211.               {Move selection bar}
  212.               Inc(Row);
  213.           end else if YSize = NumItems then begin
  214.             {Wrap to begin}
  215.             Choice := 1;
  216.             Row := 1;
  217.             DrawPage(Choice);
  218.           end;
  219.  
  220.         $5100 :              {PgDn}
  221.           if Choice < NumItems then begin
  222.             Inc(Choice, YSize);
  223.             if Choice > NumItems then
  224.               Choice := NumItems;
  225.             Row := YSize;
  226.             DrawPage(Succ(Choice-Row));
  227.           end;
  228.  
  229.       else
  230.         {See if a pick character}
  231.         PickChar := Char(lo(KW));
  232.         if PickChar in PickSet then
  233.           Done := True;
  234.       end;
  235.     until Done;
  236.  
  237.     {Restore the screen and deallocate the window}
  238.     W := EraseTopWindow;
  239.     DisposeWindow(W);
  240.     CheckBreak := SaveBreak;
  241.  
  242.     {If we get to here, all was well}
  243.     PickWindow := True;
  244.   end;
  245.  
  246. end.
  247. 
  248.