{$symtab-,$pagesize:86,$linesize:131,
$title:'MENUIT.PAS -- Creates HELP Menus'}
{	COPYRIGHT @ 1982
	Jim Holtman and Eric Holtman
	35 Dogwood Trail
	Randolph, NJ 07869
	(201) 361-3395
}
module menus;
type
    menu_c = super array [1..*] of lstring(40);
    screen = array[1..25] of array[1..160] of byte;
    screen_buffer = array[1..4000] of byte;
var
    snapscreen : array[1..20] of ^screen_buffer;
    snapptr : integer;
    snapx, snapy : array[1..20] of integer;
value
    snapptr := 1;
{$include:'graph.inc'}
{ Two routine that can take a snapshot of screen w/cursor and
  then later restore the snapshot }
procedure pushscreen [public];
var
    x : ads of char;
    display_buffer_addr [external] : word;
begin
    x.s := display_buffer_addr;
    x.r := 0;
    new(snapscreen[snapptr]);
    movesl(x, ads snapscreen[snapptr]^, 4000);
    xrcurp(snapx[snapptr], snapy[snapptr]);
    snapptr := snapptr + 1;
end;
procedure popscreen [public];
var
    x : ads of char;
    display_buffer_addr [external] : word;
begin
    x.s := display_buffer_addr;
    x.r := 0;
    if (snapptr = 1) then return;
    snapptr := snapptr - 1;
    movesl(ads snapscreen[snapptr]^, x, 4000);
    xxmove(snapx[snapptr], snapy[snapptr]);
    dispose(snapscreen[snapptr]);
end;
procedure chattr(newattr : byte; y, sx, ex : integer);

var
    i,j : integer;
    scr : ads of screen;
    display_buffer_addr [external] : word;
begin
    scr.s := display_buffer_addr;
    scr.r := 0;
    for i := sx to ex do scr^[y,i*2] := newattr;
end;
procedure errormsg(y, att : integer);
var
    errmsg : lstring(80);
begin
    errmsg := '  Use arrows to make choice, then hit space bar. Use ESC to make "no choice"  ';
    xxmove(40 - ord(errmsg.len) div 2, y);
    xttywrt(errmsg, att);
end;
function menuit(var choices : menu_c; const title : lstring ) : integer [public];
var
    max_len : integer;
    max_items : integer;
    i,j,k : integer;
    x,y : integer;
    sx, sy : integer;
    scr : ads of screen;
    ch : char;

begin
    pushscreen;
    max_len := 2 + ord(title.len);
    for i := 1 to upper(choices) do begin
	if (choices[i].len = 0) then break;
	if (ord(choices[i].len) > max_len) then max_len := ord(choices[i].len);
    end;
    max_items := i-1;
    if (max_items = 0) then begin
	menuit := 0;
	return;
    end;
    max_len := max_len + 2;
    sx := 40 - ((max_len + 2) div 2);
    sy := 12 - ((max_items + 2) div 2);
    xxmove(sx,sy-2);
    xttywrt('É',7);
    for i := 1 to max_len do
	xttywrt('Í',7);
    xttywrt('»',7);
    xxmove(sx,sy-1);
    xwca(#700, max_len+1);
    xxmove(40 - (ord(title.len) div 2), sy-1);
    xttywrt(title, 7);
    xxmove(sx,sy-1);
    xttywrt('º',7);
    xxmove(sx+max_len+1, sy-1);
    xttywrt('º',7);
    xxmove(sx,sy);
    xttywrt('Ì',7);
    for i := 1 to max_len do
	xttywrt('Í',7);
    xttywrt('¹',7);
    for i := 1 to max_items do begin
	xxmove(sx,sy+i);
	xwca(#700, max_len+1);
	xxmove(sx,sy+i);
	xttywrt('º',7);
	xxmove(40 - (ord(choices[i].len) div 2), sy+i);
	xttywrt(choices[i], 7);
	xxmove(sx+max_len+1,sy+i);
	xttywrt('º',7);
    end;
    xxmove(sx,sy+1+max_items);
    xttywrt('È',7);
    for i := 1 to max_len do
	xttywrt('Í',7);
    xttywrt('¼',7);
    i := 1;
    chattr(#70, 1+i+sy, sx+2, sx+1+max_len);
    errormsg(sy+2+max_items, #70);
    while (xxinkey(ch) = 0) do begin end;
    while ((ch <> ' ') and (ch <> chr(27))) do begin
	case ord(ch) of
	72:
	    begin
		chattr(7, 1+i+sy, sx+2, sx+1+max_len);
		i := i - 1;
		if (i = 0) then i := max_items;
		chattr(#70, 1+i+sy, sx+2, sx+1+max_len);
	    end;
	80:
	    begin
		chattr(7, 1+i+sy, sx+2, sx+1+max_len);
		i := i + 1;
		if (i = max_items + 1) then i := 1;
		chattr(#70, 1+i+sy, sx+2, sx+1+max_len);
	    end;
	otherwise ;
	end;
	while (xxinkey(ch) = 0) do begin
	end;
    end;
    if (ch = chr(27)) then menuit := 0
    else menuit := i;
    popscreen;
end;
function menutree(const s : string) : integer [public];
var
    menus : array[1..20] of menu_c(10);
    i,j,k,l: integer;
    branches : array[1..20] of array[1..10] of integer;
    titles : array[1..20] of lstring(40);
    fil : text;
    buf : lstring(128);
    cur_menu, cur_choice : integer;
    ch : char;

begin
      assign(fil, s);
      reset(fil);
      while (not eof(fil)) do begin
	if (eoln(fil)) then begin
		readln(fil);
		read(fil, cur_menu);
		readln(fil, titles[cur_menu]);
		cur_choice := 1;
		cycle;
	end;
	readln(fil, branches[cur_menu, cur_choice], menus[cur_menu,cur_choice]);
	cur_choice := cur_choice + 1;
	menus[cur_menu, cur_choice].len := 0;
      end;
      cur_menu := 1; cur_choice := 1;
      while (cur_menu > 0) do begin
	cur_choice := menuit(menus[cur_menu], titles[cur_menu]);
	if (cur_choice > 0) then cur_menu := branches[cur_menu, cur_choice]
	else cur_menu := 0;
      end;
      menutree := -1 * cur_menu;
end;
end.
