#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
use main_config_tk ;
use Tk;

my $Win_background = "white" ;
my $Win_foreground = "black" ;
my $frame_lab_bck = "grey20" ;
my $label_background = "#EEE" ;
my $mw_width; my $mw_height;
my $max_length = 20 ;
my $group_length = 15 ;
my $option_bar = 8 ;
my $choice_bar = $option_bar + 5 ;
my $sel_bar = 40 ;
my $onof_bar = 7 ;
my $hydrogen_flag = 0 ;
my $chain_memory_flag = 0 ;
my $my_file_flag = 0 ;
my @selection_list_flag ;
my %commands = () ;

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

my $conf_file ;
if ( -f "main_config.data" )
{    $conf_file = "main_config.data" ; }
else
{    $conf_file = join ('', $ENV{"MAIN_CONF"}, "main_config.data" ) ; }

my %my_main = main_config::read_main_config_file ( $conf_file ) ; 

if ( !exists($my_main{"IMAGE_COLOR_START"} ) ) 
{ 
    if ( $my_main{"SG_NMOL"}[0] > 8 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 144 ) }
    elsif( $my_main{"SG_NMOL"}[0] > 4 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 150 ) ; }
    elsif( $my_main{"SG_NMOL"}[0] > 2 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 152 ) ; }
    else
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 160 ); }
}
if ( !exists($my_main{"IMAGE_COLOR_INCR"} ) ) 
{ 
    if ( $my_main{"SG_NMOL"}[0] > 8 ) 
    { push ( @{$my_main{"IMAGE_COLOR_INCR"}}, 5 ) }
    else
    { push ( @{$my_main{"IMAGE_COLOR_INCR"}}, 5 ) ; }
}
if ( !exists($my_main{"IMAGE_RADII_FRACTION"} ) ) { @{$my_main{"IMAGE_RADII_FRACTION"}} = ( 0.25 ) ; }
if ( !exists($my_main{"IMAGE_CHAIN"} ) ) { @{$my_main{"IMAGE_CHAIN"}} = ( "OFF" ) ; }
if ( $my_main{"IMAGE_CHAIN"}[0] eq "ON" ) { $chain_memory_flag = 1 ; } else { $chain_memory_flag = 0 ; }
if ( !exists($my_main{"IMAGE_HYDRO"} ) ) { @{$my_main{"IMAGE_HYDRO"}} = ( "ON" ) ; }
if ( $my_main{"IMAGE_HYDRO"}[0] eq "ON" ) { $hydrogen_flag = 1 ; } else { $hydrogen_flag = 0 ; }
if ( !exists($my_main{"IMAGE_MY_FILE"} ) ) { @{$my_main{"IMAGE_MY_FILE"}} = ( "OFF" ) ; }
if ( $my_main{"IMAGE_MY_FILE"}[0] eq "ON" ) { $my_file_flag = 1 ; } else { $my_file_flag = 0 ; }
if ( !exists($my_main{"IMAGE_MY_FILE_NAME"} ) ) { @{$my_main{"IMAGE_MY_FILE_NAME"}} = ( "--NONE--" ) ; }
if ( !exists($my_main{"IMAGE_COLOR_1"} ) ) { @{$my_main{"IMAGE_COLOR_1"}} = ( 1,1,1 ) ; }
if ( !exists($my_main{"IMAGE_COLOR_2"} ) ) { @{$my_main{"IMAGE_COLOR_2"}} = ( 0,1,1 ) ; }
if ( !exists($my_main{"IMAGE_COLOR_3"} ) ) { @{$my_main{"IMAGE_COLOR_3"}} = ( 1,0,1 ) ; }
if ( !exists($my_main{"IMAGE_COLOR_4"} ) ) { @{$my_main{"IMAGE_COLOR_4"}} = ( 1,1,0 ) ; }
if ( !exists($my_main{"IMAGE_COLOR_5"} ) ) { @{$my_main{"IMAGE_COLOR_5"}} = ( 1,0.5,0.8 ) ; }
if ( !exists($my_main{"IMAGE_COLOR_6"} ) ) { @{$my_main{"IMAGE_COLOR_6"}} = ( 0.5,1,0.8 ) ; }
my $image_my_file_name = $my_main{"IMAGE_MY_FILE_NAME"}[0] ;


my @all_selections ;
my @selection_list ;
my @window_list ;
######################################################

my $PROC_ERROR = "FALSE" ;
my $MACRO_FILE ="re_image.cmds" ;

# # 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" ;
# # 
# #   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}} ) ; 
# # next ;
# # }
# # # if ( $PROC_ERROR eq "TRUE" ) { exit ; }
# # #######################################

my @my_segm_list = ( "A" ) ;
my @my_resid_list = () ;
my @my_seq = () ;
my @my_chain_list = () ;
my %chains_done = () ;
my $nres = 0 ; 
my @my_helix_list = () ;
my @my_beta_list = () ;

my $PDB_FILE  = ${$my_main{"FILE_MODEL"}}[0] ;
print " tk_re_image.pl> my pdb file:>", $PDB_FILE, "<\n" ;
if (-e $PDB_FILE) 
{
    my ( $ref_segm_list, $ref_resid_list, $ref_seq_list, $ref_unit_cell, $ref_space_group, $ref_atom_list, $ref_xyz_list, $ref_b_list, $ref_w_list, $ref_atom_el_list, $ref_helix_list, $ref_beta_list ) 
	= main_config::read_pdb_file ( $PDB_FILE ) ;

    @my_segm_list = @{$ref_segm_list} ;
    @my_resid_list = @{$ref_resid_list} ;
    @my_seq = @{$ref_seq_list} ;
    $nres = scalar ( @my_seq ) ;
    @my_helix_list = @{$ref_helix_list} ;
    @my_beta_list = @{$ref_beta_list} ;
} 
# else
# {
# 
#     @my_segm_list = ( "A" ) ;
#     @my_resid_list = () ;
#     @my_seq = () ;
#     $nres = 0 ;
# 
# }

my $key ; my $nn = 0 ; my $I ;

for ($I = 0; $I < $nres ; $I++ )
{
    my $ch = join(" ", $my_seq[$I]{"seg_name"}, $my_seq[$I]{"chain_id"} ) ;
    $chains_done{$ch} = "DONE" ;
}
@my_chain_list = () ;
foreach $key (keys %chains_done ) 
{
    push(@my_chain_list, $key) ;
}

my %image_cmds ;

$image_cmds{"ATOM"} = ["NONE", "BALL", "NAME", "CLASS", "CROSS"] ;
$image_cmds{"BOND"} = ["NONE", "LINE", "STICK","DISTANCE"] ;
$image_cmds{"RIBBON"} = ["NONE", "LINE", "STICK", "MAIN"] ;
$image_cmds{"SURFACE"} = ["NONE", "COLOR", "ATOM_RGB", "POINT_RGB" ] ;
#$image_cmds{"STYLE"} = ["WEIGHT", "TYPES", "N2C", "SECONDARY", "RESIDUE", "AUTO"] ;
$image_cmds{"STYLE"} = ["WEIGHT", "TYPES", "N2C", "SECONDARY", "RESIDUE", "AUTO", "WHITE", "BLACK", "RED", "ORANGE","YELLOW",  "GREEN", "CYAN", "BLUE", "AS IS", "COLOR 1", "COLOR 2", "COLOR 3", "COLOR 4", "COLOR 5", "COLOR 6"] ;
$image_cmds{"COLOR"} = ["STYLE", "WHITE", "BLACK", "RED", "ORANGE","YELLOW",  "GREEN", "CYAN", "BLUE", "AS_IS" ] ;
$image_cmds{"ONOF"} = ["ON", "OFF"] ;
$image_cmds{"RADII"} = ["VdW", "B-val","class"] ;

#my $color = $mw->chooseColor( -title => "hello color", -initialcolor => "#555" ) ;

my %color_list ;

my %selections ;

%selections = () ;

foreach $key (keys %my_main ) 
{
    if ( $key =~ "IMAGE_HASH" )
    {
	$nn++ ;
	my $value = join (" ", @{$my_main{$key}} ) ;
	my @words = split ( "IMAGE_HASH_", $key ) ;
	shift (@words) ;
	my $name = join(" ", @words ) ;
	@words = split ("_", $name ) ;
	my $kind = pop ( @words ) ;
	my $select = join ( "_", @words ) ;
#	print " HASH: $select $kind value = {", $value, "}\n" ;
	$selections{$select}{$kind} = $value ;
    }
}

if ( $nn == 0 ) 
{
    select_by_segment_init () ;
}


######################################################
my $frame_model ;

my $mw = MainWindow->new;
#my $mw ;
#$mw = MainWindow->new (-width => 600, -height => 350 );

my $w ;
my $xxxx ;
$mw_height = $mw->screenheight() ;
$mw_width = $mw->screenwidth() ;
print " screen resolution: $mw_width, $mw_height \n" ;


$mw->title("Tk: $MACRO_FILE");
$mw->packPropagate( 0 ) ;
$mw->geometry( "1100x500+500+0" ) ;

my $g = $mw->Frame->pack(-side => 'top');
$g->Button(-text => "Clear",
           -command => \&select_clear )->pack(-side => 'left');
$g->Button(-text => "By Segment", 
           -command => \&select_by_segment )->pack(-side => 'left');
$g->Button(-text => "By Chain",
           -command => \&select_by_chain )->pack(-side => 'left');
$g->Checkbutton(-text => "Hydrogens",
           -variable => \$hydrogen_flag )->pack(-side => 'left');
$g->Checkbutton(-text => "Chain memory",
           -variable => \$chain_memory_flag )->pack(-side => 'left');

$g->Button(-text => "RGB colors",
		    -command => \&tk_rgb_colors )->pack(-side => 'right');
$g->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_INCR"}[0], -width => 6  )->pack( -side => 'right' );
$g->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_START"}[0], -width => 6  )->pack( -side => 'right' );
$g->Label(-text => "    STARTING COLOR & INCREMENT", -relief => 'groove' )->pack(-side => 'right' );
$g->Entry(  -textvariable => \$my_main{"IMAGE_RADII_FRACTION"}[0], -width => 4  )->pack( -side => 'right' );
$g->Label(-text => " ATOM RADII FRACTION", -relief => 'groove' )->pack(-side => 'right' );

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 => 'left');

$f->Checkbutton(-text => "my file",
           -variable => \$my_file_flag )->pack(-side => 'left');
#$f->Label(-text => "    my file  ", -relief => 'flat', )->pack(-side => 'left' );
$w = $f->Entry(-textvariable => \$image_my_file_name, -width => 32  )->pack(-side => 'left' );
	{ $w->bind("<Double-1>" => [\&main_config_tk::file_select_box, \$image_my_file_name, "" ]); }



#$w = $f->Entry(-width => 25, -textvariable => \$image_my_file_name );
#	$f->windowCreate('end', -window => $w)->pack( -side => 'right', -anchor => 'w' );

#my $t = $mw->Scrolled("Text", -width => $max_length + 30, 
#                   -wrap => 'none')->pack(-expand => 10, -fill => 'both');

#
#    molecular model data
#
#$frame_file = $mw->Scrolled("Text", asldjfk;l
#my $frame_model = $mw->Scrolled("Text", -width => $max_length + 30, -height => 8, -background => $Win_background, -foreground => $Win_foreground,

$frame_model = $mw->Scrolled("Text", -width => $max_length + 30, -height => 8, 
                    -wrap => 'none')->pack(-expand => 'x', -fill => 'both', -side => 'top');
$frame_model->configure(-state => 'normal');
#$frame_model->Frame( -label =>  " ---  IMAGE  --- ", -relief => 
#		     'flat', -borderwidth => 2)->pack(-side => 'top');
#my $frame_model = $mw ;
#$frame_model->insert('end', "\n");
#$frame_model->insert('end', "\n");
$frame_model->insert('end', "\n");



generate_selection_frame_head ( $frame_model ) ;

generate_selection_frame ( $frame_model ) ;

MainLoop;


sub generate_selection_frame_head
{
    my ( $frame_segment ) = @_ ;
    my $w ; my $wl ;

    $w = $frame_segment->Label(-text => "GROUP NAME", -relief => 'groove', -justify => 'left', 
			       -width => $group_length , -borderwidth => 5 )
	->place( -anchor => 'w') ;
				       # pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);

    $w = $frame_segment->Label(-text => "ATOM SELECTION", -relief => 'groove', -justify => 'left', 
				   -width => $sel_bar , -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);

    $w = $frame_segment->Label(-text => " ON/OFF ", -relief => 'groove', -justify => 'left', 
				   -width => $choice_bar , -borderwidth => 5 )
	->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);

    $w = $frame_segment->Label(-text => " ATOMS ", -relief => 'groove', -justify => 'left', 
				   -width => $choice_bar , -borderwidth => 5 )
	->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);
    $w = $frame_segment->Label(-text => " BONDS ", -relief => 'groove', -justify => 'left', 
				   -width => $choice_bar , -borderwidth => 5 )
	->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);
    $w = $frame_segment->Label(-text => "RIBBONS", -justify => 'left', -relief => 'groove', 
				   -width => $choice_bar , -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);
    $w = $frame_segment->Label(-text => "SURFACE", -justify => 'left', -relief => 'groove', 
				   -width => $choice_bar , -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);
    $w = $frame_segment->Label(-text => " COLOR ", -justify => 'left', -relief => 'groove', 
				   -width => $choice_bar, -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);
    $w = $frame_segment->Label(-text => " RADII ", -justify => 'left', -relief => 'groove', 
				   -width => $choice_bar, -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' );
    $frame_segment->windowCreate('end', -window => $w);


#     $w = $frame_segment->Label(-text => "ON/OFF", -justify => 'left', -relief => 'groove', 
# 			       -background => $Win_background, -foreground => $Win_foreground, 
# 			       -width => $choice_bar , -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' )->grid(
# 	$frame_segment->Label(-text => "ATOMS" ),
# 	$frame_segment->Label(-text => "BONDS" )) ;
# 
# #    $frame_segment->Label(-text => "RIBBONS", -justify => 'left', -relief => 'groove', 
# #				   -background => $Win_background, -foreground => $Win_foreground, 
# #				   -width => $choice_bar , -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' ),
# #    $frame_segment->Label(-text => "SURFACE", -justify => 'left', -relief => 'groove', 
# #				   -background => $Win_background, -foreground => $Win_foreground, 
# #				   -width => $choice_bar , -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' ),
# #    $frame_segment->Label(-text => "COLOR", -justify => 'left', -relief => 'groove', 
# #				   -background => $Win_background, -foreground => $Win_foreground, 
# #				   -width => $choice_bar+1, -borderwidth => 5 )->pack(-side => 'top', -anchor => 'w' )
# #);

    $frame_segment->insert('end', "\n");
}

sub generate_selection_frame
{
    my ( $frame_segment ) = @_ ;

    my $text ; my $word ; my $I ; my $key ;

    my $w ; my $wl ;

    @all_selections = () ;
    foreach $word ( sort keys %selections ) { push( @all_selections, $word ) if ( $word ne "" ) ; }

    if (  scalar ( @all_selections ) == 0 )
    {push( @all_selections, "" ) ; }
    else
    { if ( $all_selections[@all_selections-1] ne "" ) { push( @all_selections, "" ) ; } }

#    print " all selection scalar:", scalar ( @all_selections ), "\n" ;

    for ($I=0; $I < @all_selections ; $I++ ) 
    {
	$key = $all_selections[$I] ;
#	print "selection key:>", $key, "<\n" ;
	if ( $key ne "" ) 
	{
	    $selection_list[$I]{"NAME"} = $selections{$key}{"NAME"} ;
	    $selection_list[$I]{"SELECTION"} = $selections{$key}{"SELECTION"} ;
	    if ( !exists($selections{$key}{"ONOF"} ) ) { $selections{$key}{"ONOF"} = "ON" ; }
	    $selection_list[$I]{"ONOF"} = $selections{$key}{"ONOF"} ;

	    if ( $selection_list[$I]{"ONOF"} eq "ON" ) { $selection_list_flag[$I] = 1 ; } else { $selection_list_flag[$I] = 0 ; }
	    $selection_list[$I]{"ATOM"} = $selections{$key}{"ATOM"} ;
	    $selection_list[$I]{"BOND"} = $selections{$key}{"BOND"} ;
	    $selection_list[$I]{"RIBBON"} = $selections{$key}{"RIBBON"} ;
	    $selection_list[$I]{"SURFACE"} = $selections{$key}{"SURFACE"} ;
	    $selection_list[$I]{"STYLE"} = $selections{$key}{"STYLE"} ;
	    $selection_list[$I]{"RADII"} = $selections{$key}{"RADII"} ;
	}
	else
	{
#	    print $I, " else the selection key:>", $key, "<\n" ;
	    $selection_list[$I]{"NAME"} = "user" ; 
	    $selection_list[$I]{"ONOF"} = "ON" ; 
	    $selection_list[$I]{"SELECTION"} = "" ; 
	    $selection_list_flag[$I] = 1 ; 
	    $selection_list[$I]{"ATOM"} = "NONE" ;
	    $selection_list[$I]{"BOND"} = "LINE" ;
	    $selection_list[$I]{"RIBBON"} = "NONE" ;
	    $selection_list[$I]{"SURFACE"} = "NONE" ;
	    $selection_list[$I]{"STYLE"} = "AUTO" ;
	    $selection_list[$I]{"RADII"} = "VdW" ;
#	    $all_selections[$I] = "xxx" ;
	}
#	print $key, " kind:>", $window_list[$I]{"GROUP"}, "< label item \n" ;
#	print $key, " numb: ", $I, " item ", $all_selections[$I], ":", $max_length, $group_length, "\n" ;
#	print $key, " numb: ", $I, " item ", $all_selections[$I], ":", $selection_list[$I]{"ONOF"}, " ", $selection_list_flag[$I] , "\n" ;
	

	$w = $frame_segment->Entry(
	    -textvariable =>  \$all_selections[$I], 
	    -justify => 'left', -relief => 'groove', 
	    -width => $group_length , -borderwidth => 4 
	    )->pack( -side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_NAME"} = $w ;
	$w->bind("<Return>", [\&hit_select_return,  $I, $frame_segment ] ) ;
	$frame_segment->windowCreate('end', -window => $w);


	$w = $frame_segment->Entry(-textvariable => \$selection_list[$I]{"SELECTION"}, -justify => 'left', -relief => 'groove', 
				   -width => $sel_bar , -borderwidth => 4 )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_SEL"} = $w ;
	$frame_segment->windowCreate('end', -window => $w);


	$wl = $frame_segment->Checkbutton(-text => "On/Off",  -width => $choice_bar -2,
					  -variable => \$selection_list_flag[$I])->pack(-side => 'right');
# 	$wl = $frame_segment->Optionmenu(-width => $onof_bar, -background => $Win_background, -foreground => $Win_foreground  ,
#		       -textvariable => \$selection_list[$I]{"ONOF"}, 
#		       -variable => \$selection_list[$I]{"ONOF"}, 
#		       -options => [ @{$image_cmds{"ONOF"}} ]
# 	    );
	$window_list[$I]{"WINDOW_ONOF"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

#	widgets_segm{$key}{"SEG_ID"} = $w ;
#	print " SEGM list group", $window_list[$I]{"GROUP"}, " x ", join(" - ", @{$image_cmds{"ATOM"}} ), "\n" ;
	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
				-textvariable => \$selection_list[$I]{"ATOM"}, 
				-variable => \$selection_list[$I]{"ATOM"}, 
					 -options => [ @{$image_cmds{"ATOM"}} ]
	    )->pack();
	$window_list[$I]{"WINDOW_ATOM"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"BOND"}, 
		       -variable => \$selection_list[$I]{"BOND"}, 
		       -options => [ @{$image_cmds{"BOND"}} ]
 	    );
	$window_list[$I]{"WINDOW_BOND"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"RIBBON"}, 
		       -variable => \$selection_list[$I]{"RIBBON"}, 
		       -options => [ @{$image_cmds{"RIBBON"}} ]
 	    );
	$window_list[$I]{"WINDOW_RIBBON"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"SURFACE"}, 
		       -variable => \$selection_list[$I]{"SURFACE"}, 
		       -options => [ @{$image_cmds{"SURFACE"}} ]
 	    );
	$window_list[$I]{"WINDOW_SURFACE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"STYLE"}, 
		       -variable => \$selection_list[$I]{"STYLE"}, 
		       -options => [ @{$image_cmds{"STYLE"}} ]
 	    );
	$window_list[$I]{"WINDOW_STYLE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"RADII"}, 
		       -variable => \$selection_list[$I]{"RADII"}, 
		       -options => [ @{$image_cmds{"RADII"}} ]
 	    );
	$window_list[$I]{"WINDOW_RADII"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


	$frame_segment->insert('end', "\n");
    }

}

sub hit_select_return
{
    my ($e, $II, $frame_segment ) = @_ ;
#    my (%z) = @_ ;
    my $w = $e->get ;
    my $wl ;
#    print " Hit_return> entry {", $w, " number: ", $II, " all: ", scalar(@all_selections), "\n" ;
#    $all_selections[$II] = uc($w) ;
    $all_selections[$II] = $w ;
#    $all_selections[$II] = $II ;
#    $window_list[$I]{"GROUP"} = $II ;
     my @new_list ;
 
    my $k ; my $nn = @all_selections ; 
    my $j ;
    $j=0 ;
    for ($k = 0; $k < $nn ; $k++ )
    {
#	print " in hit $k all_selections{", $all_selections[$k], 	 "}" ;
	if ( length($all_selections[$k]) > 0 ) 
	    
#	{ push ( @new_list, $all_selections[$k] ) ; print "PUSH print {", $all_selections[$k], "}\n" ; }
	{ 
	    $all_selections[$j] = $all_selections[$k]; 
	    $window_list[$j]{"WINDOW_NAME"} = $window_list[$k]{"WINDOW_NAME"} ;
	    $window_list[$j]{"WINDOW_SEL"} = $window_list[$k]{"WINDOW_SEL"} ;
	    $window_list[$j]{"WINDOW_ONOF"} = $window_list[$k]{"WINDOW_ONOF"} ;
	    $window_list[$j]{"WINDOW_BOND"} = $window_list[$k]{"WINDOW_BOND"} ;
	    $window_list[$j]{"WINDOW_ATOM"} = $window_list[$k]{"WINDOW_ATOM"} ;
	    $window_list[$j]{"WINDOW_RIBBON"} = $window_list[$k]{"WINDOW_RIBBON"} ;
	    $window_list[$j]{"WINDOW_SURFACE"} = $window_list[$k]{"WINDOW_SURFACE"} ;
	    $window_list[$j]{"WINDOW_STYLE"} = $window_list[$k]{"WINDOW_STYLE"} ;
	    $window_list[$j]{"WINDOW_RADII"} = $window_list[$k]{"WINDOW_RADII"} ;
	    $selection_list[$j]{"NAME"} = $selection_list[$k]{"NAME"} ;
	    $selection_list[$j]{"SELECTION"} = $selection_list[$k]{"SELECTION"} ;
	    $selection_list[$j]{"ONOF"} = $selection_list[$k]{"ONOF"} ;
	    $selection_list_flag[$j] = $selection_list_flag[$k] ; 
	    $selection_list[$j]{"ATOM"} = $selection_list[$k]{"ATOM"} ;
	    $selection_list[$j]{"BOND"} = $selection_list[$k]{"BOND"} ;
	    $selection_list[$j]{"RIBBON"} = $selection_list[$k]{"RIBBON"} ;
	    $selection_list[$j]{"SURFACE"} = $selection_list[$k]{"SURFACE"} ;
	    $selection_list[$j]{"STYLE"} = $selection_list[$k]{"STYLE"} ;
	    $selection_list[$j]{"RADII"} = $selection_list[$k]{"RADII"} ;
#	    print "PUSH $j from $k {", $all_selections[$k], "}\n" ; 
	    $j++ ; 
	}
	else
	{
#	    print " NO PUSH $j from $k {", $all_selections[$k], "}\n" ; 

	}
    }
#    push(@all_selections, "" ) ; 
#    pop (@all_selections ) ;
#    print " Hit_return> middle { all: ", scalar(@all_selections), $j, "\n" ;
    $all_selections[$j] = "" ; $j++ ;
#    print " Hit_return> { all: = blank ", scalar(@all_selections), $j, "\n" ;

    if ( $j > $nn ) 
    {

#	print " $j >> $nn \n" ;
	my $I = $j-1 ;
	my $key = $all_selections[$I] ;
	$wl = $frame_segment->Entry(-textvariable => \$all_selections[$I], 
				    -justify => 'left', -relief => 'groove', 
				    -width => $max_length )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_NAME"} = $wl ;
	$wl->bind("<Return>", [\&hit_select_return,  $I, $frame_segment ] ) ;
	$frame_segment->windowCreate('end', -window => $wl);

	$w = $frame_segment->Entry(-textvariable => \$selection_list[$I]{"SELECTION"}, 
				   -justify => 'left', -relief => 'groove', 
				   -width => $max_length + 20 , -borderwidth => 4 
	    )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_SEL"} = $w ;
	$frame_segment->windowCreate('end', -window => $w);

	$wl = $frame_segment->Checkbutton(-text => "On/Off",
					  -variable => \ \$selection_list_flag[$I])->pack(-side => 'left');
# # 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
# # 					 -textvariable => \$selection_list[$I]{"ONOF"}, 
# # 					 -variable => \$selection_list[$I]{"ONOF"}, 
# # 					 -options => [ @{$image_cmds{"ONOF"}} ]
# # 	    )->pack();
	$window_list[$I]{"WINDOW_ONOF"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"ATOM"}, 
					 -variable => \$selection_list[$I]{"ATOM"}, 
					 -options => [ @{$image_cmds{"ATOM"}} ]
	    )->pack();
	$window_list[$I]{"WINDOW_ONOF"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"BOND"}, 
					 -variable => \$selection_list[$I]{"BOND"}, 
					 -options => [ @{$image_cmds{"BOND"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_BOND"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"RIBBON"}, 
					 -variable => \$selection_list[$I]{"RIBBON"}, 
					 -options => [ @{$image_cmds{"RIBBON"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_RIBBON"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"SURFACE"}, 
					 -variable => \$selection_list[$I]{"SURFACE"}, 
					 -options => [ @{$image_cmds{"SURFACE"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_SURFACE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"STYLE"}, 
		       -variable => \$selection_list[$I]{"STYLE"}, 
		       -options => [ @{$image_cmds{"STYLE"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_STYLE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
		       -textvariable => \$selection_list[$I]{"RADII"}, 
		       -variable => \$selection_list[$I]{"RADII"}, 
		       -options => [ @{$image_cmds{"RADII"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_RADII"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


	$frame_segment->insert('end', "\n");

	if ( $II != $nn) { $window_list[$II+1]{"WINDOW_NAME"}->focus() ; }
 
    }
    elsif ( $j < $nn )
    {

#	print " $j << $nn \n" ;

	$w = $window_list[$nn-1]{"WINDOW_RADII"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_STYLE"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_SURFACE"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_RIBBON"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_BOND"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_ATOM"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_ONOF"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_SEL"} ; $w->destroy() if Tk::Exists($w) ;
	$w = $window_list[$nn-1]{"WINDOW_NAME"} ; $w->destroy() if Tk::Exists($w) ;

	my $start_line = join (".", $nn+2, "8" ) ;
	$frame_segment->delete($start_line, 'end');
	$frame_segment->insert('end', "\n");
 	pop ( @all_selections ) ; pop ( @all_selections ) ; 
	push ( @all_selections, "" ) ;

	my $I = @all_selections - 1 ;
#	print "I $I \n" ;
	$selection_list[$I]{"NAME"} = "none" ; 
	$selection_list[$I]{"SELECTION"} = "" ; 
	$selection_list[$I]{"ONOF"} = "ON" ;
	$selection_list_flag[$I] = 1 ; 
	$selection_list[$I]{"ATOM"} = "NONE" ;
	$selection_list[$I]{"BOND"} = "LINE" ;
	$selection_list[$I]{"RIBBON"} = "NONE" ;
	$selection_list[$I]{"SURFACE"} = "NONE" ;
	$selection_list[$I]{"STYLE"} = "AUTO" ;
	$selection_list[$I]{"STYLE"} = "VdW" ;
	my $key = $all_selections[$I] ;
	$wl = $frame_segment->Entry(-textvariable => \$all_selections[$I], -justify => 'left', -relief => 'groove', 
				   -width => $max_length )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_NAME"} = $wl ;
	$wl->bind("<Return>", [\&hit_select_return,  $I, $frame_segment ] ) ;
	$frame_segment->windowCreate('end', -window => $wl);

	$w = $frame_segment->Entry(-textvariable => \$selection_list[$I]{"SELECTION"}, 
				   -justify => 'left', -relief => 'groove', 
				   -width => $sel_bar , -borderwidth => 4 )->pack(-side => 'top', -anchor => 'w' );
	$window_list[$I]{"WINDOW_SEL"} = $w ;
	$frame_segment->windowCreate('end', -window => $w);

	$wl = $frame_segment->Checkbutton(-text => "On/Off",
					  -variable => \ \$selection_list_flag[$I])->pack(-side => 'right');
# # 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
# # 					 -textvariable => \$selection_list[$I]{"ONOF"}, 
# # 					 -variable => \$selection_list[$I]{"ONOF"}, 
# # 					 -options => [ @{$image_cmds{"ONOF"}} ]
# # 	    )->pack();

	$window_list[$I]{"WINDOW_ONOF"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"ATOM"}, 
					 -variable => \$selection_list[$I]{"ATOM"}, 
					 -options => [ @{$image_cmds{"ATOM"}} ]
	    )->pack();

	$window_list[$I]{"WINDOW_ATOM"} = $wl ;
	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"BOND"}, 
					 -variable => \$selection_list[$I]{"BOND"}, 
					 -options => [ @{$image_cmds{"BOND"}} ]
 	    );
	$window_list[$I]{"WINDOW_BOND"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"RIBBON"}, 
					 -variable => \$selection_list[$I]{"RIBBON"}, 
					 -options => [ @{$image_cmds{"RIBBON"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_RIBBON"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"SURFACE"}, 
					 -variable => \$selection_list[$I]{"SURFACE"}, 
					 -options => [ @{$image_cmds{"SURFACE"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_SURFACE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"STYLE"}, 
					 -variable => \$selection_list[$I]{"STYLE"}, 
					 -options => [ @{$image_cmds{"STYLE"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_STYLE"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);

 	$wl = $frame_segment->Optionmenu(-width => $option_bar, 
					 -textvariable => \$selection_list[$I]{"RADII"}, 
					 -variable => \$selection_list[$I]{"RADII"}, 
					 -options => [ @{$image_cmds{"RADII"}} ]
 	    )->pack();
	$window_list[$I]{"WINDOW_RADII"} = $wl ;
 	$frame_segment->windowCreate('end', -window => $wl);


	$frame_segment->insert('end', "\n");

	$window_list[$I]{"WINDOW_NAME"}->focus() ; 



#	print " new ", scalar(@all_selections)," \n" ;
    }
    else
    {
#	print " ELSE $j == $nn \n" ;
	if ( $II+1 != $nn) { $window_list[$II+1]{"WINDOW_NAME"}->focus() ; }
    }

#    print " Hit_return> array {", @_, "\n" ;
}

sub select_by_segment_init
{
    my $word ;

    my $I ;
    %selections = () ;
    foreach $word ( @my_segm_list ) 
    {
	$I++ ;
	my $sel_id = join( "_", "seg", $word ) ;
	my $sel = join( " ", "segm name", $word ) ;
#	print "segment list ", $word, "\n" ;
	$selections{$sel_id}{"NAME"} = "seg" ;
	$selections{$sel_id}{"SELECTION"} = $sel ;
	$selections{$sel_id}{"ONOF"} = "ON" ;
	$selection_list_flag[$I] = 1 ;
	$selections{$sel_id}{"ATOM"} = "NONE" ;
	$selections{$sel_id}{"BOND"} = "LINE" ;
	$selections{$sel_id}{"RIBBON"} = "NONE" ;
	$selections{$sel_id}{"SURFACE"} = "NONE" ;
	$selections{$sel_id}{"STYLE"} = "WEIGHT" ;
	$selections{$sel_id}{"RADII"} = "VdW" ;
    }
}

sub select_clear
{
    my $word ; my $II ;
    empty_selection_frame ( \$frame_model, \@window_list ) ;
    %selections = () ;
    $II = 1 ;
#    foreach $word ( @my_segm_list ) 
#    {
#	my $sel_id = join( "_", "seg", $word ) ;
#	my $sel = join( " ", "segm name", $word ) ;
##    print "segment list ", $word, "\n" ;
#	$selections{$sel_id}{"NAME"} = "seg" ;
#	$selections{$sel_id}{"SELECTION"} = $sel ;
#	$selections{$sel_id}{"ONOF"} = "ON" ;
#	$selection_list_flag[$II] = 1 ; 
#	$selections{$sel_id}{"ATOM"} = "NONE" ;
#	$selections{$sel_id}{"BOND"} = "LINE" ;
#	$selections{$sel_id}{"RIBBON"} = "NONE" ;
#	$selections{$sel_id}{"SURFACE"} = "NONE" ;
#	$selections{$sel_id}{"STYLE"} = "WEIGHT" ;
#	$selections{$sel_id}{"RADII"} = "VdW" ;
#	$II++;
#    }
    generate_selection_frame ( $frame_model ) ;
}

sub select_by_segment
{
    my $word ; my $II ;
    empty_selection_frame ( \$frame_model, \@window_list ) ;
    %selections = () ;
    $II = 0 ;
    foreach $word ( @my_segm_list ) 
    {
	my $sel_id = join( "_", "seg", $word ) ;
	my $sel = join( " ", "segm name", $word ) ;
#    print "segment list ", $word, "\n" ;
	$selections{$sel_id}{"NAME"} = "seg" ;
	$selections{$sel_id}{"SELECTION"} = $sel ;
	$selections{$sel_id}{"ONOF"} = "ON" ;
	$selection_list_flag[$II] = 1 ; 
	$selections{$sel_id}{"ATOM"} = "NONE" ;
	$selections{$sel_id}{"BOND"} = "LINE" ;
	$selections{$sel_id}{"RIBBON"} = "NONE" ;
	$selections{$sel_id}{"SURFACE"} = "NONE" ;
	$selections{$sel_id}{"STYLE"} = "WEIGHT" ;
	$selections{$sel_id}{"RADII"} = "VdW" ;
	$II++;
    }
    generate_selection_frame ( $frame_model ) ;
}

sub select_by_chain
{
    my $word ;	my $sel ; my $II = 0;
    empty_selection_frame ( \$frame_model, \@window_list ) ;
    %selections = () ;
    foreach $word ( @my_chain_list ) 
    {
	my $sel_id = join( "_", "chain", $word ) ;

	my @sel_data = split (" ", $word ) ;
	if ( @sel_data == 1 )
	{
	    $sel = join( " ", "segm name", $sel_data[0], ".and. chain name \" \" ") ;
	}
	else
	{
	    $sel = join( " ", "segm name", $sel_data[0], ".and. chain name",  $sel_data[1] ) ;
	}
#    print "segment list ", $word, "\n" ;
	$sel_id = join("_", split(" ", $sel_id)) ; #$ssel_id =~ s/\ /_/g ; 
	$selections{$sel_id}{"NAME"} = "chain" ;
	$selections{$sel_id}{"SELECTION"} = $sel ;
	$selections{$sel_id}{"ONOF"} = "ON" ;
	$selection_list_flag[$II] = 1 ; 
	$selections{$sel_id}{"ATOM"} = "NONE" ;
	$selections{$sel_id}{"BOND"} = "LINE" ;
	$selections{$sel_id}{"RIBBON"} = "NONE" ;
	$selections{$sel_id}{"SURFACE"} = "NONE" ;
	$selections{$sel_id}{"STYLE"} = "WEIGHT" ;
	$selections{$sel_id}{"RADII"} = "VdW" ;
	$II++;
    }
    generate_selection_frame ( $frame_model ) ;

}

sub tk_rgb_colors
{
    print "RGB colors\n" ;
    my $mw = MainWindow->new;
    $mw->title("Tk: RGB colors");
    $mw->minsize(360,180);
    $mw->geometry( "360x220+1000+0" ) ;
    $mw->packPropagate( 0 ) ;

    my $f = $mw->Frame->pack(-side => 'bottom');
    $f->Button(-text => "Close", 
	       -command => sub { 
		   &create_macro_vlt () ;
		   $mw->destroy() if Tk::Exists($mw) ;
	       }
	)->pack(-side => 'left');

    $f->Button(-text => "Write-It",
	       -command => \&create_macro_vlt )->pack(-side => 'bottom');
    my $t = $mw->Scrolled("Text", -width => $max_length + 30, 
                   -wrap => 'none')->pack(-expand => 10, -fill => 'both', -side => 'top' );
    $t->configure(-state => 'normal');

    $w = $t->Label(-text => " COLOR 1 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
 	$t->windowCreate('end', -window => $w);
   $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_1"}[0], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_1"}[1], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_1"}[2], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");

    $w = $t->Label(-text => " COLOR 2 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
 	$t->windowCreate('end', -window => $w);
   $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_2"}[0], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_2"}[1], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_2"}[2], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");

    $w = $t->Label(-text => " COLOR 3 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
 	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_3"}[0], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_3"}[1], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_3"}[2], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");

#    $w = $w = $t->Label(-text => " COLOR 4 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
# 	$t->windowCreate('end', -window => $w);
#    $w = t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_4"}[0], -width => 4  )->pack( -side => 'right' );
#	$t->windowCreate('end', -window => $w);
#    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_4"}[1], -width => 4  )->pack( -side => 'right' );
#	$t->windowCreate('end', -window => $w);
#    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_4"}[2], -width => 4  )->pack( -side => 'right' );
#	$t->windowCreate('end', -window => $w);
#	$t->insert('end', "\n");

    $w = $w = $t->Label(-text => " COLOR 4 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
 	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_4"}[0], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_4"}[1], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_4"}[2], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");

    $w = $w = $t->Label(-text => " COLOR 5 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
 	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_5"}[0], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_5"}[1], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_5"}[2], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");

    $w = $t->Label(-text => " COLOR 6 RGB [0..1]", -relief => 'groove' )->pack(-side => 'right' );
 	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_6"}[0], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_6"}[1], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
    $w = $t->Entry(  -textvariable => \$my_main{"IMAGE_COLOR_6"}[2], -width => 4  )->pack( -side => 'right' );
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");

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

#     my $c = $mw->Canvas( qw/-width 200 -height 60 -background cyan/ )->grid ;
#     my $w = 100 ;
#     my $h = 20 ;
#     my $i = $c->Photo(-width => $w, -height => $h ) ;
#     $c->createImage(100,39, -image => $i ) ;
#     $i->put ('#6363ce', -to => 0, 0, 2, $h) ;
#     my $col ;
#     for ( $col = 4; $col < $w; $col++) 
#     {
# 	print $col, "\n" ;
# 	$i->put ('#6363ce', to => $col, 0, $col+1, $h) ;
# 	$mw->upadate;
# 	$mw->after(1) ;
# 	
#     }
#     
    
}

sub empty_selection_frame
{
    my ( $frame, $list ) = @_ ;
    my $I ; my $n_groups ; my $w ; my $key ; my $nn=0 ;

    $n_groups = scalar ( @{$list} ) ;
    for ($I=$n_groups; $I > 0 ; $I-- ) 
    {
#	foreach $key ( keys %{${$list}[$I-1]} )
	$nn = 0 ;
	foreach $key ( keys %{${$list}[$I-1]} )
	{
	    my $window = join ("_", "WINDOW", $key ) ;
	    $w = ${$list}[$I-1]{$key} ; $w->destroy() if Tk::Exists($w) ;
	$nn++ ;
	}
    }
    my $start_line = join (".", "4", $nn ) ;
    ${$frame}->delete("2.7", 'end') if Tk::Exists(${$frame}) ;
    ${$frame}->insert('end', "\n") if Tk::Exists(${$frame}) ;
}

sub create_macro
{
    my $I ; my $key ;
    my %sl_keys ;
    %sl_keys = () ;
    for ($I=0; $I < @all_selections ; $I++ ) 
#    foreach $key ( @all_selections )
    {
	$key = $all_selections[$I] ;
	if ( $key ne "" ) 
	{
	    $selections{$key}{"NAME"} = $selection_list[$I]{"NAME"} ;
	    $selections{$key}{"SELECTION"} = $selection_list[$I]{"SELECTION"} ;
#	    print "list flag:",  $selection_list_flag[$I], "\n" ;
	    if ( $selection_list_flag[$I] == 0 ) { $selection_list[$I]{"ONOF"} = "OFF" ; } else { $selection_list[$I]{"ONOF"} = "ON" ; }
	    $selections{$key}{"ONOF"} = $selection_list[$I]{"ONOF"} ;
	    $selections{$key}{"ATOM"} = $selection_list[$I]{"ATOM"} ;
	    $selections{$key}{"BOND"} = $selection_list[$I]{"BOND"} ;
	    $selections{$key}{"RIBBON"} = $selection_list[$I]{"RIBBON"} ;
	    $selections{$key}{"SURFACE"} = $selection_list[$I]{"SURFACE"} ;
	    $selections{$key}{"STYLE"} = $selection_list[$I]{"STYLE"} ;
	    $selections{$key}{"RADII"} = $selection_list[$I]{"RADII"} ;
	    $sl_keys{$key} = "DONE" ;
	}
    }

    foreach $key (keys %selections ) 
    {
	if ( !exists($sl_keys{$key} ) ) 
	{
#	    print "DELETE SELECTONS KEY: $key \n" ;
	    delete $selections{$key} ;
	}
    }

foreach $key (keys %my_main ) 
{
    if ( $key =~ "IMAGE_HASH" )
    {
	delete $my_main{$key} ;
    }
}


    my $name ;
    foreach $key (keys %selections ) 
    {
#	print " create WORKING keys {$key} }\n" ;
	foreach $name (keys %{$selections{$key}} ) 
	{
	    my $word = join( "_", "IMAGE_HASH", $key, $name ) ;
	    @{$my_main{$word}} = ( $selections{$key}{$name} ) ;
#	    print " image selection: $key $name value = {", $selections{$key}{$name}, "}\n" ;
	}
    }
if ( $hydrogen_flag == 1 ) 
{ @{$my_main{"IMAGE_HYDRO"}} = ( "ON" ) ; }
else
{ @{$my_main{"IMAGE_HYDRO"}} = ( "OFF" ) ; }

if ( $chain_memory_flag == 1 ) 
{ @{$my_main{"IMAGE_CHAIN"}} = ( "ON" ) ; }
else
{ @{$my_main{"IMAGE_CHAIN"}} = ( "OFF" ) ; }
#print "tk_re_image> ", $chain_memory_flag, " ",  @{$my_main{"IMAGE_CHAIN"}}, "\n" ;

if ( $my_file_flag == 1 ) 
{ @{$my_main{"IMAGE_MY_FILE"}} = ( "ON" ) ; }
else
{ @{$my_main{"IMAGE_MY_FILE"}} = ( "OFF" ) ; }

@{$my_main{"IMAGE_MY_FILE_NAME"}} = ( $image_my_file_name ) ;

main_config::write_main_config_file ( "main_config.data", %my_main ) ; 
#print "image fraction",   $my_main{"IMAGE_RADII_FRACTION"}[0], "\n" ;
main_config::create_re_image ( %my_main ) ;

}

sub create_macro_vlt
{
    print " VLT entry\n" ;
}
