#/usr/bin/perl -w
use diagnostics;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;
use Tie::IxHash;
use subs qw/nbook_new/;

my $mw = Gtk2::Window->new ('toplevel');
$mw->set_size_request(800,300);
$mw->set_position('center');
$mw->set_title("Metavis");
$mw->signal_connect(delete_event => sub { Gtk2->main_quit; FALSE; });

my $vbox = Gtk2::VBox->new(FALSE, 1);

$mw->add($vbox);


$nb=Gtk2::Notebook->new;
$nb->set_size_request(0,0);
$nb->popup_enable;
$tabcount=0;


my $menubar = Gtk2::MenuBar->new;
$menubar->set_size_request(500,30);
### menubar item File ###
$menubar_item_file= Gtk2::MenuItem->new("File");
	### menu file ###
	$menu_file= Gtk2::Menu->new();
		$menu_file->append(Gtk2::SeparatorMenuItem->new);
		### menu file items ###
		$menu_file_new = Gtk2::MenuItem->new('_New');
		$menu_file_new->signal_connect(activate =>sub{nbook_new($nb);});
		$menu_file->append($menu_file_new);
		$menu_file_open = Gtk2::MenuItem->new('_Open');
		$menu_file_open->signal_connect(activate => sub{show_chooser('Open file','open',ret_filter("text"))});
		$menu_file->append($menu_file_open);
		$menu_file_close=Gtk2::MenuItem->new('_Close');
		$menu_file_close->signal_connect(activate =>sub{nbook_close($nb);});
		$menu_file->append($menu_file_close);
		$menu_file_save = Gtk2::MenuItem->new('_Save');
		#$menu_file_save->signal_connect(
		$menu_file->append($menu_file_save);
		$menu_file_saveas = Gtk2::MenuItem->new('_Save as');
		$menu_file->append($menu_file_saveas);
		$menu_file_saveall = Gtk2::MenuItem->new('_Save all');
		$menu_file->append($menu_file_saveall);
		
		$menu_file->append(Gtk2::SeparatorMenuItem->new);
		$menu_file_quit = Gtk2::MenuItem->new('_Quit');
		$menu_file_quit->signal_connect(activate => sub { Gtk2->main_quit; FALSE;});
		$menu_file->append($menu_file_quit);
		## end menu file items ###
	### end menu file ###
$menubar_item_file->set_submenu($menu_file);
$menubar->append($menubar_item_file);
### end menubar item File ###

### menubar item Metatool ###
$menubar_item_metatool= Gtk2::MenuItem->new("Metatool");
	### menu metatool ###
	$menu_metatool=Gtk2::Menu->new();
		$menu_metatool->append(Gtk2::SeparatorMenuItem->new);
		### menu metatool items ###
		$menu_metatool_runc = Gtk2::MenuItem->new('_Run Current');
		$menu_metatool->append($menu_metatool_runc);
		$menu_metatool_runf = Gtk2::MenuItem->new('_Run file');
		$menu_metatool->append($menu_metatool_runf);
		$menu_metatool_rund = Gtk2::MenuItem->new('_Run dir');
		$menu_metatool->append($menu_metatool_rund);
		### end menu metatool items ###
$menubar_item_metatool->set_submenu($menu_metatool);	
$menubar->append($menubar_item_metatool);
### end menubar item Metatool ###

### menubar item Graphview ###
$menubar_item_graphview= Gtk2::MenuItem->new("GraphView");
	### menu graphview ###
	$menu_graphview=Gtk2::Menu->new();
		$menu_graphview->append(Gtk2::SeparatorMenuItem->new);
		### menu graphview items ###
		$menu_graphview_runc = Gtk2::MenuItem->new('_Run Current');
		$menu_graphview->append($menu_graphview_runc);
		$menu_graphview_runf = Gtk2::MenuItem->new('_Run file');
		$menu_graphview->append($menu_graphview_runf);
		$menu_graphview_rund = Gtk2::MenuItem->new('_Run dir');
		$menu_graphview->append($menu_graphview_rund);
		$menu_graphview_vf = Gtk2::MenuItem->new('_View file');
		$menu_graphview->append($menu_graphview_vf);
		$menu_graphview_vd = Gtk2::MenuItem->new('_View dir');
		$menu_graphview->append($menu_graphview_vd);
		$menu_graphview_compare = Gtk2::MenuItem->new('_Compare files');
		$menu_graphview->append($menu_graphview_compare);
		### end menu graphview items ###
$menubar_item_graphview->set_submenu($menu_graphview);
$menubar->append($menubar_item_graphview);
### end menubar item Graphview ###

### menubar item Preferences ###
$menubar_item_prefs= Gtk2::MenuItem->new("Preferences");
	$menu_options=Gtk2::Menu->new();
	$menu_options_ops=Gtk2::MenuItem->new("Metavis Options");
	$menu_options_ops->signal_connect(activate => \&dialog_config);
	$menu_options->append($menu_options_ops);
$menubar_item_prefs->set_submenu($menu_options);
$menubar->append($menubar_item_prefs);
### end menubar item Preferences ###

### menubar item Help ###
$menubar_item_help= Gtk2::MenuItem->new("Help");
$menubar_item_help->set_right_justified(TRUE);
	### menu help ###
	$menu_help=Gtk2::Menu->new();
	$menu_help->append(Gtk2::SeparatorMenuItem->new);
		### menu help items ###
		$menu_help_help=Gtk2::MenuItem->new('_Help');
		$menu_help->append($menu_help_help);
		$menu_help_about=Gtk2::MenuItem->new('_About');
		$menu_help->append($menu_help_about);
		### end menu help items ###
$menubar_item_help->set_submenu($menu_help);
$menubar->append($menubar_item_help);
### end menubar item help ###

my $eventbox = Gtk2::EventBox->new();

#$eventbox->signal_connect('button-release-event' => sub {
# 		my ($widget,$event) = @_;		
# 		my $button_nr = $event->button;
# 		#make sure it was the right mouse button				
# 		($button_nr == 3)&&($menu_file->popup(undef,undef,undef,undef,0,0));
#		print "SOMETHING HERE\n";
# 		
# 	});

$vbox->pack_start($menubar,FALSE,FALSE,0);

$nb=Gtk2::Notebook->new;
$nb->set_size_request(0,0);
$nb->popup_enable;
$tabcount=0;

$expander = Gtk2::Expander->new_with_mnemonic('Show pdlshell');
$expander->signal_connect_after('activate' => sub {
			
	if($expander->get_expanded){
		$expander->set_label('Hide pdlshell');
		$pdlframe=Gtk2::Frame->new;
		$pdlframe->set_shadow_type ('in');
		$pdlframe->set_size_request(0,80);
		$pdlframe->show;
		$expander->add($pdlframe);
		
				
	}else{ 
		$expander->set_label('Show pdlshell');
		$expander->remove($pdlframe);
	}
});

$vbox->pack_start($nb,TRUE,TRUE,0);
$vbox->pack_end($expander,FALSE,FALSE,0);
$vbox->show_all();

$mw->show;

### Loadin the options from "config.txt"(if they are existing there) ###

open (IN, "pwd|") or die "CANT EXEC PWD:$!\n";
chomp($curdir=<IN>);# holds the current dir PWD
close IN;

%variables=();
tie %variables, "Tie::IxHash";
%results=();
tie %results, "Tie::IxHash";
do "config.pl";
$metadir=\$variables{Metadir};
$metainput=\$variables{Metainput};
$metaoutput=\$variables{Metaoutput};
$gvdir=\$variables{Gvdir};
$gvinput=\$variables{Gvinput};
$gvoutput=\$variables{Gvoutput};
$gvpictype=\$variables{Gvpictype};

### end load opts


Gtk2->main;

####### SUBS #######

sub dialog_config{
$dialog=Gtk2::Dialog->new('GraphView options',
			  undef,
			  [qw/modal destroy-with-parent/],
			  'gtk-cancel' => 'cancel',
			  'gtk-ok' => 'ok');
$dialog->set_position('center');
$dialog->set_size_request(400,230);

$vbox=$dialog->vbox;
	$vbox->add($vbox2=Gtk2::VBox->new());
	$labgv=Gtk2::Label->new();
	$labgv->set_markup('<span foreground="DarkRed" size="medium"><b>Graphview Options</b></span>');
	$labmet=Gtk2::Label->new();
	$labmet->set_markup('<span foreground="DarkRed" size="medium"><b>Metatool Options</b></span>');
	$table1=Gtk2::Table->new(1,2,FALSE);
	$table1->attach_defaults($labgv, 0,1,0,1);
	$table1->attach_defaults($labmet, 1,2,0,1);
	$vbox2->pack_start($table1,FALSE,FALSE,0);
	$vbox2->show;
	
	$vbox->add(Gtk2::HSeparator->new());		
	$table=Gtk2::Table->new(8,2,FALSE);
	$table->set_col_spacings(10);
	#### graphview ####
	$gvlabel_dir=Gtk2::Label->new('Program Dir');
	$table->attach_defaults($gvlabel_dir, 0,1,0,1);
	$gventry_dir=Gtk2::Entry->new();
	$gventry_dir->set_text("$$gvdir");
	$gventry_dir->signal_connect(changed => sub{ $$gvdir=$gventry_dir->get_text()});
	$table->attach_defaults($gventry_dir,0,1,1,2);
				
	$gvlabel_indir=Gtk2::Label->new('Input Dir');
	$table->attach_defaults($gvlabel_indir, 0,1,2,3);
	$gventry_indir=Gtk2::Entry->new();
	$gventry_indir->set_text("$$gvinput");
	$gventry_indir->signal_connect(changed => sub{ $$gvinput=$gventry_indir->get_text()});
	$table->attach_defaults($gventry_indir,0,1,3,4);
				
	$gvlabel_outdir=Gtk2::Label->new('Output Dir');
	$table->attach_defaults($gvlabel_outdir, 0,1,4,5);
	$gventry_outdir=Gtk2::Entry->new();
	$gventry_outdir->set_text("$$gvoutput");
	$gventry_outdir->signal_connect(changed => sub{ $$gvoutput=$gventry_outdir->get_text()});
	$table->attach_defaults($gventry_outdir,0,1,5,6);
				
	$gvlabel_format=Gtk2::Label->new('Graphic format: svg,png,eps');
	$table->attach_defaults($gvlabel_format, 0,1,6,7);
	$gventry_format=Gtk2::Entry->new();
	$gventry_format->set_text("$$gvpictype");
	$gventry_format->signal_connect(changed => sub{ $$gvpictype=$gventry_format->get_text()});
	$table->attach_defaults($gventry_format,0,1,7,8);
	
	#### metatool ####
	$label_dir=Gtk2::Label->new('Program Dir');
	$table->attach_defaults($label_dir, 1,2,0,1);
	$entry_dir=Gtk2::Entry->new();
	$entry_dir->set_text("$$metadir");
	$entry_dir->signal_connect(changed => sub{ $$metadir=$entry_dir->get_text()});
	$table->attach_defaults($entry_dir,1,2,1,2);
				
	$label_indir=Gtk2::Label->new('Input Dir');
	$table->attach_defaults($label_indir, 1,2,2,3);
	$entry_indir=Gtk2::Entry->new();
	$entry_indir->set_text("$$metainput");
	$entry_indir->signal_connect(changed => sub{ $$metainput=$entry_indir->get_text()});
	$table->attach_defaults($entry_indir,1,2,3,4);
				
	$label_outdir=Gtk2::Label->new('Output Dir');
	$table->attach_defaults($label_outdir, 1,2,4,5);
	$entry_outdir=Gtk2::Entry->new();
	$entry_outdir->set_text("$$metaoutput");
	$entry_outdir->signal_connect(changed => sub{ $$metaoutput=$entry_outdir->get_text()});
	$table->attach_defaults($entry_outdir,1,2,5,6);
			
$vbox->pack_start($table,FALSE,FALSE,0);

$vbox->show_all();
$dialog->show();
$response=$dialog->run;
	if($response eq "ok"){
	print "Clicked OK\n";
	#$nvg_entry_dir=$gventry_dir->get_text();
	#$$gvdir=$gventry_indir->get_text();
	print "gvd $$gvdir\n";
	
	open (MCONF, ">",glob("$curdir/config.txt")) or die "Problem:$!\n";
	print MCONF "\#Configuration file for Metavis:\n\[Metatool Options\]\nMetadir:$$metadir\nMetainput:$$metainput\nMetaoutput:$$metaoutput\n";
	close MCONF;
	open (MCONF, ">>",glob("$curdir/config.txt")) or die "Problem2:$!";
	print MCONF "\n#\[GraphView\]\nGvdir:$$gvdir\nGvinput:$$gvinput\nGvoutput:$$gvoutput\nGvpictype:$$gvpictype\n";
	close MCONF;
	$dialog->destroy;
	}else{
	print "Clicked Cancel\n";
	$dialog->destroy;
	}

}

sub show_chooser {
#---------------------------------------------------
#Pops up a standard file chooser--------------------
#Specify a header to be displayed-------------------
#Specify a type depending on your needs-------------
#Optionally add a filter to show only certain files-
#will return a path, if valid----------------------
#---------------------------------------------------
    my($heading,$type,$filter) =@_;
#$type can be:
#* 'open' 
#* 'save' 
#* 'select-folder'
#* 'create-folder' 
    my $file_chooser =  Gtk2::FileChooserDialog->new ( 
                            $heading,
                            undef,
                            $type,
                            'gtk-cancel' => 'cancel',
                            'gtk-ok' => 'ok'
                        );
    (defined $filter)&&($file_chooser->add_filter($filter));
    
    #if action = 'save' suggest a filename
    ($type eq 'save')&&($file_chooser->set_current_name("suggeste_this_file.name"));

    my $filename;

    if ('ok' eq $file_chooser->run){    
       $filename = $file_chooser->get_filename;
	   nbook_open($filename);
       print "filename $filename\n";
    }

    $file_chooser->destroy;

    if (defined $filename){
        if ((-f $filename)&&($type eq 'save')) {
            my $overwrite =show_message_dialog( $mw,
                                                'question'
                                                ,'Overwrite existing file:'."<b>\n$filename</b>"
                                                ,'yes-no'
                                    );
            return  if ($overwrite eq 'no');
        }
        return $filename;
    }
    return;
}

sub ret_filter {
$fil_kind=shift;
print "FK: $fil_kind\n";
	if($fil_kind eq "images"){
		my $filter = Gtk2::FileFilter->new();
		$filter->set_name($fil_kind);
    		$filter->add_mime_type("image/png");
		$filter->add_mime_type("image/eps");
		$filter->add_mime_type("image/svg");
    		return $filter;
	}elsif($fil_kind eq "text"){
		my $filter = Gtk2::FileFilter->new();
		$filter->set_name($fil_kind);
    		$filter->add_mime_type("text/txt");
		$filter->add_pattern("*.dat");
		$filter->add_pattern("*.pdl");
    		return $filter;
	}else{
	print "Does not handle such files!\n";
	return;
	}
}

sub nbook_new{
($nb,$data)=@_;
$nframe1=Gtk2::Frame->new("Input data $tabcount");
$nframe2=Gtk2::Frame->new("Results$tabcount");
$nvbox1=Gtk2::VBox->new;
$nvbox1->set_size_request(800,0);
$nvbox2=Gtk2::VBox->new;
$nframe1->add($nvbox1);
$nframe2->add($nvbox2);
$btn_rd=Gtk2::Button->new("Run data");
$btn_rd->signal_connect(clicked=>\&run_data);
$expand_cd = Gtk2::Expander->new_with_mnemonic('Show Create data');
$expand_cd->signal_connect_after('activate' => \&expand_create_data_form,$tabcount);
$hpaned=Gtk2::HPaned->new;
$nvbox1->pack_end($btn_rd,FALSE,FALSE,0);
$nvbox1->pack_end($expand_cd,FALSE,FALSE,1);
$hpaned->add1($nframe1);
$hpaned->add2($nframe2);
$label=Gtk2::Label->new("TAB $tabcount"); 
$nb->append_page($hpaned,$label); 
$tabcount++;
$nb->show_all;
}

sub nbook_close{
$page=$nb->get_current_page;
$nb->remove_page ($page);
$nb->queue_draw;
$tabcount--;
}
sub nbook_open{
$filename=shift;
$frame = Gtk2::Frame->new("$filename");
$label=Gtk2::Label->new("TAB $tabcount");
$tabcount++;
$nb->append_page ($frame,$label);
$nb->show_all;
}

sub run_data{
print "Run clicked\n";
}

sub expand_create_data_form{
($expand_crd,$mess)=@_;
	if($expand_crd->get_expanded){
		$expand_crd->set_label('Hide Create data');
		$dframe=(&create_data_form);
		$dframe->show;
		$expand_crd->add($dframe);
		print "Added $mess\n";
		return $expand_crd;
	}else{ 
		$expand_crd->set_label('Show Create Data');
		$expand_crd->remove($dframe);
		print "Removed $mess\n";
		return $expand_crd;
	}
	
}

sub create_data_form{
($crdframe,$data)=@_;
$crdframe=Gtk2::Frame->new;
$crdframe->set_shadow_type ('in');
$crdframe->set_size_request(500,54);
$crdvbox=Gtk2::VBox->new;
$crdtable=Gtk2::Table->new(2,8,FALSE);
$btn_add_enz=Gtk2::Button->new("Add Reaction");
$btn_add_enz->signal_connect(clicked=>sub{print "ADD ENZYME\n";});
$btn_add_met=Gtk2::Button->new("Add Metabolite");
$btn_add_met->signal_connect(clicked=>sub{print "ADD METABOLITE\n";});
$entry_add_enz=Gtk2::Entry->new;
$entry_add_met=Gtk2::Entry->new;
$crdtable->attach_defaults($btn_add_enz,0,1,0,1);
$crdtable->attach_defaults($btn_add_met,0,1,1,2);
$crdtable->attach_defaults($entry_add_enz,1,8,0,1);
$crdtable->attach_defaults($entry_add_met,1,8,1,2);
$crdvbox->pack_start($crdtable,FALSE,FLASE,0);
$crdvbox->show_all;
$crdframe->add($crdvbox);
return $crdframe;
}
