#! perl -w
#***********************************************************************************
# fileconv An example of graphical interface
# version 1.0
# Author: JL BICQUELET
# Date : 8 may 2008
#***********************************************************************************
# use strict;
use Tk;
use Tk::FileSelect;

#===================================================================================
# graphic interface
#===================================================================================
  my $w_top = new MainWindow;
  $w_top->configure(-title => "file convert v 1.0");
  my $w_file_in;
  my $w_file_out;

  my $w_frame1 = $w_top->Frame;

  $w_frame1->Label(-text => "Input file: " )->pack(-side => 'left');

  my $w_input_entry = $w_frame1->Entry(-relief => 'sunken', -width=>30);
  $w_input_entry->pack(-padx => 10, -pady => 10)->pack(-side => 'left');
  $w_frame1->Button(-text => "Browse ..", -command => \&browse_input_cb )->pack(-side => 'left');

  $w_frame1->pack;

  my $w_frame2 = $w_top->Frame;

  $w_frame2->Label(-text => "Output file:" )->pack(-side => 'left');

  my $w_output_entry = $w_frame2->Entry(-relief => 'sunken', -width=>30);
  $w_output_entry->pack(-padx => 10, -pady => 10)->pack(-side => 'left');
  $w_frame2->Button(-text => "Browse ..", -command => \&browse_output_cb )->pack(-side => 'left');

  $w_frame2->pack;

  my $w_frame3 = $w_top->Frame(-relief =>'ridge', -bd => 3);

  $w_frame3->Button(-text => "name",-command => \&name_cb )->pack(-side => 'left');
  $w_frame3->Button(-text => "convert",-command => \&convert_cb )->pack(-side => 'left');
  $w_frame3->Button(-text => "help",-command => \&help_cb )->pack(-side => 'left');
  $w_frame3->Button(-text => "about",-command => \&about_cb )->pack(-side => 'left');
  $w_frame3->Button(-text => "quit",-command => sub { exit 0; } )->pack(-side => 'left');

  $w_frame3->pack(-side => 'left',-padx => 16,-pady => 6, -ipadx => 1 , -ipady => 1);

  MainLoop;

#===================================================================================
# graphic interface extension
#===================================================================================

#===================================================================================
# call backs for action
#===================================================================================

#----------------------------------------------------------------------------------
# get input filename
#----------------------------------------------------------------------------------
sub browse_input_cb
{
  $w_file_in = $w_top->FileSelect;
  $w_input_entry->delete(0,'end');
  $w_input_entry->insert(0,$w_file_in->Show);

  &name_cb();
}

#----------------------------------------------------------------------------------
# get output filename
#----------------------------------------------------------------------------------
sub browse_output_cb
{
  $w_file_out = $w_top->FileSelect;
  $w_output_entry->delete(0,'end');
  $w_output_entry->insert(0,$w_file_out->Show);
}

#----------------------------------------------------------------------------------
# deduce name of the output filename from the input filename
#----------------------------------------------------------------------------------
sub name_cb
{
  $name=$w_input_entry->get();

  # passage de txt en html
  $name=~s/\.pl/.html/;
  $w_output_entry->delete(0,'end');
  $w_output_entry->insert(0,$name);
}

#----------------------------------------------------------------------------------
# about information dialog
#----------------------------------------------------------------------------------
sub about_cb
{
  my $response = $w_top -> messageBox(-message=>"Fileconv v 1.0\nA file converter\n(c) 2008 JL BICQUELET",-type=>'ok',-icon=>'question');
}

#----------------------------------------------------------------------------------
# convert the file
#----------------------------------------------------------------------------------
sub convert_cb
{
}

#----------------------------------------------------------------------------------
# help for fileconv
#----------------------------------------------------------------------------------
sub help_cb
{
   $w_top->Dialog(
    -title   => 'Help',
    -text    => "Convert a file txt into html file",
    -buttons => ['Dismiss'])->Show;
}
