#!/usr/local/bin/perl

use Tk;
#----------------------------------------------------------------------
# ץȤ֤Ƥǥ쥯ȥμ
#----------------------------------------------------------------------
$HERE = $0;
$HERE =~ s/[^\/]+$//;
$HERE =~ s/\/+$//;
$HERE = "." if !$HERE;
#----------------------------------------------------------------------
# Option Viewer
#----------------------------------------------------------------------
sub ViewAllOptions {
    my $main = new MainWindow();
    $main->option('add','*background' => 'bisque'); 
    $main->option('add','*activeBackground' => 'bisque2');
    $main->option('add','*selectBackground' => 'cyan2');
    $main->option('add','*highlightThickness' => '0');
    $main->option('add','*padY' => 3);
    $main->option('add','*padX' => 3);
    $main->withdraw();
    $main->Label(-text => 'Option variables',
		 qw/-width 70 -height 2/
		 )->pack(qw/-anchor nw -fill x/);

    my $tmp = $main->Frame(qw/-bg bisque -bd 10/);
    my $fl = $tmp->Frame(qw/-bg bisque/);
    my $fr = $tmp->Frame(qw/-bg bisque/);

    my ($key,$val);
    while (($key, $val) = each %OPT) {
	$fl->Label(qw/-anchor w/,
		   -text => $key)->pack(qw/-fill x -anchor e/);
	$fr->Label(qw/-bg white -anchor w/,
		   -textvariable => \$OPT{$key})->pack(qw/-fill x -anchor w/);
    }
    $fl->pack(qw/-side left -fill both/);
    $fr->pack(qw/-side left -fill both -expand y/);
    $tmp->pack(qw/-fill both -expand y/);

    my $tmp = $main->Frame(qw/-bg bisque -bd 15/);
    $tmp->Button(qw/-text OK -relief groove -bd 4/,
		-command => sub {$main->destroy()}
		 )->pack();
    $tmp->pack(qw/-fill x -side bottom/);
    $main->deiconify();
}
#----------------------------------------------------------------------
# Input dialog
#----------------------------------------------------------------------
sub InputDialog {
    my $msg = shift;
    my @ret = @_;
    my $InputDOK;
    
    my $main = new MainWindow();
    $main->option('add','*background' => 'bisque'); 
    $main->option('add','*activeBackground' => 'bisque2');
    $main->option('add','*selectBackground' => 'cyan2');
    $main->option('add','*highlightThickness' => '0');
    $main->option('add','*padY' => 3);
    $main->option('add','*padX' => 3);
    $main->withdraw();
    $main->Label(-textvariable => \$msg,
		 qw/-width 70 -height 2/
		 )->pack(qw/-anchor nw -fill x/);

    my $tmp = $main->Frame(qw/-bg bisque -bd 10/);
    my $fl = $tmp->Frame(qw/-bg bisque/);
    my $fr = $tmp->Frame(qw/-bg bisque/);
    for (my $i = 0; $i < $#ret; $i += 2) {
	$fl->Label(qw/-anchor e/,
		   -text => $ret[$i])->pack(qw/-fill x -anchor e/);
	$fr->Entry(qw/-bg white -bd 4 -relief ridge/,
		   -textvariable => \$ret[$i+1])->pack(qw/-fill x -anchor w/);
    }
    $fl->pack(qw/-side left -fill both/);
    $fr->pack(qw/-side left -fill both -expand y/);
    $tmp->pack(qw/-fill both -expand y/);

    my $tmp = $main->Frame(qw/-bg bisque -bd 15/);
    $tmp->Button(qw/-text OK -relief groove -bd 4/,
		-command => sub {$InputDOK = 1}
	     )->pack(qw/-side left/);
    $tmp->Button(qw/-text Cansel -relief groove -bd 4/,
		 -command => sub {$InputDOK = 0})->pack(qw/-side right/);
    $tmp->pack(qw/-fill x -side bottom/);

    undef $InputDOK;

    $main->deiconify();
    $main->waitVariable(\$InputDOK);
    $main->destroy();

    return @ret if $InputDOK == 1;

    return undef;
}
#----------------------------------------------------------------------
# Information Window ɽ
#----------------------------------------------------------------------
sub DispInfoWin {
    my $main = new MainWindow();
    $main->wm('title', "information");
    # OLD Tcl 饤ʿ
    $main->option('add','*background' => 'bisque'); 
    $main->option('add','*activeBackground' => 'bisque2');
    $main->option('add','*selectBackground' => 'cyan2');
    $main->option('add','*highlightThickness' => '0');
    $main->option('add','*padY' => 3);
    $main->option('add','*padX' => 3);

    my $info = <<EOS;
            X IISA: IISA GUI for X11
                  version 0.2
Copyright (C)1998, T.NAKAYAMA, T.YAMASHITA, K.TAMANO
                     NAIST
EOS

    my $txt = $main->Text(qw/-relief flat -width 52 -height 5/);
    $txt->insert('end', $info);
    $txt->pack(qw/-fill both -expand 1/);
    $main->Button(qw/-text ok/, -command => [destroy, $main]
		  )->pack(qw/-fill x/);
}
#----------------------------------------------------------------------
# եɽ
#----------------------------------------------------------------------
sub DispFile {
    shift;
    my $fname = shift;
    
    my $main = new MainWindow();
    $main->wm('title', "file:$fname");
    # OLD Tcl 饤ʿ
    $main->option('add','*background' => 'bisque'); 
    $main->option('add','*activeBackground' => 'bisque2');
    $main->option('add','*selectBackground' => 'cyan2');
    $main->option('add','*highlightThickness' => '0');
    $main->option('add','*padY' => 3);
    $main->option('add','*padX' => 3);

    my $f = $main->Frame();
    $f->pack(qw/-fill x/);
    $f->Button(qw/-text Close/,
	       -command => [sub{
		   $main = shift;
		   $main->destroy;
	       }, $main])->pack(qw/-side left/);

    my $txt = $main->Text();
    my $scr   = $main->Scrollbar(-command => ['yview', $txt]);
    $txt->configure(-yscrollcommand => ['set', $scr]);
    $scr->pack(qw/-side left -fill y/);
    $txt->pack(qw/-side left -fill both -expand y/);

    my ($key,$str,$line);
    open (FD, "$OPT{filter} $fname|");
    $line = 0;
    while (<FD>) {
	$txt->insert('end', $_);
	$str = $_;
	$line++;
	foreach $key (@KEYS) {
	    if ($str =~ /$key/) {
		$txt->tag('add', 'mstr', "$line.0", "$line.end");
	    }
	}
    }
    $txt->tag('configure', 'mstr', -background => 'pink');
    close(FD);
}
#----------------------------------------------------------------------
# 롼
#----------------------------------------------------------------------
sub Search {
    open (RET, "$OPT{iisa} -f $OPT{idxfile} '$KeyWord' |");

    $rsltT->delete('0.0', 'end');

    my $ret;
    my $count = 0;
    $_ = <RET>; /\[(.*)\/.*\] = .* \(found (\d+) from \d+ files\)$/;
    my $num = $2;
    $_ = $1; s/\s+/ /g;
    @KEYS = split;
    $STOP_SIG = 0; $MSG = '';
    while (<RET>) {
	if (++$count % 100 == 0) {
	    $MSG .= ">";
	    if ($count % 1000 == 0) {
		$MSG = '';
	    }
	    $main->update;
	    if ($STOP_SIG) {
		$MSG = ""; last;
	    }
	}
	if (/^----------------------------/) {
	    if (/-1\n$/) {
		$MSG = "$num︫Ĥޤ";
		last;
	    }
	    $ret = <RET>;
	    $rsltT->insert('end', "\n");
	    $rsltT->insert('end', $ret);
	    $count++;

	    chomp $ret;
	    $rsltT->tag('add', $ret, "$count.0", "$count.end");
	    $rsltT->tag('configure', $ret, -background => 'pink');
	    $rsltT->tag('bind',$ret,
		      '<Enter>' => [sub{
			  my (@e) = @_;
			  $e[0]->tag('configure',$e[1],
				     -background => 'yellow');
		      },$ret]);
	    $rsltT->tag('bind',$ret,
		      '<Leave>' => [sub{
			  my (@e) = @_;
			  $e[0]->tag('configure',$e[1],
				     -background => 'pink');
		      },$ret]);
	    $rsltT->tag('bind',$ret,
		      '<Button-1>' => [sub{DispFile(@_)},$ret]);
	} else {
	    $rsltT->insert('end', $_);
	}
    }
    close(RET);
}
#----------------------------------------------------------------------
# ץ
#----------------------------------------------------------------------
$OPT{idxfile} = "MKIND";
$OPT{filter} = "cat";
$OPT{iisa} = "$HERE/iisa";
$OPT{logo} = "$HERE/sufary-banner-s.gif";
# ץ
while ($_ = shift @ARGV) {
    /^\s*$/ && next;
    if (s/^-h//)    { $HELP_MODE = 1; next; }
    if (s/^-iisa//) { $OPT{'iisa'} = shift @ARGV; next; }
    if (s/^-filter//){ $OPT{'filter'} = shift @ARGV; next; }
    if (s/^-LOGO//){ $OPT{'logo'} = shift @ARGV; next; }
    if (s/^-f//)    { $OPT{'idxfile'} = shift @ARGV; next; }
}
# إפν
if ($HELP_MODE) {
    print STDERR
	"xiisa: GUI for IISA\n",
	"   usage: xiisa (options..)\n",
	" options:\n",
	"   -f ...     : set input file [$OPT{'idxfile'}]\$\n",
	"   -filter ...: set filter for viewer [$OPT{'filter'}]\$\n",
	"   -iisa ...  : use [$OPT{'iisa'}]\n",
	"   -help      : shows this help\n",
	" <dir> = [$OPT{'dir'}]\n";
    exit(1);
}
#----------------------------------------------------------------------
# ᥤ󥦥ɥ
#----------------------------------------------------------------------
$main = new MainWindow();
$main->wm('title', 'X IISA');
# OLD Tcl 饤ʿ
$main->option('add','*background' => 'bisque'); 
$main->option('add','*activeBackground' => 'bisque2');
$main->option('add','*selectBackground' => 'cyan2');
$main->option('add','*highlightThickness' => '0');
$main->option('add','*padY' => 3);
$main->option('add','*padX' => 3);
#----------------------------------------------------------------------
# ˥塼
#----------------------------------------------------------------------
my $menu_frame = $main->Frame(qw/-relief raised -bd 2/);
$menu_frame->pack(qw/-fill x/);
# ե˥塼
my $m_file = $menu_frame->Menubutton(qw/-text File/);
$m_file->command(-label => 'Change index',
		 -command => sub {
		     my @ret = &InputDialog("Change Index",
					    "Index name:", $OPT{'idxfile'});
		     if (defined $ret[1]) {
			 $OPT{'idxfile'} = $ret[1];
			 $MSG = "set index filename [$OPT{'idxfile'}]";
		     }
		 });
$m_file->separator();
$m_file->command(qw/-label Exit/,
		 -command => [sub{exit}]);
$m_file->pack(qw/-side left/);
# ץ
my $m_opt = $menu_frame->Menubutton(qw/-text Options/);
$m_opt->command(-label => 'Set Viewer filter',
		 -command => sub {
		     my @ret = &InputDialog("Viewer filter",
					    "Filter command:", $OPT{'filter'});
		     if (defined $ret[1]) {
			 $OPT{'filter'} = $ret[1];
			 $MSG = "set viwer filter command [$OPT{'filter'}]";
		     }
		 });
$m_opt->separator();
$m_opt->command(-label => 'Option variables',
		-command => \&ViewAllOptions);
$m_opt->pack(qw/-side left/);
#----------------------------------------------------------------------
# ϼ
#----------------------------------------------------------------------
my $commandF = $main->Frame(qw/-bd 2/);
$commandF->pack(qw/-fill x/);
$commandF->Button(qw/-text Search/, -command => \&Search
		  )->pack(qw/-side left/);
my $inputE = $commandF->Entry(-textvariable => \$KeyWord,
			      qw/-bg white -fg black/);
$inputE->bind('<Return>', \&Search);
$inputE->pack(qw/-side left -fill x -expand y/);
$commandF->Button(qw/-text Stop/, -command => [sub{$STOP_SIG = 1;}]
		  )->pack(qw/-side left/);
$commandF->Photo(logo, -file => "$OPT{logo}");
$commandF->Button(-image => logo, -command => sub{&DispInfoWin();})->pack();
#----------------------------------------------------------------------
# ɽ
#----------------------------------------------------------------------
my $rsltF = $main->Frame();
$rsltF->pack(qw/-fill both -expand y/);
$rsltT = $rsltF->Text(qw/-width 0 -height 10/);
my $scr   = $rsltF->Scrollbar(-command => ['yview', $rsltT]);
$rsltT->configure(-yscrollcommand => ['set', $scr]);
$scr->pack(qw/-side left -fill y/);
$rsltT->pack(qw/-side left -fill both -expand y/);
#----------------------------------------------------------------------
# åɽ
#----------------------------------------------------------------------
$main->Label(-textvariable => \$MSG,
	     qw/-width 70 -bg pink -anchor w/)->pack(qw/-fill x/);
		 
$main->MainLoop();
