#!/usr/bin/perl
# gpp - a Database Processor
# Copyright (C) 2008 Laurent Mazet

# Changelog:
# - 0.11.0
#   * some menu items have to be disabled
#   * matlabify all  m-files
#   * define a new histogram tool where each bin ccntains same number of data
#   * fix histogram tool, historams have to be computed independently for each variable
#   * fix graph display
#   * adapt for bi-variant operator
#   * enhance menu
#   * empty value are replaced by NaN
#   * integrate new code from Stephan
# - 0.10.0
#   * implement workbench saving for multiple tools processing
#   * info message when processing db or tools
#   * propagate variable after processing db
#   * refresh status bar more often
#   * structural variable in resample
#   * variable propagation bug
# - 0.9.0
#   * help window
#   * apropos window
#   * octave does not work on macos
#   * link can loop on box
#   * add bootstrap
# - 0.8.0
#   * reverse axis in graph/histogram
#   * parameters order is not preserved
#   * can lose control of box outside canvas
#   * miss lots of message for status bar (could be better)
# - 0.7.0
#   * need a frame and a name to make box more pleasent
#   * when connect a new arrow, previous arrow is reset
#   * graph is buggy
#   * value is buggy
#   * process is buggy
# - 0.6.0
#   * almost ready
#   * ...
# - 0.5.0
#   * ...
# - 0.4.0
#   * ...
# - 0.3.0
#   * ...
# - 0.2.0
#   * ...
# - 0.1.0
#   * ...
# - 0.0.1
#   * initial release

# Potential bugs:
# - none

# library
use strict;
use warnings;

use constant TRUE => 1;
use constant FALSE => 0;

use Gtk2 qw/-init/;
use Gtk2::SimpleMenu;
use Gnome2::Canvas;
use POSIX qw(locale_h);
use Data::Dumper;

# constants
use constant version => "0.11.0";

# octave pqth
our $dbprocessdir = $ENV{HOME}."/dbprocess";
our $octave = `which octave`;
$octave = '/Applications/Octave.app/Contents/Resources/bin/octave'
  if (!$octave);
chomp($octave);

our $tmpcsv = $dbprocessdir."/tmp/db.csv";
our $tmpoct = $dbprocessdir."/tmp/script.m";
our $tmppng = $dbprocessdir."/tmp/pixmap.png";
our $tmpwb  = $dbprocessdir."/tmp/wb.mat";

# gobal variables
use vars qw/$dbselected @dblist $toolsselected @toolslist
            $canvas $process_db_button $process_tools_button $window/;

unshift(@INC, $dbprocessdir);

require box;
require db;
require misc;
require tools;
require displays;
require menu;

# process argument
foreach my $arg (@ARGV) {
  ### ...
}

# main window
$window = Gtk2::Window->new;
$window->signal_connect ("delete_event" , sub{Gtk2->main_quit();});
$window->signal_connect ("destroy", sub{exit});
$window->set_title ("Database Processor");
$window->set_border_width (2);
$window->set_default_size (640, 480);

# main box
my $window_box = Gtk2::VBox->new (FALSE, 0);
$window->add($window_box);

# status bar
my $status_frame = Gtk2::Frame->new ();
$status_frame->set_shadow_type ("etched-out");
$window_box->pack_end ($status_frame, FALSE, FALSE, 0);
my $status_box = Gtk2::HBox->new (FALSE, 0);
$status_frame->add ($status_box);
our $statusbar = Gtk2::Statusbar->new ();
our $sbi = 0;
####$statusbar->set_justify ("left");
$status_box->pack_start ($statusbar, TRUE, TRUE, 1);

# menu bar
my $menu_widget = $menu::bar->{widget};
my $menu_accel_group = $menu::bar->{accel_group};
$menu::bar->get_widget('/File/Close Database')->set_sensitive(FALSE);
#$menu::bar->get_widget('/Help/Introduction')->set_sensitive(FALSE);
$window_box->pack_start ($menu_widget, FALSE, FALSE, 0);
$window->add_accel_group($menu_accel_group);

# action bar
my $bar_widget = Gtk2::HBox->new (FALSE, 0);
$window_box->pack_start ($bar_widget, FALSE, FALSE, 2);
$process_tools_button = Gtk2::Button->new_with_label ("Process tools");
$process_tools_button->signal_connect ("clicked",
  sub { for (0..$#::toolslist)
          { db::process ('TOOL', $::toolslist[$_])
              if ((defined($::toolslist[$_])) &&
                  (!defined($::toolslist[$_]->{from}))) } } );
$bar_widget->pack_start ($process_tools_button, FALSE, FALSE, 2);
$process_tools_button->set_sensitive(FALSE);
$process_db_button = Gtk2::Button->new_with_label ("Process DB");
$process_db_button->signal_connect ("clicked",
  sub { for (0..$#dblist)
          { db::process ('DB', $dblist[$_])
              if ($dbselected == $_) } } );
$bar_widget->pack_start ($process_db_button, FALSE, FALSE, 0);
$process_db_button->set_sensitive(FALSE);

# canvas
$canvas = Gnome2::Canvas->new_aa;
$canvas->set_scroll_region (0, 0, 800, 640);
my $scroller = Gtk2::ScrolledWindow->new;
$scroller->add ($canvas);
$scroller->set_shadow_type ("in");
$window_box->pack_end ($scroller, TRUE, TRUE, 3);

$window->show_all ();

# disable as those features are not yet implemented
$menu::bar->get_widget('/Tools/Methods/Nested Bootstap-t')->set_sensitive(FALSE);
$menu::bar->get_widget('/Tools/Methods/Bootstap Percentile')->set_sensitive(FALSE);
$menu::bar->get_widget('/Tools/Bi-variance/Mean')->set_sensitive(FALSE);

# main loop
Gtk2->main;
