require Tk;
package Filedisplay;
@ISA = (Listbox);
sub new {
    local($w) = shift;
    local($f1) = Frame::new($w);
    local($xscroll) = Scrollbar::new($f1, "-orient" => "horizontal");
    local($f2) = Frame::new($f1);
    local($yscroll) = Scrollbar::new($f2, "-orient" => "vertical");
    local($l) = Listbox::new($f2, "-setgrid" => 1,
                                  "-yscrollslave" => $yscroll,
                                  "-yscrollmethod" => "set",
                                  "-xscrollslave" => $xscroll,
                                  "-xscrollmethod" => "set");
    $xscroll->configure("-slave" => $l, "-method" => "xview");
    $yscroll->configure("-slave" => $l, "-method" => "yview");
    Tk::tkpack $xscroll, "-fill" => "x";
    Tk::tkpack $yscroll, "-side" => "left", "-fill" => "y";
    Tk::tkpack $l, "-side" => "left", "-fill" => "both", "-expand" => "y";
    Tk::tkpack $f2, "-fill" => "both", "-expand" => "y";
    return bless({Listbox => $l, Default => $f1});
}

sub showfile {
    local($w, $file) = @_;
    local($i);
    
    if ($file ne $w->{"filename"}) {
        $w->{"filename"} = $file;
	*dbline = "::_<$file";
	$w->{"lines"} = \@dbline;
	$w->{"actions"} = \%dbline;
    }
    $w->tkdelete(0, "end");
    for($i = 1; $i <= $#dbline; $i++) {
	$w->insert("end", sprintf("%5d %s", $i, $dbline[$i]));
    }
    1;
}

sub showline {
    local($w, $line) = @_;
    local($max, $window, $edge, $ytop, $ynew);

    $max = $#{$w->{"lines"}};
    $line--;                 # listboxes indexed from zero
    $w->selectclear;
    $w->selectfrom($line);
    $ytop = $w->nearest(0);
#    $window = $w->configure("-geometry");
#    $window =~ /x\d*$/;
    $window = $w->nearest(10000) - $ytop + 1;
    $edge = int($window / 4);
    if ($line < $ytop + $edge || $line > $ytop + $window - $edge) {
        $ynew = $line - int($window / 2);       # centre line in listbox
        if ($ynew < 0) {
            $ynew = 0;
        } elsif ($ynew > $max - 1) {
            $ynew = $max - 1;
        } 
        $w->yview($ynew) if $ynew != $ytop;
    }
    1;
}
