#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
use Cwd;
use Tk;
use Tk::Dialog;
use LWP::Simple;
#use XML::Hash;

#my $Win_background = "black" ;
#my $Win_foreground = "green" ;

my $types = [
    [ 'PDB files', [ '.pdb', '.PDB', '.CTAB', '.ent', '.cif', '.mmcif'] ],
    [ 'Structure factor files', [ '.dat', '.mtz', '.sca', '.cif', '.fcf', '.sf', '*sf*' ] ],
    [ 'Cell constant files', '*cell*' ],
    [ 'Sequence files', '*seq*' ],
    [ 'Connectivity files', '.ctab' ],
    [ 'Map files', [ '.xmap', '*map*', '.mrc' ] ],
    [ 'Surface files', [ '.surf', '*surf*' ] ],
    [ 'View files', [ '*.VIEW', '*.view'] ],
    [ 'plot files', [ '*plot*', '.pov' ] ],
    [ 'Save files', [ 'SAVE*'] ],
    [ 'com files', [ '.com', '.cmds'] ],
    [ 'All files', '*' ]
    ] ;


######################################################

my $mw = MainWindow->new;

#####################################################


my %commands = () ;

if ( @ARGV <= 0 ) { push ( @{$commands{'help'}}, ' ' ) ; } 
else { (%commands) = main_config::get_commands ( @ARGV ) ; }


my $PROC_ERROR = "FALSE" ;
my $MACRO_FILE ="" ;
my $REST_LINE = "" ;
my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'm' || substr($keyword, 0,5) eq "macro" ) { goto MACRO }
#    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;
    next ;
  HELP:
    print << "END_OF_TEXT" ;
    -h|-help|--help) prints this message with available options and current status 
    -m|--macro)      configure the perl script [$MACRO_FILE]
END_OF_TEXT
    exit ;
 MACRO: 
{ $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; last ; }
}
#if ( $PROC_ERROR eq "TRUE" ) { exit ; }
if ( $MACRO_FILE eq "" ) { exit ; }

foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'm' || substr($keyword, 0,5) eq "macro" ) { next ; }
    my $CC ;
    if ( length($keyword) ==1 ) { $CC=" -"; } else { $CC=" --" ; }

    $REST_LINE = $REST_LINE.$CC.$keyword." ".join(" ", @{$commands{$keyword}} ) ; 
    print "}", $keyword, "{ restline:>", $REST_LINE, "<\n" ;
}

#######################################

#print " macro:", $MACRO_FILE, ">+<", $REST_LINE, ">\n" ;
my $line ;
my @whole_file = () ;
my %key_list = () ;
my @key_array = () ;
my $key ;

#my $MACRO_FILE = "create_refine.pl" ;

open ( MACRO, "$MACRO_FILE |" ) ;
while(<MACRO>)
{
    chomp ;
    my $line = $_ ;
    push( @whole_file, $line ) ;
}
close(MACRO) ;


$key = "" ;
    my $n_bra = 0 ;
    my @a_key = () ;
    my @b_key = () ;
    my @a_word = () ; my @b_word = () ; my @c_word = () ;
    my @text = () ;
    my $xx ;
    my $val ;
    my $chr ;
my $nchr = 0 ; 

foreach $line ( @whole_file ) 
{
#    print "LINE>", $line, "\n" ;
    my @chr_array = split( '', $line ) ;
    
    @a_key = () ;
    $n_bra = 0 ;
    $nchr = 0 ; 
    foreach $chr (@chr_array) 
    {
	if ( $chr eq "\)" ) { $n_bra = $nchr } #
	elsif ( $n_bra == 0 ) { push( @a_key, $chr ) ; }
	else { push (@b_key, $chr ) ; } 
	$nchr++ ;
    }
#    print @a_key, " BB:", @b_key, "\n" ;
    if ( $nchr > 0 )
    {
	if ( $n_bra > 0 ) 
	{ 
	    if ( $key ne "" && lc($key) ne "help" && lc($key) ne "doit" ) 
	    { gen_entry ( @text ) ; }
	    $xx = join ( '', @a_key) ;
	    @text = split( "-", $xx ) ;
	    $key = pop( @text ) ;
#	    print " >>>>>>>", $key, "<<<<<<\n" ;
	    push ( @key_array, $key) ;
	    my $xx = join( '', @b_key ) ;
	    @text = ( $xx ) ;
	    @b_key = () ;
#	    @{$key_list{$key}{'LINE'}} = ( $xx );
	}
	else
	{
	    $xx = join( '', @a_key ) ;
#	    push( @{$key_list{$key}{'LINE'}}, "\n" ) ;
#	    push( @{$key_list{$key}{'LINE'}}, $xx ) ;
	    push ( @text, "\n", $xx ) ;
#	    print " new line:", $xx, "\n" ;
	}
    }

#   print " TABLE: key >", $key, "< LINE: >", @{$key_list{$key}{'LINE'}}, "<\n" ;
#   print " TABLE: key >", $key, "< LINE: >", $key_list{$key}{'LINE'}, "<\n" ;
#    $key = "" ;
}
if ( $key ne "" && lc($key) ne "help" && lc($key) ne "doit" ) 
{ gen_entry ( @text ) ; }

my $max_length = 0 ; my @act_lines = () ;
foreach $key ( sort keys %key_list )
{
    my $line ;
    @act_lines = split ("\n", $key_list{$key}{'LINE'} ) ;
    foreach $line (  @act_lines ) 
    {
	if ( length($line) > $max_length ) 
	{ $max_length = length($line) ; }
    }
#    print " TABLE: key >", $key, " LN:>", $key_list{$key}{'LINE'}, 
#    length($key_list{$key}{'LINE'}), "> <", $max_length, "<\n" ;

}

#######################################################################

$mw->title("Tk: $MACRO_FILE");

my $f = $mw->Frame->pack(-side => 'bottom');
$f->Button(-text => "Quit", 
           -command => sub { exit; })->pack(-side => 'left');
$f->Button(-text => "Write-It",
           -command => \&create_macro )->pack(-side => 'bottom');
my $t = $mw->Scrolled("Text", -width => $max_length + 30, 
                   -wrap => 'none')->pack(-expand => 10, -fill => 'both');

#######################################################################
 


my $w ;
my $text ;
my $value ;
my $TEXT_WIDTH = 32 ;
my $NUMB_WIDTH = 12 ;
my $OPTS_WIDTH = 12 ;
my $WIDTH ;
foreach $key ( @key_array )
{
#    print $key, " label item <", $key_list{$key}{'KIND'} ,"> \n" ;

    $w = $t->Entry(-width => $TEXT_WIDTH, -textvariable => \$key_list{$key}{'LINE'});
    if (  !exists( $key_list{$key}{'KIND'} ) ) { next ; }
    if ( $key_list{$key}{'KIND'} eq "EMPTY" )
    {
	$text = $key_list{$key}{'LINE'} ;
	$value = $key_list{$key}{'VALUE'} ;
	if ( $value =~ "[A-Za-z]" ) 
	{ $WIDTH = $TEXT_WIDTH ; }
	else
	{ $WIDTH = $NUMB_WIDTH ; }
#	print " empty label item $text \n" ;
	$val = "         " ;
	foreach (split("\\n", $text) )
	{
	    $w = $t->Label(-text => $_, -justify => 'left', -relief => 'groove', 
			   -width => $max_length );
	    $t->windowCreate('end', -window => $w);
	}

	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'VALUE'} );
	$t->windowCreate('end', -window => $w);
	if ( lc($text) =~ "file" ) 
	{ $w->bind("<Double-1>" => [\&file_select_box, \$key_list{$key}{'VALUE'}, $types ]); }
	elsif ( lc($text) =~ "select" ) 
	{ print " my selection: $text\n"; $w->bind("<Double-1>" => [\&selection_box, \$key_list{$key}{'VALUE'}, "" ]); }
	$t->insert('end', "\n");
    }
    if ( $key_list{$key}{'KIND'} eq "VALUE" )
    {
	$text = $key_list{$key}{'LINE'} ;
	$val = $key_list{$key}{'VALUE'} ;
	$value = $key_list{$key}{'VALUE'} ;
	if ( $value =~ "[a-zA-Z]" ) 
	{ $WIDTH = $TEXT_WIDTH ; }
	else
	{ $WIDTH = $NUMB_WIDTH ; }

#	print " value label item $text \n" ;
#	foreach (split("\\n", $text) )
#	{
#	    $w = $t->Label(-text => $_, -justify => 'left', -relief => 'groove', 
#			   -width => $max_length );
#	    $t->windowCreate('end', -window => $w);
#	}
	$w = $t->Label(-text => "$text", -justify => 'left', -relief => 'groove', 
		      -width => $max_length );
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'VALUE'} );
	$t->windowCreate('end', -window => $w);
	if ( lc($text) =~ "file" ) 
	{ $w->bind("<Double-1>" => [\&file_select_box, \$key_list{$key}{'VALUE'}, $types ]); }
	elsif ( lc($text) =~ "select" ) 
	{ print " my selection 2: $text\n"; $w->bind("<Double-1>" => [\&selection_box, \$key_list{$key}{'VALUE'}, "" ]); }

	$t->insert('end', "\n");
    }
    elsif ( $key_list{$key}{'KIND'} eq "PAIR" )
    {

	$text = $key_list{$key}{'LINE'} ;
	$val = $key_list{$key}{'VALUE'} ;
	$WIDTH = $NUMB_WIDTH ;
#	foreach (split("\\n", $text) )
#	{
#	    $w = $t->Label(-text => $_, -justify => 'left', -relief => 'groove', 
#			   -width => $max_length );
#	    $t->windowCreate('end', -window => $w);
#	}
	$w = $t->Label(-text => "$text", -justify => 'left', -relief => 'groove', 
		      -width => $max_length );
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'ARRAY'}[0] );
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'ARRAY'}[1] );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");
    }
    elsif ( $key_list{$key}{'KIND'} eq "THREES" )
    {
	$text = $key_list{$key}{'LINE'} ;
	$val = $key_list{$key}{'VALUE'} ;
	$WIDTH = $NUMB_WIDTH ;
#	foreach (split("\\n", $text) )
#	{
#	    $w = $t->Label(-text => $_, -justify => 'left', -relief => 'groove', 
#			   -width => $max_length );
#	    $t->windowCreate('end', -window => $w);
#	}
	$w = $t->Label(-text => "$text", -justify => 'left', -relief => 'groove', 
		      -width => $max_length );
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'ARRAY'}[0] );
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'ARRAY'}[1] );
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => $WIDTH, -textvariable => \$key_list{$key}{'ARRAY'}[2] );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");
    }
    elsif ( $key_list{$key}{'KIND'} eq "LIST" )
    {
	$text = $key_list{$key}{'LINE'} ;
#	foreach (split("\\n", $text) )
#	{
#	    $w = $t->Label(-text => $_, -justify => 'left', -relief => 'groove', 
#			   -width => $max_length );
#	    $t->windowCreate('end', -window => $w);
#	}
	$w = $t->Label(-text => "$text", -justify => 'left', -relief => 'groove', 
		      -width => $max_length );
	$t->windowCreate('end', -window => $w);
	$w = $t->Optionmenu(-width => $OPTS_WIDTH, 
			    -textvariable => \$key_list{$key}{'VALUE'}, 
			    -options => [@{$key_list{$key}{'LIST'}} ]
	    );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");
    }

}
$t->configure(-state => 'disabled');
MainLoop;


sub gen_entry
{
    my @text_line = @_ ;
    my $n_brk = 0 ;
    my $nin = 0 ; 
    my $xx ;
    my $chr ;

#    print "gen_entry> key=}", $key, "{\n";
    @a_word = () ; @b_word = () ; @c_word = () ;
    $xx = join( " ", @text_line ) ;
    @text_line = split ( "", $xx ) ;
    foreach $chr (@text_line) 
    {
	if ( $chr eq "\[" ) { $nin=1 } #
	elsif ( $chr eq "\]" ) 
	{ 
	    $xx = join ( "", @a_word ) ;
	    push ( @c_word, $xx ) ;
	    $n_brk++; $nin=0 ; @a_word = () ;
	} #
	elsif ( $nin == 0 ) 
	{
	    push( @b_word, $chr ) ;
	}
	else
	{
	    push( @a_word, $chr ) ;
	}
    }
#    print " -- a_word:[", @a_word, "]\n" ;
#    print " -- b_word:[", @b_word, "]\n" ;
#    print " -- c_word:[", @c_word, "]\n" ;
    my $kind = "" ;
    $xx = join ("", @b_word ) ;
    $xx =~ s/current// ; $xx =~ s/=// ; # $xx =~ s/:// ; 
    my @chrs = split ( " ", $xx ) ;

    $val = pop (@c_word ) ;

    my @items = split( " - ", $val ) ;
    my $nn = scalar ( @items ) ;
    if ( $nn == 0 ) { $kind = "EMPTY" ; } 
    if ( $nn == 2 ) { $kind = "PAIR" ; }
    @items = split( ", ", $val ) ;
    $nn = scalar ( @items ) ;
    if ( $nn == 3 ) { $kind = "THREES" ; }

#    print ">", $kind, "< nn=", $nn, " items:", @items, "\n" ;

    if ( $kind eq "PAIR" ) 
    {
	@items = split( " - ", $val ) ;
	$xx = join (" ", @chrs ) ;
	if ( scalar ( @c_word >= 1 ) ) { $xx = join ("", $xx, " [" , @c_word , "] " ) ; }
#	$xx = $xx . "[" . @c_word . "]" ;
	$key_list{$key}{'LINE'} = $xx ; 
	$key_list{$key}{"KIND"} = "PAIR" ;
	@{$key_list{$key}{'ARRAY'}} = @items ;
#	print "items:", @items , "\n" ;
#	print "--------------> ", $key, " ARRAYS MENU:", $nn, ':>', @{$key_list{$key}{'ARRAY'}}, "<\n" ;
    }
    elsif ( $kind eq "THREES" ) 
    {
	@items = split( ", ", $val ) ;
	$xx = join (" ", @chrs ) ;
#	$xx = $xx . "[" . @c_word . "]" ;
	$key_list{$key}{'LINE'} = $xx ; 
	$key_list{$key}{"KIND"} = "THREES" ;
	@{$key_list{$key}{'ARRAY'}} = @items ;
#	print "items:", @items , "\n" ;
#	print "--------------> ", $key, " ARRAYS MENU:", $nn, ':>', @{$key_list{$key}{'ARRAY'}}, "<\n" ;
    }
    elsif ( $kind eq "EMPTY" ) 
    {
#	print $key, " empty key \n" ;
	$key_list{$key}{"KIND"} = "EMPTY" ;
	$key_list{$key}{'LINE'} = join ( " ", @chrs ) ;
	$key_list{$key}{'VALUE'} = " " ;
    }
    else
    {
	$key_list{$key}{'VALUE'} = $val ;
	
	$xx = join( "", @c_word ) ;
	@items = split ( "/", $xx ) ;
	$nn = scalar ( @items ) ;
#	print " ELSE / nn:", $nn, " items:", @items, "\n" ;
	if ( $xx =~ m/\// )
	{
	    my @c_line = () ;
	    my @chr_line = split ( " ", join(@chrs)) ;
#	    foreach $xx ( @chr_line ) 
	    foreach $xx ( @chrs ) 
	    { 
		my $yy ;
		my $TEST = "F" ; 
		foreach $yy ( @items )
		{
#		     print "TRUE $xx $yy\n";
		     if ( $xx =~ $yy && length($yy) > 1 ) { $TEST = "T" ; push ( @c_line, "\n", $yy, ": ", "\t" ) ;}
		}
		if ( $TEST eq "F" ) 
		{ push (@c_line, $xx, " " ) } 
	    } 
	    $xx = join ("", @c_line ) ;
#	    print " xx: $xx\n" ;
	    $key_list{$key}{'LINE'} = $xx ; # join ( " ", @chrs ) ;
	    $key_list{$key}{"KIND"} = "LIST" ;
	    @{$key_list{$key}{'LIST'}} = @items ; 
#	    print "--------------> ", $key, " = OPTION MENU:", $nn, ':>', @items, "<\n" ;
	}
	else
	{
#    print " -- a_word:", @a_word, "\n" ;
#	    print " -- chrs_word:", @chrs, "\n" ;
#	    print " -- c_word:", @c_word, "\n" ;
	    $xx = join (" ", @chrs ) ;
	    if ( scalar ( @c_word >= 1 ) ) { $xx = join ("", $xx, " [" , @c_word , "] " ) ; }
#	    print " -- X_word:", $xx, "\n" ;
	    $key_list{$key}{'LINE'} = $xx ; # join ( " ", @chrs ) ;
	    $key_list{$key}{"KIND"} = "VALUE" ;
#	    print "--------------> ", $key, " VALUE:", $nn, ':>', $val, "<\n" ;
	}
	
    }
}

sub create_macro
{
    my @system_string = ( $MACRO_FILE ) ;
    
    foreach $key ( sort keys %key_list )
    {
	if (  $key eq "help" || $key eq "doit") { next ; }
	my $ss_key = "--" . $key ;
	push (@system_string, $ss_key ) ;
	if ( $key_list{$key}{'KIND'} eq "PAIR" || $key_list{$key}{'KIND'} eq "THREES" )
	{ 
	    print " ARRAY: ", @{$key_list{$key}{'ARRAY'}}, "\n" ;
	    push (@system_string, @{$key_list{$key}{'ARRAY'}} ) ; 
	}
	else 
	{ 	
	    print $key, " VALUE: ", $key_list{$key}{'VALUE'}, "\n" ;
	    push (@system_string, $key_list{$key}{'VALUE'} ) ;
	}
    }
    push (@system_string, "--doit" ) ;
    my $call = join (" ", @system_string ) ;
    print $call, "\n" ;
    system ( $call ) ;
    exit;
}


sub file_select_box
{
    my ( $event, $key, $f_types) = @_ ;
    my $homedir = getcwd ."/" ;
    my $file = $mw->getOpenFile( -title => "Tk::main_config:find file", -filetypes => $f_types ) ;
    if ( $file ) 
    {
	$file =~ s/$homedir// ;
	${$key} = $file ;
    }
}

sub file_select_box_x
{
#    my ( $event, %parent, $key, $I) = @_ ;
    my ( $event, $key, $filter_file) = @_ ;
    my @all_files ;
    my @list_files = () ;
#    my $filter_file = "[0-9,A-Z,a-z]" ;
#    my $filter_file = "" ;

    print " box:", ${$key},  "\n" ;
    my $tt = ${$key} ;
    my $lb; my $nw ; my $w ; my $ff ; my $my_file ;

    $nw = MainWindow->new (-width => 600, -height => 350 );
    $nw->title("Tk: main config:: file replacement for {$tt}");

    my $f = $nw->Text( -width => $max_length + 30, -height => 1, 
                   -wrap => 'none')->pack(-expand => 'x', -fill => 'both', -side => 'top');
    $f->configure(-state => 'normal');
#    $w = $f->Label(-text => "File Filter", -justify => 'center', -relief => 'groove', 
    $w = $f->Label(-text => "File Filter", -justify => 'left', -relief => 'groove', 
				    -width => $max_length );
    $f->windowCreate('end', -window => $w);
    $w = $f->Entry(-width => 60, -textvariable => \$filter_file, -justify => 'left' );
    $f->windowCreate('end', -window => $w);

    $w->bind("<Key>", [\&main_config::list_regenerate, \@all_files, \@list_files, \$filter_file, \$lb ] ) ;


    $f = $nw->Text()->pack(-side => 'bottom');
    $f->Button(-text => "Cancel", 
	       -command => sub {  $nw->destroy() if Tk::Exists($nw) ; })->pack(-side => 'left');
    $f->Button(-text => "Done",
	       -command => sub {  
		  my $x_file_name = $lb->get($lb->curselection()) ;
#		  print "the file name: {", $x_file_name, "}\n" ;
#		  $parent{$key}{'ARRAY'}[$I] = $x_file_name ;
		  ${$key} = $x_file_name ;
		   $nw->destroy() if Tk::Exists($nw) ; 
	       })->pack(-side => 'bottom');


    my $path = './' ;
    opendir( ALLFILES, $path );
    @all_files = readdir( ALLFILES );
    closedir( ALLFILES );

    $lb = $nw->Scrolled("Listbox", -scrollbars => "e", -selectmode => "single")->pack (-fill => 'both') ;

    @list_files = () ;
    foreach $my_file ( sort( @all_files ) )
    {
	if ( $my_file =~ $filter_file ) { push( @list_files, $my_file ) ; }
    }
#    @list_files = @all_files ;


    $lb->insert('end', @list_files ) ;
#    $lb->insert('end', qw/red green yellow blue/) ;

    $lb->bind('<Button-1>', 
	      sub{ 
		  my $x_file_name = $lb->get($lb->curselection()) ;
#		  print "the file name: {", $x_file_name, "}\n" ;
	      } ) ;
    $lb->bind('<Double-1>', 
	      sub{ 
		  my $x_file_name = $lb->get($lb->curselection()) ;
#		  print "the CLICK file name: {", $x_file_name, "}\n" ;
		  if ( -d $x_file_name ) 
		  { 
		      print " directory file \n" ; 
		  } 
		  else
		  {
		      ${$key} = $x_file_name ;
		      $nw->destroy() if Tk::Exists($nw) ; 
		  }
	      } ) ;

}


sub selection_box
{
#    my ( $event, %parent, $key, $I) = @_ ;
    my ( $event, $key, $filter_file) = @_ ;
    my @all_files = ("atom name", "atom numb", "segm name", "segm numb", "resi name", "resi numb", "sequence", "temper", "weight", "color" ) ;
    my @cont_files = (".and.", ".or.", ".not.", "\(", "\)" ) ;
    my @list_files = () ;
    my $selection_line = ${$key} ;
#    my $filter_file = "[0-9,A-Z,a-z]" ;
#    my $filter_file = "" ;
    my $my_length = length( "selection line" ) ;
    print " selection box:", $selection_line,  "\n" ;

    my $tt = ${$key} ;
    my $lb; my $lb_cont ; my $nw ; my $w ; my $ff ; my $my_file ;

    $nw = MainWindow->new (-width => 800, -height => 350 );
    $nw->title("Tk: main config:: file replacement for {$tt}");

    my $f = $nw->Text( -width => $my_length + 80, -height => 1, 
                   -wrap => 'none')->pack(-expand => 'x', -fill => 'both', -side => 'top');
    $f->configure(-state => 'normal');
#    $w = $f->Label(-text => "selection line", -justify => 'center', -relief => 'groove', 
    $w = $f->Label(-text => "selection line", -justify => 'left', -relief => 'groove', 
				    -width => $my_length+2 );
    $f->windowCreate('end', -window => $w);
    $w = $f->Entry(-width => 80, -textvariable => \$selection_line, -justify => 'left' );
    $f->windowCreate('end', -window => $w);

#    $w->bind("<Key>", [\&main_config::list_regenerate, \@all_files, \@list_files, \$filter_file, \$lb ] ) ;
    $w->bind("<Key>", \@all_files ) ;


    $f = $nw->Text()->pack(-side => 'bottom');
    $f->Button(-text => "Cancel", 
	       -command => sub {  $nw->destroy() if Tk::Exists($nw) ; })->pack(-side => 'left');
    $f->Button(-text => "Done",
	       -command => sub {  
#		  my $selection_word = $lb->get($lb->curselection()) ;
#		  print "the file name: {", $selection_word, "}\n" ;
#		  $parent{$key}{'ARRAY'}[$I] = $selection_word ;
		  ${$key} = $selection_line ;
		   $nw->destroy() if Tk::Exists($nw) ; 
	       })->pack(-side => 'bottom');


    my $path = './' ;
#    opendir( ALLFILES, $path );
#    @all_files = readdir( ALLFILES );
#    closedir( ALLFILES );

    $lb = $nw->Scrolled("Listbox", -scrollbars => "e", -selectmode => "single")->pack (-fill => 'both') ;
    $lb->insert('end', @all_files ) ;
#    $lb->insert('end', qw/red green yellow blue/) ;

    $lb->bind('<Button-1>', 
	      sub{ 
		  my $selection_word = $lb->get($lb->curselection()) ;
		  $selection_line = $selection_line . " " . $selection_word ;
#		  print "the file name: {", $selection_word, "}\n" ;
	      } ) ;
    $lb_cont = $nw->Scrolled("Listbox", -scrollbars => "e", -selectmode => "single")->pack (-fill => 'both') ;
    $lb_cont->insert('end', @cont_files ) ;
#    $lb->insert('end', qw/red green yellow blue/) ;

    $lb_cont->bind('<Button-1>', 
	      sub{ 
		  my $selection_word = $lb_cont->get($lb_cont->curselection()) ;
		  $selection_line = $selection_line . " " . $selection_word ;
#		  print "the file name: {", $selection_word, "}\n" ;
	      } ) ;
}

