#!/usr/bin/perl -w
use strict;
use warnings;
use Gtk2 '-init';

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

use constant HORI_ALIG  => 0.5;

our $command_num=0;

#------------------------------------------------------GUI DESIGN---------------------------------------------------------#
#---------Window----------#
my $window = Gtk2::Window->new;
$window->signal_connect (destroy => sub { Gtk2->main_quit; });

#---------inner-vbox----------#
my $inner_vbox = Gtk2::VBox->new(FALSE, 4);
$window->add($inner_vbox);#'add' is used when the container can contain only one child.

#----------multiline textView-------------#
my $view = Gtk2::TextView->new();
my $buffer = $view->get_buffer();
$view->set_size_request(850,500);
$view->set_editable (FALSE);
our $textmark = $buffer->create_mark('end_mark',$buffer->get_end_iter,FALSE);

my $scrolls = Gtk2::ScrolledWindow->new();
$scrolls->set_policy('automatic','always');
$scrolls->set_shadow_type('in');
$scrolls->add ($view);

my $alig_text_view = Gtk2::Alignment->new(0,0,1,1);
$alig_text_view->add($scrolls);
$inner_vbox->pack_start($alig_text_view,TRUE,TRUE,10);

#---------hbox for lable and entrie----------#
my $hbox1 = Gtk2::HBox->new(FALSE,6);
my $label_cmd = Gtk2::Label->new('SAMPLE');

my $cmd_history_list = Gtk2::ListStore->new('Glib::String');
my $cmd_combo = Gtk2::ComboBoxEntry->new_with_model($cmd_history_list,0);
$cmd_combo->set_size_request(850,25);
$hbox1->pack_start($label_cmd,FALSE,FALSE,0);
$hbox1->pack_start($cmd_combo,FALSE,FALSE,0);
$inner_vbox->pack_start($hbox1,FALSE,FALSE,2);

#---------run-button----------#
my $run_button = Gtk2::Button->new();
my $image = Gtk2::Image->new_from_file ("ch_scaled.gif");
$run_button->set_image($image);
$inner_vbox->pack_start($run_button, FALSE, FALSE, 0);

#------------------------------------------------------END OF GUI DESIGN---------------------------------------------------------#
push(my @args,$cmd_combo);
push(@args,$buffer);
push(@args,$view);
push(@args,$textmark);

$run_button->signal_connect (clicked => \&run_button_sub,\@args);

sub run_button_sub{
  (my $button, my $wed_arr) = @_;
  my $cmd = ($wed_arr->[0])->child->get_text();
  my $buffer = $wed_arr->[1];
  my $view = $wed_arr->[2];
  my $textmark = $wed_arr->[3];
  if(length($cmd) == 0) {return;}

  $command_num++;
  #printing the output on textview
  my $text_tag = $buffer->create_tag(undef,'font',"Sans Italic 12",'foreground','#680000');#,'background','#000000');
  $buffer->insert_with_tags($buffer->get_end_iter,"<$command_num>\n",$text_tag);
  $buffer->insert ($buffer->get_end_iter,"$cmd\n");
  $view->scroll_mark_onscreen($textmark);

  add_to_history(($wed_arr->[0])->get_model,$cmd);

  print "\n";
  print "CMD     :  $cmd\n";
}

#add $cmd to the history of commands
sub add_to_history{
  my($model,$text) = @_;

  my $iter = $model->get_iter_first;
  if(defined($text) && ($text !~ m/^\s*$/)){
    my $exist=0;
    while (defined($iter)){

      my @l = $model->get($iter);
      print @l;
      if($l[0] eq $text){
        $exist=1
      }
      # /* Make iter point to the next row in the list store */
      $iter = $model->iter_next($iter);
    }
    if(!$exist){
      $model->set($model->append,0,$text);
    }
  }
}
$window->show_all;
Gtk2->main;
