{ After program displays the map, the user must press any key, then
choose some region and press left mouse button. Then the borders of the
region highlight. To repeat operation user must press any key. ESC-exit}
program Lena16_MapViewer;
uses crt, graph, dos;
type
    cell = array[1..5, 1..1000] of integer;
var
   f            : text;
   level        : integer;
   gd, gm       : integer;
   r            : registers;
   coor, z      : cell;
{***************************************************************************}
 function gchar(s: string; i: integer; var ch: char):char;
 { Read character from string}
 begin { gchar}
       if i <= length(s) then
          ch:=s[i];
       gchar:=ch
 end; { gchar}
{***************************************************************************}
 procedure addchar(var s: string; ch: char);
 { Add character to end of string}
 var
    i   : integer;
 begin { addchar}
       {$r-}
       s[0]:=succ(s[0]);
       i:=length(s);
       s[i]:=ch
       {$r+}
 end; { addchar}
{***************************************************************************}
 function readline(var f: text; var level: integer):boolean;
 { Read first line of data file}
 var
    s         : string;
    i, code   : integer;
    c         : char;
 begin { readline}
    readln(f, s);
    i:=1;
    repeat
          c:=gchar(s, i, c);
          i:=i+1
    until c='/';
    c:=gchar(s, i, c);
    {$i-}
    val(c, level, code);
    readline:=(code=0)
 end; { readline}
{***************************************************************************}
 procedure getdata(var f: text; var coor: cell; counter: integer);
 { Read lines containing data about area}
 var
    s           : string;
    s1          : array[1..5] of string;
    i, j        : integer;
    c           : char;
    code        : integer;
 begin { getdata}
       s:='';
       for j:=1 to 5 do s1[j]:='';
       readln(f, s);
       i:=1;
       { read sequences of numbers to string array}
       for j:=1 to 5 do begin
           repeat
                c:=gchar(s, i, c);
                i:=i+1
           until c <> ' ';
           i:=i-1;
           while (gchar(s, i, c) in ['0'..'9']) and (i<=length(s)) do begin
                 addchar(s1[j], c);
                 i:=i+1
           end
       end;
       { Turn strings to numbers - coordinates}
       val(s1[1], coor[1, counter], code);
       val(s1[2], coor[2, counter], code);
       val(s1[3], coor[3, counter], code);
       val(s1[4], coor[4, counter], code);
       val(s1[5], coor[5, counter], code)
 end; { getdata}
{***************************************************************************}
 procedure do_level(var f: text; var coor: cell);
 { Draw areas which refer to all levels}
 var
    index, x1, y1, x2, y2       : integer;
    color                       : word;
    i                           : integer;
 begin { do_level}
       i:=1;
       while not eof(f) do begin
          getdata(f, coor, i);
          color:=coor[1, i];
          setcolor(color);
          setfillstyle(1, color);
          rectangle(coor[2, i], coor[3, i], coor[4, i], coor[5, i]);
          if (coor[2, i] <> coor[4, i]) and (coor[3, i] <> coor[5, i]) then
          floodfill(coor[2, i]+2, coor[3, i]+2, color);
          i:=i+1
       end
 end; { do_level}
{***************************************************************************}
 procedure work_with_mouse(var r: registers);
 { Work with mouse when user uses it}
 var
    i, j, n, k  : integer;
 begin
      r.ax:=0000;    { check if the driver is installed}
      intr($33, r);
      r.ax:=0001;    { Show mouse cursor on screen}
      intr($33, r);
      r.ax:=0003;
      repeat         { Wait until user clicks left button, get mouse`s
                       coordinates}
            intr($33, r)
      until r.bx=1;
      i:=1;
      k:=0;
      { check if mouse is in some region}
      repeat
            if (r.cx >= coor[2, i]) and (r.cx <= coor[4, i]) and
               (r.dx >= coor[3, i]) and (r.dx <= coor[5, i])
            then
                k:=coor[1, i];
            i:=i+1
      until (k <> 0) or (i>1000);
      n:=0;
      { get all rectangulars which refer to this region}
      if k <> 0 then for i:=1 to 1000 do
          if coor[1, i]=k then begin
             n:=n+1;       { n- number of blocks of current region}
             for j:=1 to 5 do z[j, n]:=coor[j, i];
          end;
      k:=1;
      { highlight border of region}
      if n <> 0 then
         repeat
               setcolor(z[1, k]-1);
               line(z[2, k], z[3, k], z[2, k], z[5, k]);
               line(z[4, k], z[3, k], z[4, k], z[5, k]);
               line(z[2, k], z[3, k], z[4, k], z[3, k]);
               line(z[2, k], z[5, k], z[4, k], z[5, k]);
               n:=n-1;
               k:=k+1
         until n=0;
      repeat until keypressed
 end;
{***************************************************************************}
begin {}
      clrscr;
      assign(f, 'c:/icp/penta.mdf');
      reset(f);
      readline(f,level);                { read first line from file}
      gd:=9;
      initgraph(gd, gm, 'c:/bp/bgi');
      clearviewport;
      outtextxy(20, 190, 'Press any key and choose the region');
      do_level(f, coor);                { draw all rectangulars}
      while readkey <> chr(27) do
            work_with_mouse(r);               { highlight region}
end.