home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TOOL_INC.ZIP / PULL.INC < prev    next >
Encoding:
Text File  |  1988-01-29  |  6.0 KB  |  221 lines

  1.  
  2. (*
  3.  * pull - utility library for simple "pull-down" windows
  4.  *        uses functions in popup.inc
  5.  *
  6.  * written by Samuel H. Smith, 11-nov-87
  7.  *
  8.  *)
  9.  
  10. const
  11.    max_pulldown = 10;
  12.  
  13.    quit_sel = #255;      {special select value to quit top menu}
  14.    divider_entry = -254; {special action value for divider lines}
  15.    unused_entry  = -255; {special action value for unused pulldown entries}
  16.    #define UNUSED_ENTRY  title: ''; action: unused_entry
  17.  
  18. type
  19.    pulldown_entry = record
  20.       title:  string[40];
  21.       action: integer;
  22.    end;
  23.  
  24.    pulldown_rec = record
  25.       border:    border_styles;
  26.       border_fg: byte;
  27.       border_bg: byte;
  28.       text_fg:   byte;
  29.       text_bg:   byte;
  30.       select_fg: byte;
  31.       select_bg: byte;
  32.       ainit:     integer;
  33.       aexit:     integer;
  34.       line:      array[1..max_pulldown] of pulldown_entry;
  35.    end;
  36.  
  37.  
  38. function pulldown_action(pullno:   integer;   (* pulldown menu number *)
  39.                          entry:    integer;   (* entry in pulldown menu *)
  40.                          action:   integer;   (* action code *)
  41.                          var sel:  char)      (* select key *)
  42.                              : boolean;       (* true to force menu exit *)
  43.    {pulldown action routine; called when a pulldown entry is selected}
  44. forward;
  45.  
  46. procedure pulldown_init(pullno:    integer;
  47.                         action:    integer;
  48.                         var sel:   char);
  49.    {pulldown init routine; called when a pulldown menu is opened}
  50. forward;
  51.  
  52. procedure pulldown_exit(pullno:    integer;
  53.                         action:    integer;
  54.                         var sel:   char);
  55.    {pulldown exit routine; called when a pulldown menu is closed}
  56. forward;
  57.  
  58. function pulldown_key   (pullno:   integer;   (* pulldown menu number *)
  59.                          entry:    integer;   (* entry in pulldown menu *)
  60.                          var sel:  char)      (* select key *)
  61.                              : boolean;       (* true to force menu exit *)
  62.    {process unknown keys}
  63. forward;
  64.  
  65.  
  66.  
  67. procedure pulldown(topx,topy:   integer;
  68.                    pullno:      integer;
  69.                    var pull:    pulldown_rec;
  70.                    var sel:     char);
  71.    {pulldown window processor; display the pulldown window and
  72.     select an entry from it}
  73.  
  74.    procedure display_entry(entry: integer);
  75.    begin
  76.       gotoxy(1,entry);
  77.       disp(' '+pull.line[entry].title);
  78.       clreol;
  79.       gotoxy(2,entry);
  80.    end;
  81.  
  82.    procedure display_pulldown;
  83.       {open a pulldown window at top-left x and y location.
  84.        use the pull record to describe the options}
  85.    var
  86.       i:            integer;
  87.       longest:      integer;
  88.       active:       integer;
  89.       botx,boty:    integer;
  90.  
  91.    begin
  92.  
  93.    (* determine longest selection title *)
  94.       active := 0;
  95.       longest := 0;
  96.       for i := 1 to max_pulldown do
  97.          with pull.line[i] do
  98.          begin
  99.             if LEN(title) > longest then
  100.                longest := LEN(title);
  101.             if action <> unused_entry then
  102.                inc(active);
  103.          end;
  104.  
  105.    (* determine bottom right location *)
  106.       botx := topx + longest + 4;
  107.       boty := topy + active + 1;
  108.       while botx > 79 do
  109.       begin
  110.          dec(topx);
  111.          dec(botx);
  112.       end;
  113.       while boty > 24 do
  114.       begin
  115.          dec(topy);
  116.          dec(boty);
  117.       end;
  118.  
  119.    (* draw the frame *)
  120.       window(1,1,80,25);
  121.       setcolor(pull.border_fg, pull.border_bg);
  122.       display_border(topx,topy,botx,boty,pull.border);
  123.  
  124.    (* define the new window and print option descriptions *)
  125.       window(topx+1,topy+1,botx-2,boty-1);
  126.       setcolor(pull.text_fg,pull.text_bg);
  127.       for i := 1 to active do
  128.          display_entry(i);
  129.    end;
  130.  
  131.  
  132.    procedure pick_pulldown;
  133.       {select an entry from a pulldown window.
  134.        the pulldown must already be on the display}
  135.    var
  136.       found: integer;
  137.       i:     integer;
  138.       entry: integer;
  139.  
  140.       procedure moveby(by: integer);
  141.       begin
  142.          repeat
  143.             entry := entry + by;
  144.             if entry > max_pulldown then
  145.                entry := 1
  146.             else if entry < 1 then
  147.                entry := max_pulldown;
  148.         until pull.line[entry].action >= 0;
  149.       end;
  150.  
  151.  
  152.    begin
  153.       (* pick the initial selection *)
  154.       entry := 0;
  155.       moveby(1);
  156.  
  157.       (* determine what user wants *)
  158.       repeat
  159.          found := 0;
  160.          setcolor(pull.select_fg,pull.select_bg);
  161.          display_entry(entry);
  162.  
  163.          sel := upcase(getkey);
  164.  
  165.          setcolor(pull.text_fg,pull.text_bg);
  166.          display_entry(entry);
  167.  
  168.          case sel of
  169.             ESC,
  170.             LEFT,
  171.             RIGHT:     exit;
  172.  
  173.             UP:        moveby(-1);
  174.             DOWN:      moveby(1);
  175.  
  176.             NEWLINE:   found := entry;
  177.  
  178.             else
  179.                begin
  180.                   (* test for capitalized letters *)
  181.                   for i := max_pulldown downto 1 do
  182.                   with pull.line[i] do
  183.                      if pos(sel, title) > 0 then
  184.                      begin
  185.                         entry := i;
  186.                         found := -1;
  187.                      end;
  188.  
  189.                   if found = 0 then
  190.                   begin
  191.                      if pulldown_key(pullno,entry,sel) then
  192.                         exit;
  193.                   end;
  194.                end;
  195.          end;
  196.  
  197.          (* an entry was found; select it and perform the action *)
  198.          if found > 0 then
  199.          begin
  200.             entry := found;
  201.             setcolor(pull.select_fg,pull.select_bg);
  202.             display_entry(entry);
  203.  
  204.             if pulldown_action(pullno,entry,
  205.                                pull.line[entry].action,sel) then
  206.                exit;
  207.          end;
  208.  
  209.       until true=false;
  210.    end;
  211.  
  212.  
  213. begin {pulldown}
  214.    pulldown_init(pullno,pull.ainit,sel);
  215.    display_pulldown;
  216.    pick_pulldown;
  217.    window(1,1,80,25);
  218.    pulldown_exit(pullno,pull.aexit,sel);
  219. end;
  220.  
  221.