#!/usr/bin/perl -w

# --------------------------------------------------

# Modules necessaires
use strict;
use Gtk2 '-init';

# Constantes globales
use constant false => 0;
use constant true => 1;

# Variables globales
my $FilesAdd = false;

# --------------------------------------------------

# Creation de la fenetre
my $window = Gtk2::Window->new;
$window->set_title("pct-interface");
$window->set_border_width(2);
#$window->set_default_size(300, 400);
$window->signal_connect(destroy => \&CloseAppWindow);

# VBox principale
my $vbox = Gtk2::VBox->new(false, 6);
$window->add($vbox);

# --------------------------------------------------

# Titre de la fenetre
my $title = Gtk2::Label->new("PCT-Tools (GPL)");
my $author = Gtk2::Label->new("Author : Tastalian");
$vbox->pack_start($title, false, true, 0);
$vbox->pack_start($author, false, true, 0);

# --------------------------------------------------

# files : frame
my $files_frame = Gtk2::Frame->new("files");
$files_frame->set_border_width(2);
$vbox->pack_start($files_frame, false, true, 0);

# files : hbox
my $files_hbox = Gtk2::HBox->new(false, 6);
$files_hbox->set_border_width(2);
$files_frame->add($files_hbox);

# files : find box
my $files_selection = Gtk2::FileSelection->new("Select files...");
$files_selection->ok_button->signal_connect(clicked => \&GetAppFiles);
$files_selection->show_fileop_buttons;
$files_selection->set_select_multiple(true);

# files : hbox : url
my $files_entry = Gtk2::Entry->new;
$files_hbox->pack_start($files_entry, false, true, 0);

# files : hbox : bouton parcourir
my $files_button_find = Gtk2::Button->new_from_stock("gtk-find");
$files_button_find->signal_connect(clicked => sub {
    $FilesAdd = false;
    $files_selection->run;
    $files_selection->hide;
});

# files : hbox : bouton ajouter
my $files_button_add = Gtk2::Button->new_from_stock("gtk-add");
$files_button_add->signal_connect(clicked => sub {
    $FilesAdd = true;
    $files_selection->run;
    $files_selection->hide;
});

# files : hbox : pack starting
$files_hbox->pack_start($files_button_find, false, true, 0);
$files_hbox->pack_start($files_button_add, false, true, 0);

# --------------------------------------------------

# pct-convert : frame
my $convert_frame = Gtk2::Frame->new("pct-convert");
$convert_frame->set_border_width(2);
$vbox->pack_start($convert_frame, false, true, 0);

# pct-convert : vbox
my $convert_vbox = Gtk2::VBox->new(false, 6);
$convert_vbox->set_border_width(2);
$convert_frame->add($convert_vbox);

# pct-convert : hbox
my $convert_hbox = Gtk2::HBox->new(false, 6);
$convert_hbox->set_border_width(2);

# pct-convert : command entry
my $convert_cmd = Gtk2::Label->new;
$convert_cmd->set_line_wrap(true);

# pct-convert : bouton
my $convert_active = Gtk2::CheckButton->new("Apply pct-convert");
$convert_active->signal_connect(clicked => sub {
  if ($convert_active->get_active) {
    $convert_hbox->show_all;
    $convert_cmd->show;
  } else {
    $convert_hbox->hide_all;
    $convert_cmd->hide;
  }
});

# pct-convert : vbox : pack starting
$convert_vbox->pack_start($convert_active, false, true, 0);
$convert_vbox->pack_start($convert_hbox, false, true, 0);

# --------------------------------------------------

# pct-mono : frame
my $mono_frame = Gtk2::Frame->new("pct-mono");
$mono_frame->set_border_width(2);
$vbox->pack_start($mono_frame, false, true, 0);

# pct-mono : vbox
my $mono_vbox = Gtk2::VBox->new(false, 6);
$mono_vbox->set_border_width(2);
$mono_frame->add($mono_vbox);

# pct-mono : hbox
my $mono_hbox = Gtk2::HBox->new(false, 6);
$mono_hbox->set_border_width(2);

# pct-mono : command entry
my $mono_cmd = Gtk2::Label->new;
$mono_cmd->set_line_wrap(true);

# pct-mono : bouton
my $mono_active = Gtk2::CheckButton->new("Apply pct-mono");
$mono_active->signal_connect(clicked => sub {
  if ($mono_active->get_active) {
    $mono_hbox->show_all;
    $mono_cmd->show;
  } else {
    $mono_hbox->hide_all;
    $mono_cmd->hide;
  }
});

# pct-mono : vbox : pack starting
$mono_vbox->pack_start($mono_active, false, true, 0);
$mono_vbox->pack_start($mono_hbox, false, true, 0);

# --------------------------------------------------

# pct-resize : frame
my $resize_frame = Gtk2::Frame->new("pct-resize");
$resize_frame->set_border_width(2);
$vbox->pack_start($resize_frame, false, true, 0);

# pct-resize : vbox
my $resize_vbox = Gtk2::VBox->new(false, 6);
$resize_vbox->set_border_width(2);
$resize_frame->add($resize_vbox);

# pct-resize : hbox
my $resize_hbox = Gtk2::HBox->new(false, 6);
$resize_hbox->set_border_width(2);

# pct-resize : command entry
my $resize_cmd = Gtk2::Label->new("Enter resizing coefficients.");
$resize_cmd->set_line_wrap(true);

# pct-resize : bouton
my $resize_active = Gtk2::CheckButton->new("Apply pct-resize");
$resize_active->signal_connect(clicked => sub {
  if ($resize_active->get_active) {
    $resize_hbox->show_all; 
    $resize_cmd->show;
  } else {
    $resize_hbox->hide_all;
    $resize_cmd->hide;
  }
});

# pct-resize : hbox : Entrys
my $resize_x = Gtk2::Entry->new_with_max_length(10);
my $resize_y = Gtk2::Entry->new_with_max_length(10);
$resize_x->set_size_request(40,20);
$resize_y->set_size_request(40,20);
$resize_x->signal_connect("activate" => \&UpdateAppWindow);
$resize_y->signal_connect("activate" => \&UpdateAppWindow);

# pct-resize : hbox : labels & pack starting
my $resize_label_x = Gtk2::Label->new("px / x");
my $resize_label_y = Gtk2::Label->new("px / y");
$resize_hbox->pack_start($resize_x, true, true, 0);
$resize_hbox->pack_start($resize_label_x, true, true, 0);
$resize_hbox->pack_start($resize_y, true, true, 0);
$resize_hbox->pack_start($resize_label_y, true, true, 0);

# pct-resize : vbox : pack starting
$resize_vbox->pack_start($resize_active, false, true, 0);
$resize_vbox->pack_start($resize_hbox, false, true, 0);
$resize_vbox->pack_start($resize_cmd, false, true, 0);

# --------------------------------------------------

# Barre d'outils
my $tools_hbox = Gtk2::HBox->new(false, 6);
$vbox->pack_start($tools_hbox, false, true, 0);

# Barre d'outils : 'a propos'
my $tools_about = Gtk2::Button->new("About");
$tools_about->signal_connect(clicked => \&AboutAppWindow);
$tools_hbox->pack_start($tools_about, false, true, 0);

# Barre d'outils : lancer
my $tools_launch = Gtk2::Button->new("Launch");
$tools_launch->signal_connect(clicked => \&RunSystemApp);
$tools_hbox->pack_start($tools_launch, false, true, 0);

# Barre d'outils : quitter
my $tools_quit = Gtk2::Button->new_from_stock("gtk-quit");
$tools_quit->signal_connect(clicked => \&CloseAppWindow);
$tools_hbox->pack_start($tools_quit, false, true, 0);

# --------------------------------------------------

# Lien vers le site officiel :)
my $url = Gtk2::Label->new("http://tastalian.free.fr/");
$vbox->pack_start($url, false, true, 0);

# --------------------------------------------------

# Affichage du contenu
$window->show_all;

# Cacher par defaut :
$convert_hbox->hide_all;
#$convert_cmd->hide;
$mono_hbox->hide_all;
#$mono_cmd->hide;
$resize_hbox->hide_all;
$resize_cmd->hide;

# Boucle principale
Gtk2->main;

# --------------------------------------------------

# A propos...
sub AboutAppWindow
{
  # Nouvelle boite de dialogue
  my $dialog = Gtk2::MessageDialog->new($window,
    [qw/modal destroy-with-parent/],
    'info', GTK_BUTTONS_OK =>
    "PCT-Interface : Interface for PCT-Tools\n".
    "Author: Tastalian (aka Stephane Caron)\n".
    "Official Website : http://tastalian.free.fr");

  # Affichage et fermeture
  $dialog->run;
  $dialog->destroy;
}

# --------------------------------------------------

# Fonction de fermeture
sub CloseAppWindow
{
  Gtk2->main_quit;
  return false;
}

# --------------------------------------------------

# Mise-a-jour
sub UpdateAppWindow
{
  # Mise-a-jour de la commande pour pct-resize
  $resize_cmd->set_markup("<b>pct-resize</b> ".
    "<span foreground='#ff0000'>".$resize_x->get_text."</span> ".
    "<span foreground='#ff0000'>".$resize_y->get_text."</span> ".
    "<i>[...]</i>"
  );
}

# --------------------------------------------------

# Lire les fichiers
sub GetAppFiles
{
  # Remise a nul si necessaire
  if ($FilesAdd == false) {
    $files_entry->set_text("");
  }

  # Lecture des fichiers selectionnes
  foreach my $file ($files_selection->get_selections) {
    $files_entry->append_text($file." ");
  }

  # Mise-a-jour du contenu
  UpdateAppWindow;
}

# --------------------------------------------------

# Lancer les operations
sub RunSystemApp
{
  # Application de pct-resize
  if ($resize_active->get_active) {
    system("pct-resize ".
      $resize_x->get_text." ".
      $resize_y->get_text." ".
      $files_entry->get_text."\n"
    );
  }
}

# --------------------------------------------------
