{$i-}

program MENU (input, output);

uses
  crt, CORE;

var
  T, T2   : text;
  KEUZE   : byte;
  TEKST   : string;
  TOTAL   : byte;
  ITEM    : array[-1..45] of string;

procedure Add2Menu (TEKST :  string);
  begin
    if TOTAL < 22 then begin
      inc (TOTAL, 1);
      ITEM[TOTAL]:= TEKST;
    end;
  end;

function OPENMENU : byte;
  var
    CH            : char;
    C10, C11,C12  : byte;
    C13           : byte;
    LONGEST       : byte;
  begin
    KEUZE:= 1;
    LONGEST:= length (ITEM[1]);
(*    for C10:= 2 to TOTAL do
      if length (ITEM[C10]) > LONGEST then
        LONGEST:= length (ITEM[C10]);
    for C11:= 1 to TOTAL do
      for C12:= length(ITEM[C12])+1 to LONGEST do
        ITEM[C11]:= ITEM[C11] + char (32);
    *)
    textcolor (WHITE);
    repeat
      fillchar (mem[$A000:0], 64000, LIGHTGRAY);
      for C13:= 1 to TOTAL do begin
        if (KEUZE = C13) then
          textbackground (BROWN)
            else
              textbackground (LIGHTGRAY);
        gotoxy (5, 1+C13);
        PRINTMCGA (ITEM [C13]);
      end;
      CH:= readkey;
      if CH = #0 then begin
        CH:= readkey;
        case CH of
         'H': if KEUZE > 1 then dec (KEUZE, 1);
         'P': if KEUZE < TOTAL-1 then inc (KEUZE, 1);
        end;
      end;
    until CH in [#13, #27];
    OPENMENU:= KEUZE;
  end;

begin
  TOTAL:= 0;
  assign (T, 'INPUT.DAT');
  reset (T);
  if ioresult <> 0 then begin
    writeln;
    writeln ('Fout, vooraleer u MENU[.EXE] kunt gebruiken,');
    writeln;
    writeln ('  moet u eerst de keuzes als zinnen in "INPUT.DAT" schrijven,');
    writeln ('   met "EDIT.COM", of een andere tekst editor voor ms/dos,');
    writeln ('  of windows.');
    writeln;
    writeln ('error code (113)');
    halt (113);
  end;
  readln (T, TEKST);
  while not (eof(T)) do begin
    Add2Menu (TEKST);
    readln (T, TEKST);
  end;
  close (T);
  MCGAINIT;
  KEUZE:= OPENMENU;
  textmode (CO80);
  textbackground (BLACK);
  textcolor (LIGHTGRAY);
  clrscr;
  writeln (KEUZE);
  assign (T2, 'OUTPUT.DAT');
  rewrite (T2);
  writeln (T2, KEUZE);
  close (T2);
end.